SPV

Overview

SPV (Simplified Payment Verification) enables lightweight blockchain clients to verify transactions without downloading the entire blockchain. The BSV SDK implements BRC-9 and BRC-67 standards, providing robust SPV verification through the ChainTracker interface, MerklePath class, and header validation mechanisms.

SPV allows applications to verify that a transaction is included in a block by checking the merkle proof against the block header's merkle root, requiring only block headers rather than full block data.

Purpose

SPV verification solves critical challenges for BSV applications:

  • Lightweight Verification: Verify transactions without full blockchain download

  • Trustless Operation: Verify transaction inclusion cryptographically without trusting third parties

  • Scalability: Support massive transaction throughput with minimal resource requirements

  • Header Chain Validation: Validate proof-of-work and block header chains

  • Merkle Root Verification: Confirm transactions are included in valid blocks

This enables mobile wallets, IoT devices, and web applications to securely interact with BSV blockchain.

Basic Usage

Verify Transaction with Merkle Proof

import { MerklePath, Transaction, ChainTracker } from '@bsv/sdk';

// Parse merkle proof from hex
const merklePath = MerklePath.fromHex('fed7c509000a02fddd01...');

// Get the transaction
const tx = Transaction.fromHex('...');

// Attach merkle proof to transaction
tx.merklePath = merklePath;

// Verify the transaction is in the merkle tree
const txid = tx.id('hex');
const computedRoot = merklePath.computeRoot(txid);

console.log('Computed merkle root:', computedRoot);
console.log('Block height:', merklePath.blockHeight);

Implement ChainTracker

Verify SPV Proof

Validate Block Headers

Key Features

1. ChainTracker Interface

Implement custom chain trackers for merkle root verification:

2. Merkle Path Verification

Verify transactions using merkle paths per BRC-9:

3. Transaction SPV Envelope (BRC-8)

Create and verify SPV envelopes for transactions:

4. Header Chain Validation

Validate block header chains and proof-of-work:

API Reference

ChainTracker Interface

Methods:

  • isValidRootForHeight(root, height): Verify merkle root matches block at height

MerklePath Class

Methods:

  • fromHex(hex): Parse merkle path from hex string

  • toHex(): Serialize merkle path to hex

  • computeRoot(txid): Calculate merkle root from txid and path

BlockHeader Class

Common Patterns

Pattern 1: SPV Client Implementation

Pattern 2: SPV Proof Service

Pattern 3: Multi-Source Chain Tracker

Security Considerations

  • Always verify merkle roots against trusted chain tracker

  • Validate proof-of-work for header chains

  • Check block heights are within expected range

  • Use multiple data sources for critical verifications

  • Implement caching with expiration for chain tracker results

Performance Considerations

  • Cache merkle root lookups to reduce API calls

  • Batch verification of multiple transactions

  • Pre-fetch headers for expected block ranges

  • Use local chain tracker when full node available

  • Merkle Proofs: Detailed merkle proof operations

  • Transactions: Transaction structure and SPV attachment

  • BEEF: BEEF format for transaction packages with proofs

Best Practices

  1. Always use ChainTracker for production verification

  2. Cache verification results to improve performance

  3. Validate block heights are reasonable

  4. Use multiple sources for critical applications

  5. Implement fallback trackers for reliability

Troubleshooting

Issue: Merkle Root Mismatch

Solution: Ensure transaction ID is computed correctly:

Issue: Missing Block Height

Solution: Check merkle path includes block height:

Further Reading

Status

✅ Complete - Comprehensive SPV documentation with ChainTracker implementation and header validation.

Last updated