From: Chris Duncan Date: Wed, 23 Jul 2025 15:24:23 +0000 (-0700) Subject: Destroy temporary signing account in block method. Check that wallets are destroyed... X-Git-Tag: v0.10.5~55^2~18 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=550a4197a53f677e641dc7b9b3e5619da7cef659;p=libnemo.git Destroy temporary signing account in block method. Check that wallets are destroyed during tests. --- diff --git a/src/lib/block.ts b/src/lib/block.ts index be9a740..aff883f 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -166,6 +166,7 @@ abstract class Block { try { const account = await Account.import({ index: 0, privateKey: input }, '') this.signature = await account.sign(this, '') + await account.destroy() } catch (err) { throw new Error(`Failed to sign block`, { cause: err }) } diff --git a/test/test.create-wallet.mjs b/test/test.create-wallet.mjs index b3e4bad..f53ae72 100644 --- a/test/test.create-wallet.mjs +++ b/test/test.create-wallet.mjs @@ -33,7 +33,7 @@ await Promise.all([ assert.ok('seed' in wallet) assert.ok(/^[A-Fa-f0-9]{128}$/.test(wallet.seed)) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('BLAKE2b wallet with random entropy', async () => { @@ -47,15 +47,15 @@ await Promise.all([ assert.ok('seed' in wallet) assert.ok(/^[A-Fa-f0-9]{64}$/.test(wallet.seed)) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('BIP-44 replace invalid salt with empty string', async () => { const invalidArgs = [null, true, false, 0, 1, 2, { foo: 'bar' }] for (const arg of invalidArgs) { const wallet = Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD, arg) - await assert.resolves(wallet); - (await wallet).destroy() + await assert.resolves(wallet) + await assert.resolves((await wallet).destroy()) } }) diff --git a/test/test.derive-accounts.mjs b/test/test.derive-accounts.mjs index 5dea003..b301a2f 100644 --- a/test/test.derive-accounts.mjs +++ b/test/test.derive-accounts.mjs @@ -25,7 +25,7 @@ await Promise.all([ assert.exists(accounts[0]) assert.equal(account, accounts[0]) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('derive low indexed accounts from the given BIP-44 seed', async () => { @@ -45,7 +45,7 @@ await Promise.all([ assert.equal(accounts[2].address, NANO_TEST_VECTORS.ADDRESS_2) assert.equal(accounts[2].index, 2) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('derive high indexed accounts from the given seed', async () => { @@ -64,7 +64,7 @@ await Promise.all([ assert.exists(privateKey) } - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) }), @@ -85,7 +85,7 @@ await Promise.all([ assert.exists(accounts[1]) assert.equal(account, accounts[1]) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('derive low indexed accounts from the given BLAKE2B seed', async () => { @@ -105,7 +105,7 @@ await Promise.all([ // assert.equal(accounts[2].address, NANO_TEST_VECTORS.BLAKE2B_ADDRESS_2) assert.equal(accounts[2].index, 2) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('derive high indexed accounts from the given seed', async () => { @@ -124,7 +124,7 @@ await Promise.all([ assert.exists(privateKey) } - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) }) ]) diff --git a/test/test.import-wallet.mjs b/test/test.import-wallet.mjs index ccd32e2..d56c922 100644 --- a/test/test.import-wallet.mjs +++ b/test/test.import-wallet.mjs @@ -26,7 +26,7 @@ await Promise.all([ const privateKey = await account.export(wallet.seed, 'hex') assert.equal(privateKey, NANO_TEST_VECTORS.PRIVATE_0) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('nano.org BIP-44 test vector seed with no mnemonic', async () => { @@ -45,7 +45,7 @@ await Promise.all([ const privateKey = await account.export(wallet.seed, 'hex') assert.equal(privateKey, NANO_TEST_VECTORS.PRIVATE_0) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('Trezor-derived BIP-44 entropy for 12-word mnemonic', async () => { @@ -61,7 +61,7 @@ await Promise.all([ const privateKey = await account.export(wallet.seed, 'hex') assert.equal(privateKey, CUSTOM_TEST_VECTORS.PRIVATE_0) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('Trezor-derived BIP-44 entropy for 15-word mnemonic', async () => { @@ -77,7 +77,7 @@ await Promise.all([ const privateKey = await account.export(wallet.seed, 'hex') assert.equal(privateKey, CUSTOM_TEST_VECTORS.PRIVATE_1) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('Trezor-derived BIP-44 entropy for 18-word mnemonic', async () => { @@ -93,7 +93,7 @@ await Promise.all([ const privateKey = await account.export(wallet.seed, 'hex') assert.equal(privateKey, CUSTOM_TEST_VECTORS.PRIVATE_2) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('Trezor-derived BIP-44 entropy for 21-word mnemonic', async () => { @@ -109,7 +109,7 @@ await Promise.all([ const privateKey = await account.export(wallet.seed, 'hex') assert.equal(privateKey, CUSTOM_TEST_VECTORS.PRIVATE_3) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('BIP-44 zero-string entropy', async () => { @@ -131,7 +131,7 @@ await Promise.all([ assert.exists(privateKey) } - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('BLAKE2b zero-string seed', async () => { @@ -153,7 +153,7 @@ await Promise.all([ assert.exists(privateKey) } - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('Trezor-derived BLAKE2b test vectors verified with third-party libraries', async () => { @@ -178,7 +178,7 @@ await Promise.all([ const privateKey1 = await accounts[1].export(wallet.seed, 'hex') assert.equal(privateKey1, TREZOR_TEST_VECTORS.BLAKE2B_1_PRIVATE_1) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('BLAKE2b seed creates identical wallet as its derived mnemonic', async () => { @@ -202,7 +202,8 @@ await Promise.all([ assert.equal(importedAccount.publicKey, walletAccount.publicKey) assert.equal(importedAccountPrivateKey, walletAccountPrivateKey) - await wallet.destroy() + await assert.resolves(wallet.destroy()) + await assert.resolves(imported.destroy()) }) await test('BLAKE2b mnemonic for maximum seed value', async () => { @@ -221,7 +222,7 @@ await Promise.all([ const privateKey = await account.export(wallet.seed, 'hex') assert.equal(privateKey, TREZOR_TEST_VECTORS.BLAKE2B_3_PRIVATE_0) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('Reject invalid entropy', async () => { @@ -261,7 +262,7 @@ await Promise.all([ assert.equal(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) assert.equal(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('Import BLAKE2B wallet from session storage using a wallet-generated ID', async () => { @@ -281,7 +282,7 @@ await Promise.all([ assert.equal(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_0) assert.equal(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_0) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) }) ]) diff --git a/test/test.lock-unlock.mjs b/test/test.lock-unlock.mjs index 7f46349..c4e0f3f 100644 --- a/test/test.lock-unlock.mjs +++ b/test/test.lock-unlock.mjs @@ -26,7 +26,7 @@ await Promise.all([ assert.equal(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) assert.equal(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('locking and unlocking a Bip44Wallet with random bytes', async () => { @@ -49,7 +49,7 @@ await Promise.all([ assert.equal(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) assert.equal(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('locking and unlocking a Bip44Wallet Account with a password', async () => { @@ -66,7 +66,7 @@ await Promise.all([ assert.equal(unlockResult, true) assert.equal(privateKey, NANO_TEST_VECTORS.PRIVATE_0) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('fail to unlock a Bip44Wallet with different passwords', async () => { @@ -81,7 +81,7 @@ await Promise.all([ assert.nullish(wallet.mnemonic) assert.nullish(wallet.seed) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('fail to unlock a Bip44Wallet with different random bytes', async () => { @@ -98,7 +98,7 @@ await Promise.all([ assert.nullish(wallet.mnemonic) assert.nullish(wallet.seed) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('fail to unlock a Bip44Wallet with different valid inputs', async () => { @@ -111,7 +111,7 @@ await Promise.all([ assert.nullish(wallet.mnemonic) assert.nullish(wallet.seed) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('fail to unlock a Bip44Wallet with no input', async () => { @@ -132,7 +132,7 @@ await Promise.all([ assert.nullish(wallet.mnemonic) assert.nullish(wallet.seed) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('fail to unlock a Bip44Wallet with invalid input', async () => { @@ -153,7 +153,7 @@ await Promise.all([ assert.nullish(wallet.mnemonic) assert.nullish(wallet.seed) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('locking and unlocking a Blake2bWallet with a password', async () => { @@ -172,7 +172,7 @@ await Promise.all([ assert.equal(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_0) assert.equal(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_0) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('locking and unlocking a Blake2bWallet with random bytes', async () => { @@ -196,7 +196,7 @@ await Promise.all([ assert.equal(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) assert.equal(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('locking and unlocking a Blake2bWallet Account with a password', async () => { @@ -213,7 +213,7 @@ await Promise.all([ assert.equal(unlockResult, true) assert.equal(privateKey, TREZOR_TEST_VECTORS.BLAKE2B_PRIVATE_0) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('fail to unlock a Blake2bWallet with different passwords', async () => { @@ -225,7 +225,7 @@ await Promise.all([ assert.nullish(wallet.mnemonic) assert.nullish(wallet.seed) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('fail to unlock a Blake2bWallet with different keys', async () => { @@ -242,7 +242,7 @@ await Promise.all([ assert.nullish(wallet.mnemonic) assert.nullish(wallet.seed) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('fail to unlock a Blake2bWallet with different valid inputs', async () => { @@ -255,7 +255,7 @@ await Promise.all([ assert.nullish(wallet.mnemonic) assert.nullish(wallet.seed) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('fail to unlock a Blake2bWallet with no input', async () => { @@ -276,7 +276,7 @@ await Promise.all([ assert.nullish(wallet.mnemonic) assert.nullish(wallet.seed) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('fail to unlock a Blake2bWallet with invalid input', async () => { @@ -297,7 +297,7 @@ await Promise.all([ assert.nullish(wallet.mnemonic) assert.nullish(wallet.seed) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) }) ]) diff --git a/test/test.refresh-accounts.mjs b/test/test.refresh-accounts.mjs index b4fb138..9ac26a5 100644 --- a/test/test.refresh-accounts.mjs +++ b/test/test.refresh-accounts.mjs @@ -34,7 +34,7 @@ await Promise.all([ assert.exists(account.representative?.address) assert.notEqual(account.representative?.address, '') - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('throw when refreshing unopened account', async () => { @@ -45,7 +45,7 @@ await Promise.all([ await assert.rejects(account.refresh(rpc), { message: 'Account not found' }) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('throw when referencing invalid account index', async () => { @@ -55,7 +55,7 @@ await Promise.all([ await assert.rejects(wallet.account(0x80000000), { message: 'Invalid child key index 0x80000000' }) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('throw with invalid node', async () => { @@ -67,7 +67,7 @@ await Promise.all([ await assert.rejects(account.refresh(invalidNode), { message: 'Account not found' }) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) }), @@ -82,7 +82,7 @@ await Promise.all([ assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_1) assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('return successfully for small batch size', async () => { @@ -94,7 +94,7 @@ await Promise.all([ assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_1) assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('return successfully for large batch size', async () => { @@ -106,7 +106,7 @@ await Promise.all([ assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_1) assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('should throw on invalid node URL', async () => { @@ -119,7 +119,7 @@ await Promise.all([ await assert.rejects(wallet.unopened('')) await assert.rejects(wallet.unopened('foo')) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('should throw on invalid batch size', async () => { @@ -132,7 +132,7 @@ await Promise.all([ await assert.rejects(wallet.unopened(rpc, 'foo')) await assert.rejects(wallet.unopened(rpc, { 'foo': 'bar' })) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) }), @@ -149,7 +149,7 @@ await Promise.all([ assert.exists(account.frontier) assert.equal(typeof account.frontier, 'string') - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('should get balance, frontier, and representative for multiple accounts', async () => { @@ -160,7 +160,7 @@ await Promise.all([ assert.equal(accounts.length, 1) assert.ok(accounts[0] instanceof Account) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('should handle failure gracefully', async () => { @@ -169,7 +169,7 @@ await Promise.all([ await assert.resolves(wallet.refresh(rpc, 0, 20)) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) }) ]) diff --git a/test/test.tools.mjs b/test/test.tools.mjs index f9f54c1..605930f 100644 --- a/test/test.tools.mjs +++ b/test/test.tools.mjs @@ -113,7 +113,7 @@ await Promise.all([ const valid = await sendBlock.verify(account.publicKey) assert.equal(valid, true) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) await test('should reject a block using the wrong public key', async () => { @@ -135,7 +135,8 @@ await Promise.all([ const valid = await sendBlock.verify(account.publicKey) assert.equal(valid, false) - await wallet.destroy() + await assert.resolves(sendBlock.account.destroy()) + await assert.resolves(wallet.destroy()) }) await test('sweeper throws without required parameters', async () => { @@ -147,10 +148,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 results = await Tools.sweep(rpc, wallet, NANO_TEST_VECTORS.ADDRESS_1) + assert.ok(results) assert.equal(results.length, 1) - await wallet.destroy() + await assert.resolves(wallet.destroy()) }) }) ])