Add PowWorker to mine Nostr events off the main thread

This commit is contained in:
Alex Gleason 2023-11-20 15:38:05 -06:00
parent f7aff70185
commit d293c9d900
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 25 additions and 0 deletions

View File

@ -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",

9
src/workers.ts Normal file
View File

@ -0,0 +1,9 @@
import * as Comlink from 'comlink';
import type { PowWorker } from './workers/pow.worker';
const powWorker = Comlink.wrap<typeof PowWorker>(
new Worker(new URL('./workers/pow.worker.ts', import.meta.url), { type: 'module' }),
);
export { powWorker };

10
src/workers/pow.worker.ts Normal file
View File

@ -0,0 +1,10 @@
import * as Comlink from 'comlink';
import { nip13, type UnsignedEvent } from 'nostr-tools';
export const PowWorker = {
mine<K extends number>(event: UnsignedEvent<K>, difficulty: number) {
return nip13.minePow(event, difficulty);
},
};
Comlink.expose(PowWorker);

View File

@ -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"