]> git.codecow.com Git - libnemo.git/commitdiff
Fix public key bug in account constructor.
authorChris Duncan <chris@zoso.dev>
Fri, 18 Jul 2025 07:03:30 +0000 (00:03 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 18 Jul 2025 07:03:30 +0000 (00:03 -0700)
src/lib/account.ts
src/lib/workers/safe.ts
test/test.derive-accounts.mjs

index 3fd4b0608e013a64cca1033a1c6c61d205e947b3..480f01fe7b345136b832fb19eedb02e6570cfcb4 100644 (file)
@@ -52,14 +52,16 @@ export class Account {
        }\r
        set weight (v) { this.#weight = v ? BigInt(v) : undefined }\r
 \r
-       private constructor (address: string, publicKey: Uint8Array<ArrayBuffer>, index?: number) {\r
+       private constructor (address: string, publicKey: Key, index?: number) {\r
                if (!Account.#isInternal) {\r
                        throw new Error(`Account cannot be instantiated directly. Use factory methods instead.`)\r
                }\r
                this.#address = address\r
                        .replace(PREFIX, '')\r
                        .replace(PREFIX_LEGACY, '')\r
-               this.#publicKey = publicKey\r
+               this.#publicKey = typeof publicKey === 'string'\r
+                       ? hex.toBytes(publicKey)\r
+                       : publicKey\r
                this.#index = index\r
        }\r
 \r
@@ -69,9 +71,9 @@ export class Account {
        */\r
        async destroy (): Promise<void> {\r
                await SafeWorker.assign({\r
-                       store: 'Account',\r
                        method: 'destroy',\r
-                       name: this.#publicKey\r
+                       name: this.publicKey,\r
+                       store: 'Account'\r
                })\r
                this.#frontier = undefined\r
                this.#balance = undefined\r
@@ -334,7 +336,7 @@ export class Account {
                }\r
 \r
                const accounts: Account[] = []\r
-               const data: Data = {}\r
+               const privateAccounts: Data = {}\r
                for (let keypair of keypairs) {\r
                        let { index, privateKey } = keypair\r
                        if (index == null) {\r
@@ -344,14 +346,16 @@ export class Account {
                        if (typeof privateKey === 'string') privateKey = hex.toBytes(privateKey)\r
                        try {\r
                                const headers = {\r
-                                       method: 'convert',\r
-                                       privateKey: privateKey.buffer\r
+                                       method: 'convert'\r
                                }\r
-                               const publicKey = await NanoNaClWorker.assign(headers)\r
-                               data[publicKey] = privateKey.buffer\r
-\r
-                               const address = this.#keyToAddress(publicKey)\r
+                               const data = {\r
+                                       privateKey: new Uint8Array(privateKey).buffer\r
+                               }\r
+                               const publicKey = await NanoNaClWorker.assign(headers, data)\r
+                               privateAccounts[publicKey] = privateKey.buffer\r
+                               const address = this.#keyToAddress(hex.toBytes(publicKey))\r
                                this.#isInternal = true\r
+                               debugger\r
                                accounts.push(new this(address, publicKey, index))\r
                        } catch (err) {\r
                                throw new Error(`Failed to derive public key from private key`, { cause: err })\r
@@ -363,8 +367,8 @@ export class Account {
                                method: 'set',\r
                                store: 'Account'\r
                        }\r
-                       data.password = password.buffer\r
-                       const isLocked = await SafeWorker.assign(headers, data)\r
+                       privateAccounts.password = password.buffer\r
+                       const isLocked = await SafeWorker.assign(headers, privateAccounts)\r
                        if (!isLocked) {\r
                                throw null\r
                        }\r
index a94d5eb28137a9420e2499572d550a637339639a..b4fea7bcbfe03b5ce943fc1be9cdf417a49c1ea7 100644 (file)
@@ -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'
 
index 56dad8b2d770a647f1bdb2ea78bab1c4a6a38bde..36dbd320e888981e733b2e6e9b4b82cbf3b14d89 100644 (file)
@@ -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)\r
                await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
                const account = await wallet.account()\r
+               debugger\r
                const privateKey = await account.exportPrivateKey(wallet.seed, 'hex')\r
 \r
                assert.equals(privateKey, NANO_TEST_VECTORS.PRIVATE_0)\r