@babylonlabs-io/ts-sdk 0.67.0 → 0.68.1

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 (151) hide show
  1. package/dist/PeginManager-Bsv6caG3.js +1466 -0
  2. package/dist/PeginManager-Bsv6caG3.js.map +1 -0
  3. package/dist/PeginManager-C5NWzGT7.cjs +2 -0
  4. package/dist/PeginManager-C5NWzGT7.cjs.map +1 -0
  5. package/dist/{batchPoll-cIxrSLLJ.js → batchPoll-Bc_UKaTz.js} +372 -421
  6. package/dist/batchPoll-Bc_UKaTz.js.map +1 -0
  7. package/dist/batchPoll-C9cZ5usJ.cjs +2 -0
  8. package/dist/batchPoll-C9cZ5usJ.cjs.map +1 -0
  9. package/dist/{bitcoin-d9eaaZB_.js → bitcoin-Ch_jxnDa.js} +3 -3
  10. package/dist/{bitcoin-d9eaaZB_.js.map → bitcoin-Ch_jxnDa.js.map} +1 -1
  11. package/dist/{buildAndBroadcastRefund-C4LiPATK.js → buildAndBroadcastRefund-CpMDJ8up.js} +142 -150
  12. package/dist/buildAndBroadcastRefund-CpMDJ8up.js.map +1 -0
  13. package/dist/buildAndBroadcastRefund-DrXwxrW1.cjs +2 -0
  14. package/dist/buildAndBroadcastRefund-DrXwxrW1.cjs.map +1 -0
  15. package/dist/challengeAssert-BNQ8sToz.js +64 -0
  16. package/dist/challengeAssert-BNQ8sToz.js.map +1 -0
  17. package/dist/challengeAssert-BrpC3-cQ.cjs +2 -0
  18. package/dist/challengeAssert-BrpC3-cQ.cjs.map +1 -0
  19. package/dist/index.cjs +1 -1
  20. package/dist/index.js +246 -243
  21. package/dist/{mempoolApi-BxSINXIB.js → mempoolApi-71R_CNZa.js} +193 -117
  22. package/dist/mempoolApi-71R_CNZa.js.map +1 -0
  23. package/dist/mempoolApi-BJN9fGEd.cjs +2 -0
  24. package/dist/mempoolApi-BJN9fGEd.cjs.map +1 -0
  25. package/dist/{noPayout-BaZGRLD2.js → noPayout-CXn-geIS.js} +3 -3
  26. package/dist/{noPayout-BaZGRLD2.js.map → noPayout-CXn-geIS.js.map} +1 -1
  27. package/dist/{noPayout-CnQ1WLtr.cjs → noPayout-Ox27Td9x.cjs} +2 -2
  28. package/dist/{noPayout-CnQ1WLtr.cjs.map → noPayout-Ox27Td9x.cjs.map} +1 -1
  29. package/dist/{onChainBtcPubkey-DeF4cKoO.js → onChainBtcPubkey-BAHZyKQL.js} +2 -2
  30. package/dist/{onChainBtcPubkey-DeF4cKoO.js.map → onChainBtcPubkey-BAHZyKQL.js.map} +1 -1
  31. package/dist/{parseFundingOutpoints-pBRE11Hr.js → parseFundingOutpoints-CO-f6IHK.js} +2 -2
  32. package/dist/{parseFundingOutpoints-pBRE11Hr.js.map → parseFundingOutpoints-CO-f6IHK.js.map} +1 -1
  33. package/dist/{reservation-B4i_PQhY.js → reservation-CBAvoF3U.js} +2 -2
  34. package/dist/{reservation-B4i_PQhY.js.map → reservation-CBAvoF3U.js.map} +1 -1
  35. package/dist/{signPsbtsWithFallback-Bb66DfY5.js → signPsbtsWithFallback-Bv79gLFe.js} +122 -100
  36. package/dist/signPsbtsWithFallback-Bv79gLFe.js.map +1 -0
  37. package/dist/signPsbtsWithFallback-DJ4kE9UI.cjs +2 -0
  38. package/dist/signPsbtsWithFallback-DJ4kE9UI.cjs.map +1 -0
  39. package/dist/tbv/core/clients/index.cjs +1 -1
  40. package/dist/tbv/core/clients/index.js +3 -3
  41. package/dist/tbv/core/clients/vault-provider/auth/__tests__/bip322P2wpkhVectors.d.ts +32 -0
  42. package/dist/tbv/core/clients/vault-provider/auth/__tests__/bip322P2wpkhVectors.d.ts.map +1 -0
  43. package/dist/tbv/core/clients/vault-provider/auth/__tests__/bip322Verify.test.d.ts +9 -6
  44. package/dist/tbv/core/clients/vault-provider/auth/__tests__/bip322Verify.test.d.ts.map +1 -1
  45. package/dist/tbv/core/clients/vault-provider/auth/bip322Verify.d.ts +54 -20
  46. package/dist/tbv/core/clients/vault-provider/auth/bip322Verify.d.ts.map +1 -1
  47. package/dist/tbv/core/deposit-terms/__tests__/depositTerms.test.d.ts +2 -0
  48. package/dist/tbv/core/deposit-terms/__tests__/depositTerms.test.d.ts.map +1 -0
  49. package/dist/tbv/core/deposit-terms/depositTerms.d.ts +41 -4
  50. package/dist/tbv/core/deposit-terms/depositTerms.d.ts.map +1 -1
  51. package/dist/tbv/core/deposit-terms/prePeginApproval.d.ts +10 -5
  52. package/dist/tbv/core/deposit-terms/prePeginApproval.d.ts.map +1 -1
  53. package/dist/tbv/core/index.cjs +1 -1
  54. package/dist/tbv/core/index.js +244 -241
  55. package/dist/tbv/core/managers/PayoutManager.d.ts +8 -2
  56. package/dist/tbv/core/managers/PayoutManager.d.ts.map +1 -1
  57. package/dist/tbv/core/managers/PeginManager.d.ts +7 -0
  58. package/dist/tbv/core/managers/PeginManager.d.ts.map +1 -1
  59. package/dist/tbv/core/managers/index.cjs +1 -1
  60. package/dist/tbv/core/managers/index.js +2 -2
  61. package/dist/tbv/core/managers/pegin/__tests__/verifyPopWitness.test.d.ts +11 -0
  62. package/dist/tbv/core/managers/pegin/__tests__/verifyPopWitness.test.d.ts.map +1 -0
  63. package/dist/tbv/core/managers/pegin/expandPerVaultSecrets.d.ts.map +1 -1
  64. package/dist/tbv/core/managers/pegin/index.d.ts +1 -0
  65. package/dist/tbv/core/managers/pegin/index.d.ts.map +1 -1
  66. package/dist/tbv/core/managers/pegin/verifyPopWitness.d.ts +21 -0
  67. package/dist/tbv/core/managers/pegin/verifyPopWitness.d.ts.map +1 -0
  68. package/dist/tbv/core/primitives/challengers.d.ts +31 -5
  69. package/dist/tbv/core/primitives/challengers.d.ts.map +1 -1
  70. package/dist/tbv/core/primitives/index.cjs +1 -1
  71. package/dist/tbv/core/primitives/index.d.ts +2 -0
  72. package/dist/tbv/core/primitives/index.d.ts.map +1 -1
  73. package/dist/tbv/core/primitives/index.js +43 -41
  74. package/dist/tbv/core/primitives/psbt/__tests__/helpers.d.ts +5 -0
  75. package/dist/tbv/core/primitives/psbt/__tests__/helpers.d.ts.map +1 -1
  76. package/dist/tbv/core/primitives/psbt/__tests__/verifyKeyPathSchnorrSignature.test.d.ts +12 -0
  77. package/dist/tbv/core/primitives/psbt/__tests__/verifyKeyPathSchnorrSignature.test.d.ts.map +1 -0
  78. package/dist/tbv/core/primitives/psbt/payout.d.ts +19 -7
  79. package/dist/tbv/core/primitives/psbt/payout.d.ts.map +1 -1
  80. package/dist/tbv/core/primitives/psbt/verifyKeyPathSchnorrSignature.d.ts +65 -0
  81. package/dist/tbv/core/primitives/psbt/verifyKeyPathSchnorrSignature.d.ts.map +1 -0
  82. package/dist/tbv/core/primitives/psbt/verifyP2wpkhEcdsaSignature.d.ts +40 -0
  83. package/dist/tbv/core/primitives/psbt/verifyP2wpkhEcdsaSignature.d.ts.map +1 -0
  84. package/dist/tbv/core/primitives/psbt/verifyScriptPathSchnorrSignature.d.ts +2 -1
  85. package/dist/tbv/core/primitives/psbt/verifyScriptPathSchnorrSignature.d.ts.map +1 -1
  86. package/dist/tbv/core/primitives/utils/__tests__/taproot.test.d.ts +13 -0
  87. package/dist/tbv/core/primitives/utils/__tests__/taproot.test.d.ts.map +1 -0
  88. package/dist/tbv/core/primitives/utils/taproot.d.ts +29 -0
  89. package/dist/tbv/core/primitives/utils/taproot.d.ts.map +1 -0
  90. package/dist/tbv/core/services/deposit/runDepositorPresignFlow.d.ts +2 -2
  91. package/dist/tbv/core/services/deposit/runDepositorPresignFlow.d.ts.map +1 -1
  92. package/dist/tbv/core/services/deposit/signDepositorGraph.d.ts.map +1 -1
  93. package/dist/tbv/core/services/index.cjs +1 -1
  94. package/dist/tbv/core/services/index.js +2 -2
  95. package/dist/tbv/core/utils/index.js +2 -2
  96. package/dist/tbv/core/utils/witness/witnessStack.d.ts +24 -0
  97. package/dist/tbv/core/utils/witness/witnessStack.d.ts.map +1 -0
  98. package/dist/tbv/core/vault-secrets/index.js +1 -1
  99. package/dist/tbv/index.cjs +1 -1
  100. package/dist/tbv/index.js +244 -241
  101. package/dist/testing/MockBitcoinWallet.d.ts +9 -0
  102. package/dist/testing/MockBitcoinWallet.d.ts.map +1 -1
  103. package/dist/testing/__tests__/MockBitcoinWallet.test.d.ts +2 -0
  104. package/dist/testing/__tests__/MockBitcoinWallet.test.d.ts.map +1 -0
  105. package/dist/testing/index.cjs +1 -1
  106. package/dist/testing/index.cjs.map +1 -1
  107. package/dist/testing/index.js +136 -45
  108. package/dist/testing/index.js.map +1 -1
  109. package/dist/testing/signBip322P2wpkhWitness.d.ts +25 -0
  110. package/dist/testing/signBip322P2wpkhWitness.d.ts.map +1 -0
  111. package/dist/{toRefundInputs-BY_fcvrv.js → toRefundInputs-4UJfSVEz.js} +6 -6
  112. package/dist/{toRefundInputs-BY_fcvrv.js.map → toRefundInputs-4UJfSVEz.js.map} +1 -1
  113. package/dist/{toRefundInputs-DYL9C-aG.cjs → toRefundInputs-DgDhx_Ut.cjs} +2 -2
  114. package/dist/{toRefundInputs-DYL9C-aG.cjs.map → toRefundInputs-DgDhx_Ut.cjs.map} +1 -1
  115. package/dist/verifyKeyPathSchnorrSignature-C6Cmk0R1.js +372 -0
  116. package/dist/verifyKeyPathSchnorrSignature-C6Cmk0R1.js.map +1 -0
  117. package/dist/verifyKeyPathSchnorrSignature-Cr5T4Aym.cjs +2 -0
  118. package/dist/verifyKeyPathSchnorrSignature-Cr5T4Aym.cjs.map +1 -0
  119. package/dist/verifyScriptPathSchnorrSignature-Dy9gF5RF.js +931 -0
  120. package/dist/verifyScriptPathSchnorrSignature-Dy9gF5RF.js.map +1 -0
  121. package/dist/verifyScriptPathSchnorrSignature-la8XOAFm.cjs +2 -0
  122. package/dist/verifyScriptPathSchnorrSignature-la8XOAFm.cjs.map +1 -0
  123. package/package.json +1 -1
  124. package/dist/PeginManager-BBl4xkWz.js +0 -1359
  125. package/dist/PeginManager-BBl4xkWz.js.map +0 -1
  126. package/dist/PeginManager-p-ajwSxG.cjs +0 -2
  127. package/dist/PeginManager-p-ajwSxG.cjs.map +0 -1
  128. package/dist/batchPoll-6L5oToGr.cjs +0 -2
  129. package/dist/batchPoll-6L5oToGr.cjs.map +0 -1
  130. package/dist/batchPoll-cIxrSLLJ.js.map +0 -1
  131. package/dist/buildAndBroadcastRefund-C4LiPATK.js.map +0 -1
  132. package/dist/buildAndBroadcastRefund-Daw5ZUKR.cjs +0 -2
  133. package/dist/buildAndBroadcastRefund-Daw5ZUKR.cjs.map +0 -1
  134. package/dist/challengeAssert-BFRE2DM-.cjs +0 -2
  135. package/dist/challengeAssert-BFRE2DM-.cjs.map +0 -1
  136. package/dist/challengeAssert-C7PzyJ7g.js +0 -75
  137. package/dist/challengeAssert-C7PzyJ7g.js.map +0 -1
  138. package/dist/mempoolApi-BxSINXIB.js.map +0 -1
  139. package/dist/mempoolApi-CXfr-L6x.cjs +0 -2
  140. package/dist/mempoolApi-CXfr-L6x.cjs.map +0 -1
  141. package/dist/peginInput-Ci921gTl.cjs +0 -2
  142. package/dist/peginInput-Ci921gTl.cjs.map +0 -1
  143. package/dist/peginInput-HQPWZfly.js +0 -123
  144. package/dist/peginInput-HQPWZfly.js.map +0 -1
  145. package/dist/signPsbtsWithFallback-Bb66DfY5.js.map +0 -1
  146. package/dist/signPsbtsWithFallback-PTgWxsG3.cjs +0 -2
  147. package/dist/signPsbtsWithFallback-PTgWxsG3.cjs.map +0 -1
  148. package/dist/verifyScriptPathSchnorrSignature-DPAd_qke.cjs +0 -2
  149. package/dist/verifyScriptPathSchnorrSignature-DPAd_qke.cjs.map +0 -1
  150. package/dist/verifyScriptPathSchnorrSignature-i-koT139.js +0 -817
  151. package/dist/verifyScriptPathSchnorrSignature-i-koT139.js.map +0 -1
