Skip to content

Commit

Permalink
fix: use request resolver module inside relay transport (#279)
Browse files Browse the repository at this point in the history
* docs: mark parameter as optional
* fix: use request resolver module inside relay transport
* refactor: remove not used module from RCR module
  • Loading branch information
dawidsowardx authored Nov 22, 2024
1 parent 979faa8 commit b7083a5
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 77 deletions.
19 changes: 9 additions & 10 deletions examples/simple-dapp/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -67,13 +67,15 @@ export const RequestResolverModule = (input: {
requestItemModule.patch(interactionId, { sentToWallet: true })

const addWalletResponses = (responses: WalletInteractionResponse[]) =>
walletResponses.setItems(
responses.reduce<Record<string, WalletInteractionResponse>>(
(acc, response) => {
acc[response.interactionId] = response
return acc
},
{},
ResultAsync.combine(responses.map(validateWalletResponse)).andThen(() =>
walletResponses.setItems(
responses.reduce<Record<string, WalletInteractionResponse>>(
(acc, response) => {
acc[response.interactionId] = response
return acc
},
{},
),
),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -29,7 +28,6 @@ export const RadixConnectRelayModule = (input: {
walletUrl: string
dAppDefinitionAddress: string
providers: {
requestItemModule: RequestItemModule
storageModule: StorageModule
requestResolverModule: RequestResolverModule
encryptionModule?: EncryptionModule
Expand All @@ -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()

Expand Down Expand Up @@ -158,7 +156,7 @@ export const RadixConnectRelayModule = (input: {
const sendToWallet = (
walletInteraction: WalletInteraction,
callbackFns: Partial<CallbackFns>,
): ResultAsync<WalletInteractionResponse, SdkError> =>
): ResultAsync<unknown, SdkError> =>
ResultAsync.combine([
sessionModule
.getCurrentSession()
Expand Down Expand Up @@ -191,7 +189,11 @@ export const RadixConnectRelayModule = (input: {
publicKey: dAppIdentity.x25519.getPublicKey(),
}),
)
.andThen(() => waitForWalletResponse(walletInteraction.interactionId)),
.andThen(() =>
requestResolverModule.waitForWalletResponse(
walletInteraction.interactionId,
),
)
)

const decryptWalletResponseData = (
Expand All @@ -217,54 +219,6 @@ export const RadixConnectRelayModule = (input: {
jsError: error,
}))

const waitForWalletResponse = (
interactionId: string,
): ResultAsync<WalletInteractionResponse, SdkError> =>
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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,14 @@ export const WalletRequestSdk = (input: WalletRequestSdkInput) => {
items,
}: { interactionId?: string; items: WalletInteraction['items'] },
callbackFns: Partial<CallbackFns> = {},
): ResultAsync<WalletInteractionResponse, SdkError> =>
): ResultAsync<unknown, SdkError> =>
withInterceptor({
items,
interactionId,
metadata,
}).andThen((walletInteraction) =>
getTransport(walletInteraction.interactionId).asyncAndThen((transport) =>
transport
.send(walletInteraction, callbackFns)
.andThen(validateWalletResponse),
transport.send(walletInteraction, callbackFns),
),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ export const WalletRequestModule = (input: {
baseUrl: 'https://radix-connect-relay.radixdlt.com',
dAppDefinitionAddress: input.dAppDefinitionAddress,
providers: {
requestItemModule,
storageModule,
requestResolverModule,
},
Expand Down

0 comments on commit b7083a5

Please sign in to comment.