Skip to main content

BlockHeader

Overview​

License: MIT​

library BlockHeader

A utility library for handling Bitcoin block headers. Provides functions for parsing, hashing, and converting block header data

Structs info​

HeaderData​

struct HeaderData {
bytes32 prevBlockHash;
bytes32 merkleRoot;
uint32 version;
uint32 time;
uint32 nonce;
bytes4 bits;
}

Represents the essential data contained within a Bitcoin block header

Parameters:

NameTypeDescription
prevBlockHashbytes32The hash of the previous block
merkleRootbytes32The Merkle root of the transactions in the block
versionuint32The block version number
timeuint32The block's timestamp
nonceuint32The nonce used for mining
bitsbytes4The encoded difficulty target for the block

Errors info​

InvalidBlockHeaderDataLength​

error InvalidBlockHeaderDataLength()

Emitted when the provided block header data has an invalid length. This error ensures that only correctly sized block headers are processed

Constants info​

BLOCK_HEADER_DATA_LENGTH (0xdf9899fd)​

uint256 constant BLOCK_HEADER_DATA_LENGTH = 80

The standard length of a Bitcoin block header in bytes

Functions info​

parseBlockHeader​

function parseBlockHeader(
bytes calldata blockHeaderRaw_,
bool returnInBEFormat_
)
internal
pure
returns (BlockHeader.HeaderData memory headerData_, bytes32 blockHash_)

Parses a raw byte array into a structured HeaderData and calculates its hash. It validates the length of the input and correctly decodes each field

Parameters:

NameTypeDescription
blockHeaderRaw_bytesThe raw bytes of the block header
returnInBEFormat_boolWhether to return the hashes in big-endian encoding

Return values:

NameTypeDescription
headerData_struct BlockHeader.HeaderDataThe parsed HeaderData structure
blockHash_bytes32The calculated hash of the block header

toRawBytes​

function toRawBytes(
BlockHeader.HeaderData memory headerData_,
bool inputInBEFormat_
) internal pure returns (bytes memory)

Converts a HeaderData structure back into its raw byte representation. This function reconstructs the original byte sequence of the block header

Parameters:

NameTypeDescription
headerData_struct BlockHeader.HeaderDataThe HeaderData structure to convert
inputInBEFormat_boolWhether headerData_ is expected to be in big-endian encoding

Return values:

NameTypeDescription
[0]bytesThe raw byte representation of the block header