@babylonlabs-io/ts-sdk 0.36.0 → 0.36.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dist/PeginManager-BPXVXu8t.cjs +2 -0
  2. package/dist/{PeginManager-cPQuJTB9.cjs.map → PeginManager-BPXVXu8t.cjs.map} +1 -1
  3. package/dist/{PeginManager-DF1oinIQ.js → PeginManager-CB-dVkT2.js} +387 -369
  4. package/dist/{PeginManager-DF1oinIQ.js.map → PeginManager-CB-dVkT2.js.map} +1 -1
  5. package/dist/buildAndBroadcastRefund-C2VqXiOx.js +893 -0
  6. package/dist/buildAndBroadcastRefund-C2VqXiOx.js.map +1 -0
  7. package/dist/buildAndBroadcastRefund-CBIfcF47.cjs +2 -0
  8. package/dist/buildAndBroadcastRefund-CBIfcF47.cjs.map +1 -0
  9. package/dist/index.cjs +1 -1
  10. package/dist/index.js +74 -70
  11. package/dist/tbv/core/clients/eth/types.d.ts +1 -0
  12. package/dist/tbv/core/clients/eth/types.d.ts.map +1 -1
  13. package/dist/tbv/core/clients/eth/vault-registry-reader.d.ts +1 -0
  14. package/dist/tbv/core/clients/eth/vault-registry-reader.d.ts.map +1 -1
  15. package/dist/tbv/core/clients/index.cjs +1 -1
  16. package/dist/tbv/core/clients/index.js +1 -1
  17. package/dist/tbv/core/index.cjs +1 -1
  18. package/dist/tbv/core/index.js +67 -63
  19. package/dist/tbv/core/managers/PeginManager.d.ts +22 -4
  20. package/dist/tbv/core/managers/PeginManager.d.ts.map +1 -1
  21. package/dist/tbv/core/managers/index.cjs +1 -1
  22. package/dist/tbv/core/managers/index.js +1 -1
  23. package/dist/tbv/core/services/deposit/__tests__/validateOnChainParticipantKeys.test.d.ts +2 -0
  24. package/dist/tbv/core/services/deposit/__tests__/validateOnChainParticipantKeys.test.d.ts.map +1 -0
  25. package/dist/tbv/core/services/deposit/__tests__/verifyRegisteredVaultVersions.test.d.ts +2 -0
  26. package/dist/tbv/core/services/deposit/__tests__/verifyRegisteredVaultVersions.test.d.ts.map +1 -0
  27. package/dist/tbv/core/services/deposit/index.d.ts +8 -12
  28. package/dist/tbv/core/services/deposit/index.d.ts.map +1 -1
  29. package/dist/tbv/core/services/deposit/signDepositorGraph.d.ts.map +1 -1
  30. package/dist/tbv/core/services/deposit/validateOnChainParticipantKeys.d.ts +21 -0
  31. package/dist/tbv/core/services/deposit/validateOnChainParticipantKeys.d.ts.map +1 -0
  32. package/dist/tbv/core/services/deposit/verifyRegisteredVaultVersions.d.ts +15 -0
  33. package/dist/tbv/core/services/deposit/verifyRegisteredVaultVersions.d.ts.map +1 -0
  34. package/dist/tbv/core/services/index.cjs +1 -1
  35. package/dist/tbv/core/services/index.js +23 -19
  36. package/dist/tbv/core/vault-secrets/index.d.ts +1 -0
  37. package/dist/tbv/core/vault-secrets/index.d.ts.map +1 -1
  38. package/dist/tbv/index.cjs +1 -1
  39. package/dist/tbv/index.js +67 -63
  40. package/dist/tbv/integrations/aave/constants.d.ts +15 -2
  41. package/dist/tbv/integrations/aave/constants.d.ts.map +1 -1
  42. package/dist/tbv/integrations/aave/index.cjs +1 -1
  43. package/dist/tbv/integrations/aave/index.cjs.map +1 -1
  44. package/dist/tbv/integrations/aave/index.d.ts +1 -1
  45. package/dist/tbv/integrations/aave/index.d.ts.map +1 -1
  46. package/dist/tbv/integrations/aave/index.js +94 -93
  47. package/dist/tbv/integrations/aave/index.js.map +1 -1
  48. package/dist/{vault-registry-reader-Cufa-ZkW.cjs → vault-registry-reader-CLnhAUN4.cjs} +2 -2
  49. package/dist/{vault-registry-reader-Cufa-ZkW.cjs.map → vault-registry-reader-CLnhAUN4.cjs.map} +1 -1
  50. package/dist/{vault-registry-reader-Bz8uu0dh.js → vault-registry-reader-CrLodprY.js} +220 -190
  51. package/dist/{vault-registry-reader-Bz8uu0dh.js.map → vault-registry-reader-CrLodprY.js.map} +1 -1
  52. package/package.json +1 -1
  53. package/dist/PeginManager-cPQuJTB9.cjs +0 -2
  54. package/dist/buildAndBroadcastRefund-BQ0CaL6Q.cjs +0 -2
  55. package/dist/buildAndBroadcastRefund-BQ0CaL6Q.cjs.map +0 -1
  56. package/dist/buildAndBroadcastRefund-DzX11N9S.js +0 -785
  57. package/dist/buildAndBroadcastRefund-DzX11N9S.js.map +0 -1
