BSV.Block (bsv_sdk v2.0.1)

Copy Markdown View Source

A Bitcoin block consisting of a BSV.BlockHeader and a list of transactions.

Supports parsing from binary/hex, serialization, merkle root calculation, and validation.

Summary

Types

t()

Block struct

Functions

Calculate the merkle root from the block's transactions.

Parse a block from a binary.

Parse a block from a hex-encoded string.

Check if a block is a genesis block (prev_hash is all zeros).

Return the block hash (delegates to BlockHeader.hash/1).

Return the block hash as a display-order hex string.

Serialize a block to binary.

Serialize a block to a hex-encoded string.

Return the number of transactions in the block.

Validate that the calculated merkle root matches the header's merkle root.

Types

t()

@type t() :: %BSV.Block{header: BSV.BlockHeader.t(), txns: [BSV.Transaction.t()]}

Block struct

Functions

calc_merkle_root(block)

@spec calc_merkle_root(t()) :: <<_::256>>

Calculate the merkle root from the block's transactions.

from_binary(data)

@spec from_binary(binary()) :: {:ok, t(), binary()} | {:error, term()}

Parse a block from a binary.

Returns {:ok, block, rest} or {:error, reason}.

from_hex(hex)

@spec from_hex(String.t()) :: {:ok, t()} | {:error, term()}

Parse a block from a hex-encoded string.

genesis?(block)

@spec genesis?(t()) :: boolean()

Check if a block is a genesis block (prev_hash is all zeros).

hash(block)

@spec hash(t()) :: <<_::256>>

Return the block hash (delegates to BlockHeader.hash/1).

hash_hex(block)

@spec hash_hex(t()) :: String.t()

Return the block hash as a display-order hex string.

to_binary(block)

@spec to_binary(t()) :: binary()

Serialize a block to binary.

to_hex(block)

@spec to_hex(t()) :: String.t()

Serialize a block to a hex-encoded string.

tx_count(block)

@spec tx_count(t()) :: non_neg_integer()

Return the number of transactions in the block.

valid_merkle_root?(block)

@spec valid_merkle_root?(t()) :: boolean()

Validate that the calculated merkle root matches the header's merkle root.