aboutsummaryrefslogtreecommitdiff
path: root/web/js/proof-of-work-slow.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'web/js/proof-of-work-slow.mjs')
-rw-r--r--web/js/proof-of-work-slow.mjs18
1 files changed, 15 insertions, 3 deletions
diff --git a/web/js/proof-of-work-slow.mjs b/web/js/proof-of-work-slow.mjs
index e30dc21..6522c0b 100644
--- a/web/js/proof-of-work-slow.mjs
+++ b/web/js/proof-of-work-slow.mjs
@@ -1,6 +1,11 @@
// https://dev.to/ratmd/simple-proof-of-work-in-javascript-3kgm
-export default function process(data, difficulty = 5, _threads = 1) {
+export default function process(
+ data,
+ difficulty = 5,
+ progressCallback = null,
+ _threads = 1,
+) {
console.debug("slow algo");
return new Promise((resolve, reject) => {
let webWorkerURL = URL.createObjectURL(new Blob([
@@ -10,8 +15,12 @@ export default function process(data, difficulty = 5, _threads = 1) {
let worker = new Worker(webWorkerURL);
worker.onmessage = (event) => {
- worker.terminate();
- resolve(event.data);
+ if (typeof event.data === "number") {
+ progressCallback?.(event.data);
+ } else {
+ worker.terminate();
+ resolve(event.data);
+ }
};
worker.onerror = (event) => {
@@ -47,6 +56,9 @@ function processTask() {
let hash;
let nonce = 0;
do {
+ if (nonce & 1023 === 0) {
+ postMessage(nonce);
+ }
hash = await sha256(data + nonce++);
} while (hash.substring(0, difficulty) !== Array(difficulty + 1).join('0'));