From: Chris Duncan Date: Tue, 22 Jul 2025 02:51:43 +0000 (-0700) Subject: Require seed to derive accounts. Update tests to check for null seed instead of blank. X-Git-Tag: v0.10.5~55^2~32 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=57f386cc0fb08920c803220ea96946616dc1addf;p=libnemo.git Require seed to derive accounts. Update tests to check for null seed instead of blank. --- diff --git a/src/lib/wallets/bip44-wallet.ts b/src/lib/wallets/bip44-wallet.ts index 5cd1c41..43bb831 100644 --- a/src/lib/wallets/bip44-wallet.ts +++ b/src/lib/wallets/bip44-wallet.ts @@ -212,6 +212,9 @@ export class Bip44Wallet extends Wallet { * @returns {Promise} */ async ckd (indexes: number[]): Promise { + if (this.seed == null) { + throw new Error('wallet must be unlocked to derive accounts') + } const results = await Bip44CkdWorker.assign({ indexes, seed: hex.toBytes(this.seed).buffer diff --git a/src/lib/wallets/blake2b-wallet.ts b/src/lib/wallets/blake2b-wallet.ts index d416eaf..fe9e2b5 100644 --- a/src/lib/wallets/blake2b-wallet.ts +++ b/src/lib/wallets/blake2b-wallet.ts @@ -161,6 +161,9 @@ export class Blake2bWallet extends Wallet { * @returns {Promise} */ async ckd (indexes: number[]): Promise { + if (this.seed == null) { + throw new Error('wallet must be unlocked to derive accounts') + } const results = [] for (const index of indexes) { const indexHex = index.toString(16).padStart(8, '0').toUpperCase() diff --git a/src/lib/wallets/wallet.ts b/src/lib/wallets/wallet.ts index 50e9da8..98505c3 100644 --- a/src/lib/wallets/wallet.ts +++ b/src/lib/wallets/wallet.ts @@ -86,10 +86,9 @@ export abstract class Wallet { * @returns {AccountList} Object with keys of account indexes and values of the corresponding Accounts */ async accounts (from: number = 0, to: number = from): Promise { - console.log(this.seed) - if (this.seed == null) { - throw new Error('wallet must be unlocked to derive accounts') - } + if (this.seed == null) { + throw new Error('wallet must be unlocked to derive accounts') + } if (from > to) { const swap = from from = to @@ -259,7 +258,7 @@ export abstract class Wallet { seed = null } } catch (err) { - throw new Error('Failed to unlock wallet') + throw new Error('Failed to unlock wallet', { cause: err }) } finally { bytes.erase(password) } diff --git a/test/test.create-wallet.mjs b/test/test.create-wallet.mjs index d899a44..22c723e 100644 --- a/test/test.create-wallet.mjs +++ b/test/test.create-wallet.mjs @@ -13,12 +13,14 @@ await Promise.all([ await test('destroy BIP-44 wallet before unlocking', async () => { const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD) await assert.resolves(wallet.destroy()) + assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) assert.nullish(wallet.seed) + await assert.rejects(wallet.unlock(NANO_TEST_VECTORS.PASSWORD)) - }) + }) await test('BIP-44 wallet with random entropy', async () => { const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD) diff --git a/test/test.import-wallet.mjs b/test/test.import-wallet.mjs index 3c1cd1c..fa1451e 100644 --- a/test/test.import-wallet.mjs +++ b/test/test.import-wallet.mjs @@ -251,7 +251,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.equal(wallet.seed, '') + assert.nullish(wallet.seed) const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) @@ -271,7 +271,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.equal(wallet.seed, '') + assert.nullish(wallet.seed) const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) diff --git a/test/test.lock-unlock.mjs b/test/test.lock-unlock.mjs index 2a9141a..e75bdb5 100644 --- a/test/test.lock-unlock.mjs +++ b/test/test.lock-unlock.mjs @@ -16,7 +16,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.equal(wallet.seed, '') + assert.nullish(wallet.seed) const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) @@ -39,7 +39,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.equal(wallet.seed, '') + assert.nullish(wallet.seed) const unlockResult = await wallet.unlock(new Uint8Array(key)) @@ -79,7 +79,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + assert.nullish(wallet.seed) await wallet.destroy() }) @@ -96,7 +96,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + assert.nullish(wallet.seed) await wallet.destroy() }) @@ -109,7 +109,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + assert.nullish(wallet.seed) await wallet.destroy() }) @@ -130,7 +130,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + assert.nullish(wallet.seed) await wallet.destroy() }) @@ -151,7 +151,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + assert.nullish(wallet.seed) await wallet.destroy() }) @@ -162,7 +162,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.equal(wallet.seed, '') + assert.nullish(wallet.seed) const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) @@ -185,7 +185,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.equal(wallet.seed, '') + assert.nullish(wallet.seed) const unlockResult = await wallet.unlock(new Uint8Array(key)) @@ -223,7 +223,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) + assert.nullish(wallet.seed) await wallet.destroy() }) @@ -240,7 +240,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) + assert.nullish(wallet.seed) await wallet.destroy() }) @@ -253,7 +253,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) + assert.nullish(wallet.seed) await wallet.destroy() }) @@ -274,7 +274,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) + assert.nullish(wallet.seed) await wallet.destroy() }) @@ -295,7 +295,7 @@ await Promise.all([ assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.nullish(wallet.mnemonic) - assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) + assert.nullish(wallet.seed) await wallet.destroy() })