From b89e0b1584145550a066aa191b0201914c250a0f Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 9 Aug 2025 22:16:28 -0700 Subject: [PATCH] Extract wallet load into separate module. --- src/lib/wallet/ledger.ts | 2 +- src/lib/wallet/load.ts | 48 +++++++++++++++++++++++++ src/lib/wallet/wallet.ts | 66 +++++++++++++++-------------------- test/test.blocks.mjs | 6 ++-- test/test.derive-accounts.mjs | 12 +++---- test/test.import-wallet.mjs | 44 +++++++++++------------ test/test.lock-unlock.mjs | 12 +++---- test/test.tools.mjs | 6 ++-- 8 files changed, 117 insertions(+), 79 deletions(-) create mode 100644 src/lib/wallet/load.ts diff --git a/src/lib/wallet/ledger.ts b/src/lib/wallet/ledger.ts index eab4086..024aac8 100644 --- a/src/lib/wallet/ledger.ts +++ b/src/lib/wallet/ledger.ts @@ -354,7 +354,7 @@ export class Ledger extends Wallet { */ async verify (mnemonic: string): Promise async verify (secret: string): Promise { - const testWallet = await Wallet.load('BIP-44', '', secret) + const testWallet = await Wallet.load('', 'BIP-44', secret) await testWallet.unlock('') const testAccount = await testWallet.account(0) const testOpenBlock = await new Block(testAccount.address, '0', testAccount.publicKey, testAccount.address) diff --git a/src/lib/wallet/load.ts b/src/lib/wallet/load.ts new file mode 100644 index 0000000..b2eb3c5 --- /dev/null +++ b/src/lib/wallet/load.ts @@ -0,0 +1,48 @@ +//! SPDX-FileCopyrightText: 2025 Chris Duncan +//! SPDX-License-Identifier: GPL-3.0-or-later + +import { Bip39 } from '#crypto' +import { NamedData } from '#types' +import { hex, utf8 } from '../convert' +import { Database } from '../database' +import { Wallet } from './wallet' + +/** +* Imports an existing HD wallet by using an entropy value generated using a +* cryptographically strong pseudorandom number generator.NamedD +* +* @param {string} password - Encrypts the wallet to lock and unlock it +* @param {string} [salt=''] - Used when generating the final seed +* @returns {Wallet} A newly instantiated Wallet +*/ +export async function _load (wallet: Wallet, password: ArrayBuffer, secret: string, mnemonicSalt?: string): Promise { + try { + const data: NamedData = { + action: 'load', + type: wallet.type, + password + } + if (/^(?:[A-F0-9]{64}){1,2}$/i.test(secret)) { + data.seed = hex.toBuffer(secret) + } else if (await Bip39.validate(secret)) { + data.mnemonicPhrase = secret.toLowerCase() + if (mnemonicSalt != null) data.mnemonicSalt = mnemonicSalt + } else { + throw new TypeError('Invalid wallet data') + } + const result = wallet.safe.request(data) + const { iv, salt, encrypted } = await result + const record = { + id: wallet.id, + type: wallet.type, + iv, + salt, + encrypted + } + await Database.add({ [wallet.id]: record }, Wallet.DB_NAME) + return wallet + } catch (err) { + await wallet.destroy() + throw new Error('Error creating new Wallet', { cause: err }) + } +} diff --git a/src/lib/wallet/wallet.ts b/src/lib/wallet/wallet.ts index bffd245..1a36248 100644 --- a/src/lib/wallet/wallet.ts +++ b/src/lib/wallet/wallet.ts @@ -8,6 +8,7 @@ import { Block } from '../block' import { ADDRESS_GAP } from '../constants' import { bytes, hex, utf8 } from '../convert' import { Database } from '../database' +import { _load } from './load' import { Rpc } from '../rpc' import { default as SafeWorker } from './safe' import { WorkerQueue } from './worker-queue' @@ -88,52 +89,41 @@ export class Wallet { * Imports an existing HD wallet by using an entropy value generated using a * cryptographically strong pseudorandom number generator.NamedD * - * @param {string} password - Encrypts the wallet to lock and unlock it - * @param {string} [salt=''] - Used when generating the final seed - * @returns {Wallet} A newly instantiated Wallet + * @param {string} password - Encrypts the wallet to lock and unlock it. Discard as soon as possible after loading the wallet. + * @param {string} type - Algorithm used to generate wallet and child accounts + * @param {string} seed - Used to derive child accounts + * @returns Wallet in a locked state */ - static async load (type: 'BIP-44' | 'BLAKE2b', password: string, seed: string): Promise + static async load (password: string, type: 'BIP-44' | 'BLAKE2b', seed: string): Promise /** * Imports an existing HD wallet by using an entropy value generated using a * cryptographically strong pseudorandom number generator. * - * @param {string} password - Encrypts the wallet to lock and unlock it - * @param {string} [salt=''] - Used when generating the final seed - * @returns {Wallet} A newly instantiated Wallet + * @param {string} password - Encrypts the wallet to lock and unlock it. Discard as soon as possible after loading the wallet. + * @param {string} type - Algorithm used to generate wallet and child accounts + * @param {string} mnemonicPhrase - Used to derive the wallet seed + * @param {string} [mnemonicSalt] - Used to alter the seed derived from the mnemonic phrase + * @returns Wallet in a locked state */ - static async load (type: 'BIP-44' | 'BLAKE2b', password: string, mnemonicPhrase: string, mnemonicSalt?: string): Promise - static async load (type: 'BIP-44' | 'BLAKE2b', password: string, secret: string, mnemonicSalt?: string): Promise { + static async load (password: string, type: 'BIP-44' | 'BLAKE2b', mnemonicPhrase: string, mnemonicSalt?: string): Promise + static async load (password: unknown, type: unknown, secret: unknown, mnemonicSalt?: unknown): Promise { + if (typeof password !== 'string') { + throw new TypeError('Password must be a string') + } + const passwordBuffer = utf8.toBuffer(password) + password = '' + if (type !== 'BIP-44' && type !== 'BLAKE2b') { + throw new TypeError('Invalid wallet type', { cause: type }) + } + if (typeof secret !== 'string') { + throw new TypeError('Wallet secret must be a string') + } + if (mnemonicSalt !== undefined && typeof mnemonicSalt !== 'string') { + throw new TypeError('Mnemonic salt must be a string') + } Wallet.#isInternal = true const self = new this(type) - try { - const data: NamedData = { - action: 'load', - type, - password: utf8.toBuffer(password) - } - if (/^(?:[A-F0-9]{64}){1,2}$/i.test(secret)) { - data.seed = hex.toBuffer(secret) - } else if (await Bip39.validate(secret)) { - data.mnemonicPhrase = secret.toLowerCase() - if (mnemonicSalt != null) data.mnemonicSalt = mnemonicSalt - } else { - throw new TypeError('Invalid wallet data') - } - const result = self.#safe.request(data) - const { iv, salt, encrypted } = await result - const record = { - id: self.id, - type, - iv, - salt, - encrypted - } - await Database.add({ [self.id]: record }, Wallet.DB_NAME) - return self - } catch (err) { - await self.destroy() - throw new Error('Error creating new Wallet', { cause: err }) - } + return _load(self, passwordBuffer, secret, mnemonicSalt) } /** diff --git a/test/test.blocks.mjs b/test/test.blocks.mjs index 81fe264..900b55e 100644 --- a/test/test.blocks.mjs +++ b/test/test.blocks.mjs @@ -98,7 +98,7 @@ await Promise.all([ const { ADDRESS_0, BIP39_SEED, BLAKE2B_ADDRESS_1, BLAKE2B_PUBLIC_1, BLAKE2B_SEED, OPEN_BLOCK, PASSWORD, PRIVATE_0, RECEIVE_BLOCK, SEND_BLOCK } = NANO_TEST_VECTORS await test('sign open block with BLAKE2b wallet', async () => { - const wallet = await Wallet.load('BLAKE2b', PASSWORD, BLAKE2B_SEED) + const wallet = await Wallet.load(PASSWORD, 'BLAKE2b', BLAKE2B_SEED) await assert.resolves(wallet.unlock(PASSWORD)) const block = new Block(BLAKE2B_ADDRESS_1, '0', OPEN_BLOCK.previous, OPEN_BLOCK.representative) @@ -111,7 +111,7 @@ await Promise.all([ }) await test('sign open block with BIP-44 wallet', async () => { - const wallet = await Wallet.load('BIP-44', PASSWORD, BIP39_SEED) + const wallet = await Wallet.load(PASSWORD, 'BIP-44', BIP39_SEED) await assert.resolves(wallet.unlock(PASSWORD)) const block = new Block(ADDRESS_0, '0', OPEN_BLOCK.previous, OPEN_BLOCK.representative) @@ -123,7 +123,7 @@ await Promise.all([ }) await test('fail to sign open block with wallet when locked', async () => { - const wallet = await Wallet.load('BIP-44', PASSWORD, BIP39_SEED) + const wallet = await Wallet.load(PASSWORD, 'BIP-44', BIP39_SEED) const block = new Block(ADDRESS_0, '0', OPEN_BLOCK.previous, OPEN_BLOCK.representative) .receive(OPEN_BLOCK.link, OPEN_BLOCK.balance) diff --git a/test/test.derive-accounts.mjs b/test/test.derive-accounts.mjs index 1fee8fd..dda65fb 100644 --- a/test/test.derive-accounts.mjs +++ b/test/test.derive-accounts.mjs @@ -20,7 +20,7 @@ await Promise.all([ suite('Derive accounts from BIP-44 wallet', async () => { await test('derive the first account from the given BIP-44 seed', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -36,7 +36,7 @@ await Promise.all([ }) await test('derive low indexed accounts from the given BIP-44 seed', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts(1, 2) @@ -52,7 +52,7 @@ await Promise.all([ }) await test('derive high indexed accounts from the given seed', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts(0x70000000, 0x7000000f) @@ -72,7 +72,7 @@ await Promise.all([ suite('Derive accounts from BLAKE2b wallet', async () => { await test('derive the second account from the given BLAKE2b seed', async () => { - const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BLAKE2B_SEED) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BLAKE2b', NANO_TEST_VECTORS.BLAKE2B_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account(1) @@ -90,7 +90,7 @@ await Promise.all([ }) await test('derive low indexed accounts from the given BLAKE2B seed', async () => { - const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BLAKE2B_SEED) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BLAKE2b', NANO_TEST_VECTORS.BLAKE2B_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts(2, 3) @@ -106,7 +106,7 @@ await Promise.all([ }) await test('derive high indexed accounts from the given seed', async () => { - const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BLAKE2B_SEED) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BLAKE2b', NANO_TEST_VECTORS.BLAKE2B_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts(0x70000000, 0x7000000f) diff --git a/test/test.import-wallet.mjs b/test/test.import-wallet.mjs index 784199c..925f797 100644 --- a/test/test.import-wallet.mjs +++ b/test/test.import-wallet.mjs @@ -24,7 +24,7 @@ await Promise.all([ suite('Import wallets', async () => { await test('nano.org BIP-44 test vector mnemonic', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -38,7 +38,7 @@ await Promise.all([ }) await test('nano.org BIP-44 test vector seed with no mnemonic', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -54,7 +54,7 @@ await Promise.all([ }) await test('Trezor-derived BIP-44 entropy for 12-word mnemonic', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.MNEMONIC_0) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', CUSTOM_TEST_VECTORS.MNEMONIC_0) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -67,7 +67,7 @@ await Promise.all([ }) await test('Trezor-derived BIP-44 entropy for 15-word mnemonic', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.MNEMONIC_1) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', CUSTOM_TEST_VECTORS.MNEMONIC_1) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -80,7 +80,7 @@ await Promise.all([ }) await test('Trezor-derived BIP-44 entropy for 18-word mnemonic', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.MNEMONIC_2) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', CUSTOM_TEST_VECTORS.MNEMONIC_2) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -93,7 +93,7 @@ await Promise.all([ }) await test('Trezor-derived BIP-44 entropy for 21-word mnemonic', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.MNEMONIC_3) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', CUSTOM_TEST_VECTORS.MNEMONIC_3) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -106,7 +106,7 @@ await Promise.all([ }) await test('BIP-44 zero-string entropy', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_0, TREZOR_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', TREZOR_TEST_VECTORS.MNEMONIC_0, TREZOR_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts(0, 3) @@ -126,7 +126,7 @@ await Promise.all([ }) await test('BLAKE2b zero-string seed', async () => { - const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_0) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BLAKE2b', TREZOR_TEST_VECTORS.MNEMONIC_0) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts(0, 3) @@ -148,7 +148,7 @@ await Promise.all([ }) await test('Trezor-derived BLAKE2b test vectors verified with third-party libraries', async () => { - const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_1) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BLAKE2b', TREZOR_TEST_VECTORS.MNEMONIC_1) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts(0, 1) @@ -167,14 +167,14 @@ await Promise.all([ }) await test('BLAKE2b seed creates identical wallet as its derived mnemonic', async () => { - const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_2) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BLAKE2b', TREZOR_TEST_VECTORS.ENTROPY_2) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const walletAccount = await wallet.account() assert.ok(await wallet.verify(TREZOR_TEST_VECTORS.MNEMONIC_2)) assert.ok(await wallet.verify(TREZOR_TEST_VECTORS.ENTROPY_2)) - const imported = await Wallet.load('BLAKE2b', TREZOR_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_2) + const imported = await Wallet.load(TREZOR_TEST_VECTORS.PASSWORD, 'BLAKE2b', TREZOR_TEST_VECTORS.MNEMONIC_2) await imported.unlock(TREZOR_TEST_VECTORS.PASSWORD) const importedAccount = await imported.account() @@ -187,7 +187,7 @@ await Promise.all([ }) await test('BLAKE2b mnemonic for maximum seed value', async () => { - const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_3) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BLAKE2b', TREZOR_TEST_VECTORS.MNEMONIC_3) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -201,27 +201,27 @@ await Promise.all([ }) await test('Reject invalid seed', async () => { - await assert.rejects(Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, '6CAF5A42BB8074314AAE20295975ECE663BE7AAD945A73613D193B0CC41C797')) - await assert.rejects(Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, '6CAF5A42BB8074314AAE20295975ECE663BE7AAD945A73613D193B0CC41C79701')) - await assert.rejects(Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0.replaceAll(/./g, 'x'))) + await assert.rejects(Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', '6CAF5A42BB8074314AAE20295975ECE663BE7AAD945A73613D193B0CC41C797')) + await assert.rejects(Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', '6CAF5A42BB8074314AAE20295975ECE663BE7AAD945A73613D193B0CC41C79701')) + await assert.rejects(Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', TREZOR_TEST_VECTORS.ENTROPY_0.replaceAll(/./g, 'x'))) }) await test('Reject invalid length seed', async () => { - await assert.rejects(Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED + 'f'), + await assert.rejects(Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.BIP39_SEED + 'f'), `Expected a ${NANO_TEST_VECTORS.BIP39_SEED.length}-character seed, but received ${NANO_TEST_VECTORS.BIP39_SEED.length + 1}-character string.`) - await assert.rejects(Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED.slice(0, -1)), + await assert.rejects(Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.BIP39_SEED.slice(0, -1)), `Expected a ${NANO_TEST_VECTORS.BIP39_SEED.length}-character seed, but received ${NANO_TEST_VECTORS.BIP39_SEED.length - 1}-character string.`) }) await test('Reject seed containing non-hex characters', async () => { - await assert.rejects(Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.SEED_0.replace(/./, 'g')), + await assert.rejects(Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', TREZOR_TEST_VECTORS.SEED_0.replace(/./, 'g')), 'Seed contains invalid hexadecimal characters.') - await assert.rejects(Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1.replace(/./, 'g')), + await assert.rejects(Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BLAKE2b', TREZOR_TEST_VECTORS.ENTROPY_1.replace(/./, 'g')), 'Seed contains invalid hexadecimal characters.') }) await test('Import BIP-44 wallet from storage using a wallet-generated ID', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) const restored = await Wallet.restore(wallet.id) assert.ok('mnemonic' in restored) @@ -242,7 +242,7 @@ await Promise.all([ }) await test('Import BLAKE2B wallet from storage using a wallet-generated ID', async () => { - const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BLAKE2b', TREZOR_TEST_VECTORS.ENTROPY_0) const restored = await Wallet.restore(wallet.id) assert.ok('mnemonic' in restored) @@ -263,7 +263,7 @@ await Promise.all([ }) await test('export wallet IDs from storage and reimport them', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) const backups = await Wallet.export() assert.ok(backups.some(record => record.id === wallet.id)) diff --git a/test/test.lock-unlock.mjs b/test/test.lock-unlock.mjs index 727cf02..e15b0f6 100644 --- a/test/test.lock-unlock.mjs +++ b/test/test.lock-unlock.mjs @@ -24,7 +24,7 @@ await Promise.all([ suite('Lock and unlock wallets', async () => { await test('locking and unlocking a Bip44Wallet with a password', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -41,7 +41,7 @@ await Promise.all([ }) await test('fail to unlock a Bip44Wallet with different passwords', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const lockResult = await wallet.lock() @@ -58,7 +58,7 @@ await Promise.all([ }) await test('fail to unlock a Bip44Wallet with invalid input', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) //@ts-expect-error @@ -70,7 +70,7 @@ await Promise.all([ }) await test('locking and unlocking a Blake2bWallet with a password', async () => { - const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BLAKE2b', TREZOR_TEST_VECTORS.ENTROPY_0) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -87,7 +87,7 @@ await Promise.all([ }) await test('fail to unlock a Blake2bWallet with different passwords', async () => { - const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BLAKE2b', TREZOR_TEST_VECTORS.ENTROPY_1) await assert.rejects(wallet.unlock(TREZOR_TEST_VECTORS.PASSWORD), { message: 'Failed to unlock wallet' }) assert.ok('mnemonic' in wallet) @@ -101,7 +101,7 @@ await Promise.all([ }) await test('fail to unlock a Blake2bWallet with no input', async () => { - const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BLAKE2b', TREZOR_TEST_VECTORS.ENTROPY_1) //@ts-expect-error await assert.rejects(wallet.unlock(), { message: 'Failed to unlock wallet' }) diff --git a/test/test.tools.mjs b/test/test.tools.mjs index b9b1f5c..fa8ae18 100644 --- a/test/test.tools.mjs +++ b/test/test.tools.mjs @@ -137,7 +137,7 @@ await Promise.all([ }) await test('should verify a block using the public key', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() if (account.index == null) { @@ -152,7 +152,7 @@ await Promise.all([ }) await test('should reject a block using the wrong public key', async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() if (account.index == null) { @@ -178,7 +178,7 @@ await Promise.all([ }) await test('sweeper fails gracefully for ineligible accounts', { skip: true }, async () => { - const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + const wallet = await Wallet.load(NANO_TEST_VECTORS.PASSWORD, 'BIP-44', NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const results = await Tools.sweep(rpc, wallet, NANO_TEST_VECTORS.ADDRESS_1) -- 2.47.3