@babylonlabs-io/ts-sdk 0.48.0 → 0.48.2

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.
Files changed (80) hide show
  1. package/dist/{PayoutManager-BbemBIo9.js → PayoutManager-D02AePm4.js} +78 -65
  2. package/dist/PayoutManager-D02AePm4.js.map +1 -0
  3. package/dist/PayoutManager-DD1audlx.cjs +2 -0
  4. package/dist/PayoutManager-DD1audlx.cjs.map +1 -0
  5. package/dist/{PeginManager-BMO6R9I9.js → PeginManager-6seoi9mV.js} +217 -198
  6. package/dist/PeginManager-6seoi9mV.js.map +1 -0
  7. package/dist/PeginManager-CHZieoEQ.cjs +2 -0
  8. package/dist/PeginManager-CHZieoEQ.cjs.map +1 -0
  9. package/dist/buildAndBroadcastRefund-Bi07LxuY.cjs +2 -0
  10. package/dist/buildAndBroadcastRefund-Bi07LxuY.cjs.map +1 -0
  11. package/dist/{buildAndBroadcastRefund-sfl7Aac9.js → buildAndBroadcastRefund-DXHs6unL.js} +412 -362
  12. package/dist/buildAndBroadcastRefund-DXHs6unL.js.map +1 -0
  13. package/dist/{challengeAssert-1fy_EzAi.js → challengeAssert-ChvLypwc.js} +7 -7
  14. package/dist/{challengeAssert-1fy_EzAi.js.map → challengeAssert-ChvLypwc.js.map} +1 -1
  15. package/dist/challengeAssert-Culc7DoS.cjs +2 -0
  16. package/dist/{challengeAssert-DEw-z3n9.cjs.map → challengeAssert-Culc7DoS.cjs.map} +1 -1
  17. package/dist/index.cjs +1 -1
  18. package/dist/index.js +170 -169
  19. package/dist/noPayout-CA4-x5vZ.js +174 -0
  20. package/dist/noPayout-CA4-x5vZ.js.map +1 -0
  21. package/dist/noPayout-CJ_Acpl_.cjs +2 -0
  22. package/dist/noPayout-CJ_Acpl_.cjs.map +1 -0
  23. package/dist/tbv/core/index.cjs +1 -1
  24. package/dist/tbv/core/index.js +168 -167
  25. package/dist/tbv/core/managers/PayoutManager.d.ts.map +1 -1
  26. package/dist/tbv/core/managers/PeginManager.d.ts.map +1 -1
  27. package/dist/tbv/core/managers/index.cjs +1 -1
  28. package/dist/tbv/core/managers/index.js +2 -2
  29. package/dist/tbv/core/primitives/index.cjs +1 -1
  30. package/dist/tbv/core/primitives/index.d.ts +8 -7
  31. package/dist/tbv/core/primitives/index.d.ts.map +1 -1
  32. package/dist/tbv/core/primitives/index.js +30 -29
  33. package/dist/tbv/core/primitives/psbt/__tests__/verifyScriptPathSchnorrSignature.test.d.ts +14 -0
  34. package/dist/tbv/core/primitives/psbt/__tests__/verifyScriptPathSchnorrSignature.test.d.ts.map +1 -0
  35. package/dist/tbv/core/primitives/psbt/refund.d.ts.map +1 -1
  36. package/dist/tbv/core/primitives/psbt/verifyScriptPathSchnorrSignature.d.ts +53 -0
  37. package/dist/tbv/core/primitives/psbt/verifyScriptPathSchnorrSignature.d.ts.map +1 -0
  38. package/dist/tbv/core/services/deposit/signDepositorGraph.d.ts.map +1 -1
  39. package/dist/tbv/core/services/index.cjs +1 -1
  40. package/dist/tbv/core/services/index.js +2 -2
  41. package/dist/tbv/core/services/refund/buildAndBroadcastRefund.d.ts +20 -0
  42. package/dist/tbv/core/services/refund/buildAndBroadcastRefund.d.ts.map +1 -1
  43. package/dist/tbv/index.cjs +1 -1
  44. package/dist/tbv/index.js +168 -167
  45. package/dist/tbv/integrations/aave/clients/__tests__/hub.test.d.ts +2 -0
  46. package/dist/tbv/integrations/aave/clients/__tests__/hub.test.d.ts.map +1 -0
  47. package/dist/tbv/integrations/aave/clients/abis/AaveHub.abi.json.d.ts +24 -0
  48. package/dist/tbv/integrations/aave/clients/hub.d.ts +28 -0
  49. package/dist/tbv/integrations/aave/clients/hub.d.ts.map +1 -0
  50. package/dist/tbv/integrations/aave/clients/index.d.ts +1 -0
  51. package/dist/tbv/integrations/aave/clients/index.d.ts.map +1 -1
  52. package/dist/tbv/integrations/aave/index.cjs +1 -1
  53. package/dist/tbv/integrations/aave/index.cjs.map +1 -1
  54. package/dist/tbv/integrations/aave/index.d.ts +1 -1
  55. package/dist/tbv/integrations/aave/index.d.ts.map +1 -1
  56. package/dist/tbv/integrations/aave/index.js +246 -193
  57. package/dist/tbv/integrations/aave/index.js.map +1 -1
  58. package/dist/verifyScriptPathSchnorrSignature-D43cncKJ.cjs +2 -0
  59. package/dist/verifyScriptPathSchnorrSignature-D43cncKJ.cjs.map +1 -0
  60. package/dist/verifyScriptPathSchnorrSignature-DFJAEleY.js +563 -0
  61. package/dist/verifyScriptPathSchnorrSignature-DFJAEleY.js.map +1 -0
  62. package/package.json +3 -3
  63. package/dist/PayoutManager-BLpgkfOS.cjs +0 -2
  64. package/dist/PayoutManager-BLpgkfOS.cjs.map +0 -1
  65. package/dist/PayoutManager-BbemBIo9.js.map +0 -1
  66. package/dist/PeginManager-BMO6R9I9.js.map +0 -1
  67. package/dist/PeginManager-CfkjDMy7.cjs +0 -2
  68. package/dist/PeginManager-CfkjDMy7.cjs.map +0 -1
  69. package/dist/assertPsbtUnsignedTxMatches-BHyBdtxs.js +0 -481
  70. package/dist/assertPsbtUnsignedTxMatches-BHyBdtxs.js.map +0 -1
  71. package/dist/assertPsbtUnsignedTxMatches-s9H0Qqkl.cjs +0 -2
  72. package/dist/assertPsbtUnsignedTxMatches-s9H0Qqkl.cjs.map +0 -1
  73. package/dist/buildAndBroadcastRefund-sfl7Aac9.js.map +0 -1
  74. package/dist/buildAndBroadcastRefund-tR9sGPwy.cjs +0 -2
  75. package/dist/buildAndBroadcastRefund-tR9sGPwy.cjs.map +0 -1
  76. package/dist/challengeAssert-DEw-z3n9.cjs +0 -2
  77. package/dist/noPayout-5-wtWQ_f.js +0 -154
  78. package/dist/noPayout-5-wtWQ_f.js.map +0 -1
  79. package/dist/noPayout-DNevEzJR.cjs +0 -2
  80. package/dist/noPayout-DNevEzJR.cjs.map +0 -1
