}
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 = {
}
}
- #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
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)
}
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)
}
})
}
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
* 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
\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
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