NBXplorer Documentation

repository·master·Indexed 18 days ago

https://github.com/btcpayserver/nbxplorer

A minimalist .NET-based UTXO tracker for HD wallets designed as a backend infrastructure tool. It supports Bitcoin and various altcoins (LTC, DASH, DOGE, etc.), providing a REST API for tracking UTXOs, transactions, and balances. Key features include Miniscript support via Wallet Policies (BIP0388), PostgreSQL persistence, support for multiple derivation schemes (P2SH, P2PKH, P2WPKH, P2WSH, Taproot, and multi-signature), and the ability to operate on pruned nodes.

Tokens
5.6K
Snippets
19
Records
35
Agent score
62%

What's inside NBXplorer

  1. Overview of NBXplorer

    master

    NBXplorer is a minimalist UTXO tracker for HD wallets designed as an infrastructure tool for services. It supports various derivation schemes including P2SH, P2PKH, P2WPKH, P2WSH, Taproot, and multi-signature. It is designed to work on a pruned node and only indexes the UTXOs it is actively tracking.

    Key Features:

    • Miniscript support via Wallet Policies (BIP0388).
    • Persistence using PostgreSQL.
    • Support for multiple altcoins (e.g., BTC, LTC, DASH, DOGE, etc.).
    • Automatic reconnection to nodes.
    • REST API for interaction.
    • Pruning of transaction data to save space.
  2. Understand the NBXplorer SQL Schema structure

    master

    The NBXplorer database schema is designed to be multi-tenant, multi-asset, and multi-chain. It uses a denormalized model to optimize query performance, where denormalization is managed automatically via database triggers.

    The schema is organized into several logical groups:

    • Blocks and transactions: blks, blks_txs, and txs.
    • Outputs, Inputs, and Scripts: ins, outs, ins_outs, and scripts.
    • Descriptors: descriptors and descriptors_scripts.
    • Wallets: wallets, wallets_wallets, wallets_descriptors, and wallets_scripts.
    • Double spending detection: spent_outs.

    Developers can query the schema directly if the standard NBXplorer API does not meet their needs, or use it as a foundation for building custom indexers or APIs.

  3. Optimize Postgres migration speed

    master

    You can speed up the migration process by skipping certain data types if they are not required by your specific implementation:

    • Skip Events: Use --nomigrateevts (NBXPLORER_NOMIGRATEEVTS=1) if your services do not query past events. (Note: BTCPay Server does not utilize past events and can safely use this flag).
    • Skip Raw Transactions: Use --nomigraterawtxs (NBXPLORER_NOMIGRATERAWTXS=1) if preserving raw transaction bytes is nonessential. Warning: Raw transactions are typically required for signing with a non-segwit wallet.
  4. How wallet hierarchies and scripts work

    master

    In NBXplorer, a wallet is defined as a collection of wallets_descriptors and wallets_scripts.

    Key behaviors include:

    • Descriptor-to-Script mapping: If a descriptor is associated with a wallet, any entries in descriptors_scripts are automatically added to wallets_scripts via database triggers.
    • Direct Scripting: Scripts can be added directly to wallets_scripts.
    • Wallet Hierarchies: You can create parent-child relationships using the wallets_wallets table. When a child wallet is linked to a parent, any script belonging to the child wallet is automatically added to the parent wallet's scripts.
  5. Manage Groups of Tracked Sources

    master

    A Group is a tracked source that acts as a logical container for other tracked sources.

    • Composition: You can add/remove derivation schemes, other groups, or standalone addresses to a group.
    • Inheritance: Every address attached to a child tracked source is automatically included in the group, along with its UTXOs and transactions.
    • Nesting: Groups can be children of other groups.
    • Format: GROUP:groupid.

    Warning: Avoid adding an excessive number of children to a single group, as this can degrade the performance of the Get a group API call.

  6. Query NBXplorer data using SQL

    master
    While NBXplorer provides a standard API, you can also query its underlying data using SQL. This allows for more expressive and flexible data retrieval. The database schema is documented in the Postgres-Schema.md file.
  7. Understand Tracked Sources

    master

    NBXplorer does not index the entire blockchain. Instead, it monitors specific sets of addresses and their associated UTXOs, transactions, and balances. These sets are called Tracked Sources.

    There are three types of tracked sources:

    1. Derivation Schemes: Deterministic ways to generate addresses from a master public key (e.g., DERIVATIONSCHEME:xpub1).
    2. Groups: Logical groupings of multiple tracked sources (e.g., GROUP:groupid). Groups can contain other groups or standalone addresses.
    3. Standalone addresses: Monitoring a single specific address (e.g., ADDRESS:bc1...).
  8. Add support for a new altcoin

    master

    To extend NBXplorer to support a new altcoin, follow these steps:

    1. Add Altcoin Support to NBitcoin: First, you must add support for your altcoin to the NBitcoin.Altcoins library.
    2. Implement NBXplorer Network Provider: Once NBXplorer is updated to use the latest version of NBitcoin.Altcoins, implement a new network provider by following the pattern in NBXplorer.Client/NBXplorerNetworkProvider.Litecoin.cs.
    3. Configure Test Environment: To verify your implementation, modify NBXplorer.Tests/ServerTester.Environment.cs to match your altcoin's parameters.
    4. Run Tests: Execute the test suite to ensure everything is working correctly.
  9. Configure NBXplorer

    master

    NBXplorer can be configured using three methods:

    1. Command line arguments: e.g., --chains btc
    2. Environment variables: e.g., NBXPLORER_CHAINS=btc
    3. Configuration file: e.g., chains=btc

    Configuration file locations:

    • Windows: C:\Users\<user>\AppData\Roaming\NBXplorer\<network>\settings.config
    • Linux/macOS: ~/.nbxplorer/<network>/settings.config

    Important for developers using dotnet run: When running via dotnet run, you must pass settings after a -- separator to ensure they are passed to the application rather than the dotnet CLI. Otherwise, launch profiles meant for debugging might be used instead of your intended configuration.

    dotnet run --no-launch-profile --no-build -c Release -p .\NBXplorer\NBXplorer.csproj -- --chains btc
  10. Implement a custom indexer using the SQL schema

    master

    To build a custom indexer on top of the NBXplorer schema, follow this typical workflow for processing blocks:

    1. Insert Block: Insert the block into the blks table with confirmed='f'.
    2. Match Transactions: Call the fetch_matches function using all inputs (ins) and outputs (outs) of the block. This populates temporary tables matched_outs, matched_ins, and matched_conflicts for inspection.
    3. Persist Matches: Call save_matches to instruct the database to move the matched data into permanent tables.
    4. Finalize Block: Set confirmed='t' for the block in the blks table.

    Managing Descriptors: An indexer is also responsible for maintaining descriptors_scripts. You should monitor descriptors.gap; if the gap becomes too low, the indexer must insert the necessary scripts from the descriptor into the descriptors_scripts table.

  11. Use Policy Derivation Schemes with Miniscript

    master

    Policy Derivation Schemes allow for complex spending conditions using Miniscript (BIP0379) or Output Descriptors (BIP0380). This enables features like timelocks and complex multi-signature setups.

    Workflow:

    1. Design a Miniscript policy (e.g., using the Miniscript website).
    2. Convert the policy into a Miniscript output (e.g., and_v(or_c(...),...)).
    3. Wrap it in a script type if necessary (e.g., wsh(...) for P2WSH).
    4. Replace placeholders with xpubs in the format [fingerprint/path]xpub/**.

    Example Policy Structure: wsh(and_v(or_c(pk([973a74ba/48'/1'/0']xpub.../**),or_c(pk([39bad04c/48'/1'/1']xpub.../**),v:older(1000))),pk([f19e9416/48'/1'/2']xpub.../**)))

    API Integration Note: When passing a policy derivation scheme via a URL path in an API call, you must URL-encode special characters.

    Required Escaping:

    CharacterEscapeCharacterEscape
    [%5B(%28
    ]%5D)%29
    '%27:%3A
    /%2F*%2A
    ,%2C