Home Bitcoin sha256 – calc Block Header hash

sha256 – calc Block Header hash

0
sha256 – calc Block Header hash

[ad_1]

I exploit the next code to calculate the header hash.

operate serializeBlockHeader(blockHeader) {
    const model = Buffer.alloc(4);
    model.writeUInt32LE(blockHeader.model);
    console.log('model size', model.size, model)

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

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


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

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


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

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

    return headerBuffer.toString('hex');
}

operate hash256(hexStr) {
    const information = Buffer.from(hexStr, 'hex')
    const hash1 = crypto.createHash('sha256').replace(information).digest();
    const hash2 = crypto.createHash('sha256').replace(hash1).digest();
    return hash2.toString('hex');
}

shopper.getBlockHash(600000) 
  .then((blockHash) => {
    shopper.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);
  });

ouput is

Block Header: [Object: null prototype] {
  hash: '00000000000000000007316856900e76b4f7a9139cfbfba89842c8d196cd5f91',
  confirmations: 153235,
  top: 600000,
  model: 536870912,
  versionHex: '20000000',
  merkleroot: '66b7c4a1926b41ceb2e617ddae0067e7bfea42db502017fde5b695a50384ed26',
  time: 1571443461,
  mediantime: 1571440177,
  nonce: 1066642855,
  bits: '1715a35c',
  issue: '13008091666971.9',
  chainwork: '00000000000000000000000000000000000000000962281c680c87bdb11f440b',
  nTx: 1925,
  previousblockhash: '00000000000000000003ecd827f336c6971f6f77a0b9fba362398dd867975645',
  nextblockhash: '00000000000000000000817313d6b5fe4838ec6eff47fbe7c4b9f22a40c2a4f4'
}
model size 4 <Buffer 00 00 00 20>
previousBlockHash size 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 size 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 size 4 <Buffer 05 53 aa 5d>
bits size 4 <Buffer 5c a3 15 17>
nonce size 4 <Buffer a7 advert 93 3f>
headerBytes 0000002045569767d88d3962a3fbb9a0776f1f97c636f327d8ec0300000000000000000026ed8403a595b6e5fd172050db42eabfe76700aedd17e6b2ce416b92a1c4b7660553aa5d5ca31517a7ad933f
header hash: 915fcd96d1c84298a8fbfb9c13a9f7b4760e9056683107000000000000000000
reverse hash: 00000000000000000007316856900e76b4f7a9139cfbfba89842c8d196cd5f91

Why is it essential to reverse the results of the double hash to acquire the proper header hash?

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here