Skip to content

Commit

Permalink
Merge branch 'jribbink/non-wc-deeplink' into jribbink/wc-modal
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Oct 30, 2024
2 parents 02f1f35 + bb42ce5 commit 34630d0
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 46 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-bikes-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/fcl-wc": patch
---

Fix WalletConnectModal close detection
6 changes: 6 additions & 0 deletions .changeset/shaggy-snakes-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@onflow/fcl-core": patch
"@onflow/fcl-wc": patch
---

Improve deeplinking for WC/RPC wallets using non-WC/RPC pre-authz services
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/fcl-core/src/current-user/exec-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export async function execService({
abortSignal = new AbortController().signal,
user,
execStrategy: _execStrategy,
user,
}) {
// Notify the developer if WalletConnect is not enabled
checkWalletConnectEnabled()
Expand All @@ -56,7 +57,6 @@ export async function execService({
config: execConfig,
abortSignal,
user,
opts,
})

if (res.status === "REDIRECT") {
Expand Down
14 changes: 8 additions & 6 deletions packages/fcl-core/src/current-user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ const getAuthenticate =
opts,
platform,
execStrategy: discovery?.execStrategy,
user,
})

send(NAME, SET_CURRENT_USER, await buildUser(response))
Expand Down Expand Up @@ -258,8 +259,8 @@ const normalizePreAuthzResponse = authz => ({
})

