@secondlayer/stacks 6.2.2 → 6.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/shared/{stacks-esm-nt5hf8ht.js → stacks-esm-z0rq3ejt.js} +44 -21
- package/dist/shared/stacks-esm-z0rq3ejt.js.map +12 -0
- package/dist/src/bitcoin/index.cjs +43 -19
- package/dist/src/bitcoin/index.cjs.map +3 -3
- package/dist/src/bitcoin/index.d.cts +13 -1
- package/dist/src/bitcoin/index.d.ts +13 -1
- package/dist/src/bitcoin/index.js +4 -2
- package/dist/src/bitcoin/index.js.map +2 -2
- package/dist/src/pox5/index.cjs +42 -19
- package/dist/src/pox5/index.cjs.map +3 -3
- package/dist/src/pox5/index.js +1 -1
- package/package.json +1 -1
- package/dist/shared/stacks-esm-nt5hf8ht.js.map +0 -12
|
@@ -73,6 +73,14 @@ function rootFromProof2(leafInternal, proof) {
|
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
// src/bitcoin/proof.ts
|
|
76
|
+
class BitcoinSourceError2 extends Error {
|
|
77
|
+
code;
|
|
78
|
+
name = "BitcoinSourceError";
|
|
79
|
+
constructor(code, message) {
|
|
80
|
+
super(message);
|
|
81
|
+
this.code = code;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
76
84
|
function normalizeTxid(txid) {
|
|
77
85
|
return without0x2(txid).toLowerCase();
|
|
78
86
|
}
|
|
@@ -149,22 +157,32 @@ function bitcoinRpcSource2(config) {
|
|
|
149
157
|
const basic = typeof config.auth === "string" ? config.auth : btoa(`${config.auth.username}:${config.auth.password}`);
|
|
150
158
|
headers.authorization = `Basic ${basic}`;
|
|
151
159
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
160
|
+
let res;
|
|
161
|
+
try {
|
|
162
|
+
res = await doFetch(config.url, {
|
|
163
|
+
method: "POST",
|
|
164
|
+
headers,
|
|
165
|
+
body: JSON.stringify({
|
|
166
|
+
jsonrpc: "1.0",
|
|
167
|
+
id: "secondlayer",
|
|
168
|
+
method,
|
|
169
|
+
params: rpcParams
|
|
170
|
+
})
|
|
171
|
+
});
|
|
172
|
+
} catch (error) {
|
|
173
|
+
throw new BitcoinSourceError2("unavailable", `bitcoin rpc ${method} failed: ${error instanceof Error ? error.message : "network error"}`);
|
|
174
|
+
}
|
|
175
|
+
let json;
|
|
176
|
+
try {
|
|
177
|
+
json = await res.json();
|
|
178
|
+
} catch {
|
|
179
|
+
throw new BitcoinSourceError2("unavailable", `bitcoin rpc ${method} failed: HTTP ${res.status}`);
|
|
164
180
|
}
|
|
165
|
-
const json = await res.json();
|
|
166
181
|
if (json.error) {
|
|
167
|
-
throw new
|
|
182
|
+
throw new BitcoinSourceError2(json.error.code === -5 ? "not-found" : "unavailable", `bitcoin rpc ${method} error: ${json.error.message}`);
|
|
183
|
+
}
|
|
184
|
+
if (!res.ok) {
|
|
185
|
+
throw new BitcoinSourceError2("unavailable", `bitcoin rpc ${method} failed: HTTP ${res.status}`);
|
|
168
186
|
}
|
|
169
187
|
return json.result;
|
|
170
188
|
}
|
|
@@ -178,7 +196,7 @@ function bitcoinRpcSource2(config) {
|
|
|
178
196
|
true
|
|
179
197
|
]);
|
|
180
198
|
if (!tx.blockhash) {
|
|
181
|
-
throw new
|
|
199
|
+
throw new BitcoinSourceError2("unconfirmed", `tx ${txid} is not in a block (node needs -txindex, or the tx is unconfirmed)`);
|
|
182
200
|
}
|
|
183
201
|
const block = await rpc("getblock", [
|
|
184
202
|
tx.blockhash,
|
|
@@ -199,9 +217,14 @@ function esploraSource2(config) {
|
|
|
199
217
|
const doFetch = config.fetch ?? fetch;
|
|
200
218
|
const base = config.url.replace(/\/+$/, "");
|
|
201
219
|
async function get(path) {
|
|
202
|
-
|
|
220
|
+
let res;
|
|
221
|
+
try {
|
|
222
|
+
res = await doFetch(`${base}${path}`);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
throw new BitcoinSourceError2("unavailable", `esplora GET ${path} failed: ${error instanceof Error ? error.message : "network error"}`);
|
|
225
|
+
}
|
|
203
226
|
if (!res.ok) {
|
|
204
|
-
throw new
|
|
227
|
+
throw new BitcoinSourceError2(res.status === 404 ? "not-found" : "unavailable", `esplora GET ${path} failed: HTTP ${res.status}`);
|
|
205
228
|
}
|
|
206
229
|
return res;
|
|
207
230
|
}
|
|
@@ -212,7 +235,7 @@ function esploraSource2(config) {
|
|
|
212
235
|
async getBlockForTx(txid) {
|
|
213
236
|
const status = await (await get(`/tx/${txid}`)).json();
|
|
214
237
|
if (!status.status.confirmed) {
|
|
215
|
-
throw new
|
|
238
|
+
throw new BitcoinSourceError2("unconfirmed", `tx ${txid} is unconfirmed`);
|
|
216
239
|
}
|
|
217
240
|
const blockHashHex = status.status.block_hash;
|
|
218
241
|
const header = (await (await get(`/block/${blockHashHex}/header`)).text()).trim();
|
|
@@ -231,7 +254,7 @@ function esploraSource2(config) {
|
|
|
231
254
|
// src/epochs.ts
|
|
232
255
|
var EPOCH_4_ACTIVATION_BURN_HEIGHT_MAINNET2 = 960230;
|
|
233
256
|
|
|
234
|
-
export { merkleRoot2, buildMerkleProof2, rootFromProof2, buildTxProof2, fallbackProofSource2, bitcoinRpcSource2, esploraSource2, EPOCH_4_ACTIVATION_BURN_HEIGHT_MAINNET2 };
|
|
257
|
+
export { merkleRoot2, buildMerkleProof2, rootFromProof2, BitcoinSourceError2, buildTxProof2, fallbackProofSource2, bitcoinRpcSource2, esploraSource2, EPOCH_4_ACTIVATION_BURN_HEIGHT_MAINNET2 };
|
|
235
258
|
|
|
236
|
-
//# debugId=
|
|
237
|
-
//# sourceMappingURL=stacks-esm-
|
|
259
|
+
//# debugId=4CD23767D82ECDDE64756E2164756E21
|
|
260
|
+
//# sourceMappingURL=stacks-esm-z0rq3ejt.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/bitcoin/merkle.ts", "../src/bitcoin/proof.ts", "../src/epochs.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import { concatBytes } from \"../utils/encoding.ts\";\nimport { doubleSha256 } from \"./serialize.ts\";\n\n/**\n * A Bitcoin merkle inclusion proof, shaped for the SIP-044 `verify-merkle-proof`\n * built-in: `(leaf-hash, root-hash, tx-index, tx-count, sibling-hashes)`.\n *\n * - `siblings` are the sibling node hashes from the leaf up to (excluding) the\n * root, in *internal* byte order — never reversed.\n * - `txCount` (not tree-depth) pins the canonical tree shape; the built-in\n * rejects any proof whose length differs from `ceil(log2(tx-count))`.\n */\nexport interface MerkleProof {\n\tsiblings: Uint8Array[];\n\ttxIndex: number;\n\ttxCount: number;\n}\n\n/** Combine an ordered pair of nodes into their parent hash. */\nfunction hashPair(left: Uint8Array, right: Uint8Array): Uint8Array {\n\treturn doubleSha256(concatBytes(left, right));\n}\n\n/** Collapse one merkle level into the next, duplicating the last node if odd. */\nfunction nextLevel(level: Uint8Array[]): Uint8Array[] {\n\tconst next: Uint8Array[] = [];\n\tfor (let i = 0; i < level.length; i += 2) {\n\t\tconst left = level[i];\n\t\tif (left === undefined) throw new Error(\"merkle: missing left node\");\n\t\t// Bitcoin duplicates the final node when a level has an odd count.\n\t\tconst right = level[i + 1] ?? left;\n\t\tnext.push(hashPair(left, right));\n\t}\n\treturn next;\n}\n\n/**\n * Compute the merkle root over txids in *internal* byte order. The result is\n * also internal order — it matches the `merkle-root` field read straight out of\n * an 80-byte block header, so it can be cross-checked against the header before\n * a proof is trusted.\n */\nexport function merkleRoot(txidsInternal: Uint8Array[]): Uint8Array {\n\tif (txidsInternal.length === 0) {\n\t\tthrow new Error(\"merkleRoot: empty tx list\");\n\t}\n\tlet level = txidsInternal.slice();\n\twhile (level.length > 1) {\n\t\tlevel = nextLevel(level);\n\t}\n\tconst root = level[0];\n\tif (root === undefined) throw new Error(\"merkleRoot: no root\");\n\treturn root;\n}\n\n/**\n * Build the merkle inclusion proof for the tx at `txIndex`. Sibling count is\n * exactly `ceil(log2(txCount))`, as the SIP-044 built-in requires.\n */\nexport function buildMerkleProof(\n\ttxidsInternal: Uint8Array[],\n\ttxIndex: number,\n): MerkleProof {\n\tconst txCount = txidsInternal.length;\n\tif (txCount === 0) {\n\t\tthrow new Error(\"buildMerkleProof: empty tx list\");\n\t}\n\tif (txIndex < 0 || txIndex >= txCount) {\n\t\tthrow new Error(\n\t\t\t`buildMerkleProof: txIndex ${txIndex} out of range for ${txCount} txs`,\n\t\t);\n\t}\n\n\tconst siblings: Uint8Array[] = [];\n\tlet index = txIndex;\n\tlet level = txidsInternal.slice();\n\twhile (level.length > 1) {\n\t\tconst isRight = index % 2 === 1;\n\t\tconst siblingIndex = isRight ? index - 1 : index + 1;\n\t\tconst self = level[index];\n\t\tif (self === undefined) throw new Error(\"buildMerkleProof: missing node\");\n\t\t// When the node is the last of an odd level, it is paired with itself.\n\t\tsiblings.push(level[siblingIndex] ?? self);\n\t\tlevel = nextLevel(level);\n\t\tindex = Math.floor(index / 2);\n\t}\n\n\treturn { siblings, txIndex, txCount };\n}\n\n/**\n * Recompute the merkle root from a leaf + its proof — the same fold the on-chain\n * `verify-merkle-proof` performs. Use it off-chain to self-check a constructed\n * proof against the header's merkle root before submitting a contract call.\n */\nexport function rootFromProof(\n\tleafInternal: Uint8Array,\n\tproof: MerkleProof,\n): Uint8Array {\n\tlet hash = leafInternal;\n\tlet index = proof.txIndex;\n\tfor (const sibling of proof.siblings) {\n\t\thash = index % 2 === 1 ? hashPair(sibling, hash) : hashPair(hash, sibling);\n\t\tindex = Math.floor(index / 2);\n\t}\n\treturn hash;\n}\n",
|
|
6
|
+
"import { bytesToHex, hexToBytes, without0x } from \"../utils/encoding.ts\";\nimport { type MerkleProof, buildMerkleProof, rootFromProof } from \"./merkle.ts\";\nimport { parseBitcoinTx, parseBlockHeader, reverseBytes } from \"./serialize.ts\";\n\n/**\n * A self-contained Bitcoin SPV proof: everything the SIP-044 built-ins need to\n * prove a tx (and one of its outputs) is committed in a confirmed block. Hashes\n * are internal byte order throughout.\n */\nexport interface SpvProof {\n\trawTx: Uint8Array;\n\t/** The tx's txid, internal order — the merkle leaf. */\n\ttxidInternal: Uint8Array;\n\t/** Output index of interest, if the proof targets a specific output. */\n\tvout?: number;\n\tmerkle: MerkleProof;\n\t/** The 80-byte block header that commits the tx. */\n\theader: Uint8Array;\n\t/** Bitcoin block height. */\n\theight: number;\n}\n\n/** The block context a `ProofSource` resolves for a confirmed tx. */\nexport interface BlockForTx {\n\t/** 80-byte block header. */\n\theader: Uint8Array;\n\theight: number;\n\t/** All of the block's txids, internal order, in block order. */\n\ttxidsInternal: Uint8Array[];\n\t/** Index of the target tx within the block. */\n\ttxIndex: number;\n}\n\n/**\n * Where proof inputs come from. The default is the integrator's own Bitcoin node\n * (`bitcoinRpcSource`) — trustless; a hosted Esplora-compatible endpoint\n * (`esploraSource`) is the fallback. `buildTxProof` independently re-checks\n * whatever a source returns, so a wrong or hostile source fails loudly rather\n * than producing a bad proof.\n */\nexport interface ProofSource {\n\t/** Raw (serialized) tx bytes for a txid (display-order hex). */\n\tgetRawTx(txid: string): Promise<Uint8Array>;\n\t/** The confirming block's header, height, and txid set for a txid. */\n\tgetBlockForTx(txid: string): Promise<BlockForTx>;\n}\n\n/** Why a source could not produce a proof input. */\nexport type BitcoinSourceErrorCode =\n\t| \"not-found\"\n\t| \"unconfirmed\"\n\t| \"unavailable\";\n\n/**\n * Thrown by `bitcoinRpcSource` / `esploraSource` when the source itself cannot\n * answer: the tx is unknown, not yet in a block, or the source is unreachable.\n * Inconsistent data from a source is a plain `Error` (the source lied).\n */\nexport class BitcoinSourceError extends Error {\n\toverride readonly name = \"BitcoinSourceError\";\n\tconstructor(\n\t\treadonly code: BitcoinSourceErrorCode,\n\t\tmessage: string,\n\t) {\n\t\tsuper(message);\n\t}\n}\n\nfunction normalizeTxid(txid: string): string {\n\treturn without0x(txid).toLowerCase();\n}\n\n/**\n * Locate `txid` in a block's txid set and assemble its `BlockForTx`. Owns the\n * display→internal byte-order reversal (`reverseBytes`) so the ProofSources\n * can't drift on the one operation a silent merkle-root mismatch would hide.\n * `txidsDisplay` are display-order hex txids as returned by Core / Esplora.\n */\nfunction assembleBlockForTx(args: {\n\ttxid: string;\n\tblockId: string;\n\theaderHex: string;\n\theight: number;\n\ttxidsDisplay: string[];\n}): BlockForTx {\n\tconst txIndex = args.txidsDisplay.indexOf(normalizeTxid(args.txid));\n\tif (txIndex < 0) {\n\t\tthrow new Error(`tx ${args.txid} not found in block ${args.blockId}`);\n\t}\n\treturn {\n\t\theader: hexToBytes(args.headerHex),\n\t\theight: args.height,\n\t\ttxidsInternal: args.txidsDisplay.map((t) => reverseBytes(hexToBytes(t))),\n\t\ttxIndex,\n\t};\n}\n\n/**\n * Assemble an `SpvProof` for a txid from a `ProofSource`, validating every claim\n * the source makes:\n * - the returned raw tx actually hashes to the requested txid,\n * - the claimed `txIndex` points at that txid in the block, and\n * - the resulting merkle proof folds back to the block header's merkle root.\n * Any mismatch throws — the proof is never returned half-trusted.\n */\nexport async function buildTxProof(\n\tsource: ProofSource,\n\tparams: { txid: string; vout?: number },\n): Promise<SpvProof> {\n\tconst { txid, vout } = params;\n\tconst want = normalizeTxid(txid);\n\n\tconst [rawTx, block] = await Promise.all([\n\t\tsource.getRawTx(txid),\n\t\tsource.getBlockForTx(txid),\n\t]);\n\n\tconst parsed = parseBitcoinTx(rawTx);\n\tconst gotDisplay = bytesToHex(reverseBytes(parsed.txidInternal));\n\tif (gotDisplay !== want) {\n\t\tthrow new Error(\n\t\t\t`source returned a tx whose id ${gotDisplay} does not match requested ${want}`,\n\t\t);\n\t}\n\n\tconst atIndex = block.txidsInternal[block.txIndex];\n\tif (!atIndex || bytesToHex(atIndex) !== bytesToHex(parsed.txidInternal)) {\n\t\tthrow new Error(\n\t\t\t`source txIndex ${block.txIndex} does not point at tx ${want}`,\n\t\t);\n\t}\n\n\tconst merkle = buildMerkleProof(block.txidsInternal, block.txIndex);\n\n\tconst headerRoot = parseBlockHeader(block.header).merkleRoot;\n\tconst computed = rootFromProof(parsed.txidInternal, merkle);\n\tif (bytesToHex(computed) !== bytesToHex(headerRoot)) {\n\t\tthrow new Error(\n\t\t\t\"constructed merkle proof does not reconcile with the block header merkle root\",\n\t\t);\n\t}\n\n\treturn {\n\t\trawTx,\n\t\ttxidInternal: parsed.txidInternal,\n\t\tvout,\n\t\tmerkle,\n\t\theader: block.header,\n\t\theight: block.height,\n\t};\n}\n\n/**\n * Compose sources into an ordered fallback chain: each call tries them in turn\n * and returns the first success, throwing the last error if all fail. Put the\n * trustless integrator node first and a hosted endpoint last.\n */\nexport function fallbackProofSource(sources: ProofSource[]): ProofSource {\n\tif (sources.length === 0) {\n\t\tthrow new Error(\"fallbackProofSource: at least one source is required\");\n\t}\n\tasync function firstSuccess<T>(\n\t\tfn: (s: ProofSource) => Promise<T>,\n\t): Promise<T> {\n\t\tlet lastError: unknown;\n\t\tfor (const source of sources) {\n\t\t\ttry {\n\t\t\t\treturn await fn(source);\n\t\t\t} catch (error) {\n\t\t\t\tlastError = error;\n\t\t\t}\n\t\t}\n\t\tthrow lastError;\n\t}\n\treturn {\n\t\tgetRawTx: (txid) => firstSuccess((s) => s.getRawTx(txid)),\n\t\tgetBlockForTx: (txid) => firstSuccess((s) => s.getBlockForTx(txid)),\n\t};\n}\n\nexport interface BitcoinRpcConfig {\n\t/** Bitcoin Core JSON-RPC endpoint URL. */\n\turl: string;\n\t/** Basic auth — `{ username, password }` or a pre-encoded base64 string. */\n\tauth?: { username: string; password: string } | string;\n\t/** Override the fetch implementation (testing / custom agents). */\n\tfetch?: typeof fetch;\n}\n\n/**\n * A `ProofSource` backed by the integrator's own Bitcoin Core node over\n * JSON-RPC. This is the trustless default. The node must run with `-txindex`\n * (or the tx must be in the mempool's block view) so `getrawtransaction` can\n * resolve the confirming block.\n */\nexport function bitcoinRpcSource(config: BitcoinRpcConfig): ProofSource {\n\tconst doFetch = config.fetch ?? fetch;\n\n\tasync function rpc<T>(method: string, rpcParams: unknown[]): Promise<T> {\n\t\tconst headers: Record<string, string> = {\n\t\t\t\"content-type\": \"application/json\",\n\t\t};\n\t\tif (config.auth) {\n\t\t\tconst basic =\n\t\t\t\ttypeof config.auth === \"string\"\n\t\t\t\t\t? config.auth\n\t\t\t\t\t: btoa(`${config.auth.username}:${config.auth.password}`);\n\t\t\theaders.authorization = `Basic ${basic}`;\n\t\t}\n\t\tlet res: Response;\n\t\ttry {\n\t\t\tres = await doFetch(config.url, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders,\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tjsonrpc: \"1.0\",\n\t\t\t\t\tid: \"secondlayer\",\n\t\t\t\t\tmethod,\n\t\t\t\t\tparams: rpcParams,\n\t\t\t\t}),\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tthrow new BitcoinSourceError(\n\t\t\t\t\"unavailable\",\n\t\t\t\t`bitcoin rpc ${method} failed: ${error instanceof Error ? error.message : \"network error\"}`,\n\t\t\t);\n\t\t}\n\t\t// Core answers RPC errors with HTTP 500 and the error in the JSON body.\n\t\tlet json: { result: T; error: { code?: number; message: string } | null };\n\t\ttry {\n\t\t\tjson = (await res.json()) as typeof json;\n\t\t} catch {\n\t\t\tthrow new BitcoinSourceError(\n\t\t\t\t\"unavailable\",\n\t\t\t\t`bitcoin rpc ${method} failed: HTTP ${res.status}`,\n\t\t\t);\n\t\t}\n\t\tif (json.error) {\n\t\t\tthrow new BitcoinSourceError(\n\t\t\t\tjson.error.code === -5 ? \"not-found\" : \"unavailable\",\n\t\t\t\t`bitcoin rpc ${method} error: ${json.error.message}`,\n\t\t\t);\n\t\t}\n\t\tif (!res.ok) {\n\t\t\tthrow new BitcoinSourceError(\n\t\t\t\t\"unavailable\",\n\t\t\t\t`bitcoin rpc ${method} failed: HTTP ${res.status}`,\n\t\t\t);\n\t\t}\n\t\treturn json.result;\n\t}\n\n\treturn {\n\t\tasync getRawTx(txid) {\n\t\t\treturn hexToBytes(await rpc<string>(\"getrawtransaction\", [txid, false]));\n\t\t},\n\t\tasync getBlockForTx(txid) {\n\t\t\tconst tx = await rpc<{ blockhash?: string }>(\"getrawtransaction\", [\n\t\t\t\ttxid,\n\t\t\t\ttrue,\n\t\t\t]);\n\t\t\tif (!tx.blockhash) {\n\t\t\t\tthrow new BitcoinSourceError(\n\t\t\t\t\t\"unconfirmed\",\n\t\t\t\t\t`tx ${txid} is not in a block (node needs -txindex, or the tx is unconfirmed)`,\n\t\t\t\t);\n\t\t\t}\n\t\t\tconst block = await rpc<{ tx: string[]; height: number }>(\"getblock\", [\n\t\t\t\ttx.blockhash,\n\t\t\t\t1,\n\t\t\t]);\n\t\t\tconst header = await rpc<string>(\"getblockheader\", [tx.blockhash, false]);\n\t\t\treturn assembleBlockForTx({\n\t\t\t\ttxid,\n\t\t\t\tblockId: tx.blockhash,\n\t\t\t\theaderHex: header,\n\t\t\t\theight: block.height,\n\t\t\t\ttxidsDisplay: block.tx,\n\t\t\t});\n\t\t},\n\t};\n}\n\nexport interface EsploraConfig {\n\t/** Esplora REST base URL, e.g. `https://blockstream.info/api` or a self-hosted instance. */\n\turl: string;\n\t/** Override the fetch implementation. */\n\tfetch?: typeof fetch;\n}\n\n/**\n * A `ProofSource` backed by an Esplora REST API (self-hosted, or a hosted\n * provider as a fallback). Provider-agnostic: any Esplora-compatible endpoint\n * works. Use as the hosted fallback behind `bitcoinRpcSource`.\n */\nexport function esploraSource(config: EsploraConfig): ProofSource {\n\tconst doFetch = config.fetch ?? fetch;\n\tconst base = config.url.replace(/\\/+$/, \"\");\n\n\tasync function get(path: string): Promise<Response> {\n\t\tlet res: Response;\n\t\ttry {\n\t\t\tres = await doFetch(`${base}${path}`);\n\t\t} catch (error) {\n\t\t\tthrow new BitcoinSourceError(\n\t\t\t\t\"unavailable\",\n\t\t\t\t`esplora GET ${path} failed: ${error instanceof Error ? error.message : \"network error\"}`,\n\t\t\t);\n\t\t}\n\t\tif (!res.ok) {\n\t\t\tthrow new BitcoinSourceError(\n\t\t\t\tres.status === 404 ? \"not-found\" : \"unavailable\",\n\t\t\t\t`esplora GET ${path} failed: HTTP ${res.status}`,\n\t\t\t);\n\t\t}\n\t\treturn res;\n\t}\n\n\treturn {\n\t\tasync getRawTx(txid) {\n\t\t\treturn hexToBytes((await (await get(`/tx/${txid}/hex`)).text()).trim());\n\t\t},\n\t\tasync getBlockForTx(txid) {\n\t\t\tconst status = (await (await get(`/tx/${txid}`)).json()) as {\n\t\t\t\tstatus: {\n\t\t\t\t\tconfirmed: boolean;\n\t\t\t\t\tblock_height: number;\n\t\t\t\t\tblock_hash: string;\n\t\t\t\t};\n\t\t\t};\n\t\t\tif (!status.status.confirmed) {\n\t\t\t\tthrow new BitcoinSourceError(\n\t\t\t\t\t\"unconfirmed\",\n\t\t\t\t\t`tx ${txid} is unconfirmed`,\n\t\t\t\t);\n\t\t\t}\n\t\t\tconst blockHashHex = status.status.block_hash;\n\t\t\tconst header = (\n\t\t\t\tawait (await get(`/block/${blockHashHex}/header`)).text()\n\t\t\t).trim();\n\t\t\tconst txids = (await (\n\t\t\t\tawait get(`/block/${blockHashHex}/txids`)\n\t\t\t).json()) as string[];\n\t\t\treturn assembleBlockForTx({\n\t\t\t\ttxid,\n\t\t\t\tblockId: blockHashHex,\n\t\t\t\theaderHex: header,\n\t\t\t\theight: status.status.block_height,\n\t\t\t\ttxidsDisplay: txids,\n\t\t\t});\n\t\t},\n\t};\n}\n",
|
|
7
|
+
"/**\n * Stacks hard-fork epoch activation heights, as Bitcoin burn block heights.\n *\n * Epoch 4.0 carries both SIP-044 (the native Bitcoin SPV built-ins / Clarity 6)\n * and SIP-045 (`pox-5` Bitcoin Staking) — one fork, one height. Keep it here so\n * the two modules can never disagree.\n */\n\n/**\n * Epoch 4.0 activation height on mainnet — Bitcoin block 960,230 (~2026-07-30\n * AM UTC, per the stacks-core 4.0.1 release notes).\n *\n * Only mainnet has a fixed height. On other networks, read it from the node\n * (`getPox5Activation` for pox-5) or pass it explicitly.\n */\nexport const EPOCH_4_ACTIVATION_BURN_HEIGHT_MAINNET = 960_230;\n"
|
|
8
|
+
],
|
|
9
|
+
"mappings": ";;;;;;;;;;;;;;AAmBA,SAAS,QAAQ,CAAC,MAAkB,OAA+B;AAAA,EAClE,OAAO,cAAa,aAAY,MAAM,KAAK,CAAC;AAAA;AAI7C,SAAS,SAAS,CAAC,OAAmC;AAAA,EACrD,MAAM,OAAqB,CAAC;AAAA,EAC5B,SAAS,IAAI,EAAG,IAAI,MAAM,QAAQ,KAAK,GAAG;AAAA,IACzC,MAAM,OAAO,MAAM;AAAA,IACnB,IAAI,SAAS;AAAA,MAAW,MAAM,IAAI,MAAM,2BAA2B;AAAA,IAEnE,MAAM,QAAQ,MAAM,IAAI,MAAM;AAAA,IAC9B,KAAK,KAAK,SAAS,MAAM,KAAK,CAAC;AAAA,EAChC;AAAA,EACA,OAAO;AAAA;AASD,SAAS,WAAU,CAAC,eAAyC;AAAA,EACnE,IAAI,cAAc,WAAW,GAAG;AAAA,IAC/B,MAAM,IAAI,MAAM,2BAA2B;AAAA,EAC5C;AAAA,EACA,IAAI,QAAQ,cAAc,MAAM;AAAA,EAChC,OAAO,MAAM,SAAS,GAAG;AAAA,IACxB,QAAQ,UAAU,KAAK;AAAA,EACxB;AAAA,EACA,MAAM,OAAO,MAAM;AAAA,EACnB,IAAI,SAAS;AAAA,IAAW,MAAM,IAAI,MAAM,qBAAqB;AAAA,EAC7D,OAAO;AAAA;AAOD,SAAS,iBAAgB,CAC/B,eACA,SACc;AAAA,EACd,MAAM,UAAU,cAAc;AAAA,EAC9B,IAAI,YAAY,GAAG;AAAA,IAClB,MAAM,IAAI,MAAM,iCAAiC;AAAA,EAClD;AAAA,EACA,IAAI,UAAU,KAAK,WAAW,SAAS;AAAA,IACtC,MAAM,IAAI,MACT,6BAA6B,4B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J,IAAI;AAAA,MACH,MAAM,MAAM,QAAQ,GAAG,OAAO,MAAM;AAAA,MACnC,OAAO,OAAO;AAAA,MACf,MAAM,IAAI,oBACT,eACA,eAAe,gBAAgB,iBAAiB,QAAQ,MAAM,UAAU,iBACzE;AAAA;AAAA,IAED,IAAI,CAAC,IAAI,IAAI;AAAA,MACZ,MAAM,IAAI,oBACT,IAAI,WAAW,MAAM,cAAc,eACnC,eAAe,qBAAqB,IAAI,QACzC;AAAA,IACD;AAAA,IACA,OAAO;AAAA;AAAA,EAGR,OAAO;AAAA,SACA,SAAQ,CAAC,MAAM;AAAA,MACpB,OAAO,aAAY,OAAO,MAAM,IAAI,OAAO,UAAU,GAAG,KAAK,GAAG,KAAK,CAAC;AAAA;AAAA,SAEjE,cAAa,CAAC,MAAM;AAAA,MACzB,MAAM,SAAU,OAAO,MAAM,IAAI,OAAO,MAAM,GAAG,KAAK;AAAA,MAOtD,IAAI,CAAC,OAAO,OAAO,WAAW;AAAA,QAC7B,MAAM,IAAI,oBACT,eACA,MAAM,qBACP;AAAA,MACD;AAAA,MACA,MAAM,eAAe,OAAO,OAAO;AAAA,MACnC,MAAM,UACL,OAAO,MAAM,IAAI,UAAU,qBAAqB,GAAG,KAAK,GACvD,KAAK;AAAA,MACP,MAAM,QAAS,OACd,MAAM,IAAI,UAAU,oBAAoB,GACvC,KAAK;AAAA,MACP,OAAO,mBAAmB;AAAA,QACzB;AAAA,QACA,SAAS;AAAA,QACT,WAAW;AAAA,QACX,QAAQ,OAAO,OAAO;AAAA,QACtB,cAAc;AAAA,MACf,CAAC;AAAA;AAAA,EAEH;AAAA;;;AChVM,IAAM,0CAAyC;",
|
|
10
|
+
"debugId": "4CD23767D82ECDDE64756E2164756E21",
|
|
11
|
+
"names": []
|
|
12
|
+
}
|
|
@@ -66,6 +66,7 @@ var __export = (target, all) => {
|
|
|
66
66
|
// src/bitcoin/index.ts
|
|
67
67
|
var exports_bitcoin = {};
|
|
68
68
|
__export(exports_bitcoin, {
|
|
69
|
+
BitcoinSourceError: () => BitcoinSourceError,
|
|
69
70
|
BtcReader: () => BtcReader,
|
|
70
71
|
EPOCH_4_ACTIVATION_BURN_HEIGHT_MAINNET: () => EPOCH_4_ACTIVATION_BURN_HEIGHT_MAINNET,
|
|
71
72
|
SPV_ADAPTER_ABI: () => SPV_ADAPTER_ABI,
|
|
@@ -452,6 +453,14 @@ function rootFromProof(leafInternal, proof) {
|
|
|
452
453
|
}
|
|
453
454
|
|
|
454
455
|
// src/bitcoin/proof.ts
|
|
456
|
+
class BitcoinSourceError extends Error {
|
|
457
|
+
code;
|
|
458
|
+
name = "BitcoinSourceError";
|
|
459
|
+
constructor(code, message) {
|
|
460
|
+
super(message);
|
|
461
|
+
this.code = code;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
455
464
|
function normalizeTxid(txid) {
|
|
456
465
|
return without0x(txid).toLowerCase();
|
|
457
466
|
}
|
|
@@ -528,22 +537,32 @@ function bitcoinRpcSource(config) {
|
|
|
528
537
|
const basic = typeof config.auth === "string" ? config.auth : btoa(`${config.auth.username}:${config.auth.password}`);
|
|
529
538
|
headers.authorization = `Basic ${basic}`;
|
|
530
539
|
}
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
540
|
+
let res;
|
|
541
|
+
try {
|
|
542
|
+
res = await doFetch(config.url, {
|
|
543
|
+
method: "POST",
|
|
544
|
+
headers,
|
|
545
|
+
body: JSON.stringify({
|
|
546
|
+
jsonrpc: "1.0",
|
|
547
|
+
id: "secondlayer",
|
|
548
|
+
method,
|
|
549
|
+
params: rpcParams
|
|
550
|
+
})
|
|
551
|
+
});
|
|
552
|
+
} catch (error) {
|
|
553
|
+
throw new BitcoinSourceError("unavailable", `bitcoin rpc ${method} failed: ${error instanceof Error ? error.message : "network error"}`);
|
|
554
|
+
}
|
|
555
|
+
let json;
|
|
556
|
+
try {
|
|
557
|
+
json = await res.json();
|
|
558
|
+
} catch {
|
|
559
|
+
throw new BitcoinSourceError("unavailable", `bitcoin rpc ${method} failed: HTTP ${res.status}`);
|
|
543
560
|
}
|
|
544
|
-
const json = await res.json();
|
|
545
561
|
if (json.error) {
|
|
546
|
-
throw new
|
|
562
|
+
throw new BitcoinSourceError(json.error.code === -5 ? "not-found" : "unavailable", `bitcoin rpc ${method} error: ${json.error.message}`);
|
|
563
|
+
}
|
|
564
|
+
if (!res.ok) {
|
|
565
|
+
throw new BitcoinSourceError("unavailable", `bitcoin rpc ${method} failed: HTTP ${res.status}`);
|
|
547
566
|
}
|
|
548
567
|
return json.result;
|
|
549
568
|
}
|
|
@@ -557,7 +576,7 @@ function bitcoinRpcSource(config) {
|
|
|
557
576
|
true
|
|
558
577
|
]);
|
|
559
578
|
if (!tx.blockhash) {
|
|
560
|
-
throw new
|
|
579
|
+
throw new BitcoinSourceError("unconfirmed", `tx ${txid} is not in a block (node needs -txindex, or the tx is unconfirmed)`);
|
|
561
580
|
}
|
|
562
581
|
const block = await rpc("getblock", [
|
|
563
582
|
tx.blockhash,
|
|
@@ -578,9 +597,14 @@ function esploraSource(config) {
|
|
|
578
597
|
const doFetch = config.fetch ?? fetch;
|
|
579
598
|
const base = config.url.replace(/\/+$/, "");
|
|
580
599
|
async function get(path) {
|
|
581
|
-
|
|
600
|
+
let res;
|
|
601
|
+
try {
|
|
602
|
+
res = await doFetch(`${base}${path}`);
|
|
603
|
+
} catch (error) {
|
|
604
|
+
throw new BitcoinSourceError("unavailable", `esplora GET ${path} failed: ${error instanceof Error ? error.message : "network error"}`);
|
|
605
|
+
}
|
|
582
606
|
if (!res.ok) {
|
|
583
|
-
throw new
|
|
607
|
+
throw new BitcoinSourceError(res.status === 404 ? "not-found" : "unavailable", `esplora GET ${path} failed: HTTP ${res.status}`);
|
|
584
608
|
}
|
|
585
609
|
return res;
|
|
586
610
|
}
|
|
@@ -591,7 +615,7 @@ function esploraSource(config) {
|
|
|
591
615
|
async getBlockForTx(txid) {
|
|
592
616
|
const status = await (await get(`/tx/${txid}`)).json();
|
|
593
617
|
if (!status.status.confirmed) {
|
|
594
|
-
throw new
|
|
618
|
+
throw new BitcoinSourceError("unconfirmed", `tx ${txid} is unconfirmed`);
|
|
595
619
|
}
|
|
596
620
|
const blockHashHex = status.status.block_hash;
|
|
597
621
|
const header = (await (await get(`/block/${blockHashHex}/header`)).text()).trim();
|
|
@@ -2798,5 +2822,5 @@ async function verifyBitcoinPayment(client, params) {
|
|
|
2798
2822
|
return { verified, mined, output, proof };
|
|
2799
2823
|
}
|
|
2800
2824
|
|
|
2801
|
-
//# debugId=
|
|
2825
|
+
//# debugId=0A88BBD4FA62DB1264756E2164756E21
|
|
2802
2826
|
//# sourceMappingURL=index.cjs.map
|