Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
danield9tqh committed Oct 22, 2024
1 parent 354d234 commit 77db663
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
32 changes: 26 additions & 6 deletions ironfish/src/rpc/routes/wallet/create.test.slow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ describe('Route wallet/createAccount', () => {

it('should set account createdAt if passed', async () => {
const name = uuid()
const createdAt = {
hash: '00000000000163b1c632498ac8f86cfd33f65c5d4e43e5001c75d5acb4ada4de',
sequence: 10,
}
const createdAt = 10

const response = await routeTest.client.wallet.createAccount({
name,
Expand All @@ -118,9 +115,32 @@ describe('Route wallet/createAccount', () => {
name: name,
publicAddress: response.content.publicAddress,
createdAt: {
hash: Buffer.from(createdAt.hash, 'hex'),
sequence: createdAt.sequence,
hash: Buffer.alloc(32, 0),
sequence: 10,
},
})
})

it('should set account createdAt to null', async () => {
const name = uuid()

const response = await routeTest.client.wallet.createAccount({
name,
createdAt: null,
})

expect(response.status).toBe(200)
expect(response.content).toMatchObject({
name: name,
publicAddress: expect.any(String),
isDefaultAccount: true,
})

const account = routeTest.node.wallet.getAccountByName(name)
expect(account).toMatchObject({
name: name,
publicAddress: response.content.publicAddress,
createdAt: null,
})
})
})
41 changes: 17 additions & 24 deletions ironfish/src/rpc/routes/wallet/createAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import { AssertHasRpcContext } from '../rpcContext'
export type CreateAccountRequest = {
name: string
default?: boolean
createdAt?: {
hash: string
sequence: number
}
createdAt?: number | null
}

export type CreateAccountResponse = {
Expand All @@ -37,12 +34,7 @@ export const CreateAccountRequestSchema: yup.ObjectSchema<CreateAccountRequest>
.object({
name: yup.string().defined(),
default: yup.boolean().optional(),
createdAt: yup
.object({
hash: yup.string(),
sequence: yup.number(),
})
.optional(),
createdAt: yup.number().optional().nullable(),
})
.defined()

Expand All @@ -59,23 +51,18 @@ routes.register<typeof CreateAccountRequestSchema, CreateAccountResponse>(
CreateAccountRequestSchema,
async (request, context): Promise<void> => {
AssertHasRpcContext(request, context, 'wallet')
if (
request.data.createdAt?.hash === undefined ||
request.data.createdAt?.sequence === undefined
) {
request.data.createdAt = undefined
}

const createdAt = request.data.createdAt && {
hash: Buffer.from(request.data.createdAt.hash, 'hex'),
sequence: request.data.createdAt.sequence,
}

const setDefault = !context.wallet.hasDefaultAccount || (request.data.default ?? false)
const createdAt =
typeof request.data.createdAt === 'number'
? {
hash: Buffer.alloc(32, 0),
sequence: request.data.createdAt,
}
: request.data.createdAt

let account
try {
account = await context.wallet.createAccount(request.data.name, { setDefault, createdAt })
account = await context.wallet.createAccount(request.data.name, { createdAt })
} catch (e) {
if (e instanceof DuplicateAccountNameError) {
throw new RpcValidationError(e.message, 400, RPC_ERROR_CODES.DUPLICATE_ACCOUNT_NAME)
Expand All @@ -87,10 +74,16 @@ routes.register<typeof CreateAccountRequestSchema, CreateAccountResponse>(
void context.wallet.scan()
}

let isDefaultAccount = false
if (!context.wallet.hasDefaultAccount || request.data.default) {
await context.wallet.setDefaultAccount(account.name)
isDefaultAccount = true
}

request.end({
name: account.name,
publicAddress: account.publicAddress,
isDefaultAccount: setDefault,
isDefaultAccount,
})
},
)

0 comments on commit 77db663

Please sign in to comment.