{
  "version": 3,
  "sources": ["../src/bitcoin/codec.ts", "../src/bitcoin/constants.ts", "../src/bitcoin/abi/spvAdapter.ts", "../src/bitcoin/activation.ts", "../src/bitcoin/verifier.ts", "../src/bitcoin/actions.ts"],
  "sourcesContent": [
    "import {\n\ttype BufferCV,\n\ttype ClarityValue,\n\ttype ListCV,\n\ttype TupleCV,\n\ttype UIntCV,\n\tbufferCV,\n\tlistCV,\n\tuintCV,\n} from \"../clarity/index.ts\";\nimport { hexToBytes } from \"../utils/encoding.ts\";\nimport type { MerkleProof } from \"./merkle.ts\";\n\n/**\n * Number of merkle levels for a tree of `txCount` leaves — i.e. the exact\n * sibling count the SIP-044 `verify-merkle-proof` built-in expects\n * (`ceil(log2(tx-count))`). Computed by repeated halving to avoid float error\n * at exact powers of two.\n */\nfunction merkleDepth(txCount: number): number {\n\tlet depth = 0;\n\tlet n = txCount;\n\twhile (n > 1) {\n\t\tn = Math.ceil(n / 2);\n\t\tdepth++;\n\t}\n\treturn depth;\n}\n\n/** Max siblings the native `verify-merkle-proof` accepts: `(list 24 (buff 32))`. */\nconst MAX_SIBLINGS = 24;\n\nfunction assertHash32(bytes: Uint8Array, label: string): void {\n\tif (bytes.length !== 32) {\n\t\tthrow new Error(`${label} must be 32 bytes, got ${bytes.length}`);\n\t}\n}\n\n/**\n * Encode the argument vector for the SIP-044 native built-in\n * `(verify-merkle-proof leaf-hash root-hash tx-index tx-count sibling-hashes)`.\n *\n * Returns the five args in order: `[leaf, root, tx-index, tx-count, siblings]`.\n * All hashes are passed in *internal* (raw) byte order — the built-in does NOT\n * reverse, and neither does this. The leaf is the tx's `txidInternal`; the root\n * is the block header's merkle root in internal order.\n *\n * Throws on shapes the built-in would reject, so a caller fails locally with a\n * clear message instead of an opaque on-chain `false`.\n */\nexport function encodeMerkleProofArgs(params: {\n\tleaf: Uint8Array;\n\troot: Uint8Array;\n\tproof: MerkleProof;\n}): [BufferCV, BufferCV, UIntCV, UIntCV, ListCV] {\n\tconst { leaf, root, proof } = params;\n\tassertHash32(leaf, \"merkle leaf\");\n\tassertHash32(root, \"merkle root\");\n\n\tif (proof.txIndex < 0 || proof.txIndex >= proof.txCount) {\n\t\tthrow new Error(\n\t\t\t`tx-index ${proof.txIndex} out of range for tx-count ${proof.txCount}`,\n\t\t);\n\t}\n\tif (proof.siblings.length > MAX_SIBLINGS) {\n\t\tthrow new Error(\n\t\t\t`proof has ${proof.siblings.length} siblings, native verify-merkle-proof caps at ${MAX_SIBLINGS}`,\n\t\t);\n\t}\n\tconst expectedDepth = merkleDepth(proof.txCount);\n\tif (proof.siblings.length !== expectedDepth) {\n\t\tthrow new Error(\n\t\t\t`proof has ${proof.siblings.length} siblings but tx-count ${proof.txCount} requires exactly ${expectedDepth}`,\n\t\t);\n\t}\n\tfor (const [i, sibling] of proof.siblings.entries()) {\n\t\tassertHash32(sibling, `sibling ${i}`);\n\t}\n\n\treturn [\n\t\tbufferCV(leaf),\n\t\tbufferCV(root),\n\t\tuintCV(proof.txIndex),\n\t\tuintCV(proof.txCount),\n\t\tlistCV(proof.siblings.map((s) => bufferCV(s))),\n\t];\n}\n\nexport interface DecodedTxOutput {\n\t/** scriptPubKey bytes. */\n\tscript: Uint8Array;\n\t/** Output value in satoshis. */\n\tamount: bigint;\n\t/** The tx's txid in internal byte order — ready as a merkle leaf. */\n\ttxid: Uint8Array;\n}\n\nfunction expectBuffer(cv: ClarityValue | undefined, label: string): Uint8Array {\n\tif (cv?.type !== \"buffer\") {\n\t\tthrow new Error(`expected ${label} to be a buffer, got ${cv?.type}`);\n\t}\n\treturn hexToBytes(cv.value);\n}\n\n/**\n * Decode the tuple returned by `get-bitcoin-tx-output?`:\n * `(tuple (script (buff 1024)) (amount uint) (txid (buff 32)))`.\n *\n * Accepts either the bare tuple or a `(response ok ...)` wrapping it, so it\n * works whether or not the caller has already unwrapped the response.\n */\nexport function decodeTxOutput(cv: ClarityValue): DecodedTxOutput {\n\tconst tuple: ClarityValue = cv.type === \"ok\" ? cv.value : cv;\n\tif (tuple.type !== \"tuple\") {\n\t\tthrow new Error(`expected a tuple, got ${tuple.type}`);\n\t}\n\tconst fields = (tuple as TupleCV).value;\n\tconst amount = fields.amount;\n\tif (amount?.type !== \"uint\") {\n\t\tthrow new Error(\n\t\t\t`expected tuple field \"amount\" to be uint, got ${amount?.type}`,\n\t\t);\n\t}\n\treturn {\n\t\tscript: expectBuffer(fields.script, 'tuple field \"script\"'),\n\t\tamount: amount.value,\n\t\ttxid: expectBuffer(fields.txid, 'tuple field \"txid\"'),\n\t};\n}\n\nexport type OutputScriptType =\n\t| \"p2pkh\"\n\t| \"p2sh\"\n\t| \"p2wpkh\"\n\t| \"p2wsh\"\n\t| \"p2tr\"\n\t| \"op_return\"\n\t| \"unknown\";\n\nexport interface ParsedOutputScript {\n\ttype: OutputScriptType;\n\t/**\n\t * The hash / witness program for the recognized output types: the 20-byte\n\t * pubkey-hash (p2pkh), 20-byte script-hash (p2sh), or the 20/32-byte witness\n\t * program (p2wpkh/p2wsh/p2tr). Undefined for op_return / unknown.\n\t *\n\t * Address formatting is intentionally left to the caller — base58/bech32 HRPs\n\t * and version bytes are network-dependent, so it belongs where the network is\n\t * known (see `verifyBitcoinPayment`), not in this pure decoder.\n\t */\n\thash?: Uint8Array;\n\t/** OP_RETURN payload (the pushed data after the OP_RETURN opcode). */\n\tdata?: Uint8Array;\n}\n\nconst OP_DUP = 0x76;\nconst OP_HASH160 = 0xa9;\nconst OP_EQUALVERIFY = 0x88;\nconst OP_CHECKSIG = 0xac;\nconst OP_EQUAL = 0x87;\nconst OP_RETURN = 0x6a;\nconst OP_0 = 0x00;\nconst OP_1 = 0x51;\n\n/**\n * Classify a Bitcoin output scriptPubKey by its standard template and surface\n * the embedded hash / witness program. Recognizes P2PKH, P2SH, P2WPKH, P2WSH,\n * P2TR, and OP_RETURN; anything else is `unknown`.\n */\nexport function parseOutputScript(script: Uint8Array): ParsedOutputScript {\n\tconst n = script.length;\n\n\t// P2PKH: OP_DUP OP_HASH160 <20> ... OP_EQUALVERIFY OP_CHECKSIG\n\tif (\n\t\tn === 25 &&\n\t\tscript[0] === OP_DUP &&\n\t\tscript[1] === OP_HASH160 &&\n\t\tscript[2] === 0x14 &&\n\t\tscript[23] === OP_EQUALVERIFY &&\n\t\tscript[24] === OP_CHECKSIG\n\t) {\n\t\treturn { type: \"p2pkh\", hash: script.slice(3, 23) };\n\t}\n\n\t// P2SH: OP_HASH160 <20> ... OP_EQUAL\n\tif (\n\t\tn === 23 &&\n\t\tscript[0] === OP_HASH160 &&\n\t\tscript[1] === 0x14 &&\n\t\tscript[22] === OP_EQUAL\n\t) {\n\t\treturn { type: \"p2sh\", hash: script.slice(2, 22) };\n\t}\n\n\t// P2WPKH: OP_0 <20>\n\tif (n === 22 && script[0] === OP_0 && script[1] === 0x14) {\n\t\treturn { type: \"p2wpkh\", hash: script.slice(2, 22) };\n\t}\n\n\t// P2WSH: OP_0 <32>\n\tif (n === 34 && script[0] === OP_0 && script[1] === 0x20) {\n\t\treturn { type: \"p2wsh\", hash: script.slice(2, 34) };\n\t}\n\n\t// P2TR: OP_1 <32>\n\tif (n === 34 && script[0] === OP_1 && script[1] === 0x20) {\n\t\treturn { type: \"p2tr\", hash: script.slice(2, 34) };\n\t}\n\n\t// OP_RETURN: data carrier\n\tif (n >= 1 && script[0] === OP_RETURN) {\n\t\treturn { type: \"op_return\", data: script.slice(1) };\n\t}\n\n\treturn { type: \"unknown\" };\n}\n",
    "export type BitcoinNetwork = \"mainnet\" | \"testnet\" | \"regtest\";\n\nexport interface SpvAdapterRef {\n\t/** Deployer principal. */\n\taddress: string;\n\t/** Contract name. */\n\tname: string;\n}\n\n/**\n * Reference `spv-adapter` deployments (the read-only wrapper around the SIP-044\n * built-ins) — the single source of truth for the published adapter principal.\n * A network is listed only once its adapter is deployed AND verified against a\n * real Bitcoin header; `verifyBitcoinPayment` requires an explicit `contract`\n * for any network absent here (deploy recipe: `contracts/README.md`).\n *\n * `testnet` is absent because Stacks testnet has no Epoch 4.0 — the SIP-044\n * built-ins do not exist there, so the contract cannot be deployed.\n */\nexport const SPV_ADAPTER_CONTRACTS: Partial<\n\tRecord<BitcoinNetwork, SpvAdapterRef>\n> = {\n\tmainnet: {\n\t\taddress: \"SP2M1DE95TS0QBM4K893X6ST49FFJ53CCX9CYWNVY\",\n\t\tname: \"spv-adapter\",\n\t},\n};\n\n/** Resolve the reference adapter for a network, or `undefined` if none is deployed yet. */\nexport function getSpvAdapter(\n\tnetwork: BitcoinNetwork,\n): SpvAdapterRef | undefined {\n\treturn SPV_ADAPTER_CONTRACTS[network];\n}\n\n/** A `\"address.name\"` contract principal from an adapter ref. */\nexport function spvAdapterPrincipal(ref: SpvAdapterRef): string {\n\treturn `${ref.address}.${ref.name}`;\n}\n",
    "import type { AbiContract } from \"../../clarity/abi/index.ts\";\n\n/**\n * ABI for the reference `spv-adapter` contract — a thin, read-only wrapper that\n * exposes the SIP-044 Bitcoin built-ins (callable only from within a Clarity\n * contract) over read-only RPC. Plan 013 deploys a contract matching this shape;\n * `bitcoinVerifier` binds to it (or to an integrator's own contract with the\n * same surface).\n *\n * Hashes are internal (raw) byte order — the same as the built-ins. Do NOT\n * reverse before calling.\n */\nexport const SPV_ADAPTER_ABI = {\n\tfunctions: [\n\t\t{\n\t\t\t// (get-bitcoin-tx-output? tx vout)\n\t\t\tname: \"get-tx-output\",\n\t\t\taccess: \"read-only\",\n\t\t\targs: [\n\t\t\t\t{ name: \"tx\", type: { buff: { length: 4096 } } },\n\t\t\t\t{ name: \"vout\", type: \"uint128\" },\n\t\t\t],\n\t\t\toutputs: {\n\t\t\t\tresponse: {\n\t\t\t\t\tok: {\n\t\t\t\t\t\ttuple: [\n\t\t\t\t\t\t\t{ name: \"script\", type: { buff: { length: 1024 } } },\n\t\t\t\t\t\t\t{ name: \"amount\", type: \"uint128\" },\n\t\t\t\t\t\t\t{ name: \"txid\", type: { buff: { length: 32 } } },\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t\terror: \"uint128\",\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\t// (slice? header u36 u68) → optional 32-byte merkle root\n\t\t\tname: \"header-merkle-root\",\n\t\t\taccess: \"read-only\",\n\t\t\targs: [{ name: \"header\", type: { buff: { length: 80 } } }],\n\t\t\toutputs: { optional: { buff: { length: 32 } } },\n\t\t},\n\t\t{\n\t\t\t// (verify-merkle-proof leaf-hash root-hash tx-index tx-count sibling-hashes)\n\t\t\tname: \"verify-merkle\",\n\t\t\taccess: \"read-only\",\n\t\t\targs: [\n\t\t\t\t{ name: \"leaf\", type: { buff: { length: 32 } } },\n\t\t\t\t{ name: \"root\", type: { buff: { length: 32 } } },\n\t\t\t\t{ name: \"tx-index\", type: \"uint128\" },\n\t\t\t\t{ name: \"tx-count\", type: \"uint128\" },\n\t\t\t\t{\n\t\t\t\t\tname: \"siblings\",\n\t\t\t\t\ttype: { list: { type: { buff: { length: 32 } }, length: 24 } },\n\t\t\t\t},\n\t\t\t],\n\t\t\toutputs: \"bool\",\n\t\t},\n\t\t{\n\t\t\t// Composed SPV check: authenticate the caller-supplied header against\n\t\t\t// `(get-burn-block-info? header-hash height)`, extract its merkle root,\n\t\t\t// and `verify-merkle-proof` tx inclusion under it — atomically on-chain.\n\t\t\t// (ok true) = mined, (ok false) = header valid but tx not included,\n\t\t\t// (err ...) = header not authentic at that height.\n\t\t\tname: \"was-tx-mined\",\n\t\t\taccess: \"read-only\",\n\t\t\targs: [\n\t\t\t\t{ name: \"header\", type: { buff: { length: 80 } } },\n\t\t\t\t{ name: \"height\", type: \"uint128\" },\n\t\t\t\t{ name: \"leaf\", type: { buff: { length: 32 } } },\n\t\t\t\t{ name: \"tx-index\", type: \"uint128\" },\n\t\t\t\t{ name: \"tx-count\", type: \"uint128\" },\n\t\t\t\t{\n\t\t\t\t\tname: \"siblings\",\n\t\t\t\t\ttype: { list: { type: { buff: { length: 32 } }, length: 24 } },\n\t\t\t\t},\n\t\t\t],\n\t\t\toutputs: { response: { ok: \"bool\", error: \"uint128\" } },\n\t\t},\n\t],\n} as const satisfies AbiContract;\n",
    "import type { Client } from \"../clients/types.ts\";\nimport { EPOCH_4_ACTIVATION_BURN_HEIGHT_MAINNET } from \"../epochs.ts\";\nimport { MalformedResponseError } from \"../errors/response.ts\";\n\n/**\n * SIP-044 (the native Bitcoin SPV built-ins) activates as part of the **Stacks\n * Epoch 4.0 hard fork** — the same fork that ships `pox-5` (SIP-045), so both\n * share one height: {@link EPOCH_4_ACTIVATION_BURN_HEIGHT_MAINNET}.\n */\nexport interface Clarity6Gate {\n\t/**\n\t * Bitcoin burn block height at which Clarity 6 / Epoch 4.0 activates.\n\t * Optional on a mainnet client, which falls back to the known mainnet\n\t * height; required on every other network, where no fixed height exists.\n\t */\n\tactivationBurnHeight?: number;\n}\n\n/** Read the node's current Bitcoin burn block height from `/v2/info`. */\nexport async function getBurnBlockHeight(client: Client): Promise<number> {\n\tconst info = (await client.request(\"/v2/info\")) as {\n\t\tburn_block_height?: number;\n\t};\n\tif (typeof info?.burn_block_height !== \"number\") {\n\t\tthrow new MalformedResponseError(\n\t\t\t'getBurnBlockHeight: /v2/info response is missing \"burn_block_height\"',\n\t\t);\n\t}\n\treturn info.burn_block_height;\n}\n\n/**\n * Whether Clarity 6 (the native SPV built-ins) is active on the node behind\n * `client`. Compares the node's current burn height to the SIP-044 / Epoch 4.0\n * activation height: the known mainnet height when `client.chain` is mainnet,\n * otherwise the one you supply. `bitcoinVerifier` uses this to refuse calls\n * before activation rather than failing with an opaque contract error.\n */\nexport async function isClarity6Active(\n\tclient: Client,\n\tgate: Clarity6Gate = {},\n): Promise<boolean> {\n\tconst activation =\n\t\tgate.activationBurnHeight ??\n\t\t(client.chain?.network === \"mainnet\"\n\t\t\t? EPOCH_4_ACTIVATION_BURN_HEIGHT_MAINNET\n\t\t\t: undefined);\n\tif (activation == null) {\n\t\tthrow new Error(\n\t\t\t`Clarity 6 (SIP-044 / Epoch 4.0) has no fixed activation height on ${client.chain?.network ?? \"this network\"} — pass { activationBurnHeight }`,\n\t\t);\n\t}\n\tconst current = await getBurnBlockHeight(client);\n\treturn current >= activation;\n}\n",
    "import { readContract } from \"../actions/public/readContract.ts\";\nimport {\n\ttype ClarityValue,\n\tbufferCV,\n\tlistCV,\n\tuintCV,\n} from \"../clarity/index.ts\";\nimport type { Client } from \"../clients/types.ts\";\nimport {\n\ttype DecodedTxOutput,\n\tdecodeTxOutput,\n\tencodeMerkleProofArgs,\n} from \"./codec.ts\";\nimport type { MerkleProof } from \"./merkle.ts\";\nimport type { SpvProof } from \"./proof.ts\";\nimport { parseBlockHeader } from \"./serialize.ts\";\n\nexport interface BitcoinVerifierConfig {\n\t/** Adapter contract principal, `\"address.name\"` (the reference `spv-adapter` or an integrator's own). */\n\tcontract: string;\n\t/** Optional read-only call sender; defaults to the contract address. */\n\tsender?: string;\n}\n\nexport interface BitcoinVerifier {\n\t/**\n\t * Verify a merkle inclusion proof against a supplied root via the adapter's\n\t * `verify-merkle` (the native `verify-merkle-proof`). This proves membership\n\t * under `root`; authenticating that `root` belongs to a canonical Bitcoin\n\t * block is a separate step (header → height), composed in `verifyBitcoinPayment`.\n\t */\n\tverifyMerkleProof(input: {\n\t\tleaf: Uint8Array;\n\t\troot: Uint8Array;\n\t\tproof: MerkleProof;\n\t}): Promise<boolean>;\n\t/** Verify an `SpvProof`'s merkle inclusion against its own header root (membership only — not chain-authenticated). */\n\tverifySpvProof(proof: SpvProof): Promise<boolean>;\n\t/**\n\t * Full header-authenticated SPV check via the adapter's `was-tx-mined`: the\n\t * contract authenticates the proof's header against the chain\n\t * (`get-burn-block-info? header-hash`), extracts its root, and proves\n\t * inclusion — atomically. Returns `false` if the header isn't canonical at\n\t * its height or the tx isn't included. This is the real \"is it on Bitcoin\" check.\n\t */\n\twasTxMined(proof: SpvProof): Promise<boolean>;\n\t/**\n\t * Decode one output of a serialized Bitcoin tx via the adapter's\n\t * `get-tx-output` (the native `get-bitcoin-tx-output?`).\n\t */\n\tgetTxOutput(rawTx: Uint8Array, vout: number): Promise<DecodedTxOutput>;\n}\n\nfunction clarityToBool(cv: ClarityValue): boolean {\n\tconst value = cv.type === \"ok\" ? cv.value : cv;\n\tif (value.type === \"true\") return true;\n\tif (value.type === \"false\") return false;\n\tthrow new Error(`expected a boolean result, got ${value.type}`);\n}\n\n/**\n * Bind a `BitcoinVerifier` to a deployed adapter contract. There is a single,\n * native target — no caps and no legacy `clarity-bitcoin` path. The built-ins do\n * not exist until Clarity 6 / Epoch 4.0 activates, so read-only calls only\n * succeed on a node at that epoch (a local Clarity-6 devnet, or mainnet after\n * activation); guard with `isClarity6Active` when in doubt.\n */\nexport function bitcoinVerifier(\n\tclient: Client,\n\tconfig: BitcoinVerifierConfig,\n): BitcoinVerifier {\n\tconst { contract, sender } = config;\n\n\tasync function verifyMerkleProof(input: {\n\t\tleaf: Uint8Array;\n\t\troot: Uint8Array;\n\t\tproof: MerkleProof;\n\t}): Promise<boolean> {\n\t\tconst args = encodeMerkleProofArgs(input);\n\t\tconst result = await readContract(client, {\n\t\t\tcontract,\n\t\t\tfunctionName: \"verify-merkle\",\n\t\t\targs,\n\t\t\tsender,\n\t\t});\n\t\treturn clarityToBool(result);\n\t}\n\n\treturn {\n\t\tverifyMerkleProof,\n\t\tverifySpvProof(proof) {\n\t\t\tconst root = parseBlockHeader(proof.header).merkleRoot;\n\t\t\treturn verifyMerkleProof({\n\t\t\t\tleaf: proof.txidInternal,\n\t\t\t\troot,\n\t\t\t\tproof: proof.merkle,\n\t\t\t});\n\t\t},\n\t\tasync wasTxMined(proof) {\n\t\t\tconst result = await readContract(client, {\n\t\t\t\tcontract,\n\t\t\t\tfunctionName: \"was-tx-mined\",\n\t\t\t\targs: [\n\t\t\t\t\tbufferCV(proof.header),\n\t\t\t\t\tuintCV(proof.height),\n\t\t\t\t\tbufferCV(proof.txidInternal),\n\t\t\t\t\tuintCV(proof.merkle.txIndex),\n\t\t\t\t\tuintCV(proof.merkle.txCount),\n\t\t\t\t\tlistCV(proof.merkle.siblings.map((s) => bufferCV(s))),\n\t\t\t\t],\n\t\t\t\tsender,\n\t\t\t});\n\t\t\t// (response bool uint): (ok true) mined, (ok false) not, (err) bad header → fail closed.\n\t\t\tif (result.type === \"err\") return false;\n\t\t\treturn clarityToBool(result);\n\t\t},\n\t\tasync getTxOutput(rawTx, vout) {\n\t\t\tconst result = await readContract(client, {\n\t\t\t\tcontract,\n\t\t\t\tfunctionName: \"get-tx-output\",\n\t\t\t\targs: [bufferCV(rawTx), uintCV(vout)],\n\t\t\t\tsender,\n\t\t\t});\n\t\t\tif (result.type === \"err\") {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`get-tx-output failed on-chain: ${JSON.stringify(result.value)}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn decodeTxOutput(result);\n\t\t},\n\t};\n}\n",
    "import type { Client } from \"../clients/types.ts\";\nimport { formatBitcoinAddress } from \"./address.ts\";\nimport { type OutputScriptType, parseOutputScript } from \"./codec.ts\";\nimport {\n\ttype BitcoinNetwork,\n\tgetSpvAdapter,\n\tspvAdapterPrincipal,\n} from \"./constants.ts\";\nimport { type ProofSource, type SpvProof, buildTxProof } from \"./proof.ts\";\nimport { parseBitcoinTx } from \"./serialize.ts\";\nimport { bitcoinVerifier } from \"./verifier.ts\";\n\nexport interface BitcoinPaymentOutput {\n\tvout: number;\n\t/** scriptPubKey bytes. */\n\tscript: Uint8Array;\n\t/** Value in satoshis. */\n\tamount: bigint;\n\ttype: OutputScriptType;\n\t/** Address for the configured network, if the script has a standard one. */\n\taddress?: string;\n}\n\nexport interface VerifyBitcoinPaymentResult {\n\t/** `mined` AND every supplied `expect` constraint holds. */\n\tverified: boolean;\n\t/**\n\t * On-chain proof that the tx is committed in a Bitcoin block. With\n\t * `authenticateHeader` (default), this is the adapter's `was-tx-mined` —\n\t * header authenticated against the chain AND merkle inclusion. With it off,\n\t * it is merkle inclusion against the proof's own (unauthenticated) header.\n\t */\n\tmined: boolean;\n\t/** The decoded output at `vout`. */\n\toutput: BitcoinPaymentOutput;\n\t/** The proof used (built or supplied). */\n\tproof: SpvProof;\n}\n\nexport type VerifyBitcoinPaymentParams = (\n\t| { proof: SpvProof }\n\t| { txid: string; source: ProofSource }\n) & {\n\t/**\n\t * Adapter contract principal, `\"address.name\"`. Optional on networks with a\n\t * published reference adapter (see `SPV_ADAPTER_CONTRACTS` — mainnet only);\n\t * required everywhere else.\n\t */\n\tcontract?: string;\n\t/** Output index to decode and assert against. */\n\tvout: number;\n\t/** Network for address formatting. Defaults to mainnet. */\n\tnetwork?: BitcoinNetwork;\n\t/** Optional expectations; each supplied field must match for `verified`. */\n\texpect?: { address?: string; amount?: bigint };\n\t/**\n\t * Confirm the proof's header is the canonical block at its height via\n\t * `get-header-merkle-root` (default true). Turn off only when the caller has\n\t * already authenticated the header.\n\t */\n\tauthenticateHeader?: boolean;\n\tsender?: string;\n};\n\n/**\n * Verify that a Bitcoin payment is committed on-chain and (optionally) matches\n * an expected recipient/amount. Composes the whole SPV flow:\n *  1. build the proof from a `ProofSource` (or accept a prepared `SpvProof`),\n *  2. prove the tx is mined — `was-tx-mined` (header authenticated against the\n *     chain + merkle inclusion) by default, or membership-only when\n *     `authenticateHeader` is off,\n *  3. decode the target output and assert any `expect` constraints.\n *\n * The output is decoded off-chain from the proof's raw tx, which is sound: the\n * raw tx is pinned to the proven txid (`buildTxProof` checks it hashes to the\n * leaf), so its bytes are committed.\n */\nexport async function verifyBitcoinPayment(\n\tclient: Client,\n\tparams: VerifyBitcoinPaymentParams,\n): Promise<VerifyBitcoinPaymentResult> {\n\tconst {\n\t\tcontract,\n\t\tvout,\n\t\tnetwork = \"mainnet\",\n\t\texpect,\n\t\tauthenticateHeader = true,\n\t\tsender,\n\t} = params;\n\n\tconst adapter = getSpvAdapter(network);\n\tconst resolvedContract =\n\t\tcontract ?? (adapter ? spvAdapterPrincipal(adapter) : undefined);\n\tif (!resolvedContract) {\n\t\tthrow new Error(\n\t\t\t`No spv-adapter deployed for ${network} — pass an explicit \\`contract\\`. Only mainnet has a published reference adapter; the SIP-044 built-ins require Clarity 6 / Epoch 4.0 (deploy recipe: contracts/README.md).`,\n\t\t);\n\t}\n\n\tconst proof =\n\t\t\"proof\" in params\n\t\t\t? params.proof\n\t\t\t: await buildTxProof(params.source, { txid: params.txid, vout });\n\n\tconst verifier = bitcoinVerifier(client, {\n\t\tcontract: resolvedContract,\n\t\tsender,\n\t});\n\tconst mined = authenticateHeader\n\t\t? await verifier.wasTxMined(proof)\n\t\t: await verifier.verifySpvProof(proof);\n\n\tconst parsedTx = parseBitcoinTx(proof.rawTx);\n\tconst out = parsedTx.outputs[vout];\n\tif (!out) {\n\t\tthrow new Error(\n\t\t\t`vout ${vout} out of range (tx has ${parsedTx.outputs.length} outputs)`,\n\t\t);\n\t}\n\tconst parsedScript = parseOutputScript(out.scriptPubKey);\n\tconst address = formatBitcoinAddress(parsedScript, network);\n\tconst output: BitcoinPaymentOutput = {\n\t\tvout,\n\t\tscript: out.scriptPubKey,\n\t\tamount: out.value,\n\t\ttype: parsedScript.type,\n\t\taddress,\n\t};\n\n\tconst amountOk = expect?.amount === undefined || out.value === expect.amount;\n\tconst addressOk = expect?.address === undefined || address === expect.address;\n\tconst verified = mined && amountOk && addressOk;\n\n\treturn { verified, mined, output, proof };\n}\n"
  ],
  "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmBA,SAAS,WAAW,CAAC,SAAyB;AAAA,EAC7C,IAAI,QAAQ;AAAA,EACZ,IAAI,IAAI;AAAA,EACR,OAAO,IAAI,GAAG;AAAA,IACb,IAAI,KAAK,KAAK,IAAI,CAAC;AAAA,IACnB;AAAA,EACD;AAAA,EACA,OAAO;AAAA;AAIR,IAAM,eAAe;AAErB,SAAS,YAAY,CAAC,OAAmB,OAAqB;AAAA,EAC7D,IAAI,MAAM,WAAW,IAAI;AAAA,IACxB,MAAM,IAAI,MAAM,GAAG,+BAA+B,MAAM,QAAQ;AAAA,EACjE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