Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
fix: update usePersona schema
Browse files Browse the repository at this point in the history
  • Loading branch information
xstelea committed Feb 6, 2023
1 parent 884d7d5 commit 09add37
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/IO/request-items/use-persona.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type UsePersona = {
response: AuthUsePersonaRequestResponseItem
}
method: {
input: {}
input: { identityAddress: string }
output: {
auth: { identityAddress: string }
}
Expand Down
13 changes: 12 additions & 1 deletion lib/IO/transform-method-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,20 @@ export const transformMethodInput = <I extends {}>(input: I) =>
},
}

case requestMethodRequestType.loginWithoutChallenge:
return {
...acc,
auth: { ...value, discriminator: 'login' },
discriminator: 'authorizedRequest',
}

case requestMethodRequestType.usePersona:
return {
...acc,
auth: { ...value, discriminator: 'usePersona' },
discriminator: 'authorizedRequest',
}
case requestMethodRequestType.loginWithChallenge:
case requestMethodRequestType.loginWithoutChallenge:
return {
...acc,
auth: value,
Expand Down
67 changes: 42 additions & 25 deletions lib/__tests__/schemas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,31 +158,48 @@ describe('schemas', () => {
},
})

// WalletInteractionResponseSchema.parse({
// discriminator: 'success',
// interactionId: '3b96efd3-fdfc-4d53-a43c-3d190cd77277',
// items: {
// discriminator: 'authorizedRequest',
// auth: {
// discriminator: 'loginWithoutChallenge',
// persona: {
// identityAddress:
// 'account_tdx_b_1ql5kf246qrna76pdjp7jcg7y7rznsn6qq49xpzxtmvlqeklhwj',
// label: 'persona',
// },
// },
// ongoingAccounts: {
// accounts: [
// {
// address:
// 'account_tdx_b_1qlljmf2m0tge4qk8k2e4vkuyhw6hjwf6k02rtgs3gewsfmn8fs',
// label: '234',
// appearanceId: 0,
// },
// ],
// },
// },
// })
WalletInteractionResponseSchema.parse({
discriminator: 'success',
interactionId: '3b96efd3-fdfc-4d53-a43c-3d190cd77277',
items: {
discriminator: 'authorizedRequest',
auth: {
discriminator: 'loginWithoutChallenge',
persona: {
identityAddress:
'account_tdx_b_1ql5kf246qrna76pdjp7jcg7y7rznsn6qq49xpzxtmvlqeklhwj',
label: 'persona',
},
},
ongoingAccounts: {
accounts: [
{
address:
'account_tdx_b_1qlljmf2m0tge4qk8k2e4vkuyhw6hjwf6k02rtgs3gewsfmn8fs',
label: '234',
appearanceId: 0,
},
],
},
},
})
WalletInteractionResponseSchema.parse({
interactionId: 'd4c76fd0-1881-4e0b-87e5-0b6e3e58c360',
discriminator: 'success',
items: {
discriminator: 'unauthorizedRequest',
oneTimeAccounts: {
accounts: [
{
address:
'account_tdx_b_1q7v5vaj2jxf9pm5ndzqvfd925sukk0x7xlhfghn9apxq88agsg',
label: '1',
appearanceId: 0,
},
],
},
},
})
})

it('should parse error without message', () => {
Expand Down
118 changes: 118 additions & 0 deletions lib/__tests__/wallet-sdk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,124 @@ describe('sdk flow', () => {

expect(callbackSpy).toBeCalledWith('receivedByExtension')
})

it('should request ongoing accounts and login', async () => {
const callbackSpy = jest.fn()

const { request, sendIncomingMessage, outgoingMessageSpy } =
createRequestHelper(() =>
sdk.request(
{
loginWithoutChallenge: {},
ongoingAccountsWithoutProofOfOwnership: {
quantifier: 'atLeast',
quantity: 1,
},
},
{
eventCallback: callbackSpy,
}
)
)

// mock a wallet response
delay(300).then(() => {
sendIncomingMessage({
discriminator: 'authorizedRequest',
auth: {
discriminator: 'loginWithoutChallenge',
persona: {
identityAddress:
'account_tdx_b_1qlu8fdyj77jpmu2mqe4rgh3738jcva4nfd2y2vp675zqgdg72y',
label: '2nd persona',
},
},
ongoingAccounts: {
accounts: [
{
address:
'account_tdx_b_1qaz0nxslmr9nssmy463rd57hl7q0xsadaal0gy7cwsuqwecaws',
label: 'Jakub Another Accoun',
appearanceId: 1,
},
{
address:
'account_tdx_b_1q7te4nk60fy2wt7d0wh8l2dhlp5c0n75phcnrwa8uglsrf6sjr',
label: '3rd Account',
appearanceId: 2,
},
],
},
})
})

const result = await request

if (result.isErr()) throw new Error('should not get a error response')

expect((result.value as any).ongoingAccounts).toEqual([
{
address:
'account_tdx_b_1qaz0nxslmr9nssmy463rd57hl7q0xsadaal0gy7cwsuqwecaws',
label: 'Jakub Another Accoun',
appearanceId: 1,
},
{
address:
'account_tdx_b_1q7te4nk60fy2wt7d0wh8l2dhlp5c0n75phcnrwa8uglsrf6sjr',
label: '3rd Account',
appearanceId: 2,
},
])

expect(outgoingMessageSpy.getFirstValue().metadata).toEqual({
dAppDefinitionAddress: 'radixDashboard',
networkId: 1,
})

expect(callbackSpy).toBeCalledWith('receivedByExtension')
})
it('should request usePersona', async () => {
const callbackSpy = jest.fn()

const { request, sendIncomingMessage } = createRequestHelper(() =>
sdk.request(
{
usePersona: {
identityAddress:
'account_tdx_b_1qlu8fdyj77jpmu2mqe4rgh3738jcva4nfd2y2vp675zqgdg72y',
},
},
{
eventCallback: callbackSpy,
}
)
)

// mock a wallet response
delay(300).then(() => {
sendIncomingMessage({
discriminator: 'authorizedRequest',
auth: {
discriminator: 'loginWithoutChallenge',
persona: {
identityAddress:
'account_tdx_b_1qlu8fdyj77jpmu2mqe4rgh3738jcva4nfd2y2vp675zqgdg72y',
label: '2nd persona',
},
},
})
})

const result = await request

if (result.isErr()) throw new Error('should not get a error response')
expect((result.value as any).login.persona.identityAddress).toEqual(
'account_tdx_b_1qlu8fdyj77jpmu2mqe4rgh3738jcva4nfd2y2vp675zqgdg72y'
)

expect(callbackSpy).toBeCalledWith('receivedByExtension')
})
})

describe('send transaction method', () => {
Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/validate-wallet-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const validateWalletRequest = (
.map(() => walletInteraction)
.mapErr((issues) => {
log.error(`🔵💥 invalid wallet request`)
log.debug(issues)
log.debug(JSON.stringify(walletInteraction, null, 2))
log.debug(JSON.stringify(issues, null, 2))
return createSdkError(
errorType.walletRequestValidation,
walletInteraction.interactionId
Expand Down

0 comments on commit 09add37

Please sign in to comment.