Block Hash Calculation in Python

This section describes how to calculate Bitcoin block hash in Python.

In the previous tutorial, we verify the Bitcoin block hash calculation quickly with the Python example code given in the Block hashing algorithm at https://en.bitcoin.it/wiki/Block_hashing_algorithm. It uses header values given in little-endian hex notation directly.

Now let's create a detailed version of Python code that takes header values displayed on blockchain.com and follows our Bitcoin block hash calculation algorithm precisely.

C:\>Python 
>>> import hashlib
>>> import binascii
>>> import datetime

>>> # 1. Getting header values from blockchain.com
>>> version = "00000001" # 1 from blockchain.com
>>> hashPrevBlock = \
    "00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81"
>>> hashMerkleRoot =  \
    "2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3"

>>> time = datetime.datetime(2011,5,21,13,26,31) # May 21, 2011 1:26:31 PM
>>> time = int(time.timestamp()) # 1305998791 as POSIX timstamp
>>> time = hex(int(0x100000000)+time)[-8:] # in 4-byte hex notation
>>> print(time) # should print '4dd7f5c7'
    
>>> bits = "1a44b9f2" # blockchain.com

>>> nonce = 2504433986 # in decimal notation from blockchain.com
>>> nonce = hex(int(0x100000000)+nonce)[-8:] # in 4-byte hex notation 
>>> print(nonce) # should print '9546a142'
 
>>> # 2. Converting them in little-endian hex notation
>>> version = binascii.hexlify(binascii.unhexlify(version)[::-1])
>>> hashPrevBlock = \
    binascii.hexlify(binascii.unhexlify(hashPrevBlock)[::-1])
>>> hashMerkleRoot = \
    binascii.hexlify(binascii.unhexlify(hashMerkleRoot)[::-1])
>>> time = binascii.hexlify(binascii.unhexlify(time)[::-1])
>>> bits = binascii.hexlify(binascii.unhexlify(bits)[::-1])
>>> nonce = binascii.hexlify(binascii.unhexlify(nonce)[::-1])

>>> # 3. Concatenating header values
>>> header = version+hashPrevBlock+hashMerkleRoot+time+bits+nonce

>>> # 4. Taking the double-SHA256 hash value
>>> header = binascii.unhexlify(header)
>>> hash = hashlib.sha256(hashlib.sha256(header).digest()).digest()
>>> hash = binascii.hexlify(hash)

>>> # 5. Converting the hash value in big-endian hex notation
>>> hash = binascii.hexlify(binascii.unhexlify(hash)[::-1])
>>> hash
b'00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d'

Cool. Our Python script works!

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

 Data Components of Bitcoin Block

 Data Properties of Bitcoin Block

 Merkle Root 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

 Bitcoin Transaction Data Structure

 Bitcoin Blockchain APIs

 Copay - Bitcoin Wallet

 Archived Tutorials

 References

 Full Version in PDF/EPUB