From: Chris Duncan Date: Sun, 8 Dec 2024 03:27:42 +0000 (-0800) Subject: Expand implementation of bespoke test suite. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=27862baff27624cde20e5c5882f081fc332a599e;p=libnemo.git Expand implementation of bespoke test suite. --- diff --git a/GLOBALS.mjs b/GLOBALS.mjs index 7e7d4e8..37c6c5f 100644 --- a/GLOBALS.mjs +++ b/GLOBALS.mjs @@ -62,14 +62,21 @@ console.log(`> TEST RUNNER CHECK DONE <`) console.log(` `) export function skip (name) { - console.log(`%cSKIP `, 'color:blue', name) + return new Promise(resolve => { + console.log(`%cSKIP `, 'color:blue', name) + resolve() + }) } export function suite (name, fn) { - console.group(`%c=>%c ${name}`, 'font-weight:bold;color:green', 'font-weight:bold;color:unset') if (fn.constructor.name === 'AsyncFunction') fn = fn() if (typeof fn === 'function') fn = new Promise(resolve => resolve(fn())) - fn.then(console.groupEnd) + return new Promise(async (resolve) => { + console.group(`%c${name}`, 'font-weight:bold') + await fn + console.groupEnd() + resolve() + }) } export function test (name, fn) { diff --git a/test/create-wallet.test.mjs b/test/create-wallet.test.mjs index dc051ef..f0feaab 100644 --- a/test/create-wallet.test.mjs +++ b/test/create-wallet.test.mjs @@ -3,59 +3,61 @@ 'use strict' -import { assert, skip, test } from '#GLOBALS.mjs' +import { assert, skip, suite, test } from '#GLOBALS.mjs' import { NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from '#dist/main.js' -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) - - assert.ok('id' in wallet) - assert.ok(/[A-Fa-f0-9]{32,64}/.test(wallet.id)) - assert.ok('mnemonic' in wallet) - assert.ok(/^(?:[a-z]{3,} ){11,23}[a-z]{3,}$/.test(wallet.mnemonic)) - assert.ok('seed' in wallet) - assert.ok(/[A-Fa-f0-9]{32,64}/.test(wallet.seed)) -}) - -await test('BLAKE2b wallet with random entropy', async () => { - const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - - assert.ok('id' in wallet) - assert.ok(/[A-Fa-f0-9]{32,64}/.test(wallet.id)) - assert.ok('mnemonic' in wallet) - assert.ok(/^(?:[a-z]{3,} ){11,23}[a-z]{3,}$/.test(wallet.mnemonic)) - assert.ok('seed' in wallet) - assert.ok(/[A-Fa-f0-9]{32,64}/.test(wallet.seed)) -}) - -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) { +await suite('Create wallets', async () => { + 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) + + assert.ok('id' in wallet) + assert.ok(/[A-Fa-f0-9]{32,64}/.test(wallet.id)) + assert.ok('mnemonic' in wallet) + assert.ok(/^(?:[a-z]{3,} ){11,23}[a-z]{3,}$/.test(wallet.mnemonic)) + assert.ok('seed' in wallet) + assert.ok(/[A-Fa-f0-9]{32,64}/.test(wallet.seed)) + }) + + await test('BLAKE2b wallet with random entropy', async () => { + const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + assert.ok('id' in wallet) + assert.ok(/[A-Fa-f0-9]{32,64}/.test(wallet.id)) + assert.ok('mnemonic' in wallet) + assert.ok(/^(?:[a-z]{3,} ){11,23}[a-z]{3,}$/.test(wallet.mnemonic)) + assert.ok('seed' in wallet) + assert.ok(/[A-Fa-f0-9]{32,64}/.test(wallet.seed)) + }) + + 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) { + //@ts-expect-error + await assert.resolves(Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD, arg)) + } + }) + + await test('fail when using new', async () => { //@ts-expect-error - await assert.resolves(Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD, arg)) - } -}) - -await test('fail when using new', () => { - //@ts-expect-error - assert.throws(() => new Bip44Wallet()) - //@ts-expect-error - assert.throws(() => new Blake2bWallet()) - //@ts-expect-error - assert.throws(() => new LedgerWallet()) -}) + assert.throws(() => new Bip44Wallet()) + //@ts-expect-error + assert.throws(() => new Blake2bWallet()) + //@ts-expect-error + assert.throws(() => new LedgerWallet()) + }) -await test('fail without a password', async () => { - //@ts-expect-error - await assert.rejects(Bip44Wallet.create()) - //@ts-expect-error - await assert.rejects(Blake2bWallet.create()) + await test('fail without a password', async () => { + //@ts-expect-error + await assert.rejects(Bip44Wallet.create()) + //@ts-expect-error + await assert.rejects(Blake2bWallet.create()) + }) }) -skip('connect to ledger', async () => { +await skip('connect to ledger', async () => { const wallet = await LedgerWallet.create() assert.ok(wallet) }) diff --git a/test/derive-accounts.test.mjs b/test/derive-accounts.test.mjs index ecbea6a..dca775f 100644 --- a/test/derive-accounts.test.mjs +++ b/test/derive-accounts.test.mjs @@ -3,79 +3,81 @@ 'use strict' -import { assert, skip, test } from '#GLOBALS.mjs' +import { assert, skip, suite, test } from '#GLOBALS.mjs' import { NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from '#dist/main.js' -await test('should derive the first account from the given BIP-44 seed', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts() +await suite('Account derivation', async () => { + await test('should derive the first account from the given BIP-44 seed', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.accounts() - assert.equals(accounts.length, 1) - assert.equals(accounts[0].privateKey, NANO_TEST_VECTORS.PRIVATE_0) - assert.equals(accounts[0].publicKey, NANO_TEST_VECTORS.PUBLIC_0) - assert.equals(accounts[0].address, NANO_TEST_VECTORS.ADDRESS_0) -}) + assert.equals(accounts.length, 1) + assert.equals(accounts[0].privateKey, NANO_TEST_VECTORS.PRIVATE_0) + assert.equals(accounts[0].publicKey, NANO_TEST_VECTORS.PUBLIC_0) + assert.equals(accounts[0].address, NANO_TEST_VECTORS.ADDRESS_0) + }) -await test('should derive low indexed accounts from the given BIP-44 seed', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts(1, 2) + await test('should derive low indexed accounts from the given BIP-44 seed', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.accounts(1, 2) - assert.equals(accounts.length, 2) - assert.equals(accounts[0].privateKey, NANO_TEST_VECTORS.PRIVATE_1) - assert.equals(accounts[0].publicKey, NANO_TEST_VECTORS.PUBLIC_1) - assert.equals(accounts[0].address, NANO_TEST_VECTORS.ADDRESS_1) - assert.equals(accounts[1].privateKey, NANO_TEST_VECTORS.PRIVATE_2) - assert.equals(accounts[1].publicKey, NANO_TEST_VECTORS.PUBLIC_2) - assert.equals(accounts[1].address, NANO_TEST_VECTORS.ADDRESS_2) -}) + assert.equals(accounts.length, 2) + assert.equals(accounts[0].privateKey, NANO_TEST_VECTORS.PRIVATE_1) + assert.equals(accounts[0].publicKey, NANO_TEST_VECTORS.PUBLIC_1) + assert.equals(accounts[0].address, NANO_TEST_VECTORS.ADDRESS_1) + assert.equals(accounts[1].privateKey, NANO_TEST_VECTORS.PRIVATE_2) + assert.equals(accounts[1].publicKey, NANO_TEST_VECTORS.PUBLIC_2) + assert.equals(accounts[1].address, NANO_TEST_VECTORS.ADDRESS_2) + }) -await test('should derive high indexed accounts from the given seed', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts(0x70000000, 0x700000ff) + await test('should derive high indexed accounts from the given seed', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.accounts(0x70000000, 0x700000ff) - assert.equals(accounts.length, 0x100) - for (let i = 0; i < accounts.length; i++) { - const a = accounts[i] - assert.exists(a) - assert.exists(a.address) - assert.exists(a.publicKey) - assert.exists(a.privateKey) - assert.exists(a.index) - assert.equals(a.index, i + 0x70000000) - } -}) + assert.equals(accounts.length, 0x100) + for (let i = 0; i < accounts.length; i++) { + const a = accounts[i] + assert.exists(a) + assert.exists(a.address) + assert.exists(a.publicKey) + assert.exists(a.privateKey) + assert.exists(a.index) + assert.equals(a.index, i + 0x70000000) + } + }) -await test('should derive accounts for a BLAKE2b wallet', async () => { - const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const lowAccounts = await wallet.accounts(0, 2) + await test('should derive accounts for a BLAKE2b wallet', async () => { + const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const lowAccounts = await wallet.accounts(0, 2) - assert.equals(lowAccounts.length, 3) - for (const a of lowAccounts) { - assert.exists(a) - assert.exists(a.address) - assert.exists(a.publicKey) - assert.exists(a.privateKey) - assert.exists(a.index) - } + assert.equals(lowAccounts.length, 3) + for (const a of lowAccounts) { + assert.exists(a) + assert.exists(a.address) + assert.exists(a.publicKey) + assert.exists(a.privateKey) + assert.exists(a.index) + } - const highAccounts = await wallet.accounts(0x70000000, 0x700000ff) + const highAccounts = await wallet.accounts(0x70000000, 0x700000ff) - assert.equals(highAccounts.length, 0x100) - for (const a of highAccounts) { - assert.exists(a) - assert.exists(a.address) - assert.exists(a.publicKey) - assert.exists(a.privateKey) - assert.exists(a.index) - } + assert.equals(highAccounts.length, 0x100) + for (const a of highAccounts) { + assert.exists(a) + assert.exists(a.address) + assert.exists(a.publicKey) + assert.exists(a.privateKey) + assert.exists(a.index) + } + }) }) -skip('fetch the first account from a Ledger device', async () => { +await skip('fetch the first account from a Ledger device', async () => { const wallet = await LedgerWallet.create() const accounts = await wallet.accounts() diff --git a/test/import-wallet.test.mjs b/test/import-wallet.test.mjs index b0690a9..e8187c3 100644 --- a/test/import-wallet.test.mjs +++ b/test/import-wallet.test.mjs @@ -3,249 +3,251 @@ 'use strict' -import { assert, test } from '#GLOBALS.mjs' +import { assert, suite, test } from '#GLOBALS.mjs' import { BIP32_TEST_VECTORS, CUSTOM_TEST_VECTORS, NANO_TEST_VECTORS, TREZOR_TEST_VECTORS } from '#test/TEST_VECTORS.js' import { Account, Bip44Wallet, Blake2bWallet } from '#dist/main.js' -await test('import a wallet with the official Nano test vectors mnemonic', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, 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.equals(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) - assert.equals(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) - assert.equals(accounts[0].privateKey, NANO_TEST_VECTORS.PRIVATE_0) - assert.equals(accounts[0].publicKey, NANO_TEST_VECTORS.PUBLIC_0) - assert.equals(accounts[0].address, NANO_TEST_VECTORS.ADDRESS_0) -}) - -await test('import a wallet with the checksum starting with a zero', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, 'food define cancel major spoon trash cigar basic aim bless wolf win ability seek paddle bench seed century group they mercy address monkey cake') - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - assert.equals(wallet.seed, 'F665F804E5907985455D1E5A7AD344843A2ED4179A7E06EEF263DE925FF6F4C0991B0A9344FCEE939FE0F1B1841B8C9B20FEACF6B954B74B2D26A01906B758E2') -}) - -await test('import a wallet with a 12-word phrase', async () => { - const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_0) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts() - const account = accounts[0] - - assert.equals(wallet.mnemonic, CUSTOM_TEST_VECTORS.MNEMONIC_0) - assert.equals(wallet.seed, CUSTOM_TEST_VECTORS.SEED_0) - assert.equals(account.privateKey, CUSTOM_TEST_VECTORS.PRIVATE_0) - assert.equals(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_0) - assert.equals(account.address, CUSTOM_TEST_VECTORS.ADDRESS_0) -}) - -await test('import a wallet with a 15-word phrase', async () => { - const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_1) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts() - const account = accounts[0] - - assert.equals(wallet.mnemonic, CUSTOM_TEST_VECTORS.MNEMONIC_1) - assert.equals(wallet.seed, CUSTOM_TEST_VECTORS.SEED_1) - assert.equals(account.privateKey, CUSTOM_TEST_VECTORS.PRIVATE_1) - assert.equals(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_1) - assert.equals(account.address, CUSTOM_TEST_VECTORS.ADDRESS_1) -}) - -await test('import a wallet with a 18-word phrase', async () => { - const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_2) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts() - const account = accounts[0] - - assert.equals(wallet.mnemonic, CUSTOM_TEST_VECTORS.MNEMONIC_2) - assert.equals(wallet.seed, CUSTOM_TEST_VECTORS.SEED_2) - assert.equals(account.privateKey, CUSTOM_TEST_VECTORS.PRIVATE_2) - assert.equals(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_2) - assert.equals(account.address, CUSTOM_TEST_VECTORS.ADDRESS_2) -}) - -await test('import a wallet with a 21-word phrase', async () => { - const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_3) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts() - const account = accounts[0] - - assert.equals(wallet.mnemonic, CUSTOM_TEST_VECTORS.MNEMONIC_3) - assert.equals(wallet.seed, CUSTOM_TEST_VECTORS.SEED_3) - assert.equals(account.privateKey, CUSTOM_TEST_VECTORS.PRIVATE_3) - assert.equals(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_3) - assert.equals(account.address, CUSTOM_TEST_VECTORS.ADDRESS_3) -}) - -await test('import a wallet with the official Nano test vectors seed', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - 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.equals(wallet.mnemonic, '') - assert.equals(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) - assert.equals(accounts[0].privateKey, NANO_TEST_VECTORS.PRIVATE_0) - assert.equals(accounts[0].publicKey, NANO_TEST_VECTORS.PUBLIC_0) - assert.equals(accounts[0].address, NANO_TEST_VECTORS.ADDRESS_0) -}) - -await test('import a BIP-44 wallet with the zero seed', async () => { - const wallet = await Bip44Wallet.fromEntropy(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) - - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_0) - assert.equals(wallet.seed, TREZOR_TEST_VECTORS.SEED_0.toUpperCase()) - assert.equals(accounts.length, 4) - for (let i = 0; i < accounts.length; i++) { - assert.exists(accounts[i]) - assert.exists(accounts[i].address) - assert.exists(accounts[i].publicKey) - assert.exists(accounts[i].privateKey) - assert.equals(accounts[i].index, i) - } -}) - -await test('import a BLAKE2b wallet with the zero seed', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts(0, 3) - - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_0) - assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_0) - assert.equals(accounts.length, 4) - - for (let i = 0; i < accounts.length; i++) { - assert.exists(accounts[i]) - assert.exists(accounts[i].address) - assert.exists(accounts[i].publicKey) - assert.exists(accounts[i].privateKey) - assert.equals(accounts[i].index, i) - } -}) - -await test('import a BLAKE2b wallet with Trezor test vectors', async () => { - const wallet = await Blake2bWallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_1) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts(0, 1) - - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) - assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) - assert.ok(accounts[0] instanceof Account) - assert.equals(accounts[0].index, 0) - assert.equals(accounts[0].privateKey, TREZOR_TEST_VECTORS.BLAKE2B_1_PRIVATE_0) - assert.equals(accounts[0].publicKey, TREZOR_TEST_VECTORS.BLAKE2B_1_PUBLIC_0) - assert.equals(accounts[0].address, TREZOR_TEST_VECTORS.BLAKE2B_1_ADDRESS_0) - assert.ok(accounts[1] instanceof Account) - assert.equals(accounts[1].index, 1) - assert.equals(accounts[1].privateKey, TREZOR_TEST_VECTORS.BLAKE2B_1_PRIVATE_1) - assert.equals(accounts[1].publicKey, TREZOR_TEST_VECTORS.BLAKE2B_1_PUBLIC_1) - assert.equals(accounts[1].address, TREZOR_TEST_VECTORS.BLAKE2B_1_ADDRESS_1) -}) - -await test('get identical BLAKE2b wallets when created with a seed versus with its derived mnemonic', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_2) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const walletAccounts = await wallet.accounts() - const walletAccount = walletAccounts[0] - - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.exists(walletAccount) - assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_2) - - const imported = await Blake2bWallet.fromMnemonic(TREZOR_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_2) - await imported.unlock(TREZOR_TEST_VECTORS.PASSWORD) - const importedAccounts = await imported.accounts() - const importedAccount = importedAccounts[0] - - assert.equals(imported.mnemonic, wallet.mnemonic) - assert.equals(imported.seed, wallet.seed) - assert.equals(importedAccount.privateKey, walletAccount.privateKey) - assert.equals(importedAccount.publicKey, walletAccount.publicKey) -}) - -await test('get identical BLAKE2b wallets when created with max entropy value', async () => { - const wallet = await Blake2bWallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_3) - 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.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_3) - assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_3) - assert.equals(accounts[0].index, 0) - assert.equals(accounts[0].privateKey, TREZOR_TEST_VECTORS.BLAKE2B_3_PRIVATE_0) - assert.equals(accounts[0].publicKey, TREZOR_TEST_VECTORS.BLAKE2B_3_PUBLIC_0) - assert.equals(accounts[0].address, TREZOR_TEST_VECTORS.BLAKE2B_3_ADDRESS_0) -}) - -await test('reject when given 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 test('reject when given a seed with an invalid length', async () => { - await assert.rejects(Bip44Wallet.fromSeed(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)), - `Expected a ${NANO_TEST_VECTORS.BIP39_SEED.length}-character seed, but received ${NANO_TEST_VECTORS.BIP39_SEED.length - 1}-character string.`) -}) - -await test('reject when given a seed containing non-hex characters', async () => { - await assert.rejects(Bip44Wallet.fromSeed(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')), - 'Seed contains invalid hexadecimal characters.') -}) - - -await test('retrieve a Bip44Wallet from storage using an 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) - - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, '') - assert.equals(wallet.seed, '') - - const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - - assert.equals(unlockResult, true) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) - assert.equals(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) -}) - -await test('retrieve a Blake2bWallet from storage using an ID', async () => { - const id = (await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0)).id - const wallet = await Blake2bWallet.restore(id) - - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, '') - assert.equals(wallet.seed, '') - - const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - - assert.equals(unlockResult, true) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_0) - assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_0) +await suite('Import wallets', async () => { + await test('import a wallet with the official Nano test vectors mnemonic', async () => { + const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, 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.equals(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) + assert.equals(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + assert.equals(accounts[0].privateKey, NANO_TEST_VECTORS.PRIVATE_0) + assert.equals(accounts[0].publicKey, NANO_TEST_VECTORS.PUBLIC_0) + assert.equals(accounts[0].address, NANO_TEST_VECTORS.ADDRESS_0) + }) + + await test('import a wallet with the checksum starting with a zero', async () => { + const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, 'food define cancel major spoon trash cigar basic aim bless wolf win ability seek paddle bench seed century group they mercy address monkey cake') + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + assert.equals(wallet.seed, 'F665F804E5907985455D1E5A7AD344843A2ED4179A7E06EEF263DE925FF6F4C0991B0A9344FCEE939FE0F1B1841B8C9B20FEACF6B954B74B2D26A01906B758E2') + }) + + await test('import a wallet with a 12-word phrase', async () => { + const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_0) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.accounts() + const account = accounts[0] + + assert.equals(wallet.mnemonic, CUSTOM_TEST_VECTORS.MNEMONIC_0) + assert.equals(wallet.seed, CUSTOM_TEST_VECTORS.SEED_0) + assert.equals(account.privateKey, CUSTOM_TEST_VECTORS.PRIVATE_0) + assert.equals(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_0) + assert.equals(account.address, CUSTOM_TEST_VECTORS.ADDRESS_0) + }) + + await test('import a wallet with a 15-word phrase', async () => { + const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_1) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.accounts() + const account = accounts[0] + + assert.equals(wallet.mnemonic, CUSTOM_TEST_VECTORS.MNEMONIC_1) + assert.equals(wallet.seed, CUSTOM_TEST_VECTORS.SEED_1) + assert.equals(account.privateKey, CUSTOM_TEST_VECTORS.PRIVATE_1) + assert.equals(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_1) + assert.equals(account.address, CUSTOM_TEST_VECTORS.ADDRESS_1) + }) + + await test('import a wallet with a 18-word phrase', async () => { + const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_2) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.accounts() + const account = accounts[0] + + assert.equals(wallet.mnemonic, CUSTOM_TEST_VECTORS.MNEMONIC_2) + assert.equals(wallet.seed, CUSTOM_TEST_VECTORS.SEED_2) + assert.equals(account.privateKey, CUSTOM_TEST_VECTORS.PRIVATE_2) + assert.equals(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_2) + assert.equals(account.address, CUSTOM_TEST_VECTORS.ADDRESS_2) + }) + + await test('import a wallet with a 21-word phrase', async () => { + const wallet = await Bip44Wallet.fromEntropy(NANO_TEST_VECTORS.PASSWORD, CUSTOM_TEST_VECTORS.ENTROPY_3) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.accounts() + const account = accounts[0] + + assert.equals(wallet.mnemonic, CUSTOM_TEST_VECTORS.MNEMONIC_3) + assert.equals(wallet.seed, CUSTOM_TEST_VECTORS.SEED_3) + assert.equals(account.privateKey, CUSTOM_TEST_VECTORS.PRIVATE_3) + assert.equals(account.publicKey, CUSTOM_TEST_VECTORS.PUBLIC_3) + assert.equals(account.address, CUSTOM_TEST_VECTORS.ADDRESS_3) + }) + + await test('import a wallet with the official Nano test vectors seed', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + 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.equals(wallet.mnemonic, '') + assert.equals(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + assert.equals(accounts[0].privateKey, NANO_TEST_VECTORS.PRIVATE_0) + assert.equals(accounts[0].publicKey, NANO_TEST_VECTORS.PUBLIC_0) + assert.equals(accounts[0].address, NANO_TEST_VECTORS.ADDRESS_0) + }) + + await test('import a BIP-44 wallet with the zero seed', async () => { + const wallet = await Bip44Wallet.fromEntropy(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) + + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_0) + assert.equals(wallet.seed, TREZOR_TEST_VECTORS.SEED_0.toUpperCase()) + assert.equals(accounts.length, 4) + for (let i = 0; i < accounts.length; i++) { + assert.exists(accounts[i]) + assert.exists(accounts[i].address) + assert.exists(accounts[i].publicKey) + assert.exists(accounts[i].privateKey) + assert.equals(accounts[i].index, i) + } + }) + + await test('import a BLAKE2b wallet with the zero seed', async () => { + const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.accounts(0, 3) + + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_0) + assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_0) + assert.equals(accounts.length, 4) + + for (let i = 0; i < accounts.length; i++) { + assert.exists(accounts[i]) + assert.exists(accounts[i].address) + assert.exists(accounts[i].publicKey) + assert.exists(accounts[i].privateKey) + assert.equals(accounts[i].index, i) + } + }) + + await test('import a BLAKE2b wallet with Trezor test vectors', async () => { + const wallet = await Blake2bWallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_1) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.accounts(0, 1) + + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) + assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) + assert.ok(accounts[0] instanceof Account) + assert.equals(accounts[0].index, 0) + assert.equals(accounts[0].privateKey, TREZOR_TEST_VECTORS.BLAKE2B_1_PRIVATE_0) + assert.equals(accounts[0].publicKey, TREZOR_TEST_VECTORS.BLAKE2B_1_PUBLIC_0) + assert.equals(accounts[0].address, TREZOR_TEST_VECTORS.BLAKE2B_1_ADDRESS_0) + assert.ok(accounts[1] instanceof Account) + assert.equals(accounts[1].index, 1) + assert.equals(accounts[1].privateKey, TREZOR_TEST_VECTORS.BLAKE2B_1_PRIVATE_1) + assert.equals(accounts[1].publicKey, TREZOR_TEST_VECTORS.BLAKE2B_1_PUBLIC_1) + assert.equals(accounts[1].address, TREZOR_TEST_VECTORS.BLAKE2B_1_ADDRESS_1) + }) + + await test('get identical BLAKE2b wallets when created with a seed versus with its derived mnemonic', async () => { + const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_2) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const walletAccounts = await wallet.accounts() + const walletAccount = walletAccounts[0] + + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.exists(walletAccount) + assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_2) + + const imported = await Blake2bWallet.fromMnemonic(TREZOR_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_2) + await imported.unlock(TREZOR_TEST_VECTORS.PASSWORD) + const importedAccounts = await imported.accounts() + const importedAccount = importedAccounts[0] + + assert.equals(imported.mnemonic, wallet.mnemonic) + assert.equals(imported.seed, wallet.seed) + assert.equals(importedAccount.privateKey, walletAccount.privateKey) + assert.equals(importedAccount.publicKey, walletAccount.publicKey) + }) + + await test('get identical BLAKE2b wallets when created with max entropy value', async () => { + const wallet = await Blake2bWallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_3) + 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.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_3) + assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_3) + assert.equals(accounts[0].index, 0) + assert.equals(accounts[0].privateKey, TREZOR_TEST_VECTORS.BLAKE2B_3_PRIVATE_0) + assert.equals(accounts[0].publicKey, TREZOR_TEST_VECTORS.BLAKE2B_3_PUBLIC_0) + assert.equals(accounts[0].address, TREZOR_TEST_VECTORS.BLAKE2B_3_ADDRESS_0) + }) + + await test('reject when given 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 test('reject when given a seed with an invalid length', async () => { + await assert.rejects(Bip44Wallet.fromSeed(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)), + `Expected a ${NANO_TEST_VECTORS.BIP39_SEED.length}-character seed, but received ${NANO_TEST_VECTORS.BIP39_SEED.length - 1}-character string.`) + }) + + await test('reject when given a seed containing non-hex characters', async () => { + await assert.rejects(Bip44Wallet.fromSeed(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')), + 'Seed contains invalid hexadecimal characters.') + }) + + + await test('retrieve a Bip44Wallet from storage using an 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) + + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, '') + assert.equals(wallet.seed, '') + + const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + assert.equals(unlockResult, true) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) + assert.equals(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + }) + + await test('retrieve a Blake2bWallet from storage using an ID', async () => { + const id = (await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0)).id + const wallet = await Blake2bWallet.restore(id) + + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, '') + assert.equals(wallet.seed, '') + + const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + assert.equals(unlockResult, true) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_0) + assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_0) + }) }) diff --git a/test/lock-unlock-wallet.mjs b/test/lock-unlock-wallet.mjs index 85a55ab..5ec6ec2 100644 --- a/test/lock-unlock-wallet.mjs +++ b/test/lock-unlock-wallet.mjs @@ -3,242 +3,244 @@ 'use strict' -import { assert, test } from '#GLOBALS.mjs' +import { assert, suite, test } from '#GLOBALS.mjs' import { NANO_TEST_VECTORS, TREZOR_TEST_VECTORS } from '#test/TEST_VECTORS.js' import { Bip44Wallet, Blake2bWallet } from '#dist/main.js' -await test('locking and unlocking a Bip44Wallet with a password', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) - - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, '') - assert.equals(wallet.seed, '') - - const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - - assert.equals(unlockResult, true) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) - assert.equals(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) -}) - -await test('locking and unlocking a Bip44Wallet with a random CryptoKey', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) - const lockResult = await wallet.lock(key) - - assert.ok(lockResult) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, '') - assert.equals(wallet.seed, '') - - const unlockResult = await wallet.unlock(key) - - assert.equals(unlockResult, true) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) - assert.equals(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) -}) - -await test('fail to unlock a Bip44Wallet with different passwords', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const lockResult = await wallet.lock(TREZOR_TEST_VECTORS.PASSWORD) - - await assert.rejects(wallet.unlock(NANO_TEST_VECTORS.PASSWORD), { message: 'Failed to unlock wallet' }) - assert.equals(lockResult, true) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.notEqual(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) - assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) -}) - -await test('fail to unlock a Bip44Wallet with different keys', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const rightKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) - const wrongKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) - const lockResult = await wallet.lock(rightKey) - - await assert.rejects(wallet.unlock(wrongKey), { message: 'Failed to unlock wallet' }) - assert.equals(lockResult, true) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.notEqual(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) - assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) -}) - -await test('fail to unlock a Bip44Wallet with different valid inputs', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) - const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) - - await assert.rejects(wallet.unlock(key), { message: 'Failed to unlock wallet' }) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.notEqual(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) - assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) -}) - -await test('fail to unlock a Bip44Wallet with no input', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - - //@ts-expect-error - await assert.rejects(wallet.lock(), { message: 'Failed to lock wallet' }) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) - assert.equals(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) - - await wallet.lock('password') - - //@ts-expect-error - await assert.rejects(wallet.unlock(), { message: 'Failed to unlock wallet' }) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.notEqual(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) - assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) -}) - -await test('fail to unlock a Bip44Wallet with invalid input', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - - //@ts-expect-error - await assert.rejects(wallet.lock(1), { message: 'Failed to lock wallet' }) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) - assert.equals(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) - - await wallet.lock(NANO_TEST_VECTORS.PASSWORD) - - //@ts-expect-error - await assert.rejects(wallet.unlock(1), { message: 'Failed to unlock wallet' }) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.notEqual(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) - assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) -}) - -await test('locking and unlocking a Blake2bWallet with a password', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0) - - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, '') - assert.equals(wallet.seed, '') - - const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - - assert.equals(unlockResult, true) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_0) - assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_0) -}) - -await test('locking and unlocking a Blake2bWallet with a random CryptoKey', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) - const lockResult = await wallet.lock(key) - - assert.equals(lockResult, true) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, '') - assert.equals(wallet.seed, '') - - const unlockResult = await wallet.unlock(key) - - assert.equals(lockResult, true) - assert.equals(unlockResult, true) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) - assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) -}) - -await test('fail to unlock a Blake2bWallet with different passwords', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) - - await assert.rejects(wallet.unlock(TREZOR_TEST_VECTORS.PASSWORD), { message: 'Failed to unlock wallet' }) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.notEqual(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) - assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) -}) - -await test('fail to unlock a Blake2bWallet with different keys', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const rightKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) - const wrongKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) - const lockResult = await wallet.lock(rightKey) - - await assert.rejects(wallet.unlock(wrongKey), { message: 'Failed to unlock wallet' }) - assert.equals(lockResult, true) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.notEqual(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) - assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) -}) - -await test('fail to unlock a Blake2bWallet with different valid inputs', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) - const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) - - await assert.rejects(wallet.unlock(key), { message: 'Failed to unlock wallet' }) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.notEqual(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) - assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) -}) - -await test('fail to unlock a Blake2bWallet with no input', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - - //@ts-expect-error - await assert.rejects(wallet.lock(), { message: 'Failed to lock wallet' }) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) - assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) - - await wallet.lock(NANO_TEST_VECTORS.PASSWORD) - - //@ts-expect-error - await assert.rejects(wallet.unlock(), { message: 'Failed to unlock wallet' }) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.notEqual(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) - assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) -}) - -await test('fail to unlock a Blake2bWallet with invalid input', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - - //@ts-expect-error - await assert.rejects(wallet.lock(1), { message: 'Failed to lock wallet' }) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) - assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) - - await wallet.lock(NANO_TEST_VECTORS.PASSWORD) - - //@ts-expect-error - await assert.rejects(wallet.unlock(1), { message: 'Failed to unlock wallet' }) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.notEqual(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) - assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) +await suite('Lock and unlock wallets', async () => { + await test('locking and unlocking a Bip44Wallet with a password', async () => { + const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, '') + assert.equals(wallet.seed, '') + + const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + assert.equals(unlockResult, true) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) + assert.equals(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + }) + + await test('locking and unlocking a Bip44Wallet with a random CryptoKey', async () => { + const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) + const lockResult = await wallet.lock(key) + + assert.ok(lockResult) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, '') + assert.equals(wallet.seed, '') + + const unlockResult = await wallet.unlock(key) + + assert.equals(unlockResult, true) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) + assert.equals(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + }) + + await test('fail to unlock a Bip44Wallet with different passwords', async () => { + const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const lockResult = await wallet.lock(TREZOR_TEST_VECTORS.PASSWORD) + + await assert.rejects(wallet.unlock(NANO_TEST_VECTORS.PASSWORD), { message: 'Failed to unlock wallet' }) + assert.equals(lockResult, true) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.notEqual(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) + assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + }) + + await test('fail to unlock a Bip44Wallet with different keys', async () => { + const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const rightKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) + const wrongKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) + const lockResult = await wallet.lock(rightKey) + + await assert.rejects(wallet.unlock(wrongKey), { message: 'Failed to unlock wallet' }) + assert.equals(lockResult, true) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.notEqual(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) + assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + }) + + await test('fail to unlock a Bip44Wallet with different valid inputs', async () => { + const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) + + await assert.rejects(wallet.unlock(key), { message: 'Failed to unlock wallet' }) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.notEqual(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) + assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + }) + + await test('fail to unlock a Bip44Wallet with no input', async () => { + const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + //@ts-expect-error + await assert.rejects(wallet.lock(), { message: 'Failed to lock wallet' }) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) + assert.equals(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + + await wallet.lock('password') + + //@ts-expect-error + await assert.rejects(wallet.unlock(), { message: 'Failed to unlock wallet' }) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.notEqual(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) + assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + }) + + await test('fail to unlock a Bip44Wallet with invalid input', async () => { + const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + //@ts-expect-error + await assert.rejects(wallet.lock(1), { message: 'Failed to lock wallet' }) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) + assert.equals(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + + await wallet.lock(NANO_TEST_VECTORS.PASSWORD) + + //@ts-expect-error + await assert.rejects(wallet.unlock(1), { message: 'Failed to unlock wallet' }) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.notEqual(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) + assert.notEqual(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + }) + + await test('locking and unlocking a Blake2bWallet with a password', async () => { + const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0) + + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, '') + assert.equals(wallet.seed, '') + + const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + assert.equals(unlockResult, true) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_0) + assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_0) + }) + + await test('locking and unlocking a Blake2bWallet with a random CryptoKey', async () => { + const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) + const lockResult = await wallet.lock(key) + + assert.equals(lockResult, true) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, '') + assert.equals(wallet.seed, '') + + const unlockResult = await wallet.unlock(key) + + assert.equals(lockResult, true) + assert.equals(unlockResult, true) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) + assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) + }) + + await test('fail to unlock a Blake2bWallet with different passwords', async () => { + const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) + + await assert.rejects(wallet.unlock(TREZOR_TEST_VECTORS.PASSWORD), { message: 'Failed to unlock wallet' }) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.notEqual(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) + assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) + }) + + await test('fail to unlock a Blake2bWallet with different keys', async () => { + const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const rightKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) + const wrongKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) + const lockResult = await wallet.lock(rightKey) + + await assert.rejects(wallet.unlock(wrongKey), { message: 'Failed to unlock wallet' }) + assert.equals(lockResult, true) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.notEqual(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) + assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) + }) + + await test('fail to unlock a Blake2bWallet with different valid inputs', async () => { + const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) + const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) + + await assert.rejects(wallet.unlock(key), { message: 'Failed to unlock wallet' }) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.notEqual(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) + assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) + }) + + await test('fail to unlock a Blake2bWallet with no input', async () => { + const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + //@ts-expect-error + await assert.rejects(wallet.lock(), { message: 'Failed to lock wallet' }) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) + assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) + + await wallet.lock(NANO_TEST_VECTORS.PASSWORD) + + //@ts-expect-error + await assert.rejects(wallet.unlock(), { message: 'Failed to unlock wallet' }) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.notEqual(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) + assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) + }) + + await test('fail to unlock a Blake2bWallet with invalid input', async () => { + const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + //@ts-expect-error + await assert.rejects(wallet.lock(1), { message: 'Failed to lock wallet' }) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equals(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) + assert.equals(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) + + await wallet.lock(NANO_TEST_VECTORS.PASSWORD) + + //@ts-expect-error + await assert.rejects(wallet.unlock(1), { message: 'Failed to unlock wallet' }) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.notEqual(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) + assert.notEqual(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) + }) }) diff --git a/test/manage-rolodex.mjs b/test/manage-rolodex.mjs index 206aa51..29c5005 100644 --- a/test/manage-rolodex.mjs +++ b/test/manage-rolodex.mjs @@ -3,140 +3,140 @@ 'use strict' -import { assert, test } from '#GLOBALS.mjs' +import { assert, suite, test } from '#GLOBALS.mjs' import { NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js' import { Rolodex, Tools } from '#dist/main.js' -console.log('> rolodex valid contact management <') - -await test('should create a rolodex and add two contacts', async () => { - const rolodex = new Rolodex() - assert.equals(rolodex.constructor, Rolodex) - await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) - await rolodex.add('JaneSmith', NANO_TEST_VECTORS.ADDRESS_1) - - assert.equals(rolodex.getAllNames().length, 2) - assert.equals(rolodex.getAllNames()[0], 'JohnDoe') - assert.equals(rolodex.getAllNames()[1], 'JaneSmith') - assert.equals(rolodex.getAddresses('JohnDoe').length, 1) - assert.equals(rolodex.getAddresses('JohnDoe')[0], NANO_TEST_VECTORS.ADDRESS_0) - assert.equals(rolodex.getAddresses('JaneSmith').length, 1) - assert.equals(rolodex.getAddresses('JaneSmith')[0], NANO_TEST_VECTORS.ADDRESS_1) +await suite('Rolodex valid contact management', async () => { + await test('should create a rolodex and add two contacts', async () => { + const rolodex = new Rolodex() + assert.equals(rolodex.constructor, Rolodex) + await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) + await rolodex.add('JaneSmith', NANO_TEST_VECTORS.ADDRESS_1) + + assert.equals(rolodex.getAllNames().length, 2) + assert.equals(rolodex.getAllNames()[0], 'JohnDoe') + assert.equals(rolodex.getAllNames()[1], 'JaneSmith') + assert.equals(rolodex.getAddresses('JohnDoe').length, 1) + assert.equals(rolodex.getAddresses('JohnDoe')[0], NANO_TEST_VECTORS.ADDRESS_0) + assert.equals(rolodex.getAddresses('JaneSmith').length, 1) + assert.equals(rolodex.getAddresses('JaneSmith')[0], NANO_TEST_VECTORS.ADDRESS_1) + }) + + await test('should get a name from an address', async () => { + const rolodex = new Rolodex() + await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) + assert.equals(rolodex.getName(NANO_TEST_VECTORS.ADDRESS_0), 'JohnDoe') + }) + + await test('should add three addresses to the same contact', async () => { + const rolodex = new Rolodex() + await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_1) + await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_2) + await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) + assert.equals(rolodex.getAddresses('JohnDoe').length, 3) + assert.equals(rolodex.getAddresses('JohnDoe')[0], NANO_TEST_VECTORS.ADDRESS_1) + assert.equals(rolodex.getAddresses('JohnDoe')[1], NANO_TEST_VECTORS.ADDRESS_2) + assert.equals(rolodex.getAddresses('JohnDoe')[2], NANO_TEST_VECTORS.ADDRESS_0) + }) + + await test('should update the name on an existing entry', async () => { + const rolodex = new Rolodex() + await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) + await rolodex.add('JaneSmith', NANO_TEST_VECTORS.ADDRESS_0) + assert.equals(rolodex.getAddresses('JohnDoe').length, 0) + assert.equals(rolodex.getAddresses('JaneSmith').length, 1) + assert.equals(rolodex.getAddresses('JaneSmith')[0], NANO_TEST_VECTORS.ADDRESS_0) + }) + + await test('should return empty address array for an unknown contact', async () => { + const rolodex = new Rolodex() + await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) + assert.equals(Array.isArray(rolodex.getAddresses('JaneSmith')), true) + assert.equals(rolodex.getAddresses('JaneSmith').length, 0) + }) + + await test('should return empty address array for blank contact names', () => { + const rolodex = new Rolodex() + //@ts-expect-error + assert.equals(Array.isArray(rolodex.getAddresses(undefined)), true) + //@ts-expect-error + assert.equals(rolodex.getAddresses(undefined).length, 0) + //@ts-expect-error + assert.equals(Array.isArray(rolodex.getAddresses(null)), true) + //@ts-expect-error + assert.equals(rolodex.getAddresses(null).length, 0) + assert.equals(Array.isArray(rolodex.getAddresses('')), true) + assert.equals(rolodex.getAddresses('').length, 0) + }) + + await test('should return null for an unknown address', async () => { + const rolodex = new Rolodex() + await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) + assert.ok(rolodex.getName(NANO_TEST_VECTORS.ADDRESS_1) === null) + assert.ok(rolodex.getName(NANO_TEST_VECTORS.ADDRESS_1) !== undefined) + }) + + await test('should return null for a blank address', async () => { + const rolodex = new Rolodex() + await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) + //@ts-expect-error + assert.ok(rolodex.getName(undefined) === null) + //@ts-expect-error + assert.ok(rolodex.getName(undefined) !== undefined) + //@ts-expect-error + assert.ok(rolodex.getName(null) === null) + //@ts-expect-error + assert.ok(rolodex.getName(null) !== undefined) + assert.ok(rolodex.getName('') === null) + assert.ok(rolodex.getName('') !== undefined) + }) }) -await test('should get a name from an address', async () => { - const rolodex = new Rolodex() - await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) - assert.equals(rolodex.getName(NANO_TEST_VECTORS.ADDRESS_0), 'JohnDoe') +await suite('Rolodex exceptions', async () => { + await test('should throw if adding no data', async () => { + const rolodex = new Rolodex() + //@ts-expect-error + await assert.rejects(rolodex.add()) + }) + + await test('should throw if passed no address', async () => { + const rolodex = new Rolodex() + //@ts-expect-error + await assert.rejects(rolodex.add('JohnDoe')) + //@ts-expect-error + await assert.rejects(rolodex.add('JohnDoe', undefined)) + //@ts-expect-error + await assert.rejects(rolodex.add('JohnDoe', null)) + await assert.rejects(rolodex.add('JohnDoe', '')) + }) + + await test('should throw if name is blank', async () => { + const rolodex = new Rolodex() + //@ts-expect-error + await assert.rejects(rolodex.add(undefined, NANO_TEST_VECTORS.ADDRESS_0)) + //@ts-expect-error + await assert.rejects(rolodex.add(null, NANO_TEST_VECTORS.ADDRESS_0)) + await assert.rejects(rolodex.add('', NANO_TEST_VECTORS.ADDRESS_0)) + }) }) -await test('should add three addresses to the same contact', async () => { - const rolodex = new Rolodex() - await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_1) - await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_2) - await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) - assert.equals(rolodex.getAddresses('JohnDoe').length, 3) - assert.equals(rolodex.getAddresses('JohnDoe')[0], NANO_TEST_VECTORS.ADDRESS_1) - assert.equals(rolodex.getAddresses('JohnDoe')[1], NANO_TEST_VECTORS.ADDRESS_2) - assert.equals(rolodex.getAddresses('JohnDoe')[2], NANO_TEST_VECTORS.ADDRESS_0) -}) - -await test('should update the name on an existing entry', async () => { - const rolodex = new Rolodex() - await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) - await rolodex.add('JaneSmith', NANO_TEST_VECTORS.ADDRESS_0) - assert.equals(rolodex.getAddresses('JohnDoe').length, 0) - assert.equals(rolodex.getAddresses('JaneSmith').length, 1) - assert.equals(rolodex.getAddresses('JaneSmith')[0], NANO_TEST_VECTORS.ADDRESS_0) -}) - -await test('should return empty address array for an unknown contact', async () => { - const rolodex = new Rolodex() - await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) - assert.equals(Array.isArray(rolodex.getAddresses('JaneSmith')), true) - assert.equals(rolodex.getAddresses('JaneSmith').length, 0) -}) - -await test('should return empty address array for blank contact names', () => { - const rolodex = new Rolodex() - //@ts-expect-error - assert.equals(Array.isArray(rolodex.getAddresses(undefined)), true) - //@ts-expect-error - assert.equals(rolodex.getAddresses(undefined).length, 0) - //@ts-expect-error - assert.equals(Array.isArray(rolodex.getAddresses(null)), true) - //@ts-expect-error - assert.equals(rolodex.getAddresses(null).length, 0) - assert.equals(Array.isArray(rolodex.getAddresses('')), true) - assert.equals(rolodex.getAddresses('').length, 0) -}) - -await test('should return null for an unknown address', async () => { - const rolodex = new Rolodex() - await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) - assert.ok(rolodex.getName(NANO_TEST_VECTORS.ADDRESS_1) === null) - assert.ok(rolodex.getName(NANO_TEST_VECTORS.ADDRESS_1) !== undefined) -}) - -await test('should return null for a blank address', async () => { - const rolodex = new Rolodex() - await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) - //@ts-expect-error - assert.ok(rolodex.getName(undefined) === null) - //@ts-expect-error - assert.ok(rolodex.getName(undefined) !== undefined) - //@ts-expect-error - assert.ok(rolodex.getName(null) === null) - //@ts-expect-error - assert.ok(rolodex.getName(null) !== undefined) - assert.ok(rolodex.getName('') === null) - assert.ok(rolodex.getName('') !== undefined) -}) - -console.log('> rolodex exceptions <') - -await test('should throw if adding no data', async () => { - const rolodex = new Rolodex() - //@ts-expect-error - await assert.rejects(rolodex.add()) -}) - -await test('should throw if passed no address', async () => { - const rolodex = new Rolodex() - //@ts-expect-error - await assert.rejects(rolodex.add('JohnDoe')) - //@ts-expect-error - await assert.rejects(rolodex.add('JohnDoe', undefined)) - //@ts-expect-error - await assert.rejects(rolodex.add('JohnDoe', null)) - await assert.rejects(rolodex.add('JohnDoe', '')) -}) - -await test('should throw if name is blank', async () => { - const rolodex = new Rolodex() - //@ts-expect-error - await assert.rejects(rolodex.add(undefined, NANO_TEST_VECTORS.ADDRESS_0)) - //@ts-expect-error - await assert.rejects(rolodex.add(null, NANO_TEST_VECTORS.ADDRESS_0)) - await assert.rejects(rolodex.add('', NANO_TEST_VECTORS.ADDRESS_0)) -}) - -console.log('> rolodex data signature verification <') - -await test('should verify valid data and signature', async () => { - const data = 'Test data' - const signature = await Tools.sign(NANO_TEST_VECTORS.PRIVATE_0, data) - const rolodex = new Rolodex() - await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) - const result = await rolodex.verify('JohnDoe', signature, data) - assert.equals(result, true) -}) - -await test('should reject incorrect contact for signature', async () => { - const data = 'Test data' - const signature = await Tools.sign(NANO_TEST_VECTORS.PRIVATE_0, data) - const rolodex = new Rolodex() - await rolodex.add('JaneSmith', NANO_TEST_VECTORS.ADDRESS_1) - const result = await rolodex.verify('JaneSmith', signature, data) - assert.equals(result, false) +await suite('Rolodex data signature verification', async () => { + await test('should verify valid data and signature', async () => { + const data = 'Test data' + const signature = await Tools.sign(NANO_TEST_VECTORS.PRIVATE_0, data) + const rolodex = new Rolodex() + await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) + const result = await rolodex.verify('JohnDoe', signature, data) + assert.equals(result, true) + }) + + await test('should reject incorrect contact for signature', async () => { + const data = 'Test data' + const signature = await Tools.sign(NANO_TEST_VECTORS.PRIVATE_0, data) + const rolodex = new Rolodex() + await rolodex.add('JaneSmith', NANO_TEST_VECTORS.ADDRESS_1) + const result = await rolodex.verify('JaneSmith', signature, data) + assert.equals(result, false) + }) }) diff --git a/test/refresh-accounts.test.mjs b/test/refresh-accounts.test.mjs index b2870b9..ad40a39 100644 --- a/test/refresh-accounts.test.mjs +++ b/test/refresh-accounts.test.mjs @@ -3,7 +3,7 @@ 'use strict' -import { assert, skip, test } from '#GLOBALS.mjs' +import { assert, skip, suite, test } from '#GLOBALS.mjs' import { NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js' import { Account, Bip44Wallet, Rpc } from '#dist/main.js' @@ -15,144 +15,144 @@ if (process) { rpc = new Rpc(process?.env?.NODE_URL ?? '', process?.env?.API_KEY_NAME) } -console.log('refreshing account info') - -await test('fetch balance, frontier, and representative', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts() - const account = accounts[0] - await account.refresh(rpc) - - assert.equals(typeof account.balance, 'bigint') - assert.notEqual(account.balance, undefined) - assert.notEqual(account.balance, null) - assert.notEqual(account.balance, '') - assert.notEqual(account.balance && account.balance < 0, true) - - assert.equals(typeof account.frontier, 'string') - assert.notEqual(account.frontier, undefined) - assert.notEqual(account.frontier, null) - assert.notEqual(account.frontier, '') - assert.match(account.frontier ?? '', /^[0-9A-F]{64}$/i) - - assert.equals(account.representative && account.representative.constructor, Account) - assert.notEqual(account.representative, undefined) - assert.notEqual(account.representative, null) - assert.notEqual(account.representative, '') - assert.notEqual(account.representative?.address, undefined) - assert.notEqual(account.representative?.address, null) - assert.notEqual(account.representative?.address, '') +await suite('refreshing account info', async () => { + await test('fetch balance, frontier, and representative', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.accounts() + const account = accounts[0] + await account.refresh(rpc) + + assert.equals(typeof account.balance, 'bigint') + assert.notEqual(account.balance, undefined) + assert.notEqual(account.balance, null) + assert.notEqual(account.balance, '') + assert.notEqual(account.balance && account.balance < 0, true) + + assert.equals(typeof account.frontier, 'string') + assert.notEqual(account.frontier, undefined) + assert.notEqual(account.frontier, null) + assert.notEqual(account.frontier, '') + assert.match(account.frontier ?? '', /^[0-9A-F]{64}$/i) + + assert.equals(account.representative && account.representative.constructor, Account) + assert.notEqual(account.representative, undefined) + assert.notEqual(account.representative, null) + assert.notEqual(account.representative, '') + assert.notEqual(account.representative?.address, undefined) + assert.notEqual(account.representative?.address, null) + assert.notEqual(account.representative?.address, '') + }) + + await test('throw when refreshing unopened account', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.accounts(0x7fffffff) + const account = accounts[0] + await assert.rejects(account.refresh(rpc), + { message: 'Account not found' }) + }) + + await test('throw when referencing invalid account index', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + await assert.rejects(wallet.accounts(0x80000000), + { message: 'Invalid child key index 0x80000000' }) + }) + + await test('throw with invalid node', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const invalidNode = new Rpc('http://invalid.com') + const accounts = await wallet.accounts() + const account = accounts[0] + await assert.rejects(account.refresh(invalidNode), + { message: 'Account not found' }) + }) }) -await test('throw when refreshing unopened account', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts(0x7fffffff) - const account = accounts[0] - await assert.rejects(account.refresh(rpc), - { message: 'Account not found' }) +await suite('finding next unopened account', async () => { + await test('return correct account from test vector', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const account = await wallet.getNextNewAccount(rpc) + assert.ok(account) + assert.equals(account.address, NANO_TEST_VECTORS.ADDRESS_1) + assert.equals(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1) + }) + + await test('return successfully for small batch size', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const account = await wallet.getNextNewAccount(rpc, 1) + assert.ok(account) + assert.equals(account.address, NANO_TEST_VECTORS.ADDRESS_1) + assert.equals(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1) + }) + + await test('return successfully for large batch size', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const account = await wallet.getNextNewAccount(rpc, 100) + assert.ok(account) + assert.equals(account.address, NANO_TEST_VECTORS.ADDRESS_1) + assert.equals(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1) + }) + + await test('should throw on invalid node URL', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + //@ts-expect-error + await assert.rejects(wallet.getNextNewAccount()) + //@ts-expect-error + await assert.rejects(wallet.getNextNewAccount(null)) + //@ts-expect-error + await assert.rejects(wallet.getNextNewAccount(1)) + //@ts-expect-error + await assert.rejects(wallet.getNextNewAccount('')) + //@ts-expect-error + await assert.rejects(wallet.getNextNewAccount('foo')) + }) + + await test('should throw on invalid batch size', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + //@ts-expect-error + await assert.rejects(wallet.getNextNewAccount(rpc, null)) + await assert.rejects(wallet.getNextNewAccount(rpc, -1)) + //@ts-expect-error + await assert.rejects(wallet.getNextNewAccount(rpc, '')) + //@ts-expect-error + await assert.rejects(wallet.getNextNewAccount(rpc, 'foo')) + //@ts-expect-error + await assert.rejects(wallet.getNextNewAccount(rpc, { 'foo': 'bar' })) + }) }) -await test('throw when referencing invalid account index', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - await assert.rejects(wallet.accounts(0x80000000), - { message: 'Invalid child key index 0x80000000' }) -}) - -await test('throw with invalid node', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const invalidNode = new Rpc('http://invalid.com') - const accounts = await wallet.accounts() - const account = accounts[0] - await assert.rejects(account.refresh(invalidNode), - { message: 'Account not found' }) -}) - -console.log('finding next unopened account') - -await test('return correct account from test vector', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const account = await wallet.getNextNewAccount(rpc) - assert.ok(account) - assert.equals(account.address, NANO_TEST_VECTORS.ADDRESS_1) - assert.equals(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1) -}) - -await test('return successfully for small batch size', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const account = await wallet.getNextNewAccount(rpc, 1) - assert.ok(account) - assert.equals(account.address, NANO_TEST_VECTORS.ADDRESS_1) - assert.equals(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1) -}) - -await test('return successfully for large batch size', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const account = await wallet.getNextNewAccount(rpc, 100) - assert.ok(account) - assert.equals(account.address, NANO_TEST_VECTORS.ADDRESS_1) - assert.equals(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1) -}) - -await test('should throw on invalid node URL', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - //@ts-expect-error - await assert.rejects(wallet.getNextNewAccount()) - //@ts-expect-error - await assert.rejects(wallet.getNextNewAccount(null)) - //@ts-expect-error - await assert.rejects(wallet.getNextNewAccount(1)) - //@ts-expect-error - await assert.rejects(wallet.getNextNewAccount('')) - //@ts-expect-error - await assert.rejects(wallet.getNextNewAccount('foo')) -}) - -await test('should throw on invalid batch size', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - //@ts-expect-error - await assert.rejects(wallet.getNextNewAccount(rpc, null)) - await assert.rejects(wallet.getNextNewAccount(rpc, -1)) - //@ts-expect-error - await assert.rejects(wallet.getNextNewAccount(rpc, '')) - //@ts-expect-error - await assert.rejects(wallet.getNextNewAccount(rpc, 'foo')) - //@ts-expect-error - await assert.rejects(wallet.getNextNewAccount(rpc, { 'foo': 'bar' })) -}) - -console.log('> refreshing wallet accounts <') - -skip('should get balance, frontier, and representative for one account', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.refresh(rpc) - const account = accounts[0] - assert.ok(account instanceof Account) - assert.equals(typeof account.balance, 'bigint') - assert.notEqual(account.frontier, undefined) - assert.notEqual(account.frontier, null) - assert.equals(typeof account.frontier, 'string') -}) - -skip('should get balance, frontier, and representative for multiple accounts', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.refresh(rpc, 0, 2) - assert.equals(accounts.length, 1) - assert.ok(accounts[0] instanceof Account) -}) - -skip('should handle failure gracefully', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - await assert.doesNotReject(wallet.refresh(rpc, 0, 20)) +await suite('Refreshing wallet accounts', async () => { + skip('should get balance, frontier, and representative for one account', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.refresh(rpc) + const account = accounts[0] + assert.ok(account instanceof Account) + assert.equals(typeof account.balance, 'bigint') + assert.notEqual(account.frontier, undefined) + assert.notEqual(account.frontier, null) + assert.equals(typeof account.frontier, 'string') + }) + + skip('should get balance, frontier, and representative for multiple accounts', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.refresh(rpc, 0, 2) + assert.equals(accounts.length, 1) + assert.ok(accounts[0] instanceof Account) + }) + + skip('should handle failure gracefully', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + await assert.doesNotReject(wallet.refresh(rpc, 0, 20)) + }) }) diff --git a/test/sign-blocks.test.mjs b/test/sign-blocks.test.mjs index 12c8227..f13abb6 100644 --- a/test/sign-blocks.test.mjs +++ b/test/sign-blocks.test.mjs @@ -3,161 +3,161 @@ 'use strict' -import { assert, test } from '#GLOBALS.mjs' +import { assert, suite, test } from '#GLOBALS.mjs' import { NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js' import { SendBlock, ReceiveBlock, ChangeBlock } from '#dist/main.js' -console.log('> valid blocks <') +await suite('Valid blocks', async () => { + await test('should not allow negative balances', async () => { + assert.throws(() => { + const block = new SendBlock( + NANO_TEST_VECTORS.ADDRESS_0, + '7000000000000000000000000000000', + NANO_TEST_VECTORS.ADDRESS_1, + '12000000000000000000000000000000', + NANO_TEST_VECTORS.ADDRESS_2, + '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D' + ) + }, { message: 'Negative balance' }) + }) -await test('should not allow negative balances', async () => { - assert.throws(() => { + await test('should allow zero balances', async () => { const block = new SendBlock( NANO_TEST_VECTORS.ADDRESS_0, - '7000000000000000000000000000000', + '9007199254740991', NANO_TEST_VECTORS.ADDRESS_1, - '12000000000000000000000000000000', + '9007199254740991', NANO_TEST_VECTORS.ADDRESS_2, '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D' ) - }, { message: 'Negative balance' }) -}) - -await test('should allow zero balances', async () => { - const block = new SendBlock( - NANO_TEST_VECTORS.ADDRESS_0, - '9007199254740991', - NANO_TEST_VECTORS.ADDRESS_1, - '9007199254740991', - NANO_TEST_VECTORS.ADDRESS_2, - '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D' - ) - assert.notEqual(block.balance, 0) - assert.equals(block.balance, BigInt(0)) -}) + assert.notEqual(block.balance, 0) + assert.equals(block.balance, BigInt(0)) + }) -await test('should subtract balance from SendBlock correctly', async () => { - const block = new SendBlock( - NANO_TEST_VECTORS.ADDRESS_0, - '3000000000000000000000000000000', - NANO_TEST_VECTORS.ADDRESS_1, - '2000000000000000000000000000000', - NANO_TEST_VECTORS.ADDRESS_2, - '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D' - ) - assert.equals(block.balance, 1000000000000000000000000000000n) -}) + await test('should subtract balance from SendBlock correctly', async () => { + const block = new SendBlock( + NANO_TEST_VECTORS.ADDRESS_0, + '3000000000000000000000000000000', + NANO_TEST_VECTORS.ADDRESS_1, + '2000000000000000000000000000000', + NANO_TEST_VECTORS.ADDRESS_2, + '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D' + ) + assert.equals(block.balance, 1000000000000000000000000000000n) + }) -await test('should add balance from ReceiveBlock correctly', async () => { - const block = new ReceiveBlock( - NANO_TEST_VECTORS.ADDRESS_0, - '2000000000000000000000000000000', - NANO_TEST_VECTORS.ADDRESS_1, - '1000000000000000000000000000000', - NANO_TEST_VECTORS.ADDRESS_2, - '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D' - ) - assert.equals(block.balance, 3000000000000000000000000000000n) + await test('should add balance from ReceiveBlock correctly', async () => { + const block = new ReceiveBlock( + NANO_TEST_VECTORS.ADDRESS_0, + '2000000000000000000000000000000', + NANO_TEST_VECTORS.ADDRESS_1, + '1000000000000000000000000000000', + NANO_TEST_VECTORS.ADDRESS_2, + '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D' + ) + assert.equals(block.balance, 3000000000000000000000000000000n) + }) }) -console.log('> block signing tests using official test vectors <') - -await test('should create a valid signature for an open block', async () => { - const block = new ReceiveBlock( - NANO_TEST_VECTORS.OPEN_BLOCK.account, - '0', - NANO_TEST_VECTORS.OPEN_BLOCK.link, - NANO_TEST_VECTORS.OPEN_BLOCK.balance, - NANO_TEST_VECTORS.OPEN_BLOCK.representative, - NANO_TEST_VECTORS.OPEN_BLOCK.previous, - NANO_TEST_VECTORS.OPEN_BLOCK.work - ) - await block.sign(NANO_TEST_VECTORS.OPEN_BLOCK.key) - assert.equals(await block.hash(), NANO_TEST_VECTORS.OPEN_BLOCK.hash) - assert.equals(block.signature, NANO_TEST_VECTORS.OPEN_BLOCK.signature) -}) +await suite('Block signing tests using official test vectors', async () => { + await test('should create a valid signature for an open block', async () => { + const block = new ReceiveBlock( + NANO_TEST_VECTORS.OPEN_BLOCK.account, + '0', + NANO_TEST_VECTORS.OPEN_BLOCK.link, + NANO_TEST_VECTORS.OPEN_BLOCK.balance, + NANO_TEST_VECTORS.OPEN_BLOCK.representative, + NANO_TEST_VECTORS.OPEN_BLOCK.previous, + NANO_TEST_VECTORS.OPEN_BLOCK.work + ) + await block.sign(NANO_TEST_VECTORS.OPEN_BLOCK.key) + assert.equals(await block.hash(), NANO_TEST_VECTORS.OPEN_BLOCK.hash) + assert.equals(block.signature, NANO_TEST_VECTORS.OPEN_BLOCK.signature) + }) -await test('should create a valid signature for a receive block', async () => { - const block = new ReceiveBlock( - NANO_TEST_VECTORS.RECEIVE_BLOCK.account, - NANO_TEST_VECTORS.RECEIVE_BLOCK.balance, - NANO_TEST_VECTORS.RECEIVE_BLOCK.link, - '0', - NANO_TEST_VECTORS.RECEIVE_BLOCK.representative, - NANO_TEST_VECTORS.RECEIVE_BLOCK.previous, - NANO_TEST_VECTORS.RECEIVE_BLOCK.work - ) - await block.sign(NANO_TEST_VECTORS.RECEIVE_BLOCK.key) - assert.equals(await block.hash(), NANO_TEST_VECTORS.RECEIVE_BLOCK.hash) - assert.equals(block.signature, NANO_TEST_VECTORS.RECEIVE_BLOCK.signature) -}) + await test('should create a valid signature for a receive block', async () => { + const block = new ReceiveBlock( + NANO_TEST_VECTORS.RECEIVE_BLOCK.account, + NANO_TEST_VECTORS.RECEIVE_BLOCK.balance, + NANO_TEST_VECTORS.RECEIVE_BLOCK.link, + '0', + NANO_TEST_VECTORS.RECEIVE_BLOCK.representative, + NANO_TEST_VECTORS.RECEIVE_BLOCK.previous, + NANO_TEST_VECTORS.RECEIVE_BLOCK.work + ) + await block.sign(NANO_TEST_VECTORS.RECEIVE_BLOCK.key) + assert.equals(await block.hash(), NANO_TEST_VECTORS.RECEIVE_BLOCK.hash) + assert.equals(block.signature, NANO_TEST_VECTORS.RECEIVE_BLOCK.signature) + }) -await test('should create a valid signature for a receive block without work', async () => { - const block = new ReceiveBlock( - NANO_TEST_VECTORS.RECEIVE_BLOCK.account, - NANO_TEST_VECTORS.RECEIVE_BLOCK.balance, - NANO_TEST_VECTORS.RECEIVE_BLOCK.link, - '0', - NANO_TEST_VECTORS.RECEIVE_BLOCK.representative, - NANO_TEST_VECTORS.RECEIVE_BLOCK.previous - ) - await block.sign(NANO_TEST_VECTORS.RECEIVE_BLOCK.key) - assert.equals(await block.hash(), NANO_TEST_VECTORS.RECEIVE_BLOCK.hash) - assert.equals(block.signature, NANO_TEST_VECTORS.RECEIVE_BLOCK.signature) - assert.equals(block.work, '') -}) + await test('should create a valid signature for a receive block without work', async () => { + const block = new ReceiveBlock( + NANO_TEST_VECTORS.RECEIVE_BLOCK.account, + NANO_TEST_VECTORS.RECEIVE_BLOCK.balance, + NANO_TEST_VECTORS.RECEIVE_BLOCK.link, + '0', + NANO_TEST_VECTORS.RECEIVE_BLOCK.representative, + NANO_TEST_VECTORS.RECEIVE_BLOCK.previous + ) + await block.sign(NANO_TEST_VECTORS.RECEIVE_BLOCK.key) + assert.equals(await block.hash(), NANO_TEST_VECTORS.RECEIVE_BLOCK.hash) + assert.equals(block.signature, NANO_TEST_VECTORS.RECEIVE_BLOCK.signature) + assert.equals(block.work, '') + }) -await test('should create a valid signature for a send block', async () => { - const block = new SendBlock( - NANO_TEST_VECTORS.SEND_BLOCK.account, - NANO_TEST_VECTORS.SEND_BLOCK.balance, - NANO_TEST_VECTORS.SEND_BLOCK.link, - '0', - NANO_TEST_VECTORS.SEND_BLOCK.representative, - NANO_TEST_VECTORS.SEND_BLOCK.previous, - NANO_TEST_VECTORS.SEND_BLOCK.work - ) - await block.sign(NANO_TEST_VECTORS.SEND_BLOCK.key) - assert.equals(await block.hash(), NANO_TEST_VECTORS.SEND_BLOCK.hash) - assert.equals(block.signature, NANO_TEST_VECTORS.SEND_BLOCK.signature) -}) + await test('should create a valid signature for a send block', async () => { + const block = new SendBlock( + NANO_TEST_VECTORS.SEND_BLOCK.account, + NANO_TEST_VECTORS.SEND_BLOCK.balance, + NANO_TEST_VECTORS.SEND_BLOCK.link, + '0', + NANO_TEST_VECTORS.SEND_BLOCK.representative, + NANO_TEST_VECTORS.SEND_BLOCK.previous, + NANO_TEST_VECTORS.SEND_BLOCK.work + ) + await block.sign(NANO_TEST_VECTORS.SEND_BLOCK.key) + assert.equals(await block.hash(), NANO_TEST_VECTORS.SEND_BLOCK.hash) + assert.equals(block.signature, NANO_TEST_VECTORS.SEND_BLOCK.signature) + }) -await test('should create a valid signature for a send block without work', async () => { - const block = new SendBlock( - NANO_TEST_VECTORS.SEND_BLOCK.account, - NANO_TEST_VECTORS.SEND_BLOCK.balance, - NANO_TEST_VECTORS.SEND_BLOCK.link, - '0', - NANO_TEST_VECTORS.SEND_BLOCK.representative, - NANO_TEST_VECTORS.SEND_BLOCK.previous - ) - await block.sign(NANO_TEST_VECTORS.SEND_BLOCK.key) - assert.equals(await block.hash(), NANO_TEST_VECTORS.SEND_BLOCK.hash) - assert.equals(block.signature, NANO_TEST_VECTORS.SEND_BLOCK.signature) - assert.equals(block.work, '') -}) + await test('should create a valid signature for a send block without work', async () => { + const block = new SendBlock( + NANO_TEST_VECTORS.SEND_BLOCK.account, + NANO_TEST_VECTORS.SEND_BLOCK.balance, + NANO_TEST_VECTORS.SEND_BLOCK.link, + '0', + NANO_TEST_VECTORS.SEND_BLOCK.representative, + NANO_TEST_VECTORS.SEND_BLOCK.previous + ) + await block.sign(NANO_TEST_VECTORS.SEND_BLOCK.key) + assert.equals(await block.hash(), NANO_TEST_VECTORS.SEND_BLOCK.hash) + assert.equals(block.signature, NANO_TEST_VECTORS.SEND_BLOCK.signature) + assert.equals(block.work, '') + }) -await test('should create a valid signature for a change rep block', async () => { - const work = '0000000000000000' - const block = new ChangeBlock( - 'nano_3igf8hd4sjshoibbbkeitmgkp1o6ug4xads43j6e4gqkj5xk5o83j8ja9php', - '3000000000000000000000000000000', - 'nano_1anrzcuwe64rwxzcco8dkhpyxpi8kd7zsjc1oeimpc3ppca4mrjtwnqposrs', - '128106287002E595F479ACD615C818117FCB3860EC112670557A2467386249D4', - work, - ) - await block.sign('781186FB9EF17DB6E3D1056550D9FAE5D5BBADA6A6BC370E4CBB938B1DC71DA3') // Did not find a private key at nano docs for this address - assert.equals(block.signature?.toUpperCase(), 'A3C3C66D6519CBC0A198E56855942DEACC6EF741021A1B11279269ADC587DE1DA53CD478B8A47553231104CF24D742E1BB852B0546B87038C19BAE20F9082B0D') - assert.equals(block.work, work) -}) + await test('should create a valid signature for a change rep block', async () => { + const work = '0000000000000000' + const block = new ChangeBlock( + 'nano_3igf8hd4sjshoibbbkeitmgkp1o6ug4xads43j6e4gqkj5xk5o83j8ja9php', + '3000000000000000000000000000000', + 'nano_1anrzcuwe64rwxzcco8dkhpyxpi8kd7zsjc1oeimpc3ppca4mrjtwnqposrs', + '128106287002E595F479ACD615C818117FCB3860EC112670557A2467386249D4', + work, + ) + await block.sign('781186FB9EF17DB6E3D1056550D9FAE5D5BBADA6A6BC370E4CBB938B1DC71DA3') // Did not find a private key at nano docs for this address + assert.equals(block.signature?.toUpperCase(), 'A3C3C66D6519CBC0A198E56855942DEACC6EF741021A1B11279269ADC587DE1DA53CD478B8A47553231104CF24D742E1BB852B0546B87038C19BAE20F9082B0D') + assert.equals(block.work, work) + }) -await test('should create a valid signature for a change rep block without work', async () => { - const block = new ChangeBlock( - NANO_TEST_VECTORS.ADDRESS_0, - '0', - 'nano_34amtofxstsfyqcgphp8piij9u33widykq9wbz6ysjpxhbgmqu8btu1eexer', - 'F3C1D7B6EE97DA09D4C00538CEA93CBA5F74D78FD3FBE71347D2DFE7E53DF327' - ) - await block.sign(NANO_TEST_VECTORS.PRIVATE_0) - assert.equals(block.signature?.toUpperCase(), '2BD2F905E74B5BEE3E2277CED1D1E3F7535E5286B6E22F7B08A814AA9E5C4E1FEA69B61D60B435ADC2CE756E6EE5F5BE7EC691FE87E024A0B22A3D980CA5B305') - assert.equals(block.work, '') + await test('should create a valid signature for a change rep block without work', async () => { + const block = new ChangeBlock( + NANO_TEST_VECTORS.ADDRESS_0, + '0', + 'nano_34amtofxstsfyqcgphp8piij9u33widykq9wbz6ysjpxhbgmqu8btu1eexer', + 'F3C1D7B6EE97DA09D4C00538CEA93CBA5F74D78FD3FBE71347D2DFE7E53DF327' + ) + await block.sign(NANO_TEST_VECTORS.PRIVATE_0) + assert.equals(block.signature?.toUpperCase(), '2BD2F905E74B5BEE3E2277CED1D1E3F7535E5286B6E22F7B08A814AA9E5C4E1FEA69B61D60B435ADC2CE756E6EE5F5BE7EC691FE87E024A0B22A3D980CA5B305') + assert.equals(block.work, '') + }) }) diff --git a/test/tools.test.mjs b/test/tools.test.mjs index 3187802..eabbb9f 100644 --- a/test/tools.test.mjs +++ b/test/tools.test.mjs @@ -15,11 +15,10 @@ if (process) { rpc = new Rpc(process?.env?.NODE_URL ?? '', process?.env?.API_KEY_NAME) } -suite('unit conversion tests', async () => { - +await suite('unit conversion tests', async () => { await test('should convert nano to raw', async () => { const result = await Tools.convert('1', 'NANO', 'RAW') - assert.equals(result, '100000000000000000000000000000') + assert.equals(result, '1000000000000000000000000000000') }) await test('should convert raw to nano', async () => { @@ -78,77 +77,77 @@ suite('unit conversion tests', async () => { }) }) -console.log('> signature tests <') - -await test('should sign data with a single parameter', async () => { - const result = await Tools.sign(NANO_TEST_VECTORS.PRIVATE_0, 'miro@metsanheimo.fi') - assert.equals(result, 'FECB9B084065ADC969904B55A0099C63746B68DF41FECB713244D387EED83A80B9D4907278C5EBC0998A5FC8BA597FBAAABBFCE0ABD2CA2212ACFE788637040C') -}) +await suite('signature tests', async () => { + await test('should sign data with a single parameter', async () => { + const result = await Tools.sign(NANO_TEST_VECTORS.PRIVATE_0, 'miro@metsanheimo.fi') + assert.equals(result, 'FECB9B084065ADC969904B55A0099C63746B68DF41FECB713244D387EED83A80B9D4907278C5EBC0998A5FC8BA597FBAAABBFCE0ABD2CA2212ACFE788637040C') + }) -await test('should sign data with multiple parameters', async () => { - const result = await Tools.sign(NANO_TEST_VECTORS.PRIVATE_0, 'miro@metsanheimo.fi', 'somePassword') - assert.equals(result, 'BB534F9B469AF451B1941FFEF8EE461FC5D284B5D393140900C6E13A65EF08D0AE2BC77131EE182922F66C250C7237A83878160457D5C39A70E55F7FCE925804') -}) + await test('should sign data with multiple parameters', async () => { + const result = await Tools.sign(NANO_TEST_VECTORS.PRIVATE_0, 'miro@metsanheimo.fi', 'somePassword') + assert.equals(result, 'BB534F9B469AF451B1941FFEF8EE461FC5D284B5D393140900C6E13A65EF08D0AE2BC77131EE182922F66C250C7237A83878160457D5C39A70E55F7FCE925804') + }) -await test('should verify a signature using the public key', async () => { - const result = await Tools.verify(NANO_TEST_VECTORS.PUBLIC_0, 'FECB9B084065ADC969904B55A0099C63746B68DF41FECB713244D387EED83A80B9D4907278C5EBC0998A5FC8BA597FBAAABBFCE0ABD2CA2212ACFE788637040C', 'miro@metsanheimo.fi') - assert.equals(result, true) + await test('should verify a signature using the public key', async () => { + const result = await Tools.verify(NANO_TEST_VECTORS.PUBLIC_0, 'FECB9B084065ADC969904B55A0099C63746B68DF41FECB713244D387EED83A80B9D4907278C5EBC0998A5FC8BA597FBAAABBFCE0ABD2CA2212ACFE788637040C', 'miro@metsanheimo.fi') + assert.equals(result, true) - const result2 = await Tools.verify(NANO_TEST_VECTORS.PUBLIC_0, 'FECB9B084065ADC969904B55A0099C63746B68DF41FECB713244D387EED83A80B9D4907278C5EBC0998A5FC8BA597FBAAABBFCE0ABD2CA2212ACFE788637040C', 'mir@metsanheimo.fi') - assert.equals(result2, false) + const result2 = await Tools.verify(NANO_TEST_VECTORS.PUBLIC_0, 'FECB9B084065ADC969904B55A0099C63746B68DF41FECB713244D387EED83A80B9D4907278C5EBC0998A5FC8BA597FBAAABBFCE0ABD2CA2212ACFE788637040C', 'mir@metsanheimo.fi') + assert.equals(result2, false) - const result3 = await Tools.verify(NANO_TEST_VECTORS.PUBLIC_0, 'AECB9B084065ADC969904B55A0099C63746B68DF41FECB713244D387EED83A80B9D4907278C5EBC0998A5FC8BA597FBAAABBFCE0ABD2CA2212ACFE788637040C', 'miro@metsanheimo.fi') - assert.equals(result3, false) -}) + const result3 = await Tools.verify(NANO_TEST_VECTORS.PUBLIC_0, 'AECB9B084065ADC969904B55A0099C63746B68DF41FECB713244D387EED83A80B9D4907278C5EBC0998A5FC8BA597FBAAABBFCE0ABD2CA2212ACFE788637040C', 'miro@metsanheimo.fi') + assert.equals(result3, false) + }) -await test('should verify a block using the public key', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts() - const account = accounts[0] - const sendBlock = new SendBlock( - account.address, - '5618869000000000000000000000000', - 'nano_1q3hqecaw15cjt7thbtxu3pbzr1eihtzzpzxguoc37bj1wc5ffoh7w74gi6p', - '2000000000000000000000000000000', - 'nano_1stofnrxuz3cai7ze75o174bpm7scwj9jn3nxsn8ntzg784jf1gzn1jjdkou', - '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D', - ) - await sendBlock.sign(account.privateKey ?? '') - const valid = await sendBlock.verify(account.publicKey) - assert.equals(valid, true) -}) + await test('should verify a block using the public key', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.accounts() + const account = accounts[0] + const sendBlock = new SendBlock( + account.address, + '5618869000000000000000000000000', + 'nano_1q3hqecaw15cjt7thbtxu3pbzr1eihtzzpzxguoc37bj1wc5ffoh7w74gi6p', + '2000000000000000000000000000000', + 'nano_1stofnrxuz3cai7ze75o174bpm7scwj9jn3nxsn8ntzg784jf1gzn1jjdkou', + '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D', + ) + await sendBlock.sign(account.privateKey ?? '') + const valid = await sendBlock.verify(account.publicKey) + assert.equals(valid, true) + }) -await test('should reject a block using the wrong public key', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const accounts = await wallet.accounts() - const account = accounts[0] - const sendBlock = new SendBlock( - account.address, - '5618869000000000000000000000000', - 'nano_1q3hqecaw15cjt7thbtxu3pbzr1eihtzzpzxguoc37bj1wc5ffoh7w74gi6p', - '2000000000000000000000000000000', - 'nano_1stofnrxuz3cai7ze75o174bpm7scwj9jn3nxsn8ntzg784jf1gzn1jjdkou', - '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D', - ) - await sendBlock.sign(account.privateKey ?? '') - - sendBlock.account = Account.fromAddress('nano_1q3hqecaw15cjt7thbtxu3pbzr1eihtzzpzxguoc37bj1wc5ffoh7w74gi6p') - const valid = await sendBlock.verify(account.publicKey) - assert.equals(valid, false) -}) + await test('should reject a block using the wrong public key', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + const accounts = await wallet.accounts() + const account = accounts[0] + const sendBlock = new SendBlock( + account.address, + '5618869000000000000000000000000', + 'nano_1q3hqecaw15cjt7thbtxu3pbzr1eihtzzpzxguoc37bj1wc5ffoh7w74gi6p', + '2000000000000000000000000000000', + 'nano_1stofnrxuz3cai7ze75o174bpm7scwj9jn3nxsn8ntzg784jf1gzn1jjdkou', + '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D', + ) + await sendBlock.sign(account.privateKey ?? '') + + sendBlock.account = Account.fromAddress('nano_1q3hqecaw15cjt7thbtxu3pbzr1eihtzzpzxguoc37bj1wc5ffoh7w74gi6p') + const valid = await sendBlock.verify(account.publicKey) + assert.equals(valid, false) + }) -await test('sweeper throws without required parameters', async () => { - //@ts-expect-error - await assert.rejects(Tools.sweep(), - 'Missing required sweep arguments') -}) + await test('sweeper throws without required parameters', async () => { + //@ts-expect-error + await assert.rejects(Tools.sweep(), + 'Missing required sweep arguments') + }) -skip('sweeper fails gracefully for ineligible accounts', async () => { - 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.equals(results.length, 1) + await skip('sweeper fails gracefully for ineligible accounts', async () => { + 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.equals(results.length, 1) + }) })