Bitcoin Tutorials - Herong's Tutorial Notes - v1.07, by Herong Yang
Verify Merkle Root of 7 Transactions
This section describes how to verify the Merkle Root of 7 transactions in a Bitcoin block.
Now let's try a block with more transactions.
Get the block # 500 from Bitcoin Test Network:
C:\>\local\bitcoin-0.15.1\bin\bitcoin-cli -testnet getblockhash 500
00000000a2424460c992803ed44cfe0c0333e91af04fde9a6a97b468bf1b5f70
C:\>\local\bitcoin-0.15.1\bin\bitcoin-cli -testnet getblock
00000000a2424460c992803ed44cfe0c0333e91af04fde9a6a97b468bf1b5f70
...
"height": 500,
"merkleroot":
"dd3f288510dd3b632940bd3fb1db162d3ff99b19ddb0c586cfa3ac9a76d42517",
"tx": [
"a647d0c4112b4727f3c856782ff6bbaf099be929b27214a8e0dfedee4383eb68",
"24b8a4c788b8c805b810438ddd99e569e184ff20f4394ac49a6d832e69f57242",
"c5ffd70c3bc4998465cef55ed6d5d831ab3a550406423eb611117ed8ee41c278",
"4692772a73ea834c836915089acf97f2c790380a2b8fd32f82729da72545d8c5",
"82d6d88081e3e0eb36730f7f3aedb17228142b9e00a6dbaab4b53b798d0742c1",
"fc407d7a3b819daa5cf1ecc2c2a4b103c3782104d1425d170993bd534779a0da",
"95ad3ffb2a9426d6f5f5b97a134d90153ae16c9375f74eb385f481cff2771d77"
],
...
Here is my Python script and test result:
C:\>type Merkle-Root-Hash-of-Test-500.py
#- Merkle-Root-Hash-of-Test-500.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 transaction hashes from blockexplorer
h1 = "a647d0c4112b4727f3c856782ff6bbaf099be929b27214a8e0dfedee4383eb68"
h2 = "24b8a4c788b8c805b810438ddd99e569e184ff20f4394ac49a6d832e69f57242"
h3 = "c5ffd70c3bc4998465cef55ed6d5d831ab3a550406423eb611117ed8ee41c278"
h4 = "4692772a73ea834c836915089acf97f2c790380a2b8fd32f82729da72545d8c5"
h5 = "82d6d88081e3e0eb36730f7f3aedb17228142b9e00a6dbaab4b53b798d0742c1"
h6 = "fc407d7a3b819daa5cf1ecc2c2a4b103c3782104d1425d170993bd534779a0da"
h7 = "95ad3ffb2a9426d6f5f5b97a134d90153ae16c9375f74eb385f481cff2771d77"
# Converting them in little-endian hex notation
h1 = binascii.hexlify(binascii.unhexlify(h1)[::-1])
h2 = binascii.hexlify(binascii.unhexlify(h2)[::-1])
h3 = binascii.hexlify(binascii.unhexlify(h3)[::-1])
h4 = binascii.hexlify(binascii.unhexlify(h4)[::-1])
h5 = binascii.hexlify(binascii.unhexlify(h5)[::-1])
h6 = binascii.hexlify(binascii.unhexlify(h6)[::-1])
h7 = binascii.hexlify(binascii.unhexlify(h7)[::-1])
# Rount #1
h1 = doubleSha256(h1+h2)
h2 = doubleSha256(h3+h4)
h3 = doubleSha256(h5+h6)
h4 = doubleSha256(h7+h7)
# Rount #2
h1 = doubleSha256(h1+h2)
h2 = doubleSha256(h3+h4)
# Rount #3
hash = doubleSha256(h1+h2)
# Converting result to big-endian hex notation
root = binascii.hexlify(binascii.unhexlify(hash)[::-1])
root = str(root,"ascii")
# Comparing with Merkle Root on Bitcoin Core
exp = "dd3f288510dd3b632940bd3fb1db162d3ff99b19ddb0c586cfa3ac9a76d42517";
print("Match the Bitcoin network: "+str(root==exp))
print("Merkle Root:\n "+root)
C:\>Python Merkle-Root-Hash-of-testnet-500.py
Match the Bitcoin network: True
Merkle Root:
dd3f288510dd3b632940bd3fb1db162d3ff99b19ddb0c586cfa3ac9a76d42517
Cool. You should be able to make a generic script to verify the Merkle Root of any Bitcoin block now.
Table of Contents
Data Components of Bitcoin Block
Data Properties of Bitcoin Block
Calculate Double-SHA256 Hash with Python
Verify Merkle Root of 2 Transactions
►Verify Merkle Root of 7 Transactions
Data Structure of Bitcoin Block
"getblock blockhash 0" - Serialized Hex Block Data
Block Hash Calculation Algorithm
Block Hash Calculation in Python
Calculate Double-SHA256 Hash with Java