Skip to content

Instantly share code, notes, and snippets.

@gentlyawesome
Created June 7, 2023 03:23
Show Gist options
  • Save gentlyawesome/4b176735ded4efc2b2f0c9b57592e0fa to your computer and use it in GitHub Desktop.
Save gentlyawesome/4b176735ded4efc2b2f0c9b57592e0fa to your computer and use it in GitHub Desktop.
const crypto = require('crypto');
// Block Header
const version = 1;
const previousBlockHash = 'ABC123';
const merkleRoot = 'XYZ789';
const timestamp = 1642931200;
let nonce = 0;
// Difficulty Target
const difficultyTarget = '000';
// Hash Function
function calculateHash(blockHeader) {
const hash = crypto.createHash('sha256').update(blockHeader).digest('hex');
return hash;
}
// Brute-Force Search
let hash = '';
while (!hash.startsWith(difficultyTarget)) {
const blockHeader = version + previousBlockHash + merkleRoot + timestamp + nonce;
hash = calculateHash(blockHeader);
nonce++;
}
console.log('Valid Hash Found!');
console.log('Nonce:', nonce - 1);
console.log('Hash:', hash);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment