]> zoso.dev Git - libnemo.git/commitdiff
Copyiing the entire blake2b function string into the web worker proved too expensive...
authorChris Duncan <chris@zoso.dev>
Wed, 27 Nov 2024 21:29:23 +0000 (13:29 -0800)
committerChris Duncan <chris@zoso.dev>
Wed, 27 Nov 2024 21:29:23 +0000 (13:29 -0800)
src/lib/wallet.ts
src/lib/workers/ckdBlake2b.ts [deleted file]

index 950cf88b4cb6a00d3b1d74f399c951ad9185804a..e39277d07f1bd9e82cd8f9197ee336aafee02545 100644 (file)
@@ -1,15 +1,15 @@
 // SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>\r
 // SPDX-License-Identifier: GPL-3.0-or-later\r
 \r
-import blake2b from './blake2b.js'\r
-import { ckdBip44, ckdBlake2b } from './workers.js'\r
 import { Account } from './account.js'\r
+import { blake2b } from './blake2b.js'\r
 import { Bip39Mnemonic } from './bip39-mnemonic.js'\r
 import { ADDRESS_GAP, SEED_LENGTH_BIP44, SEED_LENGTH_BLAKE2B } from './constants.js'\r
 import { Entropy } from './entropy.js'\r
 import { Pool } from './pool.js'\r
 import { Rpc } from './rpc.js'\r
 import { Safe } from './safe.js'\r
+import { ckdBip44 } from './workers.js'\r
 import type { Ledger } from './ledger.js'\r
 \r
 /**\r
@@ -453,14 +453,12 @@ export class Bip44Wallet extends Wallet {
 */\r
 export class Blake2bWallet extends Wallet {\r
        static #isInternal: boolean = false\r
-       #pool: Pool\r
 \r
        constructor (seed: string, mnemonic?: Bip39Mnemonic, id?: string) {\r
                if (!Blake2bWallet.#isInternal) {\r
                        throw new Error(`Blake2bWallet cannot be instantiated directly. Use 'await Blake2bWallet.create()' instead.`)\r
                }\r
                super(seed, mnemonic, id)\r
-               this.#pool = new Pool(ckdBlake2b)\r
                Blake2bWallet.#isInternal = false\r
        }\r
 \r
@@ -576,13 +574,19 @@ export class Blake2bWallet extends Wallet {
        async ckd (index: number | number[]): Promise<Account[]> {\r
                if (!Array.isArray(index)) index = [index]\r
                const data: any = []\r
-               index.forEach(i => data.push({ seed: this.seed, index: i, blake2b }))\r
                let now = performance.now()\r
-               const results: [{ index: number, key: string }] = await this.#pool.work(data)\r
+               const results = index.map(index => {\r
+                       const indexHex = index.toString(16).padStart(8, '0').toUpperCase()\r
+                       const inputHex = `${this.seed}${indexHex}`.padStart(72, '0')\r
+                       const inputArray = (inputHex.match(/.{1,2}/g) ?? []).map(h => parseInt(h, 16))\r
+                       const inputBytes = Uint8Array.from(inputArray)\r
+                       const key = blake2b(32).update(inputBytes).digest('hex')\r
+                       return { key, index }\r
+               })\r
                console.log(`ckd: ${-now + (now = performance.now())} ms`)\r
                const accounts = []\r
                for (const result of results) {\r
-                       const { index, key } = result\r
+                       const { key, index } = result\r
                        if (typeof key !== 'string') {\r
                                throw new TypeError('BLAKE2b child key derivation returned invalid data')\r
                        }\r
diff --git a/src/lib/workers/ckdBlake2b.ts b/src/lib/workers/ckdBlake2b.ts
deleted file mode 100644 (file)
index 3453802..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-// SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>
-// 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<string>}
-       */
-       async function ckdBlake2b (seed: string, index: number, b2b: any): Promise<string> {
-               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()})()`