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
e366ef88ac00000000
```

Take the Double-SHA256 hash value with this Python script:

```#- Calculate-Transaction-ID.py

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'+\
'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.

Last update: 2018.