From f614de09bd61687feb81ceff42a915abbc3261cb Mon Sep 17 00:00:00 2001 From: Alex Stelea Date: Fri, 7 Jun 2024 13:57:43 +0100 Subject: [PATCH] refactor: encode wallet interaction in deep link url --- package-lock.json | 9 ++++ packages/dapp-toolkit/package.json | 1 + .../radix-connect-relay/helpers/base64url.ts | 5 +++ .../radix-connect-relay.module.ts | 42 ++++++++----------- packages/dapp-toolkit/src/polyfills.ts | 5 +++ .../dapp-toolkit/src/radix-dapp-toolkit.ts | 4 ++ 6 files changed, 42 insertions(+), 24 deletions(-) create mode 100644 packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/helpers/base64url.ts create mode 100644 packages/dapp-toolkit/src/polyfills.ts diff --git a/package-lock.json b/package-lock.json index fce98d39..ed207858 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13756,6 +13756,14 @@ } ] }, + "node_modules/base64url": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/base64url/-/base64url-3.0.1.tgz", + "integrity": "sha512-ir1UPr3dkwexU7FdV8qBBbNDRUhMmIekYMFZfi+C/sLNnRESKPl23nB9b2pltqfOQNnGzsDdId90AEtG5tCx4A==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/before-after-hook": { "version": "2.2.3", "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.3.tgz", @@ -33505,6 +33513,7 @@ "license": "SEE LICENSE IN RADIX-SOFTWARE-EULA", "dependencies": { "@noble/curves": "^1.4.0", + "base64url": "^3.0.1", "bowser": "^2.11.0", "buffer": "^6.0.3", "immer": "^10.0.4", diff --git a/packages/dapp-toolkit/package.json b/packages/dapp-toolkit/package.json index 1da59b7a..b9695417 100644 --- a/packages/dapp-toolkit/package.json +++ b/packages/dapp-toolkit/package.json @@ -60,6 +60,7 @@ }, "dependencies": { "@noble/curves": "^1.4.0", + "base64url": "^3.0.1", "bowser": "^2.11.0", "buffer": "^6.0.3", "immer": "^10.0.4", diff --git a/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/helpers/base64url.ts b/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/helpers/base64url.ts new file mode 100644 index 00000000..8644dbdb --- /dev/null +++ b/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/helpers/base64url.ts @@ -0,0 +1,5 @@ +import base64url from 'base64url' + +export const base64urlEncode = >( + value: T, +): string => base64url.encode(Buffer.from(JSON.stringify(value))) diff --git a/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/radix-connect-relay.module.ts b/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/radix-connect-relay.module.ts index e2c7c722..5bedde45 100644 --- a/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/radix-connect-relay.module.ts +++ b/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/radix-connect-relay.module.ts @@ -23,6 +23,7 @@ import { RadixConnectRelayApiService } from './radix-connect-relay-api.service' import { RequestItem } from 'radix-connect-common' import type { TransportProvider } from '../../../../_types' import { RcfmPageModule, RcfmPageState } from './rcfm-page.module' +import { base64urlEncode } from './helpers/base64url' export type RadixConnectRelayModule = ReturnType export const RadixConnectRelayModule = (input: { @@ -119,16 +120,11 @@ export const RadixConnectRelayModule = (input: { .andThen((pendingItem) => requestItemModule .patch(walletInteraction.interactionId, { sentToWallet: true }) - .andThen(() => - radixConnectRelayApiService.sendRequest( - session, - pendingItem.walletInteraction, - ), - ) .andThen(() => deepLinkModule.deepLinkToWallet({ sessionId: session.sessionId, interactionId: pendingItem.interactionId, + walletInteraction: base64urlEncode(walletInteraction), }), ), ) @@ -204,27 +200,25 @@ export const RadixConnectRelayModule = (input: { pendingItem?: RequestItem }) => pendingItem - ? radixConnectRelayApiService - .sendRequest(activeSession, pendingItem.walletInteraction) - .andThen(() => - requestItemModule - .patch(pendingItem.interactionId, { - sentToWallet: true, - }) - .mapErr(() => - SdkError( - 'FailedToUpdateRequestItem', - pendingItem.interactionId, - ), - ) - .map(() => pendingItem.interactionId), + ? requestItemModule + .patch(pendingItem.interactionId, { + sentToWallet: true, + }) + .mapErr(() => + SdkError( + 'FailedToUpdateRequestItem', + pendingItem.interactionId, + ), ) - .map((interactionId) => { + .andThen(() => deepLinkModule.deepLinkToWallet({ sessionId, - interactionId, - }) - }) + interactionId: pendingItem.walletInteraction.interactionId, + walletInteraction: base64urlEncode( + pendingItem.walletInteraction, + ), + }), + ) : ok(pendingItem), ) diff --git a/packages/dapp-toolkit/src/polyfills.ts b/packages/dapp-toolkit/src/polyfills.ts new file mode 100644 index 00000000..2e956685 --- /dev/null +++ b/packages/dapp-toolkit/src/polyfills.ts @@ -0,0 +1,5 @@ +import { Buffer } from 'buffer' + +export default () => { + if (!globalThis.Buffer) globalThis.Buffer = Buffer +} diff --git a/packages/dapp-toolkit/src/radix-dapp-toolkit.ts b/packages/dapp-toolkit/src/radix-dapp-toolkit.ts index 10e4cd1c..ced31d94 100644 --- a/packages/dapp-toolkit/src/radix-dapp-toolkit.ts +++ b/packages/dapp-toolkit/src/radix-dapp-toolkit.ts @@ -1,3 +1,7 @@ +import polyfills from './polyfills' + +polyfills() + import type { ButtonApi, GatewayApiClientConfig,