// 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
*/\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
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
+++ /dev/null
-// 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()})()`