From 77db663f0d9efb16f8cef90363d3065719fa8ebc Mon Sep 17 00:00:00 2001 From: danield9tqh Date: Tue, 22 Oct 2024 15:57:17 -0400 Subject: [PATCH] PR comments --- .../src/rpc/routes/wallet/create.test.slow.ts | 32 ++++++++++++--- .../src/rpc/routes/wallet/createAccount.ts | 41 ++++++++----------- 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/ironfish/src/rpc/routes/wallet/create.test.slow.ts b/ironfish/src/rpc/routes/wallet/create.test.slow.ts index e73dad814c..03202e8671 100644 --- a/ironfish/src/rpc/routes/wallet/create.test.slow.ts +++ b/ironfish/src/rpc/routes/wallet/create.test.slow.ts @@ -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, @@ -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, + }) + }) }) diff --git a/ironfish/src/rpc/routes/wallet/createAccount.ts b/ironfish/src/rpc/routes/wallet/createAccount.ts index 581827aac6..85b4a565b4 100644 --- a/ironfish/src/rpc/routes/wallet/createAccount.ts +++ b/ironfish/src/rpc/routes/wallet/createAccount.ts @@ -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 = { @@ -37,12 +34,7 @@ export const CreateAccountRequestSchema: yup.ObjectSchema .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() @@ -59,23 +51,18 @@ routes.register( CreateAccountRequestSchema, async (request, context): Promise => { 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) @@ -87,10 +74,16 @@ routes.register( 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, }) }, )