From 9c3ae84c28aa973785b4fc364ed9d788fa003ddf Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 21 Jul 2025 17:35:14 -0700 Subject: [PATCH] Add locked wallet destroy test. Return null seed when locked. --- src/lib/account.ts | 2 +- src/lib/wallets/wallet.ts | 6 +++++- test/test.create-wallet.mjs | 10 ++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/lib/account.ts b/src/lib/account.ts index a3abc10..2cd9842 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -72,7 +72,7 @@ export class Account { async destroy (): Promise { await SafeWorker.assign({ method: 'destroy', - name: this.publicKey, + [this.publicKey]: this.publicKey, store: 'Account' }) this.#frontier = undefined diff --git a/src/lib/wallets/wallet.ts b/src/lib/wallets/wallet.ts index 2157b8a..50e9da8 100644 --- a/src/lib/wallets/wallet.ts +++ b/src/lib/wallets/wallet.ts @@ -30,7 +30,7 @@ export abstract class Wallet { get isLocked () { return this.#locked } get isUnlocked () { return !this.#locked } get mnemonic () { return this.#m instanceof Bip39Mnemonic ? this.#m.phrase : null } - get seed () { return bytes.toHex(this.#s) } + get seed () { return 0 === +(bytes.toHex(this.#s)) ? null : bytes.toHex(this.#s) } constructor (id: Entropy, seed?: Uint8Array, mnemonic?: Bip39Mnemonic) { if (this.constructor === Wallet) { @@ -86,6 +86,10 @@ 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 (from > to) { const swap = from from = to diff --git a/test/test.create-wallet.mjs b/test/test.create-wallet.mjs index 6209492..d899a44 100644 --- a/test/test.create-wallet.mjs +++ b/test/test.create-wallet.mjs @@ -10,6 +10,16 @@ import { Bip44Wallet, Blake2bWallet } from '../dist/main.min.js' await Promise.all([ suite('Create wallets', async () => { + 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) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) -- 2.47.3