@babylonlabs-io/ts-sdk 0.23.0 → 0.24.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/{challengeAssert-D1lpvuMv.js → challengeAssert-CH32j5cZ.js} +35 -29
- package/dist/challengeAssert-CH32j5cZ.js.map +1 -0
- package/dist/challengeAssert-X7V3Ik_Q.cjs +2 -0
- package/dist/challengeAssert-X7V3Ik_Q.cjs.map +1 -0
- package/dist/{errors-BcpQEG62.cjs → errors-B3TOmE31.cjs} +2 -2
- package/dist/{errors-BcpQEG62.cjs.map → errors-B3TOmE31.cjs.map} +1 -1
- package/dist/{errors-D3a9XcIX.js → errors-rJcuN2m_.js} +2 -2
- package/dist/{errors-D3a9XcIX.js.map → errors-rJcuN2m_.js.map} +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +2 -2
- package/dist/tbv/core/index.cjs +1 -1
- package/dist/tbv/core/index.js +2 -2
- package/dist/tbv/core/primitives/index.cjs +1 -1
- package/dist/tbv/core/primitives/index.js +1 -1
- package/dist/tbv/core/primitives/psbt/__tests__/peginInput.test.d.ts +10 -0
- package/dist/tbv/core/primitives/psbt/__tests__/peginInput.test.d.ts.map +1 -0
- package/dist/tbv/core/primitives/psbt/peginInput.d.ts +8 -0
- package/dist/tbv/core/primitives/psbt/peginInput.d.ts.map +1 -1
- package/dist/tbv/index.cjs +1 -1
- package/dist/tbv/index.js +2 -2
- package/dist/tbv/integrations/aave/clients/spoke.d.ts +1 -1
- package/dist/tbv/integrations/aave/constants.d.ts +3 -2
- package/dist/tbv/integrations/aave/constants.d.ts.map +1 -1
- package/dist/tbv/integrations/aave/index.cjs +1 -1
- package/dist/tbv/integrations/aave/index.cjs.map +1 -1
- package/dist/tbv/integrations/aave/index.js +1 -1
- package/dist/tbv/integrations/aave/index.js.map +1 -1
- package/dist/tbv/integrations/aave/utils/aaveConversions.d.ts +2 -2
- package/package.json +3 -3
- package/dist/challengeAssert-D1lpvuMv.js.map +0 -1
- package/dist/challengeAssert-nYlgeAI8.cjs +0 -2
- package/dist/challengeAssert-nYlgeAI8.cjs.map +0 -1
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { p as
|
|
2
|
-
import { createPrePeginTransaction as
|
|
3
|
-
import { b as
|
|
1
|
+
import { p as S, s as g, u as v, h as p, T as y } from "./bitcoin-nOOgeRyl.js";
|
|
2
|
+
import { createPrePeginTransaction as m, buildPeginTxFromPrePegin as I, getPrePeginHtlcConnectorInfo as H, tapInternalPubkey as k, getPeginPayoutScriptInfo as T, getAssertNoPayoutScriptInfo as A, getChallengeAssertScriptInfo as B } from "@babylonlabs-io/babylon-tbv-rust-wasm";
|
|
3
|
+
import { b as C } from "./fundPeginTransaction-BLYXxLBv.js";
|
|
4
4
|
import { Buffer as c } from "buffer";
|
|
5
|
-
import { Transaction as
|
|
5
|
+
import { Transaction as h, Psbt as d } from "bitcoinjs-lib";
|
|
6
6
|
function b(e) {
|
|
7
|
-
return
|
|
7
|
+
return S(e).toLowerCase();
|
|
8
8
|
}
|
|
9
9
|
function R(e, t, n) {
|
|
10
10
|
const s = /* @__PURE__ */ new Set();
|
|
@@ -14,7 +14,7 @@ function R(e, t, n) {
|
|
|
14
14
|
return s.delete(b(n)), s.size;
|
|
15
15
|
}
|
|
16
16
|
async function U(e) {
|
|
17
|
-
const t = await
|
|
17
|
+
const t = await m({
|
|
18
18
|
depositorPubkey: e.depositorPubkey,
|
|
19
19
|
vaultProviderPubkey: e.vaultProviderPubkey,
|
|
20
20
|
vaultKeeperPubkeys: e.vaultKeeperPubkeys,
|
|
@@ -27,7 +27,7 @@ async function U(e) {
|
|
|
27
27
|
councilQuorum: e.councilQuorum,
|
|
28
28
|
councilSize: e.councilSize,
|
|
29
29
|
network: e.network
|
|
30
|
-
}), s =
|
|
30
|
+
}), s = C(t.txHex).outputs.reduce(
|
|
31
31
|
(o, r) => o + BigInt(r.value),
|
|
32
32
|
0n
|
|
33
33
|
);
|
|
@@ -69,7 +69,7 @@ async function q(e) {
|
|
|
69
69
|
};
|
|
70
70
|
}
|
|
71
71
|
async function O(e) {
|
|
72
|
-
const t = g(e.peginTxHex), n = g(e.fundedPrePeginTxHex), s = await
|
|
72
|
+
const t = g(e.peginTxHex), n = g(e.fundedPrePeginTxHex), s = await H({
|
|
73
73
|
depositorPubkey: e.depositorPubkey,
|
|
74
74
|
vaultProviderPubkey: e.vaultProviderPubkey,
|
|
75
75
|
vaultKeeperPubkeys: e.vaultKeeperPubkeys,
|
|
@@ -77,7 +77,7 @@ async function O(e) {
|
|
|
77
77
|
hashlock: e.hashlock,
|
|
78
78
|
timelockRefund: e.timelockRefund,
|
|
79
79
|
network: e.network
|
|
80
|
-
}), o =
|
|
80
|
+
}), o = h.fromHex(t), r = h.fromHex(n);
|
|
81
81
|
if (o.ins.length !== 1)
|
|
82
82
|
throw new Error(
|
|
83
83
|
`PegIn transaction must have exactly 1 input, got ${o.ins.length}`
|
|
@@ -94,8 +94,8 @@ async function O(e) {
|
|
|
94
94
|
throw new Error(
|
|
95
95
|
`Pre-PegIn output ${l.index} not found (Pre-PegIn has ${r.outs.length} outputs)`
|
|
96
96
|
);
|
|
97
|
-
const P = p(s.hashlockScript), f = p(s.hashlockControlBlock),
|
|
98
|
-
|
|
97
|
+
const P = p(s.hashlockScript), f = p(s.hashlockControlBlock), x = new d();
|
|
98
|
+
x.setVersion(o.version), x.setLocktime(o.locktime), x.addInput({
|
|
99
99
|
hash: l.hash,
|
|
100
100
|
index: l.index,
|
|
101
101
|
sequence: l.sequence,
|
|
@@ -114,13 +114,13 @@ async function O(e) {
|
|
|
114
114
|
// sighashType omitted — defaults to SIGHASH_DEFAULT (0x00) for Taproot
|
|
115
115
|
});
|
|
116
116
|
for (const w of o.outs)
|
|
117
|
-
|
|
117
|
+
x.addOutput({
|
|
118
118
|
script: w.script,
|
|
119
119
|
value: w.value
|
|
120
120
|
});
|
|
121
|
-
return { psbtHex:
|
|
121
|
+
return { psbtHex: x.toHex() };
|
|
122
122
|
}
|
|
123
|
-
function
|
|
123
|
+
function _(e, t) {
|
|
124
124
|
const s = d.fromHex(e).data.inputs[0];
|
|
125
125
|
if (!s)
|
|
126
126
|
throw new Error("PegIn PSBT has no inputs");
|
|
@@ -141,7 +141,7 @@ function F(e, t) {
|
|
|
141
141
|
"No tapScriptSig or finalScriptWitness found in signed PegIn input PSBT"
|
|
142
142
|
);
|
|
143
143
|
}
|
|
144
|
-
function
|
|
144
|
+
function F(e) {
|
|
145
145
|
const t = d.fromHex(e);
|
|
146
146
|
try {
|
|
147
147
|
t.finalizeAllInputs();
|
|
@@ -158,12 +158,18 @@ function N(e) {
|
|
|
158
158
|
function E(e) {
|
|
159
159
|
if (e.length === 64)
|
|
160
160
|
return v(new Uint8Array(e));
|
|
161
|
-
if (e.length === 65)
|
|
161
|
+
if (e.length === 65) {
|
|
162
|
+
const t = e[64];
|
|
163
|
+
if (t !== h.SIGHASH_ALL)
|
|
164
|
+
throw new Error(
|
|
165
|
+
`Unexpected sighash type 0x${t.toString(16).padStart(2, "0")} in PegIn input signature. Expected SIGHASH_DEFAULT (64-byte sig) or SIGHASH_ALL (0x01).`
|
|
166
|
+
);
|
|
162
167
|
return v(new Uint8Array(e.subarray(0, 64)));
|
|
168
|
+
}
|
|
163
169
|
throw new Error(`Unexpected PegIn input signature length: ${e.length}`);
|
|
164
170
|
}
|
|
165
|
-
async function
|
|
166
|
-
const t = g(e.payoutTxHex), n =
|
|
171
|
+
async function N(e) {
|
|
172
|
+
const t = g(e.payoutTxHex), n = h.fromHex(t), { payoutScript: s, payoutControlBlock: o } = await T(
|
|
167
173
|
e.connectorParams
|
|
168
174
|
), r = p(s), l = p(o), u = new d();
|
|
169
175
|
u.setVersion(n.version), u.setLocktime(n.locktime);
|
|
@@ -195,8 +201,8 @@ async function W(e) {
|
|
|
195
201
|
});
|
|
196
202
|
return u.toHex();
|
|
197
203
|
}
|
|
198
|
-
async function
|
|
199
|
-
const t = g(e.noPayoutTxHex), n =
|
|
204
|
+
async function W(e) {
|
|
205
|
+
const t = g(e.noPayoutTxHex), n = h.fromHex(t), { noPayoutScript: s, noPayoutControlBlock: o } = await A(
|
|
200
206
|
e.connectorParams,
|
|
201
207
|
e.challengerPubkey
|
|
202
208
|
), r = p(s), l = p(o), u = new d();
|
|
@@ -229,14 +235,14 @@ async function _(e) {
|
|
|
229
235
|
});
|
|
230
236
|
return u.toHex();
|
|
231
237
|
}
|
|
232
|
-
async function
|
|
233
|
-
const t = g(e.challengeAssertTxHex), n =
|
|
238
|
+
async function D(e) {
|
|
239
|
+
const t = g(e.challengeAssertTxHex), n = h.fromHex(t);
|
|
234
240
|
if (e.connectorParamsPerInput.length !== n.ins.length)
|
|
235
241
|
throw new Error(
|
|
236
242
|
`Expected ${n.ins.length} connector params, got ${e.connectorParamsPerInput.length}`
|
|
237
243
|
);
|
|
238
244
|
const s = await Promise.all(
|
|
239
|
-
e.connectorParamsPerInput.map((r) =>
|
|
245
|
+
e.connectorParamsPerInput.map((r) => B(r))
|
|
240
246
|
), o = new d();
|
|
241
247
|
o.setVersion(n.version), o.setLocktime(n.locktime);
|
|
242
248
|
for (let r = 0; r < n.ins.length; r++) {
|
|
@@ -274,10 +280,10 @@ export {
|
|
|
274
280
|
U as b,
|
|
275
281
|
R as c,
|
|
276
282
|
O as d,
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
283
|
+
_ as e,
|
|
284
|
+
F as f,
|
|
285
|
+
N as g,
|
|
286
|
+
W as h,
|
|
287
|
+
D as i
|
|
282
288
|
};
|
|
283
|
-
//# sourceMappingURL=challengeAssert-
|
|
289
|
+
//# sourceMappingURL=challengeAssert-CH32j5cZ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"challengeAssert-CH32j5cZ.js","sources":["../src/tbv/core/primitives/challengers.ts","../src/tbv/core/primitives/psbt/pegin.ts","../src/tbv/core/primitives/psbt/peginInput.ts","../src/tbv/core/primitives/psbt/depositorPayout.ts","../src/tbv/core/primitives/psbt/noPayout.ts","../src/tbv/core/primitives/psbt/challengeAssert.ts"],"sourcesContent":["/**\n * Challenger counting utilities.\n *\n * Used for UI-level validation (e.g. computing minimum deposit amounts)\n * where the depositor's identity is known. The transaction builders use\n * `vaultKeeperBtcPubkeys.length` to match the VP's current validation.\n */\n\nimport { processPublicKeyToXOnly } from \"./utils/bitcoin\";\n\n/**\n * Normalize a public key to lowercase x-only hex for reliable comparison.\n *\n * Handles `0x` prefixes, compressed (33-byte), and uncompressed (65-byte) keys.\n */\nfunction normalizeKey(key: string): string {\n return processPublicKeyToXOnly(key).toLowerCase();\n}\n\n/**\n * Compute the number of local challengers for a vault.\n *\n * Mirrors the VP's `compute_num_challengers()` logic:\n * local challengers = {vault_provider} ∪ {vault_keepers} − {depositor}\n *\n * Keys are normalized to x-only lowercase hex before comparison, so\n * `0x`-prefixed, compressed, or mixed-case keys are handled correctly.\n *\n * @param vaultProviderPubkey - Vault provider BTC public key\n * @param vaultKeeperPubkeys - Vault keeper BTC public keys\n * @param depositorPubkey - Depositor (claimer) BTC public key\n * @returns Number of local challengers\n */\nexport function computeNumLocalChallengers(\n vaultProviderPubkey: string,\n vaultKeeperPubkeys: string[],\n depositorPubkey: string,\n): number {\n const localSet = new Set<string>();\n localSet.add(normalizeKey(vaultProviderPubkey));\n for (const vk of vaultKeeperPubkeys) {\n localSet.add(normalizeKey(vk));\n }\n localSet.delete(normalizeKey(depositorPubkey));\n return localSet.size;\n}\n","/**\n * Pre-PegIn PSBT Builder Primitive\n *\n * This module provides pure functions for building unfunded Pre-PegIn transactions\n * and deriving PegIn transactions from them, using the WASM implementation from\n * @babylonlabs-io/babylon-tbv-rust-wasm.\n *\n * Pre-PegIn Flow:\n * 1. buildPrePeginPsbt() — creates unfunded Pre-PegIn tx (HTLC output)\n * 2. [caller funds Pre-PegIn tx and computes txid]\n * 3. buildPeginTxFromFundedPrePegin() — derives PegIn tx spending the HTLC\n * 4. buildPeginInputPsbt() — PSBT for depositor to sign PegIn HTLC leaf 0 input\n *\n * @module primitives/psbt/pegin\n */\n\nimport {\n createPrePeginTransaction,\n buildPeginTxFromPrePegin,\n type Network,\n} from \"@babylonlabs-io/babylon-tbv-rust-wasm\";\n\nimport { parseUnfundedWasmTransaction } from \"../../utils/transaction/fundPeginTransaction\";\n\n/**\n * Parameters for building an unfunded Pre-PegIn PSBT\n */\nexport interface PrePeginParams {\n /** Depositor's BTC public key (x-only, 64-char hex without 0x prefix) */\n depositorPubkey: string;\n /** Vault provider's BTC public key (x-only, 64-char hex) */\n vaultProviderPubkey: string;\n /** Array of vault keeper BTC public keys (x-only, 64-char hex) */\n vaultKeeperPubkeys: string[];\n /** Array of universal challenger BTC public keys (x-only, 64-char hex) */\n universalChallengerPubkeys: string[];\n /** SHA256 hash commitment(s) (64 hex chars = 32 bytes each) */\n hashlocks: readonly string[];\n /** CSV timelock in blocks for the HTLC refund path */\n timelockRefund: number;\n /** Amounts to peg in (satoshis), one per deposit */\n pegInAmounts: readonly bigint[];\n /** Fee rate in sat/vB from contract offchain params */\n feeRate: bigint;\n /** Number of local challengers (from contract params) */\n numLocalChallengers: number;\n /** M in M-of-N council multisig (from contract params) */\n councilQuorum: number;\n /** N in M-of-N council multisig (from contract params) */\n councilSize: number;\n /** Bitcoin network */\n network: Network;\n}\n\n/**\n * Result of building an unfunded Pre-PegIn transaction\n */\nexport interface PrePeginPsbtResult {\n /**\n * Unfunded transaction hex (no inputs, HTLC output + CPFP anchor).\n *\n * The caller is responsible for:\n * - Selecting UTXOs covering totalOutputValue + network fees\n * - Funding the transaction (add inputs and change output)\n * - Calling buildPeginTxFromFundedPrePegin() with the funded tx hex\n */\n psbtHex: string;\n /** Sum of all unfunded outputs (HTLC + CPFP anchor) — use this for UTXO selection */\n totalOutputValue: bigint;\n /** HTLC output values in satoshis, one per deposit (each includes peginAmount + depositorClaimValue + minPeginFee) */\n htlcValues: readonly bigint[];\n /** HTLC output scriptPubKeys (hex encoded), one per deposit */\n htlcScriptPubKeys: readonly string[];\n /** HTLC Taproot addresses, one per deposit */\n htlcAddresses: readonly string[];\n /** Pegin amounts in satoshis, one per deposit */\n peginAmounts: readonly bigint[];\n /** Depositor claim value computed by WASM from contract parameters */\n depositorClaimValue: bigint;\n}\n\n/**\n * Parameters for building the PegIn transaction from a funded Pre-PegIn tx\n */\nexport interface BuildPeginTxParams {\n /** Same PrePeginParams used to create the Pre-PegIn transaction */\n prePeginParams: PrePeginParams;\n /** CSV timelock in blocks for the PegIn vault output */\n timelockPegin: number;\n /** Hex-encoded funded Pre-PegIn transaction */\n fundedPrePeginTxHex: string;\n /** Index of the HTLC output to spend */\n htlcVout: number;\n}\n\n/**\n * Result of building the PegIn transaction\n */\nexport interface PeginTxResult {\n /** PegIn transaction hex (1 input spending HTLC, 1 vault output) */\n txHex: string;\n /** PegIn transaction ID */\n txid: string;\n /** Vault output scriptPubKey (hex encoded) */\n vaultScriptPubKey: string;\n /** Vault output value in satoshis */\n vaultValue: bigint;\n}\n\n/**\n * Build unfunded Pre-PegIn transaction using WASM.\n *\n * Creates a Bitcoin transaction template with no inputs, an HTLC output, and a\n * CPFP anchor output. The HTLC value is computed internally from the contract\n * parameters — the caller does not need to compute depositorClaimValue separately.\n *\n * @param params - Pre-PegIn parameters\n * @returns Unfunded Pre-PegIn transaction details with HTLC output information\n * @throws If WASM initialization fails or parameters are invalid\n */\nexport async function buildPrePeginPsbt(\n params: PrePeginParams,\n): Promise<PrePeginPsbtResult> {\n const result = await createPrePeginTransaction({\n depositorPubkey: params.depositorPubkey,\n vaultProviderPubkey: params.vaultProviderPubkey,\n vaultKeeperPubkeys: params.vaultKeeperPubkeys,\n universalChallengerPubkeys: params.universalChallengerPubkeys,\n hashlocks: [...params.hashlocks],\n timelockRefund: params.timelockRefund,\n pegInAmounts: [...params.pegInAmounts],\n feeRate: params.feeRate,\n numLocalChallengers: params.numLocalChallengers,\n councilQuorum: params.councilQuorum,\n councilSize: params.councilSize,\n network: params.network,\n });\n\n // Parse the unfunded tx to sum all output values (HTLC + CPFP anchor).\n // This is the amount UTXOs must cover before adding network fees.\n const parsed = parseUnfundedWasmTransaction(result.txHex);\n const totalOutputValue = parsed.outputs.reduce(\n (sum, o) => sum + BigInt(o.value),\n 0n,\n );\n\n return {\n psbtHex: result.txHex,\n totalOutputValue,\n htlcValues: result.htlcValues,\n htlcScriptPubKeys: result.htlcScriptPubKeys,\n htlcAddresses: result.htlcAddresses,\n peginAmounts: result.peginAmounts,\n depositorClaimValue: result.depositorClaimValue,\n };\n}\n\n/**\n * Build the PegIn transaction from a funded Pre-PegIn transaction.\n *\n * The PegIn transaction spends the Pre-PegIn HTLC output at htlcVout via the\n * hashlock + all-party script (leaf 0).\n *\n * @param params - Build parameters including Pre-PegIn params and funded tx hex\n * @returns PegIn transaction details\n * @throws If WASM initialization fails or parameters are invalid\n */\nexport async function buildPeginTxFromFundedPrePegin(\n params: BuildPeginTxParams,\n): Promise<PeginTxResult> {\n const result = await buildPeginTxFromPrePegin(\n {\n depositorPubkey: params.prePeginParams.depositorPubkey,\n vaultProviderPubkey: params.prePeginParams.vaultProviderPubkey,\n vaultKeeperPubkeys: params.prePeginParams.vaultKeeperPubkeys,\n universalChallengerPubkeys: params.prePeginParams.universalChallengerPubkeys,\n hashlocks: [...params.prePeginParams.hashlocks],\n timelockRefund: params.prePeginParams.timelockRefund,\n pegInAmounts: [...params.prePeginParams.pegInAmounts],\n feeRate: params.prePeginParams.feeRate,\n numLocalChallengers: params.prePeginParams.numLocalChallengers,\n councilQuorum: params.prePeginParams.councilQuorum,\n councilSize: params.prePeginParams.councilSize,\n network: params.prePeginParams.network,\n },\n params.timelockPegin,\n params.fundedPrePeginTxHex,\n params.htlcVout,\n );\n\n return {\n txHex: result.txHex,\n txid: result.txid,\n vaultScriptPubKey: result.vaultScriptPubKey,\n vaultValue: result.vaultValue,\n };\n}\n","/**\n * PegIn Input PSBT Builder\n *\n * Builds the PSBT for the depositor to sign the PegIn transaction's HTLC input\n * (Pre-PegIn HTLC leaf 0 — the hashlock + all-party script).\n *\n * This is the \"Sign Pegin transaction HTLC leaf 0 input\" step in the pre-pegin\n * flow. The depositor signs input 0 of the PegIn transaction,\n * which spends output 0 of the funded Pre-PegIn transaction via script-path.\n *\n * @module primitives/psbt/peginInput\n */\n\nimport {\n getPrePeginHtlcConnectorInfo,\n tapInternalPubkey,\n type Network,\n} from \"@babylonlabs-io/babylon-tbv-rust-wasm\";\nimport { Buffer } from \"buffer\";\nimport { Psbt, Transaction } from \"bitcoinjs-lib\";\nimport { TAPSCRIPT_LEAF_VERSION, hexToUint8Array, stripHexPrefix, uint8ArrayToHex } from \"../utils/bitcoin\";\n\n/**\n * Parameters for building the PegIn input PSBT\n */\nexport interface BuildPeginInputPsbtParams {\n /**\n * PegIn transaction hex (1 input spending Pre-PegIn HTLC output 0).\n * Returned by buildPeginTxFromFundedPrePegin().\n */\n peginTxHex: string;\n /**\n * Funded Pre-PegIn transaction hex.\n * Used to look up the HTLC output that the PegIn input spends.\n */\n fundedPrePeginTxHex: string;\n /** Depositor's BTC public key (x-only, 64-char hex) */\n depositorPubkey: string;\n /** Vault provider's BTC public key (x-only, 64-char hex) */\n vaultProviderPubkey: string;\n /** Vault keeper BTC public keys (x-only, 64-char hex) */\n vaultKeeperPubkeys: string[];\n /** Universal challenger BTC public keys (x-only, 64-char hex) */\n universalChallengerPubkeys: string[];\n /** SHA256 hash commitment (64 hex chars = 32 bytes) */\n hashlock: string;\n /** CSV timelock in blocks for the HTLC refund path */\n timelockRefund: number;\n /** Bitcoin network */\n network: Network;\n}\n\n/**\n * Result of building the PegIn input PSBT\n */\nexport interface BuildPeginInputPsbtResult {\n /** PSBT hex for the depositor to sign */\n psbtHex: string;\n}\n\n/**\n * Build PSBT for depositor to sign the PegIn transaction's HTLC leaf 0 input.\n *\n * The PegIn transaction spends the Pre-PegIn HTLC output (output 0) via the\n * hashlock + all-party script (leaf 0). The depositor provides one of the required\n * signatures; the vault provider and keepers provide theirs separately via the\n * signPeginInput RPC.\n *\n * The PSBT uses Taproot script-path spending:\n * - witnessUtxo: the Pre-PegIn HTLC output\n * - tapLeafScript: hashlock leaf script + control block\n * - tapInternalKey: NUMS unspendable key (BIP-341 nothing-up-my-sleeve)\n *\n * @param params - PegIn input PSBT parameters\n * @returns PSBT hex ready for depositor signing\n * @throws If PegIn tx does not have exactly 1 input\n * @throws If PegIn input does not reference the Pre-PegIn HTLC output\n * @throws If Pre-PegIn tx output 0 is not found\n */\nexport async function buildPeginInputPsbt(\n params: BuildPeginInputPsbtParams,\n): Promise<BuildPeginInputPsbtResult> {\n const peginTxHex = stripHexPrefix(params.peginTxHex);\n const fundedPrePeginTxHex = stripHexPrefix(params.fundedPrePeginTxHex);\n\n const htlcConnector = await getPrePeginHtlcConnectorInfo({\n depositorPubkey: params.depositorPubkey,\n vaultProviderPubkey: params.vaultProviderPubkey,\n vaultKeeperPubkeys: params.vaultKeeperPubkeys,\n universalChallengerPubkeys: params.universalChallengerPubkeys,\n hashlock: params.hashlock,\n timelockRefund: params.timelockRefund,\n network: params.network,\n });\n\n const peginTx = Transaction.fromHex(peginTxHex);\n const prePeginTx = Transaction.fromHex(fundedPrePeginTxHex);\n\n if (peginTx.ins.length !== 1) {\n throw new Error(\n `PegIn transaction must have exactly 1 input, got ${peginTx.ins.length}`,\n );\n }\n\n const peginInput = peginTx.ins[0];\n\n // Verify PegIn input 0 spends Pre-PegIn output 0\n const prePeginTxid = prePeginTx.getId();\n const peginInputTxid = uint8ArrayToHex(\n new Uint8Array(peginInput.hash).slice().reverse(),\n );\n\n if (peginInputTxid !== prePeginTxid) {\n throw new Error(\n `PegIn input does not reference the Pre-PegIn transaction. ` +\n `Expected ${prePeginTxid}, got ${peginInputTxid}`,\n );\n }\n\n const htlcOutput = prePeginTx.outs[peginInput.index];\n if (!htlcOutput) {\n throw new Error(\n `Pre-PegIn output ${peginInput.index} not found ` +\n `(Pre-PegIn has ${prePeginTx.outs.length} outputs)`,\n );\n }\n\n const hashlockScript = hexToUint8Array(htlcConnector.hashlockScript);\n const hashlockControlBlock = hexToUint8Array(htlcConnector.hashlockControlBlock);\n\n const psbt = new Psbt();\n psbt.setVersion(peginTx.version);\n psbt.setLocktime(peginTx.locktime);\n\n // Input 0: PegIn input spending Pre-PegIn HTLC output 0 via hashlock leaf (leaf 0).\n // The depositor signs using Taproot script-path spending.\n psbt.addInput({\n hash: peginInput.hash,\n index: peginInput.index,\n sequence: peginInput.sequence,\n witnessUtxo: {\n script: htlcOutput.script,\n value: htlcOutput.value,\n },\n tapLeafScript: [\n {\n leafVersion: TAPSCRIPT_LEAF_VERSION,\n script: Buffer.from(hashlockScript),\n controlBlock: Buffer.from(hashlockControlBlock),\n },\n ],\n tapInternalKey: Buffer.from(tapInternalPubkey),\n // sighashType omitted — defaults to SIGHASH_DEFAULT (0x00) for Taproot\n });\n\n for (const output of peginTx.outs) {\n psbt.addOutput({\n script: output.script,\n value: output.value,\n });\n }\n\n return { psbtHex: psbt.toHex() };\n}\n\n/**\n * Extract the depositor's Schnorr signature from a signed PegIn input PSBT.\n *\n * Supports both non-finalized PSBTs (tapScriptSig) and finalized PSBTs (witness).\n *\n * @param signedPsbtHex - Signed PSBT hex\n * @param depositorPubkey - Depositor's x-only public key (64-char hex)\n * @returns 64-byte Schnorr signature (128 hex chars, no sighash flag)\n * @throws If no signature is found for the depositor's key\n */\nexport function extractPeginInputSignature(\n signedPsbtHex: string,\n depositorPubkey: string,\n): string {\n const signedPsbt = Psbt.fromHex(signedPsbtHex);\n const input = signedPsbt.data.inputs[0];\n\n if (!input) {\n throw new Error(\"PegIn PSBT has no inputs\");\n }\n\n // Non-finalized PSBT — extract from tapScriptSig\n if (input.tapScriptSig && input.tapScriptSig.length > 0) {\n const depositorPubkeyBytes = Buffer.from(\n hexToUint8Array(depositorPubkey),\n );\n\n for (const sigEntry of input.tapScriptSig) {\n if (sigEntry.pubkey.equals(depositorPubkeyBytes)) {\n return extractSchnorrSig(sigEntry.signature);\n }\n }\n\n throw new Error(\n `No PegIn input signature found for depositor pubkey: ${depositorPubkey}`,\n );\n }\n\n // Finalized PSBT — the witness stack order depends on the wallet's finalizer,\n // so we cannot reliably pick the depositor's signature by position. Require\n // the non-finalized tapScriptSig path which identifies signatures by pubkey.\n if (input.finalScriptWitness && input.finalScriptWitness.length > 0) {\n throw new Error(\n \"PegIn input PSBT is already finalized. Cannot reliably extract the \" +\n \"depositor signature from the witness stack. Ensure the wallet returns \" +\n \"a non-finalized PSBT with tapScriptSig entries.\",\n );\n }\n\n throw new Error(\n \"No tapScriptSig or finalScriptWitness found in signed PegIn input PSBT\",\n );\n}\n\n/**\n * Finalize a signed PegIn input PSBT and return the depositor-signed transaction hex.\n *\n * The default tapscript finalizer builds the full witness stack [sig, script, controlBlock]\n * that vaultd requires when verifying the depositor signature on-chain.\n *\n * @param signedPsbtHex - Non-finalized signed PSBT hex (returned by wallet with autoFinalized: false)\n * @returns Depositor-signed PegIn transaction hex with full taproot witness stack\n */\nexport function finalizePeginInputPsbt(signedPsbtHex: string): string {\n const psbt = Psbt.fromHex(signedPsbtHex);\n\n // Some wallets (UniSat, OKX) ignore autoFinalized: false and return\n // already-finalized PSBTs. finalizeAllInputs() throws in that case,\n // so fall back to verifying the wallet already finalized all inputs.\n try {\n psbt.finalizeAllInputs();\n } catch (e) {\n const allFinalized = psbt.data.inputs.every(\n (inp) => inp.finalScriptWitness || inp.finalScriptSig,\n );\n if (!allFinalized) {\n throw new Error(\n `PSBT finalization failed and wallet did not auto-finalize: ${e}`,\n );\n }\n }\n\n return psbt.extractTransaction().toHex();\n}\n\n/**\n * Extract and validate a 64-byte Schnorr signature, stripping sighash flag if present.\n * Accepts 64-byte sigs (implicit SIGHASH_DEFAULT) and 65-byte sigs with\n * SIGHASH_ALL (0x01). Rejects all other sighash types including 0x00, which\n * is consensus-invalid per BIP-342 when explicitly appended.\n * @internal\n */\nexport function extractSchnorrSig(sig: Uint8Array): string {\n if (sig.length === 64) {\n return uint8ArrayToHex(new Uint8Array(sig));\n }\n if (sig.length === 65) {\n const sighashByte = sig[64];\n // Only accept SIGHASH_ALL (0x01). Per BIP-342, SIGHASH_DEFAULT is signaled\n // by omitting the sighash byte (64-byte sig). A 65-byte sig with byte 64\n // set to 0x00 is consensus-invalid: Bitcoin Core rejects it with\n // SCRIPT_ERR_SCHNORR_SIG_HASHTYPE. Accepting 0x00 here would let\n // extractPeginInputSignature succeed (stripping the byte) while\n // finalizePeginInputPsbt passes the raw 65-byte sig into the witness,\n // producing a BTC transaction that can never confirm.\n if (sighashByte !== Transaction.SIGHASH_ALL) {\n throw new Error(\n `Unexpected sighash type 0x${sighashByte.toString(16).padStart(2, \"0\")} in PegIn input signature. ` +\n `Expected SIGHASH_DEFAULT (64-byte sig) or SIGHASH_ALL (0x01).`,\n );\n }\n return uint8ArrayToHex(new Uint8Array(sig.subarray(0, 64)));\n }\n throw new Error(`Unexpected PegIn input signature length: ${sig.length}`);\n}\n\n","/**\n * Depositor Payout PSBT Builder\n *\n * Builds unsigned PSBTs for the depositor's own Payout transaction\n * (depositor-as-claimer path). The depositor signs input 0 using the\n * payout taproot script from WasmPeginPayoutConnector (PegIn vault UTXO).\n *\n * Input 0 spends PegIn:0 (the vault UTXO) — the same connector used for\n * VP/VK payout signing. The VP verifies this signature using the\n * PeginPayoutConnector's payout script.\n *\n * @module primitives/psbt/depositorPayout\n * @see btc-vault crates/vault/src/sign.rs — verify_depositor_signature / get_payout_tap_leaf_hash\n */\n\nimport {\n type PayoutConnectorParams,\n getPeginPayoutScriptInfo,\n tapInternalPubkey,\n} from \"@babylonlabs-io/babylon-tbv-rust-wasm\";\nimport { Buffer } from \"buffer\";\nimport { Psbt, Transaction } from \"bitcoinjs-lib\";\n\nimport {\n TAPSCRIPT_LEAF_VERSION,\n hexToUint8Array,\n stripHexPrefix,\n} from \"../utils/bitcoin\";\n\n/**\n * Parameters for building a depositor Payout PSBT\n */\nexport interface DepositorPayoutParams {\n /** Payout transaction hex (unsigned) from VP */\n payoutTxHex: string;\n /** Prevouts for all inputs [{script_pubkey, value}] from VP */\n prevouts: Array<{ script_pubkey: string; value: number }>;\n /** Parameters for the PeginPayout connector (depositor, VP, VKs, UCs, timelock) */\n connectorParams: PayoutConnectorParams;\n}\n\n/**\n * Build unsigned depositor Payout PSBT.\n *\n * The depositor's payout transaction has 2 inputs:\n * - Input 0: PegIn:0 (vault UTXO) — depositor signs using PeginPayoutConnector payout script\n * - Input 1: Assert:0 — NOT signed by depositor\n *\n * @param params - Depositor payout parameters\n * @returns Unsigned PSBT hex ready for signing\n */\nexport async function buildDepositorPayoutPsbt(\n params: DepositorPayoutParams,\n): Promise<string> {\n const payoutTxHex = stripHexPrefix(params.payoutTxHex);\n const payoutTx = Transaction.fromHex(payoutTxHex);\n\n // Get payout script and control block from WASM (PeginPayoutConnector)\n const { payoutScript, payoutControlBlock } = await getPeginPayoutScriptInfo(\n params.connectorParams,\n );\n const scriptBytes = hexToUint8Array(payoutScript);\n const controlBlock = hexToUint8Array(payoutControlBlock);\n\n const psbt = new Psbt();\n psbt.setVersion(payoutTx.version);\n psbt.setLocktime(payoutTx.locktime);\n\n // Add all inputs - depositor signs input 0 only\n for (let i = 0; i < payoutTx.ins.length; i++) {\n const input = payoutTx.ins[i];\n const prevout = params.prevouts[i];\n\n if (!prevout) {\n throw new Error(`Missing prevout data for input ${i}`);\n }\n\n const inputData: Parameters<typeof psbt.addInput>[0] = {\n hash: input.hash,\n index: input.index,\n sequence: input.sequence,\n witnessUtxo: {\n script: Buffer.from(hexToUint8Array(stripHexPrefix(prevout.script_pubkey))),\n value: prevout.value,\n },\n };\n\n // Input 0: depositor signs using taproot script path\n if (i === 0) {\n inputData.tapLeafScript = [\n {\n leafVersion: TAPSCRIPT_LEAF_VERSION,\n script: Buffer.from(scriptBytes),\n controlBlock: Buffer.from(controlBlock),\n },\n ];\n inputData.tapInternalKey = Buffer.from(tapInternalPubkey);\n }\n\n psbt.addInput(inputData);\n }\n\n // Add outputs\n for (const output of payoutTx.outs) {\n psbt.addOutput({\n script: output.script,\n value: output.value,\n });\n }\n\n return psbt.toHex();\n}\n\n","/**\n * NoPayout PSBT Builder\n *\n * Builds unsigned PSBTs for the depositor's NoPayout transaction\n * (depositor-as-claimer path, per challenger). The depositor signs input 0\n * using the NoPayout taproot script from WasmAssertPayoutNoPayoutConnector.\n *\n * @module primitives/psbt/noPayout\n * @see btc-vault crates/vault/docs/btc-transactions-spec.md — Assert output 0 NoPayout connector\n */\n\nimport {\n type AssertPayoutNoPayoutConnectorParams,\n getAssertNoPayoutScriptInfo,\n tapInternalPubkey,\n} from \"@babylonlabs-io/babylon-tbv-rust-wasm\";\nimport { Buffer } from \"buffer\";\nimport { Psbt, Transaction } from \"bitcoinjs-lib\";\n\nimport {\n TAPSCRIPT_LEAF_VERSION,\n hexToUint8Array,\n stripHexPrefix,\n} from \"../utils/bitcoin\";\n\n/**\n * Parameters for building a NoPayout PSBT\n */\nexport interface NoPayoutParams {\n /** NoPayout transaction hex (unsigned) from VP */\n noPayoutTxHex: string;\n /** Challenger's x-only public key (hex encoded) */\n challengerPubkey: string;\n /** Prevouts for all inputs [{script_pubkey, value}] from VP */\n prevouts: Array<{ script_pubkey: string; value: number }>;\n /** Parameters for the Assert Payout/NoPayout connector */\n connectorParams: AssertPayoutNoPayoutConnectorParams;\n}\n\n/**\n * Build unsigned NoPayout PSBT.\n *\n * The NoPayout transaction is specific to each challenger.\n * Input 0 is the one the depositor signs using the NoPayout taproot script path.\n *\n * @param params - NoPayout parameters\n * @returns Unsigned PSBT hex ready for signing\n */\nexport async function buildNoPayoutPsbt(\n params: NoPayoutParams,\n): Promise<string> {\n const noPayoutTxHex = stripHexPrefix(params.noPayoutTxHex);\n const noPayoutTx = Transaction.fromHex(noPayoutTxHex);\n\n // Get NoPayout script and control block for this challenger\n const { noPayoutScript, noPayoutControlBlock } =\n await getAssertNoPayoutScriptInfo(\n params.connectorParams,\n params.challengerPubkey,\n );\n\n const scriptBytes = hexToUint8Array(noPayoutScript);\n const controlBlockBytes = hexToUint8Array(noPayoutControlBlock);\n\n const psbt = new Psbt();\n psbt.setVersion(noPayoutTx.version);\n psbt.setLocktime(noPayoutTx.locktime);\n\n // Add all inputs - depositor signs input 0 only\n for (let i = 0; i < noPayoutTx.ins.length; i++) {\n const input = noPayoutTx.ins[i];\n const prevout = params.prevouts[i];\n\n if (!prevout) {\n throw new Error(`Missing prevout data for input ${i}`);\n }\n\n const inputData: Parameters<typeof psbt.addInput>[0] = {\n hash: input.hash,\n index: input.index,\n sequence: input.sequence,\n witnessUtxo: {\n script: Buffer.from(hexToUint8Array(stripHexPrefix(prevout.script_pubkey))),\n value: prevout.value,\n },\n };\n\n // Input 0: depositor signs using taproot script path\n if (i === 0) {\n inputData.tapLeafScript = [\n {\n leafVersion: TAPSCRIPT_LEAF_VERSION,\n script: Buffer.from(scriptBytes),\n controlBlock: Buffer.from(controlBlockBytes),\n },\n ];\n inputData.tapInternalKey = Buffer.from(tapInternalPubkey);\n }\n\n psbt.addInput(inputData);\n }\n\n // Add outputs\n for (const output of noPayoutTx.outs) {\n psbt.addOutput({\n script: output.script,\n value: output.value,\n });\n }\n\n return psbt.toHex();\n}\n","/**\n * ChallengeAssert PSBT Builder\n *\n * Builds an unsigned PSBT for a ChallengeAssert transaction\n * (depositor-as-claimer path, per challenger). The ChallengeAssert tx has\n * NUM_UTXOS_FOR_CHALLENGE_ASSERT (3) inputs, each spending a different Assert\n * output segment. The depositor signs ALL inputs, each with its own taproot\n * script derived from the per-segment connector params.\n *\n * @module primitives/psbt/challengeAssert\n * @see btc-vault crates/vault/docs/btc-transactions-spec.md — ChallengeAssert connector (NUM_UTXOS_FOR_CHALLENGE_ASSERT=3)\n */\n\nimport {\n type ChallengeAssertConnectorParams,\n getChallengeAssertScriptInfo,\n tapInternalPubkey,\n} from \"@babylonlabs-io/babylon-tbv-rust-wasm\";\nimport { Buffer } from \"buffer\";\nimport { Psbt, Transaction } from \"bitcoinjs-lib\";\n\nimport {\n TAPSCRIPT_LEAF_VERSION,\n hexToUint8Array,\n stripHexPrefix,\n} from \"../utils/bitcoin\";\n\n/**\n * Parameters for building a ChallengeAssert PSBT\n */\nexport interface ChallengeAssertParams {\n /** ChallengeAssert transaction hex (unsigned) from VP */\n challengeAssertTxHex: string;\n /** Prevouts for all inputs [{script_pubkey, value}] from VP (flat, one per input) */\n prevouts: Array<{ script_pubkey: string; value: number }>;\n /** Per-input connector params (one per input/segment, determines the taproot script) */\n connectorParamsPerInput: ChallengeAssertConnectorParams[];\n}\n\n/**\n * Build unsigned ChallengeAssert PSBT.\n *\n * The ChallengeAssert transaction has 3 inputs (one per Assert output segment).\n * Each input has its own taproot script derived from its connector params.\n * The depositor signs all inputs.\n *\n * @param params - ChallengeAssert parameters\n * @returns Unsigned PSBT hex ready for signing\n */\nexport async function buildChallengeAssertPsbt(\n params: ChallengeAssertParams,\n): Promise<string> {\n const challengeAssertTxHex = stripHexPrefix(params.challengeAssertTxHex);\n const challengeAssertTx = Transaction.fromHex(challengeAssertTxHex);\n\n if (params.connectorParamsPerInput.length !== challengeAssertTx.ins.length) {\n throw new Error(\n `Expected ${challengeAssertTx.ins.length} connector params, got ${params.connectorParamsPerInput.length}`,\n );\n }\n\n // Get script and control block for each input from WASM\n const scriptInfos = await Promise.all(\n params.connectorParamsPerInput.map((cp) => getChallengeAssertScriptInfo(cp)),\n );\n\n const psbt = new Psbt();\n psbt.setVersion(challengeAssertTx.version);\n psbt.setLocktime(challengeAssertTx.locktime);\n\n // Add all inputs — depositor signs every input\n for (let i = 0; i < challengeAssertTx.ins.length; i++) {\n const input = challengeAssertTx.ins[i];\n const prevout = params.prevouts[i];\n\n if (!prevout) {\n throw new Error(`Missing prevout data for input ${i}`);\n }\n\n const { script, controlBlock } = scriptInfos[i];\n const scriptBytes = hexToUint8Array(script);\n const controlBlockBytes = hexToUint8Array(controlBlock);\n\n psbt.addInput({\n hash: input.hash,\n index: input.index,\n sequence: input.sequence,\n witnessUtxo: {\n script: Buffer.from(hexToUint8Array(stripHexPrefix(prevout.script_pubkey))),\n value: prevout.value,\n },\n tapLeafScript: [\n {\n leafVersion: TAPSCRIPT_LEAF_VERSION,\n script: Buffer.from(scriptBytes),\n controlBlock: Buffer.from(controlBlockBytes),\n },\n ],\n tapInternalKey: Buffer.from(tapInternalPubkey),\n });\n }\n\n // Add outputs\n for (const output of challengeAssertTx.outs) {\n psbt.addOutput({\n script: output.script,\n value: output.value,\n });\n }\n\n return psbt.toHex();\n}\n"],"names":["normalizeKey","key","processPublicKeyToXOnly","computeNumLocalChallengers","vaultProviderPubkey","vaultKeeperPubkeys","depositorPubkey","localSet","vk","buildPrePeginPsbt","params","result","createPrePeginTransaction","totalOutputValue","parseUnfundedWasmTransaction","sum","o","buildPeginTxFromFundedPrePegin","buildPeginTxFromPrePegin","buildPeginInputPsbt","peginTxHex","stripHexPrefix","fundedPrePeginTxHex","htlcConnector","getPrePeginHtlcConnectorInfo","peginTx","Transaction","prePeginTx","peginInput","prePeginTxid","peginInputTxid","uint8ArrayToHex","htlcOutput","hashlockScript","hexToUint8Array","hashlockControlBlock","psbt","Psbt","TAPSCRIPT_LEAF_VERSION","Buffer","tapInternalPubkey","output","extractPeginInputSignature","signedPsbtHex","input","depositorPubkeyBytes","sigEntry","extractSchnorrSig","finalizePeginInputPsbt","e","inp","sig","sighashByte","buildDepositorPayoutPsbt","payoutTxHex","payoutTx","payoutScript","payoutControlBlock","getPeginPayoutScriptInfo","scriptBytes","controlBlock","prevout","inputData","buildNoPayoutPsbt","noPayoutTxHex","noPayoutTx","noPayoutScript","noPayoutControlBlock","getAssertNoPayoutScriptInfo","controlBlockBytes","buildChallengeAssertPsbt","challengeAssertTxHex","challengeAssertTx","scriptInfos","cp","getChallengeAssertScriptInfo","i","script"],"mappings":";;;;;AAeA,SAASA,EAAaC,GAAqB;AACzC,SAAOC,EAAwBD,CAAG,EAAE,YAAA;AACtC;AAgBO,SAASE,EACdC,GACAC,GACAC,GACQ;AACR,QAAMC,wBAAe,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strict";const n=require("./bitcoin-CNnPFU6Y.cjs"),h=require("@babylonlabs-io/babylon-tbv-rust-wasm"),b=require("./fundPeginTransaction-DxNOeyNI.cjs"),a=require("buffer"),p=require("bitcoinjs-lib");function x(e){return n.processPublicKeyToXOnly(e).toLowerCase()}function k(e,t,r){const u=new Set;u.add(x(e));for(const o of t)u.add(x(o));return u.delete(x(r)),u.size}async function v(e){const t=await h.createPrePeginTransaction({depositorPubkey:e.depositorPubkey,vaultProviderPubkey:e.vaultProviderPubkey,vaultKeeperPubkeys:e.vaultKeeperPubkeys,universalChallengerPubkeys:e.universalChallengerPubkeys,hashlocks:[...e.hashlocks],timelockRefund:e.timelockRefund,pegInAmounts:[...e.pegInAmounts],feeRate:e.feeRate,numLocalChallengers:e.numLocalChallengers,councilQuorum:e.councilQuorum,councilSize:e.councilSize,network:e.network}),u=b.parseUnfundedWasmTransaction(t.txHex).outputs.reduce((o,i)=>o+BigInt(i.value),0n);return{psbtHex:t.txHex,totalOutputValue:u,htlcValues:t.htlcValues,htlcScriptPubKeys:t.htlcScriptPubKeys,htlcAddresses:t.htlcAddresses,peginAmounts:t.peginAmounts,depositorClaimValue:t.depositorClaimValue}}async function S(e){const t=await h.buildPeginTxFromPrePegin({depositorPubkey:e.prePeginParams.depositorPubkey,vaultProviderPubkey:e.prePeginParams.vaultProviderPubkey,vaultKeeperPubkeys:e.prePeginParams.vaultKeeperPubkeys,universalChallengerPubkeys:e.prePeginParams.universalChallengerPubkeys,hashlocks:[...e.prePeginParams.hashlocks],timelockRefund:e.prePeginParams.timelockRefund,pegInAmounts:[...e.prePeginParams.pegInAmounts],feeRate:e.prePeginParams.feeRate,numLocalChallengers:e.prePeginParams.numLocalChallengers,councilQuorum:e.prePeginParams.councilQuorum,councilSize:e.prePeginParams.councilSize,network:e.prePeginParams.network},e.timelockPegin,e.fundedPrePeginTxHex,e.htlcVout);return{txHex:t.txHex,txid:t.txid,vaultScriptPubKey:t.vaultScriptPubKey,vaultValue:t.vaultValue}}async function T(e){const t=n.stripHexPrefix(e.peginTxHex),r=n.stripHexPrefix(e.fundedPrePeginTxHex),u=await h.getPrePeginHtlcConnectorInfo({depositorPubkey:e.depositorPubkey,vaultProviderPubkey:e.vaultProviderPubkey,vaultKeeperPubkeys:e.vaultKeeperPubkeys,universalChallengerPubkeys:e.universalChallengerPubkeys,hashlock:e.hashlock,timelockRefund:e.timelockRefund,network:e.network}),o=p.Transaction.fromHex(t),i=p.Transaction.fromHex(r);if(o.ins.length!==1)throw new Error(`PegIn transaction must have exactly 1 input, got ${o.ins.length}`);const l=o.ins[0],c=i.getId(),s=n.uint8ArrayToHex(new Uint8Array(l.hash).slice().reverse());if(s!==c)throw new Error(`PegIn input does not reference the Pre-PegIn transaction. Expected ${c}, got ${s}`);const P=i.outs[l.index];if(!P)throw new Error(`Pre-PegIn output ${l.index} not found (Pre-PegIn has ${i.outs.length} outputs)`);const f=n.hexToUint8Array(u.hashlockScript),g=n.hexToUint8Array(u.hashlockControlBlock),d=new p.Psbt;d.setVersion(o.version),d.setLocktime(o.locktime),d.addInput({hash:l.hash,index:l.index,sequence:l.sequence,witnessUtxo:{script:P.script,value:P.value},tapLeafScript:[{leafVersion:n.TAPSCRIPT_LEAF_VERSION,script:a.Buffer.from(f),controlBlock:a.Buffer.from(g)}],tapInternalKey:a.Buffer.from(h.tapInternalPubkey)});for(const y of o.outs)d.addOutput({script:y.script,value:y.value});return{psbtHex:d.toHex()}}function w(e,t){const u=p.Psbt.fromHex(e).data.inputs[0];if(!u)throw new Error("PegIn PSBT has no inputs");if(u.tapScriptSig&&u.tapScriptSig.length>0){const o=a.Buffer.from(n.hexToUint8Array(t));for(const i of u.tapScriptSig)if(i.pubkey.equals(o))return A(i.signature);throw new Error(`No PegIn input signature found for depositor pubkey: ${t}`)}throw u.finalScriptWitness&&u.finalScriptWitness.length>0?new Error("PegIn input PSBT is already finalized. Cannot reliably extract the depositor signature from the witness stack. Ensure the wallet returns a non-finalized PSBT with tapScriptSig entries."):new Error("No tapScriptSig or finalScriptWitness found in signed PegIn input PSBT")}function I(e){const t=p.Psbt.fromHex(e);try{t.finalizeAllInputs()}catch(r){if(!t.data.inputs.every(o=>o.finalScriptWitness||o.finalScriptSig))throw new Error(`PSBT finalization failed and wallet did not auto-finalize: ${r}`)}return t.extractTransaction().toHex()}function A(e){if(e.length===64)return n.uint8ArrayToHex(new Uint8Array(e));if(e.length===65){const t=e[64];if(t!==p.Transaction.SIGHASH_ALL)throw new Error(`Unexpected sighash type 0x${t.toString(16).padStart(2,"0")} in PegIn input signature. Expected SIGHASH_DEFAULT (64-byte sig) or SIGHASH_ALL (0x01).`);return n.uint8ArrayToHex(new Uint8Array(e.subarray(0,64)))}throw new Error(`Unexpected PegIn input signature length: ${e.length}`)}async function H(e){const t=n.stripHexPrefix(e.payoutTxHex),r=p.Transaction.fromHex(t),{payoutScript:u,payoutControlBlock:o}=await h.getPeginPayoutScriptInfo(e.connectorParams),i=n.hexToUint8Array(u),l=n.hexToUint8Array(o),c=new p.Psbt;c.setVersion(r.version),c.setLocktime(r.locktime);for(let s=0;s<r.ins.length;s++){const P=r.ins[s],f=e.prevouts[s];if(!f)throw new Error(`Missing prevout data for input ${s}`);const g={hash:P.hash,index:P.index,sequence:P.sequence,witnessUtxo:{script:a.Buffer.from(n.hexToUint8Array(n.stripHexPrefix(f.script_pubkey))),value:f.value}};s===0&&(g.tapLeafScript=[{leafVersion:n.TAPSCRIPT_LEAF_VERSION,script:a.Buffer.from(i),controlBlock:a.Buffer.from(l)}],g.tapInternalKey=a.Buffer.from(h.tapInternalPubkey)),c.addInput(g)}for(const s of r.outs)c.addOutput({script:s.script,value:s.value});return c.toHex()}async function m(e){const t=n.stripHexPrefix(e.noPayoutTxHex),r=p.Transaction.fromHex(t),{noPayoutScript:u,noPayoutControlBlock:o}=await h.getAssertNoPayoutScriptInfo(e.connectorParams,e.challengerPubkey),i=n.hexToUint8Array(u),l=n.hexToUint8Array(o),c=new p.Psbt;c.setVersion(r.version),c.setLocktime(r.locktime);for(let s=0;s<r.ins.length;s++){const P=r.ins[s],f=e.prevouts[s];if(!f)throw new Error(`Missing prevout data for input ${s}`);const g={hash:P.hash,index:P.index,sequence:P.sequence,witnessUtxo:{script:a.Buffer.from(n.hexToUint8Array(n.stripHexPrefix(f.script_pubkey))),value:f.value}};s===0&&(g.tapLeafScript=[{leafVersion:n.TAPSCRIPT_LEAF_VERSION,script:a.Buffer.from(i),controlBlock:a.Buffer.from(l)}],g.tapInternalKey=a.Buffer.from(h.tapInternalPubkey)),c.addInput(g)}for(const s of r.outs)c.addOutput({script:s.script,value:s.value});return c.toHex()}async function B(e){const t=n.stripHexPrefix(e.challengeAssertTxHex),r=p.Transaction.fromHex(t);if(e.connectorParamsPerInput.length!==r.ins.length)throw new Error(`Expected ${r.ins.length} connector params, got ${e.connectorParamsPerInput.length}`);const u=await Promise.all(e.connectorParamsPerInput.map(i=>h.getChallengeAssertScriptInfo(i))),o=new p.Psbt;o.setVersion(r.version),o.setLocktime(r.locktime);for(let i=0;i<r.ins.length;i++){const l=r.ins[i],c=e.prevouts[i];if(!c)throw new Error(`Missing prevout data for input ${i}`);const{script:s,controlBlock:P}=u[i],f=n.hexToUint8Array(s),g=n.hexToUint8Array(P);o.addInput({hash:l.hash,index:l.index,sequence:l.sequence,witnessUtxo:{script:a.Buffer.from(n.hexToUint8Array(n.stripHexPrefix(c.script_pubkey))),value:c.value},tapLeafScript:[{leafVersion:n.TAPSCRIPT_LEAF_VERSION,script:a.Buffer.from(f),controlBlock:a.Buffer.from(g)}],tapInternalKey:a.Buffer.from(h.tapInternalPubkey)})}for(const i of r.outs)o.addOutput({script:i.script,value:i.value});return o.toHex()}exports.buildChallengeAssertPsbt=B;exports.buildDepositorPayoutPsbt=H;exports.buildNoPayoutPsbt=m;exports.buildPeginInputPsbt=T;exports.buildPeginTxFromFundedPrePegin=S;exports.buildPrePeginPsbt=v;exports.computeNumLocalChallengers=k;exports.extractPeginInputSignature=w;exports.finalizePeginInputPsbt=I;
|
|
2
|
+
//# sourceMappingURL=challengeAssert-X7V3Ik_Q.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"challengeAssert-X7V3Ik_Q.cjs","sources":["../src/tbv/core/primitives/challengers.ts","../src/tbv/core/primitives/psbt/pegin.ts","../src/tbv/core/primitives/psbt/peginInput.ts","../src/tbv/core/primitives/psbt/depositorPayout.ts","../src/tbv/core/primitives/psbt/noPayout.ts","../src/tbv/core/primitives/psbt/challengeAssert.ts"],"sourcesContent":["/**\n * Challenger counting utilities.\n *\n * Used for UI-level validation (e.g. computing minimum deposit amounts)\n * where the depositor's identity is known. The transaction builders use\n * `vaultKeeperBtcPubkeys.length` to match the VP's current validation.\n */\n\nimport { processPublicKeyToXOnly } from \"./utils/bitcoin\";\n\n/**\n * Normalize a public key to lowercase x-only hex for reliable comparison.\n *\n * Handles `0x` prefixes, compressed (33-byte), and uncompressed (65-byte) keys.\n */\nfunction normalizeKey(key: string): string {\n return processPublicKeyToXOnly(key).toLowerCase();\n}\n\n/**\n * Compute the number of local challengers for a vault.\n *\n * Mirrors the VP's `compute_num_challengers()` logic:\n * local challengers = {vault_provider} ∪ {vault_keepers} − {depositor}\n *\n * Keys are normalized to x-only lowercase hex before comparison, so\n * `0x`-prefixed, compressed, or mixed-case keys are handled correctly.\n *\n * @param vaultProviderPubkey - Vault provider BTC public key\n * @param vaultKeeperPubkeys - Vault keeper BTC public keys\n * @param depositorPubkey - Depositor (claimer) BTC public key\n * @returns Number of local challengers\n */\nexport function computeNumLocalChallengers(\n vaultProviderPubkey: string,\n vaultKeeperPubkeys: string[],\n depositorPubkey: string,\n): number {\n const localSet = new Set<string>();\n localSet.add(normalizeKey(vaultProviderPubkey));\n for (const vk of vaultKeeperPubkeys) {\n localSet.add(normalizeKey(vk));\n }\n localSet.delete(normalizeKey(depositorPubkey));\n return localSet.size;\n}\n","/**\n * Pre-PegIn PSBT Builder Primitive\n *\n * This module provides pure functions for building unfunded Pre-PegIn transactions\n * and deriving PegIn transactions from them, using the WASM implementation from\n * @babylonlabs-io/babylon-tbv-rust-wasm.\n *\n * Pre-PegIn Flow:\n * 1. buildPrePeginPsbt() — creates unfunded Pre-PegIn tx (HTLC output)\n * 2. [caller funds Pre-PegIn tx and computes txid]\n * 3. buildPeginTxFromFundedPrePegin() — derives PegIn tx spending the HTLC\n * 4. buildPeginInputPsbt() — PSBT for depositor to sign PegIn HTLC leaf 0 input\n *\n * @module primitives/psbt/pegin\n */\n\nimport {\n createPrePeginTransaction,\n buildPeginTxFromPrePegin,\n type Network,\n} from \"@babylonlabs-io/babylon-tbv-rust-wasm\";\n\nimport { parseUnfundedWasmTransaction } from \"../../utils/transaction/fundPeginTransaction\";\n\n/**\n * Parameters for building an unfunded Pre-PegIn PSBT\n */\nexport interface PrePeginParams {\n /** Depositor's BTC public key (x-only, 64-char hex without 0x prefix) */\n depositorPubkey: string;\n /** Vault provider's BTC public key (x-only, 64-char hex) */\n vaultProviderPubkey: string;\n /** Array of vault keeper BTC public keys (x-only, 64-char hex) */\n vaultKeeperPubkeys: string[];\n /** Array of universal challenger BTC public keys (x-only, 64-char hex) */\n universalChallengerPubkeys: string[];\n /** SHA256 hash commitment(s) (64 hex chars = 32 bytes each) */\n hashlocks: readonly string[];\n /** CSV timelock in blocks for the HTLC refund path */\n timelockRefund: number;\n /** Amounts to peg in (satoshis), one per deposit */\n pegInAmounts: readonly bigint[];\n /** Fee rate in sat/vB from contract offchain params */\n feeRate: bigint;\n /** Number of local challengers (from contract params) */\n numLocalChallengers: number;\n /** M in M-of-N council multisig (from contract params) */\n councilQuorum: number;\n /** N in M-of-N council multisig (from contract params) */\n councilSize: number;\n /** Bitcoin network */\n network: Network;\n}\n\n/**\n * Result of building an unfunded Pre-PegIn transaction\n */\nexport interface PrePeginPsbtResult {\n /**\n * Unfunded transaction hex (no inputs, HTLC output + CPFP anchor).\n *\n * The caller is responsible for:\n * - Selecting UTXOs covering totalOutputValue + network fees\n * - Funding the transaction (add inputs and change output)\n * - Calling buildPeginTxFromFundedPrePegin() with the funded tx hex\n */\n psbtHex: string;\n /** Sum of all unfunded outputs (HTLC + CPFP anchor) — use this for UTXO selection */\n totalOutputValue: bigint;\n /** HTLC output values in satoshis, one per deposit (each includes peginAmount + depositorClaimValue + minPeginFee) */\n htlcValues: readonly bigint[];\n /** HTLC output scriptPubKeys (hex encoded), one per deposit */\n htlcScriptPubKeys: readonly string[];\n /** HTLC Taproot addresses, one per deposit */\n htlcAddresses: readonly string[];\n /** Pegin amounts in satoshis, one per deposit */\n peginAmounts: readonly bigint[];\n /** Depositor claim value computed by WASM from contract parameters */\n depositorClaimValue: bigint;\n}\n\n/**\n * Parameters for building the PegIn transaction from a funded Pre-PegIn tx\n */\nexport interface BuildPeginTxParams {\n /** Same PrePeginParams used to create the Pre-PegIn transaction */\n prePeginParams: PrePeginParams;\n /** CSV timelock in blocks for the PegIn vault output */\n timelockPegin: number;\n /** Hex-encoded funded Pre-PegIn transaction */\n fundedPrePeginTxHex: string;\n /** Index of the HTLC output to spend */\n htlcVout: number;\n}\n\n/**\n * Result of building the PegIn transaction\n */\nexport interface PeginTxResult {\n /** PegIn transaction hex (1 input spending HTLC, 1 vault output) */\n txHex: string;\n /** PegIn transaction ID */\n txid: string;\n /** Vault output scriptPubKey (hex encoded) */\n vaultScriptPubKey: string;\n /** Vault output value in satoshis */\n vaultValue: bigint;\n}\n\n/**\n * Build unfunded Pre-PegIn transaction using WASM.\n *\n * Creates a Bitcoin transaction template with no inputs, an HTLC output, and a\n * CPFP anchor output. The HTLC value is computed internally from the contract\n * parameters — the caller does not need to compute depositorClaimValue separately.\n *\n * @param params - Pre-PegIn parameters\n * @returns Unfunded Pre-PegIn transaction details with HTLC output information\n * @throws If WASM initialization fails or parameters are invalid\n */\nexport async function buildPrePeginPsbt(\n params: PrePeginParams,\n): Promise<PrePeginPsbtResult> {\n const result = await createPrePeginTransaction({\n depositorPubkey: params.depositorPubkey,\n vaultProviderPubkey: params.vaultProviderPubkey,\n vaultKeeperPubkeys: params.vaultKeeperPubkeys,\n universalChallengerPubkeys: params.universalChallengerPubkeys,\n hashlocks: [...params.hashlocks],\n timelockRefund: params.timelockRefund,\n pegInAmounts: [...params.pegInAmounts],\n feeRate: params.feeRate,\n numLocalChallengers: params.numLocalChallengers,\n councilQuorum: params.councilQuorum,\n councilSize: params.councilSize,\n network: params.network,\n });\n\n // Parse the unfunded tx to sum all output values (HTLC + CPFP anchor).\n // This is the amount UTXOs must cover before adding network fees.\n const parsed = parseUnfundedWasmTransaction(result.txHex);\n const totalOutputValue = parsed.outputs.reduce(\n (sum, o) => sum + BigInt(o.value),\n 0n,\n );\n\n return {\n psbtHex: result.txHex,\n totalOutputValue,\n htlcValues: result.htlcValues,\n htlcScriptPubKeys: result.htlcScriptPubKeys,\n htlcAddresses: result.htlcAddresses,\n peginAmounts: result.peginAmounts,\n depositorClaimValue: result.depositorClaimValue,\n };\n}\n\n/**\n * Build the PegIn transaction from a funded Pre-PegIn transaction.\n *\n * The PegIn transaction spends the Pre-PegIn HTLC output at htlcVout via the\n * hashlock + all-party script (leaf 0).\n *\n * @param params - Build parameters including Pre-PegIn params and funded tx hex\n * @returns PegIn transaction details\n * @throws If WASM initialization fails or parameters are invalid\n */\nexport async function buildPeginTxFromFundedPrePegin(\n params: BuildPeginTxParams,\n): Promise<PeginTxResult> {\n const result = await buildPeginTxFromPrePegin(\n {\n depositorPubkey: params.prePeginParams.depositorPubkey,\n vaultProviderPubkey: params.prePeginParams.vaultProviderPubkey,\n vaultKeeperPubkeys: params.prePeginParams.vaultKeeperPubkeys,\n universalChallengerPubkeys: params.prePeginParams.universalChallengerPubkeys,\n hashlocks: [...params.prePeginParams.hashlocks],\n timelockRefund: params.prePeginParams.timelockRefund,\n pegInAmounts: [...params.prePeginParams.pegInAmounts],\n feeRate: params.prePeginParams.feeRate,\n numLocalChallengers: params.prePeginParams.numLocalChallengers,\n councilQuorum: params.prePeginParams.councilQuorum,\n councilSize: params.prePeginParams.councilSize,\n network: params.prePeginParams.network,\n },\n params.timelockPegin,\n params.fundedPrePeginTxHex,\n params.htlcVout,\n );\n\n return {\n txHex: result.txHex,\n txid: result.txid,\n vaultScriptPubKey: result.vaultScriptPubKey,\n vaultValue: result.vaultValue,\n };\n}\n","/**\n * PegIn Input PSBT Builder\n *\n * Builds the PSBT for the depositor to sign the PegIn transaction's HTLC input\n * (Pre-PegIn HTLC leaf 0 — the hashlock + all-party script).\n *\n * This is the \"Sign Pegin transaction HTLC leaf 0 input\" step in the pre-pegin\n * flow. The depositor signs input 0 of the PegIn transaction,\n * which spends output 0 of the funded Pre-PegIn transaction via script-path.\n *\n * @module primitives/psbt/peginInput\n */\n\nimport {\n getPrePeginHtlcConnectorInfo,\n tapInternalPubkey,\n type Network,\n} from \"@babylonlabs-io/babylon-tbv-rust-wasm\";\nimport { Buffer } from \"buffer\";\nimport { Psbt, Transaction } from \"bitcoinjs-lib\";\nimport { TAPSCRIPT_LEAF_VERSION, hexToUint8Array, stripHexPrefix, uint8ArrayToHex } from \"../utils/bitcoin\";\n\n/**\n * Parameters for building the PegIn input PSBT\n */\nexport interface BuildPeginInputPsbtParams {\n /**\n * PegIn transaction hex (1 input spending Pre-PegIn HTLC output 0).\n * Returned by buildPeginTxFromFundedPrePegin().\n */\n peginTxHex: string;\n /**\n * Funded Pre-PegIn transaction hex.\n * Used to look up the HTLC output that the PegIn input spends.\n */\n fundedPrePeginTxHex: string;\n /** Depositor's BTC public key (x-only, 64-char hex) */\n depositorPubkey: string;\n /** Vault provider's BTC public key (x-only, 64-char hex) */\n vaultProviderPubkey: string;\n /** Vault keeper BTC public keys (x-only, 64-char hex) */\n vaultKeeperPubkeys: string[];\n /** Universal challenger BTC public keys (x-only, 64-char hex) */\n universalChallengerPubkeys: string[];\n /** SHA256 hash commitment (64 hex chars = 32 bytes) */\n hashlock: string;\n /** CSV timelock in blocks for the HTLC refund path */\n timelockRefund: number;\n /** Bitcoin network */\n network: Network;\n}\n\n/**\n * Result of building the PegIn input PSBT\n */\nexport interface BuildPeginInputPsbtResult {\n /** PSBT hex for the depositor to sign */\n psbtHex: string;\n}\n\n/**\n * Build PSBT for depositor to sign the PegIn transaction's HTLC leaf 0 input.\n *\n * The PegIn transaction spends the Pre-PegIn HTLC output (output 0) via the\n * hashlock + all-party script (leaf 0). The depositor provides one of the required\n * signatures; the vault provider and keepers provide theirs separately via the\n * signPeginInput RPC.\n *\n * The PSBT uses Taproot script-path spending:\n * - witnessUtxo: the Pre-PegIn HTLC output\n * - tapLeafScript: hashlock leaf script + control block\n * - tapInternalKey: NUMS unspendable key (BIP-341 nothing-up-my-sleeve)\n *\n * @param params - PegIn input PSBT parameters\n * @returns PSBT hex ready for depositor signing\n * @throws If PegIn tx does not have exactly 1 input\n * @throws If PegIn input does not reference the Pre-PegIn HTLC output\n * @throws If Pre-PegIn tx output 0 is not found\n */\nexport async function buildPeginInputPsbt(\n params: BuildPeginInputPsbtParams,\n): Promise<BuildPeginInputPsbtResult> {\n const peginTxHex = stripHexPrefix(params.peginTxHex);\n const fundedPrePeginTxHex = stripHexPrefix(params.fundedPrePeginTxHex);\n\n const htlcConnector = await getPrePeginHtlcConnectorInfo({\n depositorPubkey: params.depositorPubkey,\n vaultProviderPubkey: params.vaultProviderPubkey,\n vaultKeeperPubkeys: params.vaultKeeperPubkeys,\n universalChallengerPubkeys: params.universalChallengerPubkeys,\n hashlock: params.hashlock,\n timelockRefund: params.timelockRefund,\n network: params.network,\n });\n\n const peginTx = Transaction.fromHex(peginTxHex);\n const prePeginTx = Transaction.fromHex(fundedPrePeginTxHex);\n\n if (peginTx.ins.length !== 1) {\n throw new Error(\n `PegIn transaction must have exactly 1 input, got ${peginTx.ins.length}`,\n );\n }\n\n const peginInput = peginTx.ins[0];\n\n // Verify PegIn input 0 spends Pre-PegIn output 0\n const prePeginTxid = prePeginTx.getId();\n const peginInputTxid = uint8ArrayToHex(\n new Uint8Array(peginInput.hash).slice().reverse(),\n );\n\n if (peginInputTxid !== prePeginTxid) {\n throw new Error(\n `PegIn input does not reference the Pre-PegIn transaction. ` +\n `Expected ${prePeginTxid}, got ${peginInputTxid}`,\n );\n }\n\n const htlcOutput = prePeginTx.outs[peginInput.index];\n if (!htlcOutput) {\n throw new Error(\n `Pre-PegIn output ${peginInput.index} not found ` +\n `(Pre-PegIn has ${prePeginTx.outs.length} outputs)`,\n );\n }\n\n const hashlockScript = hexToUint8Array(htlcConnector.hashlockScript);\n const hashlockControlBlock = hexToUint8Array(htlcConnector.hashlockControlBlock);\n\n const psbt = new Psbt();\n psbt.setVersion(peginTx.version);\n psbt.setLocktime(peginTx.locktime);\n\n // Input 0: PegIn input spending Pre-PegIn HTLC output 0 via hashlock leaf (leaf 0).\n // The depositor signs using Taproot script-path spending.\n psbt.addInput({\n hash: peginInput.hash,\n index: peginInput.index,\n sequence: peginInput.sequence,\n witnessUtxo: {\n script: htlcOutput.script,\n value: htlcOutput.value,\n },\n tapLeafScript: [\n {\n leafVersion: TAPSCRIPT_LEAF_VERSION,\n script: Buffer.from(hashlockScript),\n controlBlock: Buffer.from(hashlockControlBlock),\n },\n ],\n tapInternalKey: Buffer.from(tapInternalPubkey),\n // sighashType omitted — defaults to SIGHASH_DEFAULT (0x00) for Taproot\n });\n\n for (const output of peginTx.outs) {\n psbt.addOutput({\n script: output.script,\n value: output.value,\n });\n }\n\n return { psbtHex: psbt.toHex() };\n}\n\n/**\n * Extract the depositor's Schnorr signature from a signed PegIn input PSBT.\n *\n * Supports both non-finalized PSBTs (tapScriptSig) and finalized PSBTs (witness).\n *\n * @param signedPsbtHex - Signed PSBT hex\n * @param depositorPubkey - Depositor's x-only public key (64-char hex)\n * @returns 64-byte Schnorr signature (128 hex chars, no sighash flag)\n * @throws If no signature is found for the depositor's key\n */\nexport function extractPeginInputSignature(\n signedPsbtHex: string,\n depositorPubkey: string,\n): string {\n const signedPsbt = Psbt.fromHex(signedPsbtHex);\n const input = signedPsbt.data.inputs[0];\n\n if (!input) {\n throw new Error(\"PegIn PSBT has no inputs\");\n }\n\n // Non-finalized PSBT — extract from tapScriptSig\n if (input.tapScriptSig && input.tapScriptSig.length > 0) {\n const depositorPubkeyBytes = Buffer.from(\n hexToUint8Array(depositorPubkey),\n );\n\n for (const sigEntry of input.tapScriptSig) {\n if (sigEntry.pubkey.equals(depositorPubkeyBytes)) {\n return extractSchnorrSig(sigEntry.signature);\n }\n }\n\n throw new Error(\n `No PegIn input signature found for depositor pubkey: ${depositorPubkey}`,\n );\n }\n\n // Finalized PSBT — the witness stack order depends on the wallet's finalizer,\n // so we cannot reliably pick the depositor's signature by position. Require\n // the non-finalized tapScriptSig path which identifies signatures by pubkey.\n if (input.finalScriptWitness && input.finalScriptWitness.length > 0) {\n throw new Error(\n \"PegIn input PSBT is already finalized. Cannot reliably extract the \" +\n \"depositor signature from the witness stack. Ensure the wallet returns \" +\n \"a non-finalized PSBT with tapScriptSig entries.\",\n );\n }\n\n throw new Error(\n \"No tapScriptSig or finalScriptWitness found in signed PegIn input PSBT\",\n );\n}\n\n/**\n * Finalize a signed PegIn input PSBT and return the depositor-signed transaction hex.\n *\n * The default tapscript finalizer builds the full witness stack [sig, script, controlBlock]\n * that vaultd requires when verifying the depositor signature on-chain.\n *\n * @param signedPsbtHex - Non-finalized signed PSBT hex (returned by wallet with autoFinalized: false)\n * @returns Depositor-signed PegIn transaction hex with full taproot witness stack\n */\nexport function finalizePeginInputPsbt(signedPsbtHex: string): string {\n const psbt = Psbt.fromHex(signedPsbtHex);\n\n // Some wallets (UniSat, OKX) ignore autoFinalized: false and return\n // already-finalized PSBTs. finalizeAllInputs() throws in that case,\n // so fall back to verifying the wallet already finalized all inputs.\n try {\n psbt.finalizeAllInputs();\n } catch (e) {\n const allFinalized = psbt.data.inputs.every(\n (inp) => inp.finalScriptWitness || inp.finalScriptSig,\n );\n if (!allFinalized) {\n throw new Error(\n `PSBT finalization failed and wallet did not auto-finalize: ${e}`,\n );\n }\n }\n\n return psbt.extractTransaction().toHex();\n}\n\n/**\n * Extract and validate a 64-byte Schnorr signature, stripping sighash flag if present.\n * Accepts 64-byte sigs (implicit SIGHASH_DEFAULT) and 65-byte sigs with\n * SIGHASH_ALL (0x01). Rejects all other sighash types including 0x00, which\n * is consensus-invalid per BIP-342 when explicitly appended.\n * @internal\n */\nexport function extractSchnorrSig(sig: Uint8Array): string {\n if (sig.length === 64) {\n return uint8ArrayToHex(new Uint8Array(sig));\n }\n if (sig.length === 65) {\n const sighashByte = sig[64];\n // Only accept SIGHASH_ALL (0x01). Per BIP-342, SIGHASH_DEFAULT is signaled\n // by omitting the sighash byte (64-byte sig). A 65-byte sig with byte 64\n // set to 0x00 is consensus-invalid: Bitcoin Core rejects it with\n // SCRIPT_ERR_SCHNORR_SIG_HASHTYPE. Accepting 0x00 here would let\n // extractPeginInputSignature succeed (stripping the byte) while\n // finalizePeginInputPsbt passes the raw 65-byte sig into the witness,\n // producing a BTC transaction that can never confirm.\n if (sighashByte !== Transaction.SIGHASH_ALL) {\n throw new Error(\n `Unexpected sighash type 0x${sighashByte.toString(16).padStart(2, \"0\")} in PegIn input signature. ` +\n `Expected SIGHASH_DEFAULT (64-byte sig) or SIGHASH_ALL (0x01).`,\n );\n }\n return uint8ArrayToHex(new Uint8Array(sig.subarray(0, 64)));\n }\n throw new Error(`Unexpected PegIn input signature length: ${sig.length}`);\n}\n\n","/**\n * Depositor Payout PSBT Builder\n *\n * Builds unsigned PSBTs for the depositor's own Payout transaction\n * (depositor-as-claimer path). The depositor signs input 0 using the\n * payout taproot script from WasmPeginPayoutConnector (PegIn vault UTXO).\n *\n * Input 0 spends PegIn:0 (the vault UTXO) — the same connector used for\n * VP/VK payout signing. The VP verifies this signature using the\n * PeginPayoutConnector's payout script.\n *\n * @module primitives/psbt/depositorPayout\n * @see btc-vault crates/vault/src/sign.rs — verify_depositor_signature / get_payout_tap_leaf_hash\n */\n\nimport {\n type PayoutConnectorParams,\n getPeginPayoutScriptInfo,\n tapInternalPubkey,\n} from \"@babylonlabs-io/babylon-tbv-rust-wasm\";\nimport { Buffer } from \"buffer\";\nimport { Psbt, Transaction } from \"bitcoinjs-lib\";\n\nimport {\n TAPSCRIPT_LEAF_VERSION,\n hexToUint8Array,\n stripHexPrefix,\n} from \"../utils/bitcoin\";\n\n/**\n * Parameters for building a depositor Payout PSBT\n */\nexport interface DepositorPayoutParams {\n /** Payout transaction hex (unsigned) from VP */\n payoutTxHex: string;\n /** Prevouts for all inputs [{script_pubkey, value}] from VP */\n prevouts: Array<{ script_pubkey: string; value: number }>;\n /** Parameters for the PeginPayout connector (depositor, VP, VKs, UCs, timelock) */\n connectorParams: PayoutConnectorParams;\n}\n\n/**\n * Build unsigned depositor Payout PSBT.\n *\n * The depositor's payout transaction has 2 inputs:\n * - Input 0: PegIn:0 (vault UTXO) — depositor signs using PeginPayoutConnector payout script\n * - Input 1: Assert:0 — NOT signed by depositor\n *\n * @param params - Depositor payout parameters\n * @returns Unsigned PSBT hex ready for signing\n */\nexport async function buildDepositorPayoutPsbt(\n params: DepositorPayoutParams,\n): Promise<string> {\n const payoutTxHex = stripHexPrefix(params.payoutTxHex);\n const payoutTx = Transaction.fromHex(payoutTxHex);\n\n // Get payout script and control block from WASM (PeginPayoutConnector)\n const { payoutScript, payoutControlBlock } = await getPeginPayoutScriptInfo(\n params.connectorParams,\n );\n const scriptBytes = hexToUint8Array(payoutScript);\n const controlBlock = hexToUint8Array(payoutControlBlock);\n\n const psbt = new Psbt();\n psbt.setVersion(payoutTx.version);\n psbt.setLocktime(payoutTx.locktime);\n\n // Add all inputs - depositor signs input 0 only\n for (let i = 0; i < payoutTx.ins.length; i++) {\n const input = payoutTx.ins[i];\n const prevout = params.prevouts[i];\n\n if (!prevout) {\n throw new Error(`Missing prevout data for input ${i}`);\n }\n\n const inputData: Parameters<typeof psbt.addInput>[0] = {\n hash: input.hash,\n index: input.index,\n sequence: input.sequence,\n witnessUtxo: {\n script: Buffer.from(hexToUint8Array(stripHexPrefix(prevout.script_pubkey))),\n value: prevout.value,\n },\n };\n\n // Input 0: depositor signs using taproot script path\n if (i === 0) {\n inputData.tapLeafScript = [\n {\n leafVersion: TAPSCRIPT_LEAF_VERSION,\n script: Buffer.from(scriptBytes),\n controlBlock: Buffer.from(controlBlock),\n },\n ];\n inputData.tapInternalKey = Buffer.from(tapInternalPubkey);\n }\n\n psbt.addInput(inputData);\n }\n\n // Add outputs\n for (const output of payoutTx.outs) {\n psbt.addOutput({\n script: output.script,\n value: output.value,\n });\n }\n\n return psbt.toHex();\n}\n\n","/**\n * NoPayout PSBT Builder\n *\n * Builds unsigned PSBTs for the depositor's NoPayout transaction\n * (depositor-as-claimer path, per challenger). The depositor signs input 0\n * using the NoPayout taproot script from WasmAssertPayoutNoPayoutConnector.\n *\n * @module primitives/psbt/noPayout\n * @see btc-vault crates/vault/docs/btc-transactions-spec.md — Assert output 0 NoPayout connector\n */\n\nimport {\n type AssertPayoutNoPayoutConnectorParams,\n getAssertNoPayoutScriptInfo,\n tapInternalPubkey,\n} from \"@babylonlabs-io/babylon-tbv-rust-wasm\";\nimport { Buffer } from \"buffer\";\nimport { Psbt, Transaction } from \"bitcoinjs-lib\";\n\nimport {\n TAPSCRIPT_LEAF_VERSION,\n hexToUint8Array,\n stripHexPrefix,\n} from \"../utils/bitcoin\";\n\n/**\n * Parameters for building a NoPayout PSBT\n */\nexport interface NoPayoutParams {\n /** NoPayout transaction hex (unsigned) from VP */\n noPayoutTxHex: string;\n /** Challenger's x-only public key (hex encoded) */\n challengerPubkey: string;\n /** Prevouts for all inputs [{script_pubkey, value}] from VP */\n prevouts: Array<{ script_pubkey: string; value: number }>;\n /** Parameters for the Assert Payout/NoPayout connector */\n connectorParams: AssertPayoutNoPayoutConnectorParams;\n}\n\n/**\n * Build unsigned NoPayout PSBT.\n *\n * The NoPayout transaction is specific to each challenger.\n * Input 0 is the one the depositor signs using the NoPayout taproot script path.\n *\n * @param params - NoPayout parameters\n * @returns Unsigned PSBT hex ready for signing\n */\nexport async function buildNoPayoutPsbt(\n params: NoPayoutParams,\n): Promise<string> {\n const noPayoutTxHex = stripHexPrefix(params.noPayoutTxHex);\n const noPayoutTx = Transaction.fromHex(noPayoutTxHex);\n\n // Get NoPayout script and control block for this challenger\n const { noPayoutScript, noPayoutControlBlock } =\n await getAssertNoPayoutScriptInfo(\n params.connectorParams,\n params.challengerPubkey,\n );\n\n const scriptBytes = hexToUint8Array(noPayoutScript);\n const controlBlockBytes = hexToUint8Array(noPayoutControlBlock);\n\n const psbt = new Psbt();\n psbt.setVersion(noPayoutTx.version);\n psbt.setLocktime(noPayoutTx.locktime);\n\n // Add all inputs - depositor signs input 0 only\n for (let i = 0; i < noPayoutTx.ins.length; i++) {\n const input = noPayoutTx.ins[i];\n const prevout = params.prevouts[i];\n\n if (!prevout) {\n throw new Error(`Missing prevout data for input ${i}`);\n }\n\n const inputData: Parameters<typeof psbt.addInput>[0] = {\n hash: input.hash,\n index: input.index,\n sequence: input.sequence,\n witnessUtxo: {\n script: Buffer.from(hexToUint8Array(stripHexPrefix(prevout.script_pubkey))),\n value: prevout.value,\n },\n };\n\n // Input 0: depositor signs using taproot script path\n if (i === 0) {\n inputData.tapLeafScript = [\n {\n leafVersion: TAPSCRIPT_LEAF_VERSION,\n script: Buffer.from(scriptBytes),\n controlBlock: Buffer.from(controlBlockBytes),\n },\n ];\n inputData.tapInternalKey = Buffer.from(tapInternalPubkey);\n }\n\n psbt.addInput(inputData);\n }\n\n // Add outputs\n for (const output of noPayoutTx.outs) {\n psbt.addOutput({\n script: output.script,\n value: output.value,\n });\n }\n\n return psbt.toHex();\n}\n","/**\n * ChallengeAssert PSBT Builder\n *\n * Builds an unsigned PSBT for a ChallengeAssert transaction\n * (depositor-as-claimer path, per challenger). The ChallengeAssert tx has\n * NUM_UTXOS_FOR_CHALLENGE_ASSERT (3) inputs, each spending a different Assert\n * output segment. The depositor signs ALL inputs, each with its own taproot\n * script derived from the per-segment connector params.\n *\n * @module primitives/psbt/challengeAssert\n * @see btc-vault crates/vault/docs/btc-transactions-spec.md — ChallengeAssert connector (NUM_UTXOS_FOR_CHALLENGE_ASSERT=3)\n */\n\nimport {\n type ChallengeAssertConnectorParams,\n getChallengeAssertScriptInfo,\n tapInternalPubkey,\n} from \"@babylonlabs-io/babylon-tbv-rust-wasm\";\nimport { Buffer } from \"buffer\";\nimport { Psbt, Transaction } from \"bitcoinjs-lib\";\n\nimport {\n TAPSCRIPT_LEAF_VERSION,\n hexToUint8Array,\n stripHexPrefix,\n} from \"../utils/bitcoin\";\n\n/**\n * Parameters for building a ChallengeAssert PSBT\n */\nexport interface ChallengeAssertParams {\n /** ChallengeAssert transaction hex (unsigned) from VP */\n challengeAssertTxHex: string;\n /** Prevouts for all inputs [{script_pubkey, value}] from VP (flat, one per input) */\n prevouts: Array<{ script_pubkey: string; value: number }>;\n /** Per-input connector params (one per input/segment, determines the taproot script) */\n connectorParamsPerInput: ChallengeAssertConnectorParams[];\n}\n\n/**\n * Build unsigned ChallengeAssert PSBT.\n *\n * The ChallengeAssert transaction has 3 inputs (one per Assert output segment).\n * Each input has its own taproot script derived from its connector params.\n * The depositor signs all inputs.\n *\n * @param params - ChallengeAssert parameters\n * @returns Unsigned PSBT hex ready for signing\n */\nexport async function buildChallengeAssertPsbt(\n params: ChallengeAssertParams,\n): Promise<string> {\n const challengeAssertTxHex = stripHexPrefix(params.challengeAssertTxHex);\n const challengeAssertTx = Transaction.fromHex(challengeAssertTxHex);\n\n if (params.connectorParamsPerInput.length !== challengeAssertTx.ins.length) {\n throw new Error(\n `Expected ${challengeAssertTx.ins.length} connector params, got ${params.connectorParamsPerInput.length}`,\n );\n }\n\n // Get script and control block for each input from WASM\n const scriptInfos = await Promise.all(\n params.connectorParamsPerInput.map((cp) => getChallengeAssertScriptInfo(cp)),\n );\n\n const psbt = new Psbt();\n psbt.setVersion(challengeAssertTx.version);\n psbt.setLocktime(challengeAssertTx.locktime);\n\n // Add all inputs — depositor signs every input\n for (let i = 0; i < challengeAssertTx.ins.length; i++) {\n const input = challengeAssertTx.ins[i];\n const prevout = params.prevouts[i];\n\n if (!prevout) {\n throw new Error(`Missing prevout data for input ${i}`);\n }\n\n const { script, controlBlock } = scriptInfos[i];\n const scriptBytes = hexToUint8Array(script);\n const controlBlockBytes = hexToUint8Array(controlBlock);\n\n psbt.addInput({\n hash: input.hash,\n index: input.index,\n sequence: input.sequence,\n witnessUtxo: {\n script: Buffer.from(hexToUint8Array(stripHexPrefix(prevout.script_pubkey))),\n value: prevout.value,\n },\n tapLeafScript: [\n {\n leafVersion: TAPSCRIPT_LEAF_VERSION,\n script: Buffer.from(scriptBytes),\n controlBlock: Buffer.from(controlBlockBytes),\n },\n ],\n tapInternalKey: Buffer.from(tapInternalPubkey),\n });\n }\n\n // Add outputs\n for (const output of challengeAssertTx.outs) {\n psbt.addOutput({\n script: output.script,\n value: output.value,\n });\n }\n\n return psbt.toHex();\n}\n"],"names":["normalizeKey","key","processPublicKeyToXOnly","computeNumLocalChallengers","vaultProviderPubkey","vaultKeeperPubkeys","depositorPubkey","localSet","vk","buildPrePeginPsbt","params","result","createPrePeginTransaction","totalOutputValue","parseUnfundedWasmTransaction","sum","o","buildPeginTxFromFundedPrePegin","buildPeginTxFromPrePegin","buildPeginInputPsbt","peginTxHex","stripHexPrefix","fundedPrePeginTxHex","htlcConnector","getPrePeginHtlcConnectorInfo","peginTx","Transaction","prePeginTx","peginInput","prePeginTxid","peginInputTxid","uint8ArrayToHex","htlcOutput","hashlockScript","hexToUint8Array","hashlockControlBlock","psbt","Psbt","TAPSCRIPT_LEAF_VERSION","Buffer","tapInternalPubkey","output","extractPeginInputSignature","signedPsbtHex","input","depositorPubkeyBytes","sigEntry","extractSchnorrSig","finalizePeginInputPsbt","e","inp","sig","sighashByte","buildDepositorPayoutPsbt","payoutTxHex","payoutTx","payoutScript","payoutControlBlock","getPeginPayoutScriptInfo","scriptBytes","controlBlock","i","prevout","inputData","buildNoPayoutPsbt","noPayoutTxHex","noPayoutTx","noPayoutScript","noPayoutControlBlock","getAssertNoPayoutScriptInfo","controlBlockBytes","buildChallengeAssertPsbt","challengeAssertTxHex","challengeAssertTx","scriptInfos","cp","getChallengeAssertScriptInfo","script"],"mappings":"0MAeA,SAASA,EAAaC,EAAqB,CACzC,OAAOC,EAAAA,wBAAwBD,CAAG,EAAE,YAAA,CACtC,CAgBO,SAASE,EACdC,EACAC,EACAC,EACQ,CACR,MAAMC,MAAe,IACrBA,EAAS,IAAIP,EAAaI,CAAmB,CAAC,EAC9C,UAAWI,KAAMH,EACfE,EAAS,IAAIP,EAAaQ,CAAE,CAAC,EAE/B,OAAAD,EAAS,OAAOP,EAAaM,CAAe,CAAC,EACtCC,EAAS,IAClB,CC2EA,eAAsBE,EACpBC,EAC6B,CAC7B,MAAMC,EAAS,MAAMC,4BAA0B,CAC7C,gBAAiBF,EAAO,gBACxB,oBAAqBA,EAAO,oBAC5B,mBAAoBA,EAAO,mBAC3B,2BAA4BA,EAAO,2BACnC,UAAW,CAAC,GAAGA,EAAO,SAAS,EAC/B,eAAgBA,EAAO,eACvB,aAAc,CAAC,GAAGA,EAAO,YAAY,EACrC,QAASA,EAAO,QAChB,oBAAqBA,EAAO,oBAC5B,cAAeA,EAAO,cACtB,YAAaA,EAAO,YACpB,QAASA,EAAO,OAAA,CACjB,EAKKG,EADSC,EAAAA,6BAA6BH,EAAO,KAAK,EACxB,QAAQ,OACtC,CAACI,EAAKC,IAAMD,EAAM,OAAOC,EAAE,KAAK,EAChC,EAAA,EAGF,MAAO,CACL,QAASL,EAAO,MAChB,iBAAAE,EACA,WAAYF,EAAO,WACnB,kBAAmBA,EAAO,kBAC1B,cAAeA,EAAO,cACtB,aAAcA,EAAO,aACrB,oBAAqBA,EAAO,mBAAA,CAEhC,CAYA,eAAsBM,EACpBP,EACwB,CACxB,MAAMC,EAAS,MAAMO,EAAAA,yBACnB,CACE,gBAAiBR,EAAO,eAAe,gBACvC,oBAAqBA,EAAO,eAAe,oBAC3C,mBAAoBA,EAAO,eAAe,mBAC1C,2BAA4BA,EAAO,eAAe,2BA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S,CAAC,EAEjC,GAAI,CAACoD,EACH,MAAM,IAAI,MAAM,kCAAkC,CAAC,EAAE,EAGvD,KAAM,CAAE,OAAAe,EAAQ,aAAAjB,GAAiBc,EAAY,CAAC,EACxCf,EAAczB,EAAAA,gBAAgB2C,CAAM,EACpCP,EAAoBpC,EAAAA,gBAAgB0B,CAAY,EAEtDxB,EAAK,SAAS,CACZ,KAAMQ,EAAM,KACZ,MAAOA,EAAM,MACb,SAAUA,EAAM,SAChB,YAAa,CACX,OAAQL,EAAAA,OAAO,KAAKL,EAAAA,gBAAgBb,EAAAA,eAAeyC,EAAQ,aAAa,CAAC,CAAC,EAC1E,MAAOA,EAAQ,KAAA,EAEjB,cAAe,CACb,CACE,YAAaxB,EAAAA,uBACb,OAAQC,EAAAA,OAAO,KAAKoB,CAAW,EAC/B,aAAcpB,EAAAA,OAAO,KAAK+B,CAAiB,CAAA,CAC7C,EAEF,eAAgB/B,EAAAA,OAAO,KAAKC,EAAAA,iBAAiB,CAAA,CAC9C,CACH,CAGA,UAAWC,KAAUgC,EAAkB,KACrCrC,EAAK,UAAU,CACb,OAAQK,EAAO,OACf,MAAOA,EAAO,KAAA,CACf,EAGH,OAAOL,EAAK,MAAA,CACd"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var Vt=Object.defineProperty;var Ft=(e,t,n)=>t in e?Vt(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var P=(e,t,n)=>Ft(e,typeof t!="symbol"?t+"":t,n);const M=require("bitcoinjs-lib"),ot=require("buffer"),B=require("viem"),Kt=require("./signing-cl-lowxV.cjs"),ut=require("@babylonlabs-io/babylon-tbv-rust-wasm"),R=require("./challengeAssert-nYlgeAI8.cjs"),k=require("./bitcoin-CNnPFU6Y.cjs"),ht=require("./validation-u8W7Lp2x.cjs"),$=require("./psbtInputFields-05ZrwRzf.cjs"),ft=require("./fundPeginTransaction-DxNOeyNI.cjs"),wt=require("./index-BAECy2oz.cjs"),O=require("./types-B-p4dhEH.cjs"),u=require("./buildAndBroadcastRefund-BDGGbGeK.cjs");function Dt(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const n in e)if(n!=="default"){const s=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,s.get?s:{enumerable:!0,get:()=>e[n]})}}return t.default=e,Object.freeze(t)}const Mt=Dt(M),C={"0x04aabf33":"Vault already exists: This Bitcoin transaction has already been registered. Please select different UTXOs or use a different amount to create a unique transaction.","0x4fec082d":"Script mismatch: The Bitcoin transaction's taproot output does not match the expected vault script. This may be caused by incorrect vault participants or key configuration.","0x6cc363a5":"Invalid BTC proof of possession: The signature could not be verified. Please ensure you're signing with the correct Bitcoin wallet.","0x6c3f2bf6":"Invalid BTC public key: The Bitcoin public key format is invalid.","0x2c5211c6":"Invalid amount: The deposit amount is invalid or below the minimum required.","0x0405f772":"Application not registered: The application controller is not registered in the system.","0x24e165cc":"Invalid provider status: The vault provider is not in a valid state to accept deposits.","0xd92e233d":"Zero address: One of the required addresses is the zero address.","0x65aa7007":"BTC key mismatch: The Bitcoin public key does not match the expected key.","0x82b42900":"Unauthorized: You must be the depositor or vault provider to submit this transaction.","0x8baa579f":"Invalid signature: The BTC proof of possession signature could not be verified.","0x2f9d01e9":"Invalid BTC transaction: The Bitcoin transaction format is invalid.","0x5a3c6b3e":"Vault provider not registered: The selected vault provider is not registered.","0x979f4518":"Invalid pegin fee: The ETH fee sent does not match the required amount. This may indicate a fee rate change during the transaction.","0x5fad9694":"This pre-pegin output has already been used to activate another vault.","0x7ed061c9":"This pegin transaction has already been used to activate another vault."};function N(e){if(!e||typeof e!="object")return;const t=e;if(typeof t.data=="string"&&t.data.startsWith("0x"))return t.data;if(typeof t.details=="string"&&t.details.startsWith("0x"))return t.details;let n=t.cause,s=0;const o=5;for(;n&&typeof n=="object"&&s<o;){const a=n;if(typeof a.data=="string"&&a.data.startsWith("0x"))return a.data;n=a.cause,s++}const i=(typeof t.message=="string"?t.message:"").match(/\b(0x[a-fA-F0-9]{8})\b/);if(i)return i[1]}function Nt(e){const t=N(e);if(t){const n=t.substring(0,10);return C[t]??C[n]}}function Xt(e){const t=N(e);if(t===void 0)return!1;const n=t.substring(0,10);return t in C||n in C}function S(e){console.error("[Contract Error] Raw error:",e);const t=N(e);if(console.error("[Contract Error] Extracted error data:",t),t){const s=t.substring(0,10),o=C[t]??C[s];if(o)throw console.error("[Contract Error] Known error:",o),new Error(o)}const n=(e==null?void 0:e.message)||"";if(n.includes("gas limit too high")||n.includes("21000000")||n.includes("Internal JSON-RPC error")){const s=t?` (error code: ${t})`:"";throw console.error("[Contract Error] Transaction rejected. Error code:",t,"Message:",n),new Error(`Transaction failed: The contract rejected this transaction${s}. Possible causes: (1) Vault already exists for this transaction, (2) Invalid signature, (3) Unauthorized caller. Please check your transaction parameters and try again.`)}throw e instanceof Error?(console.error("[Contract Error] Unhandled error:",e.message),e):new Error(`Contract call failed: ${String(e)}`)}const qt=0,jt=/^0x[0-9a-f]+$/i,Gt=/^[0-9a-f]+$/i,zt=/^[A-Za-z0-9+/]+={0,2}$/;function W(e){if(typeof e!="string"||e.length===0)throw new Error("BTC wallet returned empty public key");return k.processPublicKeyToXOnly(e).toLowerCase()}function Zt(e){if(typeof e!="string"||e.length===0)throw new Error("BTC wallet returned empty BIP-322 signature");if(e.startsWith("0x")||e.startsWith("0X")){if(!jt.test(e)||e.length<4||e.length%2!==0)throw new Error("BTC wallet returned malformed hex BIP-322 signature");return e.toLowerCase()}if(Gt.test(e)){if(e.length%2!==0)throw new Error("BTC wallet returned malformed hex BIP-322 signature");return`0x${e.toLowerCase()}`}if(!zt.test(e)||e.length%4!==0)throw new Error("BTC wallet returned malformed base64 BIP-322 signature");const t=ot.Buffer.from(e,"base64");if(t.length===0||t.toString("base64")!==e)throw new Error("BTC wallet returned malformed base64 BIP-322 signature");return`0x${t.toString("hex")}`}function Yt(e,t,n,s){const o=n==null?void 0:n[`${e}:${t}`];return o?Promise.resolve({txid:e,vout:t,value:o.value,scriptPubKey:o.scriptPubKey}):wt.getUtxoInfo(e,t,s)}const dt=12e4;class Jt{constructor(t){P(this,"config");this.config=t}async preparePegin(t){const n=await this.config.btcWallet.getPublicKeyHex(),s=W(n),o=k.stripHexPrefix(t.vaultProviderBtcPubkey),r=t.vaultKeeperBtcPubkeys.map(k.stripHexPrefix),i=t.universalChallengerBtcPubkeys.map(k.stripHexPrefix);if(t.hashlocks.length!==t.amounts.length)throw new Error(`hashlocks.length (${t.hashlocks.length}) must equal amounts.length (${t.amounts.length})`);if(t.hashlocks.length===0)throw new Error("hashlocks must contain at least one entry");const a=r.length,p={depositorPubkey:s,vaultProviderPubkey:o,vaultKeeperPubkeys:r,universalChallengerPubkeys:i,hashlocks:t.hashlocks,timelockRefund:t.timelockRefund,pegInAmounts:t.amounts,feeRate:t.protocolFeeRate,numLocalChallengers:a,councilQuorum:t.councilQuorum,councilSize:t.councilSize,network:this.config.btcNetwork},h=await R.buildPrePeginPsbt(p),c=$.selectUtxosForPegin([...t.availableUTXOs],h.totalOutputValue,t.mempoolFeeRate,ft.peginOutputCount(h.htlcValues.length)),y=k.getNetwork(this.config.btcNetwork),l=ft.fundPeginTransaction({unfundedTxHex:h.psbtHex,selectedUTXOs:c.selectedUTXOs,changeAddress:t.changeAddress,changeAmount:c.changeAmount,network:y}),x=k.stripHexPrefix($.calculateBtcTxHash(l)),g=[],w=[],T=[];for(let m=0;m<t.hashlocks.length;m++){const b=await R.buildPeginTxFromFundedPrePegin({prePeginParams:p,timelockPegin:t.timelockPegin,fundedPrePeginTxHex:l,htlcVout:m}),I=await R.buildPeginInputPsbt({peginTxHex:b.txHex,fundedPrePeginTxHex:l,depositorPubkey:s,vaultProviderPubkey:o,vaultKeeperPubkeys:r,universalChallengerPubkeys:i,hashlock:t.hashlocks[m],timelockRefund:t.timelockRefund,network:this.config.btcNetwork});g.push(b),w.push(I.psbtHex),T.push(Kt.createTaprootScriptPathSignOptions(n,1))}const d=await this.signPsbtsWithFallback(w,T),f=[];for(let m=0;m<d.length;m++){const b=R.extractPeginInputSignature(d[m],s),I=R.finalizePeginInputPsbt(d[m]);f.push({htlcVout:m,htlcValue:h.htlcValues[m],peginTxHex:I,peginTxid:g[m].txid,peginInputSignature:b,vaultScriptPubKey:g[m].vaultScriptPubKey})}return{fundedPrePeginTxHex:l,prePeginTxid:x,perVault:f,selectedUTXOs:c.selectedUTXOs,fee:c.fee,changeAmount:c.changeAmount}}async signPsbtsWithFallback(t,n){if(typeof this.config.btcWallet.signPsbts=="function"){const o=await this.config.btcWallet.signPsbts(t,n);if(o.length!==t.length)throw new Error(`Expected ${t.length} signed PSBTs but received ${o.length}`);return o}const s=[];for(let o=0;o<t.length;o++){const r=await this.config.btcWallet.signPsbt(t[o],n[o]);s.push(r)}return s}async signAndBroadcast(t){const{fundedPrePeginTxHex:n,depositorBtcPubkey:s}=t,o=n.startsWith("0x")?n.slice(2):n,r=M.Transaction.fromHex(o);if(r.ins.length===0)throw new Error("Transaction has no inputs");const i=new M.Psbt;i.setVersion(r.version),i.setLocktime(r.locktime);const a=ot.Buffer.from(W(s),"hex"),p=this.config.mempoolApiUrl,h=r.ins.map(f=>{const m=ot.Buffer.from(f.hash).reverse().toString("hex"),b=f.index;return Yt(m,b,t.localPrevouts,p).then(I=>({input:f,utxoData:I,txid:m,vout:b}))}),c=await Promise.all(h),y=c.reduce((f,m)=>f+BigInt(m.utxoData.value),0n),l=r.outs.reduce((f,m)=>f+BigInt(m.value),0n);if(y<l)throw new Error(`UTXO value mismatch: total input value (${y} sat) is less than total output value (${l} sat). This may indicate the mempool API returned manipulated UTXO data.`);const x=y-l;if(x>ht.MAX_REASONABLE_FEE_SATS)throw new Error(`Implied transaction fee (${x} sat) exceeds maximum reasonable fee (${ht.MAX_REASONABLE_FEE_SATS} sat). This may indicate manipulated UTXO data.`);for(const{input:f,utxoData:m,txid:b,vout:I}of c){const E=$.getPsbtInputFields({value:m.value,scriptPubKey:m.scriptPubKey},a);i.addInput({hash:f.hash,index:f.index,sequence:f.sequence,...E})}for(const f of r.outs)i.addOutput({script:f.script,value:f.value});const g=await this.config.btcWallet.signPsbt(i.toHex()),w=M.Psbt.fromHex(g);try{w.finalizeAllInputs()}catch(f){if(!w.data.inputs.every(b=>b.finalScriptWitness||b.finalScriptSig))throw new Error(`PSBT finalization failed and wallet did not auto-finalize: ${f}`)}const T=w.extractTransaction().toHex();return await wt.pushTx(T,p)}async registerPeginOnChain(t){const{unsignedPrePeginTx:n,depositorSignedPeginTx:s,vaultProvider:o,hashlock:r,htlcVout:i,depositorPayoutBtcAddress:a,depositorWotsPkHash:p,popSignature:h}=t;if(!this.config.ethWallet.account)throw new Error("Ethereum wallet account not found");const c=this.config.ethWallet.account.address;if(!B.isAddressEqual(h.depositorEthAddress,c))throw new Error(`Proof of possession was signed for ${h.depositorEthAddress} but the Ethereum wallet is currently connected to ${c}. Reconnect the original account or call signProofOfPossession() again.`);await this.assertPopMatchesBtcWallet(h);const y=h.btcPopSignature,l=k.ensureHexPrefix(h.depositorBtcPubkey),x=k.ensureHexPrefix(n),g=k.ensureHexPrefix(s),w=await this.resolvePayoutScriptPubKey(a),T=$.calculateBtcTxHash(g),d=await ut.deriveVaultId(k.stripHexPrefix(T),k.stripHexPrefix(c)),f=k.ensureHexPrefix(d);if(await this.checkVaultExists(f))throw new Error(`Vault already exists (ID: ${f}, peginTxHash: ${T}). Vault IDs are derived from the pegin transaction hash and depositor address. To create a new vault, use different UTXOs or a different amount to generate a unique transaction.`);const b=B.createPublicClient({chain:this.config.ethChain,transport:B.http()});let I;try{I=await b.readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:O.BTCVaultRegistryABI,functionName:"getPegInFee",args:[o]})}catch{throw new Error("Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct.")}const E=B.encodeFunctionData({abi:O.BTCVaultRegistryABI,functionName:"submitPeginRequest",args:[c,l,y,x,g,o,r,i,w,p]});let v;try{v=await b.estimateGas({to:this.config.vaultContracts.btcVaultRegistry,data:E,value:I,account:this.config.ethWallet.account.address})}catch(q){S(q)}let _;try{_=await this.config.ethWallet.sendTransaction({to:this.config.vaultContracts.btcVaultRegistry,data:E,value:I,account:this.config.ethWallet.account,chain:this.config.ethChain,gas:v})}catch(q){S(q)}const A=await b.waitForTransactionReceipt({hash:_,timeout:dt});return A.status==="reverted"&&S(new Error(`Transaction reverted. Hash: ${_}. Check the transaction on block explorer for details.`)),{ethTxHash:A.transactionHash,vaultId:f,peginTxHash:T}}async registerPeginBatchOnChain(t){const{vaultProvider:n,unsignedPrePeginTx:s,requests:o,popSignature:r}=t;if(o.length===0)throw new Error("Batch pegin requires at least one request");if(!this.config.ethWallet.account)throw new Error("Ethereum wallet account not found");const i=this.config.ethWallet.account.address;if(!B.isAddressEqual(r.depositorEthAddress,i))throw new Error(`Proof of possession was signed for ${r.depositorEthAddress} but the Ethereum wallet is currently connected to ${i}. Reconnect the original account or call signProofOfPossession() again.`);await this.assertPopMatchesBtcWallet(r);const a=r.btcPopSignature,p=[];for(const b of o)p.push(await this.resolvePayoutScriptPubKey(b.depositorPayoutBtcAddress));const h=[];for(const b of o){const I=k.ensureHexPrefix(b.depositorSignedPeginTx),E=$.calculateBtcTxHash(I),v=await ut.deriveVaultId(k.stripHexPrefix(E),k.stripHexPrefix(i)),_=k.ensureHexPrefix(v);if(await this.checkVaultExists(_))throw new Error(`Vault already exists (ID: ${_}, peginTxHash: ${E}). To create a new vault, use different UTXOs or a different amount.`);h.push({vaultId:_,peginTxHash:E})}const c=B.createPublicClient({chain:this.config.ethChain,transport:B.http()});let y;try{y=await c.readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:O.BTCVaultRegistryABI,functionName:"getPegInFee",args:[n]})}catch{throw new Error("Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct.")}const l=y*BigInt(o.length),x=k.ensureHexPrefix(r.depositorBtcPubkey),g=k.ensureHexPrefix(s),w=o.map((b,I)=>({depositorBtcPubKey:x,btcPopSignature:a,unsignedPrePeginTx:g,depositorSignedPeginTx:k.ensureHexPrefix(b.depositorSignedPeginTx),hashlock:b.hashlock,htlcVout:b.htlcVout,referralCode:qt,depositorPayoutBtcAddress:p[I],depositorWotsPkHash:b.depositorWotsPkHash})),T=B.encodeFunctionData({abi:O.BTCVaultRegistryABI,functionName:"submitPeginRequestBatch",args:[i,n,w]});let d;try{d=await c.estimateGas({to:this.config.vaultContracts.btcVaultRegistry,data:T,value:l,account:this.config.ethWallet.account.address})}catch(b){S(b)}let f;try{f=await this.config.ethWallet.sendTransaction({to:this.config.vaultContracts.btcVaultRegistry,data:T,value:l,account:this.config.ethWallet.account,chain:this.config.ethChain,gas:d})}catch(b){S(b)}const m=await c.waitForTransactionReceipt({hash:f,timeout:dt});return m.status==="reverted"&&S(new Error(`Batch transaction reverted. Hash: ${f}. Check the transaction on block explorer for details.`)),{ethTxHash:m.transactionHash,vaults:h}}async checkVaultExists(t){try{return(await B.createPublicClient({chain:this.config.ethChain,transport:B.http()}).readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:O.BTCVaultRegistryABI,functionName:"getBtcVaultBasicInfo",args:[t]}))[0]!==B.zeroAddress}catch{return!1}}async resolvePayoutScriptPubKey(t){let n;if(t)n=t;else{n=await this.config.btcWallet.getAddress();const o=await this.config.btcWallet.getPublicKeyHex();if(!k.isAddressFromPublicKey(n,o,this.config.btcNetwork))throw new Error("The BTC address from your wallet does not match the wallet's public key. Please ensure your wallet is using a supported address type (Taproot or Native SegWit).")}const s=k.getNetwork(this.config.btcNetwork);try{return`0x${Mt.address.toOutputScript(n,s).toString("hex")}`}catch{throw new Error(`Invalid BTC payout address: "${n}". Please provide a valid Bitcoin address for the ${this.config.btcNetwork} network.`)}}async signProofOfPossession(){if(!this.config.ethWallet.account)throw new Error("Ethereum wallet account not found");const t=this.config.ethWallet.account.address,n=W(await this.config.btcWallet.getPublicKeyHex()),s=this.config.vaultContracts.btcVaultRegistry,o=`${t.toLowerCase()}:${this.config.ethChain.id}:pegin:${s.toLowerCase()}`,r=await this.config.btcWallet.signMessage(o,"bip322-simple");return{btcPopSignature:Zt(r),depositorEthAddress:t,depositorBtcPubkey:n}}async assertPopMatchesBtcWallet(t){const n=W(await this.config.btcWallet.getPublicKeyHex()),s=W(t.depositorBtcPubkey);if(n!==s)throw new Error(`Proof of possession was signed with BTC pubkey ${s} but the BTC wallet is currently connected to ${n}. Reconnect the original wallet or call signProofOfPossession() again.`)}getNetwork(){return this.config.btcNetwork}getVaultContractAddress(){return this.config.vaultContracts.btcVaultRegistry}}class Pt{constructor(t,n){P(this,"oHash");P(this,"iHash");P(this,"blockLen");P(this,"outputLen");P(this,"finished",!1);P(this,"destroyed",!1);if(u.ahash(t),u.abytes(n,void 0,"key"),this.iHash=t.create(),typeof this.iHash.update!="function")throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;const s=this.blockLen,o=new Uint8Array(s);o.set(n.length>s?t.create().update(n).digest():n);for(let r=0;r<o.length;r++)o[r]^=54;this.iHash.update(o),this.oHash=t.create();for(let r=0;r<o.length;r++)o[r]^=106;this.oHash.update(o),u.clean(o)}update(t){return u.aexists(this),this.iHash.update(t),this}digestInto(t){u.aexists(this),u.abytes(t,this.outputLen,"output"),this.finished=!0,this.iHash.digestInto(t),this.oHash.update(t),this.oHash.digestInto(t),this.destroy()}digest(){const t=new Uint8Array(this.oHash.outputLen);return this.digestInto(t),t}_cloneInto(t){t||(t=Object.create(Object.getPrototypeOf(this),{}));const{oHash:n,iHash:s,finished:o,destroyed:r,blockLen:i,outputLen:a}=this;return t=t,t.finished=o,t.destroyed=r,t.blockLen=i,t.outputLen=a,t.oHash=n._cloneInto(t.oHash),t.iHash=s._cloneInto(t.iHash),t}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}}const X=(e,t,n)=>new Pt(e,t).update(n).digest();X.create=(e,t)=>new Pt(e,t);const Qt=Uint8Array.from([7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8]),kt=Uint8Array.from(new Array(16).fill(0).map((e,t)=>t)),te=kt.map(e=>(9*e+5)%16),Tt=(()=>{const n=[[kt],[te]];for(let s=0;s<4;s++)for(let o of n)o.push(o[s].map(r=>Qt[r]));return n})(),Et=Tt[0],It=Tt[1],Ht=[[11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8],[12,13,11,15,6,9,9,7,12,15,11,13,7,8,7,7],[13,15,14,11,7,7,6,8,13,14,13,12,5,5,6,9],[14,11,12,14,8,6,5,5,15,12,15,14,9,9,8,6],[15,12,13,13,9,5,8,6,14,11,12,11,8,6,5,5]].map(e=>Uint8Array.from(e)),ee=Et.map((e,t)=>e.map(n=>Ht[t][n])),ne=It.map((e,t)=>e.map(n=>Ht[t][n])),se=Uint32Array.from([0,1518500249,1859775393,2400959708,2840853838]),oe=Uint32Array.from([1352829926,1548603684,1836072691,2053994217,0]);function gt(e,t,n,s){return e===0?t^n^s:e===1?t&n|~t&s:e===2?(t|~n)^s:e===3?t&s|n&~s:t^(n|~s)}const V=new Uint32Array(16);class re extends u.HashMD{constructor(){super(64,20,8,!0);P(this,"h0",1732584193);P(this,"h1",-271733879);P(this,"h2",-1732584194);P(this,"h3",271733878);P(this,"h4",-1009589776)}get(){const{h0:n,h1:s,h2:o,h3:r,h4:i}=this;return[n,s,o,r,i]}set(n,s,o,r,i){this.h0=n|0,this.h1=s|0,this.h2=o|0,this.h3=r|0,this.h4=i|0}process(n,s){for(let g=0;g<16;g++,s+=4)V[g]=n.getUint32(s,!0);let o=this.h0|0,r=o,i=this.h1|0,a=i,p=this.h2|0,h=p,c=this.h3|0,y=c,l=this.h4|0,x=l;for(let g=0;g<5;g++){const w=4-g,T=se[g],d=oe[g],f=Et[g],m=It[g],b=ee[g],I=ne[g];for(let E=0;E<16;E++){const v=u.rotl(o+gt(g,i,p,c)+V[f[E]]+T,b[E])+l|0;o=l,l=c,c=u.rotl(p,10)|0,p=i,i=v}for(let E=0;E<16;E++){const v=u.rotl(r+gt(w,a,h,y)+V[m[E]]+d,I[E])+x|0;r=x,x=y,y=u.rotl(h,10)|0,h=a,a=v}}this.set(this.h1+p+y|0,this.h2+c+x|0,this.h3+l+r|0,this.h4+o+a|0,this.h0+i+h|0)}roundClean(){u.clean(V)}destroy(){this.destroyed=!0,u.clean(this.buffer),this.set(0,0,0,0,0)}}const vt=u.createHasher(()=>new re),ie=BigInt(0),U=BigInt(1),ae=BigInt(2),ce=BigInt(7),le=BigInt(256),ue=BigInt(113),Bt=[],_t=[],St=[];for(let e=0,t=U,n=1,s=0;e<24;e++){[n,s]=[s,(2*n+3*s)%5],Bt.push(2*(5*s+n)),_t.push((e+1)*(e+2)/2%64);let o=ie;for(let r=0;r<7;r++)t=(t<<U^(t>>ce)*ue)%le,t&ae&&(o^=U<<(U<<BigInt(r))-U);St.push(o)}const Ct=u.split(St,!0),he=Ct[0],fe=Ct[1],pt=(e,t,n)=>n>32?u.rotlBH(e,t,n):u.rotlSH(e,t,n),mt=(e,t,n)=>n>32?u.rotlBL(e,t,n):u.rotlSL(e,t,n);function de(e,t=24){const n=new Uint32Array(10);for(let s=24-t;s<24;s++){for(let i=0;i<10;i++)n[i]=e[i]^e[i+10]^e[i+20]^e[i+30]^e[i+40];for(let i=0;i<10;i+=2){const a=(i+8)%10,p=(i+2)%10,h=n[p],c=n[p+1],y=pt(h,c,1)^n[a],l=mt(h,c,1)^n[a+1];for(let x=0;x<50;x+=10)e[i+x]^=y,e[i+x+1]^=l}let o=e[2],r=e[3];for(let i=0;i<24;i++){const a=_t[i],p=pt(o,r,a),h=mt(o,r,a),c=Bt[i];o=e[c],r=e[c+1],e[c]=p,e[c+1]=h}for(let i=0;i<50;i+=10){for(let a=0;a<10;a++)n[a]=e[i+a];for(let a=0;a<10;a++)e[i+a]^=~n[(a+2)%10]&n[(a+4)%10]}e[0]^=he[s],e[1]^=fe[s]}u.clean(n)}class ct{constructor(t,n,s,o=!1,r=24){P(this,"state");P(this,"pos",0);P(this,"posOut",0);P(this,"finished",!1);P(this,"state32");P(this,"destroyed",!1);P(this,"blockLen");P(this,"suffix");P(this,"outputLen");P(this,"enableXOF",!1);P(this,"rounds");if(this.blockLen=t,this.suffix=n,this.outputLen=s,this.enableXOF=o,this.rounds=r,u.anumber(s,"outputLen"),!(0<t&&t<200))throw new Error("only keccak-f1600 function is supported");this.state=new Uint8Array(200),this.state32=u.u32(this.state)}clone(){return this._cloneInto()}keccak(){u.swap32IfBE(this.state32),de(this.state32,this.rounds),u.swap32IfBE(this.state32),this.posOut=0,this.pos=0}update(t){u.aexists(this),u.abytes(t);const{blockLen:n,state:s}=this,o=t.length;for(let r=0;r<o;){const i=Math.min(n-this.pos,o-r);for(let a=0;a<i;a++)s[this.pos++]^=t[r++];this.pos===n&&this.keccak()}return this}finish(){if(this.finished)return;this.finished=!0;const{state:t,suffix:n,pos:s,blockLen:o}=this;t[s]^=n,(n&128)!==0&&s===o-1&&this.keccak(),t[o-1]^=128,this.keccak()}writeInto(t){u.aexists(this,!1),u.abytes(t),this.finish();const n=this.state,{blockLen:s}=this;for(let o=0,r=t.length;o<r;){this.posOut>=s&&this.keccak();const i=Math.min(s-this.posOut,r-o);t.set(n.subarray(this.posOut,this.posOut+i),o),this.posOut+=i,o+=i}return t}xofInto(t){if(!this.enableXOF)throw new Error("XOF is not possible for this instance");return this.writeInto(t)}xof(t){return u.anumber(t),this.xofInto(new Uint8Array(t))}digestInto(t){if(u.aoutput(t,this),this.finished)throw new Error("digest() was already called");return this.writeInto(t),this.destroy(),t}digest(){return this.digestInto(new Uint8Array(this.outputLen))}destroy(){this.destroyed=!0,u.clean(this.state)}_cloneInto(t){const{blockLen:n,suffix:s,outputLen:o,rounds:r,enableXOF:i}=this;return t||(t=new ct(n,s,o,i,r)),t.state32.set(this.state32),t.pos=this.pos,t.posOut=this.posOut,t.finished=this.finished,t.rounds=r,t.suffix=s,t.outputLen=o,t.enableXOF=i,t.destroyed=this.destroyed,t}}const ge=(e,t,n,s={})=>u.createHasher(()=>new ct(t,e,n),s),At=ge(1,136,32);function pe(e,t,n,s){u.ahash(e);const o=u.checkOpts({dkLen:32,asyncTick:10},s),{c:r,dkLen:i,asyncTick:a}=o;if(u.anumber(r,"c"),u.anumber(i,"dkLen"),u.anumber(a,"asyncTick"),r<1)throw new Error("iterations (c) must be >= 1");const p=u.kdfInputToBytes(t,"password"),h=u.kdfInputToBytes(n,"salt"),c=new Uint8Array(i),y=X.create(e,p),l=y._cloneInto().update(h);return{c:r,dkLen:i,asyncTick:a,DK:c,PRF:y,PRFSalt:l}}function me(e,t,n,s,o){return e.destroy(),t.destroy(),s&&s.destroy(),u.clean(o),n}function ye(e,t,n,s){const{c:o,dkLen:r,DK:i,PRF:a,PRFSalt:p}=pe(e,t,n,s);let h;const c=new Uint8Array(4),y=u.createView(c),l=new Uint8Array(a.outputLen);for(let x=1,g=0;g<r;x++,g+=a.outputLen){const w=i.subarray(g,g+a.outputLen);y.setInt32(0,x,!1),(h=p._cloneInto(h)).update(c).digestInto(l),w.set(l.subarray(0,w.length));for(let T=1;T<o;T++){a._cloneInto(h).update(l).digestInto(l);for(let d=0;d<w.length;d++)w[d]^=l[d]}}return me(a,p,i,h,l)}function Rt(e){if(typeof e!="string")throw new TypeError("invalid mnemonic type: "+typeof e);return e.normalize("NFKD")}function be(e){const t=Rt(e),n=t.split(" ");if(![12,15,18,21,24].includes(n.length))throw new Error("Invalid mnemonic");return{nfkd:t,words:n}}const xe=e=>Rt("mnemonic"+e);function we(e,t=""){return ye(u.sha512,be(e).nfkd,xe(t),{c:2048,dkLen:64})}const Pe=508,yt=16,F=32,K=64,bt=5,ke=4;function L(...e){const t=e.reduce((o,r)=>o+r.length,0),n=new Uint8Array(t);let s=0;for(const o of e)n.set(o,s),s+=o.length;return n}function j(e){return new TextEncoder().encode(e)}function G(e){const t=new Uint8Array(ke);return new DataView(t.buffer).setUint32(0,e.length,!1),L(t,e)}function z(e,t){return X(u.sha512,e,t)}function xt(e){return vt(u.sha256(e))}const rt=e=>Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("");function $t(e){const t=we(e),n=new Uint8Array(t);return t.fill(0),n}async function Ot(e,t,n,s){if(e.length!==K)throw new Error(`WOTS seed must be ${K} bytes, got ${e.length}`);t=k.stripHexPrefix(t),n=k.stripHexPrefix(n);const o=e.slice(F,K),r=e.slice(0,F),i=[o,r];try{const a=L(G(j(t)),G(j(n)),G(j(s))),p=L(r,a);i.push(p);const h=z(o,p);i.push(h);const c=h.slice(0,F),y=h.slice(F,K);i.push(c,y);const l=[],x=[],g=[],w=[];let T=!1;try{for(let d=0;d<Pe;d++){const f=new Uint8Array(bt);f[0]=0,new DataView(f.buffer).setUint32(1,d,!1);const m=new Uint8Array(bt);m[0]=1,new DataView(m.buffer).setUint32(1,d,!1);const b=L(c,f),I=L(c,m),E=z(y,b),v=z(y,I);try{const _=E.slice(0,yt),A=v.slice(0,yt);l.push(_),x.push(A),g.push(xt(_)),w.push(xt(A))}finally{b.fill(0),I.fill(0),E.fill(0),v.fill(0)}}return T=!0,{falsePreimages:l,truePreimages:x,falseHashes:g,trueHashes:w}}finally{if(!T){for(const d of l)d.fill(0);for(const d of x)d.fill(0)}}}finally{for(const a of i)a.fill(0)}}function Te(e){return{false_list:e.falseHashes.map(rt),true_list:e.trueHashes.map(rt)}}function Wt(e){if(e.falseHashes.length===0||e.trueHashes.length===0)throw new Error("computeWotsPkHash: keypair hash arrays must not be empty");const t=e.falseHashes[0].length,n=(e.falseHashes.length+e.trueHashes.length)*t,s=new Uint8Array(n);let o=0;for(const i of e.falseHashes)s.set(i,o),o+=t;for(const i of e.trueHashes)s.set(i,o),o+=t;const r=At(s);return`0x${rt(r)}`}const Z=32,Y=64,Ee=4,H=20,it=4,Ut=2,Ie=0,He=1,D=[64,64];function at(...e){const t=e.reduce((o,r)=>o+r.length,0),n=new Uint8Array(t);let s=0;for(const o of e)n.set(o,s),s+=o.length;return n}function J(e){return new TextEncoder().encode(e)}function Q(e){const t=new Uint8Array(Ee);return new DataView(t.buffer).setUint32(0,e.length,!1),at(t,e)}function tt(e){return e.startsWith("0x")||e.startsWith("0X")?e.slice(2):e}const ve=e=>Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("");function Be(e,t){return X(u.sha512,e,t)}function lt(e){return vt(u.sha256(e))}function Lt(e){return(1<<e)-1}function _e(e){let t=1;for(;t*t<e+1;)t++;return Math.max(t,2)}function Se(e){const t=it,n=Lt(t),s=e*n;return{d:t,n:e,checksum_radix:_e(s)}}function et(e,t){const n=[];let s=t;for(;s>0;)n.push(s&255),s>>>=8;const o=new Uint8Array(e.length+n.length);o.set(e);for(let r=0;r<n.length;r++)o[e.length+r]=n[r];return lt(o)}function nt(e,t){let n=e;for(let s=0;s<t;s++)n=lt(n);return n}function Ce(e,t){const n=Lt(t.d),s=t.checksum_radix-1,o=Math.floor(t.n*n/t.checksum_radix),r=[];for(let c=0;c<t.n;c++){const y=et(e,c+Ut),l=nt(y,n);r.push(Array.from(l))}const i=et(e,Ie),a=nt(i,s),p=et(e,He),h=nt(p,o);return{config:t,message_terminals:r,checksum_major_terminal:Array.from(h),checksum_minor_terminal:Array.from(a)}}async function Ae(e,t,n,s){if(e.length!==Y)throw new Error(`WOTS seed must be exactly ${Y} bytes, got ${e.length}`);const o=tt(t),r=tt(n),i=e.slice(Z,Y),a=e.slice(0,Z),p=at(a,at(Q(J(o)),Q(J(r)),Q(J(tt(s))))),h=Be(i,p),c=h.slice(0,Z);try{const y=[];for(let l=0;l<D.length;l++){const x=D[l],g=Se(x),w=new Uint8Array(c.length+1);w.set(c),w[c.length]=l;const T=lt(w);try{const d=Ce(T,g);if(d.config.d!==it)throw new Error(`Block ${l}: expected d=${it}, got d=${d.config.d}`);if(d.config.n!==x)throw new Error(`Block ${l}: expected n=${x}, got n=${d.config.n}`);if(d.message_terminals.length!==x)throw new Error(`Block ${l}: expected ${x} message terminals, got ${d.message_terminals.length}`);for(let f=0;f<d.message_terminals.length;f++)if(d.message_terminals[f].length!==H)throw new Error(`Block ${l} terminal ${f}: expected ${H} bytes, got ${d.message_terminals[f].length}`);if(d.checksum_minor_terminal.length!==H)throw new Error(`Block ${l} checksum_minor: expected ${H} bytes`);if(d.checksum_major_terminal.length!==H)throw new Error(`Block ${l} checksum_major: expected ${H} bytes`);y.push(d)}finally{w.fill(0),T.fill(0)}}if(y.length!==D.length)throw new Error(`Expected ${D.length} blocks, got ${y.length}`);return y}finally{p.fill(0),i.fill(0),a.fill(0),h.fill(0),c.fill(0),e.fill(0)}}function st(e,t,n){if(e.length!==H)throw new Error(`Block ${t} ${n}: expected ${H} bytes, got ${e.length}`);for(let s=0;s<e.length;s++){const o=e[s];if(!Number.isInteger(o)||o<0||o>255)throw new Error(`Block ${t} ${n}[${s}]: invalid byte value ${o}`)}}function Re(e){if(e.length===0)throw new Error("Public keys array must not be empty");for(let r=0;r<e.length;r++){const i=e[r];st(i.checksum_minor_terminal,r,"checksum_minor_terminal"),st(i.checksum_major_terminal,r,"checksum_major_terminal");for(let a=0;a<i.message_terminals.length;a++)st(i.message_terminals[a],r,`message_terminal[${a}]`)}let t=0;for(const r of e)t+=Ut+r.message_terminals.length;const n=new Uint8Array(t*H);let s=0;for(const r of e){n.set(r.checksum_minor_terminal,s),s+=H,n.set(r.checksum_major_terminal,s),s+=H;for(const i of r.message_terminals)n.set(i,s),s+=H}const o=At(n);return`0x${ve(o)}`}async function $e(e,t,n,s){const o=$t(e);try{const r=await Ot(o,t,n,s);try{return Wt(r)}finally{for(const i of r.falsePreimages)i.fill(0);for(const i of r.truePreimages)i.fill(0)}}finally{o.fill(0)}}function Oe(e){const t=(e instanceof Error?e.message:typeof e=="string"?e:"").toLowerCase();return t.includes("wots")&&t.includes("hash")&&t.includes("does not match")}exports.CONTRACT_ERRORS=C;exports.PeginManager=Jt;exports.computeWotsBlockPublicKeysHash=Re;exports.computeWotsPkHash=Wt;exports.deriveWotsBlockPublicKeys=Ae;exports.deriveWotsKeypair=Ot;exports.deriveWotsPkHash=$e;exports.extractErrorData=N;exports.getContractErrorMessage=Nt;exports.handleContractError=S;exports.isKnownContractError=Xt;exports.isWotsMismatchError=Oe;exports.keypairToPublicKey=Te;exports.mnemonicToWotsSeed=$t;
|
|
2
|
-
//# sourceMappingURL=errors-
|
|
1
|
+
"use strict";var Vt=Object.defineProperty;var Ft=(e,t,n)=>t in e?Vt(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var P=(e,t,n)=>Ft(e,typeof t!="symbol"?t+"":t,n);const M=require("bitcoinjs-lib"),ot=require("buffer"),B=require("viem"),Kt=require("./signing-cl-lowxV.cjs"),ut=require("@babylonlabs-io/babylon-tbv-rust-wasm"),R=require("./challengeAssert-X7V3Ik_Q.cjs"),k=require("./bitcoin-CNnPFU6Y.cjs"),ht=require("./validation-u8W7Lp2x.cjs"),$=require("./psbtInputFields-05ZrwRzf.cjs"),ft=require("./fundPeginTransaction-DxNOeyNI.cjs"),wt=require("./index-BAECy2oz.cjs"),O=require("./types-B-p4dhEH.cjs"),u=require("./buildAndBroadcastRefund-BDGGbGeK.cjs");function Dt(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const n in e)if(n!=="default"){const s=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,s.get?s:{enumerable:!0,get:()=>e[n]})}}return t.default=e,Object.freeze(t)}const Mt=Dt(M),C={"0x04aabf33":"Vault already exists: This Bitcoin transaction has already been registered. Please select different UTXOs or use a different amount to create a unique transaction.","0x4fec082d":"Script mismatch: The Bitcoin transaction's taproot output does not match the expected vault script. This may be caused by incorrect vault participants or key configuration.","0x6cc363a5":"Invalid BTC proof of possession: The signature could not be verified. Please ensure you're signing with the correct Bitcoin wallet.","0x6c3f2bf6":"Invalid BTC public key: The Bitcoin public key format is invalid.","0x2c5211c6":"Invalid amount: The deposit amount is invalid or below the minimum required.","0x0405f772":"Application not registered: The application controller is not registered in the system.","0x24e165cc":"Invalid provider status: The vault provider is not in a valid state to accept deposits.","0xd92e233d":"Zero address: One of the required addresses is the zero address.","0x65aa7007":"BTC key mismatch: The Bitcoin public key does not match the expected key.","0x82b42900":"Unauthorized: You must be the depositor or vault provider to submit this transaction.","0x8baa579f":"Invalid signature: The BTC proof of possession signature could not be verified.","0x2f9d01e9":"Invalid BTC transaction: The Bitcoin transaction format is invalid.","0x5a3c6b3e":"Vault provider not registered: The selected vault provider is not registered.","0x979f4518":"Invalid pegin fee: The ETH fee sent does not match the required amount. This may indicate a fee rate change during the transaction.","0x5fad9694":"This pre-pegin output has already been used to activate another vault.","0x7ed061c9":"This pegin transaction has already been used to activate another vault."};function N(e){if(!e||typeof e!="object")return;const t=e;if(typeof t.data=="string"&&t.data.startsWith("0x"))return t.data;if(typeof t.details=="string"&&t.details.startsWith("0x"))return t.details;let n=t.cause,s=0;const o=5;for(;n&&typeof n=="object"&&s<o;){const a=n;if(typeof a.data=="string"&&a.data.startsWith("0x"))return a.data;n=a.cause,s++}const i=(typeof t.message=="string"?t.message:"").match(/\b(0x[a-fA-F0-9]{8})\b/);if(i)return i[1]}function Nt(e){const t=N(e);if(t){const n=t.substring(0,10);return C[t]??C[n]}}function Xt(e){const t=N(e);if(t===void 0)return!1;const n=t.substring(0,10);return t in C||n in C}function S(e){console.error("[Contract Error] Raw error:",e);const t=N(e);if(console.error("[Contract Error] Extracted error data:",t),t){const s=t.substring(0,10),o=C[t]??C[s];if(o)throw console.error("[Contract Error] Known error:",o),new Error(o)}const n=(e==null?void 0:e.message)||"";if(n.includes("gas limit too high")||n.includes("21000000")||n.includes("Internal JSON-RPC error")){const s=t?` (error code: ${t})`:"";throw console.error("[Contract Error] Transaction rejected. Error code:",t,"Message:",n),new Error(`Transaction failed: The contract rejected this transaction${s}. Possible causes: (1) Vault already exists for this transaction, (2) Invalid signature, (3) Unauthorized caller. Please check your transaction parameters and try again.`)}throw e instanceof Error?(console.error("[Contract Error] Unhandled error:",e.message),e):new Error(`Contract call failed: ${String(e)}`)}const qt=0,jt=/^0x[0-9a-f]+$/i,Gt=/^[0-9a-f]+$/i,zt=/^[A-Za-z0-9+/]+={0,2}$/;function W(e){if(typeof e!="string"||e.length===0)throw new Error("BTC wallet returned empty public key");return k.processPublicKeyToXOnly(e).toLowerCase()}function Zt(e){if(typeof e!="string"||e.length===0)throw new Error("BTC wallet returned empty BIP-322 signature");if(e.startsWith("0x")||e.startsWith("0X")){if(!jt.test(e)||e.length<4||e.length%2!==0)throw new Error("BTC wallet returned malformed hex BIP-322 signature");return e.toLowerCase()}if(Gt.test(e)){if(e.length%2!==0)throw new Error("BTC wallet returned malformed hex BIP-322 signature");return`0x${e.toLowerCase()}`}if(!zt.test(e)||e.length%4!==0)throw new Error("BTC wallet returned malformed base64 BIP-322 signature");const t=ot.Buffer.from(e,"base64");if(t.length===0||t.toString("base64")!==e)throw new Error("BTC wallet returned malformed base64 BIP-322 signature");return`0x${t.toString("hex")}`}function Yt(e,t,n,s){const o=n==null?void 0:n[`${e}:${t}`];return o?Promise.resolve({txid:e,vout:t,value:o.value,scriptPubKey:o.scriptPubKey}):wt.getUtxoInfo(e,t,s)}const dt=12e4;class Jt{constructor(t){P(this,"config");this.config=t}async preparePegin(t){const n=await this.config.btcWallet.getPublicKeyHex(),s=W(n),o=k.stripHexPrefix(t.vaultProviderBtcPubkey),r=t.vaultKeeperBtcPubkeys.map(k.stripHexPrefix),i=t.universalChallengerBtcPubkeys.map(k.stripHexPrefix);if(t.hashlocks.length!==t.amounts.length)throw new Error(`hashlocks.length (${t.hashlocks.length}) must equal amounts.length (${t.amounts.length})`);if(t.hashlocks.length===0)throw new Error("hashlocks must contain at least one entry");const a=r.length,p={depositorPubkey:s,vaultProviderPubkey:o,vaultKeeperPubkeys:r,universalChallengerPubkeys:i,hashlocks:t.hashlocks,timelockRefund:t.timelockRefund,pegInAmounts:t.amounts,feeRate:t.protocolFeeRate,numLocalChallengers:a,councilQuorum:t.councilQuorum,councilSize:t.councilSize,network:this.config.btcNetwork},h=await R.buildPrePeginPsbt(p),c=$.selectUtxosForPegin([...t.availableUTXOs],h.totalOutputValue,t.mempoolFeeRate,ft.peginOutputCount(h.htlcValues.length)),y=k.getNetwork(this.config.btcNetwork),l=ft.fundPeginTransaction({unfundedTxHex:h.psbtHex,selectedUTXOs:c.selectedUTXOs,changeAddress:t.changeAddress,changeAmount:c.changeAmount,network:y}),x=k.stripHexPrefix($.calculateBtcTxHash(l)),g=[],w=[],T=[];for(let m=0;m<t.hashlocks.length;m++){const b=await R.buildPeginTxFromFundedPrePegin({prePeginParams:p,timelockPegin:t.timelockPegin,fundedPrePeginTxHex:l,htlcVout:m}),I=await R.buildPeginInputPsbt({peginTxHex:b.txHex,fundedPrePeginTxHex:l,depositorPubkey:s,vaultProviderPubkey:o,vaultKeeperPubkeys:r,universalChallengerPubkeys:i,hashlock:t.hashlocks[m],timelockRefund:t.timelockRefund,network:this.config.btcNetwork});g.push(b),w.push(I.psbtHex),T.push(Kt.createTaprootScriptPathSignOptions(n,1))}const d=await this.signPsbtsWithFallback(w,T),f=[];for(let m=0;m<d.length;m++){const b=R.extractPeginInputSignature(d[m],s),I=R.finalizePeginInputPsbt(d[m]);f.push({htlcVout:m,htlcValue:h.htlcValues[m],peginTxHex:I,peginTxid:g[m].txid,peginInputSignature:b,vaultScriptPubKey:g[m].vaultScriptPubKey})}return{fundedPrePeginTxHex:l,prePeginTxid:x,perVault:f,selectedUTXOs:c.selectedUTXOs,fee:c.fee,changeAmount:c.changeAmount}}async signPsbtsWithFallback(t,n){if(typeof this.config.btcWallet.signPsbts=="function"){const o=await this.config.btcWallet.signPsbts(t,n);if(o.length!==t.length)throw new Error(`Expected ${t.length} signed PSBTs but received ${o.length}`);return o}const s=[];for(let o=0;o<t.length;o++){const r=await this.config.btcWallet.signPsbt(t[o],n[o]);s.push(r)}return s}async signAndBroadcast(t){const{fundedPrePeginTxHex:n,depositorBtcPubkey:s}=t,o=n.startsWith("0x")?n.slice(2):n,r=M.Transaction.fromHex(o);if(r.ins.length===0)throw new Error("Transaction has no inputs");const i=new M.Psbt;i.setVersion(r.version),i.setLocktime(r.locktime);const a=ot.Buffer.from(W(s),"hex"),p=this.config.mempoolApiUrl,h=r.ins.map(f=>{const m=ot.Buffer.from(f.hash).reverse().toString("hex"),b=f.index;return Yt(m,b,t.localPrevouts,p).then(I=>({input:f,utxoData:I,txid:m,vout:b}))}),c=await Promise.all(h),y=c.reduce((f,m)=>f+BigInt(m.utxoData.value),0n),l=r.outs.reduce((f,m)=>f+BigInt(m.value),0n);if(y<l)throw new Error(`UTXO value mismatch: total input value (${y} sat) is less than total output value (${l} sat). This may indicate the mempool API returned manipulated UTXO data.`);const x=y-l;if(x>ht.MAX_REASONABLE_FEE_SATS)throw new Error(`Implied transaction fee (${x} sat) exceeds maximum reasonable fee (${ht.MAX_REASONABLE_FEE_SATS} sat). This may indicate manipulated UTXO data.`);for(const{input:f,utxoData:m,txid:b,vout:I}of c){const E=$.getPsbtInputFields({value:m.value,scriptPubKey:m.scriptPubKey},a);i.addInput({hash:f.hash,index:f.index,sequence:f.sequence,...E})}for(const f of r.outs)i.addOutput({script:f.script,value:f.value});const g=await this.config.btcWallet.signPsbt(i.toHex()),w=M.Psbt.fromHex(g);try{w.finalizeAllInputs()}catch(f){if(!w.data.inputs.every(b=>b.finalScriptWitness||b.finalScriptSig))throw new Error(`PSBT finalization failed and wallet did not auto-finalize: ${f}`)}const T=w.extractTransaction().toHex();return await wt.pushTx(T,p)}async registerPeginOnChain(t){const{unsignedPrePeginTx:n,depositorSignedPeginTx:s,vaultProvider:o,hashlock:r,htlcVout:i,depositorPayoutBtcAddress:a,depositorWotsPkHash:p,popSignature:h}=t;if(!this.config.ethWallet.account)throw new Error("Ethereum wallet account not found");const c=this.config.ethWallet.account.address;if(!B.isAddressEqual(h.depositorEthAddress,c))throw new Error(`Proof of possession was signed for ${h.depositorEthAddress} but the Ethereum wallet is currently connected to ${c}. Reconnect the original account or call signProofOfPossession() again.`);await this.assertPopMatchesBtcWallet(h);const y=h.btcPopSignature,l=k.ensureHexPrefix(h.depositorBtcPubkey),x=k.ensureHexPrefix(n),g=k.ensureHexPrefix(s),w=await this.resolvePayoutScriptPubKey(a),T=$.calculateBtcTxHash(g),d=await ut.deriveVaultId(k.stripHexPrefix(T),k.stripHexPrefix(c)),f=k.ensureHexPrefix(d);if(await this.checkVaultExists(f))throw new Error(`Vault already exists (ID: ${f}, peginTxHash: ${T}). Vault IDs are derived from the pegin transaction hash and depositor address. To create a new vault, use different UTXOs or a different amount to generate a unique transaction.`);const b=B.createPublicClient({chain:this.config.ethChain,transport:B.http()});let I;try{I=await b.readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:O.BTCVaultRegistryABI,functionName:"getPegInFee",args:[o]})}catch{throw new Error("Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct.")}const E=B.encodeFunctionData({abi:O.BTCVaultRegistryABI,functionName:"submitPeginRequest",args:[c,l,y,x,g,o,r,i,w,p]});let v;try{v=await b.estimateGas({to:this.config.vaultContracts.btcVaultRegistry,data:E,value:I,account:this.config.ethWallet.account.address})}catch(q){S(q)}let _;try{_=await this.config.ethWallet.sendTransaction({to:this.config.vaultContracts.btcVaultRegistry,data:E,value:I,account:this.config.ethWallet.account,chain:this.config.ethChain,gas:v})}catch(q){S(q)}const A=await b.waitForTransactionReceipt({hash:_,timeout:dt});return A.status==="reverted"&&S(new Error(`Transaction reverted. Hash: ${_}. Check the transaction on block explorer for details.`)),{ethTxHash:A.transactionHash,vaultId:f,peginTxHash:T}}async registerPeginBatchOnChain(t){const{vaultProvider:n,unsignedPrePeginTx:s,requests:o,popSignature:r}=t;if(o.length===0)throw new Error("Batch pegin requires at least one request");if(!this.config.ethWallet.account)throw new Error("Ethereum wallet account not found");const i=this.config.ethWallet.account.address;if(!B.isAddressEqual(r.depositorEthAddress,i))throw new Error(`Proof of possession was signed for ${r.depositorEthAddress} but the Ethereum wallet is currently connected to ${i}. Reconnect the original account or call signProofOfPossession() again.`);await this.assertPopMatchesBtcWallet(r);const a=r.btcPopSignature,p=[];for(const b of o)p.push(await this.resolvePayoutScriptPubKey(b.depositorPayoutBtcAddress));const h=[];for(const b of o){const I=k.ensureHexPrefix(b.depositorSignedPeginTx),E=$.calculateBtcTxHash(I),v=await ut.deriveVaultId(k.stripHexPrefix(E),k.stripHexPrefix(i)),_=k.ensureHexPrefix(v);if(await this.checkVaultExists(_))throw new Error(`Vault already exists (ID: ${_}, peginTxHash: ${E}). To create a new vault, use different UTXOs or a different amount.`);h.push({vaultId:_,peginTxHash:E})}const c=B.createPublicClient({chain:this.config.ethChain,transport:B.http()});let y;try{y=await c.readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:O.BTCVaultRegistryABI,functionName:"getPegInFee",args:[n]})}catch{throw new Error("Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct.")}const l=y*BigInt(o.length),x=k.ensureHexPrefix(r.depositorBtcPubkey),g=k.ensureHexPrefix(s),w=o.map((b,I)=>({depositorBtcPubKey:x,btcPopSignature:a,unsignedPrePeginTx:g,depositorSignedPeginTx:k.ensureHexPrefix(b.depositorSignedPeginTx),hashlock:b.hashlock,htlcVout:b.htlcVout,referralCode:qt,depositorPayoutBtcAddress:p[I],depositorWotsPkHash:b.depositorWotsPkHash})),T=B.encodeFunctionData({abi:O.BTCVaultRegistryABI,functionName:"submitPeginRequestBatch",args:[i,n,w]});let d;try{d=await c.estimateGas({to:this.config.vaultContracts.btcVaultRegistry,data:T,value:l,account:this.config.ethWallet.account.address})}catch(b){S(b)}let f;try{f=await this.config.ethWallet.sendTransaction({to:this.config.vaultContracts.btcVaultRegistry,data:T,value:l,account:this.config.ethWallet.account,chain:this.config.ethChain,gas:d})}catch(b){S(b)}const m=await c.waitForTransactionReceipt({hash:f,timeout:dt});return m.status==="reverted"&&S(new Error(`Batch transaction reverted. Hash: ${f}. Check the transaction on block explorer for details.`)),{ethTxHash:m.transactionHash,vaults:h}}async checkVaultExists(t){try{return(await B.createPublicClient({chain:this.config.ethChain,transport:B.http()}).readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:O.BTCVaultRegistryABI,functionName:"getBtcVaultBasicInfo",args:[t]}))[0]!==B.zeroAddress}catch{return!1}}async resolvePayoutScriptPubKey(t){let n;if(t)n=t;else{n=await this.config.btcWallet.getAddress();const o=await this.config.btcWallet.getPublicKeyHex();if(!k.isAddressFromPublicKey(n,o,this.config.btcNetwork))throw new Error("The BTC address from your wallet does not match the wallet's public key. Please ensure your wallet is using a supported address type (Taproot or Native SegWit).")}const s=k.getNetwork(this.config.btcNetwork);try{return`0x${Mt.address.toOutputScript(n,s).toString("hex")}`}catch{throw new Error(`Invalid BTC payout address: "${n}". Please provide a valid Bitcoin address for the ${this.config.btcNetwork} network.`)}}async signProofOfPossession(){if(!this.config.ethWallet.account)throw new Error("Ethereum wallet account not found");const t=this.config.ethWallet.account.address,n=W(await this.config.btcWallet.getPublicKeyHex()),s=this.config.vaultContracts.btcVaultRegistry,o=`${t.toLowerCase()}:${this.config.ethChain.id}:pegin:${s.toLowerCase()}`,r=await this.config.btcWallet.signMessage(o,"bip322-simple");return{btcPopSignature:Zt(r),depositorEthAddress:t,depositorBtcPubkey:n}}async assertPopMatchesBtcWallet(t){const n=W(await this.config.btcWallet.getPublicKeyHex()),s=W(t.depositorBtcPubkey);if(n!==s)throw new Error(`Proof of possession was signed with BTC pubkey ${s} but the BTC wallet is currently connected to ${n}. Reconnect the original wallet or call signProofOfPossession() again.`)}getNetwork(){return this.config.btcNetwork}getVaultContractAddress(){return this.config.vaultContracts.btcVaultRegistry}}class Pt{constructor(t,n){P(this,"oHash");P(this,"iHash");P(this,"blockLen");P(this,"outputLen");P(this,"finished",!1);P(this,"destroyed",!1);if(u.ahash(t),u.abytes(n,void 0,"key"),this.iHash=t.create(),typeof this.iHash.update!="function")throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;const s=this.blockLen,o=new Uint8Array(s);o.set(n.length>s?t.create().update(n).digest():n);for(let r=0;r<o.length;r++)o[r]^=54;this.iHash.update(o),this.oHash=t.create();for(let r=0;r<o.length;r++)o[r]^=106;this.oHash.update(o),u.clean(o)}update(t){return u.aexists(this),this.iHash.update(t),this}digestInto(t){u.aexists(this),u.abytes(t,this.outputLen,"output"),this.finished=!0,this.iHash.digestInto(t),this.oHash.update(t),this.oHash.digestInto(t),this.destroy()}digest(){const t=new Uint8Array(this.oHash.outputLen);return this.digestInto(t),t}_cloneInto(t){t||(t=Object.create(Object.getPrototypeOf(this),{}));const{oHash:n,iHash:s,finished:o,destroyed:r,blockLen:i,outputLen:a}=this;return t=t,t.finished=o,t.destroyed=r,t.blockLen=i,t.outputLen=a,t.oHash=n._cloneInto(t.oHash),t.iHash=s._cloneInto(t.iHash),t}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}}const X=(e,t,n)=>new Pt(e,t).update(n).digest();X.create=(e,t)=>new Pt(e,t);const Qt=Uint8Array.from([7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8]),kt=Uint8Array.from(new Array(16).fill(0).map((e,t)=>t)),te=kt.map(e=>(9*e+5)%16),Tt=(()=>{const n=[[kt],[te]];for(let s=0;s<4;s++)for(let o of n)o.push(o[s].map(r=>Qt[r]));return n})(),Et=Tt[0],It=Tt[1],Ht=[[11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8],[12,13,11,15,6,9,9,7,12,15,11,13,7,8,7,7],[13,15,14,11,7,7,6,8,13,14,13,12,5,5,6,9],[14,11,12,14,8,6,5,5,15,12,15,14,9,9,8,6],[15,12,13,13,9,5,8,6,14,11,12,11,8,6,5,5]].map(e=>Uint8Array.from(e)),ee=Et.map((e,t)=>e.map(n=>Ht[t][n])),ne=It.map((e,t)=>e.map(n=>Ht[t][n])),se=Uint32Array.from([0,1518500249,1859775393,2400959708,2840853838]),oe=Uint32Array.from([1352829926,1548603684,1836072691,2053994217,0]);function gt(e,t,n,s){return e===0?t^n^s:e===1?t&n|~t&s:e===2?(t|~n)^s:e===3?t&s|n&~s:t^(n|~s)}const V=new Uint32Array(16);class re extends u.HashMD{constructor(){super(64,20,8,!0);P(this,"h0",1732584193);P(this,"h1",-271733879);P(this,"h2",-1732584194);P(this,"h3",271733878);P(this,"h4",-1009589776)}get(){const{h0:n,h1:s,h2:o,h3:r,h4:i}=this;return[n,s,o,r,i]}set(n,s,o,r,i){this.h0=n|0,this.h1=s|0,this.h2=o|0,this.h3=r|0,this.h4=i|0}process(n,s){for(let g=0;g<16;g++,s+=4)V[g]=n.getUint32(s,!0);let o=this.h0|0,r=o,i=this.h1|0,a=i,p=this.h2|0,h=p,c=this.h3|0,y=c,l=this.h4|0,x=l;for(let g=0;g<5;g++){const w=4-g,T=se[g],d=oe[g],f=Et[g],m=It[g],b=ee[g],I=ne[g];for(let E=0;E<16;E++){const v=u.rotl(o+gt(g,i,p,c)+V[f[E]]+T,b[E])+l|0;o=l,l=c,c=u.rotl(p,10)|0,p=i,i=v}for(let E=0;E<16;E++){const v=u.rotl(r+gt(w,a,h,y)+V[m[E]]+d,I[E])+x|0;r=x,x=y,y=u.rotl(h,10)|0,h=a,a=v}}this.set(this.h1+p+y|0,this.h2+c+x|0,this.h3+l+r|0,this.h4+o+a|0,this.h0+i+h|0)}roundClean(){u.clean(V)}destroy(){this.destroyed=!0,u.clean(this.buffer),this.set(0,0,0,0,0)}}const vt=u.createHasher(()=>new re),ie=BigInt(0),U=BigInt(1),ae=BigInt(2),ce=BigInt(7),le=BigInt(256),ue=BigInt(113),Bt=[],_t=[],St=[];for(let e=0,t=U,n=1,s=0;e<24;e++){[n,s]=[s,(2*n+3*s)%5],Bt.push(2*(5*s+n)),_t.push((e+1)*(e+2)/2%64);let o=ie;for(let r=0;r<7;r++)t=(t<<U^(t>>ce)*ue)%le,t&ae&&(o^=U<<(U<<BigInt(r))-U);St.push(o)}const Ct=u.split(St,!0),he=Ct[0],fe=Ct[1],pt=(e,t,n)=>n>32?u.rotlBH(e,t,n):u.rotlSH(e,t,n),mt=(e,t,n)=>n>32?u.rotlBL(e,t,n):u.rotlSL(e,t,n);function de(e,t=24){const n=new Uint32Array(10);for(let s=24-t;s<24;s++){for(let i=0;i<10;i++)n[i]=e[i]^e[i+10]^e[i+20]^e[i+30]^e[i+40];for(let i=0;i<10;i+=2){const a=(i+8)%10,p=(i+2)%10,h=n[p],c=n[p+1],y=pt(h,c,1)^n[a],l=mt(h,c,1)^n[a+1];for(let x=0;x<50;x+=10)e[i+x]^=y,e[i+x+1]^=l}let o=e[2],r=e[3];for(let i=0;i<24;i++){const a=_t[i],p=pt(o,r,a),h=mt(o,r,a),c=Bt[i];o=e[c],r=e[c+1],e[c]=p,e[c+1]=h}for(let i=0;i<50;i+=10){for(let a=0;a<10;a++)n[a]=e[i+a];for(let a=0;a<10;a++)e[i+a]^=~n[(a+2)%10]&n[(a+4)%10]}e[0]^=he[s],e[1]^=fe[s]}u.clean(n)}class ct{constructor(t,n,s,o=!1,r=24){P(this,"state");P(this,"pos",0);P(this,"posOut",0);P(this,"finished",!1);P(this,"state32");P(this,"destroyed",!1);P(this,"blockLen");P(this,"suffix");P(this,"outputLen");P(this,"enableXOF",!1);P(this,"rounds");if(this.blockLen=t,this.suffix=n,this.outputLen=s,this.enableXOF=o,this.rounds=r,u.anumber(s,"outputLen"),!(0<t&&t<200))throw new Error("only keccak-f1600 function is supported");this.state=new Uint8Array(200),this.state32=u.u32(this.state)}clone(){return this._cloneInto()}keccak(){u.swap32IfBE(this.state32),de(this.state32,this.rounds),u.swap32IfBE(this.state32),this.posOut=0,this.pos=0}update(t){u.aexists(this),u.abytes(t);const{blockLen:n,state:s}=this,o=t.length;for(let r=0;r<o;){const i=Math.min(n-this.pos,o-r);for(let a=0;a<i;a++)s[this.pos++]^=t[r++];this.pos===n&&this.keccak()}return this}finish(){if(this.finished)return;this.finished=!0;const{state:t,suffix:n,pos:s,blockLen:o}=this;t[s]^=n,(n&128)!==0&&s===o-1&&this.keccak(),t[o-1]^=128,this.keccak()}writeInto(t){u.aexists(this,!1),u.abytes(t),this.finish();const n=this.state,{blockLen:s}=this;for(let o=0,r=t.length;o<r;){this.posOut>=s&&this.keccak();const i=Math.min(s-this.posOut,r-o);t.set(n.subarray(this.posOut,this.posOut+i),o),this.posOut+=i,o+=i}return t}xofInto(t){if(!this.enableXOF)throw new Error("XOF is not possible for this instance");return this.writeInto(t)}xof(t){return u.anumber(t),this.xofInto(new Uint8Array(t))}digestInto(t){if(u.aoutput(t,this),this.finished)throw new Error("digest() was already called");return this.writeInto(t),this.destroy(),t}digest(){return this.digestInto(new Uint8Array(this.outputLen))}destroy(){this.destroyed=!0,u.clean(this.state)}_cloneInto(t){const{blockLen:n,suffix:s,outputLen:o,rounds:r,enableXOF:i}=this;return t||(t=new ct(n,s,o,i,r)),t.state32.set(this.state32),t.pos=this.pos,t.posOut=this.posOut,t.finished=this.finished,t.rounds=r,t.suffix=s,t.outputLen=o,t.enableXOF=i,t.destroyed=this.destroyed,t}}const ge=(e,t,n,s={})=>u.createHasher(()=>new ct(t,e,n),s),At=ge(1,136,32);function pe(e,t,n,s){u.ahash(e);const o=u.checkOpts({dkLen:32,asyncTick:10},s),{c:r,dkLen:i,asyncTick:a}=o;if(u.anumber(r,"c"),u.anumber(i,"dkLen"),u.anumber(a,"asyncTick"),r<1)throw new Error("iterations (c) must be >= 1");const p=u.kdfInputToBytes(t,"password"),h=u.kdfInputToBytes(n,"salt"),c=new Uint8Array(i),y=X.create(e,p),l=y._cloneInto().update(h);return{c:r,dkLen:i,asyncTick:a,DK:c,PRF:y,PRFSalt:l}}function me(e,t,n,s,o){return e.destroy(),t.destroy(),s&&s.destroy(),u.clean(o),n}function ye(e,t,n,s){const{c:o,dkLen:r,DK:i,PRF:a,PRFSalt:p}=pe(e,t,n,s);let h;const c=new Uint8Array(4),y=u.createView(c),l=new Uint8Array(a.outputLen);for(let x=1,g=0;g<r;x++,g+=a.outputLen){const w=i.subarray(g,g+a.outputLen);y.setInt32(0,x,!1),(h=p._cloneInto(h)).update(c).digestInto(l),w.set(l.subarray(0,w.length));for(let T=1;T<o;T++){a._cloneInto(h).update(l).digestInto(l);for(let d=0;d<w.length;d++)w[d]^=l[d]}}return me(a,p,i,h,l)}function Rt(e){if(typeof e!="string")throw new TypeError("invalid mnemonic type: "+typeof e);return e.normalize("NFKD")}function be(e){const t=Rt(e),n=t.split(" ");if(![12,15,18,21,24].includes(n.length))throw new Error("Invalid mnemonic");return{nfkd:t,words:n}}const xe=e=>Rt("mnemonic"+e);function we(e,t=""){return ye(u.sha512,be(e).nfkd,xe(t),{c:2048,dkLen:64})}const Pe=508,yt=16,F=32,K=64,bt=5,ke=4;function L(...e){const t=e.reduce((o,r)=>o+r.length,0),n=new Uint8Array(t);let s=0;for(const o of e)n.set(o,s),s+=o.length;return n}function j(e){return new TextEncoder().encode(e)}function G(e){const t=new Uint8Array(ke);return new DataView(t.buffer).setUint32(0,e.length,!1),L(t,e)}function z(e,t){return X(u.sha512,e,t)}function xt(e){return vt(u.sha256(e))}const rt=e=>Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("");function $t(e){const t=we(e),n=new Uint8Array(t);return t.fill(0),n}async function Ot(e,t,n,s){if(e.length!==K)throw new Error(`WOTS seed must be ${K} bytes, got ${e.length}`);t=k.stripHexPrefix(t),n=k.stripHexPrefix(n);const o=e.slice(F,K),r=e.slice(0,F),i=[o,r];try{const a=L(G(j(t)),G(j(n)),G(j(s))),p=L(r,a);i.push(p);const h=z(o,p);i.push(h);const c=h.slice(0,F),y=h.slice(F,K);i.push(c,y);const l=[],x=[],g=[],w=[];let T=!1;try{for(let d=0;d<Pe;d++){const f=new Uint8Array(bt);f[0]=0,new DataView(f.buffer).setUint32(1,d,!1);const m=new Uint8Array(bt);m[0]=1,new DataView(m.buffer).setUint32(1,d,!1);const b=L(c,f),I=L(c,m),E=z(y,b),v=z(y,I);try{const _=E.slice(0,yt),A=v.slice(0,yt);l.push(_),x.push(A),g.push(xt(_)),w.push(xt(A))}finally{b.fill(0),I.fill(0),E.fill(0),v.fill(0)}}return T=!0,{falsePreimages:l,truePreimages:x,falseHashes:g,trueHashes:w}}finally{if(!T){for(const d of l)d.fill(0);for(const d of x)d.fill(0)}}}finally{for(const a of i)a.fill(0)}}function Te(e){return{false_list:e.falseHashes.map(rt),true_list:e.trueHashes.map(rt)}}function Wt(e){if(e.falseHashes.length===0||e.trueHashes.length===0)throw new Error("computeWotsPkHash: keypair hash arrays must not be empty");const t=e.falseHashes[0].length,n=(e.falseHashes.length+e.trueHashes.length)*t,s=new Uint8Array(n);let o=0;for(const i of e.falseHashes)s.set(i,o),o+=t;for(const i of e.trueHashes)s.set(i,o),o+=t;const r=At(s);return`0x${rt(r)}`}const Z=32,Y=64,Ee=4,H=20,it=4,Ut=2,Ie=0,He=1,D=[64,64];function at(...e){const t=e.reduce((o,r)=>o+r.length,0),n=new Uint8Array(t);let s=0;for(const o of e)n.set(o,s),s+=o.length;return n}function J(e){return new TextEncoder().encode(e)}function Q(e){const t=new Uint8Array(Ee);return new DataView(t.buffer).setUint32(0,e.length,!1),at(t,e)}function tt(e){return e.startsWith("0x")||e.startsWith("0X")?e.slice(2):e}const ve=e=>Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("");function Be(e,t){return X(u.sha512,e,t)}function lt(e){return vt(u.sha256(e))}function Lt(e){return(1<<e)-1}function _e(e){let t=1;for(;t*t<e+1;)t++;return Math.max(t,2)}function Se(e){const t=it,n=Lt(t),s=e*n;return{d:t,n:e,checksum_radix:_e(s)}}function et(e,t){const n=[];let s=t;for(;s>0;)n.push(s&255),s>>>=8;const o=new Uint8Array(e.length+n.length);o.set(e);for(let r=0;r<n.length;r++)o[e.length+r]=n[r];return lt(o)}function nt(e,t){let n=e;for(let s=0;s<t;s++)n=lt(n);return n}function Ce(e,t){const n=Lt(t.d),s=t.checksum_radix-1,o=Math.floor(t.n*n/t.checksum_radix),r=[];for(let c=0;c<t.n;c++){const y=et(e,c+Ut),l=nt(y,n);r.push(Array.from(l))}const i=et(e,Ie),a=nt(i,s),p=et(e,He),h=nt(p,o);return{config:t,message_terminals:r,checksum_major_terminal:Array.from(h),checksum_minor_terminal:Array.from(a)}}async function Ae(e,t,n,s){if(e.length!==Y)throw new Error(`WOTS seed must be exactly ${Y} bytes, got ${e.length}`);const o=tt(t),r=tt(n),i=e.slice(Z,Y),a=e.slice(0,Z),p=at(a,at(Q(J(o)),Q(J(r)),Q(J(tt(s))))),h=Be(i,p),c=h.slice(0,Z);try{const y=[];for(let l=0;l<D.length;l++){const x=D[l],g=Se(x),w=new Uint8Array(c.length+1);w.set(c),w[c.length]=l;const T=lt(w);try{const d=Ce(T,g);if(d.config.d!==it)throw new Error(`Block ${l}: expected d=${it}, got d=${d.config.d}`);if(d.config.n!==x)throw new Error(`Block ${l}: expected n=${x}, got n=${d.config.n}`);if(d.message_terminals.length!==x)throw new Error(`Block ${l}: expected ${x} message terminals, got ${d.message_terminals.length}`);for(let f=0;f<d.message_terminals.length;f++)if(d.message_terminals[f].length!==H)throw new Error(`Block ${l} terminal ${f}: expected ${H} bytes, got ${d.message_terminals[f].length}`);if(d.checksum_minor_terminal.length!==H)throw new Error(`Block ${l} checksum_minor: expected ${H} bytes`);if(d.checksum_major_terminal.length!==H)throw new Error(`Block ${l} checksum_major: expected ${H} bytes`);y.push(d)}finally{w.fill(0),T.fill(0)}}if(y.length!==D.length)throw new Error(`Expected ${D.length} blocks, got ${y.length}`);return y}finally{p.fill(0),i.fill(0),a.fill(0),h.fill(0),c.fill(0),e.fill(0)}}function st(e,t,n){if(e.length!==H)throw new Error(`Block ${t} ${n}: expected ${H} bytes, got ${e.length}`);for(let s=0;s<e.length;s++){const o=e[s];if(!Number.isInteger(o)||o<0||o>255)throw new Error(`Block ${t} ${n}[${s}]: invalid byte value ${o}`)}}function Re(e){if(e.length===0)throw new Error("Public keys array must not be empty");for(let r=0;r<e.length;r++){const i=e[r];st(i.checksum_minor_terminal,r,"checksum_minor_terminal"),st(i.checksum_major_terminal,r,"checksum_major_terminal");for(let a=0;a<i.message_terminals.length;a++)st(i.message_terminals[a],r,`message_terminal[${a}]`)}let t=0;for(const r of e)t+=Ut+r.message_terminals.length;const n=new Uint8Array(t*H);let s=0;for(const r of e){n.set(r.checksum_minor_terminal,s),s+=H,n.set(r.checksum_major_terminal,s),s+=H;for(const i of r.message_terminals)n.set(i,s),s+=H}const o=At(n);return`0x${ve(o)}`}async function $e(e,t,n,s){const o=$t(e);try{const r=await Ot(o,t,n,s);try{return Wt(r)}finally{for(const i of r.falsePreimages)i.fill(0);for(const i of r.truePreimages)i.fill(0)}}finally{o.fill(0)}}function Oe(e){const t=(e instanceof Error?e.message:typeof e=="string"?e:"").toLowerCase();return t.includes("wots")&&t.includes("hash")&&t.includes("does not match")}exports.CONTRACT_ERRORS=C;exports.PeginManager=Jt;exports.computeWotsBlockPublicKeysHash=Re;exports.computeWotsPkHash=Wt;exports.deriveWotsBlockPublicKeys=Ae;exports.deriveWotsKeypair=Ot;exports.deriveWotsPkHash=$e;exports.extractErrorData=N;exports.getContractErrorMessage=Nt;exports.handleContractError=S;exports.isKnownContractError=Xt;exports.isWotsMismatchError=Oe;exports.keypairToPublicKey=Te;exports.mnemonicToWotsSeed=$t;
|
|
2
|
+
//# sourceMappingURL=errors-B3TOmE31.cjs.map
|