@babylonlabs-io/ts-sdk 0.55.1 → 0.56.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.
Files changed (160) hide show
  1. package/README.md +0 -1
  2. package/dist/{PayoutManager-BJmPnyRP.js → PayoutManager-BVgmuKuK.js} +117 -105
  3. package/dist/PayoutManager-BVgmuKuK.js.map +1 -0
  4. package/dist/PayoutManager-Cu4_l-_Y.cjs +2 -0
  5. package/dist/PayoutManager-Cu4_l-_Y.cjs.map +1 -0
  6. package/dist/PeginManager-BkWdZI7P.cjs +2 -0
  7. package/dist/PeginManager-BkWdZI7P.cjs.map +1 -0
  8. package/dist/PeginManager-aYBkb-Qx.js +1289 -0
  9. package/dist/PeginManager-aYBkb-Qx.js.map +1 -0
  10. package/dist/{buildAndBroadcastRefund-BiSO4zg2.js → buildAndBroadcastRefund-BtXUqBkb.js} +444 -430
  11. package/dist/buildAndBroadcastRefund-BtXUqBkb.js.map +1 -0
  12. package/dist/buildAndBroadcastRefund-Cq01wyZX.cjs +2 -0
  13. package/dist/buildAndBroadcastRefund-Cq01wyZX.cjs.map +1 -0
  14. package/dist/challengeAssert-BVQq50su.js +75 -0
  15. package/dist/challengeAssert-BVQq50su.js.map +1 -0
  16. package/dist/challengeAssert-Cu-PtJ1g.cjs +2 -0
  17. package/dist/challengeAssert-Cu-PtJ1g.cjs.map +1 -0
  18. package/dist/constants-BqU1Uz15.cjs +2 -0
  19. package/dist/{constants-CvbARMl-.cjs.map → constants-BqU1Uz15.cjs.map} +1 -1
  20. package/dist/{constants-BhclpREj.js → constants-CDQE812z.js} +8 -6
  21. package/dist/{constants-BhclpREj.js.map → constants-CDQE812z.js.map} +1 -1
  22. package/dist/deriveVaultRoot-B_gEOTD6.js +72 -0
  23. package/dist/deriveVaultRoot-B_gEOTD6.js.map +1 -0
  24. package/dist/deriveVaultRoot-CebLadyJ.cjs +2 -0
  25. package/dist/deriveVaultRoot-CebLadyJ.cjs.map +1 -0
  26. package/dist/errors-B-H0seUG.js +8 -0
  27. package/dist/errors-B-H0seUG.js.map +1 -0
  28. package/dist/errors-Bfjxg_iJ.cjs +2 -0
  29. package/dist/errors-Bfjxg_iJ.cjs.map +1 -0
  30. package/dist/index.cjs +1 -1
  31. package/dist/index.js +197 -193
  32. package/dist/index.js.map +1 -1
  33. package/dist/{mempoolApi-CB7bHuwi.js → mempoolApi-P_ApODLd.js} +2 -2
  34. package/dist/{mempoolApi-CB7bHuwi.js.map → mempoolApi-P_ApODLd.js.map} +1 -1
  35. package/dist/{mempoolApi-DqJByvqR.cjs → mempoolApi-emec8bHK.cjs} +2 -2
  36. package/dist/{mempoolApi-DqJByvqR.cjs.map → mempoolApi-emec8bHK.cjs.map} +1 -1
  37. package/dist/{noPayout-CNMqNNpc.cjs → noPayout-Cb8vFrkY.cjs} +2 -2
  38. package/dist/{noPayout-CNMqNNpc.cjs.map → noPayout-Cb8vFrkY.cjs.map} +1 -1
  39. package/dist/{noPayout-CfBg3HzC.js → noPayout-CihCk1HE.js} +2 -2
  40. package/dist/{noPayout-CfBg3HzC.js.map → noPayout-CihCk1HE.js.map} +1 -1
  41. package/dist/parseFundingOutpoints-C_-PT9BT.cjs +2 -0
  42. package/dist/parseFundingOutpoints-C_-PT9BT.cjs.map +1 -0
  43. package/dist/parseFundingOutpoints-GSM-IaaK.js +17 -0
  44. package/dist/parseFundingOutpoints-GSM-IaaK.js.map +1 -0
  45. package/dist/{primeVpAuth-l-5x_2UV.js → primeVpAuth-DZOaC_Ui.js} +3 -3
  46. package/dist/{primeVpAuth-l-5x_2UV.js.map → primeVpAuth-DZOaC_Ui.js.map} +1 -1
  47. package/dist/{primeVpAuth-CF4cH5_e.cjs → primeVpAuth-j2getl3f.cjs} +2 -2
  48. package/dist/{primeVpAuth-CF4cH5_e.cjs.map → primeVpAuth-j2getl3f.cjs.map} +1 -1
  49. package/dist/tbv/core/clients/index.cjs +1 -1
  50. package/dist/tbv/core/clients/index.js +2 -2
  51. package/dist/tbv/core/deposit-terms/__tests__/buildDepositTerms.test.d.ts +2 -0
  52. package/dist/tbv/core/deposit-terms/__tests__/buildDepositTerms.test.d.ts.map +1 -0
  53. package/dist/tbv/core/deposit-terms/buildDepositTerms.d.ts +8 -0
  54. package/dist/tbv/core/deposit-terms/buildDepositTerms.d.ts.map +1 -0
  55. package/dist/tbv/core/deposit-terms/depositTerms.d.ts +92 -0
  56. package/dist/tbv/core/deposit-terms/depositTerms.d.ts.map +1 -0
  57. package/dist/tbv/core/deposit-terms/index.d.ts +12 -0
  58. package/dist/tbv/core/deposit-terms/index.d.ts.map +1 -0
  59. package/dist/tbv/core/index.cjs +1 -1
  60. package/dist/tbv/core/index.d.ts +1 -0
  61. package/dist/tbv/core/index.d.ts.map +1 -1
  62. package/dist/tbv/core/index.js +195 -191
  63. package/dist/tbv/core/index.js.map +1 -1
  64. package/dist/tbv/core/managers/PayoutManager.d.ts +7 -0
  65. package/dist/tbv/core/managers/PayoutManager.d.ts.map +1 -1
  66. package/dist/tbv/core/managers/PeginManager.d.ts +13 -0
  67. package/dist/tbv/core/managers/PeginManager.d.ts.map +1 -1
  68. package/dist/tbv/core/managers/index.cjs +1 -1
  69. package/dist/tbv/core/managers/index.js +2 -2
  70. package/dist/tbv/core/primitives/index.cjs +1 -1
  71. package/dist/tbv/core/primitives/index.d.ts +0 -3
  72. package/dist/tbv/core/primitives/index.d.ts.map +1 -1
  73. package/dist/tbv/core/primitives/index.js +37 -38
  74. package/dist/tbv/core/primitives/psbt/__tests__/assertPayoutFeeBand.test.d.ts +7 -0
  75. package/dist/tbv/core/primitives/psbt/__tests__/assertPayoutFeeBand.test.d.ts.map +1 -0
  76. package/dist/tbv/core/primitives/psbt/assertPayoutFeeBand.d.ts +63 -0
  77. package/dist/tbv/core/primitives/psbt/assertPayoutFeeBand.d.ts.map +1 -0
  78. package/dist/tbv/core/primitives/psbt/assertWasmPeginSizing.d.ts +4 -1
  79. package/dist/tbv/core/primitives/psbt/assertWasmPeginSizing.d.ts.map +1 -1
  80. package/dist/tbv/core/primitives/psbt/constants.d.ts +18 -4
  81. package/dist/tbv/core/primitives/psbt/constants.d.ts.map +1 -1
  82. package/dist/tbv/core/primitives/psbt/payout.d.ts +21 -2
  83. package/dist/tbv/core/primitives/psbt/payout.d.ts.map +1 -1
  84. package/dist/tbv/core/primitives/psbt/pegin.d.ts +6 -0
  85. package/dist/tbv/core/primitives/psbt/pegin.d.ts.map +1 -1
  86. package/dist/tbv/core/services/deposit/runDepositorPresignFlow.d.ts +12 -0
  87. package/dist/tbv/core/services/deposit/runDepositorPresignFlow.d.ts.map +1 -1
  88. package/dist/tbv/core/services/deposit/signDepositorGraph.d.ts +5 -0
  89. package/dist/tbv/core/services/deposit/signDepositorGraph.d.ts.map +1 -1
  90. package/dist/tbv/core/services/index.cjs +1 -1
  91. package/dist/tbv/core/services/index.js +2 -2
  92. package/dist/tbv/core/vault-secrets/index.cjs +2 -0
  93. package/dist/tbv/core/vault-secrets/index.cjs.map +1 -0
  94. package/dist/tbv/core/vault-secrets/index.js +14 -0
  95. package/dist/tbv/core/vault-secrets/index.js.map +1 -0
  96. package/dist/tbv/index.cjs +1 -1
  97. package/dist/tbv/index.js +195 -191
  98. package/dist/tbv/index.js.map +1 -1
  99. package/dist/tbv/integrations/aave/clients/index.d.ts +1 -1
  100. package/dist/tbv/integrations/aave/clients/index.d.ts.map +1 -1
  101. package/dist/tbv/integrations/aave/clients/spoke.d.ts +0 -20
  102. package/dist/tbv/integrations/aave/clients/spoke.d.ts.map +1 -1
  103. package/dist/tbv/integrations/aave/constants.d.ts +0 -22
  104. package/dist/tbv/integrations/aave/constants.d.ts.map +1 -1
  105. package/dist/tbv/integrations/aave/index.cjs +1 -1
  106. package/dist/tbv/integrations/aave/index.cjs.map +1 -1
  107. package/dist/tbv/integrations/aave/index.d.ts +5 -6
  108. package/dist/tbv/integrations/aave/index.d.ts.map +1 -1
  109. package/dist/tbv/integrations/aave/index.js +236 -339
  110. package/dist/tbv/integrations/aave/index.js.map +1 -1
  111. package/dist/tbv/integrations/aave/types.d.ts +0 -7
  112. package/dist/tbv/integrations/aave/types.d.ts.map +1 -1
  113. package/dist/tbv/integrations/aave/utils/healthFactor.d.ts +0 -7
  114. package/dist/tbv/integrations/aave/utils/healthFactor.d.ts.map +1 -1
  115. package/dist/tbv/integrations/aave/utils/index.d.ts +3 -8
  116. package/dist/tbv/integrations/aave/utils/index.d.ts.map +1 -1
  117. package/dist/tbv/integrations/aave/utils/vaultSplit.d.ts +0 -65
  118. package/dist/tbv/integrations/aave/utils/vaultSplit.d.ts.map +1 -1
  119. package/dist/verifyScriptPathSchnorrSignature-BZhX-SOd.cjs +2 -0
  120. package/dist/verifyScriptPathSchnorrSignature-BZhX-SOd.cjs.map +1 -0
  121. package/dist/verifyScriptPathSchnorrSignature-CgrnAjMW.js +724 -0
  122. package/dist/verifyScriptPathSchnorrSignature-CgrnAjMW.js.map +1 -0
  123. package/package.json +4 -4
  124. package/dist/PayoutManager-BJmPnyRP.js.map +0 -1
  125. package/dist/PayoutManager-aqtsj60t.cjs +0 -2
  126. package/dist/PayoutManager-aqtsj60t.cjs.map +0 -1
  127. package/dist/PeginManager-D22MLWfM.js +0 -1295
  128. package/dist/PeginManager-D22MLWfM.js.map +0 -1
  129. package/dist/PeginManager-vUUfZCzj.cjs +0 -2
  130. package/dist/PeginManager-vUUfZCzj.cjs.map +0 -1
  131. package/dist/buildAndBroadcastRefund-BiSO4zg2.js.map +0 -1
  132. package/dist/buildAndBroadcastRefund-Bwi1I5we.cjs +0 -2
  133. package/dist/buildAndBroadcastRefund-Bwi1I5we.cjs.map +0 -1
  134. package/dist/challengeAssert-DLQfNcfU.cjs +0 -2
  135. package/dist/challengeAssert-DLQfNcfU.cjs.map +0 -1
  136. package/dist/challengeAssert-DvdbpEDY.js +0 -128
  137. package/dist/challengeAssert-DvdbpEDY.js.map +0 -1
  138. package/dist/constants-CvbARMl-.cjs +0 -2
  139. package/dist/errors-9AkghWyk.js +0 -22
  140. package/dist/errors-9AkghWyk.js.map +0 -1
  141. package/dist/errors-Bu0H-dZD.cjs +0 -2
  142. package/dist/errors-Bu0H-dZD.cjs.map +0 -1
  143. package/dist/tbv/core/primitives/psbt/depositorPayout.d.ts +0 -34
  144. package/dist/tbv/core/primitives/psbt/depositorPayout.d.ts.map +0 -1
  145. package/dist/tbv/integrations/aave/utils/__tests__/borrowRatio.test.d.ts +0 -5
  146. package/dist/tbv/integrations/aave/utils/__tests__/borrowRatio.test.d.ts.map +0 -1
  147. package/dist/tbv/integrations/aave/utils/__tests__/seizureSimulation.test.d.ts +0 -5
  148. package/dist/tbv/integrations/aave/utils/__tests__/seizureSimulation.test.d.ts.map +0 -1
  149. package/dist/tbv/integrations/aave/utils/__tests__/vaultSelection.test.d.ts +0 -2
  150. package/dist/tbv/integrations/aave/utils/__tests__/vaultSelection.test.d.ts.map +0 -1
  151. package/dist/tbv/integrations/aave/utils/borrowRatio.d.ts +0 -15
  152. package/dist/tbv/integrations/aave/utils/borrowRatio.d.ts.map +0 -1
  153. package/dist/tbv/integrations/aave/utils/seizureSimulation.d.ts +0 -109
  154. package/dist/tbv/integrations/aave/utils/seizureSimulation.d.ts.map +0 -1
  155. package/dist/tbv/integrations/aave/utils/vaultSelection.d.ts +0 -33
  156. package/dist/tbv/integrations/aave/utils/vaultSelection.d.ts.map +0 -1
  157. package/dist/verifyScriptPathSchnorrSignature-BDznSCOR.cjs +0 -2
  158. package/dist/verifyScriptPathSchnorrSignature-BDznSCOR.cjs.map +0 -1
  159. package/dist/verifyScriptPathSchnorrSignature-DA4Z4yZ3.js +0 -658
  160. package/dist/verifyScriptPathSchnorrSignature-DA4Z4yZ3.js.map +0 -1