const getResolvePreAuthz =
({platform, user}) =>
authz => {
({platform}) =>
(authz, {user}) => {
const resp = normalizePreAuthzResponse(authz)
const axs = []

Expand All @@ -276,9 +277,6 @@ const getResolvePreAuthz =
service: az,
msg: signable,
platform,
opts: {
initiatedByPreAuthz: true,
},
user,
})
},
Expand Down Expand Up @@ -322,7 +320,11 @@ const getAuthorization =
msg: preSignable,
user,
platform,
})
user,
}),
{
user,
}
)
if (authz) {
return {
Expand Down
9 changes: 1 addition & 8 deletions packages/fcl-wc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
# @onflow/fcl-wc

## 6.0.0-alpha.2
## 5.4.1-alpha.1

### Patch Changes

- Updated dependencies [[`8a5f8e9874980c40c1feb3eac915c6e8570abbf3`](https://github.com/onflow/fcl-js/commit/8a5f8e9874980c40c1feb3eac915c6e8570abbf3)]:
- @onflow/fcl-core@1.13.0-alpha.2

## 6.0.0-alpha.1

### Patch Changes

- Updated dependencies [[`b9f49494d5b3faed1bc24005adc6ba312f653a21`](https://github.com/onflow/fcl-js/commit/b9f49494d5b3faed1bc24005adc6ba312f653a21)]:
- @onflow/fcl-core@1.13.0-alpha.1

## 5.4.1-alpha.0

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/fcl-wc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onflow/fcl-wc",
"version": "6.0.0-alpha.2",
"version": "5.4.1-alpha.1",
"description": "WalletConnect adapter for FCL",
"license": "Apache-2.0",
"author": "Dapper Labs <[email protected]>",
Expand Down
42 changes: 29 additions & 13 deletions packages/fcl-wc/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import {invariant} from "@onflow/util-invariant"
import {log, LEVELS} from "@onflow/util-logger"
import {isMobile, openDeeplink} from "./utils"
import {FLOW_METHODS, REQUEST_TYPES} from "./constants"
import {isMobile, openDeeplink, shouldDeepLink} from "./utils"
import {REQUEST_TYPES} from "./constants"
import {SignClient} from "@walletconnect/sign-client/dist/types/client"
import {createSessionProposal, request} from "./session"
import {showNotification} from "./ui/notifications"

import mobileIcon from "./ui/assets/mobile.png"
import {ModalCtrlState} from "@walletconnect/modal-core/dist/_types/src/types/controllerTypes"

type WalletConnectModalType =
typeof import("@walletconnect/modal").WalletConnectModal
type WalletConnectModalType = import("@walletconnect/modal").WalletConnectModal

type Constructor<T> = new (...args: any[]) => T

export const SERVICE_PLUGIN_NAME = "fcl-plugin-service-walletconnect"
export const WC_SERVICE_METHOD = "WC/RPC"
Expand Down Expand Up @@ -47,7 +49,7 @@ export const makeServicePlugin = (
const makeExec = (
clientPromise: Promise<SignClient | null>,
{wcRequestHook, pairingModalOverride}: any,
WalletConnectModal: Promise<WalletConnectModalType>
WalletConnectModal: Promise<Constructor<WalletConnectModalType>>
) => {
return async ({
service,
Expand Down Expand Up @@ -110,11 +112,8 @@ const makeExec = (
})
}

if (
isMobile() &&
method !== FLOW_METHODS.FLOW_AUTHN &&
!(method === FLOW_METHODS.FLOW_AUTHZ && opts.initiatedByPreAuthz)
) {
// Deeplink to the wallet app if necessary
if (shouldDeepLink({service, user})) {
openDeeplink(appLink)
}

Expand Down Expand Up @@ -148,7 +147,9 @@ const makeExec = (
}

// Connect to WalletConnect directly from the browser via deep link or WalletConnectModal
function connectWc(WalletConnectModal: Promise<WalletConnectModalType>) {
function connectWc(
WalletConnectModal: Promise<Constructor<WalletConnectModalType>>
) {
return async ({
service,
onClose,
Expand Down Expand Up @@ -177,7 +178,7 @@ function connectWc(WalletConnectModal: Promise<WalletConnectModalType>) {
)

let _uri: string | null = null,
walletConnectModal: any
walletConnectModal: WalletConnectModalType | null = null

try {
const {uri, approval} = await createSessionProposal({
Expand Down Expand Up @@ -206,7 +207,22 @@ function connectWc(WalletConnectModal: Promise<WalletConnectModalType>) {
walletConnectModal = new (await WalletConnectModal)({
projectId,
})
walletConnectModal.openModal({uri, onClose})

// Open WalletConnectModal
walletConnectModal.openModal({
uri,
onClose,
})

// Subscribe to modal state changes
const unsubscribeModal = walletConnectModal.subscribeModal(
(state: ModalCtrlState) => {
if (state.open === false) {
onClose?.()
unsubscribeModal()
}
}
)
} else {
pairingModalOverride(uri, onClose)
}
Expand Down
20 changes: 20 additions & 0 deletions packages/fcl-wc/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {log, LEVELS} from "@onflow/util-logger"
import {invariant} from "@onflow/util-invariant"
import * as fclCore from "@onflow/fcl-core"
import {FLOW_METHODS} from "./constants"
import {WC_SERVICE_METHOD} from "./service"
import {Service} from "@onflow/typedefs"

const makeFlowServicesFromWallets = (wallets: any[]) => {
return Object.values(wallets)
Expand Down Expand Up @@ -94,3 +97,20 @@ export function openDeeplink(url: string) {
window.open(url, "_blank")
}
}

export function shouldDeepLink({service, user}: {service: Service; user: any}) {
// Only deeplink on mobile
if (!isMobile()) return false

// If this is an authn request, the user has already been deeplinked by connectWc
if (service.method === FLOW_METHODS.FLOW_AUTHN) return false

// If there was a pre-authz WC request, the user has already been deeplinked
if (
service.method === FLOW_METHODS.FLOW_AUTHZ &&
user?.services?.find((s: Service) => s.method === WC_SERVICE_METHOD)
)
return false

return true
}
15 changes: 3 additions & 12 deletions packages/fcl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
# @onflow/fcl

## 1.12.4-alpha.2
## 1.12.4-alpha.3

### Patch Changes

- Updated dependencies [[`8a5f8e9874980c40c1feb3eac915c6e8570abbf3`](https://github.com/onflow/fcl-js/commit/8a5f8e9874980c40c1feb3eac915c6e8570abbf3)]:
- Updated dependencies:
- @onflow/fcl-core@1.13.0-alpha.2
- @onflow/fcl-wc@6.0.0-alpha.2

## 1.12.4-alpha.1

### Patch Changes

- Updated dependencies [[`b9f49494d5b3faed1bc24005adc6ba312f653a21`](https://github.com/onflow/fcl-js/commit/b9f49494d5b3faed1bc24005adc6ba312f653a21)]:
- @onflow/fcl-core@1.13.0-alpha.1
- @onflow/sdk@1.5.4-alpha.1
- @onflow/fcl-wc@6.0.0-alpha.1
- @onflow/fcl-wc@5.4.1-alpha.1

## 1.12.4-alpha.0

Expand Down
4 changes: 2 additions & 2 deletions packages/fcl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onflow/fcl",
"version": "1.12.4-alpha.2",
"version": "1.12.4-alpha.3",
"description": "Flow Client Library",
"license": "Apache-2.0",
"author": "Dapper Labs <[email protected]>",
Expand Down Expand Up @@ -50,7 +50,7 @@
"@babel/runtime": "^7.25.7",
"@onflow/config": "1.5.1-alpha.0",
"@onflow/fcl-core": "1.13.0-alpha.2",
"@onflow/fcl-wc": "6.0.0-alpha.2",
"@onflow/fcl-wc": "5.4.1-alpha.1",
"@onflow/interaction": "0.0.11",
"@onflow/rlp": "1.2.3-alpha.0",
"@onflow/sdk": "1.5.4-alpha.1",
Expand Down

0 comments on commit 34630d0

Please sign in to comment.