--- /dev/null
+// SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>
+// 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)
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 }