Crypto Flexs
  • DIRECTORY
  • CRYPTO
    • ETHEREUM
    • BITCOIN
    • ALTCOIN
  • BLOCKCHAIN
  • EXCHANGE
  • ADOPTION
  • TRADING
  • HACKING
  • SLOT
Crypto Flexs
  • DIRECTORY
  • CRYPTO
    • ETHEREUM
    • BITCOIN
    • ALTCOIN
  • BLOCKCHAIN
  • EXCHANGE
  • ADOPTION
  • TRADING
  • HACKING
  • SLOT
Crypto Flexs
Home»BITCOIN NEWS»sha256 – Compute block header hash
BITCOIN NEWS

sha256 – Compute block header hash

By Crypto FlexsNovember 29, 20232 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email
sha256 – Compute block header hash
Share
Facebook Twitter LinkedIn Pinterest Email

I use the following code to calculate the header hash:

function serializeBlockHeader(blockHeader) 
    const version = Buffer.alloc(4);
    version.writeUInt32LE(blockHeader.version);
    console.log('version length', version.length, version)

    const previousBlockHash = Buffer.from(blockHeader.previousblockhash, 'hex').reverse();
    console.log('previousBlockHash length', previousBlockHash.length, previousBlockHash)

    const merkleRoot = Buffer.from(blockHeader.merkleroot, 'hex').reverse();
    console.log('merkleRoot length', merkleRoot.length, merkleRoot)


    const time = Buffer.alloc(4);
    time.writeUInt32LE(blockHeader.time);
    console.log('time length', time.length, time)

    const bits = Buffer.from(blockHeader.bits, 'hex').reverse();
    console.log('bits length', bits.length, bits)


    const nonce = Buffer.alloc(4);
    nonce.writeUInt32LE(blockHeader.nonce);
    console.log('nonce length', nonce.length, nonce)

    const headerBuffer = Buffer.concat((
        version,
        previousBlockHash,
        merkleRoot,
        time,
        bits,
        nonce
    ));

    return headerBuffer.toString('hex');


function hash256(hexStr) 
    const data = Buffer.from(hexStr, 'hex')
    const hash1 = crypto.createHash('sha256').update(data).digest();
    const hash2 = crypto.createHash('sha256').update(hash1).digest();
    return hash2.toString('hex');


client.getBlockHash(600000) 
  .then((blockHash) => 
    client.getBlockHeader(blockHash)
      .then((blockHeader) => 
        console.log('Block Header:', blockHeader);
        const headerBytes = serializeBlockHeader(blockHeader)
        console.log("headerBytes", headerBytes)
        
        console.log('header hash:', hash256(headerBytes))
      )
      .catch((err) => 
        console.error('Error getting block header:', err);
      );
  )
  .catch((err) => 
    console.error('Error getting block hash:', err);
  );

The output is

Block Header: (Object: null prototype) 
  hash: '00000000000000000007316856900e76b4f7a9139cfbfba89842c8d196cd5f91',
  confirmations: 153235,
  height: 600000,
  version: 536870912,
  versionHex: '20000000',
  merkleroot: '66b7c4a1926b41ceb2e617ddae0067e7bfea42db502017fde5b695a50384ed26',
  time: 1571443461,
  mediantime: 1571440177,
  nonce: 1066642855,
  bits: '1715a35c',
  difficulty: '13008091666971.9',
  chainwork: '00000000000000000000000000000000000000000962281c680c87bdb11f440b',
  nTx: 1925,
  previousblockhash: '00000000000000000003ecd827f336c6971f6f77a0b9fba362398dd867975645',
  nextblockhash: '00000000000000000000817313d6b5fe4838ec6eff47fbe7c4b9f22a40c2a4f4'

version length 4 <Buffer 00 00 00 20>
previousBlockHash length 32 <Buffer 45 56 97 67 d8 8d 39 62 a3 fb b9 a0 77 6f 1f 97 c6 36 f3 27 d8 ec 03 00 00 00 00 00 00 00 00 00>
merkleRoot length 32 <Buffer 26 ed 84 03 a5 95 b6 e5 fd 17 20 50 db 42 ea bf e7 67 00 ae dd 17 e6 b2 ce 41 6b 92 a1 c4 b7 66>
time length 4 <Buffer 05 53 aa 5d>
bits length 4 <Buffer 5c a3 15 17>
nonce length 4 <Buffer a7 ad 93 3f>
headerBytes 0000002045569767d88d3962a3fbb9a0776f1f97c636f327d8ec0300000000000000000026ed8403a595b6e5fd172050db42eabfe76700aedd17e6b2ce416b92a1c4b7660553aa5d5ca31517a7ad933f
header hash: 915fcd96d1c84298a8fbfb9c13a9f7b4760e9056683107000000000000000000
reverse hash: 00000000000000000007316856900e76b4f7a9139cfbfba89842c8d196cd5f91

Why do I need to invert the result of a double hash to get the correct header hash?

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

Related Posts

It starts the flash launch flash 2.0 and simplifies Bitcoin payment for business around the world.

May 15, 2025

Binance: 80%of Asian encryption users adopt 2FA but advanced security delay

May 15, 2025

Bitcoin on ‘diversifier’ more than Safe-haven assets

May 14, 2025
Add A Comment

Comments are closed.

Recent Posts

Is the US PPI a surge in 2.4%, Bitcoin and Altcoin?

May 16, 2025

GeForce is now expanded to ‘Doom: The Dark Ages’.

May 16, 2025

As Momentum faces important tests, Solana is seeing the return of investors.

May 16, 2025

Solana Network Activity Surge and ‘Megaphone’ Chart Pattern Set $ ​​210 SOL Trame Target

May 16, 2025

Dow Jump 271 Points, S & P 500 is a victory march, NASDAQ SHEDS 0.18%

May 16, 2025

New distributed game token sky rock according to binary list

May 15, 2025

Network rendering April 2025 ecosystem development and strategic partnership

May 15, 2025

Bitcoin Traders evolves to the role of BTC in all portfolios for $ 100K support $ 100K support.

May 15, 2025

How to discover quality in floods in the Internet capital market tokens

May 15, 2025

The judge rejects the proposed agreement agreement of the SEC and Ripple and supports a $ 125m fine.

May 15, 2025

Clothing manufacturers, headquartered in China, say they are looking at $ 800 million BTC and Trump.

May 15, 2025

Crypto Flexs is a Professional Cryptocurrency News Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of Cryptocurrency. We hope you enjoy our Cryptocurrency News as much as we enjoy offering them to you.

Contact Us : Partner(@)Cryptoflexs.com

Top Insights

Is the US PPI a surge in 2.4%, Bitcoin and Altcoin?

May 16, 2025

GeForce is now expanded to ‘Doom: The Dark Ages’.

May 16, 2025

As Momentum faces important tests, Solana is seeing the return of investors.

May 16, 2025
Most Popular

MicroStrategy’s Role as a Bitcoin (BTC) Proxy: A Strategic Analysis

November 26, 2024

Solana Up 27% in July and SOL Price Nears Yearly High After ‘Double Bottom’

July 29, 2024

After a Decade of Legal Battles, Mt. Gox Bitcoin Distribution Is Underway

July 29, 2024
  • Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions
© 2025 Crypto Flexs

Type above and press Enter to search. Press Esc to cancel.