Skip to content

Commit c14746a

Browse files
authored
PKG -- [FCL-WC] Deeplink on authz if pre-authz is not a WC/RPC service (#1999)
1 parent 21dc277 commit c14746a

File tree

8 files changed

+63
-23
lines changed

8 files changed

+63
-23
lines changed

.changeset/shaggy-snakes-vanish.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@onflow/fcl-core": patch
3+
"@onflow/fcl-wc": patch
4+
---
5+
6+
Improve deeplinking for WC/RPC wallets using non-WC/RPC pre-authz services

packages/fcl-core/src/current-user/exec-service/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ export const execStrategy = async ({
1616
abortSignal,
1717
customRpc,
1818
opts,
19+
user,
1920
}) => {
2021
const strategy = getServiceRegistry().getStrategy(service.method)
21-
return strategy({service, body, config, abortSignal, customRpc, opts})
22+
return strategy({service, body, config, abortSignal, customRpc, opts, user})
2223
}
2324

2425
export async function execService({
@@ -29,6 +30,7 @@ export async function execService({
2930
platform,
3031
abortSignal = new AbortController().signal,
3132
execStrategy: _execStrategy,
33+
user,
3234
}) {
3335
// Notify the developer if WalletConnect is not enabled
3436
checkWalletConnectEnabled()
@@ -53,6 +55,7 @@ export async function execService({
5355
body: msg,
5456
config: execConfig,
5557
opts,
58+
user,
5659
abortSignal,
5760
})
5861

packages/fcl-core/src/current-user/index.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {execService} from "./exec-service"
1212
import {normalizeCompositeSignature} from "../normalizers/service/composite-signature"
1313
import {getDiscoveryService, makeDiscoveryServices} from "../discovery"
1414
import {getServiceRegistry} from "./exec-service/plugins"
15-
import {isMobile} from "../utils"
1615

1716
/**
1817
* @typedef {import("@onflow/typedefs").CurrentUser} CurrentUser
@@ -160,6 +159,7 @@ const getAuthenticate =
160159
* @description - Authenticate a user
161160
* @param {object} [opts] - Options
162161
* @param {object} [opts.service] - Optional service to use for authentication
162+
* @param {object} [opts.user] - Optional user object
163163
* @param {boolean} [opts.redir] - Optional redirect flag
164164
* @returns
165165
*/
@@ -189,6 +189,7 @@ const getAuthenticate =
189189
msg: accountProofData,
190190
opts,
191191
platform,
192+
user,
192193
})
193194
send(NAME, SET_CURRENT_USER, await buildUser(response))
194195
} catch (error) {
@@ -224,6 +225,7 @@ const getAuthenticate =
224225
opts,
225226
platform,
226227
execStrategy: discovery?.execStrategy,
228+
user,
227229
})
228230

229231
send(NAME, SET_CURRENT_USER, await buildUser(response))
@@ -258,7 +260,7 @@ const normalizePreAuthzResponse = authz => ({
258260

259261
const getResolvePreAuthz =
260262
({platform}) =>
261-
authz => {
263+
(authz, {user}) => {
262264
const resp = normalizePreAuthzResponse(authz)
263265
const axs = []
264266

@@ -275,9 +277,7 @@ const getResolvePreAuthz =
275277
service: az,
276278
msg: signable,
277279
platform,
278-
opts: {
279-
initiatedByPreAuthz: true,
280-
},
280+
user,
281281
})
282282
},
283283
role: {
@@ -319,7 +319,11 @@ const getAuthorization =
319319
service: preAuthz,
320320
msg: preSignable,
321321
platform,
322-
})
322+
user,
323+
}),
324+
{
325+
user,
326+
}
323327
)
324328
if (authz) {
325329
return {
@@ -339,6 +343,7 @@ const getAuthorization =
339343
includeOlderJsonRpcCall: true,
340344
},
341345
platform,
346+
user,
342347
})
343348
)
344349
},
@@ -446,6 +451,7 @@ const getSignUserMessage =
446451
service: signingService,
447452
msg: makeSignable(msg),
448453
platform,
454+
user,
449455
})
450456
if (Array.isArray(response)) {
451457
return response.map(compSigs => normalizeCompositeSignature(compSigs))

packages/fcl-wc/src/constants.ts

+3
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ export enum REQUEST_TYPES {
99
SESSION_REQUEST = "session_proposal",
1010
SIGNING_REQUEST = "signing_request",
1111
}
12+
13+
export const SERVICE_PLUGIN_NAME = "fcl-plugin-service-walletconnect"
14+
export const WC_SERVICE_METHOD = "WC/RPC"

packages/fcl-wc/src/fcl-wc.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import SignClient from "@walletconnect/sign-client"
33
import {invariant} from "@onflow/util-invariant"
44
import {LEVELS, log} from "@onflow/util-logger"
55
export {getSdkError} from "@walletconnect/utils"
6-
import {SERVICE_PLUGIN_NAME, makeServicePlugin} from "./service"
6+
import {makeServicePlugin} from "./service"
77
import {CoreTypes} from "@walletconnect/types"
88

99
export interface FclWalletConnectConfig {
@@ -131,4 +131,4 @@ export async function getSignClient() {
131131
})
132132
}
133133