@@ -1,785 +0,0 @@
1
- var M = Object.defineProperty;
2
- var U = (e, t, r) => t in e ? M(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
3
- var v = (e, t, r) => U(e, typeof t != "symbol" ? t + "" : t, r);
4
- import { B as F } from "./BTCVaultRegistry.abi-DbJ5lsFJ.js";
5
- import { e as X, v as z, s as c, u as R, p as m, f as h } from "./bitcoin-B0S8SHCX.js";
6
- import { v as G, P as W } from "./PayoutManager-s_uH8Uuj.js";
7
- import { c as Q, a as Y, R as q, d as $, D as b } from "./types-TiIjyo2b.js";
8
- import * as j from "bitcoinjs-lib";
9
- import { Transaction as y, Psbt as J } from "bitcoinjs-lib";
10
- import { Buffer as Z } from "buffer";
11
- import { c as ee, a as te, b as re } from "./noPayout-DBX6G96_.js";
12
- import { a as oe, b as ne, e as A } from "./payout-BNFMBXS6.js";
13
- import { c as _ } from "./signing-DaLvGwQe.js";
14
- const ae = /^0x[0-9a-fA-F]{64}$/, ie = /^0x[0-9a-fA-F]{40}$/, se = /^0x([0-9a-fA-F]{2})*$/;
15
- function T(e, t) {
16
- if (e.length !== 66)
17
- throw new Error(
18
- `${t} must be 32 bytes (66 hex chars with 0x prefix), got length ${e.length}`
19
- );
20
- if (!ae.test(e))
21
- throw new Error(
22
- `${t} must contain only hex characters after the 0x prefix`
23
- );
24
- }
25
- function ue(e, t) {
26
- if (!ie.test(e))
27
- throw new Error(
28
- `${t} must be a 20-byte 0x-prefixed hex address (42 chars)`
29
- );
30
- }
31
- function le(e, t) {
32
- if (!se.test(e))
33
- throw new Error(
34
- `${t} must be a 0x-prefixed hex string with an even number of hex chars`
35
- );
36
- }
37
- async function je(e) {
38
- const {
39
- btcVaultRegistryAddress: t,
40
- vaultId: r,
41
- hashlock: o,
42
- activationMetadata: i,
43
- writeContract: u,
44
- signal: n
45
- } = e;
46
- n == null || n.throwIfAborted(), ue(t, "btcVaultRegistryAddress"), T(r, "vaultId");
47
- const s = X(e.secret);
48
- if (T(s, "secret"), o !== void 0 && (T(o, "hashlock"), !G(s, o)))
49
- throw new Error(
50
- "Invalid secret: SHA256(secret) does not match the provided hashlock"
51
- );
52
- return le(i, "activationMetadata"), u({
53
- address: t,
54
- abi: F,
55
- functionName: "activateVaultWithSecret",
56
- args: [r, s, i]
57
- });
58
- }
59
- const ce = 1e4;
60
- async function N(e) {
61
- const {
62
- statusReader: t,
63
- peginTxid: r,
64
- targetStatuses: o,
65
- timeoutMs: i,
66
- pollIntervalMs: u = ce,
67
- signal: n
68
- } = e, s = Date.now();
69
- for (; ; ) {
70
- if (n != null && n.aborted)
71
- throw new Error(
72
- `Polling aborted for pegin ${r.slice(0, 8)}… (target: ${[...o].join(", ")})`
73
- );
74
- if (Date.now() - s >= i)
75
- throw new Error(
76
- `Polling timeout after ${i}ms for pegin ${r.slice(0, 8)}… (target: ${[...o].join(", ")})`
77
- );
78
- try {
79
- const a = await t.getPeginStatus(
80
- { pegin_txid: r },
81
- n
82
- );
83
- if (a.pegin_txid.toLowerCase() !== r.toLowerCase())
84
- throw new Error(
85
- `getPeginStatus returned status for pegin ${a.pegin_txid.slice(0, 8)}…, requested ${r.slice(0, 8)}…`
86
- );
87
- const l = a.status;
88
- if (o.has(l))
89
- return l;
90
- if (Q.has(l) && !o.has(l))
91
- throw new Error(
92
- `Pegin ${r.slice(0, 8)}… reached terminal status "${l}" while waiting for ${[...o].join(", ")}`
93
- );
94
- } catch (a) {
95
- if (!(a instanceof Y && a.code === q.NOT_FOUND || a instanceof Error && a.message.includes("PegIn not found")))
96
- throw a;
97
- }
98
- await new Promise((a, l) => {
99
- const f = () => {
100
- clearTimeout(d), l(
101
- new Error(
102
- `Polling aborted for pegin ${r.slice(0, 8)}… (target: ${[...o].join(", ")})`
103
- )
104
- );
105
- }, d = setTimeout(() => {
106
- n == null || n.removeEventListener("abort", f), a();
107
- }, u);
108
- n == null || n.addEventListener("abort", f, { once: !0 });
109
- });
110
- }
111
- }
112
- const de = 300 * 1e3, fe = /* @__PURE__ */ new Set([
113
- b.PENDING_DEPOSITOR_WOTS_PK,
114
- ...$
115
- ]);
116
- async function Je(e) {
117
- const {
118
- statusReader: t,
119
- wotsSubmitter: r,
120
- peginTxid: o,
121
- depositorPk: i,
122
- wotsPublicKeys: u,
123
- timeoutMs: n = de,
124
- signal: s
125
- } = e;
126
- s == null || s.throwIfAborted();
127
- const a = await N({
128
- statusReader: t,
129
- peginTxid: o,
130
- targetStatuses: fe,
131
- timeoutMs: n,
132
- signal: s
133
- });
134
- $.has(a) || (s == null || s.throwIfAborted(), await r.submitDepositorWotsKey(
135
- {
136
- pegin_txid: o,
137
- depositor_pk: i,
138
- wots_public_keys: u
139
- },
140
- s
141
- ));
142
- }
143
- const B = 1;
144
- function pe(e, t, r) {
145
- const o = c(r).toLowerCase(), u = [e, ...t].map(
146
- (n) => c(n).toLowerCase()
147
- ).filter((n) => n !== o);
148
- if (u.length === 0)
149
- throw new Error(
150
- "Cannot derive localChallengers: removing depositor from {vaultProvider, vaultKeepers} left an empty set"
151
- );
152
- return u;
153
- }
154
- function he(e, t) {
155
- const r = e.ins[t];
156
- return R(new Uint8Array(r.hash).slice().reverse());
157
- }
158
- function k(e, t, r, o, i) {
159
- const u = e.ins[t];
160
- if (u.index !== 0)
161
- throw new Error(
162
- `NoPayout (challenger ${i}) input ${t} expected to spend ${o} vout 0, got vout ${u.index}`
163
- );
164
- const n = r.getId(), s = he(e, t);
165
- if (s !== n)
166
- throw new Error(
167
- `NoPayout (challenger ${i}) input ${t} does not reference ${o} (expected txid ${n}, got ${s})`
168
- );
169
- }
170
- async function ge(e, t, r) {
171
- const o = [], i = [], u = [];
172
- oe(
173
- e.payout_tx.tx_hex,
174
- r.registeredPayoutScriptPubKey
175
- );
176
- const n = await ne({
177
- payoutTxHex: e.payout_tx.tx_hex,
178
- peginTxHex: r.peginTxHex,
179
- assertTxHex: e.assert_tx.tx_hex,
180
- depositorBtcPubkey: r.depositorBtcPubkey,
181
- vaultProviderBtcPubkey: r.vaultProviderBtcPubkey,
182
- vaultKeeperBtcPubkeys: r.vaultKeeperBtcPubkeys,
183
- universalChallengerBtcPubkeys: r.universalChallengerBtcPubkeys,
184
- timelockPegin: r.timelockPegin,
185
- network: r.network
186
- });
187
- o.push(n.psbtHex), i.push(
188
- _(
189
- t,
190
- B
191
- )
192
- );
193
- const s = pe(
194
- r.vaultProviderBtcPubkey,
195
- r.vaultKeeperBtcPubkeys,
196
- r.depositorBtcPubkey
197
- ), a = c(r.depositorBtcPubkey), l = y.fromHex(
198
- c(e.assert_tx.tx_hex)
199
- );
200
- for (const f of e.challenger_presign_data) {
201
- const d = c(f.challenger_pubkey), g = o.length, P = await Pe({
202
- challenger: f,
203
- challengerPubkey: d,
204
- claimerPubkey: a,
205
- localChallengers: s,
206
- assertTxParsed: l,
207
- ctx: r
208
- });
209
- o.push(P), i.push(
210
- _(
211
- t,
212
- B
213
- )
214
- ), u.push({
215
- challengerPubkey: d,
216
- noPayoutIdx: g
217
- });
218
- }
219
- return { psbtHexes: o, signOptions: i, challengerEntries: u };
220
- }
221
- async function Pe(e) {
222
- const {
223
- challenger: t,
224
- challengerPubkey: r,
225
- claimerPubkey: o,
226
- localChallengers: i,
227
- assertTxParsed: u,
228
- ctx: n
229
- } = e;
230
- ee(
231
- t.nopayout_tx.tx_hex,
232
- r,
233
- n.network
234
- );
235
- const s = y.fromHex(
236
- c(t.nopayout_tx.tx_hex)
237
- ), a = y.fromHex(
238
- c(t.challenge_assert_x_tx.tx_hex)
239
- ), l = y.fromHex(
240
- c(t.challenge_assert_y_tx.tx_hex)
241
- );
242
- if (s.ins.length !== 3)
243
- throw new Error(
244
- `NoPayout (challenger ${r}) must have exactly 3 inputs, got ${s.ins.length}`
245
- );
246
- k(
247
- s,
248
- 0,
249
- u,
250
- "Assert",
251
- r
252
- ), k(
253
- s,
254
- 1,
255
- a,
256
- "ChallengeAssertX",
257
- r
258
- ), k(
259
- s,
260
- 2,
261
- l,
262
- "ChallengeAssertY",
263
- r
264
- );
265
- const f = [
266
- u.outs[0],
267
- a.outs[0],
268
- l.outs[0]
269
- ].map((d) => ({
270
- script_pubkey: R(new Uint8Array(d.script)),
271
- value: d.value
272
- }));
273
- return te({
274
- noPayoutTxHex: t.nopayout_tx.tx_hex,
275
- challengerPubkey: r,
276
- prevouts: f,
277
- connectorParams: {
278
- claimer: o,
279
- localChallengers: i,
280
- universalChallengers: n.universalChallengerBtcPubkeys,
281
- timelockAssert: n.timelockAssert,
282
- councilMembers: n.councilMembers,
283
- councilQuorum: n.councilQuorum
284
- }
285
- });
286
- }
287
- function be(e, t, r) {
288
- const o = A(
289
- e[0],
290
- r
291
- ), i = {};
292
- for (const u of t)
293
- i[u.challengerPubkey] = {
294
- nopayout_signature: A(
295
- e[u.noPayoutIdx],
296
- r
297
- )
298
- };
299
- return {
300
- payout_signatures: {
301
- payout_signature: o
302
- },
303
- per_challenger: i
304
- };
305
- }
306
- async function ye(e, t, r) {
307
- if (typeof e.signPsbts == "function")
308
- return e.signPsbts(t, r);
309
- const o = [];
310
- for (let i = 0; i < t.length; i++)
311
- o.push(await e.signPsbt(t[i], r == null ? void 0 : r[i]));
312
- return o;
313
- }
314
- async function me(e) {
315
- const { depositorGraph: t, btcWallet: r, signingContext: o } = e, i = await r.getPublicKeyHex(), { depositorPubkey: u } = z(
316
- i,
317
- c(o.depositorBtcPubkey)
318
- ), { psbtHexes: n, signOptions: s, challengerEntries: a } = await ge(
319
- t,
320
- i,
321
- o
322
- ), l = await ye(
323
- r,
324
- n,
325
- s
326
- );
327
- if (l.length !== n.length)
328
- throw new Error(
329
- `Wallet returned ${l.length} signed PSBTs, expected ${n.length}`
330
- );
331
- return be(
332
- l,
333
- a,
334
- u
335
- );
336
- }
337
- const we = 1200 * 1e3, H = /* @__PURE__ */ new Set([
338
- b.PENDING_ACKS,
339
- b.PENDING_ACTIVATION,
340
- b.ACTIVATED
341
- ]), xe = /* @__PURE__ */ new Set([
342
- b.PENDING_DEPOSITOR_SIGNATURES,
343
- ...H
344
- ]);
345
- function ve(e) {
346
- return e.map((t) => ({
347
- claimerPubkeyXOnly: m(t.claimer_pubkey),
348
- payoutTxHex: t.payout_tx.tx_hex,
349
- assertTxHex: t.assert_tx.tx_hex
350
- }));
351
- }
352
- function Te(e) {
353
- const { output: t } = j.payments.p2tr({
354
- internalPubkey: Z.from(e, "hex")
355
- });
356
- if (!t)
357
- throw new Error("Failed to derive BIP-86 P2TR scriptPubKey");
358
- return t.toString("hex");
359
- }
360
- function ke(e, t) {
361
- const r = c(e).toLowerCase(), o = c(
362
- t.vaultProviderBtcPubkey
363
- ).toLowerCase(), i = c(
364
- t.depositorBtcPubkey
365
- ).toLowerCase();
366
- if (r === o || r === i)
367
- return t.registeredPayoutScriptPubKey;
368
- if (!t.vaultKeeperBtcPubkeys.some(
369
- (s) => c(s).toLowerCase() === r
370
- ))
371
- throw new Error(
372
- `Unknown claimer pubkey ${r}: not VP, depositor, or a registered vault keeper`
373
- );
374
- return `0x${Te(r)}`;
375
- }
376
- function C(e, t) {
377
- return {
378
- payoutTxHex: e.payoutTxHex,
379
- peginTxHex: t.peginTxHex,
380
- assertTxHex: e.assertTxHex,
381
- vaultProviderBtcPubkey: t.vaultProviderBtcPubkey,
382
- vaultKeeperBtcPubkeys: t.vaultKeeperBtcPubkeys,
383
- universalChallengerBtcPubkeys: t.universalChallengerBtcPubkeys,
384
- depositorBtcPubkey: t.depositorBtcPubkey,
385
- timelockPegin: t.timelockPegin,
386
- registeredPayoutScriptPubKey: ke(
387
- e.claimerPubkeyXOnly,
388
- t
389
- )
390
- };
391
- }
392
- async function Ee(e, t, r, o) {
393
- const i = new W({
394
- network: t.network,
395
- btcWallet: e
396
- }), u = r.length;
397
- o == null || o(0, u);
398
- let n;
399
- if (i.supportsBatchSigning())
400
- n = (await i.signPayoutTransactionsBatch(
401
- r.map((l) => C(l, t))
402
- )).map((l) => l.payoutSignature);
403
- else {
404
- n = [];
405
- for (let a = 0; a < r.length; a++) {
406
- o == null || o(a, u);
407
- const l = await i.signPayoutTransaction(
408
- C(r[a], t)
409
- );
410
- n.push(l.signature);
411
- }
412
- }
413
- const s = {};
414
- for (let a = 0; a < r.length; a++)
415
- s[r[a].claimerPubkeyXOnly] = {
416
- payout_signature: n[a]
417
- };
418
- return o == null || o(u, u), s;
419
- }
420
- async function Ze(e) {
421
- const {
422
- statusReader: t,
423
- presignClient: r,
424
- btcWallet: o,
425
- peginTxid: i,
426
- depositorPk: u,
427
- signingContext: n,
428
- timeoutMs: s = we,
429
- signal: a,
430
- onProgress: l
431
- } = e, f = await N({
432
- statusReader: t,
433
- peginTxid: i,
434
- targetStatuses: xe,
435
- timeoutMs: s,
436
- signal: a
437
- });
438
- if (H.has(f))
439
- return;
440
- a == null || a.throwIfAborted();
441
- const d = await r.requestDepositorPresignTransactions(
442
- {
443
- pegin_txid: i,
444
- depositor_pk: u
445
- },
446
- a
447
- );
448
- a == null || a.throwIfAborted();
449
- const g = m(u), P = d.txs.filter(
450
- (K) => m(K.claimer_pubkey) !== g
451
- ), w = ve(P), x = await Ee(
452
- o,
453
- n,
454
- w,
455
- l
456
- );
457
- a == null || a.throwIfAborted();
458
- const p = await me({
459
- depositorGraph: d.depositor_graph,
460
- btcWallet: o,
461
- signingContext: {
462
- peginTxHex: n.peginTxHex,
463
- depositorBtcPubkey: u,
464
- vaultProviderBtcPubkey: n.vaultProviderBtcPubkey,
465
- vaultKeeperBtcPubkeys: n.vaultKeeperBtcPubkeys,
466
- universalChallengerBtcPubkeys: n.universalChallengerBtcPubkeys,
467
- timelockPegin: n.timelockPegin,
468
- timelockAssert: n.timelockAssert,
469
- councilMembers: n.councilMembers,
470
- councilQuorum: n.councilQuorum,
471
- network: n.network,
472
- registeredPayoutScriptPubKey: n.registeredPayoutScriptPubKey
473
- }
474
- });
475
- a == null || a.throwIfAborted();
476
- const S = { ...x };
477
- S[c(u)] = p.payout_signatures, await r.submitDepositorPresignatures(
478
- {
479
- pegin_txid: i,
480
- depositor_pk: u,
481
- signatures: S,
482
- depositor_claimer_presignatures: p
483
- },
484
- a
485
- );
486
- }
487
- function _e(e) {
488
- return /^[0-9a-fA-F]{64}$/.test(e);
489
- }
490
- function et(e) {
491
- const {
492
- amountSats: t,
493
- minDeposit: r,
494
- maxDeposit: o,
495
- btcBalance: i,
496
- estimatedFeeSats: u,
497
- depositorClaimValue: n
498
- } = e;
499
- return !(t <= 0n || t < r || o && o > 0n && t > o || u == null || n == null || t + u + n > i);
500
- }
501
- function tt(e, t, r) {
502
- return e <= 0n ? {
503
- valid: !1,
504
- error: "Deposit amount must be greater than zero"
505
- } : e < t ? {
506
- valid: !1,
507
- error: `Minimum deposit is ${h(t)} BTC`
508
- } : r && r > 0n && e > r ? {
509
- valid: !1,
510
- error: `Maximum deposit is ${h(r)} BTC`
511
- } : { valid: !0 };
512
- }
513
- function rt(e) {
514
- const { amount: t, effectiveRemaining: r } = e;
515
- return r === null ? { valid: !0 } : r === 0n ? {
516
- valid: !1,
517
- error: "Supply cap reached — deposits temporarily paused"
518
- } : t > r ? {
519
- valid: !1,
520
- error: `Vault size exceeds remaining capacity (${h(r)} BTC)`
521
- } : { valid: !0 };
522
- }
523
- function ot(e, t) {
524
- if (!e || e.length === 0)
525
- return {
526
- valid: !1,
527
- error: "At least one vault provider must be selected"
528
- };
529
- const r = t.map(
530
- (i) => i.toLowerCase()
531
- );
532
- return e.filter(
533
- (i) => !r.includes(i.toLowerCase())
534
- ).length > 0 ? {
535
- valid: !1,
536
- error: "Invalid vault provider selected"
537
- } : { valid: !0 };
538
- }
539
- function Se(e, t, r) {
540
- if (!e || e.length === 0)
541
- return {
542
- valid: !1,
543
- error: "At least one vault amount required"
544
- };
545
- for (let o = 0; o < e.length; o++) {
546
- const i = e[o];
547
- if (i <= 0n)
548
- return {
549
- valid: !1,
550
- error: `Vault ${o + 1} amount must be positive`
551
- };
552
- if (t && i < t)
553
- return {
554
- valid: !1,
555
- error: `Vault ${o + 1} amount ${h(i)} BTC is below minimum deposit ${h(t)} BTC`
556
- };
557
- if (r && i > r)
558
- return {
559
- valid: !1,
560
- error: `Vault ${o + 1} amount ${h(i)} BTC exceeds maximum deposit ${h(r)} BTC`
561
- };
562
- }
563
- return { valid: !0 };
564
- }
565
- function Ae(e) {
566
- const t = c(e);
567
- return _e(t) ? { valid: !0 } : {
568
- valid: !1,
569
- error: "Invalid pubkey format: must be 64 hex characters (32-byte x-only public key, no 0x prefix)"
570
- };
571
- }
572
- function Be(e) {
573
- if (!e || e.length === 0)
574
- throw new Error(
575
- "No vault keepers available. The system requires at least one vault keeper to create a deposit."
576
- );
577
- }
578
- function Ce(e) {
579
- if (!e || e.length === 0)
580
- throw new Error(
581
- "No universal challengers available. The system requires at least one universal challenger to create a deposit."
582
- );
583
- }
584
- function Ie(e) {
585
- if (e.length === 0)
586
- throw new Error("No spendable UTXOs available");
587
- }
588
- function nt(e) {
589
- const {
590
- vaultAmounts: t,
591
- confirmedUTXOs: r,
592
- vaultProviderBtcPubkey: o,
593
- vaultKeeperBtcPubkeys: i,
594
- universalChallengerBtcPubkeys: u,
595
- minDeposit: n,
596
- maxDeposit: s
597
- } = e, a = Se(
598
- t,
599
- n,
600
- s
601
- );
602
- if (!a.valid)
603
- throw new Error(a.error);
604
- const l = Ae(o);
605
- if (!l.valid)
606
- throw new Error(l.error);
607
- Be(i), Ce(u), Ie(r);
608
- }
609
- var O = /* @__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))(O || {});
610
- const Re = /* @__PURE__ */ new Set([
611
- "PayoutBroadcast",
612
- "Failed"
613
- /* FAILED */
614
- ]);
615
- function at(e) {
616
- return Object.values(O).includes(
617
- e
618
- );
619
- }
620
- function it(e) {
621
- return !!e && Re.has(e);
622
- }
623
- class $e extends Error {
624
- constructor(r, o) {
625
- super(`Refund not yet mature (BIP68 not final): ${o.message}`);
626
- v(this, "vaultId");
627
- v(this, "cause");
628
- this.name = "BIP68NotMatureError", this.vaultId = r, this.cause = o;
629
- }
630
- }
631
- const Ne = /^0x[0-9a-fA-F]{64}$/, He = /^(?:0x)?(?:[0-9a-fA-F]{2})+$/, V = /^(?:0x)?(?:[0-9a-fA-F]{64}|[0-9a-fA-F]{66})$/, D = 160;
632
- function st(e) {
633
- if (!Number.isFinite(e) || e <= 0)
634
- throw new Error(
635
- `feeRateSatsVb must be a positive finite number, got ${e}`
636
- );
637
- return BigInt(Math.ceil(e * D));
638
- }
639
- const Oe = 1, I = 65535, Ve = /non-BIP68-final/i;
640
- function L(e, t) {
641
- if (e.length !== 66)
642
- throw new Error(
643
- `${t} must be 32 bytes (66 hex chars with 0x prefix), got length ${e.length}`
644
- );
645
- if (!Ne.test(e))
646
- throw new Error(
647
- `${t} must contain only hex characters after the 0x prefix`
648
- );
649
- }
650
- function E(e, t) {
651
- if (!Number.isInteger(e) || e < 0)
652
- throw new Error(`${t} must be a non-negative integer, got ${e}`);
653
- }
654
- function De(e) {
655
- if (L(e.hashlock, "hashlock"), !Number.isInteger(e.htlcVout) || e.htlcVout < 0 || e.htlcVout > I)
656
- throw new Error(
657
- `htlcVout must be an integer 0-${I}, got ${e.htlcVout}`
658
- );
659
- if (E(e.offchainParamsVersion, "offchainParamsVersion"), E(e.appVaultKeepersVersion, "appVaultKeepersVersion"), E(
660
- e.universalChallengersVersion,
661
- "universalChallengersVersion"
662
- ), typeof e.unsignedPrePeginTxHex != "string" || e.unsignedPrePeginTxHex.length === 0)
663
- throw new Error("unsignedPrePeginTxHex must be a non-empty hex string");
664
- if (!He.test(e.unsignedPrePeginTxHex))
665
- throw new Error(
666
- "unsignedPrePeginTxHex must be a hex byte string (optional 0x prefix, even length)"
667
- );
668
- if (!e.depositorBtcPubkey || !V.test(e.depositorBtcPubkey))
669
- throw new Error(
670
- "depositorBtcPubkey must be 32 or 33 bytes of hex (optional 0x prefix)"
671
- );
672
- if (typeof e.amount != "bigint" || e.amount <= 0n)
673
- throw new Error(`amount must be a positive bigint, got ${e.amount}`);
674
- }
675
- function Le(e) {
676
- if (!e.vaultProviderPubkey || !V.test(e.vaultProviderPubkey))
677
- throw new Error("vaultProviderPubkey must be 32 or 33 bytes of hex");
678
- if (e.vaultKeeperPubkeys.length === 0)
679
- throw new Error("vaultKeeperPubkeys must be non-empty");
680
- if (e.universalChallengerPubkeys.length === 0)
681
- throw new Error("universalChallengerPubkeys must be non-empty");
682
- if (!Number.isInteger(e.timelockRefund) || e.timelockRefund <= 0)
683
- throw new Error(
684
- `timelockRefund must be a positive integer, got ${e.timelockRefund}`
685
- );
686
- if (typeof e.feeRate != "bigint" || e.feeRate <= 0n)
687
- throw new Error(
688
- `protocol feeRate must be a positive bigint, got ${e.feeRate}`
689
- );
690
- if (!Number.isInteger(e.numLocalChallengers) || e.numLocalChallengers < 0)
691
- throw new Error("numLocalChallengers must be a non-negative integer");
692
- if (!Number.isInteger(e.councilQuorum) || !Number.isInteger(e.councilSize) || e.councilQuorum <= 0 || e.councilSize <= 0 || e.councilQuorum > e.councilSize)
693
- throw new Error(
694
- `councilQuorum (${e.councilQuorum}) must be in [1, councilSize=${e.councilSize}]`
695
- );
696
- }
697
- function Ke(e) {
698
- const t = J.fromHex(e);
699
- try {
700
- t.finalizeAllInputs();
701
- } catch (r) {
702
- const o = r instanceof Error ? r.message : String(r);
703
- if (!o.includes("already finalized"))
704
- throw new Error(`Failed to finalize refund PSBT: ${o}`);
705
- }
706
- return t.extractTransaction().toHex();
707
- }
708
- async function ut(e) {
709
- const {
710
- vaultId: t,
711
- readVault: r,
712
- readPrePeginContext: o,
713
- feeRate: i,
714
- signPsbt: u,
715
- broadcastTx: n,
716
- signal: s
717
- } = e;
718
- s == null || s.throwIfAborted(), L(t, "vaultId");
719
- const a = await r();
720
- De(a), s == null || s.throwIfAborted();
721
- const l = await o(a);
722
- if (Le(l), s == null || s.throwIfAborted(), !Number.isFinite(i) || i <= 0)
723
- throw new Error(`feeRate must be a positive number, got ${i}`);
724
- const f = BigInt(Math.ceil(i * D));
725
- s == null || s.throwIfAborted();
726
- const d = m(
727
- a.depositorBtcPubkey
728
- ), { psbtHex: g } = await re({
729
- prePeginParams: {
730
- depositorPubkey: d,
731
- vaultProviderPubkey: c(l.vaultProviderPubkey),
732
- vaultKeeperPubkeys: l.vaultKeeperPubkeys.map(c),
733
- universalChallengerPubkeys: l.universalChallengerPubkeys.map(c),
734
- hashlocks: [c(a.hashlock)],
735
- timelockRefund: l.timelockRefund,
736
- pegInAmounts: [a.amount],
737
- feeRate: l.feeRate,
738
- numLocalChallengers: l.numLocalChallengers,
739
- councilQuorum: l.councilQuorum,
740
- councilSize: l.councilSize,
741
- network: l.network
742
- },
743
- fundedPrePeginTxHex: c(a.unsignedPrePeginTxHex),
744
- htlcVout: a.htlcVout,
745
- refundFee: f,
746
- // buildRefundPsbt's top-level `hashlock` param is documented as "no 0x
747
- // prefix" and flows into the WASM HTLC connector derivation; a prefixed
748
- // value would derive the wrong refund script leaf and yield an
749
- // unspendable PSBT. Match the `hashlocks` array handling above.
750
- hashlock: c(a.hashlock)
751
- });
752
- s == null || s.throwIfAborted();
753
- const P = _(
754
- a.depositorBtcPubkey,
755
- Oe
756
- ), w = await u(g, P), x = Ke(w);
757
- s == null || s.throwIfAborted();
758
- try {
759
- return await n(x);
760
- } catch (p) {
761
- throw p instanceof Error && Ve.test(p.message) ? new $e(t, p) : p;
762
- }
763
- }
764
- export {
765
- $e as B,
766
- O as C,
767
- D as R,
768
- je as a,
769
- me as b,
770
- rt as c,
771
- ot as d,
772
- Se as e,
773
- Ae as f,
774
- nt as g,
775
- at as h,
776
- et as i,
777
- it as j,
778
- ut as k,
779
- st as l,
780
- Ze as r,
781
- Je as s,
782
- tt as v,
783
- N as w
784
- };
785
- //# sourceMappingURL=buildAndBroadcastRefund-DzX11N9S.js.map