From 459f2cd8bd9d0a34718fbec92f5d87cf7cbce788 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 27 Jul 2025 15:39:58 -0700 Subject: [PATCH] Remove deprecated account export from tests. Fix block signature verification tests. --- src/lib/wallets/wallet.ts | 10 +++++----- test/VECTORS.mjs | 3 +++ test/test.derive-accounts.mjs | 34 ++++++++++------------------------ test/test.import-wallet.mjs | 32 -------------------------------- test/test.ledger.mjs | 1 - test/test.lock-unlock.mjs | 6 ------ test/test.tools.mjs | 18 ++++++++++++++---- 7 files changed, 32 insertions(+), 72 deletions(-) diff --git a/src/lib/wallets/wallet.ts b/src/lib/wallets/wallet.ts index 63184a0..1981193 100644 --- a/src/lib/wallets/wallet.ts +++ b/src/lib/wallets/wallet.ts @@ -270,13 +270,13 @@ export abstract class Wallet { * @returns {Promise} Hexadecimal-formatted 64-byte signature */ async sign (index: number, block: ChangeBlock | ReceiveBlock | SendBlock): Promise { - if (this.#locked) throw new Error('wallet must be unlocked to sign') - if (this.#s == null) throw new Error('wallet seed not found') + if (this.#locked) throw new Error('Wallet must be unlocked to sign') + if (this.#s == null) throw new Error('Wallet seed not found') try { - const account = this.account(index) - return (await account).sign(block, new Uint8Array(this.#s)) + const account = await this.account(index) + return await account.sign(block, this.seed) } catch (err) { - throw new Error(`failed to sign block`, { cause: err }) + throw new Error(`Failed to sign block`, { cause: err }) } } diff --git a/test/VECTORS.mjs b/test/VECTORS.mjs index 355f53f..ec24c9c 100644 --- a/test/VECTORS.mjs +++ b/test/VECTORS.mjs @@ -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', diff --git a/test/test.derive-accounts.mjs b/test/test.derive-accounts.mjs index 29eb02e..8c570c1 100644 --- a/test/test.derive-accounts.mjs +++ b/test/test.derive-accounts.mjs @@ -27,9 +27,7 @@ await Promise.all([ 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() - const privateKey = await account.export(wallet.seed, 'hex') - assert.equal(privateKey, NANO_TEST_VECTORS.PRIVATE_0) assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_0) assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_0) assert.equal(account.index, 0) @@ -45,15 +43,11 @@ await Promise.all([ const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts(1, 2) - const privateKey1 = await accounts[1].export(wallet.seed, 'hex') - const privateKey2 = await accounts[2].export(wallet.seed, 'hex') assert.equal(accounts.length, 2) - assert.equal(privateKey1, NANO_TEST_VECTORS.PRIVATE_1) assert.equal(accounts[1].publicKey, NANO_TEST_VECTORS.PUBLIC_1) assert.equal(accounts[1].address, NANO_TEST_VECTORS.ADDRESS_1) assert.equal(accounts[1].index, 1) - assert.equal(privateKey2, NANO_TEST_VECTORS.PRIVATE_2) assert.equal(accounts[2].publicKey, NANO_TEST_VECTORS.PUBLIC_2) assert.equal(accounts[2].address, NANO_TEST_VECTORS.ADDRESS_2) assert.equal(accounts[2].index, 2) @@ -73,8 +67,6 @@ await Promise.all([ assert.equal(a.index, i) assert.exists(a.address) assert.exists(a.publicKey) - const privateKey = await a.export(wallet.seed, 'hex') - assert.exists(privateKey) } await assert.resolves(wallet.destroy()) @@ -87,15 +79,15 @@ await Promise.all([ const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BLAKE2B_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account(1) - const privateKey = await account.export(wallet.seed, 'hex') - assert.equal(privateKey, NANO_TEST_VECTORS.BLAKE2B_PRIVATE_1) - // assert.equal(account.publicKey, NANO_TEST_VECTORS.BLAKE2B_PUBLIC_0) - // assert.equal(account.address, NANO_TEST_VECTORS.BLAKE2B_ADDRESS_0) + assert.equal(account.publicKey, NANO_TEST_VECTORS.BLAKE2B_PUBLIC_1) + assert.equal(account.address, NANO_TEST_VECTORS.BLAKE2B_ADDRESS_1) assert.equal(account.index, 1) const accounts = await wallet.accounts(1) assert.exists(accounts[1]) + assert.exists(accounts[1].publicKey) + assert.exists(accounts[1].address) assert.equal(account, accounts[1]) await assert.resolves(wallet.destroy()) @@ -104,19 +96,15 @@ await Promise.all([ await test('derive low indexed accounts from the given BLAKE2B seed', async () => { const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BLAKE2B_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts(1, 2) - // const privateKey1 = await accounts[1].export(wallet.seed, 'hex') - // const privateKey2 = await accounts[2].export(wallet.seed, 'hex') + const accounts = await wallet.accounts(2, 3) assert.equal(accounts.length, 2) - // assert.equal(privateKey1, NANO_TEST_VECTORS.BLAKE2B_PRIVATE_1) - // assert.equal(accounts[1].publicKey, NANO_TEST_VECTORS.BLAKE2B_PUBLIC_1) - // assert.equal(accounts[1].address, NANO_TEST_VECTORS.BLAKE2B_ADDRESS_1) - assert.equal(accounts[1].index, 1) - // assert.equal(privateKey2, NANO_TEST_VECTORS.BLAKE2B_PRIVATE_2) - // assert.equal(accounts[2].publicKey, NANO_TEST_VECTORS.BLAKE2B_PUBLIC_2) - // assert.equal(accounts[2].address, NANO_TEST_VECTORS.BLAKE2B_ADDRESS_2) + assert.exists(accounts[2].publicKey) + assert.exists(accounts[2].address) assert.equal(accounts[2].index, 2) + assert.exists(accounts[3].publicKey) + assert.exists(accounts[3].address) + assert.equal(accounts[3].index, 3) await assert.resolves(wallet.destroy()) }) @@ -133,8 +121,6 @@ await Promise.all([ assert.equal(a.index, i) assert.exists(a.address) assert.exists(a.publicKey) - const privateKey = await a.export(wallet.seed, 'hex') - assert.exists(privateKey) } await assert.resolves(wallet.destroy()) diff --git a/test/test.import-wallet.mjs b/test/test.import-wallet.mjs index a7b5dfd..78ead7f 100644 --- a/test/test.import-wallet.mjs +++ b/test/test.import-wallet.mjs @@ -40,9 +40,6 @@ await Promise.all([ assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_0) assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_0) - const privateKey = await account.export(wallet.seed, 'hex') - assert.equal(privateKey, NANO_TEST_VECTORS.PRIVATE_0) - await assert.resolves(wallet.destroy()) }) @@ -59,9 +56,6 @@ await Promise.all([ assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_0) assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_0) - const privateKey = await account.export(wallet.seed, 'hex') - assert.equal(privateKey, NANO_TEST_VECTORS.PRIVATE_0) - await assert.resolves(wallet.destroy()) }) @@ -75,9 +69,6 @@ await Promise.all([ assert.equal(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_0) assert.equal(account.address, CUSTOM_TEST_VECTORS.ADDRESS_0) - const privateKey = await account.export(wallet.seed, 'hex') - assert.equal(privateKey, CUSTOM_TEST_VECTORS.PRIVATE_0) - await assert.resolves(wallet.destroy()) }) @@ -91,9 +82,6 @@ await Promise.all([ assert.equal(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_1) assert.equal(account.address, CUSTOM_TEST_VECTORS.ADDRESS_1) - const privateKey = await account.export(wallet.seed, 'hex') - assert.equal(privateKey, CUSTOM_TEST_VECTORS.PRIVATE_1) - await assert.resolves(wallet.destroy()) }) @@ -107,9 +95,6 @@ await Promise.all([ assert.equal(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_2) assert.equal(account.address, CUSTOM_TEST_VECTORS.ADDRESS_2) - const privateKey = await account.export(wallet.seed, 'hex') - assert.equal(privateKey, CUSTOM_TEST_VECTORS.PRIVATE_2) - await assert.resolves(wallet.destroy()) }) @@ -123,9 +108,6 @@ await Promise.all([ assert.equal(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_3) assert.equal(account.address, CUSTOM_TEST_VECTORS.ADDRESS_3) - const privateKey = await account.export(wallet.seed, 'hex') - assert.equal(privateKey, CUSTOM_TEST_VECTORS.PRIVATE_3) - await assert.resolves(wallet.destroy()) }) @@ -144,8 +126,6 @@ await Promise.all([ assert.exists(accounts[i]) assert.exists(accounts[i].address) assert.exists(accounts[i].publicKey) - const privateKey = await accounts[i].export(wallet.seed, 'hex') - assert.exists(privateKey) } await assert.resolves(wallet.destroy()) @@ -166,8 +146,6 @@ await Promise.all([ assert.exists(accounts[i]) assert.exists(accounts[i].address) assert.exists(accounts[i].publicKey) - const privateKey = await accounts[i].export(wallet.seed, 'hex') - assert.exists(privateKey) } await assert.resolves(wallet.destroy()) @@ -186,14 +164,10 @@ await Promise.all([ assert.ok(accounts[0] instanceof Account) assert.equal(accounts[0].publicKey, TREZOR_TEST_VECTORS.BLAKE2B_1_PUBLIC_0) assert.equal(accounts[0].address, TREZOR_TEST_VECTORS.BLAKE2B_1_ADDRESS_0) - const privateKey0 = await accounts[0].export(wallet.seed, 'hex') - assert.equal(privateKey0, TREZOR_TEST_VECTORS.BLAKE2B_1_PRIVATE_0) assert.ok(accounts[1] instanceof Account) assert.equal(accounts[1].publicKey, TREZOR_TEST_VECTORS.BLAKE2B_1_PUBLIC_1) assert.equal(accounts[1].address, TREZOR_TEST_VECTORS.BLAKE2B_1_ADDRESS_1) - const privateKey1 = await accounts[1].export(wallet.seed, 'hex') - assert.equal(privateKey1, TREZOR_TEST_VECTORS.BLAKE2B_1_PRIVATE_1) await assert.resolves(wallet.destroy()) }) @@ -202,7 +176,6 @@ await Promise.all([ const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_2) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const walletAccount = await wallet.account() - const walletAccountPrivateKey = await walletAccount.export(wallet.seed, 'hex') assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -212,12 +185,10 @@ await Promise.all([ const imported = await Blake2bWallet.fromMnemonic(TREZOR_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_2) await imported.unlock(TREZOR_TEST_VECTORS.PASSWORD) const importedAccount = await imported.account() - const importedAccountPrivateKey = await importedAccount.export(imported.seed, 'hex') assert.equal(imported.mnemonic, wallet.mnemonic) assert.equal(imported.seed, wallet.seed) assert.equal(importedAccount.publicKey, walletAccount.publicKey) - assert.equal(importedAccountPrivateKey, walletAccountPrivateKey) await assert.resolves(wallet.destroy()) await assert.resolves(imported.destroy()) @@ -236,9 +207,6 @@ await Promise.all([ assert.equal(account.publicKey, TREZOR_TEST_VECTORS.BLAKE2B_3_PUBLIC_0) assert.equal(account.address, TREZOR_TEST_VECTORS.BLAKE2B_3_ADDRESS_0) - const privateKey = await account.export(wallet.seed, 'hex') - assert.equal(privateKey, TREZOR_TEST_VECTORS.BLAKE2B_3_PRIVATE_0) - await assert.resolves(wallet.destroy()) }) diff --git a/test/test.ledger.mjs b/test/test.ledger.mjs index 51b8bb5..b4c53ed 100644 --- a/test/test.ledger.mjs +++ b/test/test.ledger.mjs @@ -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 () => { diff --git a/test/test.lock-unlock.mjs b/test/test.lock-unlock.mjs index a9ed5c5..bcd3ee8 100644 --- a/test/test.lock-unlock.mjs +++ b/test/test.lock-unlock.mjs @@ -55,11 +55,8 @@ await Promise.all([ assert.equal(lockResult, true) const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const privateKey = await account.export(wallet.seed, 'hex') assert.equal(unlockResult, true) - assert.equal(privateKey, NANO_TEST_VECTORS.PRIVATE_0) - await assert.resolves(wallet.destroy()) }) @@ -165,11 +162,8 @@ await Promise.all([ assert.equal(lockResult, true) const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const privateKey = await account.export(wallet.seed, 'hex') assert.equal(unlockResult, true) - assert.equal(privateKey, TREZOR_TEST_VECTORS.BLAKE2B_PRIVATE_0) - await assert.resolves(wallet.destroy()) }) diff --git a/test/test.tools.mjs b/test/test.tools.mjs index db72666..a9d8b6a 100644 --- a/test/test.tools.mjs +++ b/test/test.tools.mjs @@ -125,7 +125,9 @@ await Promise.all([ 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() - const privateKey = await account.export(wallet.seed, 'hex') + if (account.index == null) { + throw new Error('Account index missing') + } const sendBlock = new SendBlock( account.address, '5618869000000000000000000000000', @@ -134,8 +136,11 @@ await Promise.all([ 'nano_1stofnrxuz3cai7ze75o174bpm7scwj9jn3nxsn8ntzg784jf1gzn1jjdkou', '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D', ) - await sendBlock.sign(privateKey) + + const signature = await wallet.sign(account.index, sendBlock) const valid = await sendBlock.verify(account.publicKey) + + assert.equal(signature, sendBlock.signature) assert.equal(valid, true) await assert.resolves(wallet.destroy()) @@ -145,7 +150,9 @@ await Promise.all([ 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() - const privateKey = await account.export(wallet.seed, 'hex') + if (account.index == null) { + throw new Error('Account index missing') + } const sendBlock = new SendBlock( account.address, '5618869000000000000000000000000', @@ -154,7 +161,10 @@ await Promise.all([ 'nano_1stofnrxuz3cai7ze75o174bpm7scwj9jn3nxsn8ntzg784jf1gzn1jjdkou', '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D', ) - await sendBlock.sign(privateKey) + assert.equal(account.index, 0) + const signature = await wallet.sign(account.index, sendBlock) + + assert.equal(signature, sendBlock.signature) sendBlock.account = Account.import('nano_1q3hqecaw15cjt7thbtxu3pbzr1eihtzzpzxguoc37bj1wc5ffoh7w74gi6p') const valid = await sendBlock.verify(account.publicKey) -- 2.47.3