From: Chris Duncan Date: Sat, 2 Aug 2025 06:24:51 +0000 (-0700) Subject: Fix wallet imports in test. X-Git-Tag: v0.10.5~47^2~23 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=405bde60d6f7d39a9918223ee4b79f5536fd23ff;p=libnemo.git Fix wallet imports in test. --- diff --git a/test/test.import-wallet.mjs b/test/test.import-wallet.mjs index b657060..c03f382 100644 --- a/test/test.import-wallet.mjs +++ b/test/test.import-wallet.mjs @@ -11,24 +11,20 @@ import { BIP32_TEST_VECTORS, CUSTOM_TEST_VECTORS, NANO_TEST_VECTORS, TREZOR_TEST */ let Account /** -* @type {typeof import('../dist/types.d.ts').Bip44Wallet} +* @type {typeof import('../dist/types.d.ts').Wallet} */ -let Bip44Wallet -/** -* @type {typeof import('../dist/types.d.ts').Blake2bWallet} -*/ -let Blake2bWallet +let Wallet if (isNode) { - ({ Account, Bip44Wallet, Blake2bWallet } = await import('../dist/nodejs.min.js')) + ({ Account, Wallet } = await import('../dist/nodejs.min.js')) } else { - ({ Account, Bip44Wallet, Blake2bWallet } = await import('../dist/browser.min.js')) + ({ Account, Wallet } = await import('../dist/browser.min.js')) } await Promise.all([ suite('Import wallets', async () => { await test('nano.org BIP-44 test vector mnemonic', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -44,7 +40,7 @@ await Promise.all([ }) await test('nano.org BIP-44 test vector seed with no mnemonic', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -60,7 +56,7 @@ await Promise.all([ }) await test('Trezor-derived BIP-44 entropy for 12-word mnemonic', async () => { - const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_0) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_0) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -73,7 +69,7 @@ await Promise.all([ }) await test('Trezor-derived BIP-44 entropy for 15-word mnemonic', async () => { - const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_1) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_1) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -86,7 +82,7 @@ await Promise.all([ }) await test('Trezor-derived BIP-44 entropy for 18-word mnemonic', async () => { - const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_2) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_2) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -99,7 +95,7 @@ await Promise.all([ }) await test('Trezor-derived BIP-44 entropy for 21-word mnemonic', async () => { - const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_3) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_3) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -112,7 +108,7 @@ await Promise.all([ }) await test('BIP-44 zero-string entropy', async () => { - const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0, TREZOR_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0, TREZOR_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts(0, 3) @@ -132,7 +128,7 @@ await Promise.all([ }) await test('BLAKE2b zero-string seed', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0) + const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts(0, 3) @@ -152,7 +148,7 @@ await Promise.all([ }) await test('Trezor-derived BLAKE2b test vectors verified with third-party libraries', async () => { - const wallet = await Blake2bWallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_1) + const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_1) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts(0, 1) @@ -173,7 +169,7 @@ await Promise.all([ }) await test('BLAKE2b seed creates identical wallet as its derived mnemonic', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_2) + const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_2) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const walletAccount = await wallet.account() @@ -183,7 +179,7 @@ await Promise.all([ assert.equal(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_2) assert.equal(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_2) - const imported = await Blake2bWallet.fromMnemonic(TREZOR_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_2) + const imported = await Wallet.import('BLAKE2b', TREZOR_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_2) await imported.unlock(TREZOR_TEST_VECTORS.PASSWORD) const importedAccount = await imported.account() @@ -196,7 +192,7 @@ await Promise.all([ }) await test('BLAKE2b mnemonic for maximum seed value', async () => { - const wallet = await Blake2bWallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_3) + const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_3) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() @@ -212,28 +208,28 @@ await Promise.all([ }) await test('Reject invalid entropy', async () => { - await assert.rejects(Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, '6CAF5A42BB8074314AAE20295975ECE663BE7AAD945A73613D193B0CC41C797')) - await assert.rejects(Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, '6CAF5A42BB8074314AAE20295975ECE663BE7AAD945A73613D193B0CC41C79701')) - await assert.rejects(Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0.replaceAll(/./g, 'x'))) + await assert.rejects(Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, '6CAF5A42BB8074314AAE20295975ECE663BE7AAD945A73613D193B0CC41C797')) + await assert.rejects(Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, '6CAF5A42BB8074314AAE20295975ECE663BE7AAD945A73613D193B0CC41C79701')) + await assert.rejects(Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0.replaceAll(/./g, 'x'))) }) await test('Reject invalid length seed', async () => { - await assert.rejects(Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED + 'f'), + await assert.rejects(Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED + 'f'), `Expected a ${NANO_TEST_VECTORS.BIP39_SEED.length}-character seed, but received ${NANO_TEST_VECTORS.BIP39_SEED.length + 1}-character string.`) - await assert.rejects(Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED.slice(0, -1)), + await assert.rejects(Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED.slice(0, -1)), `Expected a ${NANO_TEST_VECTORS.BIP39_SEED.length}-character seed, but received ${NANO_TEST_VECTORS.BIP39_SEED.length - 1}-character string.`) }) await test('Reject seed containing non-hex characters', async () => { - await assert.rejects(Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.SEED_0.replace(/./, 'g')), + await assert.rejects(Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.SEED_0.replace(/./, 'g')), 'Seed contains invalid hexadecimal characters.') - await assert.rejects(Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1.replace(/./, 'g')), + await assert.rejects(Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1.replace(/./, 'g')), 'Seed contains invalid hexadecimal characters.') }) await test('Import BIP-44 wallet from storage using a wallet-generated ID', async () => { - const id = (await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)).id - const wallet = await Bip44Wallet.restore(id) + const id = (await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)).id + const wallet = await Wallet.restore(id) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -252,8 +248,8 @@ await Promise.all([ }) await test('Import BLAKE2B wallet from storage using a wallet-generated ID', async () => { - const id = (await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0)).id - const wallet = await Blake2bWallet.restore(id) + const id = (await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0)).id + const wallet = await Wallet.restore(id) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -272,12 +268,12 @@ await Promise.all([ }) await test('export wallet IDs from storage and reimport them', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) - const ids = await Bip44Wallet.export() + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + const ids = await Wallet.export() assert.ok(ids['BIP-44'].some(id => `BIP-44_${id}` === wallet.id)) - const restored = await Bip44Wallet.restore(wallet.id) + const restored = await Wallet.restore(wallet.id) assert.ok('mnemonic' in restored) assert.ok('seed' in restored)