From 8a65f96be4a001add3c31fa79e64c0a9678e67e7 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 20 Nov 2024 21:54:25 -0800 Subject: [PATCH] Create new ckd functions for BIP-44 and BLAKE2b. Set up class for A/B performance testing. --- src/lib/wallet.ts | 119 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 106 insertions(+), 13 deletions(-) diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 723083f..2581de7 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -43,7 +43,7 @@ abstract class Wallet { return '' } - abstract ckd (index: number): Promise + abstract ckd (index: number | number[]): Promise | Promise constructor (seed?: string, mnemonic?: Bip39Mnemonic, id?: string) { if (this.constructor === Wallet) { @@ -81,9 +81,20 @@ abstract class Wallet { from = to to = swap } + const indexes = [] for (let i = from; i <= to; i++) { if (this.#accounts[i] == null) { - this.#accounts[i] = await this.ckd(i) + // this.#accounts[i] = await this.ckd(i) + indexes.push(i) + } + } + let results = await this.ckd(indexes) + if (!Array.isArray(results)) results = [results] + for (const result of results) { + if (result.index == null) { + console.error('Wallet account calculated with null index') + } else { + this.#accounts[result.index] = result } } return this.#accounts.slice(from, to + 1) @@ -404,12 +415,53 @@ export class Bip44Wallet extends Wallet { * @param {number} index - Index of the account * @returns {Promise} */ - async ckd (index: number): Promise { - const key = await nanoCKD(this.seed, index) - if (typeof key !== 'string') { - throw new TypeError('BIP-44 child key derivation returned invalid data') + async ckd (index: number | number[]): Promise { + if (!Array.isArray(index)) index = [index] + const keys = [] + let now = performance.now() + for (const i of index) { + const key = await nanoCKD(this.seed, i) + if (typeof key !== 'string') { + throw new TypeError('BIP-44 child key derivation returned invalid data') + } + keys.push({ key, i }) + } + console.log(-now + (now = performance.now()), 'ms') + console.log('bip44 ckd done') + const accounts = [] + for (const key of keys) { + accounts.push(await Account.fromPrivateKey(key.key, key.i)) + } + console.log(-now + (now = performance.now()), 'ms') + console.log('bip44 account list done') + return accounts + } + + /** + * Derives BIP-44 Nano account private keys. + * + * @param {number} index - Index of the account + * @returns {Promise} + */ + async ckd_new (index: number | number[]): Promise { + if (!Array.isArray(index)) index = [index] + const data: any = [] + let now = performance.now() + index.forEach(i => data.push({ seed: this.seed, index: i })) + const results: [{ index: number, key: string }] = await this.#pool.work(data) + console.log(-now + (now = performance.now()), 'ms') + console.log('bip44 ckd done') + const accounts = [] + for (const result of results) { + const { index, key } = result + if (typeof key !== 'string') { + throw new TypeError('BIP-44 child key derivation returned invalid data') + } + accounts.push(await Account.fromPrivateKey(key, index)) } - return Account.fromPrivateKey(key, index) + console.log(-now + (now = performance.now()), 'ms') + console.log('bip44 account list done') + return accounts } } @@ -551,13 +603,54 @@ export class Blake2bWallet extends Wallet { * @param {number} index - Index of the account * @returns {Promise} */ - async ckd (index: number): Promise { - const input = `${this.seed}${dec.toHex(index, 8)}` - const key = blake2b(32).update(hex.toBytes(input)).digest('hex') - if (typeof key !== 'string') { - throw new TypeError('BLAKE2b child key derivation returned invalid data') + async ckd (index: number | number[]): Promise { + if (!Array.isArray(index)) index = [index] + const keys = [] + let now = performance.now() + for (const i of index) { + const input = `${this.seed}${dec.toHex(i, 8)}` + const key = blake2b(32).update(hex.toBytes(input)).digest('hex') + if (typeof key !== 'string') { + throw new TypeError('BLAKE2b child key derivation returned invalid data') + } + keys.push({ key, i }) + } + console.log(-now + (now = performance.now()), 'ms') + console.log('blake2b ckd done') + const accounts = [] + for (const key of keys) { + accounts.push(await Account.fromPrivateKey(key.key, key.i)) + } + console.log(-now + (now = performance.now()), 'ms') + console.log('blake2b account list done') + return accounts + } + + /** + * Derives BLAKE2b account private keys. + * + * @param {number} index - Index of the account + * @returns {Promise} + */ + async ckd_new (index: number | number[]): Promise { + if (!Array.isArray(index)) index = [index] + const data: any = [] + let now = performance.now() + index.forEach(i => data.push({ seed: this.seed, index: i })) + const results: [{ index: number, key: string }] = await this.#pool.work(data) + console.log(-now + (now = performance.now()), 'ms') + console.log('blake2b ckd done') + const accounts = [] + for (const result of results) { + const { index, key } = result + if (typeof key !== 'string') { + throw new TypeError('BLAKE2b child key derivation returned invalid data') + } + accounts.push(await Account.fromPrivateKey(key, index)) } - return Account.fromPrivateKey(key, index) + console.log(-now + (now = performance.now()), 'ms') + console.log('blake2b account list done') + return accounts } } -- 2.34.1