134-
export {SERVICE_PLUGIN_NAME}
134+
export {SERVICE_PLUGIN_NAME} from "./constants"

packages/fcl-wc/src/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export {SERVICE_PLUGIN_NAME, WC_SERVICE_METHOD} from "./service"
21
export {init, initLazy, getSignClient} from "./fcl-wc"
32
export {createSessionProposal, request} from "./session"
4-
export {FLOW_METHODS} from "./constants"
3+
export {FLOW_METHODS, SERVICE_PLUGIN_NAME, WC_SERVICE_METHOD} from "./constants"

packages/fcl-wc/src/service.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import {invariant} from "@onflow/util-invariant"
22
import {log, LEVELS} from "@onflow/util-logger"
3-
import {isMobile, openDeeplink} from "./utils"
4-
import {FLOW_METHODS, REQUEST_TYPES} from "./constants"
3+
import {isMobile, openDeeplink, shouldDeepLink} from "./utils"
4+
import {
5+
REQUEST_TYPES,
6+
SERVICE_PLUGIN_NAME,
7+
WC_SERVICE_METHOD,
8+
} from "./constants"
59
import {SignClient} from "@walletconnect/sign-client/dist/types/client"
6-
import {createSessionProposal, makeSessionData, request} from "./session"
10+
import {createSessionProposal, request} from "./session"
711
import {ModalCtrlState} from "@walletconnect/modal-core/dist/_types/src/types/controllerTypes"
812

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

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

13-
export const SERVICE_PLUGIN_NAME = "fcl-plugin-service-walletconnect"
14-
export const WC_SERVICE_METHOD = "WC/RPC"
15-
1617
export const makeServicePlugin = (
1718
client: Promise<SignClient | null>,
1819
opts: {
@@ -53,11 +54,13 @@ const makeExec = (
5354
body,
5455
opts,
5556
abortSignal,
57+
user,
5658
}: {
5759
service: any
5860
body: any
5961
opts: any
6062
abortSignal?: AbortSignal
63+
user: any
6164
}) => {
6265
const client = await clientPromise
6366
invariant(!!client, "WalletConnect is not initialized")
@@ -107,11 +110,8 @@ const makeExec = (
107110
})
108111
}
109112

110-
if (
111-
isMobile() &&
112-
method !== FLOW_METHODS.FLOW_AUTHN &&
113-
!(method === FLOW_METHODS.FLOW_AUTHZ && opts.initiatedByPreAuthz)
114-
) {
113+
// Deeplink to the wallet app if necessary
114+
if (shouldDeepLink({service, user})) {
115115
openDeeplink(appLink)
116116
}
117117

packages/fcl-wc/src/utils.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import {log, LEVELS} from "@onflow/util-logger"
2-
import {invariant} from "@onflow/util-invariant"
32
import * as fclCore from "@onflow/fcl-core"
3+
import {FLOW_METHODS, WC_SERVICE_METHOD} from "./constants"
4+
import {Service} from "@onflow/typedefs"
5+
6+
const PRE_AUTHZ_SERVICE_TYPE = "pre-authz"
47

58
const makeFlowServicesFromWallets = (wallets: any[]) => {
69
return Object.values(wallets)
@@ -94,3 +97,23 @@ export function openDeeplink(url: string) {
9497
window.open(url, "_blank")
9598
}
9699
}
100+
101+
export function shouldDeepLink({service, user}: {service: Service; user: any}) {
102+
// Only deeplink on mobile
103+
if (!isMobile()) return false
104+
105+
// If this is an authn request, the user has already been deeplinked by connectWc
106+
if (service.endpoint === FLOW_METHODS.FLOW_AUTHN) return false
107+
108+
// If there was a pre-authz WC request, the user has already been deeplinked
109+
if (
110+
service.endpoint === FLOW_METHODS.FLOW_AUTHZ &&
111+
user?.services?.find(
112+
(s: Service) =>
113+
s.method === WC_SERVICE_METHOD && s.type === PRE_AUTHZ_SERVICE_TYPE
114+
)
115+
)
116+
return false
117+
118+
return true
119+
}

0 commit comments

Comments
 (0)