Skip to content

Commit

Permalink
PKG -- [FCL-WC] Deeplink on authz if pre-authz is not a WC/RPC service (
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink authored Oct 31, 2024
1 parent 21dc277 commit c14746a
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 23 deletions.
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
5 changes: 4 additions & 1 deletion packages/fcl-core/src/current-user/exec-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ export const execStrategy = async ({
abortSignal,
customRpc,
opts,
user,
}) => {
const strategy = getServiceRegistry().getStrategy(service.method)
return strategy({service, body, config, abortSignal, customRpc, opts})
return strategy({service, body, config, abortSignal, customRpc, opts, user})
}

export async function execService({
Expand All @@ -29,6 +30,7 @@ export async function execService({
platform,
abortSignal = new AbortController().signal,
execStrategy: _execStrategy,
user,
}) {
// Notify the developer if WalletConnect is not enabled
checkWalletConnectEnabled()
Expand All @@ -53,6 +55,7 @@ export async function execService({
body: msg,
config: execConfig,
opts,
user,
abortSignal,
})

Expand Down
18 changes: 12 additions & 6 deletions packages/fcl-core/src/current-user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {execService} from "./exec-service"
import {normalizeCompositeSignature} from "../normalizers/service/composite-signature"
import {getDiscoveryService, makeDiscoveryServices} from "../discovery"
import {getServiceRegistry} from "./exec-service/plugins"
import {isMobile} from "../utils"

/**
* @typedef {import("@onflow/typedefs").CurrentUser} CurrentUser
Expand Down Expand Up @@ -160,6 +159,7 @@ const getAuthenticate =
* @description - Authenticate a user
* @param {object} [opts] - Options
* @param {object} [opts.service] - Optional service to use for authentication
* @param {object} [opts.user] - Optional user object
* @param {boolean} [opts.redir] - Optional redirect flag
* @returns
*/
Expand Down Expand Up @@ -189,6 +189,7 @@ const getAuthenticate =
msg: accountProofData,
opts,
platform,
user,
})
send(NAME, SET_CURRENT_USER, await buildUser(response))
} catch (error) {
Expand Down Expand Up @@ -224,6 +225,7 @@ const getAuthenticate =
opts,
platform,
execStrategy: discovery?.execStrategy,
user,
})

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

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

Expand All @@ -275,9 +277,7 @@ const getResolvePreAuthz =
service: az,
msg: signable,
platform,
opts: {
initiatedByPreAuthz: true,
},
user,
})
},
role: {
Expand Down Expand Up @@ -319,7 +319,11 @@ const getAuthorization =
service: preAuthz,
msg: preSignable,
platform,
})
user,
}),
{
user,
}
)
if (authz) {
return {
Expand All @@ -339,6 +343,7 @@ const getAuthorization =
includeOlderJsonRpcCall: true,
},
platform,
user,
})
)
},
Expand Down Expand Up @@ -446,6 +451,7 @@ const getSignUserMessage =
service: signingService,
msg: makeSignable(msg),
platform,
user,
})
if (Array.isArray(response)) {
return response.map(compSigs => normalizeCompositeSignature(compSigs))
Expand Down
3 changes: 3 additions & 0 deletions packages/fcl-wc/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ export enum REQUEST_TYPES {
SESSION_REQUEST = "session_proposal",
SIGNING_REQUEST = "signing_request",
}

export const SERVICE_PLUGIN_NAME = "fcl-plugin-service-walletconnect"
export const WC_SERVICE_METHOD = "WC/RPC"
4 changes: 2 additions & 2 deletions packages/fcl-wc/src/fcl-wc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SignClient from "@walletconnect/sign-client"
import {invariant} from "@onflow/util-invariant"
import {LEVELS, log} from "@onflow/util-logger"
export {getSdkError} from "@walletconnect/utils"
import {SERVICE_PLUGIN_NAME, makeServicePlugin} from "./service"
import {makeServicePlugin} from "./service"
import {CoreTypes} from "@walletconnect/types"

export interface FclWalletConnectConfig {
Expand Down Expand Up @@ -131,4 +131,4 @@ export async function getSignClient() {
})
}

export {SERVICE_PLUGIN_NAME}
export {SERVICE_PLUGIN_NAME} from "./constants"
3 changes: 1 addition & 2 deletions packages/fcl-wc/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export {SERVICE_PLUGIN_NAME, WC_SERVICE_METHOD} from "./service"
export {init, initLazy, getSignClient} from "./fcl-wc"
export {createSessionProposal, request} from "./session"
export {FLOW_METHODS} from "./constants"
export {FLOW_METHODS, SERVICE_PLUGIN_NAME, WC_SERVICE_METHOD} from "./constants"
22 changes: 11 additions & 11 deletions packages/fcl-wc/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
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,
SERVICE_PLUGIN_NAME,
WC_SERVICE_METHOD,
} from "./constants"
import {SignClient} from "@walletconnect/sign-client/dist/types/client"
import {createSessionProposal, makeSessionData, request} from "./session"
import {createSessionProposal, request} from "./session"
import {ModalCtrlState} from "@walletconnect/modal-core/dist/_types/src/types/controllerTypes"

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"

export const makeServicePlugin = (
client: Promise<SignClient | null>,
opts: {
Expand Down Expand Up @@ -53,11 +54,13 @@ const makeExec = (
body,
opts,
abortSignal,
user,
}: {
service: any
body: any
opts: any
abortSignal?: AbortSignal
user: any
}) => {
const client = await clientPromise
invariant(!!client, "WalletConnect is not initialized")
Expand Down Expand Up @@ -107,11 +110,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
25 changes: 24 additions & 1 deletion 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, WC_SERVICE_METHOD} from "./constants"
import {Service} from "@onflow/typedefs"

const PRE_AUTHZ_SERVICE_TYPE = "pre-authz"

const makeFlowServicesFromWallets = (wallets: any[]) => {
return Object.values(wallets)
Expand Down Expand Up @@ -94,3 +97,23 @@ 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.endpoint === FLOW_METHODS.FLOW_AUTHN) return false

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

return true
}

0 comments on commit c14746a

Please sign in to comment.