import hashlib
bitcoin_header_format = struct.Struct("
def serialize_block_header(block):
"""Serialize a block header from the RPC interface"""
return bitcoin_header_format.pack(
block['version'],
unhexlify(block['previousblockhash'])[::-1],
unhexlify(block['merkleroot'])[::-1],
block['time'],
unhexlify(block['bits'])[::-1],
block['nonce'])
def calculate_block_hash(raw_block_header):
hexlify(hashlib.sha256(hashlib.sha256(raw_block_header).digest()).digest()[::-1])
Note the [::-1] to the byte order to change the endianness in a few places.