@@ -0,0 +1,724 @@
1
+ import { computeMinClaimValue as D, peginP2aAnchorOutput as G, computeMinPeginFee as Z, buildPeginTxFromPrePegin as j, createPrePeginTransaction as J, validatePeginP2aAnchor as ee, tapInternalPubkey as Y, createPayoutConnector as te, computePayoutFeeFloor as ne } from "@babylonlabs-io/babylon-tbv-rust-wasm";
2
+ import { Buffer as d } from "buffer";
3
+ import { Transaction as S, script as oe, opcodes as re, payments as ie, Psbt as A, crypto as se } from "bitcoinjs-lib";
4
+ import { c as V, d as ue } from "./fundPeginTransaction-B2wyjqp0.js";
5
+ import { s as P, u as _, h as m, c as ce, d as ae, T as I, S as k, X as H } from "./bitcoin-B5aNKtsk.js";
6
+ import { D as O, P as R, M as L, b, c as le, A as he, B as de, V as pe, N as U } from "./constants-CDQE812z.js";
7
+ import * as Pe from "@bitcoin-js/tiny-secp256k1-asmjs";
8
+ async function ge(e, t) {
9
+ const n = t.pegInAmounts.length;
10
+ if (e.htlcValues.length !== n)
11
+ throw new Error(
12
+ `WASM Pre-PegIn returned ${e.htlcValues.length} HTLC value(s), expected ${n} (one per requested deposit).`
13
+ );
14
+ if (e.peginAmounts.length !== n || e.htlcScriptPubKeys.length !== n || e.htlcAddresses.length !== n)
15
+ throw new Error(
16
+ `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.`
17
+ );
18
+ if (e.depositorClaimValue <= 0n)
19
+ throw new Error(
20
+ `WASM Pre-PegIn returned non-positive depositorClaimValue ${e.depositorClaimValue}; expected > 0.`
21
+ );
22
+ const i = await D(
23
+ // Must price the same graph version the builder constructed.
24
+ t.vaultCoreVersion,
25
+ t.numLocalChallengers,
26
+ t.universalChallengerPubkeys.length,
27
+ t.councilQuorum,
28
+ t.councilSize,
29
+ t.feeRate
30
+ );
31
+ if (e.depositorClaimValue !== i)
32
+ throw new Error(
33
+ `WASM Pre-PegIn depositorClaimValue ${e.depositorClaimValue} does not match the independently computed minimum claim value ${i} (vaultCoreVersion=${t.vaultCoreVersion}, numLocalChallengers=${t.numLocalChallengers}, numUniversalChallengers=${t.universalChallengerPubkeys.length}, councilQuorum=${t.councilQuorum}, councilSize=${t.councilSize}, feeRate=${t.feeRate}).`
34
+ );
35
+ const o = await G(t.vaultCoreVersion), r = (o == null ? void 0 : o.value) ?? 0n, s = await Z(
36
+ t.vaultCoreVersion,
37
+ t.vaultKeeperPubkeys.length,
38
+ t.universalChallengerPubkeys.length,
39
+ t.minPeginFeeRate
40
+ ), u = s + r, l = t.minPeginFeeRate * V;
41
+ for (let c = 0; c < n; c++) {
42
+ const h = t.pegInAmounts[c], p = e.peginAmounts[c], g = e.htlcValues[c];
43
+ if (p !== h)
44
+ throw new Error(
45
+ `WASM Pre-PegIn peginAmount[${c}] ${p} does not match the requested amount ${h}; refusing to build a tx whose recorded amount differs from the depositor's request.`
46
+ );
47
+ if (p <= 0n)
48
+ throw new Error(
49
+ `WASM Pre-PegIn peginAmount[${c}] is non-positive (${p}); expected > 0.`
50
+ );
51
+ if (g <= 0n)
52
+ throw new Error(
53
+ `WASM Pre-PegIn htlcValue[${c}] is non-positive (${g}); expected > 0.`
54
+ );
55
+ const a = g - p - e.depositorClaimValue;
56
+ if (a <= 0n)
57
+ throw new Error(
58
+ `WASM Pre-PegIn htlcValue[${c}] ${g} does not strictly cover peginAmount ${p} + depositorClaimValue ${e.depositorClaimValue} + a PegIn reserve (implied reserve ${a}).`
59
+ );
60
+ if (a > l)
61
+ throw new Error(
62
+ `WASM Pre-PegIn implied reserve for HTLC[${c}] (${a} sat) exceeds the plausibility cap ${l} sat (minPeginFeeRate=${t.minPeginFeeRate} × ${V} vbytes); htlcValue ${g} appears grossly inflated.`
63
+ );
64
+ if (a !== u)
65
+ throw new Error(
66
+ `WASM Pre-PegIn htlcValue[${c}] ${g} implies a PegIn fee+anchor reserve of ${a} sat, expected exactly ${u} sat (minPeginFee ${s} + p2aAnchor ${r} for vaultCoreVersion ${t.vaultCoreVersion}, vaultKeepers=${t.vaultKeeperPubkeys.length}, universalChallengers=${t.universalChallengerPubkeys.length}, minPeginFeeRate=${t.minPeginFeeRate}).`
67
+ );
68
+ }
69
+ return s;
70
+ }
71
+ function fe(e, t, n) {
72
+ if (e.length < t.length)
73
+ throw new Error(
74
+ `Encoded Pre-PegIn tx has ${e.length} output(s), fewer than the ${t.length} HTLC output(s) the cross-check validated.`
75
+ );
76
+ for (let i = 0; i < t.length; i++) {
77
+ const o = BigInt(e[i].value);
78
+ if (o !== t[i])
79
+ throw new Error(
80
+ `Encoded Pre-PegIn HTLC output[${i}] value ${o} does not match the cross-checked htlcValue ${t[i]}; the funded/signed tx would not pay the validated amount.`
81
+ );
82
+ const r = e[i].script.toString("hex").toLowerCase(), s = n[i].toLowerCase();
83
+ if (r !== s)
84
+ throw new Error(
85
+ `Encoded Pre-PegIn HTLC output[${i}] scriptPubKey ${r} does not match the cross-checked htlcScriptPubKey ${s}.`
86
+ );
87
+ }
88
+ }
89
+ const B = 64, ve = /^[0-9a-fA-F]+$/;
90
+ async function We(e) {
91
+ const t = Q(e.authAnchorHash), n = await J({
92
+ txGraphVersion: e.vaultCoreVersion,
93
+ depositorPubkey: e.depositorPubkey,
94
+ vaultProviderPubkey: e.vaultProviderPubkey,
95
+ vaultKeeperPubkeys: e.vaultKeeperPubkeys,
96
+ universalChallengerPubkeys: e.universalChallengerPubkeys,
97
+ hashlocks: [...e.hashlocks],
98
+ timelockRefund: e.timelockRefund,
99
+ pegInAmounts: [...e.pegInAmounts],
100
+ feeRate: e.feeRate,
101
+ minPeginFeeRate: e.minPeginFeeRate,
102
+ numLocalChallengers: e.numLocalChallengers,
103
+ councilQuorum: e.councilQuorum,
104
+ councilSize: e.councilSize,
105
+ network: e.network,
106
+ authAnchorHash: t
107
+ }), i = await ge(n, e), o = ue(n.txHex);
108
+ fe(
109
+ o.outputs,
110
+ n.htlcValues,
111
+ n.htlcScriptPubKeys
112
+ );
113
+ const r = o.outputs.reduce(
114
+ (u, l) => u + BigInt(l.value),
115
+ 0n
116
+ ), s = t !== void 0 ? n.htlcValues.length : null;
117
+ return {
118
+ psbtHex: n.txHex,
119
+ totalOutputValue: r,
120
+ htlcValues: n.htlcValues,
121
+ htlcScriptPubKeys: n.htlcScriptPubKeys,
122
+ htlcAddresses: n.htlcAddresses,
123
+ peginAmounts: n.peginAmounts,
124
+ depositorClaimValue: n.depositorClaimValue,
125
+ authAnchorVout: s,
126
+ minPeginFee: i
127
+ };
128
+ }
129
+ function Q(e) {
130
+ if (e === void 0) return;
131
+ const t = e.startsWith("0x") || e.startsWith("0X") ? e.slice(2) : e;
132
+ if (t.length !== B || !ve.test(t))
133
+ throw new Error(
134
+ `authAnchorHash must be 32-byte hex (${B} chars, no 0x prefix); got length ${t.length}`
135
+ );
136
+ return t.toLowerCase();
137
+ }
138
+ async function Xe(e) {
139
+ const t = await j(
140
+ {
141
+ txGraphVersion: e.prePeginParams.vaultCoreVersion,
142
+ depositorPubkey: e.prePeginParams.depositorPubkey,
143
+ vaultProviderPubkey: e.prePeginParams.vaultProviderPubkey,
144
+ vaultKeeperPubkeys: e.prePeginParams.vaultKeeperPubkeys,
145
+ universalChallengerPubkeys: e.prePeginParams.universalChallengerPubkeys,
146
+ hashlocks: [...e.prePeginParams.hashlocks],
147
+ timelockRefund: e.prePeginParams.timelockRefund,
148
+ pegInAmounts: [...e.prePeginParams.pegInAmounts],
149
+ feeRate: e.prePeginParams.feeRate,
150
+ minPeginFeeRate: e.prePeginParams.minPeginFeeRate,
151
+ numLocalChallengers: e.prePeginParams.numLocalChallengers,
152
+ councilQuorum: e.prePeginParams.councilQuorum,
153
+ councilSize: e.prePeginParams.councilSize,
154
+ network: e.prePeginParams.network,
155
+ authAnchorHash: Q(
156
+ e.prePeginParams.authAnchorHash
157
+ )
158
+ },
159
+ e.timelockPegin,
160
+ e.fundedPrePeginTxHex,
161
+ e.htlcVout
162
+ );
163
+ return await xe(t, e), {
164
+ txHex: t.txHex,
165
+ txid: t.txid,
166
+ vaultScriptPubKey: t.vaultScriptPubKey,
167
+ vaultValue: t.vaultValue
168
+ };
169
+ }
170
+ const we = 2, $e = 1;
171
+ async function xe(e, t) {
172
+ const n = t.prePeginParams.vaultCoreVersion;
173
+ await ee(n, e.txHex);
174
+ const i = await G(n), o = we + (i ? 1 : 0), r = S.fromHex(P(e.txHex));
175
+ if (r.ins.length !== 1)
176
+ throw new Error(
177
+ `PegIn tx has ${r.ins.length} input(s), expected exactly 1 (the Pre-PegIn HTLC outpoint).`
178
+ );
179
+ const s = S.fromHex(
180
+ P(t.fundedPrePeginTxHex)
181
+ ).getId(), u = _(
182
+ new Uint8Array(r.ins[0].hash).slice().reverse()
183
+ );
184
+ if (u !== s)
185
+ throw new Error(
186
+ `PegIn input spends txid ${u}, expected the funded Pre-PegIn ${s}.`
187
+ );
188
+ if (r.ins[0].index !== t.htlcVout)
189
+ throw new Error(
190
+ `PegIn input spends Pre-PegIn output ${r.ins[0].index}, expected the requested HTLC vout ${t.htlcVout}.`
191
+ );
192
+ if (r.outs.length !== o)
193
+ throw new Error(
194
+ `PegIn tx has ${r.outs.length} output(s), expected exactly ${o} for vaultCoreVersion ${n} (vault + depositor claim${i ? " + P2A anchor" : ""}).`
195
+ );
196
+ const l = t.prePeginParams.pegInAmounts[t.htlcVout];
197
+ if (e.vaultValue !== l)
198
+ throw new Error(
199
+ `PegIn vault output value ${e.vaultValue} does not match the requested peg-in amount ${l} for HTLC ${t.htlcVout}; refusing to sign a vault that locks a different amount than requested.`
200
+ );
201
+ const c = r.outs[0];
202
+ if (BigInt(c.value) !== e.vaultValue)
203
+ throw new Error(
204
+ `Encoded PegIn vault output value ${c.value} does not match the WASM-reported vaultValue ${e.vaultValue}.`
205
+ );
206
+ const h = c.script.toString("hex"), p = P(
207
+ e.vaultScriptPubKey
208
+ ).toLowerCase();
209
+ if (h.toLowerCase() !== p)
210
+ throw new Error(
211
+ `Encoded PegIn vault output scriptPubKey ${h} does not match the WASM-reported vaultScriptPubKey ${e.vaultScriptPubKey}.`
212
+ );
213
+ const g = r.getId();
214
+ if (g !== P(e.txid).toLowerCase())
215
+ throw new Error(
216
+ `Encoded PegIn txid ${g} does not match the WASM-reported txid ${e.txid}.`
217
+ );
218
+ const a = await D(
219
+ n,
220
+ t.prePeginParams.numLocalChallengers,
221
+ t.prePeginParams.universalChallengerPubkeys.length,
222
+ t.prePeginParams.councilQuorum,
223
+ t.prePeginParams.councilSize,
224
+ t.prePeginParams.feeRate
225
+ ), v = r.outs[$e];
226
+ if (BigInt(v.value) !== a)
227
+ throw new Error(
228
+ `Encoded PegIn depositor-claim output value ${v.value} does not match the independently computed claim value ${a} for vaultCoreVersion ${n}.`
229
+ );
230
+ const $ = Se(
231
+ t.prePeginParams.depositorPubkey
232
+ );
233
+ if (!v.script.equals($))
234
+ throw new Error(
235
+ `Encoded PegIn depositor-claim output scriptPubKey ${v.script.toString("hex")} does not pay to the depositor's claim script (expected ${$.toString("hex")}).`
236
+ );
237
+ }
238
+ function Se(e) {
239
+ const t = oe.compile([
240
+ d.from(m(e)),
241
+ re.OP_CHECKSIG
242
+ ]), { output: n } = ie.p2tr({
243
+ internalPubkey: d.from(Y),
244
+ scriptTree: { output: t }
245
+ });
246
+ if (!n)
247
+ throw new Error(
248
+ "Failed to derive the depositor-claim P2TR scriptPubKey for PegIn output validation"
249
+ );
250
+ return n;
251
+ }
252
+ async function me(e) {
253
+ const t = await te(
254
+ {
255
+ txGraphVersion: e.vaultCoreVersion,
256
+ depositor: e.depositor,
257
+ vaultProvider: e.vaultProvider,
258
+ vaultKeepers: e.vaultKeepers,
259
+ universalChallengers: e.universalChallengers,
260
+ timelockPegin: e.timelockPegin
261
+ },
262
+ e.network
263
+ );
264
+ return {
265
+ payoutScript: t.payoutScript,
266
+ taprootScriptHash: t.taprootScriptHash,
267
+ scriptPubKey: t.scriptPubKey,
268
+ address: t.address,
269
+ payoutControlBlock: t.payoutControlBlock
270
+ };
271
+ }
272
+ const ye = 500, Ee = 55, F = 0xffffffffn, K = 32, Ce = 34;
273
+ function be(e) {
274
+ if (typeof e.protocolFeeRate != "bigint" || e.protocolFeeRate <= 0n || e.protocolFeeRate > F)
275
+ throw new Error(
276
+ `protocolFeeRate must be in [1, ${F}] sat/vB, got ${e.protocolFeeRate}`
277
+ );
278
+ for (const [t, n] of [
279
+ ["keepers", e.numVaultKeepers],
280
+ ["challengers", e.numUniversalChallengers]
281
+ ])
282
+ if (!Number.isInteger(n) || n < 1 || n > K)
283
+ throw new Error(
284
+ `Participant count for ${t} (${n}) is outside the device range [1, ${K}].`
285
+ );
286
+ if (!Number.isInteger(e.councilSize) || e.councilSize < 1)
287
+ throw new Error(
288
+ `councilSize must be an integer >= 1, got ${e.councilSize}`
289
+ );
290
+ }
291
+ async function Ae(e, t) {
292
+ const { implicitFeeSats: n, out0Len: i, out1Len: o } = t, r = e.numVaultKeepers + e.numUniversalChallengers, s = BigInt(n), u = Math.max(0, i - Ce), l = ye + Ee * r + u, c = e.protocolFeeRate * BigInt(l);
293
+ if (s > c)
294
+ throw new Error(
295
+ `Payout implicit fee ${n} sats exceeds the safety cap of ${c} sats (${e.protocolFeeRate} sat/vB x ${l} vB for ${r} participants); refusing to sign payout.`
296
+ );
297
+ const h = await ne(
298
+ e.vaultCoreVersion,
299
+ e.numVaultKeepers,
300
+ e.numUniversalChallengers,
301
+ e.numVaultKeepers,
302
+ e.councilSize,
303
+ i,
304
+ o,
305
+ e.protocolFeeRate
306
+ );
307
+ if (s < h)
308
+ throw new Error(
309
+ `Payout implicit fee ${n} sats is below the floor of ${h} sats (the smallest fee any known vault-provider build produces for this shape); refusing to sign payout.`
310
+ );
311
+ }
312
+ const N = 3;
313
+ async function ze(e) {
314
+ const t = {
315
+ vaultCoreVersion: e.vaultCoreVersion,
316
+ numVaultKeepers: e.vaultKeeperBtcPubkeys.length,
317
+ numUniversalChallengers: e.universalChallengerBtcPubkeys.length,
318
+ councilSize: e.councilSize,
319
+ protocolFeeRate: e.protocolFeeRate
320
+ };
321
+ be(t);
322
+ const n = S.fromHex(P(e.payoutTxHex)), i = S.fromHex(P(e.peginTxHex)), o = S.fromHex(P(e.assertTxHex));
323
+ if (n.ins.length !== O)
324
+ throw new Error(
325
+ `Payout transaction must have exactly ${O} inputs, got ${n.ins.length}`
326
+ );
327
+ const r = M(
328
+ n.ins[0],
329
+ 0,
330
+ i,
331
+ "PegIn",
332
+ le
333
+ ), s = M(
334
+ n.ins[1],
335
+ 1,
336
+ o,
337
+ "Assert",
338
+ he
339
+ ), { out0Len: u, out1Len: l } = Te(
340
+ n,
341
+ r.value,
342
+ e
343
+ ), c = r.value + s.value;
344
+ let h = 0;
345
+ for (const a of n.outs) h += a.value;
346
+ if (h > c)
347
+ throw new Error(
348
+ `Payout outputs (${h} sats) exceed inputs (${c} sats); invalid transaction.`
349
+ );
350
+ const p = c - h;
351
+ await Ae(t, {
352
+ implicitFeeSats: p,
353
+ out0Len: u,
354
+ out1Len: l
355
+ });
356
+ const g = await me({
357
+ vaultCoreVersion: e.vaultCoreVersion,
358
+ depositor: e.depositorBtcPubkey,
359
+ vaultProvider: e.vaultProviderBtcPubkey,
360
+ vaultKeepers: e.vaultKeeperBtcPubkeys,
361
+ universalChallengers: e.universalChallengerBtcPubkeys,
362
+ timelockPegin: e.timelockPegin,
363
+ network: e.network
364
+ });
365
+ return {
366
+ psbtHex: Ie(
367
+ n,
368
+ r,
369
+ s,
370
+ g
371
+ )
372
+ };
373
+ }
374
+ function M(e, t, n, i, o) {
375
+ const r = _(
376
+ new Uint8Array(e.hash).slice().reverse()
377
+ ), s = n.getId();
378
+ if (r !== s || e.index !== o)
379
+ throw new Error(
380
+ `Input ${t} must spend ${i}:${o}. Expected ${s}:${o}, got ${r}:${e.index}`
381
+ );
382
+ const u = n.outs[e.index];
383
+ if (!u)
384
+ throw new Error(
385
+ `Previous output not found for input ${t} (txid: ${r}, index: ${e.index})`
386
+ );
387
+ return u;
388
+ }
389
+ function Ie(e, t, n, i) {
390
+ const o = new A();
391
+ o.setVersion(e.version), o.setLocktime(e.locktime);
392
+ const [r, s] = e.ins;
393
+ o.addInput({
394
+ hash: r.hash,
395
+ index: r.index,
396
+ sequence: r.sequence,
397
+ witnessUtxo: {
398
+ script: t.script,
399
+ value: t.value
400
+ },
401
+ tapLeafScript: [
402
+ {
403
+ leafVersion: I,
404
+ script: d.from(m(i.payoutScript)),
405
+ controlBlock: d.from(
406
+ m(i.payoutControlBlock)
407
+ )
408
+ }
409
+ ],
410
+ tapInternalKey: d.from(Y)
411
+ // sighashType omitted - defaults to SIGHASH_DEFAULT (0x00) for Taproot
412
+ }), o.addInput({
413
+ hash: s.hash,
414
+ index: s.index,
415
+ sequence: s.sequence,
416
+ witnessUtxo: {
417
+ script: n.script,
418
+ value: n.value
419
+ }
420
+ });
421
+ for (const u of e.outs)
422
+ o.addOutput({
423
+ script: u.script,
424
+ value: u.value
425
+ });
426
+ return o.toHex();
427
+ }
428
+ function Te(e, t, n) {
429
+ const {
430
+ claimerBtcPubkey: i,
431
+ vaultProviderBtcPubkey: o,
432
+ depositorBtcPubkey: r,
433
+ vaultKeeperBtcPubkeys: s,
434
+ registeredPayoutScriptPubKey: u,
435
+ commissionBps: l
436
+ } = n;
437
+ if (!ce(u))
438
+ throw new Error("Invalid registeredPayoutScriptPubKey: not valid hex");
439
+ const c = P(i).toLowerCase(), h = P(o).toLowerCase(), p = P(r).toLowerCase(), g = s.map(
440
+ (C) => P(C).toLowerCase()
441
+ );
442
+ let a, v, $;
443
+ if (c === h)
444
+ a = "vp-claimer", v = pe, $ = P(u);
445
+ else if (c === p)
446
+ a = "depositor-as-claimer", v = U, $ = P(u);
447
+ else if (g.includes(c))
448
+ a = "vk-claimer", v = U, $ = P(ae(c));
449
+ else
450
+ throw new Error(
451
+ `Unknown claimer pubkey ${c}: not VP, depositor, or a registered vault keeper`
452
+ );
453
+ if (e.outs.length !== v)
454
+ throw new Error(
455
+ `Payout transaction has ${e.outs.length} output(s), expected exactly ${v} for role ${a}.`
456
+ );
457
+ const f = d.from($, "hex");
458
+ if (!e.outs[0].script.equals(f))
459
+ throw new Error(
460
+ `Payout transaction output 0 does not pay the expected scriptPubKey for role ${a}`
461
+ );
462
+ const x = v - 1;
463
+ if (e.outs[x].value !== R)
464
+ throw new Error(
465
+ `Payout CPFP anchor (out ${x}) value ${e.outs[x].value} sats must equal ${R} sats`
466
+ );
467
+ if (a === "vp-claimer") {
468
+ if (!Number.isInteger(l) || l < 0 || l >= L)
469
+ throw new Error(
470
+ `commissionBps must be an integer in [0, ${L}), got ${l}`
471
+ );
472
+ const C = Math.floor(
473
+ t * l / de
474
+ );
475
+ if (e.outs[1].value > C)
476
+ throw new Error(
477
+ `Payout VP commission (out 1) value ${e.outs[1].value} sats exceeds cap ${C} sats (${l} bps of peginValue=${t})`
478
+ );
479
+ }
480
+ const E = f.length;
481
+ if (E === 0 || E > b)
482
+ throw new Error(
483
+ `Payout receiver scriptPubKey length ${E} is outside the contract's registration cap [1, ${b}]; refusing to sign payout.`
484
+ );
485
+ const y = a === "vp-claimer" ? e.outs[1].script.length : void 0;
486
+ if (y !== void 0 && (y === 0 || y > b))
487
+ throw new Error(
488
+ `Payout commission scriptPubKey length ${y} is outside the contract's registration cap [1, ${b}]; refusing to sign payout.`
489
+ );
490
+ return { out0Len: E, out1Len: y };
491
+ }
492
+ function De(e, t, n = 0) {
493
+ const i = A.fromHex(e);
494
+ if (n >= i.data.inputs.length)
495
+ throw new Error(
496
+ `Input index ${n} out of range (${i.data.inputs.length} inputs)`
497
+ );
498
+ const o = i.data.inputs[n];
499
+ if (o.tapScriptSig && o.tapScriptSig.length > 0) {
500
+ const r = m(t);
501
+ for (const s of o.tapScriptSig)
502
+ if (s.pubkey.equals(d.from(r)))
503
+ return q(s.signature, n);
504
+ throw new Error(
505
+ `No signature found for depositor pubkey: ${t} at input ${n}`
506
+ );
507
+ }
508
+ if (o.finalScriptWitness && o.finalScriptWitness.length > 0) {
509
+ const r = _e(o.finalScriptWitness);
510
+ if (r.length !== N)
511
+ throw new Error(
512
+ `Unexpected finalized witness stack size at input ${n}: expected ${N} items (signature, script, controlBlock), got ${r.length}`
513
+ );
514
+ return q(r[0], n);
515
+ }
516
+ throw new Error(
517
+ `No tapScriptSig or finalScriptWitness found in signed PSBT at input ${n}`
518
+ );
519
+ }
520
+ function q(e, t) {
521
+ if (e.length === 64)
522
+ return _(new Uint8Array(e));
523
+ throw e.length === 65 ? new Error(
524
+ `Unexpected sighash byte 0x${e[64].toString(16).padStart(2, "0")} at input ${t}. Expected implicit SIGHASH_DEFAULT as a 64-byte signature.`
525
+ ) : new Error(
526
+ `Unexpected signature length at input ${t}: ${e.length}`
527
+ );
528
+ }
529
+ function _e(e) {
530
+ const t = [];
531
+ let n = 0;
532
+ const i = (s) => {
533
+ if (n + s > e.length)
534
+ throw new Error(
535
+ `Malformed witness data: need ${s} byte(s) at offset ${n}, only ${e.length - n} remaining`
536
+ );
537
+ }, o = () => {
538
+ i(1);
539
+ const s = e[n++];
540
+ if (s < 253) return s;
541
+ if (s === 253) {
542
+ i(2);
543
+ const u = (e[n] | e[n + 1] << 8) >>> 0;
544
+ return n += 2, u;
545
+ }
546
+ if (s === 254) {
547
+ i(4);
548
+ const u = (e[n] | e[n + 1] << 8 | e[n + 2] << 16 | e[n + 3] << 24) >>> 0;
549
+ return n += 4, u;
550
+ }
551
+ throw new Error(
552
+ `Malformed witness data: 8-byte varint (0xff) not supported at offset ${n - 1}`
553
+ );
554
+ }, r = o();
555
+ for (let s = 0; s < r; s++) {
556
+ const u = o();
557
+ i(u), t.push(d.from(e.subarray(n, n + u))), n += u;
558
+ }
559
+ if (n !== e.length)
560
+ throw new Error(
561
+ `Malformed witness data: ${e.length - n} trailing byte(s) after parsing ${r} item(s)`
562
+ );
563
+ return t;
564
+ }
565
+ class w extends Error {
566
+ constructor(t) {
567
+ super(
568
+ `Wallet returned a PSBT for a different transaction: ${t}`
569
+ ), this.name = "PsbtSubstitutionError";
570
+ }
571
+ }
572
+ function W(e, t) {
573
+ try {
574
+ return A.fromHex(t);
575
+ } catch (n) {
576
+ const i = n instanceof Error ? n.message : String(n);
577
+ throw new Error(`Failed to parse ${e} PSBT: ${i}`);
578
+ }
579
+ }
580
+ const Ve = 8;
581
+ function T(e) {
582
+ return `${e.toString("hex").slice(0, Ve)}…`;
583
+ }
584
+ function X(e) {
585
+ const t = d.from(e).reverse();
586
+ return T(t);
587
+ }
588
+ function Ge(e) {
589
+ const t = W("requested", e.requestedPsbtHex), n = W("returned", e.returnedPsbtHex);
590
+ if (t.version !== n.version)
591
+ throw new w(
592
+ `tx version differs (requested=${t.version}, returned=${n.version})`
593
+ );
594
+ if (t.locktime !== n.locktime)
595
+ throw new w(
596
+ `tx locktime differs (requested=${t.locktime}, returned=${n.locktime})`
597
+ );
598
+ if (t.txInputs.length !== n.txInputs.length)
599
+ throw new w(
600
+ `input count differs (requested=${t.txInputs.length}, returned=${n.txInputs.length})`
601
+ );
602
+ if (t.txOutputs.length !== n.txOutputs.length)
603
+ throw new w(
604
+ `output count differs (requested=${t.txOutputs.length}, returned=${n.txOutputs.length})`
605
+ );
606
+ for (let i = 0; i < t.txInputs.length; i++) {
607
+ const o = t.txInputs[i], r = n.txInputs[i];
608
+ if (!o.hash.equals(r.hash))
609
+ throw new w(
610
+ `input ${i} prevout txid differs (requested=${X(o.hash)}, returned=${X(r.hash)})`
611
+ );
612
+ if (o.index !== r.index)
613
+ throw new w(
614
+ `input ${i} prevout vout differs (requested=${o.index}, returned=${r.index})`
615
+ );
616
+ if (o.sequence !== r.sequence)
617
+ throw new w(
618
+ `input ${i} sequence differs (requested=${o.sequence}, returned=${r.sequence})`
619
+ );
620
+ }
621
+ for (let i = 0; i < t.txOutputs.length; i++) {
622
+ const o = t.txOutputs[i], r = n.txOutputs[i];
623
+ if (!o.script.equals(r.script))
624
+ throw new w(
625
+ `output ${i} scriptPubKey differs (requested=${T(o.script)}, returned=${T(r.script)})`
626
+ );
627
+ if (o.value !== r.value)
628
+ throw new w(
629
+ `output ${i} value differs (requested=${o.value}, returned=${r.value})`
630
+ );
631
+ }
632
+ }
633
+ const z = 253, ke = 254, He = 65535, Oe = 4294967295;
634
+ function Re(e) {
635
+ if (e < z)
636
+ return d.from([e]);
637
+ if (e <= He) {
638
+ const t = d.alloc(2);
639
+ return t.writeUInt16LE(e), d.concat([d.from([z]), t]);
640
+ }
641
+ if (e <= Oe) {
642
+ const t = d.alloc(4);
643
+ return t.writeUInt32LE(e), d.concat([d.from([ke]), t]);
644
+ }
645
+ throw new Error(`Script too large to encode as CompactSize: ${e} bytes`);
646
+ }
647
+ const Le = "TapLeaf";
648
+ function Ue(e, t) {
649
+ const n = d.concat([
650
+ d.from([e]),
651
+ Re(t.length),
652
+ d.from(t)
653
+ ]);
654
+ return se.taggedHash(Le, n);
655
+ }
656
+ function Ye(e) {
657
+ const { requestedPsbtHex: t, signatureHex: n, signerXOnlyPubkeyHex: i, inputIndex: o } = e, r = P(n);
658
+ if (r.length !== k)
659
+ throw new Error(
660
+ `Schnorr signature for input ${o} must be ${k} hex chars (64 bytes), got ${r.length}.`
661
+ );
662
+ const s = P(i);
663
+ if (s.length !== H)
664
+ throw new Error(
665
+ `Signer x-only pubkey for input ${o} must be ${H} hex chars (32 bytes), got ${s.length}.`
666
+ );
667
+ const u = A.fromHex(t);
668
+ if (o < 0 || o >= u.data.inputs.length)
669
+ throw new Error(
670
+ `Input index ${o} out of range (${u.data.inputs.length} inputs).`
671
+ );
672
+ const l = [], c = [];
673
+ for (let f = 0; f < u.data.inputs.length; f++) {
674
+ const x = u.data.inputs[f].witnessUtxo;
675
+ if (!x)
676
+ throw new Error(
677
+ `Cannot verify signature: input ${f} of the requested PSBT has no witnessUtxo (required to recompute the Taproot sighash).`
678
+ );
679
+ l.push(x.script), c.push(x.value);
680
+ }
681
+ const h = u.data.inputs[o].tapLeafScript;
682
+ if (!h || h.length !== 1)
683
+ throw new Error(
684
+ `Cannot verify signature: input ${o} of the requested PSBT must have exactly one tapLeafScript, got ${(h == null ? void 0 : h.length) ?? 0}.`
685
+ );
686
+ const p = h[0];
687
+ if (p.leafVersion !== I)
688
+ throw new Error(
689
+ `Cannot verify signature: input ${o} tapLeafScript has leaf version 0x${p.leafVersion.toString(16)}, expected 0x${I.toString(16)}.`
690
+ );
691
+ const g = Ue(p.leafVersion, p.script), a = new S();
692
+ a.version = u.version, a.locktime = u.locktime;
693
+ for (const f of u.txInputs)
694
+ a.addInput(f.hash, f.index, f.sequence);
695
+ for (const f of u.txOutputs)
696
+ a.addOutput(f.script, f.value);
697
+ const v = a.hashForWitnessV1(
698
+ o,
699
+ l,
700
+ c,
701
+ S.SIGHASH_DEFAULT,
702
+ g
703
+ );
704
+ if (!Pe.verifySchnorr(
705
+ v,
706
+ m(s),
707
+ m(r)
708
+ ))
709
+ throw new Error(
710
+ `Schnorr signature for input ${o} (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.`
711
+ );
712
+ }
713
+ export {
714
+ w as P,
715
+ We as a,
716
+ Xe as b,
717
+ ze as c,
718
+ Ge as d,
719
+ De as e,
720
+ Ye as f,
721
+ me as g,
722
+ Q as n
723
+ };
724
+ //# sourceMappingURL=verifyScriptPathSchnorrSignature-CgrnAjMW.js.map