/** * Bitcoin Data Provider Module * * Provides Bitcoin network data with support for future consensus mechanisms. * Current implementation: Single provider with fallback support * Future implementation: 2-of-3 provider consensus * * This module centralizes all Bitcoin data queries to maintain a consistent * interface that won't break when upgrading to multi-provider consensus. */ import { BitcoinDataProviderConfig, UTXO, UTXOSet } from "../interfaces/chunks/vault-balance.i"; import { IBitcoinDataProvider } from "../interfaces/chunks/bitcoin-data-provider.i"; /** * One provider could not state the chain tip, so no confirmation depth exists * to report. Distinct from every other provider error on purpose: * * - `getTransaction`'s own boundary translates a message containing "404" into * `null` ("transaction not found"). The tip endpoint is a DIFFERENT resource * on the same provider, so a 404 there must not be read as a missing * transaction — that `null` is the fabricated-depth bug in another costume. * - The consensus layer treats THIS class, and only this class, as provider * ABSENCE (quorum reduced, floor still enforced). Every other failure — * notably a provider that answers "I have never seen that transaction" — * stays fatal, because it is a claim about the chain, not an outage, and it * is the signature of a fabricated UTXO (review 2026-07-15 P1). * * Identified by a structural marker rather than `instanceof`: the bundler and * downlevel transpilation can break the prototype chain across the action * bundle, and a mis-identified error here fails in the dangerous direction. */ export declare class BitcoinTipUnreadableError extends Error { readonly isBitcoinTipUnreadable: true; constructor(message: string); } /** True when `error` is a {@link BitcoinTipUnreadableError} (bundle-safe check). */ export declare function isBitcoinTipUnreadableError(error: unknown): boolean; /** * Bitcoin Data Provider * * Fetches UTXO data from Bitcoin network. * Interface designed to remain stable when adding multi-provider consensus. */ export declare class BitcoinDataProvider implements IBitcoinDataProvider { private config; private timeout; private logger; constructor(config: BitcoinDataProviderConfig); /** * Merge per-provider auth headers into a base header set. No-op for public * Esplora / regtest faucet (no `auth`); attaches the `api-key` header for * Maestro and performs a (memoized) OAuth exchange → `Bearer` for Blockstream * Enterprise. Awaited at every fetch site so a single run shares one token. */ private withAuth; /** * Get current Bitcoin block height from provider */ private getCurrentBlockHeight; /** * Fetch all UTXOs for a Bitcoin address * * @param address - Bitcoin address (legacy, segwit, or taproot) * @returns Array of UTXOs */ getUTXOs(address: string): Promise; /** * Strip network prefix from Bitcoin address * * Addresses may be stored with network prefix (e.g., "REGTEST_address") * but APIs expect clean addresses without prefix. * * @param address - Address with optional prefix * @returns Clean address without prefix */ private stripNetworkPrefix; /** * Fetch UTXOs using Bitcoin RPC (for regtest/testing) */ private fetchUTXOsFromRPC; /** * Parse Esplora/Blockstream/Mempool.space UTXO response * * Supports multiple formats: * - Esplora format: {txid, vout, value, status: {confirmed, block_height}} * - Blockstream format: {txid, vout, value, confirmations} * * @param data Raw response from provider API * @param currentBlockHeight Current block height (for calculating confirmations from block_height) * @returns Array of normalized UTXOs */ private parseBlockstreamUTXOs; /** * Internal method to fetch UTXOs from a specific provider * * Supports standard Esplora API format: * - Standard format: {providerUrl}/address/{address}/utxo * - providerUrl must include full base path (e.g., "http://localhost:3002/api/esplora") */ private fetchUTXOsFromProvider; /** * Get complete UTXO set with balance summary * * This is the SINGLE method that vault-balance module should call. * Returns everything needed to know about Bitcoin network state for an address. * * @param address - Bitcoin address * @param minConfirmations - Minimum confirmations for "confirmed" status (REQUIRED - caller must specify) * @returns Complete UTXO set with balance breakdown */ getUTXOSet(address: string, minConfirmations: number): Promise; /** * Get balance for a Bitcoin address (fast, no UTXO details) * * WARNING: This method does NOT validate individual UTXOs. * Use ONLY for non-critical balance display, never for authorization validation. * For authorization/security: ALWAYS use getUTXOSet() with full UTXO validation. * * Uses Esplora /address/:address endpoint to get confirmed balance from chain_stats. * * @param address - Bitcoin address * @returns Balance in satoshis (confirmed balance only) * @throws Error if the request fails or response is invalid */ getBalance(address: string): Promise; /** * Get transaction with confirmation count * * Checks if transaction exists on Bitcoin network and returns confirmation count. * Used to validate authorized spends have been broadcasted. * * @param txid - Transaction ID to check * @returns Transaction with confirmation count, or null if not found */ getTransaction(txid: string): Promise<{ txid: string; confirmations: number; } | null>; /** * Current chain tip height from THIS provider — or throw. * * Every exit is a throw naming the provider and the precise failure, because * the only alternative is inventing a confirmation depth (see the call site). * The single `try` exists solely to attach that name to a transport-level * rejection (DNS, TLS, connection reset, abort); it re-throws the cause * verbatim inside the message and never substitutes a value — authorized for * this case by the Batch 2 plan, §C2. * * @param signal - The caller's AbortSignal, so this read shares the same * timeout budget as the transaction fetch it serves. */ private fetchTipHeight; /** * Get outputs (vouts) of a Bitcoin transaction * * Fetches the full transaction from the Esplora API and returns all outputs * with their index, scriptPubKey, address, and value. * * Used to canonicalize UTXO vout index when the Esplora UTXO endpoint * returns a wrong vout (a known faucet API bug in regtest environments). * * @param txid - Transaction ID * @returns Array of transaction outputs with index, or null if not found */ getTransactionOutputs(txid: string): Promise | null>; } /** * Factory function for creating Bitcoin Data Provider * Useful for dependency injection and testing */ export declare function createBitcoinDataProvider(config: BitcoinDataProviderConfig): BitcoinDataProvider; //# sourceMappingURL=bitcoin-data-provider.module.d.ts.map