@babylonlabs-io/ts-sdk 0.19.0 → 0.20.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 (161) hide show
  1. package/README.md +56 -12
  2. package/dist/bitcoin-CNnPFU6Y.cjs +2 -0
  3. package/dist/bitcoin-CNnPFU6Y.cjs.map +1 -0
  4. package/dist/{constants-Q7v2O7Ps.js → bitcoin-nOOgeRyl.js} +64 -76
  5. package/dist/bitcoin-nOOgeRyl.js.map +1 -0
  6. package/dist/buildAndBroadcastRefund-DWEQvj9T.cjs +2 -0
  7. package/dist/buildAndBroadcastRefund-DWEQvj9T.cjs.map +1 -0
  8. package/dist/buildAndBroadcastRefund-DnTQkCgG.js +1331 -0
  9. package/dist/buildAndBroadcastRefund-DnTQkCgG.js.map +1 -0
  10. package/dist/challengeAssert-D1lpvuMv.js +283 -0
  11. package/dist/challengeAssert-D1lpvuMv.js.map +1 -0
  12. package/dist/challengeAssert-nYlgeAI8.cjs +2 -0
  13. package/dist/challengeAssert-nYlgeAI8.cjs.map +1 -0
  14. package/dist/errors-D9EtjJoD.cjs +2 -0
  15. package/dist/errors-D9EtjJoD.cjs.map +1 -0
  16. package/dist/{errors-aMocmFr-.js → errors-D_PI__IT.js} +98 -97
  17. package/dist/errors-D_PI__IT.js.map +1 -0
  18. package/dist/fundPeginTransaction-BLYXxLBv.js +70 -0
  19. package/dist/fundPeginTransaction-BLYXxLBv.js.map +1 -0
  20. package/dist/fundPeginTransaction-DxNOeyNI.cjs +2 -0
  21. package/dist/fundPeginTransaction-DxNOeyNI.cjs.map +1 -0
  22. package/dist/index-BpXpESWu.cjs +2 -0
  23. package/dist/index-BpXpESWu.cjs.map +1 -0
  24. package/dist/{index-BvFLf3vm.js → index-CFwoGJNQ.js} +146 -518
  25. package/dist/index-CFwoGJNQ.js.map +1 -0
  26. package/dist/index.cjs +1 -1
  27. package/dist/index.js +133 -109
  28. package/dist/payout-DzGsAnhf.js +261 -0
  29. package/dist/payout-DzGsAnhf.js.map +1 -0
  30. package/dist/payout-RFIh7GLN.cjs +2 -0
  31. package/dist/payout-RFIh7GLN.cjs.map +1 -0
  32. package/dist/psbtInputFields-05ZrwRzf.cjs +2 -0
  33. package/dist/psbtInputFields-05ZrwRzf.cjs.map +1 -0
  34. package/dist/psbtInputFields-r1ss6WLU.js +237 -0
  35. package/dist/psbtInputFields-r1ss6WLU.js.map +1 -0
  36. package/dist/signing-DeWVBl7m.js +64 -0
  37. package/dist/signing-DeWVBl7m.js.map +1 -0
  38. package/dist/signing-cl-lowxV.cjs +2 -0
  39. package/dist/signing-cl-lowxV.cjs.map +1 -0
  40. package/dist/tbv/core/clients/index.cjs +1 -1
  41. package/dist/tbv/core/clients/index.js +2 -2
  42. package/dist/tbv/core/index.cjs +1 -1
  43. package/dist/tbv/core/index.d.ts +3 -1
  44. package/dist/tbv/core/index.d.ts.map +1 -1
  45. package/dist/tbv/core/index.js +130 -106
  46. package/dist/tbv/core/managers/PeginManager.d.ts +1 -1
  47. package/dist/tbv/core/primitives/index.cjs +1 -1
  48. package/dist/tbv/core/primitives/index.d.ts +3 -2
  49. package/dist/tbv/core/primitives/index.d.ts.map +1 -1
  50. package/dist/tbv/core/primitives/index.js +24 -23
  51. package/dist/tbv/core/primitives/psbt/__tests__/helpers.d.ts +1 -1
  52. package/dist/tbv/core/primitives/psbt/index.d.ts +1 -1
  53. package/dist/tbv/core/primitives/psbt/payout.d.ts.map +1 -1
  54. package/dist/tbv/core/primitives/psbt/pegin.d.ts +2 -3
  55. package/dist/tbv/core/primitives/psbt/pegin.d.ts.map +1 -1
  56. package/dist/tbv/core/primitives/utils/bitcoin.d.ts +4 -0
  57. package/dist/tbv/core/primitives/utils/bitcoin.d.ts.map +1 -1
  58. package/dist/tbv/core/primitives/utils/index.d.ts +1 -1
  59. package/dist/tbv/core/primitives/utils/index.d.ts.map +1 -1
  60. package/dist/tbv/core/services/activation/__tests__/activateVault.test.d.ts +2 -0
  61. package/dist/tbv/core/services/activation/__tests__/activateVault.test.d.ts.map +1 -0
  62. package/dist/tbv/core/services/activation/activateVault.d.ts +77 -0
  63. package/dist/tbv/core/services/activation/activateVault.d.ts.map +1 -0
  64. package/dist/tbv/core/services/activation/index.d.ts +7 -0
  65. package/dist/tbv/core/services/activation/index.d.ts.map +1 -0
  66. package/dist/tbv/core/services/deposit/__tests__/peginState.test.d.ts +2 -0
  67. package/dist/tbv/core/services/deposit/__tests__/peginState.test.d.ts.map +1 -0
  68. package/dist/tbv/core/services/deposit/__tests__/validation.test.d.ts +5 -0
  69. package/dist/tbv/core/services/deposit/__tests__/validation.test.d.ts.map +1 -0
  70. package/dist/tbv/core/services/deposit/index.d.ts +4 -0
  71. package/dist/tbv/core/services/deposit/index.d.ts.map +1 -1
  72. package/dist/tbv/core/services/deposit/peginState.d.ts +93 -0
  73. package/dist/tbv/core/services/deposit/peginState.d.ts.map +1 -0
  74. package/dist/tbv/core/services/deposit/validation.d.ts +114 -0
  75. package/dist/tbv/core/services/deposit/validation.d.ts.map +1 -0
  76. package/dist/tbv/core/services/index.cjs +1 -1
  77. package/dist/tbv/core/services/index.d.ts +3 -0
  78. package/dist/tbv/core/services/index.d.ts.map +1 -1
  79. package/dist/tbv/core/services/index.js +25 -7
  80. package/dist/tbv/core/services/index.js.map +1 -1
  81. package/dist/tbv/core/services/pegout/__tests__/state.test.d.ts +2 -0
  82. package/dist/tbv/core/services/pegout/__tests__/state.test.d.ts.map +1 -0
  83. package/dist/tbv/core/services/pegout/index.d.ts +2 -0
  84. package/dist/tbv/core/services/pegout/index.d.ts.map +1 -0
  85. package/dist/tbv/core/services/pegout/state.d.ts +30 -0
  86. package/dist/tbv/core/services/pegout/state.d.ts.map +1 -0
  87. package/dist/tbv/core/services/refund/__tests__/buildAndBroadcastRefund.test.d.ts +2 -0
  88. package/dist/tbv/core/services/refund/__tests__/buildAndBroadcastRefund.test.d.ts.map +1 -0
  89. package/dist/tbv/core/services/refund/buildAndBroadcastRefund.d.ts +103 -0
  90. package/dist/tbv/core/services/refund/buildAndBroadcastRefund.d.ts.map +1 -0
  91. package/dist/tbv/core/services/refund/errors.d.ts +13 -0
  92. package/dist/tbv/core/services/refund/errors.d.ts.map +1 -0
  93. package/dist/tbv/core/services/refund/index.d.ts +8 -0
  94. package/dist/tbv/core/services/refund/index.d.ts.map +1 -0
  95. package/dist/tbv/core/utils/index.cjs +1 -1
  96. package/dist/tbv/core/utils/index.js +30 -24
  97. package/dist/tbv/core/utils/transaction/fundPeginTransaction.d.ts +1 -1
  98. package/dist/tbv/core/utils/utxo/__tests__/availability.test.d.ts +3 -0
  99. package/dist/tbv/core/utils/utxo/__tests__/availability.test.d.ts.map +1 -0
  100. package/dist/tbv/core/utils/utxo/__tests__/reservation.test.d.ts +3 -0
  101. package/dist/tbv/core/utils/utxo/__tests__/reservation.test.d.ts.map +1 -0
  102. package/dist/tbv/core/utils/utxo/availability.d.ts +62 -0
  103. package/dist/tbv/core/utils/utxo/availability.d.ts.map +1 -0
  104. package/dist/tbv/core/utils/utxo/index.d.ts +2 -0
  105. package/dist/tbv/core/utils/utxo/index.d.ts.map +1 -1
  106. package/dist/tbv/core/utils/utxo/reservation.d.ts +91 -0
  107. package/dist/tbv/core/utils/utxo/reservation.d.ts.map +1 -0
  108. package/dist/tbv/index.cjs +1 -1
  109. package/dist/tbv/index.js +130 -106
  110. package/dist/tbv/integrations/aave/index.cjs +1 -1
  111. package/dist/tbv/integrations/aave/index.cjs.map +1 -1
  112. package/dist/tbv/integrations/aave/index.d.ts +3 -3
  113. package/dist/tbv/integrations/aave/index.d.ts.map +1 -1
  114. package/dist/tbv/integrations/aave/index.js +145 -169
  115. package/dist/tbv/integrations/aave/index.js.map +1 -1
  116. package/dist/tbv/integrations/aave/utils/healthFactor.d.ts +0 -30
  117. package/dist/tbv/integrations/aave/utils/healthFactor.d.ts.map +1 -1
  118. package/dist/tbv/integrations/aave/utils/index.d.ts +2 -2
  119. package/dist/tbv/integrations/aave/utils/index.d.ts.map +1 -1
  120. package/dist/types-B-p4dhEH.cjs +2 -0
  121. package/dist/types-B-p4dhEH.cjs.map +1 -0
  122. package/dist/types-DWjaqVfP.js +608 -0
  123. package/dist/types-DWjaqVfP.js.map +1 -0
  124. package/package.json +5 -1
  125. package/dist/challengeAssert-D_k_ADgP.cjs +0 -2
  126. package/dist/challengeAssert-D_k_ADgP.cjs.map +0 -1
  127. package/dist/challengeAssert-k5_LWUtO.js +0 -362
  128. package/dist/challengeAssert-k5_LWUtO.js.map +0 -1
  129. package/dist/constants-EiyZkXce.cjs +0 -2
  130. package/dist/constants-EiyZkXce.cjs.map +0 -1
  131. package/dist/constants-Q7v2O7Ps.js.map +0 -1
  132. package/dist/errors-DVNYib5y.cjs +0 -2
  133. package/dist/errors-DVNYib5y.cjs.map +0 -1
  134. package/dist/errors-aMocmFr-.js.map +0 -1
  135. package/dist/fundPeginTransaction-DpwnDslW.js +0 -50
  136. package/dist/fundPeginTransaction-DpwnDslW.js.map +0 -1
  137. package/dist/fundPeginTransaction-EbrZzlrh.cjs +0 -2
  138. package/dist/fundPeginTransaction-EbrZzlrh.cjs.map +0 -1
  139. package/dist/index-BX-V3C9t.js +0 -995
  140. package/dist/index-BX-V3C9t.js.map +0 -1
  141. package/dist/index-BgnxXV5G.cjs +0 -2
  142. package/dist/index-BgnxXV5G.cjs.map +0 -1
  143. package/dist/index-BvFLf3vm.js.map +0 -1
  144. package/dist/index-DpKhuCta.cjs +0 -2
  145. package/dist/index-DpKhuCta.cjs.map +0 -1
  146. package/dist/payout-Ce9vSs9e.js +0 -164
  147. package/dist/payout-Ce9vSs9e.js.map +0 -1
  148. package/dist/payout-CfsDnjKI.cjs +0 -2
  149. package/dist/payout-CfsDnjKI.cjs.map +0 -1
  150. package/dist/psbtInputFields-C0nKn_GD.cjs +0 -2
  151. package/dist/psbtInputFields-C0nKn_GD.cjs.map +0 -1
  152. package/dist/psbtInputFields-DO0ELwiv.js +0 -116
  153. package/dist/psbtInputFields-DO0ELwiv.js.map +0 -1
  154. package/dist/signing-Deg5lCoC.cjs +0 -2
  155. package/dist/signing-Deg5lCoC.cjs.map +0 -1
  156. package/dist/signing-Drwr3bXB.js +0 -16
  157. package/dist/signing-Drwr3bXB.js.map +0 -1
  158. package/dist/types-D1rYwwCu.js +0 -235
  159. package/dist/types-D1rYwwCu.js.map +0 -1
  160. package/dist/types-DEWiqXYp.cjs +0 -2
  161. package/dist/types-DEWiqXYp.cjs.map +0 -1
