aboutsummaryrefslogtreecommitdiff
path: root/static/solver.js
blob: a3f8366e431c19c034883add2d773ea5dba4efa5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

let wasm_instance = null;
let wasm_exports = null;

async function load_wasm() {
	let response = await fetch("/.powxy/wasm/solver.wasm");
	let { instance } = await WebAssembly.instantiateStreaming(response, {
		env: {
			memory: new WebAssembly.Memory({ initial: 1 })
		}
	});
	wasm_instance = instance;
	wasm_exports = instance.exports;
}

onmessage = async function(e) {
	let { identifier_bytes, difficulty } = e.data;

	if (!wasm_instance) {
		await load_wasm();
	}

	let ptr = wasm_exports.get_challenge_ptr();
	let memory = new Uint8Array(wasm_instance.exports.memory.buffer, ptr, 32);
	memory.set(identifier_bytes);

	let nonce = wasm_exports.solve(difficulty);

	let buf = new ArrayBuffer(8);
	let view = new DataView(buf);
	view.setBigUint64(0, BigInt(nonce), true);
	let nonce_bytes = new Uint8Array(buf);

	postMessage({ nonce, nonce_bytes });
};