]> zoso.dev Git - libnemo.git/commitdiff
Fix chunking assignment in Pool. Get keys as hex string instead of Uint8Array in...
authorChris Duncan <chris@zoso.dev>
Sat, 30 Nov 2024 22:58:51 +0000 (14:58 -0800)
committerChris Duncan <chris@zoso.dev>
Sat, 30 Nov 2024 22:58:51 +0000 (14:58 -0800)
src/lib/pool.ts
src/lib/wallet.ts
src/lib/workers/nano-nacl.ts

index afab9020345f8127f9e1c974b636003e2e13e2ac..e66472009f44e50c1672a4da7d1ee1525ea18775 100644 (file)
@@ -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)
                        }
                })
        }
index e417737143401bb292848fadfcc49cad10160a6b..36997e07c48973a14eca4ca30c9c5b84f4cc3533 100644 (file)
@@ -97,7 +97,9 @@ abstract class Wallet {
                if (indexes.length > 0) {\r
                        let results = await this.ckd(indexes)\r
                        const data: any = []\r
-                       results.forEach(r => data.push({ privateKey: hex.toBytes(r.privateKey as string), index: r.index }))\r
+                       results.forEach(r => data.push({ privateKey: r.privateKey, index: r.index }))\r
+                       console.log(`accounts data`)\r
+                       console.dir(data)\r
                        let now = performance.now()\r
                        const keypairs: KeyPair[] = await this.#pool.work(data)\r
                        console.log(`keypairs: ${-now + (now = performance.now())} ms`)\r
index d2c5d98ac89c37e0152e10549a1e4a7f14d1c168..8bfa104bfb51df4ea20db3b812529d99043f2433 100644 (file)
@@ -20,9 +20,13 @@ const NanoNaCl = () => {
        * Listens for messages from a calling function.\r
        */\r
        addEventListener('message', (message) => {\r
-               const { privateKey, index } = message.data ?? message\r
-               const { publicKey } = keyPair(privateKey)\r
-               postMessage({ publicKey, privateKey, index })\r
+               const data = JSON.parse(new TextDecoder().decode(message.data ?? message))\r
+               for (const d of data) {\r
+                       d.publicKey = keyPair(d.privateKey)\r
+               }\r
+               const buf = new TextEncoder().encode(JSON.stringify(data)).buffer\r
+               //@ts-expect-error\r
+               postMessage(buf, [buf])\r
        })\r
 \r
        var gf = function (init?: number[]) {\r
@@ -760,13 +764,17 @@ const NanoNaCl = () => {
 \r
        function checkArrayTypes (...args: Uint8Array[]) {\r
                for (var i = 0; i < args.length; i++) {\r
-                       if (!(args[i] instanceof Uint8Array))\r
+                       if (!(args[i] instanceof Uint8Array)) {\r
+                               console.log(args[i])\r
                                throw new TypeError(`expected Uint8Array; received ${args[i].constructor?.name ?? typeof args[i]}`)\r
+                       }\r
                }\r
        }\r
 \r
-       function cleanup (arr: Uint8Array | any[]) {\r
-               for (var i = 0; i < arr.length; i++) arr[i] = 0\r
+       function parseHex (hex: string) {\r
+               if (hex.length % 2 === 1) hex = `0${hex}`\r
+               const arr = hex.match(/.{1,2}/g)?.map(byte => parseInt(byte, 16))\r
+               return Uint8Array.from(arr)\r
        }\r
 \r
        const sign = function (msg: Uint8Array, secretKey: Uint8Array) {\r
@@ -811,7 +819,8 @@ const NanoNaCl = () => {
                return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0)\r
        }\r
 \r
-       const keyPair = function (seed: Uint8Array) {\r
+       const keyPair = function (seed: string | Uint8Array) {\r
+               if (typeof seed === 'string') seed = parseHex(seed)\r
                checkArrayTypes(seed)\r
                if (seed.length !== crypto_sign_SEEDBYTES)\r
                        throw new Error('bad seed size')\r