From d293c9d9006df3ea729da5b513c1bf38345f4c40 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 20 Nov 2023 15:38:05 -0600 Subject: [PATCH] Add PowWorker to mine Nostr events off the main thread --- package.json | 1 + src/workers.ts | 9 +++++++++ src/workers/pow.worker.ts | 10 ++++++++++ yarn.lock | 5 +++++ 4 files changed, 25 insertions(+) create mode 100644 src/workers.ts create mode 100644 src/workers/pow.worker.ts diff --git a/package.json b/package.json index d90340b7f1..a6cfd4f568 100644 --- a/package.json +++ b/package.json @@ -99,6 +99,7 @@ "bowser": "^2.11.0", "browserslist": "^4.16.6", "clsx": "^2.0.0", + "comlink": "^4.4.1", "core-js": "^3.27.2", "cryptocurrency-icons": "^0.18.1", "cssnano": "^6.0.0", diff --git a/src/workers.ts b/src/workers.ts new file mode 100644 index 0000000000..098aaa15cd --- /dev/null +++ b/src/workers.ts @@ -0,0 +1,9 @@ +import * as Comlink from 'comlink'; + +import type { PowWorker } from './workers/pow.worker'; + +const powWorker = Comlink.wrap( + new Worker(new URL('./workers/pow.worker.ts', import.meta.url), { type: 'module' }), +); + +export { powWorker }; \ No newline at end of file diff --git a/src/workers/pow.worker.ts b/src/workers/pow.worker.ts new file mode 100644 index 0000000000..dcfb948e5a --- /dev/null +++ b/src/workers/pow.worker.ts @@ -0,0 +1,10 @@ +import * as Comlink from 'comlink'; +import { nip13, type UnsignedEvent } from 'nostr-tools'; + +export const PowWorker = { + mine(event: UnsignedEvent, difficulty: number) { + return nip13.minePow(event, difficulty); + }, +}; + +Comlink.expose(PowWorker); \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 1d7fefcf3d..a2d4c7e5dd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3566,6 +3566,11 @@ combined-stream@^1.0.8: dependencies: delayed-stream "~1.0.0" +comlink@^4.4.1: + version "4.4.1" + resolved "https://registry.yarnpkg.com/comlink/-/comlink-4.4.1.tgz#e568b8e86410b809e8600eb2cf40c189371ef981" + integrity sha512-+1dlx0aY5Jo1vHy/tSsIGpSkN4tS9rZSW8FIhG0JH/crs9wwweswIo/POr451r7bZww3hFbPAKnTpimzL/mm4Q== + commander@^2.20.0: version "2.20.3" resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"