From: Chris Duncan Date: Tue, 22 Jul 2025 00:35:14 +0000 (-0700) Subject: Add locked wallet destroy test. Return null seed when locked. X-Git-Tag: v0.10.5~55^2~33 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=9c3ae84c28aa973785b4fc364ed9d788fa003ddf;p=libnemo.git Add locked wallet destroy test. Return null seed when locked. --- 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)