From 4f01edb9230f58ff84b0dd892c931ec8ac9aad55 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Tue, 13 Sep 2022 12:56:08 +0200 Subject: move src out of srv, clean up default.nix and Makefile --- src/http/static/solvePow.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/http/static/solvePow.js (limited to 'src/http/static/solvePow.js') diff --git a/src/http/static/solvePow.js b/src/http/static/solvePow.js new file mode 100644 index 0000000..900400c --- /dev/null +++ b/src/http/static/solvePow.js @@ -0,0 +1,28 @@ +const fromHexString = hexString => + new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); + +const toHexString = bytes => + bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), ''); + +onmessage = async (e) => { + const seed = fromHexString(e.data.seedHex); + const target = e.data.target; + + const fullBuf = new ArrayBuffer(seed.byteLength*2); + + const fullBufSeed = new Uint8Array(fullBuf, 0, seed.byteLength); + seed.forEach((v, i) => fullBufSeed[i] = v); + + const randBuf = new Uint8Array(fullBuf, seed.byteLength); + + while (true) { + crypto.getRandomValues(randBuf); + const digest = await crypto.subtle.digest('SHA-512', fullBuf); + const digestView = new DataView(digest); + if (digestView.getUint32(0) < target) { + postMessage(toHexString(randBuf)); + return; + } + } + +}; -- cgit v1.2.3