I wrote a short piece of software to check if I understood correctly how the block hash calculation is done.
I started from the python code published here:
https://bitcointalk.org/index.php?topic=5439273.0I analyzed a random block, namely block 784697
the program generates a wrong hash.
please, where am I wrong?
import binascii
import hashlib
# block 784697
# decoded_raw_block":{"hash":"000000000000000000044b32a02685122ddef6a1a54990a7f0883577ae67128e","confirmations":5,"height":784697,"version":577257472,"versionHex":"22684000","merkleroot":"97cbf0be2eb628cd1548d3b755f0e04b0fe4f06c15b4882ea90b7a2ae93728c0","time":1681074323,"mediantime":1681068560,"nonce":3518743859,"bits":"1705e0b2","difficulty":47887764338536.25,"chainwork":"0000000000000000000000000000000000000000451ad1754406a7d5e02bdd08","nTx":3932,"previousblockhash":"000000000000000000010550de6754269ccb5adeed388581cfc84f5aea6f45a1","nextblockhash":"0000000000000000000271ea86cffc855dc61554ed66b3b824d6e9f7778b5ce9","strippedsize":797005,"size":1601732,"weight":3992747,"tx":
#hash":"000000000000000000044b32a02685122ddef6a1a54990a7f0883577ae67128e
version="22684000"
prevhash="000000000000000000010550de6754269ccb5adeed388581cfc84f5aea6f45a1"
merkle_root="97cbf0be2eb628cd1548d3b755f0e04b0fe4f06c15b4882ea90b7a2ae93728c0"
# "time":1681074323,"mediantime":1681068560,"nonce":3518743859,"bits":"1705e0b2"
nbits="1705e0b2"
ntime="1681074323"
nonce="3518743859"
blockheader = version + prevhash + merkle_root + nbits + ntime + nonce +\
'000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000'
# print('blockheader:\n{}\n'.format(blockheader))
hash = hashlib.sha256(hashlib.sha256(binascii.unhexlify(blockheader)).digest()).digest()
hash = binascii.hexlify(hash).decode()
print('hash: {}'.format(hash))
Edit:
I used this link for the node:
https://api.blockchair.com/bitcoin/raw/block/000000000000000000044b32a02685122ddef6a1a54990a7f0883577ae67128e