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

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

#- Copyright (c) 2018,, 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 = \

# 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:

Match the Bitcoin network: True

This confirms our understanding that the transaction ID is the Double-SHA256 hash of the raw transaction data.

Last update: 2018.

Table of Contents

 About This Book

 Introduction of Bitcoin

 Bitcoin Blockchain

 Bitcoin Wallet

 Bitcoin Core

 Bitcoin Transaction

 Bitcoin-Qt - Bitcoin Core GUI

 Bitcoin Mining

 Bitcoin Consensus Rules

 Bitcoin Block Data Structure

Bitcoin Transaction Data Structure

 Data Properties of Bitcoin Transaction

 Data Structure of Bitcoin Raw Transaction Format

 Decode Bitcoin Raw Transaction Format

 Data Structure of Coinbase Transaction

Calculate Transaction ID API

 Copay - Bitcoin Wallet


 PDF Printing Version