From b6482d86fa58dee7d55889fa873463274f81d3db Mon Sep 17 00:00:00 2001 From: Jakob L. Kreuze Date: Tue, 15 Nov 2022 18:54:30 -0500 Subject: [dynamic] Cut chalkline --- haunt/static/js/proof-of-work.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 haunt/static/js/proof-of-work.js (limited to 'haunt/static') diff --git a/haunt/static/js/proof-of-work.js b/haunt/static/js/proof-of-work.js new file mode 100644 index 0000000..5207ac6 --- /dev/null +++ b/haunt/static/js/proof-of-work.js @@ -0,0 +1,34 @@ +const text = 'An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth.'; + +function makeid(length) { + var result = ''; + var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + var charactersLength = characters.length; + for ( var i = 0; i < length; i++ ) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; +} + +async function digestMessage(message) { + const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array + const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8); // hash the message + const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array + const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join(''); // convert bytes to hex string + return hashHex; +} + +const HARDNESS = 4; + +async function findPrefix(challenge) { + while (true) { + let prefix = makeid(32); + let digestHex = await digestMessage(prefix + challenge); + if (digestHex.startsWith("0".repeat(HARDNESS))) { + return [prefix + challenge, digestHex]; + } + } +} + +const suffix = "p12IyPB2dZsmBHYZyaEMrR5OUxqNc5Z9Cijal+9/iuQ="; +findPrefix(suffix).then(console.log); -- cgit v1.3