mirror of
https://github.com/TecharoHQ/anubis.git
synced 2025-08-04 10:18:17 -04:00

This completely rewrites how the proof of work challenge works based on feedback from browser engine developers and starts the process of making the proof of work function easier to change out. - Import @aws-crypto/sha256-js to use in Firefox as its implementation of WebCrypto doesn't jump directly from highly optimized browser internals to JIT-ed JavaScript like Chrome's seems to. - Move the worker code to `web/js/worker/*` with each worker named after the hashing method and hash method implementation it uses. - Update bench.mjs to import algorithms the new way. - Delete video.mjs, it was part of a legacy experiment that I never had time to finish. - Update LibreJS comment to add info about the use of @aws-crypto/sha256-js. - Also update my email to my @techaro.lol address. Signed-off-by: Xe Iaso <me@xeiaso.net>
77 lines
1.8 KiB
JavaScript
77 lines
1.8 KiB
JavaScript
export default function process(
|
|
{ basePrefix, version },
|
|
data,
|
|
difficulty = 5,
|
|
signal = null,
|
|
progressCallback = null,
|
|
threads = Math.max(navigator.hardwareConcurrency / 2, 1),
|
|
) {
|
|
console.debug("fast algo");
|
|
|
|
let workerMethod = window.crypto !== undefined ? "webcrypto" : "purejs";
|
|
|
|
if (navigator.userAgent.includes("Firefox") || navigator.userAgent.includes("Goanna")) {
|
|
console.log("Firefox detected, using pure-JS fallback");
|
|
workerMethod = "purejs";
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
let webWorkerURL = `${basePrefix}/.within.website/x/cmd/anubis/static/js/worker/sha256-${workerMethod}.mjs?cacheBuster=${version}`;
|
|
|
|
console.log(webWorkerURL);
|
|
|
|
const workers = [];
|
|
let settled = false;
|
|
|
|
const cleanup = () => {
|
|
if (settled) {
|
|
return;
|
|
}
|
|
settled = true;
|
|
workers.forEach((w) => w.terminate());
|
|
if (signal != null) {
|
|
signal.removeEventListener("abort", onAbort);
|
|
}
|
|
};
|
|
|
|
const onAbort = () => {
|
|
console.log("PoW aborted");
|
|
cleanup();
|
|
reject(new DOMException("Aborted", "AbortError"));
|
|
};
|
|
|
|
if (signal != null) {
|
|
if (signal.aborted) {
|
|
return onAbort();
|
|
}
|
|
signal.addEventListener("abort", onAbort, { once: true });
|
|
}
|
|
|
|
for (let i = 0; i < threads; i++) {
|
|
let worker = new Worker(webWorkerURL);
|
|
|
|
worker.onmessage = (event) => {
|
|
if (typeof event.data === "number") {
|
|
progressCallback?.(event.data);
|
|
} else {
|
|
cleanup();
|
|
resolve(event.data);
|
|
}
|
|
};
|
|
|
|
worker.onerror = (event) => {
|
|
cleanup();
|
|
reject(event);
|
|
};
|
|
|
|
worker.postMessage({
|
|
data,
|
|
difficulty,
|
|
nonce: i,
|
|
threads,
|
|
});
|
|
|
|
workers.push(worker);
|
|
}
|
|
});
|
|
} |