From 87036fbe8d4091cc620f962c6515245635b6b4dd Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 30 Nov 2024 14:58:51 -0800 Subject: [PATCH] Fix chunking assignment in Pool. Get keys as hex string instead of Uint8Array in nacl worker since the stringifier mucks up the array. --- src/lib/pool.ts | 13 +++++++------ src/lib/wallet.ts | 4 +++- src/lib/workers/nano-nacl.ts | 23 ++++++++++++++++------- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/lib/pool.ts b/src/lib/pool.ts index afab902..e664720 100644 --- a/src/lib/pool.ts +++ b/src/lib/pool.ts @@ -34,6 +34,7 @@ export class Pool { } constructor (fn: string) { + console.log(`cores: ${this.#cores}`) const url = URL.createObjectURL(new Blob([fn], { type: 'text/javascript' })) for (let i = this.#cores; i > 0; i--) { const thread = { @@ -50,10 +51,7 @@ export class Pool { } } - #assign (thread: Thread) { - const chunk = 1 + (this.#queue.length / this.#cores) - const next = this.#queue.slice(0, chunk) - this.#queue = this.#queue.slice(chunk) + #assign (thread: Thread, next: any[]) { if (next.length > 0) { thread.isBusy = true const buf = new TextEncoder().encode(JSON.stringify(next)).buffer @@ -66,7 +64,7 @@ export class Pool { this.#results.push(...result) thread.isBusy = false if (this.#queue.length > 0) { - this.#assign(thread) + this.#assign(thread, [this.#queue.shift()]) } else if (this.isDone) { this.#resolve(this.#results) } @@ -77,8 +75,11 @@ export class Pool { return new Promise(resolve => { this.#queue = data this.#resolve = resolve + const chunk = 1 + (this.#queue.length / this.#cores) for (const thread of this.#threads) { - this.#assign(thread) + const next = this.#queue.slice(0, chunk) + this.#queue = this.#queue.slice(chunk) + this.#assign(thread, next) } }) } diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index e417737..36997e0 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -97,7 +97,9 @@ abstract class Wallet { if (indexes.length > 0) { let results = await this.ckd(indexes) const data: any = [] - results.forEach(r => data.push({ privateKey: hex.toBytes(r.privateKey as string), index: r.index })) + results.forEach(r => data.push({ privateKey: r.privateKey, index: r.index })) + console.log(`accounts data`) + console.dir(data) let now = performance.now() const keypairs: KeyPair[] = await this.#pool.work(data) console.log(`keypairs: ${-now + (now = performance.now())} ms`) diff --git a/src/lib/workers/nano-nacl.ts b/src/lib/workers/nano-nacl.ts index d2c5d98..8bfa104 100644 --- a/src/lib/workers/nano-nacl.ts +++ b/src/lib/workers/nano-nacl.ts @@ -20,9 +20,13 @@ const NanoNaCl = () => { * Listens for messages from a calling function. */ addEventListener('message', (message) => { - const { privateKey, index } = message.data ?? message - const { publicKey } = keyPair(privateKey) - postMessage({ publicKey, privateKey, index }) + const data = JSON.parse(new TextDecoder().decode(message.data ?? message)) + for (const d of data) { + d.publicKey = keyPair(d.privateKey) + } + const buf = new TextEncoder().encode(JSON.stringify(data)).buffer + //@ts-expect-error + postMessage(buf, [buf]) }) var gf = function (init?: number[]) { @@ -760,13 +764,17 @@ const NanoNaCl = () => { function checkArrayTypes (...args: Uint8Array[]) { for (var i = 0; i < args.length; i++) { - if (!(args[i] instanceof Uint8Array)) + if (!(args[i] instanceof Uint8Array)) { + console.log(args[i]) throw new TypeError(`expected Uint8Array; received ${args[i].constructor?.name ?? typeof args[i]}`) + } } } - function cleanup (arr: Uint8Array | any[]) { - for (var i = 0; i < arr.length; i++) arr[i] = 0 + function parseHex (hex: string) { + if (hex.length % 2 === 1) hex = `0${hex}` + const arr = hex.match(/.{1,2}/g)?.map(byte => parseInt(byte, 16)) + return Uint8Array.from(arr) } const sign = function (msg: Uint8Array, secretKey: Uint8Array) { @@ -811,7 +819,8 @@ const NanoNaCl = () => { return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0) } - const keyPair = function (seed: Uint8Array) { + const keyPair = function (seed: string | Uint8Array) { + if (typeof seed === 'string') seed = parseHex(seed) checkArrayTypes(seed) if (seed.length !== crypto_sign_SEEDBYTES) throw new Error('bad seed size') -- 2.34.1