How do you calculate the Bitcoin transaction hash for a Coinbase transaction in 2022?


According to all the internet sources I’ve found, the hash/id of the transaction is the result of the double_sha256 algorithm which takes all the raw data of the transaction. I tried to calculate it in Python but got incorrect results in the last blocks. Here is my test script for block #745778:

import hashlib

def d_SHA256(hex_string):
    return hashlib.sha256(hashlib.sha256(bytes.fromhex(hex_string)).digest()).digest()

# 
coinbase_hash="aa7f5b5f200feac9364ec40b4a5c0cb5c291f846422dec373a71cf30c55ce856"

# 
coinbase_data="010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff640332610b2cfabe6d6ddcb8d8f2a2ddf5191d8191cfa7aa4fd9d85c529f2ce8ed4363c1a8942f14810b10000000f09f909f092f4632506f6f6c2f6500000000000000000000000000000000000000000000000000000000000000000000000500540000000000000004b1bbbb26000000001976a914c825a1ecf2a6830c4401620c3a16f1995057c2ab88ac0000000000000000266a24aa21a9edd02fc86dcb2b66db1a5add17b3660e4046f50cde03199425f0944c7becb6546a0000000000000000266a2448617468e62698d5bdd57572ff76305ed48933e8b787a67df4319ade7d798df03c706edf00000000000000002c6a4c2952534b424c4f434b3a3c622bf845a23d0ee4927d14fe0455d8e30af6e68e7e3620aa1af6270044738b01200000000000000000000000000000000000000000000000000000000000000000730b053f"
tmp_coinbase_hash   = d_SHA256(coinbase_data)       # big-endian in bytes
my_coinbase_hash    = tmp_coinbase_hash[::-1].hex() # litte-endian in hex

print('coinb_hash:', coinbase_hash)
print('my_cb_hash:', my_coinbase_hash)

coinb_hash: aa7f5b5f200feac9364ec40b4a5c0cb5c291f846422dec373a71cf30c55ce856
my_cb_hash: b33db7a32051e9cf9f73bcbcfb2f6f454c8baa9317738f2c8a703593dd01bf44

I don’t understand what I’m doing wrong. Has the protocol changed? Because it works for old blocks but not for modern mined blocks and I can’t find any information about this issue.



Source link

Related Posts