From 5806bd34a0ebd0d7ce8775def880a94c652ebfc6 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 20 Nov 2024 22:25:42 -0800 Subject: [PATCH] Remove deprecated ckd functions from wallet subclasses. Fix freeze from ckd being called on empty array of indexes. Throw error if index of derived account is missing instead of just logging to the console. --- src/lib/wallet.ts | 84 +++++------------------------------------------ 1 file changed, 9 insertions(+), 75 deletions(-) diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index b2be001..a1b74cb 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -84,17 +84,18 @@ abstract class Wallet { const indexes = [] for (let i = from; i <= to; i++) { if (this.#accounts[i] == null) { - // 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 + if (indexes.length > 0) { + let results = await this.ckd(indexes) + if (!Array.isArray(results)) results = [results] + for (const result of results) { + if (result.index == null) { + throw new RangeError('Account key derived but index missing') + } else { + this.#accounts[result.index] = result + } } } return this.#accounts.slice(from, to + 1) @@ -409,34 +410,6 @@ export class Bip44Wallet extends Wallet { return wallet } - /** - * Derives BIP-44 Nano account private keys. - * - * @param {number} index - Index of the account - * @returns {Promise} - */ - async ckd_old (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. * @@ -446,11 +419,8 @@ export class Bip44Wallet extends Wallet { async ckd (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 @@ -459,8 +429,6 @@ export class Bip44Wallet extends Wallet { } accounts.push(await Account.fromPrivateKey(key, index)) } - console.log(-now + (now = performance.now()), 'ms') - console.log('bip44 account list done') return accounts } } @@ -597,35 +565,6 @@ export class Blake2bWallet extends Wallet { return wallet } - /** - * Derives BLAKE2b account private keys. - * - * @param {number} index - Index of the account - * @returns {Promise} - */ - async ckd_old (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. * @@ -635,11 +574,8 @@ export class Blake2bWallet extends Wallet { async ckd (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 @@ -648,8 +584,6 @@ export class Blake2bWallet extends Wallet { } accounts.push(await Account.fromPrivateKey(key, index)) } - console.log(-now + (now = performance.now()), 'ms') - console.log('blake2b account list done') return accounts } } -- 2.34.1