@@ -0,0 +1,563 @@
1
+ import { computeMinClaimValue as Q, buildPeginTxFromPrePegin as Z, createPrePeginTransaction as j, createPayoutConnector as J, tapInternalPubkey as ee } from "@babylonlabs-io/babylon-tbv-rust-wasm";
2
+ import { c as L, d as te } from "./fundPeginTransaction-96FxwYYJ.js";
3
+ import { Buffer as a } from "buffer";
4
+ import { Transaction as S, Psbt as T, crypto as ne } from "bitcoinjs-lib";
5
+ import { s as x, h as E, u as _, T as H, c as re, d as oe, S as B, X as R } from "./bitcoin-B5aNKtsk.js";
6
+ import * as ie from "@bitcoin-js/tiny-secp256k1-asmjs";
7
+ async function se(e, t) {
8
+ const n = t.pegInAmounts.length;
9
+ if (e.htlcValues.length !== n)
10
+ throw new Error(
11
+ `WASM Pre-PegIn returned ${e.htlcValues.length} HTLC value(s), expected ${n} (one per requested deposit).`
12
+ );
13
+ if (e.peginAmounts.length !== n || e.htlcScriptPubKeys.length !== n || e.htlcAddresses.length !== n)
14
+ throw new Error(
15
+ `WASM Pre-PegIn returned mismatched array lengths (htlcValues=${e.htlcValues.length}, peginAmounts=${e.peginAmounts.length}, htlcScriptPubKeys=${e.htlcScriptPubKeys.length}, htlcAddresses=${e.htlcAddresses.length}); expected ${n} each.`
16
+ );
17
+ if (e.depositorClaimValue <= 0n)
18
+ throw new Error(
19
+ `WASM Pre-PegIn returned non-positive depositorClaimValue ${e.depositorClaimValue}; expected > 0.`
20
+ );
21
+ const r = await Q(
22
+ t.numLocalChallengers,
23
+ t.universalChallengerPubkeys.length,
24
+ t.councilQuorum,
25
+ t.councilSize,
26
+ t.feeRate
27
+ );
28
+ if (e.depositorClaimValue !== r)
29
+ throw new Error(
30
+ `WASM Pre-PegIn depositorClaimValue ${e.depositorClaimValue} does not match the independently computed minimum claim value ${r} (numLocalChallengers=${t.numLocalChallengers}, numUniversalChallengers=${t.universalChallengerPubkeys.length}, councilQuorum=${t.councilQuorum}, councilSize=${t.councilSize}, feeRate=${t.feeRate}).`
31
+ );
32
+ const o = t.minPeginFeeRate * L;
33
+ for (let i = 0; i < n; i++) {
34
+ const u = t.pegInAmounts[i], s = e.peginAmounts[i], c = e.htlcValues[i];
35
+ if (s !== u)
36
+ throw new Error(
37
+ `WASM Pre-PegIn peginAmount[${i}] ${s} does not match the requested amount ${u}; refusing to build a tx whose recorded amount differs from the depositor's request.`
38
+ );
39
+ if (s <= 0n)
40
+ throw new Error(
41
+ `WASM Pre-PegIn peginAmount[${i}] is non-positive (${s}); expected > 0.`
42
+ );
43
+ if (c <= 0n)
44
+ throw new Error(
45
+ `WASM Pre-PegIn htlcValue[${i}] is non-positive (${c}); expected > 0.`
46
+ );
47
+ const l = c - s - e.depositorClaimValue;
48
+ if (l <= 0n)
49
+ throw new Error(
50
+ `WASM Pre-PegIn htlcValue[${i}] ${c} does not strictly cover peginAmount ${s} + depositorClaimValue ${e.depositorClaimValue} + a PegIn fee (implied fee ${l}).`
51
+ );
52
+ if (l > o)
53
+ throw new Error(
54
+ `WASM Pre-PegIn implied PegIn fee for HTLC[${i}] (${l} sat) exceeds the plausibility cap ${o} sat (minPeginFeeRate=${t.minPeginFeeRate} × ${L} vbytes); htlcValue ${c} appears grossly inflated.`
55
+ );
56
+ }
57
+ }
58
+ function ue(e, t, n) {
59
+ if (e.length < t.length)
60
+ throw new Error(
61
+ `Encoded Pre-PegIn tx has ${e.length} output(s), fewer than the ${t.length} HTLC output(s) the cross-check validated.`
62
+ );
63
+ for (let r = 0; r < t.length; r++) {
64
+ const o = BigInt(e[r].value);
65
+ if (o !== t[r])
66
+ throw new Error(
67
+ `Encoded Pre-PegIn HTLC output[${r}] value ${o} does not match the cross-checked htlcValue ${t[r]}; the funded/signed tx would not pay the validated amount.`
68
+ );
69
+ const i = e[r].script.toString("hex").toLowerCase(), u = n[r].toLowerCase();
70
+ if (i !== u)
71
+ throw new Error(
72
+ `Encoded Pre-PegIn HTLC output[${r}] scriptPubKey ${i} does not match the cross-checked htlcScriptPubKey ${u}.`
73
+ );
74
+ }
75
+ }
76
+ const q = 64, ce = /^[0-9a-fA-F]+$/;
77
+ async function be(e) {
78
+ const t = G(e.authAnchorHash), n = await j({
79
+ depositorPubkey: e.depositorPubkey,
80
+ vaultProviderPubkey: e.vaultProviderPubkey,
81
+ vaultKeeperPubkeys: e.vaultKeeperPubkeys,
82
+ universalChallengerPubkeys: e.universalChallengerPubkeys,
83
+ hashlocks: [...e.hashlocks],
84
+ timelockRefund: e.timelockRefund,
85
+ pegInAmounts: [...e.pegInAmounts],
86
+ feeRate: e.feeRate,
87
+ minPeginFeeRate: e.minPeginFeeRate,
88
+ numLocalChallengers: e.numLocalChallengers,
89
+ councilQuorum: e.councilQuorum,
90
+ councilSize: e.councilSize,
91
+ network: e.network,
92
+ authAnchorHash: t
93
+ });
94
+ await se(n, e);
95
+ const r = te(n.txHex);
96
+ ue(
97
+ r.outputs,
98
+ n.htlcValues,
99
+ n.htlcScriptPubKeys
100
+ );
101
+ const o = r.outputs.reduce(
102
+ (u, s) => u + BigInt(s.value),
103
+ 0n
104
+ ), i = t !== void 0 ? n.htlcValues.length : null;
105
+ return {
106
+ psbtHex: n.txHex,
107
+ totalOutputValue: o,
108
+ htlcValues: n.htlcValues,
109
+ htlcScriptPubKeys: n.htlcScriptPubKeys,
110
+ htlcAddresses: n.htlcAddresses,
111
+ peginAmounts: n.peginAmounts,
112
+ depositorClaimValue: n.depositorClaimValue,
113
+ authAnchorVout: i
114
+ };
115
+ }
116
+ function G(e) {
117
+ if (e === void 0) return;
118
+ const t = e.startsWith("0x") || e.startsWith("0X") ? e.slice(2) : e;
119
+ if (t.length !== q || !ce.test(t))
120
+ throw new Error(
121
+ `authAnchorHash must be 32-byte hex (${q} chars, no 0x prefix); got length ${t.length}`
122
+ );
123
+ return t.toLowerCase();
124
+ }
125
+ async function Ae(e) {
126
+ const t = await Z(
127
+ {
128
+ depositorPubkey: e.prePeginParams.depositorPubkey,
129
+ vaultProviderPubkey: e.prePeginParams.vaultProviderPubkey,
130
+ vaultKeeperPubkeys: e.prePeginParams.vaultKeeperPubkeys,
131
+ universalChallengerPubkeys: e.prePeginParams.universalChallengerPubkeys,
132
+ hashlocks: [...e.prePeginParams.hashlocks],
133
+ timelockRefund: e.prePeginParams.timelockRefund,
134
+ pegInAmounts: [...e.prePeginParams.pegInAmounts],
135
+ feeRate: e.prePeginParams.feeRate,
136
+ minPeginFeeRate: e.prePeginParams.minPeginFeeRate,
137
+ numLocalChallengers: e.prePeginParams.numLocalChallengers,
138
+ councilQuorum: e.prePeginParams.councilQuorum,
139
+ councilSize: e.prePeginParams.councilSize,
140
+ network: e.prePeginParams.network,
141
+ authAnchorHash: G(
142
+ e.prePeginParams.authAnchorHash
143
+ )
144
+ },
145
+ e.timelockPegin,
146
+ e.fundedPrePeginTxHex,
147
+ e.htlcVout
148
+ );
149
+ return {
150
+ txHex: t.txHex,
151
+ txid: t.txid,
152
+ vaultScriptPubKey: t.vaultScriptPubKey,
153
+ vaultValue: t.vaultValue
154
+ };
155
+ }
156
+ async function ae(e) {
157
+ const t = await J(
158
+ {
159
+ depositor: e.depositor,
160
+ vaultProvider: e.vaultProvider,
161
+ vaultKeepers: e.vaultKeepers,
162
+ universalChallengers: e.universalChallengers,
163
+ timelockPegin: e.timelockPegin
164
+ },
165
+ e.network
166
+ );
167
+ return {
168
+ payoutScript: t.payoutScript,
169
+ taprootScriptHash: t.taprootScriptHash,
170
+ scriptPubKey: t.scriptPubKey,
171
+ address: t.address,
172
+ payoutControlBlock: t.payoutControlBlock
173
+ };
174
+ }
175
+ const Te = 2, k = 0, C = 0, N = 546, le = 3, K = 2, I = 1e4, M = 3, F = 10, X = 100;
176
+ async function ke(e) {
177
+ const t = x(e.payoutTxHex), n = x(e.peginTxHex), r = x(e.assertTxHex), o = await ae({
178
+ depositor: e.depositorBtcPubkey,
179
+ vaultProvider: e.vaultProviderBtcPubkey,
180
+ vaultKeepers: e.vaultKeeperBtcPubkeys,
181
+ universalChallengers: e.universalChallengerBtcPubkeys,
182
+ timelockPegin: e.timelockPegin,
183
+ network: e.network
184
+ }), i = E(o.payoutScript), u = E(o.payoutControlBlock), s = S.fromHex(t), c = S.fromHex(n), l = S.fromHex(r), p = new T();
185
+ if (p.setVersion(s.version), p.setLocktime(s.locktime), s.ins.length !== 2)
186
+ throw new Error(
187
+ `Payout transaction must have exactly 2 inputs, got ${s.ins.length}`
188
+ );
189
+ const f = s.ins[0], P = s.ins[1], h = _(
190
+ new Uint8Array(f.hash).slice().reverse()
191
+ ), w = c.getId();
192
+ if (h !== w || f.index !== k)
193
+ throw new Error(
194
+ `Input 0 must spend PegIn:${k}. Expected ${w}:${k}, got ${h}:${f.index}`
195
+ );
196
+ const $ = _(
197
+ new Uint8Array(P.hash).slice().reverse()
198
+ ), d = l.getId();
199
+ if ($ !== d || P.index !== C)
200
+ throw new Error(
201
+ `Input 1 must spend Assert:${C}. Expected ${d}:${C}, got ${$}:${P.index}`
202
+ );
203
+ const g = c.outs[f.index];
204
+ if (!g)
205
+ throw new Error(
206
+ `Previous output not found for input 0 (txid: ${h}, index: ${f.index})`
207
+ );
208
+ const y = l.outs[P.index];
209
+ if (!y)
210
+ throw new Error(
211
+ `Previous output not found for input 1 (txid: ${$}, index: ${P.index})`
212
+ );
213
+ he({
214
+ payoutTx: s,
215
+ peginValueSats: g.value,
216
+ claimerBtcPubkey: e.claimerBtcPubkey,
217
+ vaultProviderBtcPubkey: e.vaultProviderBtcPubkey,
218
+ depositorBtcPubkey: e.depositorBtcPubkey,
219
+ vaultKeeperBtcPubkeys: e.vaultKeeperBtcPubkeys,
220
+ registeredPayoutScriptPubKey: e.registeredPayoutScriptPubKey,
221
+ commissionBps: e.commissionBps
222
+ });
223
+ const v = g.value + y.value;
224
+ let b = 0;
225
+ for (const A of s.outs) b += A.value;
226
+ if (b > v)
227
+ throw new Error(
228
+ `Payout outputs (${b} sats) exceed inputs (${v} sats); invalid transaction.`
229
+ );
230
+ const V = v - b, U = Math.floor(
231
+ v * F / X
232
+ );
233
+ if (V > U)
234
+ throw new Error(
235
+ `Payout implicit fee ${V} sats exceeds the safety cap of ${U} sats (${F}/${X} of inputs=${v}); refusing to sign payout.`
236
+ );
237
+ p.addInput({
238
+ hash: f.hash,
239
+ index: f.index,
240
+ sequence: f.sequence,
241
+ witnessUtxo: {
242
+ script: g.script,
243
+ value: g.value
244
+ },
245
+ tapLeafScript: [
246
+ {
247
+ leafVersion: H,
248
+ script: a.from(i),
249
+ controlBlock: a.from(u)
250
+ }
251
+ ],
252
+ tapInternalKey: a.from(ee)
253
+ // sighashType omitted - defaults to SIGHASH_DEFAULT (0x00) for Taproot
254
+ }), p.addInput({
255
+ hash: P.hash,
256
+ index: P.index,
257
+ sequence: P.sequence,
258
+ witnessUtxo: {
259
+ script: y.script,
260
+ value: y.value
261
+ }
262
+ // No tapLeafScript - depositor doesn't sign this input
263
+ });
264
+ for (const A of s.outs)
265
+ p.addOutput({
266
+ script: A.script,
267
+ value: A.value
268
+ });
269
+ return {
270
+ psbtHex: p.toHex()
271
+ };
272
+ }
273
+ function he(e) {
274
+ const {
275
+ payoutTx: t,
276
+ peginValueSats: n,
277
+ claimerBtcPubkey: r,
278
+ vaultProviderBtcPubkey: o,
279
+ depositorBtcPubkey: i,
280
+ vaultKeeperBtcPubkeys: u,
281
+ registeredPayoutScriptPubKey: s,
282
+ commissionBps: c
283
+ } = e;
284
+ if (!re(s))
285
+ throw new Error("Invalid registeredPayoutScriptPubKey: not valid hex");
286
+ const l = x(r).toLowerCase(), p = x(o).toLowerCase(), f = x(i).toLowerCase(), P = u.map(
287
+ (y) => x(y).toLowerCase()
288
+ );
289
+ let h, w, $;
290
+ if (l === p)
291
+ h = "vp-claimer", w = le, $ = x(s);
292
+ else if (l === f)
293
+ h = "depositor-as-claimer", w = K, $ = x(s);
294
+ else if (P.includes(l))
295
+ h = "vk-claimer", w = K, $ = x(oe(l));
296
+ else
297
+ throw new Error(
298
+ `Unknown claimer pubkey ${l}: not VP, depositor, or a registered vault keeper`
299
+ );
300
+ if (t.outs.length !== w)
301
+ throw new Error(
302
+ `Payout transaction has ${t.outs.length} output(s), expected exactly ${w} for role ${h}.`
303
+ );
304
+ const d = a.from($, "hex");
305
+ if (!t.outs[0].script.equals(d))
306
+ throw new Error(
307
+ `Payout transaction output 0 does not pay the expected scriptPubKey for role ${h}`
308
+ );
309
+ const g = w - 1;
310
+ if (t.outs[g].value !== N)
311
+ throw new Error(
312
+ `Payout CPFP anchor (out ${g}) value ${t.outs[g].value} sats must equal ${N} sats`
313
+ );
314
+ if (h === "vp-claimer") {
315
+ if (!Number.isInteger(c) || c < 0 || c >= I)
316
+ throw new Error(
317
+ `commissionBps must be an integer in [0, ${I}), got ${c}`
318
+ );
319
+ const y = Math.floor(
320
+ n * c / I
321
+ );
322
+ if (t.outs[1].value > y)
323
+ throw new Error(
324
+ `Payout VP commission (out 1) value ${t.outs[1].value} sats exceeds cap ${y} sats (${c} bps of peginValue=${n})`
325
+ );
326
+ }
327
+ }
328
+ function Ce(e, t, n = 0) {
329
+ const r = T.fromHex(e);
330
+ if (n >= r.data.inputs.length)
331
+ throw new Error(
332
+ `Input index ${n} out of range (${r.data.inputs.length} inputs)`
333
+ );
334
+ const o = r.data.inputs[n];
335
+ if (o.tapScriptSig && o.tapScriptSig.length > 0) {
336
+ const i = E(t);
337
+ for (const u of o.tapScriptSig)
338
+ if (u.pubkey.equals(a.from(i)))
339
+ return W(u.signature, n);
340
+ throw new Error(
341
+ `No signature found for depositor pubkey: ${t} at input ${n}`
342
+ );
343
+ }
344
+ if (o.finalScriptWitness && o.finalScriptWitness.length > 0) {
345
+ const i = de(o.finalScriptWitness);
346
+ if (i.length !== M)
347
+ throw new Error(
348
+ `Unexpected finalized witness stack size at input ${n}: expected ${M} items (signature, script, controlBlock), got ${i.length}`
349
+ );
350
+ return W(i[0], n);
351
+ }
352
+ throw new Error(
353
+ `No tapScriptSig or finalScriptWitness found in signed PSBT at input ${n}`
354
+ );
355
+ }
356
+ function W(e, t) {
357
+ if (e.length === 64)
358
+ return _(new Uint8Array(e));
359
+ throw e.length === 65 ? new Error(
360
+ `Unexpected sighash byte 0x${e[64].toString(16).padStart(2, "0")} at input ${t}. Expected implicit SIGHASH_DEFAULT as a 64-byte signature.`
361
+ ) : new Error(
362
+ `Unexpected signature length at input ${t}: ${e.length}`
363
+ );
364
+ }
365
+ function de(e) {
366
+ const t = [];
367
+ let n = 0;
368
+ const r = (u) => {
369
+ if (n + u > e.length)
370
+ throw new Error(
371
+ `Malformed witness data: need ${u} byte(s) at offset ${n}, only ${e.length - n} remaining`
372
+ );
373
+ }, o = () => {
374
+ r(1);
375
+ const u = e[n++];
376
+ if (u < 253) return u;
377
+ if (u === 253) {
378
+ r(2);
379
+ const s = (e[n] | e[n + 1] << 8) >>> 0;
380
+ return n += 2, s;
381
+ }
382
+ if (u === 254) {
383
+ r(4);
384
+ const s = (e[n] | e[n + 1] << 8 | e[n + 2] << 16 | e[n + 3] << 24) >>> 0;
385
+ return n += 4, s;
386
+ }
387
+ throw new Error(
388
+ `Malformed witness data: 8-byte varint (0xff) not supported at offset ${n - 1}`
389
+ );
390
+ }, i = o();
391
+ for (let u = 0; u < i; u++) {
392
+ const s = o();
393
+ r(s), t.push(a.from(e.subarray(n, n + s))), n += s;
394
+ }
395
+ if (n !== e.length)
396
+ throw new Error(
397
+ `Malformed witness data: ${e.length - n} trailing byte(s) after parsing ${i} item(s)`
398
+ );
399
+ return t;
400
+ }
401
+ class m extends Error {
402
+ constructor(t) {
403
+ super(
404
+ `Wallet returned a PSBT for a different transaction: ${t}`
405
+ ), this.name = "PsbtSubstitutionError";
406
+ }
407
+ }
408
+ function z(e, t) {
409
+ try {
410
+ return T.fromHex(t);
411
+ } catch (n) {
412
+ const r = n instanceof Error ? n.message : String(n);
413
+ throw new Error(`Failed to parse ${e} PSBT: ${r}`);
414
+ }
415
+ }
416
+ const pe = 8;
417
+ function O(e) {
418
+ return `${e.toString("hex").slice(0, pe)}…`;
419
+ }
420
+ function D(e) {
421
+ const t = a.from(e).reverse();
422
+ return O(t);
423
+ }
424
+ function Ie(e) {
425
+ const t = z("requested", e.requestedPsbtHex), n = z("returned", e.returnedPsbtHex);
426
+ if (t.version !== n.version)
427
+ throw new m(
428
+ `tx version differs (requested=${t.version}, returned=${n.version})`
429
+ );
430
+ if (t.locktime !== n.locktime)
431
+ throw new m(
432
+ `tx locktime differs (requested=${t.locktime}, returned=${n.locktime})`
433
+ );
434
+ if (t.txInputs.length !== n.txInputs.length)
435
+ throw new m(
436
+ `input count differs (requested=${t.txInputs.length}, returned=${n.txInputs.length})`
437
+ );
438
+ if (t.txOutputs.length !== n.txOutputs.length)
439
+ throw new m(
440
+ `output count differs (requested=${t.txOutputs.length}, returned=${n.txOutputs.length})`
441
+ );
442
+ for (let r = 0; r < t.txInputs.length; r++) {
443
+ const o = t.txInputs[r], i = n.txInputs[r];
444
+ if (!o.hash.equals(i.hash))
445
+ throw new m(
446
+ `input ${r} prevout txid differs (requested=${D(o.hash)}, returned=${D(i.hash)})`
447
+ );
448
+ if (o.index !== i.index)
449
+ throw new m(
450
+ `input ${r} prevout vout differs (requested=${o.index}, returned=${i.index})`
451
+ );
452
+ if (o.sequence !== i.sequence)
453
+ throw new m(
454
+ `input ${r} sequence differs (requested=${o.sequence}, returned=${i.sequence})`
455
+ );
456
+ }
457
+ for (let r = 0; r < t.txOutputs.length; r++) {
458
+ const o = t.txOutputs[r], i = n.txOutputs[r];
459
+ if (!o.script.equals(i.script))
460
+ throw new m(
461
+ `output ${r} scriptPubKey differs (requested=${O(o.script)}, returned=${O(i.script)})`
462
+ );
463
+ if (o.value !== i.value)
464
+ throw new m(
465
+ `output ${r} value differs (requested=${o.value}, returned=${i.value})`
466
+ );
467
+ }
468
+ }
469
+ const Y = 253, fe = 254, ge = 65535, Pe = 4294967295;
470
+ function xe(e) {
471
+ if (e < Y)
472
+ return a.from([e]);
473
+ if (e <= ge) {
474
+ const t = a.alloc(2);
475
+ return t.writeUInt16LE(e), a.concat([a.from([Y]), t]);
476
+ }
477
+ if (e <= Pe) {
478
+ const t = a.alloc(4);
479
+ return t.writeUInt32LE(e), a.concat([a.from([fe]), t]);
480
+ }
481
+ throw new Error(`Script too large to encode as CompactSize: ${e} bytes`);
482
+ }
483
+ const we = "TapLeaf";
484
+ function $e(e, t) {
485
+ const n = a.concat([
486
+ a.from([e]),
487
+ xe(t.length),
488
+ a.from(t)
489
+ ]);
490
+ return ne.taggedHash(we, n);
491
+ }
492
+ function _e(e) {
493
+ const { requestedPsbtHex: t, signatureHex: n, signerXOnlyPubkeyHex: r, inputIndex: o } = e, i = x(n);
494
+ if (i.length !== B)
495
+ throw new Error(
496
+ `Schnorr signature for input ${o} must be ${B} hex chars (64 bytes), got ${i.length}.`
497
+ );
498
+ const u = x(r);
499
+ if (u.length !== R)
500
+ throw new Error(
501
+ `Signer x-only pubkey for input ${o} must be ${R} hex chars (32 bytes), got ${u.length}.`
502
+ );
503
+ const s = T.fromHex(t);
504
+ if (o < 0 || o >= s.data.inputs.length)
505
+ throw new Error(
506
+ `Input index ${o} out of range (${s.data.inputs.length} inputs).`
507
+ );
508
+ const c = [], l = [];
509
+ for (let d = 0; d < s.data.inputs.length; d++) {
510
+ const g = s.data.inputs[d].witnessUtxo;
511
+ if (!g)
512
+ throw new Error(
513
+ `Cannot verify signature: input ${d} of the requested PSBT has no witnessUtxo (required to recompute the Taproot sighash).`
514
+ );
515
+ c.push(g.script), l.push(g.value);
516
+ }
517
+ const p = s.data.inputs[o].tapLeafScript;
518
+ if (!p || p.length !== 1)
519
+ throw new Error(
520
+ `Cannot verify signature: input ${o} of the requested PSBT must have exactly one tapLeafScript, got ${(p == null ? void 0 : p.length) ?? 0}.`
521
+ );
522
+ const f = p[0];
523
+ if (f.leafVersion !== H)
524
+ throw new Error(
525
+ `Cannot verify signature: input ${o} tapLeafScript has leaf version 0x${f.leafVersion.toString(16)}, expected 0x${H.toString(16)}.`
526
+ );
527
+ const P = $e(f.leafVersion, f.script), h = new S();
528
+ h.version = s.version, h.locktime = s.locktime;
529
+ for (const d of s.txInputs)
530
+ h.addInput(d.hash, d.index, d.sequence);
531
+ for (const d of s.txOutputs)
532
+ h.addOutput(d.script, d.value);
533
+ const w = h.hashForWitnessV1(
534
+ o,
535
+ c,
536
+ l,
537
+ S.SIGHASH_DEFAULT,
538
+ P
539
+ );
540
+ if (!ie.verifySchnorr(
541
+ w,
542
+ E(u),
543
+ E(i)
544
+ ))
545
+ throw new Error(
546
+ `Schnorr signature for input ${o} (signer ${u}) does not verify against the expected Taproot script-path sighash. The wallet may have signed with the tweaked key, signed a different transaction, or returned an invalid signature.`
547
+ );
548
+ }
549
+ export {
550
+ C as A,
551
+ Te as D,
552
+ m as P,
553
+ be as a,
554
+ Ae as b,
555
+ ke as c,
556
+ Ie as d,
557
+ Ce as e,
558
+ _e as f,
559
+ ae as g,
560
+ k as h,
561
+ G as n
562
+ };
563
+ //# sourceMappingURL=verifyScriptPathSchnorrSignature-DFJAEleY.js.map