Skip to content

Class BSV::Transaction::MerklePath

Inherits: Object

A BRC-74 merkle path (BUMP — Bitcoin Unified Merkle Path).

Encodes the proof that a transaction is included in a block by storing the minimum set of intermediate hashes needed to recompute the block's merkle root from a given transaction ID.

@example Parse a BUMP from hex and compute the merkle root

mp = BSV::Transaction::MerklePath.from_hex(bump_hex)
root_hex = mp.compute_root_hex(txid_hex)

Attributes

block_height [R]

  • @return [Integer] the block height this merkle path belongs to

path [R]

  • @return [Array>] tree levels, each an array of leaves

Public Class Methods

from_binary(data, offset = 0)

Deserialise a merkle path from BRC-74 binary format. - @param data [String] binary data - @param offset [Integer] byte offset to start reading from - @return [Array(MerklePath, Integer)] the merkle path and bytes consumed

from_hex(hex)

Deserialise a merkle path from a BRC-74 hex string. - @param hex [String] hex-encoded BUMP data - @return [MerklePath] the parsed merkle path

merkle_tree_parent(left, right)

Compute the parent hash of two sibling nodes. - @param left [String] 32-byte left child hash - @param right [String] 32-byte right child hash - @return [String] 32-byte parent hash (double-SHA-256 of concatenation)

Public Instance Methods

combine(other)

Merge another merkle path into this one.

Both paths must share the same block height and merkle root. After combining, this path contains the union of all leaves. - @param other [MerklePath] the path to merge in - @raise [ArgumentError] if block heights or merkle roots differ - @return [self] for chaining

compute_root(txid = nil)

Recompute the merkle root from this path and a transaction ID. - @param txid [String, nil] 32-byte txid in internal byte order (auto-detected if nil) - @raise [ArgumentError] if the txid is not found in the path - @return [String] 32-byte merkle root in internal byte order

compute_root_hex(txid_hex = nil)

Recompute the merkle root and return it as a hex string. - @param txid_hex [String, nil] hex-encoded txid (display order) - @return [String] hex-encoded merkle root (display order)

initialize(block_height:, path:)

  • @param block_height [Integer] the block height
  • @param path [Array>] tree levels
  • @return [MerklePath] a new instance of MerklePath

to_binary()

Serialise the merkle path to BRC-74 binary format. - @return [String] binary BUMP data

to_hex()

Serialise the merkle path to a BRC-74 hex string. - @return [String] hex-encoded BUMP data

verify(txid_hex, chain_tracker)

Verify that this merkle path is valid for a given transaction.

Computes the merkle root from the path and txid, then checks it against the blockchain via the provided chain tracker. - @param txid_hex [String] hex-encoded transaction ID (display order) - @param chain_tracker [ChainTracker] chain tracker to verify the root against - @return [Boolean] true if the computed root matches the block at this height