BSV.SPV.MerklePath (bsv_sdk v1.1.0)

Copy Markdown View Source

Merkle path (BUMP) types and verification — BRC-74 binary format.

A MerklePath associates a transaction with a block via a sequence of hashes at each tree level.

Summary

Functions

Compute the Merkle root given a transaction hash (32 bytes, internal byte order).

Compute root from hex txid string (display order, byte-reversed).

Parse a MerklePath from binary data (BRC-74).

Parse a MerklePath from a hex string.

Serialize to BRC-74 binary format.

Serialize to hex string.

Types

t()

@type t() :: %BSV.SPV.MerklePath{
  block_height: non_neg_integer(),
  path: [[BSV.SPV.MerklePath.PathElement.t()]]
}

Functions

compute_root(mp, txid \\ nil)

@spec compute_root(t(), binary() | nil) :: {:ok, binary()} | {:error, String.t()}

Compute the Merkle root given a transaction hash (32 bytes, internal byte order).

If txid is nil, uses the first available hash from level 0.

Validates that duplicate elements only appear at the rightmost position of each level (mitigates CVE-2012-2459 second preimage attack).

compute_root_hex(mp, txid_hex \\ nil)

@spec compute_root_hex(t(), String.t() | nil) ::
  {:ok, String.t()} | {:error, String.t()}

Compute root from hex txid string (display order, byte-reversed).

from_bytes(data)

@spec from_bytes(binary()) :: {:ok, t(), binary()} | {:error, String.t()}

Parse a MerklePath from binary data (BRC-74).

Returns {:ok, merkle_path, remaining_bytes} so callers (e.g. BEEF parser) know exactly how many bytes were consumed without re-serialization.

from_hex(hex)

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

Parse a MerklePath from a hex string.

to_bytes(mp)

@spec to_bytes(t()) :: binary()

Serialize to BRC-74 binary format.

to_hex(mp)

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

Serialize to hex string.