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

Commit

Permalink
feat: add account with proof requests
Browse files Browse the repository at this point in the history
  • Loading branch information
xstelea committed May 8, 2023
1 parent 40ca7fa commit e6df547
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 63 deletions.
11 changes: 8 additions & 3 deletions lib/IO/request-items/one-time-accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import {
Account,
AccountWithProofOfOwnership,
NumberOfAccountsQuantifier,
OneTimeAccountsRequestItem,
OneTimeAccountsWithoutProofOfOwnershipRequestItem,
OneTimeAccountsWithoutProofOfOwnershipRequestResponseItem,
OneTimeAccountsWithProofOfOwnershipRequestItem,
OneTimeAccountsWithProofOfOwnershipRequestResponseItem,
} from '../schemas'

export type OneTimeAccounts = {
WithoutProofOfOwnership: {
wallet: {
request: OneTimeAccountsRequestItem
request: OneTimeAccountsWithoutProofOfOwnershipRequestItem
response: OneTimeAccountsWithoutProofOfOwnershipRequestResponseItem
}
method: {
Expand All @@ -26,7 +27,7 @@ export type OneTimeAccounts = {
}
WithProofOfOwnership: {
wallet: {
request: OneTimeAccountsRequestItem
request: OneTimeAccountsWithProofOfOwnershipRequestItem
response: OneTimeAccountsWithProofOfOwnershipRequestResponseItem
}
method: {
Expand Down Expand Up @@ -55,20 +56,24 @@ export const oneTimeAccounts = {
<I>(input: I extends NotAllowedKeys ? never : I) => ({
...input,
oneTimeAccountsWithoutProofOfOwnership: {
discriminator: 'oneTimeAccountsWithoutProofOfOwnership',
quantity,
quantifier,
},
}),
withProofOfOwnership:
(
challenge: string,
quantity = config.defaultNumberOfAccountsQuantity,
quantifier = config.defaultNumberOfAccountsQuantifier
) =>
<I>(input: I extends NotAllowedKeys ? never : I) => ({
...input,
oneTimeAccountsWithProofOfOwnership: {
discriminator: 'oneTimeAccountsWithProofOfOwnership',
quantity,
quantifier,
challenge,
},
}),
}
11 changes: 8 additions & 3 deletions lib/IO/request-items/ongoing-accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import {
Account,
AccountWithProofOfOwnership,
NumberOfAccountsQuantifier,
OngoingAccountsRequestItem,
OngoingAccountsWithProofOfOwnershipRequestItem,
OngoingAccountsWithoutProofOfOwnershipRequestItem,
OngoingAccountsWithoutProofOfOwnershipRequestResponseItem,
OngoingAccountsWithProofOfOwnershipRequestResponseItem,
} from '../schemas'

export type OngoingAccounts = {
WithoutProofOfOwnership: {
wallet: {
request: OngoingAccountsRequestItem
request: OngoingAccountsWithoutProofOfOwnershipRequestItem
response: OngoingAccountsWithoutProofOfOwnershipRequestResponseItem
}
method: {
Expand All @@ -24,7 +25,7 @@ export type OngoingAccounts = {
}
WithProofOfOwnership: {
wallet: {
request: OngoingAccountsRequestItem
request: OngoingAccountsWithProofOfOwnershipRequestItem
response: OngoingAccountsWithProofOfOwnershipRequestResponseItem
}
method: {
Expand Down Expand Up @@ -56,20 +57,24 @@ export const ongoingAccounts = {
<I extends RequiredKeys>(input: I extends NotAllowedKeys ? never : I) => ({
...input,
ongoingAccountsWithoutProofOfOwnership: {
discriminator: 'ongoingAccountsWithoutProofOfOwnership',
quantity,
quantifier,
},
}),
withProofOfOwnership:
(
challenge: string,
quantity = config.defaultNumberOfAccountsQuantity,
quantifier = config.defaultNumberOfAccountsQuantifier
) =>
<I extends RequiredKeys>(input: I extends NotAllowedKeys ? never : I) => ({
...input,
ongoingAccountsWithProofOfOwnership: {
discriminator: 'ongoingAccountsWithProofOfOwnership',
quantity,
quantifier,
challenge,
},
}),
}
71 changes: 53 additions & 18 deletions lib/IO/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ export const PersonaDataFieldSchema = union([
literal(personaDataField.phoneNumber),
])

const ProofSchema = object({
publicKey: string(),
signature: string(),
curve: string(),
})

export type Proof = z.infer<typeof ProofSchema>

export type PersonaDataField = z.infer<typeof PersonaDataFieldSchema>

const AccountSchema = object({
Expand All @@ -55,7 +63,7 @@ export type Account = z.infer<typeof AccountSchema>
const AccountWithProofOfOwnershipSchema = object({
account: AccountSchema,
challenge: string(),
signature: string(),
proof: ProofSchema,
})

export type AccountWithProofOfOwnership = z.infer<
Expand Down Expand Up @@ -99,16 +107,27 @@ const MetadataSchema = object({

export type Metadata = z.infer<typeof MetadataSchema>

const OneTimeAccountsRequestItemSchema = object({
requiresProofOfOwnership: boolean(),
const OneTimeAccountsWithoutProofOfOwnershipRequestItemSchema = object({
discriminator: literal('oneTimeAccountsWithoutProofOfOwnership'),
numberOfAccounts: NumberOfAccountsSchema,
})

export type OneTimeAccountsRequestItem = z.infer<
typeof OneTimeAccountsRequestItemSchema
export type OneTimeAccountsWithoutProofOfOwnershipRequestItem = z.infer<
typeof OneTimeAccountsWithoutProofOfOwnershipRequestItemSchema
>

const OneTimeAccountsWithProofOfOwnershipRequestItemSchema = object({
discriminator: literal('oneTimeAccountsWithProofOfOwnership'),
numberOfAccounts: NumberOfAccountsSchema,
challenge: string(),
})

export type OneTimeAccountsWithProofOfOwnershipRequestItem = z.infer<
typeof OneTimeAccountsWithProofOfOwnershipRequestItemSchema
>

const OneTimeAccountsWithProofOfOwnershipRequestResponseItemSchema = object({
discriminator: literal('oneTimeAccountsWithProofOfOwnership'),
accounts: AccountWithProofOfOwnershipSchema.array(),
})

Expand All @@ -117,6 +136,7 @@ export type OneTimeAccountsWithProofOfOwnershipRequestResponseItem = z.infer<
>

const OneTimeAccountsWithoutProofOfOwnershipRequestResponseItemSchema = object({
discriminator: literal('oneTimeAccountsWithoutProofOfOwnership'),
accounts: AccountSchema.array(),
})

Expand All @@ -133,16 +153,27 @@ export type OneTimeAccountsRequestResponseItem = z.infer<
typeof OneTimeAccountsRequestResponseItemSchema
>

const OngoingAccountsRequestItemSchema = object({
requiresProofOfOwnership: boolean(),
const OngoingAccountsWithProofOfOwnershipRequestItemSchema = object({
discriminator: literal('ongoingAccountsWithProofOfOwnership'),
numberOfAccounts: NumberOfAccountsSchema,
challenge: string(),
})

export type OngoingAccountsWithProofOfOwnershipRequestItem = z.infer<
typeof OngoingAccountsWithProofOfOwnershipRequestItemSchema
>

const OngoingAccountsWithoutProofOfOwnershipRequestItemSchema = object({
discriminator: literal('ongoingAccountsWithoutProofOfOwnership'),
numberOfAccounts: NumberOfAccountsSchema,
})

export type OngoingAccountsRequestItem = z.infer<
typeof OngoingAccountsRequestItemSchema
export type OngoingAccountsWithoutProofOfOwnershipRequestItem = z.infer<
typeof OngoingAccountsWithoutProofOfOwnershipRequestItemSchema
>

const OngoingAccountsWithProofOfOwnershipRequestResponseItemSchema = object({
discriminator: literal('ongoingAccountsWithProofOfOwnership'),
accounts: AccountWithProofOfOwnershipSchema.array(),
})

Expand All @@ -151,6 +182,7 @@ export type OngoingAccountsWithProofOfOwnershipRequestResponseItem = z.infer<
>

const OngoingAccountsWithoutProofOfOwnershipRequestResponseItemSchema = object({
discriminator: literal('ongoingAccountsWithoutProofOfOwnership'),
accounts: AccountSchema.array(),
})

Expand Down Expand Up @@ -242,12 +274,6 @@ export type AuthLoginWithoutChallengeRequestResponseItem = z.infer<
typeof AuthLoginWithoutChallengeRequestResponseItemSchema
>

const ProofSchema = object({
publicKey: string(),
signature: string(),
curve: string(),
})

const AuthLoginWithChallengeRequestResponseItemSchema = object({
discriminator: literal('loginWithChallenge'),
persona: PersonaSchema,
Expand Down Expand Up @@ -313,7 +339,10 @@ export type SendTransactionResponseItem = z.infer<

const WalletUnauthorizedRequestItemsSchema = object({
discriminator: literal('unauthorizedRequest'),
oneTimeAccounts: OneTimeAccountsRequestItemSchema.optional(),
oneTimeAccounts: union([
OneTimeAccountsWithProofOfOwnershipRequestItemSchema,
OneTimeAccountsWithoutProofOfOwnershipRequestItemSchema,
]).optional(),
oneTimePersonaData: OneTimePersonaDataRequestItemSchema.optional(),
})

Expand All @@ -324,8 +353,14 @@ export type WalletUnauthorizedRequestItems = z.infer<
const WalletAuthorizedRequestItemsSchema = object({
discriminator: literal('authorizedRequest'),
auth: AuthRequestItemSchema,
oneTimeAccounts: OneTimeAccountsRequestItemSchema.optional(),
ongoingAccounts: OngoingAccountsRequestItemSchema.optional(),
oneTimeAccounts: union([
OneTimeAccountsWithProofOfOwnershipRequestItemSchema,
OneTimeAccountsWithoutProofOfOwnershipRequestItemSchema,
]).optional(),
ongoingAccounts: union([
OngoingAccountsWithProofOfOwnershipRequestItemSchema,
OngoingAccountsWithoutProofOfOwnershipRequestItemSchema,
]).optional(),
oneTimePersonaData: OneTimePersonaDataRequestItemSchema.optional(),
ongoingPersonaData: OngoingPersonaDataRequestItemSchema.optional(),
reset: ResetRequestSchema.optional(),
Expand Down
58 changes: 38 additions & 20 deletions lib/IO/transform-method-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { ok } from 'neverthrow'
import { requestMethodRequestType } from '../methods/request'
import {
NumberOfAccounts,
OneTimeAccountsWithProofOfOwnershipRequestItem,
OneTimeAccountsWithoutProofOfOwnershipRequestItem,
OngoingAccountsWithProofOfOwnershipRequestItem,
OngoingAccountsWithoutProofOfOwnershipRequestItem,
ResetRequestItem,
WalletInteractionItems,
WalletUnauthorizedRequestItems,
Expand Down Expand Up @@ -33,41 +37,55 @@ export const transformMethodInput = <I extends {}>(input: I) =>
Object.entries(input).reduce<WalletInteractionItems>(
(acc, [requestType, value]: [string, any]) => {
switch (requestType) {
case requestMethodRequestType.oneTimeAccountsWithoutProofOfOwnership:
case requestMethodRequestType.oneTimeAccountsWithoutProofOfOwnership: {
const oneTimeAccounts: OneTimeAccountsWithoutProofOfOwnershipRequestItem =
{
discriminator: 'oneTimeAccountsWithoutProofOfOwnership',
numberOfAccounts: provideDefaultNumberOfAccounts(value),
}
return {
...acc,
oneTimeAccounts: {
requiresProofOfOwnership: false,
numberOfAccounts: provideDefaultNumberOfAccounts(value),
},
oneTimeAccounts,
}
}

case requestMethodRequestType.oneTimeAccountsWithProofOfOwnership:
case requestMethodRequestType.oneTimeAccountsWithProofOfOwnership: {
const oneTimeAccounts: OneTimeAccountsWithProofOfOwnershipRequestItem =
{
discriminator: 'oneTimeAccountsWithProofOfOwnership',
numberOfAccounts: provideDefaultNumberOfAccounts(value),
challenge: value.challenge,
}
return {
...acc,
oneTimeAccounts: {
requiresProofOfOwnership: true,
numberOfAccounts: provideDefaultNumberOfAccounts(value),
},
oneTimeAccounts,
}
}

case requestMethodRequestType.ongoingAccountsWithProofOfOwnership:
case requestMethodRequestType.ongoingAccountsWithProofOfOwnership: {
const ongoingAccounts: OngoingAccountsWithProofOfOwnershipRequestItem =
{
discriminator: 'ongoingAccountsWithProofOfOwnership',
numberOfAccounts: provideDefaultNumberOfAccounts(value),
challenge: value.challenge,
}
return {
...acc,
ongoingAccounts: {
requiresProofOfOwnership: true,
numberOfAccounts: provideDefaultNumberOfAccounts(value),
},
ongoingAccounts,
}
}

case requestMethodRequestType.ongoingAccountsWithoutProofOfOwnership:
case requestMethodRequestType.ongoingAccountsWithoutProofOfOwnership: {
const ongoingAccounts: OngoingAccountsWithoutProofOfOwnershipRequestItem =
{
discriminator: 'ongoingAccountsWithoutProofOfOwnership',
numberOfAccounts: provideDefaultNumberOfAccounts(value),
}
return {
...acc,
ongoingAccounts: {
requiresProofOfOwnership: false,
numberOfAccounts: provideDefaultNumberOfAccounts(value),
},
ongoingAccounts,
}
}

case requestMethodRequestType.oneTimePersonaData:
return {
Expand Down
8 changes: 6 additions & 2 deletions lib/__tests__/schemas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
describe('schemas', () => {
it('OneTimeAccountsResponseItemSchema', () => {
OneTimeAccountsRequestResponseItemSchema.parse({
discriminator: 'oneTimeAccountsWithoutProofOfOwnership',
accounts: [
{
address: 'rdx61333732663539372d383861352d3461',
Expand Down Expand Up @@ -73,14 +74,14 @@ describe('schemas', () => {
identityAddress: 'randomAddress1',
},
oneTimeAccounts: {
requiresProofOfOwnership: false,
discriminator: 'oneTimeAccountsWithoutProofOfOwnership',
numberOfAccounts: {
quantity: 1,
quantifier: 'exactly',
},
},
ongoingAccounts: {
requiresProofOfOwnership: true,
discriminator: 'ongoingAccountsWithoutProofOfOwnership',
numberOfAccounts: {
quantity: 5,
quantifier: 'atLeast',
Expand Down Expand Up @@ -118,6 +119,7 @@ describe('schemas', () => {
items: {
discriminator: 'unauthorizedRequest',
oneTimeAccounts: {
discriminator: 'oneTimeAccountsWithoutProofOfOwnership',
accounts: [],
},
oneTimePersonaData: {
Expand Down Expand Up @@ -171,6 +173,7 @@ describe('schemas', () => {
},
},
ongoingAccounts: {
discriminator: 'ongoingAccountsWithoutProofOfOwnership',
accounts: [
{
address:
Expand All @@ -188,6 +191,7 @@ describe('schemas', () => {
items: {
discriminator: 'unauthorizedRequest',
oneTimeAccounts: {
discriminator: 'oneTimeAccountsWithoutProofOfOwnership',
accounts: [
{
address:
Expand Down
Loading

0 comments on commit e6df547

Please sign in to comment.