]> git.codecow.com Git - libnemo.git/commitdiff
Remove deprecated account export from tests. Fix block signature verification tests.
authorChris Duncan <chris@zoso.dev>
Sun, 27 Jul 2025 22:39:58 +0000 (15:39 -0700)
committerChris Duncan <chris@zoso.dev>
Sun, 27 Jul 2025 22:39:58 +0000 (15:39 -0700)
src/lib/wallets/wallet.ts
test/VECTORS.mjs
test/test.derive-accounts.mjs
test/test.import-wallet.mjs
test/test.ledger.mjs
test/test.lock-unlock.mjs
test/test.tools.mjs

index 63184a02d51204675eb0946456fc497ab3c3af5b..19811938af7c4b25ce83a4dd3de2d36fa6317698 100644 (file)
@@ -270,13 +270,13 @@ export abstract class Wallet {
        * @returns {Promise<string>} Hexadecimal-formatted 64-byte signature\r
        */\r
        async sign (index: number, block: ChangeBlock | ReceiveBlock | SendBlock): Promise<string> {\r
-               if (this.#locked) throw new Error('wallet must be unlocked to sign')\r
-               if (this.#s == null) throw new Error('wallet seed not found')\r
+               if (this.#locked) throw new Error('Wallet must be unlocked to sign')\r
+               if (this.#s == null) throw new Error('Wallet seed not found')\r
                try {\r
-                       const account = this.account(index)\r
-                       return (await account).sign(block, new Uint8Array(this.#s))\r
+                       const account = await this.account(index)\r
+                       return await account.sign(block, this.seed)\r
                } catch (err) {\r
-                       throw new Error(`failed to sign block`, { cause: err })\r
+                       throw new Error(`Failed to sign block`, { cause: err })\r
                }\r
        }\r
 \r
index 355f53f370c3d0fd6f5e6ef77b855f06c62a4ed5..ec24c9cdea1d72256d8a65fb3ddf5cb7d11a80e3 100644 (file)
@@ -23,6 +23,9 @@ export const NANO_TEST_VECTORS = Object.freeze({
        // from nano.org python sample code
        BLAKE2B_SEED: '0000000000000000000000000000000000000000000000000000000000000001',
        BLAKE2B_PRIVATE_1: '1495F2D49159CC2EAAAA97EBB42346418E1268AFF16D7FCA90E6BAD6D0965520',
+       // derived from, but not provided by, python sample code
+       BLAKE2B_PUBLIC_1: '8F26EF538DE2D678FF8524CCF07C089E90844B204C821D74AEAE416F5C301604',
+       BLAKE2B_ADDRESS_1: 'nano_35s8xxbrurpph5zrcb8ey3y1j9niij7k1m645otcxdk3fxg517i6j5empshy',
 
        // from nano.org test vectors
        MNEMONIC: 'edge defense waste choose enrich upon flee junk siren film clown finish luggage leader kid quick brick print evidence swap drill paddle truly occur',
index 29eb02e0e8c7ca58652858311293f614c9e964eb..8c570c1b5c4d7264f2558ee3fece73ea2f8f396b 100644 (file)
@@ -27,9 +27,7 @@ await Promise.all([
                        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
-                       const privateKey = await account.export(wallet.seed, 'hex')\r
 \r
-                       assert.equal(privateKey, NANO_TEST_VECTORS.PRIVATE_0)\r
                        assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_0)\r
                        assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_0)\r
                        assert.equal(account.index, 0)\r
@@ -45,15 +43,11 @@ await Promise.all([
                        const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED)\r
                        await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
                        const accounts = await wallet.accounts(1, 2)\r
-                       const privateKey1 = await accounts[1].export(wallet.seed, 'hex')\r
-                       const privateKey2 = await accounts[2].export(wallet.seed, 'hex')\r
 \r
                        assert.equal(accounts.length, 2)\r
-                       assert.equal(privateKey1, NANO_TEST_VECTORS.PRIVATE_1)\r
                        assert.equal(accounts[1].publicKey, NANO_TEST_VECTORS.PUBLIC_1)\r
                        assert.equal(accounts[1].address, NANO_TEST_VECTORS.ADDRESS_1)\r
                        assert.equal(accounts[1].index, 1)\r
-                       assert.equal(privateKey2, NANO_TEST_VECTORS.PRIVATE_2)\r
                        assert.equal(accounts[2].publicKey, NANO_TEST_VECTORS.PUBLIC_2)\r
                        assert.equal(accounts[2].address, NANO_TEST_VECTORS.ADDRESS_2)\r
                        assert.equal(accounts[2].index, 2)\r
@@ -73,8 +67,6 @@ await Promise.all([
                                assert.equal(a.index, i)\r
                                assert.exists(a.address)\r
                                assert.exists(a.publicKey)\r
-                               const privateKey = await a.export(wallet.seed, 'hex')\r
-                               assert.exists(privateKey)\r
                        }\r
 \r
                        await assert.resolves(wallet.destroy())\r
@@ -87,15 +79,15 @@ await Promise.all([
                        const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BLAKE2B_SEED)\r
                        await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
                        const account = await wallet.account(1)\r
-                       const privateKey = await account.export(wallet.seed, 'hex')\r
 \r
-                       assert.equal(privateKey, NANO_TEST_VECTORS.BLAKE2B_PRIVATE_1)\r
-                       // assert.equal(account.publicKey, NANO_TEST_VECTORS.BLAKE2B_PUBLIC_0)\r
-                       // assert.equal(account.address, NANO_TEST_VECTORS.BLAKE2B_ADDRESS_0)\r
+                       assert.equal(account.publicKey, NANO_TEST_VECTORS.BLAKE2B_PUBLIC_1)\r
+                       assert.equal(account.address, NANO_TEST_VECTORS.BLAKE2B_ADDRESS_1)\r
                        assert.equal(account.index, 1)\r
 \r
                        const accounts = await wallet.accounts(1)\r
                        assert.exists(accounts[1])\r
+                       assert.exists(accounts[1].publicKey)\r
+                       assert.exists(accounts[1].address)\r
                        assert.equal(account, accounts[1])\r
 \r
                        await assert.resolves(wallet.destroy())\r
@@ -104,19 +96,15 @@ await Promise.all([
                await test('derive low indexed accounts from the given BLAKE2B seed', async () => {\r
                        const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BLAKE2B_SEED)\r
                        await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
-                       const accounts = await wallet.accounts(1, 2)\r
-                       // const privateKey1 = await accounts[1].export(wallet.seed, 'hex')\r
-                       // const privateKey2 = await accounts[2].export(wallet.seed, 'hex')\r
+                       const accounts = await wallet.accounts(2, 3)\r
 \r
                        assert.equal(accounts.length, 2)\r
-                       // assert.equal(privateKey1, NANO_TEST_VECTORS.BLAKE2B_PRIVATE_1)\r
-                       // assert.equal(accounts[1].publicKey, NANO_TEST_VECTORS.BLAKE2B_PUBLIC_1)\r
-                       // assert.equal(accounts[1].address, NANO_TEST_VECTORS.BLAKE2B_ADDRESS_1)\r
-                       assert.equal(accounts[1].index, 1)\r
-                       // assert.equal(privateKey2, NANO_TEST_VECTORS.BLAKE2B_PRIVATE_2)\r
-                       // assert.equal(accounts[2].publicKey, NANO_TEST_VECTORS.BLAKE2B_PUBLIC_2)\r
-                       // assert.equal(accounts[2].address, NANO_TEST_VECTORS.BLAKE2B_ADDRESS_2)\r
+                       assert.exists(accounts[2].publicKey)\r
+                       assert.exists(accounts[2].address)\r
                        assert.equal(accounts[2].index, 2)\r
+                       assert.exists(accounts[3].publicKey)\r
+                       assert.exists(accounts[3].address)\r
+                       assert.equal(accounts[3].index, 3)\r
 \r
                        await assert.resolves(wallet.destroy())\r
                })\r
@@ -133,8 +121,6 @@ await Promise.all([
                                assert.equal(a.index, i)\r
                                assert.exists(a.address)\r
                                assert.exists(a.publicKey)\r
-                               const privateKey = await a.export(wallet.seed, 'hex')\r
-                               assert.exists(privateKey)\r
                        }\r
 \r
                        await assert.resolves(wallet.destroy())\r
index a7b5dfd361b556d5ac9b95170dbfd16341e8c360..78ead7fa9d40bdb35df41c70119af283ceb880e9 100644 (file)
@@ -40,9 +40,6 @@ await Promise.all([
                        assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_0)\r
                        assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_0)\r
 \r
-                       const privateKey = await account.export(wallet.seed, 'hex')\r
-                       assert.equal(privateKey, NANO_TEST_VECTORS.PRIVATE_0)\r
-\r
                        await assert.resolves(wallet.destroy())\r
                })\r
 \r
@@ -59,9 +56,6 @@ await Promise.all([
                        assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_0)\r
                        assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_0)\r
 \r
-                       const privateKey = await account.export(wallet.seed, 'hex')\r
-                       assert.equal(privateKey, NANO_TEST_VECTORS.PRIVATE_0)\r
-\r
                        await assert.resolves(wallet.destroy())\r
                })\r
 \r
@@ -75,9 +69,6 @@ await Promise.all([
                        assert.equal(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_0)\r
                        assert.equal(account.address, CUSTOM_TEST_VECTORS.ADDRESS_0)\r
 \r
-                       const privateKey = await account.export(wallet.seed, 'hex')\r
-                       assert.equal(privateKey, CUSTOM_TEST_VECTORS.PRIVATE_0)\r
-\r
                        await assert.resolves(wallet.destroy())\r
                })\r
 \r
@@ -91,9 +82,6 @@ await Promise.all([
                        assert.equal(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_1)\r
                        assert.equal(account.address, CUSTOM_TEST_VECTORS.ADDRESS_1)\r
 \r
-                       const privateKey = await account.export(wallet.seed, 'hex')\r
-                       assert.equal(privateKey, CUSTOM_TEST_VECTORS.PRIVATE_1)\r
-\r
                        await assert.resolves(wallet.destroy())\r
                })\r
 \r
@@ -107,9 +95,6 @@ await Promise.all([
                        assert.equal(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_2)\r
                        assert.equal(account.address, CUSTOM_TEST_VECTORS.ADDRESS_2)\r
 \r
-                       const privateKey = await account.export(wallet.seed, 'hex')\r
-                       assert.equal(privateKey, CUSTOM_TEST_VECTORS.PRIVATE_2)\r
-\r
                        await assert.resolves(wallet.destroy())\r
                })\r
 \r
@@ -123,9 +108,6 @@ await Promise.all([
                        assert.equal(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_3)\r
                        assert.equal(account.address, CUSTOM_TEST_VECTORS.ADDRESS_3)\r
 \r
-                       const privateKey = await account.export(wallet.seed, 'hex')\r
-                       assert.equal(privateKey, CUSTOM_TEST_VECTORS.PRIVATE_3)\r
-\r
                        await assert.resolves(wallet.destroy())\r
                })\r
 \r
@@ -144,8 +126,6 @@ await Promise.all([
                                assert.exists(accounts[i])\r
                                assert.exists(accounts[i].address)\r
                                assert.exists(accounts[i].publicKey)\r
-                               const privateKey = await accounts[i].export(wallet.seed, 'hex')\r
-                               assert.exists(privateKey)\r
                        }\r
 \r
                        await assert.resolves(wallet.destroy())\r
@@ -166,8 +146,6 @@ await Promise.all([
                                assert.exists(accounts[i])\r
                                assert.exists(accounts[i].address)\r
                                assert.exists(accounts[i].publicKey)\r
-                               const privateKey = await accounts[i].export(wallet.seed, 'hex')\r
-                               assert.exists(privateKey)\r
                        }\r
 \r
                        await assert.resolves(wallet.destroy())\r
@@ -186,14 +164,10 @@ await Promise.all([
                        assert.ok(accounts[0] instanceof Account)\r
                        assert.equal(accounts[0].publicKey, TREZOR_TEST_VECTORS.BLAKE2B_1_PUBLIC_0)\r
                        assert.equal(accounts[0].address, TREZOR_TEST_VECTORS.BLAKE2B_1_ADDRESS_0)\r
-                       const privateKey0 = await accounts[0].export(wallet.seed, 'hex')\r
-                       assert.equal(privateKey0, TREZOR_TEST_VECTORS.BLAKE2B_1_PRIVATE_0)\r
 \r
                        assert.ok(accounts[1] instanceof Account)\r
                        assert.equal(accounts[1].publicKey, TREZOR_TEST_VECTORS.BLAKE2B_1_PUBLIC_1)\r
                        assert.equal(accounts[1].address, TREZOR_TEST_VECTORS.BLAKE2B_1_ADDRESS_1)\r
-                       const privateKey1 = await accounts[1].export(wallet.seed, 'hex')\r
-                       assert.equal(privateKey1, TREZOR_TEST_VECTORS.BLAKE2B_1_PRIVATE_1)\r
 \r
                        await assert.resolves(wallet.destroy())\r
                })\r
@@ -202,7 +176,6 @@ await Promise.all([
                        const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_2)\r
                        await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
                        const walletAccount = await wallet.account()\r
-                       const walletAccountPrivateKey = await walletAccount.export(wallet.seed, 'hex')\r
 \r
                        assert.ok('mnemonic' in wallet)\r
                        assert.ok('seed' in wallet)\r
@@ -212,12 +185,10 @@ await Promise.all([
                        const imported = await Blake2bWallet.fromMnemonic(TREZOR_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_2)\r
                        await imported.unlock(TREZOR_TEST_VECTORS.PASSWORD)\r
                        const importedAccount = await imported.account()\r
-                       const importedAccountPrivateKey = await importedAccount.export(imported.seed, 'hex')\r
 \r
                        assert.equal(imported.mnemonic, wallet.mnemonic)\r
                        assert.equal(imported.seed, wallet.seed)\r
                        assert.equal(importedAccount.publicKey, walletAccount.publicKey)\r
-                       assert.equal(importedAccountPrivateKey, walletAccountPrivateKey)\r
 \r
                        await assert.resolves(wallet.destroy())\r
                        await assert.resolves(imported.destroy())\r
@@ -236,9 +207,6 @@ await Promise.all([
                        assert.equal(account.publicKey, TREZOR_TEST_VECTORS.BLAKE2B_3_PUBLIC_0)\r
                        assert.equal(account.address, TREZOR_TEST_VECTORS.BLAKE2B_3_ADDRESS_0)\r
 \r
-                       const privateKey = await account.export(wallet.seed, 'hex')\r
-                       assert.equal(privateKey, TREZOR_TEST_VECTORS.BLAKE2B_3_PRIVATE_0)\r
-\r
                        await assert.resolves(wallet.destroy())\r
                })\r
 \r
index 51b8bb53e4e314457dc638115b4ec966d887bd83..b4c53ed4cea27baffd583ce47807bc04e1232eb1 100644 (file)
@@ -88,7 +88,6 @@ await Promise.all([
                        assert.ok(account instanceof Account)
                        assert.exists(account.address)
                        assert.exists(account.publicKey)
-                       await assert.rejects(account.export(''))
                })
 
                await test('get second and third accounts', async () => {
index a9ed5c586d0147405d3c86c122076836dae5417d..bcd3ee86a5f485a1dad801d89e76439d5dba9741 100644 (file)
@@ -55,11 +55,8 @@ await Promise.all([
                        assert.equal(lockResult, true)\r
 \r
                        const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
-                       const privateKey = await account.export(wallet.seed, 'hex')\r
 \r
                        assert.equal(unlockResult, true)\r
-                       assert.equal(privateKey, NANO_TEST_VECTORS.PRIVATE_0)\r
-\r
                        await assert.resolves(wallet.destroy())\r
                })\r
 \r
@@ -165,11 +162,8 @@ await Promise.all([
                        assert.equal(lockResult, true)\r
 \r
                        const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
-                       const privateKey = await account.export(wallet.seed, 'hex')\r
 \r
                        assert.equal(unlockResult, true)\r
-                       assert.equal(privateKey, TREZOR_TEST_VECTORS.BLAKE2B_PRIVATE_0)\r
-\r
                        await assert.resolves(wallet.destroy())\r
                })\r
 \r
index db726665a8f37504923abc5f7061fe2319dc6cb2..a9d8b6a3c81b24988856cced9fcabb36b6f789f8 100644 (file)
@@ -125,7 +125,9 @@ await Promise.all([
                        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
-                       const privateKey = await account.export(wallet.seed, 'hex')\r
+                       if (account.index == null) {\r
+                               throw new Error('Account index missing')\r
+                       }\r
                        const sendBlock = new SendBlock(\r
                                account.address,\r
                                '5618869000000000000000000000000',\r
@@ -134,8 +136,11 @@ await Promise.all([
                                'nano_1stofnrxuz3cai7ze75o174bpm7scwj9jn3nxsn8ntzg784jf1gzn1jjdkou',\r
                                '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D',\r
                        )\r
-                       await sendBlock.sign(privateKey)\r
+\r
+                       const signature = await wallet.sign(account.index, sendBlock)\r
                        const valid = await sendBlock.verify(account.publicKey)\r
+\r
+                       assert.equal(signature, sendBlock.signature)\r
                        assert.equal(valid, true)\r
 \r
                        await assert.resolves(wallet.destroy())\r
@@ -145,7 +150,9 @@ await Promise.all([
                        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
-                       const privateKey = await account.export(wallet.seed, 'hex')\r
+                       if (account.index == null) {\r
+                               throw new Error('Account index missing')\r
+                       }\r
                        const sendBlock = new SendBlock(\r
                                account.address,\r
                                '5618869000000000000000000000000',\r
@@ -154,7 +161,10 @@ await Promise.all([
                                'nano_1stofnrxuz3cai7ze75o174bpm7scwj9jn3nxsn8ntzg784jf1gzn1jjdkou',\r
                                '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D',\r
                        )\r
-                       await sendBlock.sign(privateKey)\r
+                       assert.equal(account.index, 0)\r
+                       const signature = await wallet.sign(account.index, sendBlock)\r
+\r
+                       assert.equal(signature, sendBlock.signature)\r
 \r
                        sendBlock.account = Account.import('nano_1q3hqecaw15cjt7thbtxu3pbzr1eihtzzpzxguoc37bj1wc5ffoh7w74gi6p')\r
                        const valid = await sendBlock.verify(account.publicKey)\r