From 3637a368756df0bce615cb13834e17c84cdd5bc4 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 18 Nov 2024 13:48:07 -0800 Subject: [PATCH] Move performance tests to separate file. Move warning about sending funds to test vectors into TEST_VECTORS file only. In Wallet class, expand documentation of accounts method. --- src/lib/wallet.ts | 14 +++++++++- test/TEST_VECTORS.js | 11 ++++++++ test/create-wallet.test.mjs | 21 -------------- test/derive-accounts.test.mjs | 37 ++++--------------------- test/import-wallet.test.mjs | 2 -- test/lock-unlock-wallet.mjs | 2 -- test/performance.test.mjs | 50 ++++++++++++++++++++++++++++++++++ test/refresh-accounts.test.mjs | 3 -- test/sign-blocks.test.mjs | 2 -- 9 files changed, 80 insertions(+), 62 deletions(-) create mode 100644 test/performance.test.mjs diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index b180edb..f2a79b9 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -59,6 +59,17 @@ abstract class Wallet { /** * Retrieves accounts from a wallet using its child key derivation function. + * Defaults to the first account at index 0. + * + * Note: The wallet index of a given requested account might not correlate with + * its index in the returned array. + * + * For example, calling `await wallet.accounts(5)` will return the sixth + * account of the wallet in the first array element position at index 0. + * + * A shorthand way of getting one specific account could be + * `const account = (await wallet.accounts(N))[0]` where N is the wallet index + * of the desired account. * * @param {number} from - Start index of secret keys. Default: 0 * @param {number} to - End index of secret keys. Default: `from` @@ -107,7 +118,8 @@ abstract class Wallet { } /** - * Refreshes wallet account balances, frontiers, and representatives from the current state on the network. + * Refreshes wallet account balances, frontiers, and representatives from the + * current state on the network. * * A successful response will set these properties on each account. * diff --git a/test/TEST_VECTORS.js b/test/TEST_VECTORS.js index 32bfdf9..b3cbeac 100644 --- a/test/TEST_VECTORS.js +++ b/test/TEST_VECTORS.js @@ -1,6 +1,17 @@ // SPDX-FileCopyrightText: 2024 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later +/** +* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING +* +* Do not send any funds to the test vectors below! +* +* Sources: +* https://docs.nano.org/integration-guides/key-management/ +* https://github.com/trezor/python-mnemonic/blob/master/vectors.json +* https://tools.nanos.cc/?tool=seed +*/ + export const GENESIS_ADDRESS = 'nano_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3' export const RAW_MAX = '340282366920938463463374607431768211455' export const SUPPLY_MAX = '133248297920938463463374607431768211455' diff --git a/test/create-wallet.test.mjs b/test/create-wallet.test.mjs index b1ba84b..f07dbf2 100644 --- a/test/create-wallet.test.mjs +++ b/test/create-wallet.test.mjs @@ -9,10 +9,7 @@ import { strict as assert } from 'assert' import { NANO_TEST_VECTORS } from './TEST_VECTORS.js' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from '../dist/main.js' -// WARNING: Do not send any funds to the test vectors below -// Test vectors from https://docs.nano.org/integration-guides/key-management/ and elsewhere describe('creating a new wallet', async () => { - it('BIP-44 wallet with random entropy', async () => { const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) @@ -60,21 +57,3 @@ describe('creating a new wallet', async () => { assert.ok(wallet) }) }) - -describe('wallet generation performance', { skip: true }, async () => { - it('performance test creating BIP-44 wallets', async () => { - const wallets = [] - for (let i = 0x100; i > 0; i--) { - wallets.push(await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD)) - } - assert.equal(wallets.length, 0x100) - }) - - it('performance test creating BLAKE2b wallets', async () => { - const wallets = [] - for (let i = 0x100; i > 0; i--) { - wallets.push(await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD)) - } - assert.equal(wallets.length, 0x100) - }) -}) diff --git a/test/derive-accounts.test.mjs b/test/derive-accounts.test.mjs index d35f546..226a875 100644 --- a/test/derive-accounts.test.mjs +++ b/test/derive-accounts.test.mjs @@ -9,13 +9,11 @@ import { strict as assert } from 'assert' import { NANO_TEST_VECTORS } from './TEST_VECTORS.js' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from '../dist/main.js' -// WARNING: Do not send any funds to the test vectors below -// Test vectors from https://docs.nano.org/integration-guides/key-management/ and elsewhere describe('derive child accounts from the same seed', async () => { const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - it('should derive the first account from the given BIP-44 seed', async function () { + it('should derive the first account from the given BIP-44 seed', async () => { const accounts = await wallet.accounts() assert.equal(accounts.length, 1) @@ -24,7 +22,7 @@ describe('derive child accounts from the same seed', async () => { assert.equal(accounts[0].address, NANO_TEST_VECTORS.ADDRESS_0) }) - it('should derive low indexed accounts from the given BIP-44 seed', async function () { + it('should derive low indexed accounts from the given BIP-44 seed', async () => { const accounts = await wallet.accounts(1, 2) assert.equal(accounts.length, 2) @@ -36,7 +34,7 @@ describe('derive child accounts from the same seed', async () => { assert.equal(accounts[1].address, NANO_TEST_VECTORS.ADDRESS_2) }) - it('should derive high indexed accounts from the given seed', async function () { + it('should derive high indexed accounts from the given seed', async () => { const accounts = await wallet.accounts(0x70000000, 0x700000ff) assert.equal(accounts.length, 0x100) @@ -49,7 +47,7 @@ describe('derive child accounts from the same seed', async () => { } }) - it('should derive accounts for a BLAKE2b wallet', async function () { + it('should derive accounts for a BLAKE2b wallet', async () => { const bwallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD) await bwallet.unlock(NANO_TEST_VECTORS.PASSWORD) const lowAccounts = await bwallet.accounts(0, 2) @@ -74,12 +72,9 @@ describe('derive child accounts from the same seed', async () => { assert.ok(a.index != null) } }) -}) - -describe('Ledger device accounts', { skip: true }, async () => { - const wallet = await LedgerWallet.create() - it('should fetch the first account from a Ledger device', async function () { + it('should fetch the first account from a Ledger device', { skip: true }, async () => { + const wallet = await LedgerWallet.create() const accounts = await wallet.accounts() assert.equal(accounts.length, 1) @@ -87,23 +82,3 @@ describe('Ledger device accounts', { skip: true }, async () => { assert.ok(accounts[0].address) }) }) - -describe('child key derivation performance', { skip: true }, async () => { - it('performance test of BIP-44 ckd', async function () { - const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - - const accounts = await wallet.accounts(0, 0x7fff) - - assert.equal(accounts.length, 0x8000) - }) - - it('performance test of BLAKE2b ckd', async () => { - const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - - const accounts = await wallet.accounts(0, 0x7fff) - - assert.equal(accounts.length, 0x8000) - }) -}) diff --git a/test/import-wallet.test.mjs b/test/import-wallet.test.mjs index 30bc542..1ac1954 100644 --- a/test/import-wallet.test.mjs +++ b/test/import-wallet.test.mjs @@ -9,8 +9,6 @@ import { strict as assert } from 'assert' import { BIP32_TEST_VECTORS, CUSTOM_TEST_VECTORS, NANO_TEST_VECTORS, TREZOR_TEST_VECTORS } from './TEST_VECTORS.js' import { Account, Bip44Wallet, Blake2bWallet } from '../dist/main.js' -// WARNING: Do not send any funds to the test vectors below -// Test vectors from https://docs.nano.org/integration-guides/key-management/ and elsewhere describe('import wallet with test vectors test', () => { it('should successfully 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) diff --git a/test/lock-unlock-wallet.mjs b/test/lock-unlock-wallet.mjs index a67089f..e75b109 100644 --- a/test/lock-unlock-wallet.mjs +++ b/test/lock-unlock-wallet.mjs @@ -11,8 +11,6 @@ import { Bip44Wallet, Blake2bWallet } from '../dist/main.js' const skip = false -// WARNING: Do not send any funds to the test vectors below -// Test vectors from https://docs.nano.org/integration-guides/key-management/ and elsewhere describe('locking and unlocking a Bip44Wallet', { skip }, async () => { it('should succeed with a password', async () => { const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) diff --git a/test/performance.test.mjs b/test/performance.test.mjs new file mode 100644 index 0000000..d7bd28e --- /dev/null +++ b/test/performance.test.mjs @@ -0,0 +1,50 @@ +// SPDX-FileCopyrightText: 2024 Chris Duncan +// SPDX-License-Identifier: GPL-3.0-or-later + +'use strict' + +import './GLOBALS.mjs' +import { describe, it } from 'node:test' +import { strict as assert } from 'assert' +import { NANO_TEST_VECTORS } from './TEST_VECTORS.js' +import { Bip44Wallet, Blake2bWallet } from '../dist/main.js' + +const skip = true + +describe('wallet generation performance', { skip }, async () => { + it('performance test creating BIP-44 wallets', async () => { + const wallets = [] + for (let i = 0x80; i > 0; i--) { + wallets.push(await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD)) + } + assert.equal(wallets.length, 0x80) + }) + + it('performance test creating BLAKE2b wallets', async () => { + const wallets = [] + for (let i = 0x80; i > 0; i--) { + wallets.push(await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD)) + } + assert.equal(wallets.length, 0x80) + }) +}) + +describe('child key derivation performance', { skip }, async () => { + it('performance test of BIP-44 ckd', async () => { + const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + const accounts = await wallet.accounts(0, 0x7fff) + + assert.equal(accounts.length, 0x8000) + }) + + it('performance test of BLAKE2b ckd', async () => { + const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + const accounts = await wallet.accounts(0, 0x7fff) + + assert.equal(accounts.length, 0x8000) + }) +}) diff --git a/test/refresh-accounts.test.mjs b/test/refresh-accounts.test.mjs index 9dc71cc..52636ac 100644 --- a/test/refresh-accounts.test.mjs +++ b/test/refresh-accounts.test.mjs @@ -9,9 +9,6 @@ import { strict as assert } from 'assert' import { NANO_TEST_VECTORS } from './TEST_VECTORS.js' import { Account, Bip44Wallet, Rpc } from '../dist/main.js' -// WARNING: Do not send any funds to the test vectors below -// Test vectors from https://docs.nano.org/integration-guides/key-management/ and elsewhere - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const node = new Rpc(process.env.NODE_URL, process.env.API_KEY_NAME, process.env.API_KEY_VALUE) diff --git a/test/sign-blocks.test.mjs b/test/sign-blocks.test.mjs index 06b1790..e1e676b 100644 --- a/test/sign-blocks.test.mjs +++ b/test/sign-blocks.test.mjs @@ -9,8 +9,6 @@ import { strict as assert } from 'assert' import { NANO_TEST_VECTORS } from './TEST_VECTORS.js' import { SendBlock, ReceiveBlock, ChangeBlock } from '../dist/main.js' -// WARNING: Do not send any funds to the test vectors below -// Test vectors from https://docs.nano.org/integration-guides/key-management/ describe('valid blocks', async () => { it('should not allow negative balances', async () => { assert.throws(() => { -- 2.34.1