Bitcoin Tutorials - Herong's Tutorial Examples - v1.06, by Herong Yang
Calculate Transaction ID
This section describes how to calculate the transaction ID from the raw transaction data.
If you want to verify the transaction ID or the raw transaction data of a given transaction, you take calculate the Double-SHA256 hash of the raw transaction data. It should match the transaction ID:
tx_id == SHA256(SHA256(raw_transaction))
Now let's verify transaction ID of 02f754075a7fae665fa4440b799c711e319a56357eda6e6bc84d40220b200361 on the Bitcoin test network by taking the raw transaction data first.
C:\>\local\bitcoin-0.15.1\bin\bitcoin-cli.exe -testnet getrawtransaction 02f754075a7fae665fa4440b799c711e319a56357eda6e6bc84d40220b200361 false 0100000001000000000000000000000000000000000000000000000000000000000000 0000ffffffff1d030f8d13049faa805a063538706f6f6c0c00010000fe220300000000 00ffffffff015341cb04000000001976a914f11298ce777cb5db5c09250cad4eb856b1 e366ef88ac00000000
Take the Double-SHA256 hash value with this Python script:
#- Calculate-Transaction-ID.py #- Copyright (c) 2018, HerongYang.com, All Rights Reserved. import hashlib import binascii def doubleSha256(hex): bin = binascii.unhexlify(hex) hash = hashlib.sha256(bin).digest() hash2 = hashlib.sha256(hash).digest() return binascii.hexlify(hash2) # Getting raw transaction data raw = \ '0100000001000000000000000000000000000000000000000000000000000000000000'+\ '0000ffffffff1d030f8d13049faa805a063538706f6f6c0c00010000fe220300000000'+\ '00ffffffff015341cb04000000001976a914f11298ce777cb5db5c09250cad4eb856b1'+\ 'e366ef88ac00000000' # Calculating Double-SHA256 hash hash = doubleSha256(raw) # Converting result to big-endian hex notation txid = binascii.hexlify(binascii.unhexlify(hash)[::-1]) txid = str(txid,"ascii") # Comparing with transaction ID exp = "02f754075a7fae665fa4440b799c711e319a56357eda6e6bc84d40220b200361"; print("Match the Bitcoin network: "+str(txid==exp)) print("Tx_ID:\n "+txid)
Here is output of the Python script:
C:\>Python Calculate-Transaction-ID.py Match the Bitcoin network: True Tx_ID: 02f754075a7fae665fa4440b799c711e319a56357eda6e6bc84d40220b200361
This confirms our understanding that the transaction ID is the Double-SHA256 hash of the raw transaction data.
Table of Contents
►Bitcoin Transaction Data Structure
Data Properties of Bitcoin Transaction
Data Structure of Bitcoin Raw Transaction Format
Decode Bitcoin Raw Transaction Format