Created
June 7, 2023 03:23
-
-
Save gentlyawesome/4b176735ded4efc2b2f0c9b57592e0fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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