/** * Vault Balance Consensus — multi-provider cross-checks for the trusted balance * * Extracted from `vault-balance.module.ts` (review 2026-07-15) so the balance * module owns only the contract-state / authorized-spend / filter orchestration * while this class owns everything provider-facing: * - provider expansion (one data reader per registered provider), * - confirmed-UTXO-set consensus (each confirmed UTXO must match, by value, * on every provider; tip skew demotes, forgery / depth-lie throws), * - self-change proofing (unconfirmed change admitted only when every * provider agrees the parent tx spends confirmed vault-owned outputs and * corroborates the claimed output), * - the fan-out / concurrency / timeout policy those checks depend on. * * Bundling note: this file is imported ONLY by `vault-balance.module.ts` (hence * only by the six vault-balance consumer actions). It deliberately does NOT * touch `bitcoin-data-provider.module.ts`, whose other importers — including * `btc-transaction-signer`, pinned by an in-flight deploy ceremony — must not * rebundle. See docs/vault-balance-confirmed-gate-plan-2026-07-14.md §3.1/§3.2. */ import { SelfChangeProviderConfig, UTXO } from "../interfaces/chunks/vault-balance.i"; import { IBitcoinDataProvider } from "../interfaces/chunks/bitcoin-data-provider.i"; /** * Multi-provider consensus over a vault's UTXO set. * * Constructed per balance calculation from the primary data provider (the one * the primary UTXO set came from — index 0, reused verbatim to preserve test * doubles) plus the full registered provider list. All secondary readers are * built once, up front, and shared by both consensus passes. */ export declare class VaultBalanceConsensus { private readonly primaryProvider; private readonly providerConfigs; private readonly minConsensusProviders; private readonly readers; /** * @param minConsensusProviders Policy floor (`policy.minConsensusProviders`, * remediation P1/#1). Both consensus passes THROW when fewer providers are * configured than the floor demands — below it the cross-checks cannot run * and neither trusting the primary (inflation) nor excluding UTXOs * (deflation → wrongful liquidation) is a safe direction. A floor of 1 * (hardhat / single-provider sepolia) preserves the trust-primary / * exclude-unconfirmed baseline unchanged. */ constructor(primaryProvider: IBitcoinDataProvider, providerConfigs: SelfChangeProviderConfig[] | undefined, minConsensusProviders: number); /** * Fail-closed floor check shared by both consensus passes. Only floors above * 1 can bite: a floor of 1 with zero configs means "no esplora access * configured" (dev/test), where the documented pre-gate behaviour — * trust-primary confirmed set, exclude unconfirmed — remains correct. */ private assertProviderFloor; /** * Build one data reader per configured provider (primary first). Index 0 * reuses the injected primary instance; the rest are constructed from their * configs so cross-check reads hit the right endpoint. Null when no providers * are configured — both passes then fall back to primary-only behaviour. */ private buildReaders; /** * Confirmed-UTXO-set consensus across every registered provider (strict). * * The signed balance is dominated by the primary's confirmed UTXOs, but the * UTXO set — txid, vout, and value — is read only from the primary; the * `assertBitcoinBalanceConsensus` check the validators run compares * `chain_stats` totals, not individual outpoints. So a compromised primary * can add a fabricated confirmed UTXO, or inflate a real one's value, and the * balance consensus still passes (it serves honest `chain_stats`). This * closes that: each UTXO the primary reports at or above `minConfirmations` * must be present with the EXACT same value on every registered provider. * * Semantics: * - Absent on any provider, or value mismatch → THROW (fail-closed). Confirmed * transactions are fully propagated, so absence is unambiguous forgery, not * a race. * - Present + value-matched everywhere: the UTXO counts as confirmed only if * it meets `minConfirmations` on ALL providers (gated on the minimum * reported depth). A UTXO just past the boundary on the primary but a block * behind on a lagging provider is DEMOTED — excluded until it matures — * rather than signed over. A depth spread beyond CONFIRMATION_SKEW_TOLERANCE * is not benign skew but a provider lying about depth (to inflate a shallow * UTXO past the gate, or deflate a deep one into a wrongful liquidation) → * THROW. * - SYMMETRIC (finding #6): a confirmed outpoint any secondary reports that * the primary omits entirely, reports with a different value, or reports * at a depth more than the skew tolerance BELOW the secondary's → THROW. * Without this, a compromised primary deflates the balance by omission and * forces a wrongful liquidation. * * Below `minConsensusProviders` this THROWS (P1/#1). With a floor of 1 and * 0/1 providers configured, no cross-check is possible and the primary's * confirmed UTXOs are returned unchanged (behaviour identical to before * this gate). * * Cross-checking `chain_stats` totals against the confirmed-UTXO sum was * considered and rejected: `getBalance` reads mempool-blind `chain_stats` * while `getUTXOSet` removes mempool-spent outputs, so the two legitimately * diverge during any pending spend — precisely the self-change window — and * that comparison would false-fail there. Outpoint-set consensus asserts * agreement only on confirmed outpoints, which the mempool does not touch. * * @returns Set of "txid:vout" keys admitted as confirmed-with-consensus. */ confirmedUtxoConsensus(primaryUtxos: UTXO[], vaultAddress: string, minConfirmations: number): Promise>; /** * Classify unconfirmed UTXOs as protocol self-change. * * A UTXO below minConfirmations counts as confirmed-equivalent ONLY when the * transaction that created it spends exclusively vault-owned, confirmed * outputs. Such a transaction can only have been signed by the vault's PKP, * so no third party can RBF-replace or double-spend it — the reversal risk * the confirmation gate exists to close does not apply. Any non-vault input, * unconfirmed funding tx, or coinbase input disqualifies the UTXO * (fail-safe: exclusion, never inclusion). * * Multi-provider consensus (audit C-1/C-2 parity): the parent-tx proof is * verified independently against EVERY configured provider and admitted * only on unanimity. The confirmed-balance consensus the validators run * covers `chain_stats` only, so without this the primary provider alone * could fabricate an unconfirmed UTXO plus a self-consistent parent proof * and inflate the signed balance. Any verdict disagreement THROWS * (fail-closed) — exclusion on a suppressed proof would deflate the balance * and open a wrongful-liquidation window, so neither direction may proceed * without agreement. A provider that cannot state the chain tip — and only * that failure — is dropped from the quorum rather than aborting the read (it * never counts as agreeing), and falling below `minConsensusProviders` * survivors throws — see `verifyParentIsSelfSpendConsensus`. * * The same per-provider `/tx` fetch also returns the parent's outputs, and * every provider must corroborate each claimed UTXO (index, vault address, * exact value) before admission — the UTXO list itself comes only from the * primary, so an unchecked claim would let it attach a fabricated or * inflated output to a genuine self-spend. * * Fetch cost: zero when every UTXO is confirmed (the common case); otherwise * per provider: one tx lookup per classified parent (vins + vouts in one * response), plus per distinct funding tx a confirmation check and — only * when the provider omits vin prevout data — an output lookup. Classified * parents are capped at MAX_CLASSIFIED_PARENTS (largest claimed value * first, deterministic) and evaluated CLASSIFICATION_CONCURRENCY at a time, * so third-party dust spam cannot amplify into provider rate-limiting and * an action-wide outage. * * See docs/vault-balance-confirmed-gate-plan-2026-07-14.md §3.1. * * @param utxos - Full UTXO set for the vault address * @param vaultAddress - Bitcoin address of the vault (may carry network prefix) * @param minConfirmations - Confirmation threshold for the chain * @returns Set of "txid:vout" keys for UTXOs verified as self-change */ classifySelfChange(utxos: UTXO[], vaultAddress: string, minConfirmations: number): Promise>; /** * Run the self-spend proof against every configured provider and require * unanimity AMONG THE PROVIDERS THAT ANSWERED. Fail-closed on disagreement: * a verdict mismatch throws and aborts the balance calculation — see * classifySelfChange for why exclusion is not a safe fallback here. * * Quorum semantics (2026-09-23, plan C2). ONE failure class is tolerated: a * provider that could not state the chain tip (`BitcoinTipUnreadableError`). * It is ABSENT from the quorum — never counted as agreeing, never allowed to * decide the verdict — and the policy floor, not "at least one", decides when * that absence is fatal: below `minConsensusProviders` surviving readings * there is no cross-check left to trust, and the read fails loudly naming * every provider that dropped out. * * Why only that class. `getTransaction` now throws on an unreadable tip * instead of fabricating `confirmations: 1`; without this carve-out one flaky * `/blocks/tip/height` endpoint would take every balance and spend path down * with it. Every OTHER failure remains fatal, as before: a provider that * answers "I have never seen that transaction" is making a claim about the * chain rather than suffering an outage, and that shape is precisely the * fabricated-UTXO signature the fail-closed rule exists to catch. * * When the parent IS a unanimous self-spend, every provider's view of the * parent's OUTPUTS must additionally corroborate the primary's claimed * UTXOs (index exists, pays the vault, exact value). Without this, a * compromised primary could attach a fabricated or value-inflated output * to a genuine self-spend the honest providers happily verify. * * @returns True when every provider verified the parent and corroborated * the claimed outputs, false when every provider rejected the * parent (genuine third-party deposit) */ private verifyParentIsSelfSpendConsensus; /** * Single provider's evaluation of a parent tx: the self-spend proof plus — * when the proof holds — corroboration of the claimed outputs against this * provider's view of the parent's vout set. The output check is skipped for * non-self-spend parents (their UTXOs are excluded regardless, and a * third-party deposit's values are not ours to police). */ private evaluateParentForProvider; /** * Verify that a transaction's inputs spend exclusively vault-owned, * confirmed outputs, using a single provider's view of the chain. * * @param inputs - Parsed inputs of the parent transaction * @param cleanVaultAddress - Vault address with network prefix stripped * @param minConfirmations - Required depth for every funding transaction * @param reader - Data provider for funding-tx lookups (same provider the inputs came from) * @returns True only when every input is a confirmed, vault-owned outpoint */ private verifyInputsAreSelfSpend; /** * Fetch a parent transaction's inputs (vins) and outputs (vouts) via Esplora. * * A module-private fetch (not a `BitcoinDataProvider` method) so editing this * consensus logic does not rebundle the shared provider module's other * importers — see this file's header for the ripple-set constraint. * * @param txid - Transaction ID * @param providerConfig - Esplora endpoint to query (one of the configured * consensus providers) * @returns Parsed inputs + outputs, or null ONLY when the tx is provably not * a self-spend candidate (coinbase-funded). Provider inability — * 404, malformed vin rows, missing vin array — THROWS (review * 2026-07-15 P1): the parent of a UTXO the primary lists must * exist, so "can't fetch it" is a provider failure, and treating it * as a false self-change vote would silently deflate collateral * (wrongful-liquidation direction) if every provider failed the * same way. */ private fetchParentTransaction; } //# sourceMappingURL=vault-balance-consensus.module.d.ts.map