From 794ac3a2fb4139dbd12fe517db3d17cedff57bbc Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 27 Nov 2024 13:29:23 -0800 Subject: [PATCH] Copyiing the entire blake2b function string into the web worker proved too expensive, and blake2b is so optimized that web workers are 3x slower even it's embedded directly in the worker, so just revert to implementing the ckd in the wallet itself. --- src/lib/wallet.ts | 18 +++++++++++------- src/lib/workers/ckdBlake2b.ts | 30 ------------------------------ 2 files changed, 11 insertions(+), 37 deletions(-) delete mode 100644 src/lib/workers/ckdBlake2b.ts diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 950cf88..e39277d 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -1,15 +1,15 @@ // SPDX-FileCopyrightText: 2024 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later -import blake2b from './blake2b.js' -import { ckdBip44, ckdBlake2b } from './workers.js' import { Account } from './account.js' +import { blake2b } from './blake2b.js' import { Bip39Mnemonic } from './bip39-mnemonic.js' import { ADDRESS_GAP, SEED_LENGTH_BIP44, SEED_LENGTH_BLAKE2B } from './constants.js' import { Entropy } from './entropy.js' import { Pool } from './pool.js' import { Rpc } from './rpc.js' import { Safe } from './safe.js' +import { ckdBip44 } from './workers.js' import type { Ledger } from './ledger.js' /** @@ -453,14 +453,12 @@ export class Bip44Wallet extends Wallet { */ export class Blake2bWallet extends Wallet { static #isInternal: boolean = false - #pool: Pool constructor (seed: string, mnemonic?: Bip39Mnemonic, id?: string) { if (!Blake2bWallet.#isInternal) { throw new Error(`Blake2bWallet cannot be instantiated directly. Use 'await Blake2bWallet.create()' instead.`) } super(seed, mnemonic, id) - this.#pool = new Pool(ckdBlake2b) Blake2bWallet.#isInternal = false } @@ -576,13 +574,19 @@ export class Blake2bWallet extends Wallet { async ckd (index: number | number[]): Promise { if (!Array.isArray(index)) index = [index] const data: any = [] - index.forEach(i => data.push({ seed: this.seed, index: i, blake2b })) let now = performance.now() - const results: [{ index: number, key: string }] = await this.#pool.work(data) + const results = index.map(index => { + const indexHex = index.toString(16).padStart(8, '0').toUpperCase() + const inputHex = `${this.seed}${indexHex}`.padStart(72, '0') + const inputArray = (inputHex.match(/.{1,2}/g) ?? []).map(h => parseInt(h, 16)) + const inputBytes = Uint8Array.from(inputArray) + const key = blake2b(32).update(inputBytes).digest('hex') + return { key, index } + }) console.log(`ckd: ${-now + (now = performance.now())} ms`) const accounts = [] for (const result of results) { - const { index, key } = result + const { key, index } = result if (typeof key !== 'string') { throw new TypeError('BLAKE2b child key derivation returned invalid data') } diff --git a/src/lib/workers/ckdBlake2b.ts b/src/lib/workers/ckdBlake2b.ts deleted file mode 100644 index 3453802..0000000 --- a/src/lib/workers/ckdBlake2b.ts +++ /dev/null @@ -1,30 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Chris Duncan -// SPDX-License-Identifier: GPL-3.0-or-later - -async function fn () { - /** - * Listens for messages from a calling function. - */ - addEventListener('message', (message) => { - const { seed, index, blake2b } = message.data ?? message - ckdBlake2b(seed, index, blake2b).then(key => postMessage({ index, key })) - }) - - /** - * Derives BLAKE2b account private keys. - * - * @param {number} index - Index of the account - * @returns {Promise} - */ - async function ckdBlake2b (seed: string, index: number, b2b: any): Promise { - const blake2b = Function(`return ${b2b}`)() - const indexHex = index.toString(16).padStart(8, '0').toUpperCase() - const inputHex = `${seed}${indexHex}`.padStart(72, '0') - const inputArray = (inputHex.match(/.{1,2}/g) ?? []).map(h => parseInt(h, 16)) - const inputBytes = Uint8Array.from(inputArray) - const hash = blake2b(32).update(inputBytes).digest('hex') - return hash - } -} - -export default `(${fn.toString()})()` -- 2.34.1