@@ -0,0 +1,1331 @@
1
+ var ke = Object.defineProperty;
2
+ var Be = (e, t, r) => t in e ? ke(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
3
+ var f = (e, t, r) => Be(e, typeof t != "symbol" ? t + "" : t, r);
4
+ import { B as He, c as _e, a as Ie, R as Ce, d as be, D as O } from "./types-DWjaqVfP.js";
5
+ import { v as ne, b as Le, s as g, e as $e, p as J, f as L } from "./bitcoin-nOOgeRyl.js";
6
+ import * as Re from "bitcoinjs-lib";
7
+ import { Transaction as Oe, Psbt as ee } from "bitcoinjs-lib";
8
+ import { Buffer as xe } from "buffer";
9
+ import { c as M } from "./signing-DeWVBl7m.js";
10
+ import "@babylonlabs-io/babylon-tbv-rust-wasm";
11
+ import { a as se, e as G, b as Ne } from "./payout-DzGsAnhf.js";
12
+ class Ue {
13
+ /**
14
+ * Creates a new PayoutManager instance.
15
+ *
16
+ * @param config - Manager configuration including wallet
17
+ */
18
+ constructor(t) {
19
+ f(this, "config");
20
+ this.config = t;
21
+ }
22
+ /**
23
+ * Signs a Payout transaction and extracts the Schnorr signature.
24
+ *
25
+ * Flow:
26
+ * 1. Vault provider submits Claim transaction
27
+ * 2. Claimer submits Assert transaction to prove validity
28
+ * 3. Payout can be executed (references Assert tx)
29
+ *
30
+ * This method orchestrates the following steps:
31
+ * 1. Get wallet's public key and convert to x-only format
32
+ * 2. Validate wallet pubkey matches on-chain depositor pubkey (if provided)
33
+ * 3. Build unsigned PSBT using primitives
34
+ * 4. Sign PSBT via btcWallet.signPsbt()
35
+ * 5. Extract 64-byte Schnorr signature using primitives
36
+ *
37
+ * The returned signature can be submitted to the vault provider API.
38
+ *
39
+ * @param params - Payout signing parameters
40
+ * @returns Signature result with 64-byte Schnorr signature and depositor pubkey
41
+ * @throws Error if wallet pubkey doesn't match depositor pubkey
42
+ * @throws Error if wallet operations fail or signature extraction fails
43
+ */
44
+ async signPayoutTransaction(t) {
45
+ this.validatePayoutOutputs(
46
+ t.payoutTxHex,
47
+ t.registeredPayoutScriptPubKey
48
+ );
49
+ const r = await this.config.btcWallet.getPublicKeyHex(), { depositorPubkey: n } = ne(
50
+ r,
51
+ t.depositorBtcPubkey
52
+ ), s = await se({
53
+ payoutTxHex: t.payoutTxHex,
54
+ peginTxHex: t.peginTxHex,
55
+ assertTxHex: t.assertTxHex,
56
+ depositorBtcPubkey: n,
57
+ vaultProviderBtcPubkey: t.vaultProviderBtcPubkey,
58
+ vaultKeeperBtcPubkeys: t.vaultKeeperBtcPubkeys,
59
+ universalChallengerBtcPubkeys: t.universalChallengerBtcPubkeys,
60
+ timelockPegin: t.timelockPegin,
61
+ network: this.config.network
62
+ }), c = await this.config.btcWallet.signPsbt(
63
+ s.psbtHex,
64
+ M(r, 1)
65
+ );
66
+ return {
67
+ signature: G(c, n),
68
+ depositorBtcPubkey: n
69
+ };
70
+ }
71
+ /**
72
+ * Gets the configured Bitcoin network.
73
+ *
74
+ * @returns The Bitcoin network (mainnet, testnet, signet, regtest)
75
+ */
76
+ getNetwork() {
77
+ return this.config.network;
78
+ }
79
+ /**
80
+ * Checks if the wallet supports batch signing (signPsbts).
81
+ *
82
+ * @returns true if batch signing is supported
83
+ */
84
+ supportsBatchSigning() {
85
+ return typeof this.config.btcWallet.signPsbts == "function";
86
+ }
87
+ /**
88
+ * Batch signs multiple payout transactions (1 per claimer).
89
+ * This allows signing all transactions with a single wallet interaction.
90
+ *
91
+ * @param transactions - Array of payout params to sign
92
+ * @returns Array of signature results matching input order
93
+ * @throws Error if wallet doesn't support batch signing
94
+ * @throws Error if any signing operation fails
95
+ */
96
+ async signPayoutTransactionsBatch(t) {
97
+ if (!this.supportsBatchSigning())
98
+ throw new Error(
99
+ "Wallet does not support batch signing (signPsbts method not available)"
100
+ );
101
+ const r = await this.config.btcWallet.getPublicKeyHex(), n = [], s = [], c = [];
102
+ for (const o of t) {
103
+ this.validatePayoutOutputs(
104
+ o.payoutTxHex,
105
+ o.registeredPayoutScriptPubKey
106
+ );
107
+ const { depositorPubkey: u } = ne(
108
+ r,
109
+ o.depositorBtcPubkey
110
+ );
111
+ c.push(u);
112
+ const h = await se({
113
+ payoutTxHex: o.payoutTxHex,
114
+ peginTxHex: o.peginTxHex,
115
+ assertTxHex: o.assertTxHex,
116
+ depositorBtcPubkey: u,
117
+ vaultProviderBtcPubkey: o.vaultProviderBtcPubkey,
118
+ vaultKeeperBtcPubkeys: o.vaultKeeperBtcPubkeys,
119
+ universalChallengerBtcPubkeys: o.universalChallengerBtcPubkeys,
120
+ timelockPegin: o.timelockPegin,
121
+ network: this.config.network
122
+ });
123
+ n.push(h.psbtHex), s.push(M(r, 1));
124
+ }
125
+ const a = await this.config.btcWallet.signPsbts(
126
+ n,
127
+ s
128
+ );
129
+ if (a.length !== t.length)
130
+ throw new Error(
131
+ `Expected ${t.length} signed PSBTs but received ${a.length}`
132
+ );
133
+ const i = [];
134
+ for (let o = 0; o < t.length; o++) {
135
+ const u = c[o], h = G(
136
+ a[o],
137
+ u
138
+ );
139
+ i.push({
140
+ payoutSignature: h,
141
+ depositorBtcPubkey: u
142
+ });
143
+ }
144
+ return i;
145
+ }
146
+ /**
147
+ * Validates that the payout transaction's largest output pays to the
148
+ * registered depositor payout address (scriptPubKey).
149
+ *
150
+ * This prevents two attack vectors from a malicious vault provider:
151
+ * 1. Substituting a completely different payout address
152
+ * 2. Including a dust output to the correct address while routing
153
+ * the actual funds to an attacker-controlled address
154
+ *
155
+ * @param payoutTxHex - Raw payout transaction hex
156
+ * @param registeredPayoutScriptPubKey - On-chain registered scriptPubKey (hex, with or without 0x prefix)
157
+ * @throws Error if scriptPubKey is invalid hex
158
+ * @throws Error if the largest output does not pay to the registered address
159
+ */
160
+ validatePayoutOutputs(t, r) {
161
+ if (!Le(r))
162
+ throw new Error(
163
+ "Invalid registeredPayoutScriptPubKey: not valid hex"
164
+ );
165
+ const n = xe.from(
166
+ g(r),
167
+ "hex"
168
+ ), s = Oe.fromHex(g(t));
169
+ if (s.outs.length === 0)
170
+ throw new Error("Payout transaction has no outputs");
171
+ if (!s.outs.reduce(
172
+ (a, i) => i.value > a.value ? i : a
173
+ ).script.equals(n))
174
+ throw new Error(
175
+ "Payout transaction does not pay to the registered depositor payout address"
176
+ );
177
+ }
178
+ }
179
+ function De(e) {
180
+ return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array";
181
+ }
182
+ function oe(e, t = "") {
183
+ if (!Number.isSafeInteger(e) || e < 0) {
184
+ const r = t && `"${t}" `;
185
+ throw new Error(`${r}expected integer >= 0, got ${e}`);
186
+ }
187
+ }
188
+ function te(e, t, r = "") {
189
+ const n = De(e), s = e == null ? void 0 : e.length, c = t !== void 0;
190
+ if (!n || c && s !== t) {
191
+ const a = r && `"${r}" `, i = c ? ` of length ${t}` : "", o = n ? `length=${s}` : `type=${typeof e}`;
192
+ throw new Error(a + "expected Uint8Array" + i + ", got " + o);
193
+ }
194
+ return e;
195
+ }
196
+ function nr(e) {
197
+ if (typeof e != "function" || typeof e.create != "function")
198
+ throw new Error("Hash must wrapped by utils.createHasher");
199
+ oe(e.outputLen), oe(e.blockLen);
200
+ }
201
+ function ie(e, t = !0) {
202
+ if (e.destroyed)
203
+ throw new Error("Hash instance has been destroyed");
204
+ if (t && e.finished)
205
+ throw new Error("Hash#digest() has already been called");
206
+ }
207
+ function Ve(e, t) {
208
+ te(e, void 0, "digestInto() output");
209
+ const r = t.outputLen;
210
+ if (e.length < r)
211
+ throw new Error('"digestInto() output" expected to be of length >=' + r);
212
+ }
213
+ function sr(e) {
214
+ return new Uint32Array(e.buffer, e.byteOffset, Math.floor(e.byteLength / 4));
215
+ }
216
+ function N(...e) {
217
+ for (let t = 0; t < e.length; t++)
218
+ e[t].fill(0);
219
+ }
220
+ function Y(e) {
221
+ return new DataView(e.buffer, e.byteOffset, e.byteLength);
222
+ }
223
+ function m(e, t) {
224
+ return e << 32 - t | e >>> t;
225
+ }
226
+ function or(e, t) {
227
+ return e << t | e >>> 32 - t >>> 0;
228
+ }
229
+ const Fe = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
230
+ function Ke(e) {
231
+ return e << 24 & 4278190080 | e << 8 & 16711680 | e >>> 8 & 65280 | e >>> 24 & 255;
232
+ }
233
+ function Me(e) {
234
+ for (let t = 0; t < e.length; t++)
235
+ e[t] = Ke(e[t]);
236
+ return e;
237
+ }
238
+ const ir = Fe ? (e) => e : Me;
239
+ function Ge(e) {
240
+ if (typeof e != "string")
241
+ throw new Error("string expected");
242
+ return new Uint8Array(new TextEncoder().encode(e));
243
+ }
244
+ function ar(e, t = "") {
245
+ return typeof e == "string" ? Ge(e) : te(e, void 0, t);
246
+ }
247
+ function cr(e, t) {
248
+ if (t !== void 0 && {}.toString.call(t) !== "[object Object]")
249
+ throw new Error("options must be object or undefined");
250
+ return Object.assign(e, t);
251
+ }
252
+ function pe(e, t = {}) {
253
+ const r = (s, c) => e(c).update(s).digest(), n = e(void 0);
254
+ return r.outputLen = n.outputLen, r.blockLen = n.blockLen, r.create = (s) => e(s), Object.assign(r, t), Object.freeze(r);
255
+ }
256
+ const ge = (e) => ({
257
+ oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, e])
258
+ });
259
+ function We(e, t, r) {
260
+ return e & t ^ ~e & r;
261
+ }
262
+ function ze(e, t, r) {
263
+ return e & t ^ e & r ^ t & r;
264
+ }
265
+ class ye {
266
+ constructor(t, r, n, s) {
267
+ f(this, "blockLen");
268
+ f(this, "outputLen");
269
+ f(this, "padOffset");
270
+ f(this, "isLE");
271
+ // For partial updates less than block size
272
+ f(this, "buffer");
273
+ f(this, "view");
274
+ f(this, "finished", !1);
275
+ f(this, "length", 0);
276
+ f(this, "pos", 0);
277
+ f(this, "destroyed", !1);
278
+ this.blockLen = t, this.outputLen = r, this.padOffset = n, this.isLE = s, this.buffer = new Uint8Array(t), this.view = Y(this.buffer);
279
+ }
280
+ update(t) {
281
+ ie(this), te(t);
282
+ const { view: r, buffer: n, blockLen: s } = this, c = t.length;
283
+ for (let a = 0; a < c; ) {
284
+ const i = Math.min(s - this.pos, c - a);
285
+ if (i === s) {
286
+ const o = Y(t);
287
+ for (; s <= c - a; a += s)
288
+ this.process(o, a);
289
+ continue;
290
+ }
291
+ n.set(t.subarray(a, a + i), this.pos), this.pos += i, a += i, this.pos === s && (this.process(r, 0), this.pos = 0);
292
+ }
293
+ return this.length += t.length, this.roundClean(), this;
294
+ }
295
+ digestInto(t) {
296
+ ie(this), Ve(t, this), this.finished = !0;
297
+ const { buffer: r, view: n, blockLen: s, isLE: c } = this;
298
+ let { pos: a } = this;
299
+ r[a++] = 128, N(this.buffer.subarray(a)), this.padOffset > s - a && (this.process(n, 0), a = 0);
300
+ for (let l = a; l < s; l++)
301
+ r[l] = 0;
302
+ n.setBigUint64(s - 8, BigInt(this.length * 8), c), this.process(n, 0);
303
+ const i = Y(t), o = this.outputLen;
304
+ if (o % 4)
305
+ throw new Error("_sha2: outputLen must be aligned to 32bit");
306
+ const u = o / 4, h = this.get();
307
+ if (u > h.length)
308
+ throw new Error("_sha2: outputLen bigger than state");
309
+ for (let l = 0; l < u; l++)
310
+ i.setUint32(4 * l, h[l], c);
311
+ }
312
+ digest() {
313
+ const { buffer: t, outputLen: r } = this;
314
+ this.digestInto(t);
315
+ const n = t.slice(0, r);
316
+ return this.destroy(), n;
317
+ }
318
+ _cloneInto(t) {
319
+ t || (t = new this.constructor()), t.set(...this.get());
320
+ const { blockLen: r, buffer: n, length: s, finished: c, destroyed: a, pos: i } = this;
321
+ return t.destroyed = a, t.finished = c, t.length = s, t.pos = i, s % r && t.buffer.set(n), t;
322
+ }
323
+ clone() {
324
+ return this._cloneInto();
325
+ }
326
+ }
327
+ const B = /* @__PURE__ */ Uint32Array.from([
328
+ 1779033703,
329
+ 3144134277,
330
+ 1013904242,
331
+ 2773480762,
332
+ 1359893119,
333
+ 2600822924,
334
+ 528734635,
335
+ 1541459225
336
+ ]), w = /* @__PURE__ */ Uint32Array.from([
337
+ 1779033703,
338
+ 4089235720,
339
+ 3144134277,
340
+ 2227873595,
341
+ 1013904242,
342
+ 4271175723,
343
+ 2773480762,
344
+ 1595750129,
345
+ 1359893119,
346
+ 2917565137,
347
+ 2600822924,
348
+ 725511199,
349
+ 528734635,
350
+ 4215389547,
351
+ 1541459225,
352
+ 327033209
353
+ ]), V = /* @__PURE__ */ BigInt(2 ** 32 - 1), ae = /* @__PURE__ */ BigInt(32);
354
+ function Xe(e, t = !1) {
355
+ return t ? { h: Number(e & V), l: Number(e >> ae & V) } : { h: Number(e >> ae & V) | 0, l: Number(e & V) | 0 };
356
+ }
357
+ function je(e, t = !1) {
358
+ const r = e.length;
359
+ let n = new Uint32Array(r), s = new Uint32Array(r);
360
+ for (let c = 0; c < r; c++) {
361
+ const { h: a, l: i } = Xe(e[c], t);
362
+ [n[c], s[c]] = [a, i];
363
+ }
364
+ return [n, s];
365
+ }
366
+ const ce = (e, t, r) => e >>> r, ue = (e, t, r) => e << 32 - r | t >>> r, $ = (e, t, r) => e >>> r | t << 32 - r, R = (e, t, r) => e << 32 - r | t >>> r, F = (e, t, r) => e << 64 - r | t >>> r - 32, K = (e, t, r) => e >>> r - 32 | t << 64 - r, ur = (e, t, r) => e << r | t >>> 32 - r, lr = (e, t, r) => t << r | e >>> 32 - r, fr = (e, t, r) => t << r - 32 | e >>> 64 - r, hr = (e, t, r) => e << r - 32 | t >>> 64 - r;
367
+ function S(e, t, r, n) {
368
+ const s = (t >>> 0) + (n >>> 0);
369
+ return { h: e + r + (s / 2 ** 32 | 0) | 0, l: s | 0 };
370
+ }
371
+ const Ye = (e, t, r) => (e >>> 0) + (t >>> 0) + (r >>> 0), qe = (e, t, r, n) => t + r + n + (e / 2 ** 32 | 0) | 0, Qe = (e, t, r, n) => (e >>> 0) + (t >>> 0) + (r >>> 0) + (n >>> 0), Je = (e, t, r, n, s) => t + r + n + s + (e / 2 ** 32 | 0) | 0, Ze = (e, t, r, n, s) => (e >>> 0) + (t >>> 0) + (r >>> 0) + (n >>> 0) + (s >>> 0), et = (e, t, r, n, s, c) => t + r + n + s + c + (e / 2 ** 32 | 0) | 0, tt = /* @__PURE__ */ Uint32Array.from([
372
+ 1116352408,
373
+ 1899447441,
374
+ 3049323471,
375
+ 3921009573,
376
+ 961987163,
377
+ 1508970993,
378
+ 2453635748,
379
+ 2870763221,
380
+ 3624381080,
381
+ 310598401,
382
+ 607225278,
383
+ 1426881987,
384
+ 1925078388,
385
+ 2162078206,
386
+ 2614888103,
387
+ 3248222580,
388
+ 3835390401,
389
+ 4022224774,
390
+ 264347078,
391
+ 604807628,
392
+ 770255983,
393
+ 1249150122,
394
+ 1555081692,
395
+ 1996064986,
396
+ 2554220882,
397
+ 2821834349,
398
+ 2952996808,
399
+ 3210313671,
400
+ 3336571891,
401
+ 3584528711,
402
+ 113926993,
403
+ 338241895,
404
+ 666307205,
405
+ 773529912,
406
+ 1294757372,
407
+ 1396182291,
408
+ 1695183700,
409
+ 1986661051,
410
+ 2177026350,
411
+ 2456956037,
412
+ 2730485921,
413
+ 2820302411,
414
+ 3259730800,
415
+ 3345764771,
416
+ 3516065817,
417
+ 3600352804,
418
+ 4094571909,
419
+ 275423344,
420
+ 430227734,
421
+ 506948616,
422
+ 659060556,
423
+ 883997877,
424
+ 958139571,
425
+ 1322822218,
426
+ 1537002063,
427
+ 1747873779,
428
+ 1955562222,
429
+ 2024104815,
430
+ 2227730452,
431
+ 2361852424,
432
+ 2428436474,
433
+ 2756734187,
434
+ 3204031479,
435
+ 3329325298
436
+ ]), H = /* @__PURE__ */ new Uint32Array(64);
437
+ class rt extends ye {
438
+ constructor(t) {
439
+ super(64, t, 8, !1);
440
+ }
441
+ get() {
442
+ const { A: t, B: r, C: n, D: s, E: c, F: a, G: i, H: o } = this;
443
+ return [t, r, n, s, c, a, i, o];
444
+ }
445
+ // prettier-ignore
446
+ set(t, r, n, s, c, a, i, o) {
447
+ this.A = t | 0, this.B = r | 0, this.C = n | 0, this.D = s | 0, this.E = c | 0, this.F = a | 0, this.G = i | 0, this.H = o | 0;
448
+ }
449
+ process(t, r) {
450
+ for (let l = 0; l < 16; l++, r += 4)
451
+ H[l] = t.getUint32(r, !1);
452
+ for (let l = 16; l < 64; l++) {
453
+ const d = H[l - 15], x = H[l - 2], y = m(d, 7) ^ m(d, 18) ^ d >>> 3, p = m(x, 17) ^ m(x, 19) ^ x >>> 10;
454
+ H[l] = p + H[l - 7] + y + H[l - 16] | 0;
455
+ }
456
+ let { A: n, B: s, C: c, D: a, E: i, F: o, G: u, H: h } = this;
457
+ for (let l = 0; l < 64; l++) {
458
+ const d = m(i, 6) ^ m(i, 11) ^ m(i, 25), x = h + d + We(i, o, u) + tt[l] + H[l] | 0, p = (m(n, 2) ^ m(n, 13) ^ m(n, 22)) + ze(n, s, c) | 0;
459
+ h = u, u = o, o = i, i = a + x | 0, a = c, c = s, s = n, n = x + p | 0;
460
+ }
461
+ n = n + this.A | 0, s = s + this.B | 0, c = c + this.C | 0, a = a + this.D | 0, i = i + this.E | 0, o = o + this.F | 0, u = u + this.G | 0, h = h + this.H | 0, this.set(n, s, c, a, i, o, u, h);
462
+ }
463
+ roundClean() {
464
+ N(H);
465
+ }
466
+ destroy() {
467
+ this.set(0, 0, 0, 0, 0, 0, 0, 0), N(this.buffer);
468
+ }
469
+ }
470
+ class nt extends rt {
471
+ constructor() {
472
+ super(32);
473
+ // We cannot use array here since array allows indexing by variable
474
+ // which means optimizer/compiler cannot use registers.
475
+ f(this, "A", B[0] | 0);
476
+ f(this, "B", B[1] | 0);
477
+ f(this, "C", B[2] | 0);
478
+ f(this, "D", B[3] | 0);
479
+ f(this, "E", B[4] | 0);
480
+ f(this, "F", B[5] | 0);
481
+ f(this, "G", B[6] | 0);
482
+ f(this, "H", B[7] | 0);
483
+ }
484
+ }
485
+ const we = je([
486
+ "0x428a2f98d728ae22",
487
+ "0x7137449123ef65cd",
488
+ "0xb5c0fbcfec4d3b2f",
489
+ "0xe9b5dba58189dbbc",
490
+ "0x3956c25bf348b538",
491
+ "0x59f111f1b605d019",
492
+ "0x923f82a4af194f9b",
493
+ "0xab1c5ed5da6d8118",
494
+ "0xd807aa98a3030242",
495
+ "0x12835b0145706fbe",
496
+ "0x243185be4ee4b28c",
497
+ "0x550c7dc3d5ffb4e2",
498
+ "0x72be5d74f27b896f",
499
+ "0x80deb1fe3b1696b1",
500
+ "0x9bdc06a725c71235",
501
+ "0xc19bf174cf692694",
502
+ "0xe49b69c19ef14ad2",
503
+ "0xefbe4786384f25e3",
504
+ "0x0fc19dc68b8cd5b5",
505
+ "0x240ca1cc77ac9c65",
506
+ "0x2de92c6f592b0275",
507
+ "0x4a7484aa6ea6e483",
508
+ "0x5cb0a9dcbd41fbd4",
509
+ "0x76f988da831153b5",
510
+ "0x983e5152ee66dfab",
511
+ "0xa831c66d2db43210",
512
+ "0xb00327c898fb213f",
513
+ "0xbf597fc7beef0ee4",
514
+ "0xc6e00bf33da88fc2",
515
+ "0xd5a79147930aa725",
516
+ "0x06ca6351e003826f",
517
+ "0x142929670a0e6e70",
518
+ "0x27b70a8546d22ffc",
519
+ "0x2e1b21385c26c926",
520
+ "0x4d2c6dfc5ac42aed",
521
+ "0x53380d139d95b3df",
522
+ "0x650a73548baf63de",
523
+ "0x766a0abb3c77b2a8",
524
+ "0x81c2c92e47edaee6",
525
+ "0x92722c851482353b",
526
+ "0xa2bfe8a14cf10364",
527
+ "0xa81a664bbc423001",
528
+ "0xc24b8b70d0f89791",
529
+ "0xc76c51a30654be30",
530
+ "0xd192e819d6ef5218",
531
+ "0xd69906245565a910",
532
+ "0xf40e35855771202a",
533
+ "0x106aa07032bbd1b8",
534
+ "0x19a4c116b8d2d0c8",
535
+ "0x1e376c085141ab53",
536
+ "0x2748774cdf8eeb99",
537
+ "0x34b0bcb5e19b48a8",
538
+ "0x391c0cb3c5c95a63",
539
+ "0x4ed8aa4ae3418acb",
540
+ "0x5b9cca4f7763e373",
541
+ "0x682e6ff3d6b2b8a3",
542
+ "0x748f82ee5defb2fc",
543
+ "0x78a5636f43172f60",
544
+ "0x84c87814a1f0ab72",
545
+ "0x8cc702081a6439ec",
546
+ "0x90befffa23631e28",
547
+ "0xa4506cebde82bde9",
548
+ "0xbef9a3f7b2c67915",
549
+ "0xc67178f2e372532b",
550
+ "0xca273eceea26619c",
551
+ "0xd186b8c721c0c207",
552
+ "0xeada7dd6cde0eb1e",
553
+ "0xf57d4f7fee6ed178",
554
+ "0x06f067aa72176fba",
555
+ "0x0a637dc5a2c898a6",
556
+ "0x113f9804bef90dae",
557
+ "0x1b710b35131c471b",
558
+ "0x28db77f523047d84",
559
+ "0x32caab7b40c72493",
560
+ "0x3c9ebe0a15c9bebc",
561
+ "0x431d67c49c100d4c",
562
+ "0x4cc5d4becb3e42b6",
563
+ "0x597f299cfc657e2a",
564
+ "0x5fcb6fab3ad6faec",
565
+ "0x6c44198c4a475817"
566
+ ].map((e) => BigInt(e))), st = we[0], ot = we[1], _ = /* @__PURE__ */ new Uint32Array(80), I = /* @__PURE__ */ new Uint32Array(80);
567
+ class it extends ye {
568
+ constructor(t) {
569
+ super(128, t, 16, !1);
570
+ }
571
+ // prettier-ignore
572
+ get() {
573
+ const { Ah: t, Al: r, Bh: n, Bl: s, Ch: c, Cl: a, Dh: i, Dl: o, Eh: u, El: h, Fh: l, Fl: d, Gh: x, Gl: y, Hh: p, Hl: P } = this;
574
+ return [t, r, n, s, c, a, i, o, u, h, l, d, x, y, p, P];
575
+ }
576
+ // prettier-ignore
577
+ set(t, r, n, s, c, a, i, o, u, h, l, d, x, y, p, P) {
578
+ this.Ah = t | 0, this.Al = r | 0, this.Bh = n | 0, this.Bl = s | 0, this.Ch = c | 0, this.Cl = a | 0, this.Dh = i | 0, this.Dl = o | 0, this.Eh = u | 0, this.El = h | 0, this.Fh = l | 0, this.Fl = d | 0, this.Gh = x | 0, this.Gl = y | 0, this.Hh = p | 0, this.Hl = P | 0;
579
+ }
580
+ process(t, r) {
581
+ for (let b = 0; b < 16; b++, r += 4)
582
+ _[b] = t.getUint32(r), I[b] = t.getUint32(r += 4);
583
+ for (let b = 16; b < 80; b++) {
584
+ const v = _[b - 15] | 0, k = I[b - 15] | 0, W = $(v, k, 1) ^ $(v, k, 8) ^ ce(v, k, 7), z = R(v, k, 1) ^ R(v, k, 8) ^ ue(v, k, 7), E = _[b - 2] | 0, T = I[b - 2] | 0, U = $(E, T, 19) ^ F(E, T, 61) ^ ce(E, T, 6), X = R(E, T, 19) ^ K(E, T, 61) ^ ue(E, T, 6), D = Qe(z, X, I[b - 7], I[b - 16]), j = Je(D, W, U, _[b - 7], _[b - 16]);
585
+ _[b] = j | 0, I[b] = D | 0;
586
+ }
587
+ let { Ah: n, Al: s, Bh: c, Bl: a, Ch: i, Cl: o, Dh: u, Dl: h, Eh: l, El: d, Fh: x, Fl: y, Gh: p, Gl: P, Hh: A, Hl: C } = this;
588
+ for (let b = 0; b < 80; b++) {
589
+ const v = $(l, d, 14) ^ $(l, d, 18) ^ F(l, d, 41), k = R(l, d, 14) ^ R(l, d, 18) ^ K(l, d, 41), W = l & x ^ ~l & p, z = d & y ^ ~d & P, E = Ze(C, k, z, ot[b], I[b]), T = et(E, A, v, W, st[b], _[b]), U = E | 0, X = $(n, s, 28) ^ F(n, s, 34) ^ F(n, s, 39), D = R(n, s, 28) ^ K(n, s, 34) ^ K(n, s, 39), j = n & c ^ n & i ^ c & i, ve = s & a ^ s & o ^ a & o;
590
+ A = p | 0, C = P | 0, p = x | 0, P = y | 0, x = l | 0, y = d | 0, { h: l, l: d } = S(u | 0, h | 0, T | 0, U | 0), u = i | 0, h = o | 0, i = c | 0, o = a | 0, c = n | 0, a = s | 0;
591
+ const re = Ye(U, D, ve);
592
+ n = qe(re, T, X, j), s = re | 0;
593
+ }
594
+ ({ h: n, l: s } = S(this.Ah | 0, this.Al | 0, n | 0, s | 0)), { h: c, l: a } = S(this.Bh | 0, this.Bl | 0, c | 0, a | 0), { h: i, l: o } = S(this.Ch | 0, this.Cl | 0, i | 0, o | 0), { h: u, l: h } = S(this.Dh | 0, this.Dl | 0, u | 0, h | 0), { h: l, l: d } = S(this.Eh | 0, this.El | 0, l | 0, d | 0), { h: x, l: y } = S(this.Fh | 0, this.Fl | 0, x | 0, y | 0), { h: p, l: P } = S(this.Gh | 0, this.Gl | 0, p | 0, P | 0), { h: A, l: C } = S(this.Hh | 0, this.Hl | 0, A | 0, C | 0), this.set(n, s, c, a, i, o, u, h, l, d, x, y, p, P, A, C);
595
+ }
596
+ roundClean() {
597
+ N(_, I);
598
+ }
599
+ destroy() {
600
+ N(this.buffer), this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
601
+ }
602
+ }
603
+ class at extends it {
604
+ constructor() {
605
+ super(64);
606
+ f(this, "Ah", w[0] | 0);
607
+ f(this, "Al", w[1] | 0);
608
+ f(this, "Bh", w[2] | 0);
609
+ f(this, "Bl", w[3] | 0);
610
+ f(this, "Ch", w[4] | 0);
611
+ f(this, "Cl", w[5] | 0);
612
+ f(this, "Dh", w[6] | 0);
613
+ f(this, "Dl", w[7] | 0);
614
+ f(this, "Eh", w[8] | 0);
615
+ f(this, "El", w[9] | 0);
616
+ f(this, "Fh", w[10] | 0);
617
+ f(this, "Fl", w[11] | 0);
618
+ f(this, "Gh", w[12] | 0);
619
+ f(this, "Gl", w[13] | 0);
620
+ f(this, "Hh", w[14] | 0);
621
+ f(this, "Hl", w[15] | 0);
622
+ }
623
+ }
624
+ const ct = /* @__PURE__ */ pe(
625
+ () => new nt(),
626
+ /* @__PURE__ */ ge(1)
627
+ ), dr = /* @__PURE__ */ pe(
628
+ () => new at(),
629
+ /* @__PURE__ */ ge(3)
630
+ ), le = 66;
631
+ function Pe(e) {
632
+ if (!e.startsWith("0x") && !e.startsWith("0X"))
633
+ throw new Error("Expected 0x-prefixed hex string");
634
+ const t = e.slice(2);
635
+ if (t.length % 2 !== 0)
636
+ throw new Error(`Hex string has odd length: ${t.length}`);
637
+ if (!/^[0-9a-fA-F]*$/.test(t))
638
+ throw new Error("Hex string contains non-hex characters");
639
+ const r = new Uint8Array(t.length / 2);
640
+ for (let n = 0; n < r.length; n++)
641
+ r[n] = parseInt(t.slice(n * 2, n * 2 + 2), 16);
642
+ return r;
643
+ }
644
+ function ut(e) {
645
+ return `0x${Array.from(e).map((t) => t.toString(16).padStart(2, "0")).join("")}`;
646
+ }
647
+ function Z(e, t) {
648
+ if (e.length !== le)
649
+ throw new Error(
650
+ `${t} must be exactly 32 bytes (${le} hex chars with 0x prefix), got ${e.length}`
651
+ );
652
+ }
653
+ function lt(e) {
654
+ Z(e, "Secret");
655
+ const t = Pe(e), r = ct(t);
656
+ return ut(r);
657
+ }
658
+ function ft(e, t) {
659
+ return Z(e, "Secret"), Z(t, "Hashlock"), Pe(t), lt(e).toLowerCase() === t.toLowerCase();
660
+ }
661
+ const ht = /^0x[0-9a-fA-F]{64}$/, dt = /^0x[0-9a-fA-F]{40}$/, bt = /^0x([0-9a-fA-F]{2})*$/;
662
+ function q(e, t) {
663
+ if (e.length !== 66)
664
+ throw new Error(
665
+ `${t} must be 32 bytes (66 hex chars with 0x prefix), got length ${e.length}`
666
+ );
667
+ if (!ht.test(e))
668
+ throw new Error(
669
+ `${t} must contain only hex characters after the 0x prefix`
670
+ );
671
+ }
672
+ function xt(e, t) {
673
+ if (!dt.test(e))
674
+ throw new Error(
675
+ `${t} must be a 20-byte 0x-prefixed hex address (42 chars)`
676
+ );
677
+ }
678
+ function pt(e, t) {
679
+ if (!bt.test(e))
680
+ throw new Error(
681
+ `${t} must be a 0x-prefixed hex string with an even number of hex chars`
682
+ );
683
+ }
684
+ async function br(e) {
685
+ const {
686
+ btcVaultRegistryAddress: t,
687
+ vaultId: r,
688
+ hashlock: n,
689
+ activationMetadata: s,
690
+ writeContract: c,
691
+ signal: a
692
+ } = e;
693
+ a == null || a.throwIfAborted(), xt(t, "btcVaultRegistryAddress"), q(r, "vaultId");
694
+ const i = $e(e.secret);
695
+ if (q(i, "secret"), n !== void 0 && (q(n, "hashlock"), !ft(i, n)))
696
+ throw new Error(
697
+ "Invalid secret: SHA256(secret) does not match the provided hashlock"
698
+ );
699
+ return pt(s, "activationMetadata"), c({
700
+ address: t,
701
+ abi: He,
702
+ functionName: "activateVaultWithSecret",
703
+ args: [r, i, s]
704
+ });
705
+ }
706
+ const gt = 1e4;
707
+ async function me(e) {
708
+ const {
709
+ statusReader: t,
710
+ peginTxid: r,
711
+ targetStatuses: n,
712
+ timeoutMs: s,
713
+ pollIntervalMs: c = gt,
714
+ signal: a
715
+ } = e, i = Date.now();
716
+ for (; ; ) {
717
+ if (a != null && a.aborted)
718
+ throw new Error(
719
+ `Polling aborted for pegin ${r.slice(0, 8)}… (target: ${[...n].join(", ")})`
720
+ );
721
+ if (Date.now() - i >= s)
722
+ throw new Error(
723
+ `Polling timeout after ${s}ms for pegin ${r.slice(0, 8)}… (target: ${[...n].join(", ")})`
724
+ );
725
+ try {
726
+ const u = (await t.getPeginStatus(
727
+ { pegin_txid: r },
728
+ a
729
+ )).status;
730
+ if (n.has(u))
731
+ return u;
732
+ if (_e.has(u) && !n.has(u))
733
+ throw new Error(
734
+ `Pegin ${r.slice(0, 8)}… reached terminal status "${u}" while waiting for ${[...n].join(", ")}`
735
+ );
736
+ } catch (o) {
737
+ if (!(o instanceof Ie && o.code === Ce.NOT_FOUND || o instanceof Error && o.message.includes("PegIn not found")))
738
+ throw o;
739
+ }
740
+ await new Promise((o, u) => {
741
+ const h = () => {
742
+ clearTimeout(l), u(
743
+ new Error(
744
+ `Polling aborted for pegin ${r.slice(0, 8)}… (target: ${[...n].join(", ")})`
745
+ )
746
+ );
747
+ }, l = setTimeout(() => {
748
+ a == null || a.removeEventListener("abort", h), o();
749
+ }, c);
750
+ a == null || a.addEventListener("abort", h, { once: !0 });
751
+ });
752
+ }
753
+ }
754
+ const yt = 300 * 1e3, wt = /* @__PURE__ */ new Set([
755
+ O.PENDING_DEPOSITOR_WOTS_PK,
756
+ ...be
757
+ ]);
758
+ async function xr(e) {
759
+ const {
760
+ statusReader: t,
761
+ wotsSubmitter: r,
762
+ peginTxid: n,
763
+ depositorPk: s,
764
+ wotsPublicKeys: c,
765
+ timeoutMs: a = yt,
766
+ signal: i
767
+ } = e;
768
+ i == null || i.throwIfAborted();
769
+ const o = await me({
770
+ statusReader: t,
771
+ peginTxid: n,
772
+ targetStatuses: wt,
773
+ timeoutMs: a,
774
+ signal: i
775
+ });
776
+ be.has(o) || (i == null || i.throwIfAborted(), await r.submitDepositorWotsKey(
777
+ {
778
+ pegin_txid: n,
779
+ depositor_pk: s,
780
+ wots_public_keys: c
781
+ },
782
+ i
783
+ ));
784
+ }
785
+ const Pt = 1;
786
+ function mt(e, t, r) {
787
+ const n = ee.fromBase64(e), s = n.data.getTransaction().toString("hex").toLowerCase(), c = g(t).toLowerCase();
788
+ if (s !== c)
789
+ throw new Error(
790
+ `PSBT integrity check failed for ${r}: unsigned transaction does not match tx_hex`
791
+ );
792
+ return n;
793
+ }
794
+ function Et(e) {
795
+ const t = ee.fromHex(e.toHex());
796
+ for (const r of t.data.inputs)
797
+ delete r.tapBip32Derivation, delete r.tapMerkleRoot;
798
+ return t;
799
+ }
800
+ function fe(e, t, r) {
801
+ if (!e)
802
+ throw new Error(`Missing ${r} PSBT`);
803
+ const n = mt(e, t, r);
804
+ return Et(n).toHex();
805
+ }
806
+ function Tt(e, t) {
807
+ const r = [], n = [], s = [], c = M(
808
+ t,
809
+ Pt
810
+ ), a = fe(
811
+ e.payout_psbt,
812
+ e.payout_tx.tx_hex,
813
+ "depositor payout"
814
+ );
815
+ r.push(a), n.push(c);
816
+ for (const i of e.challenger_presign_data) {
817
+ const o = g(i.challenger_pubkey), u = r.length, h = fe(
818
+ i.nopayout_psbt,
819
+ i.nopayout_tx.tx_hex,
820
+ `nopayout (challenger ${o})`
821
+ );
822
+ r.push(h), n.push(c), s.push({
823
+ challengerPubkey: o,
824
+ noPayoutIdx: u
825
+ });
826
+ }
827
+ return { psbtHexes: r, signOptions: n, challengerEntries: s };
828
+ }
829
+ function St(e, t, r) {
830
+ const n = G(
831
+ e[0],
832
+ r
833
+ ), s = {};
834
+ for (const c of t)
835
+ s[c.challengerPubkey] = {
836
+ nopayout_signature: G(
837
+ e[c.noPayoutIdx],
838
+ r
839
+ )
840
+ };
841
+ return {
842
+ payout_signatures: {
843
+ payout_signature: n
844
+ },
845
+ per_challenger: s
846
+ };
847
+ }
848
+ async function At(e, t, r) {
849
+ if (typeof e.signPsbts == "function")
850
+ return e.signPsbts(t, r);
851
+ const n = [];
852
+ for (let s = 0; s < t.length; s++)
853
+ n.push(await e.signPsbt(t[s], r == null ? void 0 : r[s]));
854
+ return n;
855
+ }
856
+ async function vt(e) {
857
+ const { depositorGraph: t, depositorBtcPubkey: r, btcWallet: n } = e, s = g(r), c = await n.getPublicKeyHex(), { psbtHexes: a, signOptions: i, challengerEntries: o } = Tt(t, c), u = await At(
858
+ n,
859
+ a,
860
+ i
861
+ );
862
+ if (u.length !== a.length)
863
+ throw new Error(
864
+ `Wallet returned ${u.length} signed PSBTs, expected ${a.length}`
865
+ );
866
+ return St(
867
+ u,
868
+ o,
869
+ s
870
+ );
871
+ }
872
+ const kt = 1200 * 1e3, Ee = /* @__PURE__ */ new Set([
873
+ O.PENDING_ACKS,
874
+ O.PENDING_ACTIVATION,
875
+ O.ACTIVATED
876
+ ]), Bt = /* @__PURE__ */ new Set([
877
+ O.PENDING_DEPOSITOR_SIGNATURES,
878
+ ...Ee
879
+ ]);
880
+ function Ht(e) {
881
+ return e.map((t) => ({
882
+ claimerPubkeyXOnly: J(t.claimer_pubkey),
883
+ payoutTxHex: t.payout_tx.tx_hex,
884
+ assertTxHex: t.assert_tx.tx_hex
885
+ }));
886
+ }
887
+ function _t(e) {
888
+ const { output: t } = Re.payments.p2tr({
889
+ internalPubkey: xe.from(e, "hex")
890
+ });
891
+ if (!t)
892
+ throw new Error("Failed to derive BIP-86 P2TR scriptPubKey");
893
+ return t.toString("hex");
894
+ }
895
+ function It(e, t) {
896
+ const r = g(e).toLowerCase(), n = g(
897
+ t.vaultProviderBtcPubkey
898
+ ).toLowerCase(), s = g(
899
+ t.depositorBtcPubkey
900
+ ).toLowerCase();
901
+ if (r === n || r === s)
902
+ return t.registeredPayoutScriptPubKey;
903
+ if (!t.vaultKeeperBtcPubkeys.some(
904
+ (i) => g(i).toLowerCase() === r
905
+ ))
906
+ throw new Error(
907
+ `Unknown claimer pubkey ${r}: not VP, depositor, or a registered vault keeper`
908
+ );
909
+ return `0x${_t(r)}`;
910
+ }
911
+ function he(e, t) {
912
+ return {
913
+ payoutTxHex: e.payoutTxHex,
914
+ peginTxHex: t.peginTxHex,
915
+ assertTxHex: e.assertTxHex,
916
+ vaultProviderBtcPubkey: t.vaultProviderBtcPubkey,
917
+ vaultKeeperBtcPubkeys: t.vaultKeeperBtcPubkeys,
918
+ universalChallengerBtcPubkeys: t.universalChallengerBtcPubkeys,
919
+ depositorBtcPubkey: t.depositorBtcPubkey,
920
+ timelockPegin: t.timelockPegin,
921
+ registeredPayoutScriptPubKey: It(
922
+ e.claimerPubkeyXOnly,
923
+ t
924
+ )
925
+ };
926
+ }
927
+ async function Ct(e, t, r, n) {
928
+ const s = new Ue({
929
+ network: t.network,
930
+ btcWallet: e
931
+ }), c = r.length;
932
+ n == null || n(0, c);
933
+ let a;
934
+ if (s.supportsBatchSigning())
935
+ a = (await s.signPayoutTransactionsBatch(
936
+ r.map((u) => he(u, t))
937
+ )).map((u) => u.payoutSignature);
938
+ else {
939
+ a = [];
940
+ for (let o = 0; o < r.length; o++) {
941
+ n == null || n(o, c);
942
+ const u = await s.signPayoutTransaction(
943
+ he(r[o], t)
944
+ );
945
+ a.push(u.signature);
946
+ }
947
+ }
948
+ const i = {};
949
+ for (let o = 0; o < r.length; o++)
950
+ i[r[o].claimerPubkeyXOnly] = {
951
+ payout_signature: a[o]
952
+ };
953
+ return n == null || n(c, c), i;
954
+ }
955
+ async function pr(e) {
956
+ const {
957
+ statusReader: t,
958
+ presignClient: r,
959
+ btcWallet: n,
960
+ peginTxid: s,
961
+ depositorPk: c,
962
+ signingContext: a,
963
+ timeoutMs: i = kt,
964
+ signal: o,
965
+ onProgress: u
966
+ } = e, h = await me({
967
+ statusReader: t,
968
+ peginTxid: s,
969
+ targetStatuses: Bt,
970
+ timeoutMs: i,
971
+ signal: o
972
+ });
973
+ if (Ee.has(h))
974
+ return;
975
+ o == null || o.throwIfAborted();
976
+ const l = await r.requestDepositorPresignTransactions(
977
+ {
978
+ pegin_txid: s,
979
+ depositor_pk: c
980
+ },
981
+ o
982
+ );
983
+ o == null || o.throwIfAborted();
984
+ const d = J(c), x = l.txs.filter(
985
+ (C) => J(C.claimer_pubkey) !== d
986
+ ), y = Ht(x), p = await Ct(
987
+ n,
988
+ a,
989
+ y,
990
+ u
991
+ );
992
+ o == null || o.throwIfAborted();
993
+ const P = await vt({
994
+ depositorGraph: l.depositor_graph,
995
+ depositorBtcPubkey: c,
996
+ btcWallet: n
997
+ });
998
+ o == null || o.throwIfAborted();
999
+ const A = { ...p };
1000
+ A[g(c)] = P.payout_signatures, await r.submitDepositorPresignatures(
1001
+ {
1002
+ pegin_txid: s,
1003
+ depositor_pk: c,
1004
+ signatures: A,
1005
+ depositor_claimer_presignatures: P
1006
+ },
1007
+ o
1008
+ );
1009
+ }
1010
+ function Lt(e) {
1011
+ return /^[0-9a-fA-F]{64}$/.test(e);
1012
+ }
1013
+ function gr(e) {
1014
+ const {
1015
+ amountSats: t,
1016
+ minDeposit: r,
1017
+ maxDeposit: n,
1018
+ btcBalance: s,
1019
+ estimatedFeeSats: c,
1020
+ depositorClaimValue: a
1021
+ } = e;
1022
+ return !(t <= 0n || t < r || n && n > 0n && t > n || c == null || a == null || t + c + a > s);
1023
+ }
1024
+ function yr(e, t, r) {
1025
+ return e <= 0n ? {
1026
+ valid: !1,
1027
+ error: "Deposit amount must be greater than zero"
1028
+ } : e < t ? {
1029
+ valid: !1,
1030
+ error: `Minimum deposit is ${L(t)} BTC`
1031
+ } : r && r > 0n && e > r ? {
1032
+ valid: !1,
1033
+ error: `Maximum deposit is ${L(r)} BTC`
1034
+ } : { valid: !0 };
1035
+ }
1036
+ function wr(e) {
1037
+ const { amount: t, effectiveRemaining: r } = e;
1038
+ return r === null ? { valid: !0 } : r === 0n ? {
1039
+ valid: !1,
1040
+ error: "Supply cap reached — deposits temporarily paused"
1041
+ } : t > r ? {
1042
+ valid: !1,
1043
+ error: `Vault size exceeds remaining capacity (${L(r)} BTC)`
1044
+ } : { valid: !0 };
1045
+ }
1046
+ function Pr(e, t) {
1047
+ if (!e || e.length === 0)
1048
+ return {
1049
+ valid: !1,
1050
+ error: "At least one vault provider must be selected"
1051
+ };
1052
+ const r = t.map(
1053
+ (s) => s.toLowerCase()
1054
+ );
1055
+ return e.filter(
1056
+ (s) => !r.includes(s.toLowerCase())
1057
+ ).length > 0 ? {
1058
+ valid: !1,
1059
+ error: "Invalid vault provider selected"
1060
+ } : { valid: !0 };
1061
+ }
1062
+ function $t(e, t, r) {
1063
+ if (!e || e.length === 0)
1064
+ return {
1065
+ valid: !1,
1066
+ error: "At least one vault amount required"
1067
+ };
1068
+ for (let n = 0; n < e.length; n++) {
1069
+ const s = e[n];
1070
+ if (s <= 0n)
1071
+ return {
1072
+ valid: !1,
1073
+ error: `Vault ${n + 1} amount must be positive`
1074
+ };
1075
+ if (t && s < t)
1076
+ return {
1077
+ valid: !1,
1078
+ error: `Vault ${n + 1} amount ${L(s)} BTC is below minimum deposit ${L(t)} BTC`
1079
+ };
1080
+ if (r && s > r)
1081
+ return {
1082
+ valid: !1,
1083
+ error: `Vault ${n + 1} amount ${L(s)} BTC exceeds maximum deposit ${L(r)} BTC`
1084
+ };
1085
+ }
1086
+ return { valid: !0 };
1087
+ }
1088
+ function Rt(e) {
1089
+ const t = g(e);
1090
+ return Lt(t) ? { valid: !0 } : {
1091
+ valid: !1,
1092
+ error: "Invalid pubkey format: must be 64 hex characters (32-byte x-only public key, no 0x prefix)"
1093
+ };
1094
+ }
1095
+ function Ot(e) {
1096
+ if (!e || e.length === 0)
1097
+ throw new Error(
1098
+ "No vault keepers available. The system requires at least one vault keeper to create a deposit."
1099
+ );
1100
+ }
1101
+ function Nt(e) {
1102
+ if (!e || e.length === 0)
1103
+ throw new Error(
1104
+ "No universal challengers available. The system requires at least one universal challenger to create a deposit."
1105
+ );
1106
+ }
1107
+ function Ut(e) {
1108
+ if (e.length === 0)
1109
+ throw new Error("No spendable UTXOs available");
1110
+ }
1111
+ function mr(e) {
1112
+ const {
1113
+ vaultAmounts: t,
1114
+ confirmedUTXOs: r,
1115
+ vaultProviderBtcPubkey: n,
1116
+ vaultKeeperBtcPubkeys: s,
1117
+ universalChallengerBtcPubkeys: c,
1118
+ minDeposit: a,
1119
+ maxDeposit: i,
1120
+ htlcSecretHexesLength: o,
1121
+ depositorSecretHashesLength: u
1122
+ } = e, h = t.length;
1123
+ if (o !== h)
1124
+ throw new Error(
1125
+ `htlcSecretHexes length (${o}) must match vaultAmounts length (${h})`
1126
+ );
1127
+ if (u !== h)
1128
+ throw new Error(
1129
+ `depositorSecretHashes length (${u}) must match vaultAmounts length (${h})`
1130
+ );
1131
+ const l = $t(
1132
+ t,
1133
+ a,
1134
+ i
1135
+ );
1136
+ if (!l.valid)
1137
+ throw new Error(l.error);
1138
+ const d = Rt(n);
1139
+ if (!d.valid)
1140
+ throw new Error(d.error);
1141
+ Ot(s), Nt(c), Ut(r);
1142
+ }
1143
+ var Te = /* @__PURE__ */ ((e) => (e.CLAIM_EVENT_RECEIVED = "ClaimEventReceived", e.CLAIM_BROADCAST = "ClaimBroadcast", e.ASSERT_BROADCAST = "AssertBroadcast", e.CHALLENGE_ASSERT_OBSERVED = "ChallengeAssertObserved", e.WRONGLY_CHALLENGED_BROADCAST = "WronglyChallengedBroadcast", e.PAYOUT_BROADCAST = "PayoutBroadcast", e.FAILED = "Failed", e))(Te || {});
1144
+ const Dt = /* @__PURE__ */ new Set([
1145
+ "PayoutBroadcast",
1146
+ "Failed"
1147
+ /* FAILED */
1148
+ ]);
1149
+ function Er(e) {
1150
+ return Object.values(Te).includes(
1151
+ e
1152
+ );
1153
+ }
1154
+ function Tr(e) {
1155
+ return !!e && Dt.has(e);
1156
+ }
1157
+ class Vt extends Error {
1158
+ constructor(r, n) {
1159
+ super(`Refund not yet mature (BIP68 not final): ${n.message}`);
1160
+ f(this, "vaultId");
1161
+ f(this, "cause");
1162
+ this.name = "BIP68NotMatureError", this.vaultId = r, this.cause = n;
1163
+ }
1164
+ }
1165
+ const Ft = /^0x[0-9a-fA-F]{64}$/, Kt = /^(?:0x)?(?:[0-9a-fA-F]{2})+$/, Se = /^(?:0x)?(?:[0-9a-fA-F]{64}|[0-9a-fA-F]{66})$/, Mt = 160, Gt = 1, de = 65535, Wt = /non-BIP68-final/i;
1166
+ function Ae(e, t) {
1167
+ if (e.length !== 66)
1168
+ throw new Error(
1169
+ `${t} must be 32 bytes (66 hex chars with 0x prefix), got length ${e.length}`
1170
+ );
1171
+ if (!Ft.test(e))
1172
+ throw new Error(
1173
+ `${t} must contain only hex characters after the 0x prefix`
1174
+ );
1175
+ }
1176
+ function Q(e, t) {
1177
+ if (!Number.isInteger(e) || e < 0)
1178
+ throw new Error(`${t} must be a non-negative integer, got ${e}`);
1179
+ }
1180
+ function zt(e) {
1181
+ if (Ae(e.hashlock, "hashlock"), !Number.isInteger(e.htlcVout) || e.htlcVout < 0 || e.htlcVout > de)
1182
+ throw new Error(
1183
+ `htlcVout must be an integer 0-${de}, got ${e.htlcVout}`
1184
+ );
1185
+ if (Q(e.offchainParamsVersion, "offchainParamsVersion"), Q(e.appVaultKeepersVersion, "appVaultKeepersVersion"), Q(
1186
+ e.universalChallengersVersion,
1187
+ "universalChallengersVersion"
1188
+ ), typeof e.unsignedPrePeginTxHex != "string" || e.unsignedPrePeginTxHex.length === 0)
1189
+ throw new Error("unsignedPrePeginTxHex must be a non-empty hex string");
1190
+ if (!Kt.test(e.unsignedPrePeginTxHex))
1191
+ throw new Error(
1192
+ "unsignedPrePeginTxHex must be a hex byte string (optional 0x prefix, even length)"
1193
+ );
1194
+ if (!e.depositorBtcPubkey || !Se.test(e.depositorBtcPubkey))
1195
+ throw new Error(
1196
+ "depositorBtcPubkey must be 32 or 33 bytes of hex (optional 0x prefix)"
1197
+ );
1198
+ if (typeof e.amount != "bigint" || e.amount <= 0n)
1199
+ throw new Error(`amount must be a positive bigint, got ${e.amount}`);
1200
+ }
1201
+ function Xt(e) {
1202
+ if (!e.vaultProviderPubkey || !Se.test(e.vaultProviderPubkey))
1203
+ throw new Error("vaultProviderPubkey must be 32 or 33 bytes of hex");
1204
+ if (e.vaultKeeperPubkeys.length === 0)
1205
+ throw new Error("vaultKeeperPubkeys must be non-empty");
1206
+ if (e.universalChallengerPubkeys.length === 0)
1207
+ throw new Error("universalChallengerPubkeys must be non-empty");
1208
+ if (!Number.isInteger(e.timelockRefund) || e.timelockRefund <= 0)
1209
+ throw new Error(
1210
+ `timelockRefund must be a positive integer, got ${e.timelockRefund}`
1211
+ );
1212
+ if (typeof e.feeRate != "bigint" || e.feeRate <= 0n)
1213
+ throw new Error(
1214
+ `protocol feeRate must be a positive bigint, got ${e.feeRate}`
1215
+ );
1216
+ if (!Number.isInteger(e.numLocalChallengers) || e.numLocalChallengers < 0)
1217
+ throw new Error("numLocalChallengers must be a non-negative integer");
1218
+ if (!Number.isInteger(e.councilQuorum) || !Number.isInteger(e.councilSize) || e.councilQuorum <= 0 || e.councilSize <= 0 || e.councilQuorum > e.councilSize)
1219
+ throw new Error(
1220
+ `councilQuorum (${e.councilQuorum}) must be in [1, councilSize=${e.councilSize}]`
1221
+ );
1222
+ }
1223
+ function jt(e) {
1224
+ const t = ee.fromHex(e);
1225
+ try {
1226
+ t.finalizeAllInputs();
1227
+ } catch (r) {
1228
+ const n = r instanceof Error ? r.message : String(r);
1229
+ if (!n.includes("already finalized"))
1230
+ throw new Error(`Failed to finalize refund PSBT: ${n}`);
1231
+ }
1232
+ return t.extractTransaction().toHex();
1233
+ }
1234
+ async function Sr(e) {
1235
+ const {
1236
+ vaultId: t,
1237
+ readVault: r,
1238
+ readPrePeginContext: n,
1239
+ feeRate: s,
1240
+ signPsbt: c,
1241
+ broadcastTx: a,
1242
+ signal: i
1243
+ } = e;
1244
+ i == null || i.throwIfAborted(), Ae(t, "vaultId");
1245
+ const o = await r();
1246
+ zt(o), i == null || i.throwIfAborted();
1247
+ const u = await n(o);
1248
+ if (Xt(u), i == null || i.throwIfAborted(), !Number.isFinite(s) || s <= 0)
1249
+ throw new Error(`feeRate must be a positive number, got ${s}`);
1250
+ const h = BigInt(Math.ceil(s * Mt));
1251
+ i == null || i.throwIfAborted();
1252
+ const { psbtHex: l } = await Ne({
1253
+ prePeginParams: {
1254
+ depositorPubkey: g(o.depositorBtcPubkey),
1255
+ vaultProviderPubkey: g(u.vaultProviderPubkey),
1256
+ vaultKeeperPubkeys: u.vaultKeeperPubkeys.map(g),
1257
+ universalChallengerPubkeys: u.universalChallengerPubkeys.map(g),
1258
+ hashlocks: [g(o.hashlock)],
1259
+ timelockRefund: u.timelockRefund,
1260
+ pegInAmounts: [o.amount],
1261
+ feeRate: u.feeRate,
1262
+ numLocalChallengers: u.numLocalChallengers,
1263
+ councilQuorum: u.councilQuorum,
1264
+ councilSize: u.councilSize,
1265
+ network: u.network
1266
+ },
1267
+ fundedPrePeginTxHex: g(o.unsignedPrePeginTxHex),
1268
+ htlcVout: o.htlcVout,
1269
+ refundFee: h,
1270
+ // buildRefundPsbt's top-level `hashlock` param is documented as "no 0x
1271
+ // prefix" and flows into the WASM HTLC connector derivation; a prefixed
1272
+ // value would derive the wrong refund script leaf and yield an
1273
+ // unspendable PSBT. Match the `hashlocks` array handling above.
1274
+ hashlock: g(o.hashlock)
1275
+ });
1276
+ i == null || i.throwIfAborted();
1277
+ const d = M(
1278
+ o.depositorBtcPubkey,
1279
+ Gt
1280
+ ), x = await c(l, d), y = jt(x);
1281
+ i == null || i.throwIfAborted();
1282
+ try {
1283
+ return await a(y);
1284
+ } catch (p) {
1285
+ throw p instanceof Error && Wt.test(p.message) ? new Vt(t, p) : p;
1286
+ }
1287
+ }
1288
+ export {
1289
+ ir as A,
1290
+ Vt as B,
1291
+ Te as C,
1292
+ Ve as D,
1293
+ ur as E,
1294
+ lr as F,
1295
+ fr as G,
1296
+ ye as H,
1297
+ hr as I,
1298
+ Y as J,
1299
+ cr as K,
1300
+ ar as L,
1301
+ dr as M,
1302
+ ct as N,
1303
+ Ue as P,
1304
+ br as a,
1305
+ vt as b,
1306
+ wr as c,
1307
+ Pr as d,
1308
+ $t as e,
1309
+ Rt as f,
1310
+ mr as g,
1311
+ lt as h,
1312
+ gr as i,
1313
+ ft as j,
1314
+ Er as k,
1315
+ Tr as l,
1316
+ Sr as m,
1317
+ nr as n,
1318
+ te as o,
1319
+ pr as p,
1320
+ N as q,
1321
+ ie as r,
1322
+ xr as s,
1323
+ pe as t,
1324
+ or as u,
1325
+ yr as v,
1326
+ me as w,
1327
+ je as x,
1328
+ oe as y,
1329
+ sr as z
1330
+ };
1331
+ //# sourceMappingURL=buildAndBroadcastRefund-DnTQkCgG.js.map