Merkle Proofs
Overview
The Merkle Proofs component implements Simplified Payment Verification (SPV) using merkle trees, enabling lightweight clients to verify transaction inclusion in blocks without downloading the entire blockchain. The MerklePath class provides functionality for creating, parsing, and verifying merkle proofs according to BSV's TSC (Transaction Signature Component) format.
Merkle proofs are essential for SPV validation, allowing wallets and applications to cryptographically prove that a transaction exists in a specific block by providing only the merkle branch path and block header, rather than all transactions in the block.
Purpose
Merkle Proofs in the BSV SDK solve several critical problems:
SPV Verification: Verify transaction inclusion without full blockchain data
Bandwidth Efficiency: Reduce data transmission by providing only merkle paths
Scalability: Enable lightweight clients to operate without full node infrastructure
Proof of Inclusion: Cryptographically prove transaction existence in blocks
BEEF Integration: Support transaction envelope format (BRC-62) with merkle proofs
Chain Verification: Validate merkle roots against block headers
This component is fundamental for building scalable BSV applications that don't require full node infrastructure while maintaining cryptographic security guarantees.
Basic Usage
Parse Merkle Path from Hex
Attach Merkle Proof to Transaction
Verify Merkle Proof Against Block Header
Create Merkle Path from Transaction Data
Key Features
1. TSC Merkle Path Format
The SDK implements the TSC (Transaction Signature Component) format for merkle paths, providing a standardized binary representation:
2. SPV Transaction Validation
Merkle proofs enable SPV validation by proving transaction inclusion:
3. Merkle Path Computation
Calculate merkle roots from transaction hashes:
4. Source Transaction with SPV Proofs
Use merkle proofs with source transactions for SPV-enabled transaction chains:
API Reference
MerklePath Class
MerklePath Properties
blockHeight: Block height where transaction was included
path: Array of merkle tree levels, each containing offset/hash pairs
MerklePath Methods
static fromHex(hex: string): MerklePath
static fromHex(hex: string): MerklePathParse merkle path from hexadecimal string.
static fromBinary(binary: number[]): MerklePath
static fromBinary(binary: number[]): MerklePathParse merkle path from binary array.
toHex(): string
toHex(): stringSerialize merkle path to hexadecimal string.
toBinary(): number[]
toBinary(): number[]Serialize merkle path to binary array.
computeRoot(txid?: string): Uint8Array
computeRoot(txid?: string): Uint8ArrayCompute merkle root from the path and optional transaction ID.
verify(txid: string, chainTracker: ChainTracker): Promise<boolean>
verify(txid: string, chainTracker: ChainTracker): Promise<boolean>Verify the merkle proof against blockchain using ChainTracker.
ChainTracker Interface
Implementations must verify that a given merkle root is valid for a specific block height by checking against blockchain data.
Common Patterns
1. WhatsOnChain SPV Verification
2. Transaction Chain with SPV Proofs
3. Merkle Proof Caching and Storage
Security Considerations
1. Always Verify Merkle Roots
BAD - Trusting merkle proofs without verification:
GOOD - Always verify against blockchain:
2. Validate ChainTracker Sources
BAD - Using untrusted chain tracker:
GOOD - Use trusted data sources:
3. Protect Against Merkle Path Manipulation
4. Handle Merkle Proof Errors Gracefully
Performance Considerations
1. Cache Merkle Proofs
Merkle proofs don't change once a transaction is confirmed, so aggressive caching is beneficial:
2. Batch Merkle Proof Fetching
3. Optimize Merkle Root Computation
Related Components
Transaction - Attach merkle proofs to transactions for SPV
SPV - SPV verification using merkle proofs and chain tracking
BEEF - Transaction envelopes with merkle proofs (BRC-62)
ARC - Broadcasting transactions with merkle proof callbacks
Script - Transaction scripts verified through merkle proofs
Best Practices
1. Always Verify Merkle Proofs
Never trust merkle proofs without verification against the blockchain:
2. Use Merkle Proofs for Source Transactions
Always attach merkle proofs when using previous transactions as inputs:
3. Implement Robust ChainTrackers
Use multiple data sources for merkle root verification:
4. Cache Merkle Proofs Aggressively
Merkle proofs are immutable once confirmed, so cache them indefinitely:
5. Validate Merkle Path Structure
Always validate the structure of merkle paths before using them:
Troubleshooting
Invalid Merkle Root
Problem: Computed merkle root doesn't match blockchain.
Solution:
ChainTracker Failures
Problem: ChainTracker always returns false or throws errors.
Solution:
Merkle Path Parsing Errors
Problem: Cannot parse merkle path from hex string.
Solution:
Performance Issues with Large Chains
Problem: Slow performance when verifying many merkle proofs.
Solution:
Further Reading
BRC-9: Simplified Payment Verification - SPV implementation specification
BRC-67: SPV Validation Rules - SPV validation requirements
BRC-62: BEEF Format - Transaction envelopes with merkle proofs
BRC-8: Transaction Envelopes - Background exchange of SPV proofs
BSV SDK Documentation - Official SDK documentation
Bitcoin Merkle Trees - Understanding merkle tree structure
Status
✅ Complete - Comprehensive documentation with TSC format, SPV verification, and merkle path management patterns.
Last updated
