From: Chris Duncan Date: Mon, 2 Dec 2024 04:49:45 +0000 (-0800) Subject: Implement initial PoW worker. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=21abae8f47e298b058fac4d52c51dc5efa47e9a6;p=libnemo.git Implement initial PoW worker. --- diff --git a/src/lib/pow.ts b/src/lib/pow.ts new file mode 100644 index 0000000..38e9782 --- /dev/null +++ b/src/lib/pow.ts @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: 2024 Chris Duncan +// SPDX-License-Identifier: GPL-3.0-or-later + +import { Blake2b } from './blake2b.js' +import { Entropy } from './entropy.js' + +const p = () => { + /** + * Listens for messages from a calling function. + */ + addEventListener('message', (message) => { + const data = JSON.parse(new TextDecoder().decode(message.data ?? message)) + for (const d of data) { + d.nonce = find(d.hash, d.threshold) + } + const buf = new TextEncoder().encode(JSON.stringify(data)).buffer + //@ts-expect-error + postMessage(buf, [buf]) + }) + + function find (hash: string, threshold: string) { + let nonce = null + do { + const e: Entropy = new Entropy(16) + const bytes: Uint8Array = parseHex(`${e.hex}${hash}`) + const result: string = new Blake2b(16).update(bytes).digest('hex') as string + const delta = BigInt(`0x${result}`) - BigInt(`0x${threshold}`) + if (delta >= 0) { + nonce = result + } + } while (nonce == null) + return nonce + } + + 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 ?? []) + } + + return { find } +} + +export const Pow = p() + +const start = p.toString().indexOf('{') + 1 +const end = p.toString().lastIndexOf('return') +export const worker = p.toString().substring(start, end) diff --git a/src/lib/workers.ts b/src/lib/workers.ts index e75c766..bfe19fa 100644 --- a/src/lib/workers.ts +++ b/src/lib/workers.ts @@ -1,8 +1,6 @@ import { worker as Bip44Ckd } from './bip44-ckd.js' import { worker as NanoNaCl } from './nano-nacl.js' // import './workers/passkey.js' -import pow from './workers/pow.js' +import { worker as Pow } from './pow.js' - - -export { Bip44Ckd, NanoNaCl } +export { Bip44Ckd, NanoNaCl, Pow } diff --git a/src/lib/workers/pow.ts b/src/lib/workers/pow.ts deleted file mode 100644 index f75adb4..0000000 --- a/src/lib/workers/pow.ts +++ /dev/null @@ -1,8 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Chris Duncan -// SPDX-License-Identifier: GPL-3.0-or-later - -const pow = () => { - -} - -export default pow.toString().substring(pow.toString().indexOf('{') + 1, pow.toString().lastIndexOf('}'))