diff options
| author | Jakob L. Kreuze | 2022-11-15 18:54:30 -0500 |
|---|---|---|
| committer | Jakob L. Kreuze | 2022-11-15 18:58:20 -0500 |
| commit | b6482d86fa58dee7d55889fa873463274f81d3db (patch) | |
| tree | ab82cfff0cbc7ed4294975bd526ad0f1a36c8812 /haunt/static | |
| parent | 311c083a7786ff83b4810a507fb7af492b20121d (diff) | |
[dynamic] Cut chalkline
Diffstat (limited to 'haunt/static')
| -rw-r--r-- | haunt/static/js/proof-of-work.js | 34 |
1 files changed, 34 insertions, 0 deletions
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); |