JSFiddle - React, Tailwind, and code Playground
by Josh Pullen
JavaScript
const messagePrefix = "Alice pays Bob 100 LD\nBob pays Charlie 20 LD\nCharlie pays You 50 LD\n";
const startingNonce = 100000 * 57;
const testCount = 100000;
const promises = new Array(testCount).fill(null).map((_, n) => {
const nonce = startingNonce + n;
const message = messagePrefix + String(nonce);
return (async function() {
const encoder = new TextEncoder();
const data = encoder.encode(message);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
const hashArray = Array.from(new Uint8Array(hashBuffer));;
const hashString = hashArray
.map(byte => byte.toString(2).padStart(8, "0"))
.join("");
return hashString;
})();
})
Promise.all(promises)
.then(results => {
const zeroes = results.map(hash => hash.split("1")[0].length);
let maxIndex = 0;
for (let i = 1; i < zeroes.length; i++) {
if (zeroes[i] > zeroes[maxIndex]) {
maxIndex = i;
}
}
console.log(startingNonce + maxIndex, zeroes[maxIndex]);
});