From 43fb9743d60c5ea75d598a44eea9ed469ab49579 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 18 Jul 2025 00:03:30 -0700 Subject: [PATCH] Fix public key bug in account constructor. --- src/lib/account.ts | 30 +++++++++++++++++------------- src/lib/workers/safe.ts | 2 +- test/test.derive-accounts.mjs | 1 + 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/lib/account.ts b/src/lib/account.ts index 3fd4b06..480f01f 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -52,14 +52,16 @@ export class Account { } set weight (v) { this.#weight = v ? BigInt(v) : undefined } - private constructor (address: string, publicKey: Uint8Array, index?: number) { + private constructor (address: string, publicKey: Key, index?: number) { if (!Account.#isInternal) { throw new Error(`Account cannot be instantiated directly. Use factory methods instead.`) } this.#address = address .replace(PREFIX, '') .replace(PREFIX_LEGACY, '') - this.#publicKey = publicKey + this.#publicKey = typeof publicKey === 'string' + ? hex.toBytes(publicKey) + : publicKey this.#index = index } @@ -69,9 +71,9 @@ export class Account { */ async destroy (): Promise { await SafeWorker.assign({ - store: 'Account', method: 'destroy', - name: this.#publicKey + name: this.publicKey, + store: 'Account' }) this.#frontier = undefined this.#balance = undefined @@ -334,7 +336,7 @@ export class Account { } const accounts: Account[] = [] - const data: Data = {} + const privateAccounts: Data = {} for (let keypair of keypairs) { let { index, privateKey } = keypair if (index == null) { @@ -344,14 +346,16 @@ export class Account { if (typeof privateKey === 'string') privateKey = hex.toBytes(privateKey) try { const headers = { - method: 'convert', - privateKey: privateKey.buffer + method: 'convert' } - const publicKey = await NanoNaClWorker.assign(headers) - data[publicKey] = privateKey.buffer - - const address = this.#keyToAddress(publicKey) + const data = { + privateKey: new Uint8Array(privateKey).buffer + } + const publicKey = await NanoNaClWorker.assign(headers, data) + privateAccounts[publicKey] = privateKey.buffer + const address = this.#keyToAddress(hex.toBytes(publicKey)) this.#isInternal = true + debugger accounts.push(new this(address, publicKey, index)) } catch (err) { throw new Error(`Failed to derive public key from private key`, { cause: err }) @@ -363,8 +367,8 @@ export class Account { method: 'set', store: 'Account' } - data.password = password.buffer - const isLocked = await SafeWorker.assign(headers, data) + privateAccounts.password = password.buffer + const isLocked = await SafeWorker.assign(headers, privateAccounts) if (!isLocked) { throw null } diff --git a/src/lib/workers/safe.ts b/src/lib/workers/safe.ts index a94d5eb..b4fea7b 100644 --- a/src/lib/workers/safe.ts +++ b/src/lib/workers/safe.ts @@ -4,7 +4,7 @@ 'use strict' import { WorkerInterface } from './worker-interface' -import { default as Convert, base32, bytes } from '#src/lib/convert.js' +import { default as Convert, bytes } from '#src/lib/convert.js' import { Entropy } from '#src/lib/entropy.js' import { Data, Headers, SafeRecord } from '#types' diff --git a/test/test.derive-accounts.mjs b/test/test.derive-accounts.mjs index 56dad8b..36dbd32 100644 --- a/test/test.derive-accounts.mjs +++ b/test/test.derive-accounts.mjs @@ -12,6 +12,7 @@ await suite('BIP-44 account derivation', async () => { const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() + debugger const privateKey = await account.exportPrivateKey(wallet.seed, 'hex') assert.equals(privateKey, NANO_TEST_VECTORS.PRIVATE_0) -- 2.47.3