mirror of
https://github.com/TecharoHQ/anubis.git
synced 2025-10-04 22:01:53 +08:00
* chore(challenge/preact): port to typescript Signed-off-by: Xe Iaso <me@xeiaso.net> * chore(js/algorithms): port to typescript Signed-off-by: Xe Iaso <me@xeiaso.net> * chore(js/worker): port to typescript Signed-off-by: Xe Iaso <me@xeiaso.net> * chore(web): fix TypeScript build logic Signed-off-by: Xe Iaso <me@xeiaso.net> * chore(web): port bench.mjs to typescript Signed-off-by: Xe Iaso <me@xeiaso.net> * chore(web): port main.mjs to typescript Signed-off-by: Xe Iaso <me@xeiaso.net> * Update metadata check-spelling run (pull_request) for Xe/use-typescript Signed-off-by: check-spelling-bot <check-spelling-bot@users.noreply.github.com> on-behalf-of: @check-spelling <check-spelling-bot@check-spelling.dev> * fix(js/algorithms/fast): handle old browsers Closes #1082 Signed-off-by: Xe Iaso <me@xeiaso.net> --------- Signed-off-by: Xe Iaso <me@xeiaso.net> Signed-off-by: check-spelling-bot <check-spelling-bot@users.noreply.github.com>
70 lines
No EOL
1.7 KiB
TypeScript
70 lines
No EOL
1.7 KiB
TypeScript
const encoder = new TextEncoder();
|
|
|
|
const calculateSHA256 = async (input: string) => {
|
|
const data = encoder.encode(input);
|
|
return await crypto.subtle.digest("SHA-256", data);
|
|
};
|
|
|
|
const toHexString = (byteArray: Uint8Array) => {
|
|
return byteArray.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
|
|
};
|
|
|
|
addEventListener("message", async ({ data: eventData }) => {
|
|
const { data, difficulty, threads } = eventData;
|
|
let nonce = eventData.nonce;
|
|
const isMainThread = nonce === 0;
|
|
let iterations = 0;
|
|
|
|
const requiredZeroBytes = Math.floor(difficulty / 2);
|
|
const isDifficultyOdd = difficulty % 2 !== 0;
|
|
|
|
for (; ;) {
|
|
const hashBuffer = await calculateSHA256(data + nonce);
|
|
const hashArray = new Uint8Array(hashBuffer);
|
|
|
|
let isValid = true;
|
|
for (let i = 0; i < requiredZeroBytes; i++) {
|
|
if (hashArray[i] !== 0) {
|
|
isValid = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (isValid && isDifficultyOdd) {
|
|
if ((hashArray[requiredZeroBytes] >> 4) !== 0) {
|
|
isValid = false;
|
|
}
|
|
}
|
|
|
|
if (isValid) {
|
|
const finalHash = toHexString(hashArray);
|
|
postMessage({
|
|
hash: finalHash,
|
|
data,
|
|
difficulty,
|
|
nonce,
|
|
});
|
|
return; // Exit worker
|
|
}
|
|
|
|
nonce += threads;
|
|
iterations++;
|
|
|
|
/* Truncate the decimal portion of the nonce. This is a bit of an evil bit
|
|
* hack, but it works reliably enough. The core of why this works is:
|
|
*
|
|
* > 13.4 % 1 !== 0
|
|
* true
|
|
* > 13 % 1 !== 0
|
|
* false
|
|
*/
|
|
if (nonce % 1 !== 0) {
|
|
nonce = Math.trunc(nonce);
|
|
}
|
|
|
|
// Send a progress update from the main thread every 1024 iterations.
|
|
if (isMainThread && (iterations & 1023) === 0) {
|
|
postMessage(nonce);
|
|
}
|
|
}
|
|
}); |