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

Commit

Permalink
feat: add reset request item
Browse files Browse the repository at this point in the history
  • Loading branch information
xstelea committed Mar 7, 2023
1 parent 234ba75 commit a840f6d
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 13 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ You may wish to consider using this with the [√ Connect Button](https://github
- [Get list of Persona data](#get-list-of-persona-data)
- [oneTimePersonaData](#onetimepersonadata)
- [ongoingPersonaData](#ongoingpersonadata)
- [Reset](#reset)
- [Login](#login)
- [login](#login-1)
- [usePersona](#usepersona)
Expand Down Expand Up @@ -84,6 +85,7 @@ type WalletSdkInput = {
| [loginWithChallenge](#loginwithchallenge) | ❌ |
| [loginWithoutChallenge](#loginwithoutchallenge) | ✅ |
| [usePersona](#usepersona) | ✅ |
| [reset](#reset) | ✅ |
### About oneTime VS ongoing requests
Expand Down Expand Up @@ -286,8 +288,10 @@ const value = result.value

```typescript
const result = await walletSdk.request(
requestItem.usePersona(identityAddress),
requestBuilder(requestItem.ongoingPersonaData(['firstName', 'email']))
requestBuilder(
requestItem.usePersona(identityAddress),
requestItem.ongoingPersonaData(['firstName', 'email'])
)
)

if (result.isErr()) {
Expand All @@ -304,6 +308,19 @@ if (result.isErr()) {
const value = result.value
```

### Reset

You can send a reset request to ask the user to provide new values for ongoing accounts and/or persona data.

```typescript
const result = await walletSdk.request(
requestBuilder(
requestItem.usePersona(identityAddress),
requestItem.reset({ account: true, personaData: true })
)
)
```

### Login

Sometimes your dApp may want a more personalized, consistent user experience and the Radix Wallet is able to login users with a Persona.
Expand Down
19 changes: 8 additions & 11 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { WalletSdk, requestBuilder, requestItem } from '../lib/wallet-sdk'
import { Result } from 'neverthrow'
import { login } from '../lib/IO/request-items/login'
import { reset } from '../lib/IO/request-items/reset'

const sdk = WalletSdk({
dAppDefinitionAddress:
Expand Down Expand Up @@ -62,7 +63,10 @@ const accountAddressInputElement = document.getElementById(

document.getElementById('login-btn')!.onclick = async () => {
clearResults()
const result = await sdk.request({ loginWithoutChallenge: {} })
const result = await sdk.request({
loginWithoutChallenge: {},
reset: { accounts: true },
})

displayResults(result)
}
Expand All @@ -86,7 +90,9 @@ document.getElementById('account-address-btn')!.onclick = async () => {
document.getElementById('persona-data-btn')!.onclick = async () => {
clearResults()

const result = await sdk.request(requestBuilder(login.withoutChallenge()))
const result = await sdk.request(
requestBuilder(login.withoutChallenge(), reset({ personaData: true }))
)

displayResults(result)
}
Expand Down Expand Up @@ -115,15 +121,6 @@ document.getElementById('send-tx-btn')!.onclick = async () => {
displayResults(result)
}

sdk
.request(
requestBuilder(
requestItem.login.withoutChallenge(),
requestItem.ongoingAccounts.withoutProofOfOwnership()
)
)
.map((result) => result.persona)

window.radixWalletSdk = sdk

declare global {
Expand Down
2 changes: 2 additions & 0 deletions lib/IO/request-items/request-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ongoingAccounts } from './ongoing-accounts'
import { usePersona } from './use-persona'
import { oneTimePersonaData } from './one-time-persona-data'
import { ongoingPersonaData } from './ongoing-persona-data'
import { reset } from './reset'

export const requestItem = {
oneTimeAccounts,
Expand All @@ -12,4 +13,5 @@ export const requestItem = {
ongoingPersonaData,
login,
usePersona,
reset,
} as const
26 changes: 26 additions & 0 deletions lib/IO/request-items/reset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ResetRequestItem } from '../schemas'

export type Reset = {
wallet: {
request: ResetRequestItem
response: {}
}
method: {
input: Partial<ResetRequestItem>
output: {}
}
}

type RequiredKeys =
| { usePersona: any }
| { login: any }
| { loginWithoutChallenge: any }

type NotAllowedKeys = Partial<{ reset: any }>

export const reset =
({ accounts = false, personaData = false }: Partial<ResetRequestItem>) =>
<I extends RequiredKeys>(input: I extends NotAllowedKeys ? never : I) => ({
...input,
reset: { accounts, personaData },
})
9 changes: 9 additions & 0 deletions lib/IO/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ export type AuthRequestResponseItem = z.infer<
typeof AuthRequestResponseItemSchema
>

export type ResetRequestItem = z.infer<typeof ResetRequestSchema>

const ResetRequestSchema = object({
accounts: boolean(),
personaData: boolean(),
})

const SendTransactionRequestItemSchema = object({
transactionManifest: string(),
version: number(),
Expand All @@ -276,6 +283,7 @@ const WalletUnauthorizedRequestItemsSchema = object({
discriminator: literal('unauthorizedRequest'),
oneTimeAccounts: OneTimeAccountsRequestItemSchema.optional(),
oneTimePersonaData: OneTimePersonaDataRequestItemSchema.optional(),
reset: ResetRequestSchema.optional(),
})

export type WalletUnauthorizedRequestItems = z.infer<
Expand All @@ -289,6 +297,7 @@ const WalletAuthorizedRequestItemsSchema = object({
ongoingAccounts: OngoingAccountsRequestItemSchema.optional(),
oneTimePersonaData: OneTimePersonaDataRequestItemSchema.optional(),
ongoingPersonaData: OngoingPersonaDataRequestItemSchema.optional(),
reset: ResetRequestSchema.optional(),
})

export type WalletAuthorizedRequestItems = z.infer<
Expand Down
9 changes: 9 additions & 0 deletions lib/IO/transform-method-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,22 @@ export const transformMethodInput = <I extends {}>(input: I) =>
auth: { ...value, discriminator: 'usePersona' },
discriminator: 'authorizedRequest',
}

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

case requestMethodRequestType.reset: {
const { accounts = false, personaData = false } = value
return {
...acc,
reset: { accounts, personaData },
}
}

case 'send':
return {
discriminator: 'transaction',
Expand Down
17 changes: 17 additions & 0 deletions lib/__tests__/transform-method-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,21 @@ describe('transformMethodInput', () => {
})
})
})
describe('reset', () => {
it('should return correct transformed value', () => {
;[
{
actual: requestItem.reset({ accounts: true }),
expected: {
reset: { accounts: true, personaData: false },
discriminator: 'unauthorizedRequest',
},
},
].forEach((testItem) => {
expect(transformMethodInput(testItem.actual({} as any))).toEqual(
ok(testItem.expected)
)
})
})
})
})
3 changes: 3 additions & 0 deletions lib/methods/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Login } from '../IO/request-items/login'
import { OneTimeAccounts } from '../IO/request-items/one-time-accounts'
import { OngoingAccounts } from '../IO/request-items/ongoing-accounts'
import { Reset } from '../IO/request-items/reset'
import { UsePersona } from '../IO/request-items/use-persona'

export const requestMethodRequestType = {
Expand All @@ -15,9 +16,11 @@ export const requestMethodRequestType = {
loginWithChallenge: 'loginWithChallenge',
oneTimePersonaData: 'oneTimePersonaData',
ongoingPersonaData: 'ongoingPersonaData',
reset: 'reset',
} as const

type RequestItems = {
[requestMethodRequestType.reset]: Reset
[requestMethodRequestType.oneTimeAccountsWithoutProofOfOwnership]: OneTimeAccounts['WithoutProofOfOwnership']
// [requestMethodRequestType.oneTimeAccountsWithProofOfOwnership]: OneTimeAccounts['WithProofOfOwnership']
[requestMethodRequestType.ongoingAccountsWithoutProofOfOwnership]: OngoingAccounts['WithoutProofOfOwnership']
Expand Down

0 comments on commit a840f6d

Please sign in to comment.