From: Chris Duncan Date: Sun, 17 Nov 2024 01:16:12 +0000 (-0800) Subject: Reorganize wallet tests to remove unknown vectors, simplify messaging, and ignore... X-Git-Tag: v0.0.20~18 X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=d9772e3d2d762e123c13668a00da3e1c5834ddad;p=libnemo.git Reorganize wallet tests to remove unknown vectors, simplify messaging, and ignore account derivation since that is tested in another file. --- diff --git a/test/create-wallet.test.mjs b/test/create-wallet.test.mjs index 26b8e77..5797a1b 100644 --- a/test/create-wallet.test.mjs +++ b/test/create-wallet.test.mjs @@ -6,97 +6,50 @@ import './GLOBALS.mjs' import { describe, it } from 'node:test' import { strict as assert } from 'assert' -import { NANO_TEST_VECTORS, TREZOR_TEST_VECTORS } from './TEST_VECTORS.js' -import { Account, Bip44Wallet, Blake2bWallet, LedgerWallet } from '../dist/main.js' +import { NANO_TEST_VECTORS } from './TEST_VECTORS.js' +import { Bip44Wallet, Blake2bWallet, LedgerWallet } from '../dist/main.js' // WARNING: Do not send any funds to the test vectors below // Test vectors from https://docs.nano.org/integration-guides/key-management/ and elsewhere -describe('generate wallet test', async () => { - it('should fail to create a wallet when using new', () => { - assert.throws(() => new Bip44Wallet()) - assert.throws(() => new Blake2bWallet()) - assert.throws(() => new LedgerWallet()) - }) +describe('creating a new wallet', async () => { - it('should fail to create a software wallet without a password', async () => { - await assert.rejects(Bip44Wallet.create()) - await assert.rejects(Blake2bWallet.create()) - }) - - it('should replace invalid salt with empty string', async () => { - const invalidArgs = [null, true, false, 0, 1, 2, { "foo": "bar" }] - for (const arg of invalidArgs) { - await assert.doesNotReject(Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD, arg), `Rejected ${arg}`) - } - }) - - it('should generate a BIP-44 wallet with random entropy', async () => { + it('BIP-44 wallet with random entropy', async () => { const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts() assert.ok('id' in wallet) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) - assert.ok(accounts[0] instanceof Account) }) - it('should generate a BLAKE2b wallet with random entropy', async () => { + it('BLAKE2b wallet with random entropy', async () => { const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts() assert.ok('id' in wallet) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) - assert.ok(accounts[0] instanceof Account) }) - it('should generate the correct wallet with the given test vector', async () => { - const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, '6CAF5A42BB8074314AAE20295975ECE663BE7AAD945A73613D193B0CC41C7970') - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts() - - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.ok(accounts[0] instanceof Account) - assert.equal(wallet.mnemonic, 'hole kiss mouse jacket also board click series citizen slight kite smoke desk diary rent mercy inflict antique edge invite slush athlete total brain') - assert.equal(wallet.seed, '1ACCDD4C25E06E47310D0C62C290EC166071D024352E003E5366E8BA6BA523F2A0CB34116AC55A238A886778880A9B2A547112FD7CFFADE81D8D8D084CCB7D36') - assert.equal(accounts[0].privateKey, 'EB18B748BCC48F824CF8A1FE92F7FC93BFC6F2A1EB9C1D40FA26D335D8A0C30F') - assert.equal(accounts[0].publicKey, 'A9EF7BBC004813CF75C5FC5C582066182D5C9CFFD42EB7EB81CEFEA8E78C47C5') - assert.equal(accounts[0].address, 'nano_3chhhgy11k1msxtwdz4wd1i8e83fdkghzo3gpzor5mqyo5mrrjy79zpw1g34') + it('BIP-44 replace invalid salt with empty string', async () => { + const invalidArgs = [null, true, false, 0, 1, 2, { "foo": "bar" }] + for (const arg of invalidArgs) { + await assert.doesNotReject(Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD, arg), `Rejected ${arg}`) + } }) - it('should generate the correct wallet with the given test vector and a seed password', async () => { - const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, '6CAF5A42BB8074314AAE20295975ECE663BE7AAD945A73613D193B0CC41C7970', NANO_TEST_VECTORS.PASSWORD) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts() - - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.ok(accounts[0] instanceof Account) - - assert.equal(wallet.mnemonic, 'hole kiss mouse jacket also board click series citizen slight kite smoke desk diary rent mercy inflict antique edge invite slush athlete total brain') - assert.notEqual(wallet.seed, '1ACCDD4C25E06E47310D0C62C290EC166071D024352E003E5366E8BA6BA523F2A0CB34116AC55A238A886778880A9B2A547112FD7CFFADE81D8D8D084CCB7D36') - assert.notEqual(accounts[0].privateKey, 'EB18B748BCC48F824CF8A1FE92F7FC93BFC6F2A1EB9C1D40FA26D335D8A0C30F') - assert.notEqual(accounts[0].publicKey, 'A9EF7BBC004813CF75C5FC5C582066182D5C9CFFD42EB7EB81CEFEA8E78C47C5') - assert.notEqual(accounts[0].address, 'nano_3chhhgy11k1msxtwdz4wd1i8e83fdkghzo3gpzor5mqyo5mrrjy79zpw1g34') - - assert.equal(wallet.seed, '146E3E2A0530848C9174D45ECEC8C3F74A7BE3F1EE832F92EB6227284121EB2E48A6B8FC469403984CD5E8F0D1ED05777C78F458D0E98C911841590E5D645DC3') - assert.equal(accounts[0].privateKey, '2D5851BD5A89B8C943078BE6AD5BBEE8AEAB77D6A4744C20D1B87D78E3286B93') - assert.equal(accounts[0].publicKey, '923B6C7E281C1C5529FD2DC848117781216A1753CFD487FC34009F3591E636D7') - assert.equal(accounts[0].address, 'nano_36jufjz4i91wcnnztdgab1aqh1b3fado9mynizy5a16z8payefpqo81zsshc') + it('fail when using new', () => { + assert.throws(() => new Bip44Wallet()) + assert.throws(() => new Blake2bWallet()) + assert.throws(() => new LedgerWallet()) }) - it('should throw when given invalid entropy with an invalid length', async () => { - assert.rejects(async () => await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, '6CAF5A42BB8074314AAE20295975ECE663BE7AAD945A73613D193B0CC41C797')) - assert.rejects(async () => await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, '6CAF5A42BB8074314AAE20295975ECE663BE7AAD945A73613D193B0CC41C79701')) - assert.rejects(async () => await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0.replaceAll(/./g, 'x'))) + it('fail without a password', async () => { + await assert.rejects(Bip44Wallet.create()) + await assert.rejects(Blake2bWallet.create()) }) -}) -describe('ledger wallet', { skip: true }, async () => { - it('should connect to ledger', async () => { + it('connect to ledger', { skip: true }, async () => { const wallet = await LedgerWallet.create() assert.ok(wallet) }) diff --git a/test/import-wallet.test.mjs b/test/import-wallet.test.mjs index ba3cbd5..30bc542 100644 --- a/test/import-wallet.test.mjs +++ b/test/import-wallet.test.mjs @@ -199,6 +199,12 @@ describe('import wallet with test vectors test', () => { }) describe('invalid wallet', async () => { + it('throw when given invalid entropy', async () => { + assert.rejects(async () => await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, '6CAF5A42BB8074314AAE20295975ECE663BE7AAD945A73613D193B0CC41C797')) + assert.rejects(async () => await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, '6CAF5A42BB8074314AAE20295975ECE663BE7AAD945A73613D193B0CC41C79701')) + assert.rejects(async () => await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0.replaceAll(/./g, 'x'))) + }) + it('should throw when given a seed with an invalid length', async () => { await assert.rejects(Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED + 'f'), { message: `Expected a ${NANO_TEST_VECTORS.BIP39_SEED.length}-character seed, but received ${NANO_TEST_VECTORS.BIP39_SEED.length + 1}-character string.` })