UTXO Management

Overview

The UTXO (Unspent Transaction Output) Management component provides essential tools for tracking, selecting, and managing unspent outputs in BSV blockchain applications. While the BSV SDK doesn't include a built-in UTXO manager, it provides all the primitives needed to implement robust UTXO tracking systems.

This documentation demonstrates how to build efficient UTXO management using the SDK's Transaction, UnspentOutput, and wallet interfaces, including coin selection strategies, balance calculation, and UTXO lifecycle management.

Purpose

UTXO Management solves several critical challenges in BSV application development:

  • UTXO Tracking: Monitor all unspent outputs across multiple addresses and protocols

  • Coin Selection: Choose optimal UTXOs for transaction creation based on various strategies

  • Balance Calculation: Calculate spendable balances accurately across different contexts

  • UTXO Consolidation: Merge small UTXOs to reduce transaction size and fees

  • State Management: Track UTXO states (unspent, pending, spent) throughout lifecycle

  • Multi-Protocol Support: Manage UTXOs separately for different application protocols

Proper UTXO management is essential for building efficient, scalable BSV applications that minimize fees and maximize transaction throughput.

Basic Usage

Define UTXO Structure

Basic UTXO Tracker

Simple Coin Selection

Create Transaction from UTXOs

Key Features

1. UTXO State Management

Track UTXO lifecycle states from creation to spending:

2. Advanced Coin Selection Strategies

Implement multiple coin selection algorithms:

3. UTXO Consolidation

Merge small UTXOs to reduce fragmentation:

4. Multi-Address UTXO Tracking

Track UTXOs across multiple addresses and key derivation paths:

API Reference

UTXO Interface

ManagedUTXO Interface

UTXOState Enum

CoinSelectionStrategy Interface

Common Patterns

Pattern 1: UTXO Discovery and Syncing

Discover and sync UTXOs from blockchain:

Pattern 2: Optimistic UTXO Updates

Update UTXO set optimistically before confirmation:

Pattern 3: UTXO-Based Balance Calculation

Calculate balances with different confirmation requirements:

Security Considerations

Double-Spend Protection

Prevent double-spending by tracking UTXO states:

Performance Considerations

UTXO Set Pruning

Keep UTXO set size manageable:

  • Transactions: Create transactions from UTXOs

  • HD Wallets: Derive addresses for UTXO tracking

  • P2PKH: Most common locking script for UTXOs

  • SPV: Verify UTXO validity with SPV

Best Practices

  1. Always track UTXO states to prevent double-spending

  2. Use confirmation thresholds appropriate for transaction value

  3. Implement UTXO consolidation when fragmentation increases

  4. Choose coin selection strategy based on use case

  5. Sync UTXOs regularly from blockchain to stay current

  6. Prune old spent UTXOs to keep memory usage reasonable

  7. Handle optimistic updates carefully with rollback capability

  8. Validate UTXO ownership before spending

Troubleshooting

Issue: Insufficient Funds Despite Balance

Problem: Balance shows funds but coin selection fails.

Solution: Check for pending UTXOs:

Issue: UTXO Already Spent Error

Problem: Attempting to spend already-used UTXO.

Solution: Implement double-spend protection and sync UTXOs:

Further Reading

Status

✅ Complete - Comprehensive UTXO management documentation with tracking, selection strategies, and lifecycle management.

Last updated