@@ -0,0 +1,931 @@
1
+ import { computeMinClaimValue as se, peginP2aAnchorOutput as ue, computeMinPeginFee as pe, buildPeginTxFromPrePegin as Pe, createPrePeginTransaction as fe, validatePeginP2aAnchor as ge, tapInternalPubkey as V, createPayoutConnector as we, computePayoutFeeFloor as ve, getAssertPayoutScriptInfo as $e } from "@babylonlabs-io/babylon-tbv-rust-wasm";
2
+ import { Buffer as a } from "buffer";
3
+ import { Transaction as E, script as ye, opcodes as me, payments as Se, crypto as L, Psbt as _ } from "bitcoinjs-lib";
4
+ import { c as K, d as xe } from "./fundPeginTransaction-B2wyjqp0.js";
5
+ import { p as Ee, s as p, u as H, h as S, j as Ce, T as C, d as be, S as B, X as N } from "./bitcoin-Ch_jxnDa.js";
6
+ import { D as U, P as M, b as F, c as q, M as W, d as X, e as Ae, A as ce, B as ke, V as _e, N as z } from "./constants-GjWXdaqV.js";
7
+ import * as ae from "@bitcoin-js/tiny-secp256k1-asmjs";
8
+ function x(e) {
9
+ return Ee(e).toLowerCase();
10
+ }
11
+ function Et(e, t, o) {
12
+ const n = /* @__PURE__ */ new Set();
13
+ n.add(x(e));
14
+ for (const i of t)
15
+ n.add(x(i));
16
+ return n.delete(x(o)), n.size;
17
+ }
18
+ function Te(e) {
19
+ const t = x(e.claimerBtcPubkey), o = x(e.depositorBtcPubkey), n = e.vaultKeeperBtcPubkeys.map(x);
20
+ if (t === o) {
21
+ const s = n.filter((u) => u !== o);
22
+ if (s.length === 0)
23
+ throw new Error(
24
+ "Cannot derive localChallengers: vault keeper set is empty (or contains only the depositor)"
25
+ );
26
+ if (new Set(s).size !== s.length)
27
+ throw new Error(
28
+ "Cannot derive localChallengers: duplicate vaultKeeper key — signing context is misconfigured"
29
+ );
30
+ return s;
31
+ }
32
+ const r = [
33
+ .../* @__PURE__ */ new Set([x(e.vaultProviderBtcPubkey), ...n])
34
+ ].sort().filter((s) => s !== t);
35
+ if (r.length === 0)
36
+ throw new Error(
37
+ `Cannot derive localChallengers: no vault provider or vault keeper remains after excluding claimer ${t}`
38
+ );
39
+ return r;
40
+ }
41
+ async function Ie(e, t) {
42
+ const o = t.pegInAmounts.length;
43
+ if (e.htlcValues.length !== o)
44
+ throw new Error(
45
+ `WASM Pre-PegIn returned ${e.htlcValues.length} HTLC value(s), expected ${o} (one per requested deposit).`
46
+ );
47
+ if (e.peginAmounts.length !== o || e.htlcScriptPubKeys.length !== o || e.htlcAddresses.length !== o)
48
+ throw new Error(
49
+ `WASM Pre-PegIn returned mismatched array lengths (htlcValues=${e.htlcValues.length}, peginAmounts=${e.peginAmounts.length}, htlcScriptPubKeys=${e.htlcScriptPubKeys.length}, htlcAddresses=${e.htlcAddresses.length}); expected ${o} each.`
50
+ );
51
+ if (e.depositorClaimValue <= 0n)
52
+ throw new Error(
53
+ `WASM Pre-PegIn returned non-positive depositorClaimValue ${e.depositorClaimValue}; expected > 0.`
54
+ );
55
+ const n = await se(
56
+ // Must price the same graph version the builder constructed.
57
+ t.vaultCoreVersion,
58
+ t.numLocalChallengers,
59
+ t.universalChallengerPubkeys.length,
60
+ t.councilQuorum,
61
+ t.councilSize,
62
+ t.feeRate
63
+ );
64
+ if (e.depositorClaimValue !== n)
65
+ throw new Error(
66
+ `WASM Pre-PegIn depositorClaimValue ${e.depositorClaimValue} does not match the independently computed minimum claim value ${n} (vaultCoreVersion=${t.vaultCoreVersion}, numLocalChallengers=${t.numLocalChallengers}, numUniversalChallengers=${t.universalChallengerPubkeys.length}, councilQuorum=${t.councilQuorum}, councilSize=${t.councilSize}, feeRate=${t.feeRate}).`
67
+ );
68
+ const i = await ue(t.vaultCoreVersion), r = (i == null ? void 0 : i.value) ?? 0n, s = await pe(
69
+ t.vaultCoreVersion,
70
+ t.vaultKeeperPubkeys.length,
71
+ t.universalChallengerPubkeys.length,
72
+ t.minPeginFeeRate
73
+ ), u = s + r, h = t.minPeginFeeRate * K;
74
+ for (let c = 0; c < o; c++) {
75
+ const d = t.pegInAmounts[c], l = e.peginAmounts[c], P = e.htlcValues[c];
76
+ if (l !== d)
77
+ throw new Error(
78
+ `WASM Pre-PegIn peginAmount[${c}] ${l} does not match the requested amount ${d}; refusing to build a tx whose recorded amount differs from the depositor's request.`
79
+ );
80
+ if (l <= 0n)
81
+ throw new Error(
82
+ `WASM Pre-PegIn peginAmount[${c}] is non-positive (${l}); expected > 0.`
83
+ );
84
+ if (P <= 0n)
85
+ throw new Error(
86
+ `WASM Pre-PegIn htlcValue[${c}] is non-positive (${P}); expected > 0.`
87
+ );
88
+ const f = P - l - e.depositorClaimValue;
89
+ if (f <= 0n)
90
+ throw new Error(
91
+ `WASM Pre-PegIn htlcValue[${c}] ${P} does not strictly cover peginAmount ${l} + depositorClaimValue ${e.depositorClaimValue} + a PegIn reserve (implied reserve ${f}).`
92
+ );
93
+ if (f > h)
94
+ throw new Error(
95
+ `WASM Pre-PegIn implied reserve for HTLC[${c}] (${f} sat) exceeds the plausibility cap ${h} sat (minPeginFeeRate=${t.minPeginFeeRate} × ${K} vbytes); htlcValue ${P} appears grossly inflated.`
96
+ );
97
+ if (f !== u)
98
+ throw new Error(
99
+ `WASM Pre-PegIn htlcValue[${c}] ${P} implies a PegIn fee+anchor reserve of ${f} sat, expected exactly ${u} sat (minPeginFee ${s} + p2aAnchor ${r} for vaultCoreVersion ${t.vaultCoreVersion}, vaultKeepers=${t.vaultKeeperPubkeys.length}, universalChallengers=${t.universalChallengerPubkeys.length}, minPeginFeeRate=${t.minPeginFeeRate}).`
100
+ );
101
+ }
102
+ return s;
103
+ }
104
+ class I extends Error {
105
+ constructor(t) {
106
+ super(t), this.name = "HtlcOutputMismatchError";
107
+ }
108
+ }
109
+ function Ve(e, t, o) {
110
+ if (e.length < t.length)
111
+ throw new I(
112
+ `Encoded Pre-PegIn tx has ${e.length} output(s), fewer than the ${t.length} HTLC output(s) the cross-check validated.`
113
+ );
114
+ for (let n = 0; n < t.length; n++) {
115
+ const i = BigInt(e[n].value);
116
+ if (i !== t[n])
117
+ throw new I(
118
+ `Encoded Pre-PegIn HTLC output[${n}] value ${i} does not match the cross-checked htlcValue ${t[n]}; the funded/signed tx would not pay the validated amount.`
119
+ );
120
+ const r = e[n].script.toString("hex").toLowerCase(), s = o[n].toLowerCase();
121
+ if (r !== s)
122
+ throw new I(
123
+ `Encoded Pre-PegIn HTLC output[${n}] scriptPubKey ${r} does not match the cross-checked htlcScriptPubKey ${s}.`
124
+ );
125
+ }
126
+ }
127
+ const D = 64, Le = /^[0-9a-fA-F]+$/;
128
+ async function Ct(e) {
129
+ const t = le(e.authAnchorHash), o = await fe({
130
+ txGraphVersion: e.vaultCoreVersion,
131
+ depositorPubkey: e.depositorPubkey,
132
+ vaultProviderPubkey: e.vaultProviderPubkey,
133
+ vaultKeeperPubkeys: e.vaultKeeperPubkeys,
134
+ universalChallengerPubkeys: e.universalChallengerPubkeys,
135
+ hashlocks: [...e.hashlocks],
136
+ timelockRefund: e.timelockRefund,
137
+ pegInAmounts: [...e.pegInAmounts],
138
+ feeRate: e.feeRate,
139
+ minPeginFeeRate: e.minPeginFeeRate,
140
+ numLocalChallengers: e.numLocalChallengers,
141
+ councilQuorum: e.councilQuorum,
142
+ councilSize: e.councilSize,
143
+ network: e.network,
144
+ authAnchorHash: t
145
+ }), n = await Ie(o, e), i = xe(o.txHex);
146
+ Ve(
147
+ i.outputs,
148
+ o.htlcValues,
149
+ o.htlcScriptPubKeys
150
+ );
151
+ const r = i.outputs.reduce(
152
+ (u, h) => u + BigInt(h.value),
153
+ 0n
154
+ ), s = t !== void 0 ? o.htlcValues.length : null;
155
+ return {
156
+ psbtHex: o.txHex,
157
+ totalOutputValue: r,
158
+ htlcValues: o.htlcValues,
159
+ htlcScriptPubKeys: o.htlcScriptPubKeys,
160
+ htlcAddresses: o.htlcAddresses,
161
+ peginAmounts: o.peginAmounts,
162
+ depositorClaimValue: o.depositorClaimValue,
163
+ authAnchorVout: s,
164
+ minPeginFee: n
165
+ };
166
+ }
167
+ function le(e) {
168
+ if (e === void 0) return;
169
+ const t = e.startsWith("0x") || e.startsWith("0X") ? e.slice(2) : e;
170
+ if (t.length !== D || !Le.test(t))
171
+ throw new Error(
172
+ `authAnchorHash must be 32-byte hex (${D} chars, no 0x prefix); got length ${t.length}`
173
+ );
174
+ return t.toLowerCase();
175
+ }
176
+ async function bt(e) {
177
+ const t = await Pe(
178
+ {
179
+ txGraphVersion: e.prePeginParams.vaultCoreVersion,
180
+ depositorPubkey: e.prePeginParams.depositorPubkey,
181
+ vaultProviderPubkey: e.prePeginParams.vaultProviderPubkey,
182
+ vaultKeeperPubkeys: e.prePeginParams.vaultKeeperPubkeys,
183
+ universalChallengerPubkeys: e.prePeginParams.universalChallengerPubkeys,
184
+ hashlocks: [...e.prePeginParams.hashlocks],
185
+ timelockRefund: e.prePeginParams.timelockRefund,
186
+ pegInAmounts: [...e.prePeginParams.pegInAmounts],
187
+ feeRate: e.prePeginParams.feeRate,
188
+ minPeginFeeRate: e.prePeginParams.minPeginFeeRate,
189
+ numLocalChallengers: e.prePeginParams.numLocalChallengers,
190
+ councilQuorum: e.prePeginParams.councilQuorum,
191
+ councilSize: e.prePeginParams.councilSize,
192
+ network: e.prePeginParams.network,
193
+ authAnchorHash: le(
194
+ e.prePeginParams.authAnchorHash
195
+ )
196
+ },
197
+ e.timelockPegin,
198
+ e.fundedPrePeginTxHex,
199
+ e.htlcVout
200
+ );
201
+ return await Re(t, e), {
202
+ txHex: t.txHex,
203
+ txid: t.txid,
204
+ vaultScriptPubKey: t.vaultScriptPubKey,
205
+ vaultValue: t.vaultValue
206
+ };
207
+ }
208
+ const Oe = 2, He = 1;
209
+ async function Re(e, t) {
210
+ const o = t.prePeginParams.vaultCoreVersion;
211
+ await ge(o, e.txHex);
212
+ const n = await ue(o), i = Oe + (n ? 1 : 0), r = E.fromHex(p(e.txHex));
213
+ if (r.ins.length !== 1)
214
+ throw new Error(
215
+ `PegIn tx has ${r.ins.length} input(s), expected exactly 1 (the Pre-PegIn HTLC outpoint).`
216
+ );
217
+ const s = E.fromHex(
218
+ p(t.fundedPrePeginTxHex)
219
+ ).getId(), u = H(
220
+ new Uint8Array(r.ins[0].hash).slice().reverse()
221
+ );
222
+ if (u !== s)
223
+ throw new Error(
224
+ `PegIn input spends txid ${u}, expected the funded Pre-PegIn ${s}.`
225
+ );
226
+ if (r.ins[0].index !== t.htlcVout)
227
+ throw new Error(
228
+ `PegIn input spends Pre-PegIn output ${r.ins[0].index}, expected the requested HTLC vout ${t.htlcVout}.`
229
+ );
230
+ if (r.outs.length !== i)
231
+ throw new Error(
232
+ `PegIn tx has ${r.outs.length} output(s), expected exactly ${i} for vaultCoreVersion ${o} (vault + depositor claim${n ? " + P2A anchor" : ""}).`
233
+ );
234
+ const h = t.prePeginParams.pegInAmounts[t.htlcVout];
235
+ if (e.vaultValue !== h)
236
+ throw new Error(
237
+ `PegIn vault output value ${e.vaultValue} does not match the requested peg-in amount ${h} for HTLC ${t.htlcVout}; refusing to sign a vault that locks a different amount than requested.`
238
+ );
239
+ const c = r.outs[0];
240
+ if (BigInt(c.value) !== e.vaultValue)
241
+ throw new Error(
242
+ `Encoded PegIn vault output value ${c.value} does not match the WASM-reported vaultValue ${e.vaultValue}.`
243
+ );
244
+ const d = c.script.toString("hex"), l = p(
245
+ e.vaultScriptPubKey
246
+ ).toLowerCase();
247
+ if (d.toLowerCase() !== l)
248
+ throw new Error(
249
+ `Encoded PegIn vault output scriptPubKey ${d} does not match the WASM-reported vaultScriptPubKey ${e.vaultScriptPubKey}.`
250
+ );
251
+ const P = r.getId();
252
+ if (P !== p(e.txid).toLowerCase())
253
+ throw new Error(
254
+ `Encoded PegIn txid ${P} does not match the WASM-reported txid ${e.txid}.`
255
+ );
256
+ const f = await se(
257
+ o,
258
+ t.prePeginParams.numLocalChallengers,
259
+ t.prePeginParams.universalChallengerPubkeys.length,
260
+ t.prePeginParams.councilQuorum,
261
+ t.prePeginParams.councilSize,
262
+ t.prePeginParams.feeRate
263
+ ), v = r.outs[He];
264
+ if (BigInt(v.value) !== f)
265
+ throw new Error(
266
+ `Encoded PegIn depositor-claim output value ${v.value} does not match the independently computed claim value ${f} for vaultCoreVersion ${o}.`
267
+ );
268
+ const w = Ke(
269
+ t.prePeginParams.depositorPubkey
270
+ );
271
+ if (!v.script.equals(w))
272
+ throw new Error(
273
+ `Encoded PegIn depositor-claim output scriptPubKey ${v.script.toString("hex")} does not pay to the depositor's claim script (expected ${w.toString("hex")}).`
274
+ );
275
+ }
276
+ function Ke(e) {
277
+ const t = ye.compile([
278
+ a.from(S(e)),
279
+ me.OP_CHECKSIG
280
+ ]), { output: o } = Se.p2tr({
281
+ internalPubkey: a.from(V),
282
+ scriptTree: { output: t }
283
+ });
284
+ if (!o)
285
+ throw new Error(
286
+ "Failed to derive the depositor-claim P2TR scriptPubKey for PegIn output validation"
287
+ );
288
+ return o;
289
+ }
290
+ async function Be(e) {
291
+ const t = await we(
292
+ {
293
+ txGraphVersion: e.vaultCoreVersion,
294
+ depositor: e.depositor,
295
+ vaultProvider: e.vaultProvider,
296
+ vaultKeepers: e.vaultKeepers,
297
+ universalChallengers: e.universalChallengers,
298
+ timelockPegin: e.timelockPegin
299
+ },
300
+ e.network
301
+ );
302
+ return {
303
+ payoutScript: t.payoutScript,
304
+ taprootScriptHash: t.taprootScriptHash,
305
+ scriptPubKey: t.scriptPubKey,
306
+ address: t.address,
307
+ payoutControlBlock: t.payoutControlBlock
308
+ };
309
+ }
310
+ const G = 253, Ne = 254, Ue = 65535, Me = 4294967295, Fe = "TapLeaf", qe = "TapBranch", We = "TapTweak", A = 33, k = 32, Y = 128, Xe = 1, ze = 254, De = a.from([81, 32]);
311
+ function Ge(e) {
312
+ if (e < G)
313
+ return a.from([e]);
314
+ if (e <= Ue) {
315
+ const t = a.alloc(2);
316
+ return t.writeUInt16LE(e), a.concat([a.from([G]), t]);
317
+ }
318
+ if (e <= Me) {
319
+ const t = a.alloc(4);
320
+ return t.writeUInt32LE(e), a.concat([a.from([Ne]), t]);
321
+ }
322
+ throw new Error(`Script too large to encode as CompactSize: ${e} bytes`);
323
+ }
324
+ function he(e, t) {
325
+ const o = a.concat([
326
+ a.from([e]),
327
+ Ge(t.length),
328
+ a.from(t)
329
+ ]);
330
+ return L.taggedHash(Fe, o);
331
+ }
332
+ function Ye(e) {
333
+ const { leafVersion: t, script: o, controlBlock: n } = e, i = n.length - A;
334
+ if (i < 0 || i % k !== 0 || i / k > Y)
335
+ throw new Error(
336
+ `Malformed Taproot control block: length ${n.length} must be ${A} + 32*m with 0 <= m <= ${Y}`
337
+ );
338
+ const r = n[0] & ze;
339
+ if (r !== t)
340
+ throw new Error(
341
+ `Taproot control block leaf version 0x${r.toString(16)} does not match the tapLeafScript leaf version 0x${t.toString(16)}`
342
+ );
343
+ const s = a.from(
344
+ n.subarray(1, A)
345
+ );
346
+ let u = he(t, o);
347
+ for (let l = A; l < n.length; l += k) {
348
+ const P = a.from(
349
+ n.subarray(l, l + k)
350
+ );
351
+ u = L.taggedHash(
352
+ qe,
353
+ a.compare(u, P) <= 0 ? a.concat([u, P]) : a.concat([P, u])
354
+ );
355
+ }
356
+ const h = L.taggedHash(
357
+ We,
358
+ a.concat([s, u])
359
+ ), c = ae.xOnlyPointAddTweak(s, h);
360
+ if (c === null)
361
+ throw new Error(
362
+ "Taproot control block does not yield a valid output key (tweak is off-curve)"
363
+ );
364
+ const d = n[0] & Xe;
365
+ if (c.parity !== d)
366
+ throw new Error(
367
+ `Taproot output key parity ${c.parity} contradicts the control block's parity bit ${d}`
368
+ );
369
+ return a.concat([
370
+ De,
371
+ a.from(c.xOnlyPubkey)
372
+ ]);
373
+ }
374
+ const Qe = 256, Ze = 1500, Q = 256, Z = 0xffffffffn, je = 500, Je = 55, et = 34;
375
+ function tt(e) {
376
+ if (typeof e.protocolFeeRate != "bigint" || e.protocolFeeRate <= 0n || e.protocolFeeRate > Z)
377
+ throw new Error(
378
+ `protocolFeeRate must be in [1, ${Z}] sat/vB, got ${e.protocolFeeRate}`
379
+ );
380
+ for (const [t, o, n, i] of [
381
+ [
382
+ "keepers",
383
+ e.numVaultKeepers,
384
+ Qe,
385
+ "fee-floor model limit"
386
+ ],
387
+ [
388
+ "challengers",
389
+ e.numUniversalChallengers,
390
+ Ze,
391
+ "protocol maximum"
392
+ ]
393
+ ])
394
+ if (!Number.isInteger(o) || o < 1 || o > n)
395
+ throw new Error(
396
+ `Participant count for ${t} (${o}) is outside the supported range [1, ${n}]${o > n ? ` (${i})` : ""}.`
397
+ );
398
+ if (!Number.isInteger(e.councilSize) || e.councilSize < 1 || e.councilSize > Q)
399
+ throw new Error(
400
+ `councilSize must be an integer in [1, ${Q}], got ${e.councilSize}`
401
+ );
402
+ }
403
+ async function ot(e, t) {
404
+ const { implicitFeeSats: o, out0Len: n, out1Len: i } = t, r = e.numVaultKeepers + e.numUniversalChallengers, s = BigInt(o), u = Math.max(0, n - et), h = je + Je * r + u, c = e.protocolFeeRate * BigInt(h);
405
+ if (s > c)
406
+ throw new Error(
407
+ `Payout implicit fee ${o} sats exceeds the safety cap of ${c} sats (${e.protocolFeeRate} sat/vB x ${h} vB for ${r} participants); refusing to sign payout.`
408
+ );
409
+ const d = await ve(
410
+ e.vaultCoreVersion,
411
+ e.numVaultKeepers,
412
+ e.numUniversalChallengers,
413
+ e.numVaultKeepers,
414
+ e.councilSize,
415
+ n,
416
+ i,
417
+ e.protocolFeeRate
418
+ );
419
+ if (s < d)
420
+ throw new Error(
421
+ `Payout implicit fee ${o} sats is below the floor of ${d} sats (the smallest fee any known vault-provider build produces for this shape); refusing to sign payout.`
422
+ );
423
+ }
424
+ const nt = 22, rt = 34, it = 34, st = 25, ut = 23;
425
+ function ct(e) {
426
+ return e.length === nt * 2 && e.startsWith("0014");
427
+ }
428
+ function at(e) {
429
+ return e.length === rt * 2 && e.startsWith("0020");
430
+ }
431
+ function lt(e) {
432
+ return e.length === it * 2 && e.startsWith("5120");
433
+ }
434
+ function ht(e) {
435
+ return e.length === st * 2 && e.startsWith("76a914") && e.endsWith("88ac");
436
+ }
437
+ function dt(e) {
438
+ return e.length === ut * 2 && e.startsWith("a914") && e.endsWith("87");
439
+ }
440
+ function j(e, t) {
441
+ const o = p(e).toLowerCase();
442
+ if (o.length === 0)
443
+ throw new Error(`${t} is empty`);
444
+ if (!/^[0-9a-f]+$/.test(o) || o.length % 2 !== 0)
445
+ throw new Error(`${t} is not valid hex`);
446
+ if (o.startsWith("6a"))
447
+ throw new Error(
448
+ `${t} is an OP_RETURN output, which is provably unspendable`
449
+ );
450
+ if (!lt(o) && !ct(o) && !at(o) && !ht(o) && !dt(o))
451
+ throw new Error(
452
+ `${t} is not a standard scriptPubKey (expected P2TR, P2WPKH, P2WSH, P2PKH, or P2SH; got ${o.length / 2} bytes: ${o})`
453
+ );
454
+ }
455
+ const J = 3;
456
+ async function At(e) {
457
+ const t = {
458
+ vaultCoreVersion: e.vaultCoreVersion,
459
+ numVaultKeepers: e.vaultKeeperBtcPubkeys.length,
460
+ numUniversalChallengers: e.universalChallengerBtcPubkeys.length,
461
+ councilSize: e.councilMembers.length,
462
+ protocolFeeRate: e.protocolFeeRate
463
+ };
464
+ tt(t);
465
+ const o = E.fromHex(p(e.payoutTxHex)), n = E.fromHex(p(e.peginTxHex)), i = E.fromHex(p(e.assertTxHex));
466
+ if (o.ins.length !== U)
467
+ throw new Error(
468
+ `Payout transaction must have exactly ${U} inputs, got ${o.ins.length}`
469
+ );
470
+ if (o.version !== M)
471
+ throw new Error(
472
+ `Payout transaction version ${o.version} must be ${M}; refusing to sign payout.`
473
+ );
474
+ if (o.locktime !== F)
475
+ throw new Error(
476
+ `Payout transaction locktime ${o.locktime} must be ${F}; refusing to sign payout.`
477
+ );
478
+ const r = ee(
479
+ o.ins[0],
480
+ 0,
481
+ n,
482
+ "PegIn",
483
+ Ae
484
+ );
485
+ if (o.ins[0].sequence !== e.timelockPegin)
486
+ throw new Error(
487
+ `Payout input 0 sequence ${o.ins[0].sequence} must equal the PegIn CSV timelock ${e.timelockPegin}; refusing to sign payout.`
488
+ );
489
+ const s = ee(
490
+ o.ins[1],
491
+ 1,
492
+ i,
493
+ "Assert",
494
+ ce
495
+ );
496
+ if (o.ins[1].sequence !== e.timelockAssert)
497
+ throw new Error(
498
+ `Payout input 1 sequence ${o.ins[1].sequence} must equal the Assert CSV timelock ${e.timelockAssert}; refusing to sign payout.`
499
+ );
500
+ const { out0Len: u, out1Len: h } = ft(
501
+ o,
502
+ r.value,
503
+ e
504
+ ), c = r.value + s.value;
505
+ let d = 0;
506
+ for (const v of o.outs) d += v.value;
507
+ if (d > c)
508
+ throw new Error(
509
+ `Payout outputs (${d} sats) exceed inputs (${c} sats); invalid transaction.`
510
+ );
511
+ const l = c - d;
512
+ await ot(t, {
513
+ implicitFeeSats: l,
514
+ out0Len: u,
515
+ out1Len: h
516
+ });
517
+ const P = await Be({
518
+ vaultCoreVersion: e.vaultCoreVersion,
519
+ depositor: e.depositorBtcPubkey,
520
+ vaultProvider: e.vaultProviderBtcPubkey,
521
+ vaultKeepers: e.vaultKeeperBtcPubkeys,
522
+ universalChallengers: e.universalChallengerBtcPubkeys,
523
+ timelockPegin: e.timelockPegin,
524
+ network: e.network
525
+ }), f = await pt(e, s);
526
+ return {
527
+ psbtHex: Pt(
528
+ o,
529
+ r,
530
+ s,
531
+ P,
532
+ f
533
+ )
534
+ };
535
+ }
536
+ async function pt(e, t) {
537
+ const { payoutScript: o, payoutControlBlock: n } = await $e({
538
+ txGraphVersion: e.vaultCoreVersion,
539
+ claimer: p(e.claimerBtcPubkey).toLowerCase(),
540
+ localChallengers: Te({
541
+ claimerBtcPubkey: e.claimerBtcPubkey,
542
+ depositorBtcPubkey: e.depositorBtcPubkey,
543
+ vaultProviderBtcPubkey: e.vaultProviderBtcPubkey,
544
+ vaultKeeperBtcPubkeys: e.vaultKeeperBtcPubkeys
545
+ }),
546
+ universalChallengers: e.universalChallengerBtcPubkeys.map(
547
+ (u) => p(u).toLowerCase()
548
+ ),
549
+ timelockAssert: e.timelockAssert,
550
+ councilMembers: e.councilMembers.map(
551
+ (u) => p(u).toLowerCase()
552
+ ),
553
+ councilQuorum: e.councilQuorum
554
+ }), i = S(o), r = S(n), s = Ye({
555
+ leafVersion: C,
556
+ script: i,
557
+ controlBlock: r
558
+ });
559
+ if (!t.script.equals(s))
560
+ throw new Error(
561
+ `Rebuilt Assert:0 payout leaf does not spend the Assert output being referenced: leaf binds to ${s.toString("hex")}, Assert:${ce} pays ${t.script.toString("hex")}; refusing to sign payout.`
562
+ );
563
+ return { script: i, controlBlock: r };
564
+ }
565
+ function ee(e, t, o, n, i) {
566
+ const r = H(
567
+ new Uint8Array(e.hash).slice().reverse()
568
+ ), s = o.getId();
569
+ if (r !== s || e.index !== i)
570
+ throw new Error(
571
+ `Input ${t} must spend ${n}:${i}. Expected ${s}:${i}, got ${r}:${e.index}`
572
+ );
573
+ const u = o.outs[e.index];
574
+ if (!u)
575
+ throw new Error(
576
+ `Previous output not found for input ${t} (txid: ${r}, index: ${e.index})`
577
+ );
578
+ return u;
579
+ }
580
+ function Pt(e, t, o, n, i) {
581
+ const r = new _();
582
+ r.setVersion(e.version), r.setLocktime(e.locktime);
583
+ const [s, u] = e.ins;
584
+ r.addInput({
585
+ hash: s.hash,
586
+ index: s.index,
587
+ sequence: s.sequence,
588
+ witnessUtxo: {
589
+ script: t.script,
590
+ value: t.value
591
+ },
592
+ tapLeafScript: [
593
+ {
594
+ leafVersion: C,
595
+ script: a.from(S(n.payoutScript)),
596
+ controlBlock: a.from(
597
+ S(n.payoutControlBlock)
598
+ )
599
+ }
600
+ ],
601
+ tapInternalKey: a.from(V)
602
+ // sighashType omitted - defaults to SIGHASH_DEFAULT (0x00) for Taproot
603
+ }), r.addInput({
604
+ hash: u.hash,
605
+ index: u.index,
606
+ sequence: u.sequence,
607
+ witnessUtxo: {
608
+ script: o.script,
609
+ value: o.value
610
+ },
611
+ tapLeafScript: [
612
+ {
613
+ leafVersion: C,
614
+ script: a.from(i.script),
615
+ controlBlock: a.from(i.controlBlock)
616
+ }
617
+ ],
618
+ tapInternalKey: a.from(V)
619
+ });
620
+ for (const h of e.outs)
621
+ r.addOutput({
622
+ script: h.script,
623
+ value: h.value
624
+ });
625
+ return r.toHex();
626
+ }
627
+ function te(e, t) {
628
+ const o = p(e).toLowerCase(), n = p(
629
+ be(t)
630
+ ).toLowerCase();
631
+ return o === n ? [o] : [o, n];
632
+ }
633
+ function oe(e, t) {
634
+ return t.some((o) => e.equals(a.from(o, "hex")));
635
+ }
636
+ function ft(e, t, o) {
637
+ const {
638
+ claimerBtcPubkey: n,
639
+ vaultProviderBtcPubkey: i,
640
+ depositorBtcPubkey: r,
641
+ vaultKeeperBtcPubkeys: s,
642
+ registeredPayoutScriptPubKey: u,
643
+ commissionBps: h,
644
+ vkClaimerPayoutScriptPubKeys: c,
645
+ vpCommissionScriptPubKey: d
646
+ } = o;
647
+ if (!Ce(u))
648
+ throw new Error("Invalid registeredPayoutScriptPubKey: not valid hex");
649
+ const l = p(n).toLowerCase(), P = p(i).toLowerCase(), f = p(r).toLowerCase(), v = s.map(
650
+ (m) => p(m).toLowerCase()
651
+ );
652
+ let w, g, $;
653
+ if (l === P)
654
+ w = "vp-claimer", g = _e, $ = [p(u)];
655
+ else if (l === f)
656
+ w = "depositor-as-claimer", g = z, $ = [p(u)];
657
+ else if (v.includes(l)) {
658
+ w = "vk-claimer", g = z;
659
+ const m = c[l];
660
+ if (m === void 0)
661
+ throw new Error(
662
+ `No registered payout script resolved for vault keeper claimer ${l}`
663
+ );
664
+ j(
665
+ m,
666
+ `Vault keeper payout script for claimer ${l}`
667
+ ), $ = te(m, l);
668
+ } else
669
+ throw new Error(
670
+ `Unknown claimer pubkey ${l}: not VP, depositor, or a registered vault keeper`
671
+ );
672
+ if (e.outs.length !== g)
673
+ throw new Error(
674
+ `Payout transaction has ${e.outs.length} output(s), expected exactly ${g} for role ${w}.`
675
+ );
676
+ if (!oe(e.outs[0].script, $))
677
+ throw new Error(
678
+ `Payout transaction output 0 does not pay the expected scriptPubKey for role ${w}. Accepted: ${$.join(" or ")}; got ${e.outs[0].script.toString("hex")}`
679
+ );
680
+ const T = g - 1;
681
+ if (e.outs[T].value !== q)
682
+ throw new Error(
683
+ `Payout CPFP anchor (out ${T}) value ${e.outs[T].value} sats must equal ${q} sats`
684
+ );
685
+ if (w === "vp-claimer") {
686
+ j(
687
+ d,
688
+ "Vault provider commission payout script"
689
+ );
690
+ const m = te(
691
+ d,
692
+ P
693
+ );
694
+ if (!oe(e.outs[1].script, m))
695
+ throw new Error(
696
+ `Payout transaction output 1 does not pay the vault provider's commission scriptPubKey. Accepted: ${m.join(" or ")}; got ${e.outs[1].script.toString("hex")}`
697
+ );
698
+ if (!Number.isInteger(h) || h < 0 || h >= W)
699
+ throw new Error(
700
+ `commissionBps must be an integer in [0, ${W}), got ${h}`
701
+ );
702
+ const R = Math.floor(
703
+ t * h / ke
704
+ );
705
+ if (e.outs[1].value > R)
706
+ throw new Error(
707
+ `Payout VP commission (out 1) value ${e.outs[1].value} sats exceeds cap ${R} sats (${h} bps of peginValue=${t})`
708
+ );
709
+ }
710
+ const b = e.outs[0].script.length;
711
+ if (b === 0 || b > X)
712
+ throw new Error(
713
+ `Payout receiver scriptPubKey length ${b} is outside the contract's registration cap [1, ${X}]; refusing to sign payout.`
714
+ );
715
+ const de = w === "vp-claimer" ? e.outs[1].script.length : void 0;
716
+ return { out0Len: b, out1Len: de };
717
+ }
718
+ function kt(e, t, o = 0) {
719
+ const n = _.fromHex(e);
720
+ if (o >= n.data.inputs.length)
721
+ throw new Error(
722
+ `Input index ${o} out of range (${n.data.inputs.length} inputs)`
723
+ );
724
+ const i = n.data.inputs[o];
725
+ if (i.tapScriptSig && i.tapScriptSig.length > 0) {
726
+ const r = S(t);
727
+ for (const s of i.tapScriptSig)
728
+ if (s.pubkey.equals(a.from(r)))
729
+ return ne(s.signature, o);
730
+ throw new Error(
731
+ `No signature found for depositor pubkey: ${t} at input ${o}`
732
+ );
733
+ }
734
+ if (i.finalScriptWitness && i.finalScriptWitness.length > 0) {
735
+ const r = gt(i.finalScriptWitness);
736
+ if (r.length !== J)
737
+ throw new Error(
738
+ `Unexpected finalized witness stack size at input ${o}: expected ${J} items (signature, script, controlBlock), got ${r.length}`
739
+ );
740
+ return ne(r[0], o);
741
+ }
742
+ throw new Error(
743
+ `No tapScriptSig or finalScriptWitness found in signed PSBT at input ${o}`
744
+ );
745
+ }
746
+ function ne(e, t) {
747
+ if (e.length === 64)
748
+ return H(new Uint8Array(e));
749
+ throw e.length === 65 ? new Error(
750
+ `Unexpected sighash byte 0x${e[64].toString(16).padStart(2, "0")} at input ${t}. Expected implicit SIGHASH_DEFAULT as a 64-byte signature.`
751
+ ) : new Error(
752
+ `Unexpected signature length at input ${t}: ${e.length}`
753
+ );
754
+ }
755
+ function gt(e) {
756
+ const t = [];
757
+ let o = 0;
758
+ const n = (s) => {
759
+ if (o + s > e.length)
760
+ throw new Error(
761
+ `Malformed witness data: need ${s} byte(s) at offset ${o}, only ${e.length - o} remaining`
762
+ );
763
+ }, i = () => {
764
+ n(1);
765
+ const s = e[o++];
766
+ if (s < 253) return s;
767
+ if (s === 253) {
768
+ n(2);
769
+ const u = (e[o] | e[o + 1] << 8) >>> 0;
770
+ return o += 2, u;
771
+ }
772
+ if (s === 254) {
773
+ n(4);
774
+ const u = (e[o] | e[o + 1] << 8 | e[o + 2] << 16 | e[o + 3] << 24) >>> 0;
775
+ return o += 4, u;
776
+ }
777
+ throw new Error(
778
+ `Malformed witness data: 8-byte varint (0xff) not supported at offset ${o - 1}`
779
+ );
780
+ }, r = i();
781
+ for (let s = 0; s < r; s++) {
782
+ const u = i();
783
+ n(u), t.push(a.from(e.subarray(o, o + u))), o += u;
784
+ }
785
+ if (o !== e.length)
786
+ throw new Error(
787
+ `Malformed witness data: ${e.length - o} trailing byte(s) after parsing ${r} item(s)`
788
+ );
789
+ return t;
790
+ }
791
+ class y extends Error {
792
+ constructor(t) {
793
+ super(
794
+ `Wallet returned a PSBT for a different transaction: ${t}`
795
+ ), this.name = "PsbtSubstitutionError";
796
+ }
797
+ }
798
+ function re(e, t) {
799
+ try {
800
+ return _.fromHex(t);
801
+ } catch (o) {
802
+ const n = o instanceof Error ? o.message : String(o);
803
+ throw new Error(`Failed to parse ${e} PSBT: ${n}`);
804
+ }
805
+ }
806
+ const wt = 8;
807
+ function O(e) {
808
+ return `${e.toString("hex").slice(0, wt)}…`;
809
+ }
810
+ function ie(e) {
811
+ const t = a.from(e).reverse();
812
+ return O(t);
813
+ }
814
+ function _t(e) {
815
+ const t = re("requested", e.requestedPsbtHex), o = re("returned", e.returnedPsbtHex);
816
+ if (t.version !== o.version)
817
+ throw new y(
818
+ `tx version differs (requested=${t.version}, returned=${o.version})`
819
+ );
820
+ if (t.locktime !== o.locktime)
821
+ throw new y(
822
+ `tx locktime differs (requested=${t.locktime}, returned=${o.locktime})`
823
+ );
824
+ if (t.txInputs.length !== o.txInputs.length)
825
+ throw new y(
826
+ `input count differs (requested=${t.txInputs.length}, returned=${o.txInputs.length})`
827
+ );
828
+ if (t.txOutputs.length !== o.txOutputs.length)
829
+ throw new y(
830
+ `output count differs (requested=${t.txOutputs.length}, returned=${o.txOutputs.length})`
831
+ );
832
+ for (let n = 0; n < t.txInputs.length; n++) {
833
+ const i = t.txInputs[n], r = o.txInputs[n];
834
+ if (!i.hash.equals(r.hash))
835
+ throw new y(
836
+ `input ${n} prevout txid differs (requested=${ie(i.hash)}, returned=${ie(r.hash)})`
837
+ );
838
+ if (i.index !== r.index)
839
+ throw new y(
840
+ `input ${n} prevout vout differs (requested=${i.index}, returned=${r.index})`
841
+ );
842
+ if (i.sequence !== r.sequence)
843
+ throw new y(
844
+ `input ${n} sequence differs (requested=${i.sequence}, returned=${r.sequence})`
845
+ );
846
+ }
847
+ for (let n = 0; n < t.txOutputs.length; n++) {
848
+ const i = t.txOutputs[n], r = o.txOutputs[n];
849
+ if (!i.script.equals(r.script))
850
+ throw new y(
851
+ `output ${n} scriptPubKey differs (requested=${O(i.script)}, returned=${O(r.script)})`
852
+ );
853
+ if (i.value !== r.value)
854
+ throw new y(
855
+ `output ${n} value differs (requested=${i.value}, returned=${r.value})`
856
+ );
857
+ }
858
+ }
859
+ function Tt(e) {
860
+ const { requestedPsbtHex: t, signatureHex: o, signerXOnlyPubkeyHex: n, inputIndex: i } = e, r = p(o);
861
+ if (r.length !== B)
862
+ throw new Error(
863
+ `Schnorr signature for input ${i} must be ${B} hex chars (64 bytes), got ${r.length}.`
864
+ );
865
+ const s = p(n);
866
+ if (s.length !== N)
867
+ throw new Error(
868
+ `Signer x-only pubkey for input ${i} must be ${N} hex chars (32 bytes), got ${s.length}.`
869
+ );
870
+ const u = _.fromHex(t);
871
+ if (i < 0 || i >= u.data.inputs.length)
872
+ throw new Error(
873
+ `Input index ${i} out of range (${u.data.inputs.length} inputs).`
874
+ );
875
+ const h = [], c = [];
876
+ for (let g = 0; g < u.data.inputs.length; g++) {
877
+ const $ = u.data.inputs[g].witnessUtxo;
878
+ if (!$)
879
+ throw new Error(
880
+ `Cannot verify signature: input ${g} of the requested PSBT has no witnessUtxo (required to recompute the Taproot sighash).`
881
+ );
882
+ h.push($.script), c.push($.value);
883
+ }
884
+ const d = u.data.inputs[i].tapLeafScript;
885
+ if (!d || d.length !== 1)
886
+ throw new Error(
887
+ `Cannot verify signature: input ${i} of the requested PSBT must have exactly one tapLeafScript, got ${(d == null ? void 0 : d.length) ?? 0}.`
888
+ );
889
+ const l = d[0];
890
+ if (l.leafVersion !== C)
891
+ throw new Error(
892
+ `Cannot verify signature: input ${i} tapLeafScript has leaf version 0x${l.leafVersion.toString(16)}, expected 0x${C.toString(16)}.`
893
+ );
894
+ const P = he(l.leafVersion, l.script), f = new E();
895
+ f.version = u.version, f.locktime = u.locktime;
896
+ for (const g of u.txInputs)
897
+ f.addInput(g.hash, g.index, g.sequence);
898
+ for (const g of u.txOutputs)
899
+ f.addOutput(g.script, g.value);
900
+ const v = f.hashForWitnessV1(
901
+ i,
902
+ h,
903
+ c,
904
+ E.SIGHASH_DEFAULT,
905
+ P
906
+ );
907
+ if (!ae.verifySchnorr(
908
+ v,
909
+ S(s),
910
+ S(r)
911
+ ))
912
+ throw new Error(
913
+ `Schnorr signature for input ${i} (signer ${s}) 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.`
914
+ );
915
+ }
916
+ export {
917
+ I as H,
918
+ y as P,
919
+ Ct as a,
920
+ bt as b,
921
+ Et as c,
922
+ At as d,
923
+ kt as e,
924
+ _t as f,
925
+ Tt as g,
926
+ Be as h,
927
+ Ve as i,
928
+ Te as j,
929
+ le as n
930
+ };
931
+ //# sourceMappingURL=verifyScriptPathSchnorrSignature-Dy9gF5RF.js.map