diff --git a/examples/simple-dapp/src/main.ts b/examples/simple-dapp/src/main.ts index 401fcaa8..ca6e273a 100644 --- a/examples/simple-dapp/src/main.ts +++ b/examples/simple-dapp/src/main.ts @@ -250,17 +250,16 @@ proofOfOwnershipRequest.onclick = async () => { return } - const result = await dAppToolkit.walletApi - .sendOneTimeRequest( - OneTimeDataRequestBuilder.accounts().exactly(1), - OneTimeDataRequestBuilder.proofOfOwnership() - .accounts(connectedAccounts.map((account) => account.address)) - .identity(connectedPersona.identityAddress), - ) - .map(() => 'success') - .unwrapOr('error') + const result = await dAppToolkit.walletApi.sendOneTimeRequest( + OneTimeDataRequestBuilder.accounts().exactly(1), + OneTimeDataRequestBuilder.proofOfOwnership() + .accounts(connectedAccounts.map((account) => account.address)) + .identity(connectedPersona.identityAddress), + ) + + console.log(result) - alert(result) + alert(`Result is ok: ${result.isOk()}`) } setInterval(() => { diff --git a/packages/dapp-toolkit/src/modules/wallet-request/encryption/encryption.module.ts b/packages/dapp-toolkit/src/modules/wallet-request/encryption/encryption.module.ts index 3de67e94..62e59689 100644 --- a/packages/dapp-toolkit/src/modules/wallet-request/encryption/encryption.module.ts +++ b/packages/dapp-toolkit/src/modules/wallet-request/encryption/encryption.module.ts @@ -61,7 +61,7 @@ export const EncryptionModule = () => { * * @param {Buffer} data - payload to be encrypted * @param {Buffer} encryptionKey - key used for encryption - * @param {Buffer} iv - optional initialization vector + * @param {Buffer=} iv - optional initialization vector * @returns encrypted data wrapped inside ResultAsync */ const encrypt = ( diff --git a/packages/dapp-toolkit/src/modules/wallet-request/request-resolver/request-resolver.module.ts b/packages/dapp-toolkit/src/modules/wallet-request/request-resolver/request-resolver.module.ts index 31c122ca..d7879455 100644 --- a/packages/dapp-toolkit/src/modules/wallet-request/request-resolver/request-resolver.module.ts +++ b/packages/dapp-toolkit/src/modules/wallet-request/request-resolver/request-resolver.module.ts @@ -1,5 +1,5 @@ import { err, ok, okAsync, ResultAsync } from 'neverthrow' -import type { Logger } from '../../../helpers' +import { validateWalletResponse, type Logger } from '../../../helpers' import type { WalletInteractionResponse } from '../../../schemas' import type { StorageModule } from '../../storage' import type { RequestItemModule } from '../request-items' @@ -67,13 +67,15 @@ export const RequestResolverModule = (input: { requestItemModule.patch(interactionId, { sentToWallet: true }) const addWalletResponses = (responses: WalletInteractionResponse[]) => - walletResponses.setItems( - responses.reduce>( - (acc, response) => { - acc[response.interactionId] = response - return acc - }, - {}, + ResultAsync.combine(responses.map(validateWalletResponse)).andThen(() => + walletResponses.setItems( + responses.reduce>( + (acc, response) => { + acc[response.interactionId] = response + return acc + }, + {}, + ), ), ) 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 c90eba7d..58fea336 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 @@ -11,7 +11,6 @@ import { Logger, isMobile, parseJSON } from '../../../../helpers' import { SdkError } from '../../../../error' import { DeepLinkModule } from './deep-link.module' import { IdentityModule } from '../../identity/identity.module' -import { RequestItemModule } from '../../request-items/request-item.module' import { StorageModule } from '../../../storage' import { Curve25519 } from '../../crypto' import { @@ -29,7 +28,6 @@ export const RadixConnectRelayModule = (input: { walletUrl: string dAppDefinitionAddress: string providers: { - requestItemModule: RequestItemModule storageModule: StorageModule requestResolverModule: RequestResolverModule encryptionModule?: EncryptionModule @@ -40,7 +38,7 @@ export const RadixConnectRelayModule = (input: { }): TransportProvider => { const logger = input.logger?.getSubLogger({ name: 'RadixConnectRelayModule' }) const { baseUrl, providers, walletUrl } = input - const { requestItemModule, storageModule, requestResolverModule } = providers + const { storageModule, requestResolverModule } = providers const encryptionModule = providers?.encryptionModule ?? EncryptionModule() @@ -158,7 +156,7 @@ export const RadixConnectRelayModule = (input: { const sendToWallet = ( walletInteraction: WalletInteraction, callbackFns: Partial, - ): ResultAsync => + ): ResultAsync => ResultAsync.combine([ sessionModule .getCurrentSession() @@ -191,7 +189,11 @@ export const RadixConnectRelayModule = (input: { publicKey: dAppIdentity.x25519.getPublicKey(), }), ) - .andThen(() => waitForWalletResponse(walletInteraction.interactionId)), + .andThen(() => + requestResolverModule.waitForWalletResponse( + walletInteraction.interactionId, + ), + ) ) const decryptWalletResponseData = ( @@ -217,54 +219,6 @@ export const RadixConnectRelayModule = (input: { jsError: error, })) - const waitForWalletResponse = ( - interactionId: string, - ): ResultAsync => - ResultAsync.fromPromise( - new Promise(async (resolve, reject) => { - let response: WalletInteractionResponse | undefined - let error: SdkError | undefined - - logger?.debug({ - method: 'waitForWalletResponse', - interactionId, - }) - - while (!response) { - const requestItemResult = - await requestItemModule.getById(interactionId) - - const requestItem = - requestItemResult.isOk() && requestItemResult.value - - if (requestItem) { - logger?.trace({ - method: 'waitForWalletResponse.requestItemResult', - requestItem, - }) - - if (requestItem.status !== 'pending') { - error = SdkError( - 'RequestItemNotPending', - interactionId, - 'request not in pending state', - ) - break - } else if (requestItem.walletResponse) { - response = requestItem.walletResponse - } - } - - if (!response) { - await wait() - } - } - - return response ? resolve(response) : reject(error) - }), - (err) => err as SdkError, - ) - return { id: 'radix-connect-relay' as const, isSupported: () => isMobile(), diff --git a/packages/dapp-toolkit/src/modules/wallet-request/wallet-request-sdk.ts b/packages/dapp-toolkit/src/modules/wallet-request/wallet-request-sdk.ts index 92921bf8..f13a9a8c 100644 --- a/packages/dapp-toolkit/src/modules/wallet-request/wallet-request-sdk.ts +++ b/packages/dapp-toolkit/src/modules/wallet-request/wallet-request-sdk.ts @@ -86,16 +86,14 @@ export const WalletRequestSdk = (input: WalletRequestSdkInput) => { items, }: { interactionId?: string; items: WalletInteraction['items'] }, callbackFns: Partial = {}, - ): ResultAsync => + ): ResultAsync => withInterceptor({ items, interactionId, metadata, }).andThen((walletInteraction) => getTransport(walletInteraction.interactionId).asyncAndThen((transport) => - transport - .send(walletInteraction, callbackFns) - .andThen(validateWalletResponse), + transport.send(walletInteraction, callbackFns), ), ) diff --git a/packages/dapp-toolkit/src/modules/wallet-request/wallet-request.ts b/packages/dapp-toolkit/src/modules/wallet-request/wallet-request.ts index 16853f4c..894c965a 100644 --- a/packages/dapp-toolkit/src/modules/wallet-request/wallet-request.ts +++ b/packages/dapp-toolkit/src/modules/wallet-request/wallet-request.ts @@ -140,7 +140,6 @@ export const WalletRequestModule = (input: { baseUrl: 'https://radix-connect-relay.radixdlt.com', dAppDefinitionAddress: input.dAppDefinitionAddress, providers: { - requestItemModule, storageModule, requestResolverModule, },