@babylonlabs-io/ts-sdk 0.33.3 → 0.33.5

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 (153) hide show
  1. package/dist/BTCVaultRegistry.abi-DbJ5lsFJ.js +417 -0
  2. package/dist/BTCVaultRegistry.abi-DbJ5lsFJ.js.map +1 -0
  3. package/dist/BTCVaultRegistry.abi-ZdPpION2.cjs +2 -0
  4. package/dist/BTCVaultRegistry.abi-ZdPpION2.cjs.map +1 -0
  5. package/dist/PayoutManager-C9XHKZ5j.js +200 -0
  6. package/dist/PayoutManager-C9XHKZ5j.js.map +1 -0
  7. package/dist/PayoutManager-DDnNB0mj.cjs +2 -0
  8. package/dist/PayoutManager-DDnNB0mj.cjs.map +1 -0
  9. package/dist/PeginManager-BXg2S1mH.cjs +2 -0
  10. package/dist/PeginManager-BXg2S1mH.cjs.map +1 -0
  11. package/dist/{PeginManager-C-L3huRO.js → PeginManager-C5HaMAcw.js} +328 -437
  12. package/dist/PeginManager-C5HaMAcw.js.map +1 -0
  13. package/dist/ProtocolParams.abi-BmvHwQJV.cjs +2 -0
  14. package/dist/ProtocolParams.abi-BmvHwQJV.cjs.map +1 -0
  15. package/dist/ProtocolParams.abi-DXu8L0Fn.js +382 -0
  16. package/dist/ProtocolParams.abi-DXu8L0Fn.js.map +1 -0
  17. package/dist/buildAndBroadcastRefund-B332dykQ.cjs +2 -0
  18. package/dist/buildAndBroadcastRefund-B332dykQ.cjs.map +1 -0
  19. package/dist/buildAndBroadcastRefund-CPjXNaMi.js +782 -0
  20. package/dist/buildAndBroadcastRefund-CPjXNaMi.js.map +1 -0
  21. package/dist/challengeAssert-BzxQmdZy.js +128 -0
  22. package/dist/challengeAssert-BzxQmdZy.js.map +1 -0
  23. package/dist/challengeAssert-Yyyj-EdR.cjs +2 -0
  24. package/dist/challengeAssert-Yyyj-EdR.cjs.map +1 -0
  25. package/dist/errors-9AkghWyk.js +22 -0
  26. package/dist/errors-9AkghWyk.js.map +1 -0
  27. package/dist/errors-BP73_stm.cjs +2 -0
  28. package/dist/errors-BP73_stm.cjs.map +1 -0
  29. package/dist/errors-Bu0H-dZD.cjs +2 -0
  30. package/dist/errors-Bu0H-dZD.cjs.map +1 -0
  31. package/dist/errors-CznAK5NB.js +96 -0
  32. package/dist/errors-CznAK5NB.js.map +1 -0
  33. package/dist/index.cjs +1 -1
  34. package/dist/index.js +159 -147
  35. package/dist/index.js.map +1 -1
  36. package/dist/mempoolApi-CAIge7Nj.js +205 -0
  37. package/dist/mempoolApi-CAIge7Nj.js.map +1 -0
  38. package/dist/mempoolApi-YNkKjQCU.cjs +2 -0
  39. package/dist/mempoolApi-YNkKjQCU.cjs.map +1 -0
  40. package/dist/noPayout-BXeUw0Qq.cjs +2 -0
  41. package/dist/noPayout-BXeUw0Qq.cjs.map +1 -0
  42. package/dist/noPayout-DBX6G96_.js +141 -0
  43. package/dist/noPayout-DBX6G96_.js.map +1 -0
  44. package/dist/payout-BNFMBXS6.js +193 -0
  45. package/dist/payout-BNFMBXS6.js.map +1 -0
  46. package/dist/payout-DQ_fmJUA.cjs +2 -0
  47. package/dist/payout-DQ_fmJUA.cjs.map +1 -0
  48. package/dist/peginInput-C2QPvuhR.js +177 -0
  49. package/dist/peginInput-C2QPvuhR.js.map +1 -0
  50. package/dist/peginInput-tbw9BpZy.cjs +2 -0
  51. package/dist/peginInput-tbw9BpZy.cjs.map +1 -0
  52. package/dist/peginState-BijNNT15.cjs +2 -0
  53. package/dist/peginState-BijNNT15.cjs.map +1 -0
  54. package/dist/peginState-CBAlxgXk.js +51 -0
  55. package/dist/peginState-CBAlxgXk.js.map +1 -0
  56. package/dist/psbtInputFields-2224j2ZY.js +128 -0
  57. package/dist/psbtInputFields-2224j2ZY.js.map +1 -0
  58. package/dist/psbtInputFields-B1lrwYzH.cjs +2 -0
  59. package/dist/psbtInputFields-B1lrwYzH.cjs.map +1 -0
  60. package/dist/reservation-CHUGW0F_.js +142 -0
  61. package/dist/reservation-CHUGW0F_.js.map +1 -0
  62. package/dist/reservation-ho7mjW3X.cjs +2 -0
  63. package/dist/reservation-ho7mjW3X.cjs.map +1 -0
  64. package/dist/signing-Bnsro0hE.cjs +2 -0
  65. package/dist/signing-Bnsro0hE.cjs.map +1 -0
  66. package/dist/signing-DaLvGwQe.js +16 -0
  67. package/dist/signing-DaLvGwQe.js.map +1 -0
  68. package/dist/tbv/core/clients/index.cjs +1 -1
  69. package/dist/tbv/core/clients/index.js +35 -32
  70. package/dist/tbv/core/clients/index.js.map +1 -1
  71. package/dist/tbv/core/clients/vault-provider/__tests__/batchAttribution.test.d.ts +2 -0
  72. package/dist/tbv/core/clients/vault-provider/__tests__/batchAttribution.test.d.ts.map +1 -0
  73. package/dist/tbv/core/clients/vault-provider/__tests__/batchPoll.test.d.ts +2 -0
  74. package/dist/tbv/core/clients/vault-provider/__tests__/batchPoll.test.d.ts.map +1 -0
  75. package/dist/tbv/core/clients/vault-provider/api.d.ts +13 -4
  76. package/dist/tbv/core/clients/vault-provider/api.d.ts.map +1 -1
  77. package/dist/tbv/core/clients/vault-provider/batchAttribution.d.ts +45 -0
  78. package/dist/tbv/core/clients/vault-provider/batchAttribution.d.ts.map +1 -0
  79. package/dist/tbv/core/clients/vault-provider/batchPoll.d.ts +55 -0
  80. package/dist/tbv/core/clients/vault-provider/batchPoll.d.ts.map +1 -0
  81. package/dist/tbv/core/clients/vault-provider/index.d.ts +2 -0
  82. package/dist/tbv/core/clients/vault-provider/index.d.ts.map +1 -1
  83. package/dist/tbv/core/clients/vault-provider/json-rpc-client.d.ts +3 -3
  84. package/dist/tbv/core/clients/vault-provider/json-rpc-client.d.ts.map +1 -1
  85. package/dist/tbv/core/clients/vault-provider/types.d.ts +68 -24
  86. package/dist/tbv/core/clients/vault-provider/types.d.ts.map +1 -1
  87. package/dist/tbv/core/clients/vault-provider/validators.d.ts +12 -2
  88. package/dist/tbv/core/clients/vault-provider/validators.d.ts.map +1 -1
  89. package/dist/tbv/core/contracts/index.cjs +2 -0
  90. package/dist/tbv/core/contracts/index.cjs.map +1 -0
  91. package/dist/tbv/core/contracts/index.js +14 -0
  92. package/dist/tbv/core/contracts/index.js.map +1 -0
  93. package/dist/tbv/core/index.cjs +1 -1
  94. package/dist/tbv/core/index.js +157 -145
  95. package/dist/tbv/core/index.js.map +1 -1
  96. package/dist/tbv/core/managers/index.cjs +2 -0
  97. package/dist/tbv/core/managers/index.cjs.map +1 -0
  98. package/dist/tbv/core/managers/index.js +7 -0
  99. package/dist/tbv/core/managers/index.js.map +1 -0
  100. package/dist/tbv/core/primitives/index.cjs +1 -1
  101. package/dist/tbv/core/primitives/index.js +31 -29
  102. package/dist/tbv/core/primitives/index.js.map +1 -1
  103. package/dist/tbv/core/services/deposit/waitForPeginStatus.d.ts.map +1 -1
  104. package/dist/tbv/core/services/index.cjs +1 -1
  105. package/dist/tbv/core/services/index.js +28 -27
  106. package/dist/tbv/core/services/index.js.map +1 -1
  107. package/dist/tbv/core/services/pegout/state.d.ts +1 -1
  108. package/dist/tbv/core/utils/index.cjs +1 -1
  109. package/dist/tbv/core/utils/index.js +21 -20
  110. package/dist/tbv/core/utils/index.js.map +1 -1
  111. package/dist/tbv/index.cjs +1 -1
  112. package/dist/tbv/index.js +157 -145
  113. package/dist/tbv/index.js.map +1 -1
  114. package/dist/types-DnyyBNcC.cjs +2 -0
  115. package/dist/types-DnyyBNcC.cjs.map +1 -0
  116. package/dist/types-TiIjyo2b.js +320 -0
  117. package/dist/types-TiIjyo2b.js.map +1 -0
  118. package/dist/vault-registry-reader-BDFpXeH7.js +1152 -0
  119. package/dist/vault-registry-reader-BDFpXeH7.js.map +1 -0
  120. package/dist/vault-registry-reader-ejm2UYzk.cjs +2 -0
  121. package/dist/vault-registry-reader-ejm2UYzk.cjs.map +1 -0
  122. package/package.json +11 -1
  123. package/dist/PeginManager-C-L3huRO.js.map +0 -1
  124. package/dist/PeginManager-DmPmzPHz.cjs +0 -2
  125. package/dist/PeginManager-DmPmzPHz.cjs.map +0 -1
  126. package/dist/buildAndBroadcastRefund-Dx09Zbla.js +0 -965
  127. package/dist/buildAndBroadcastRefund-Dx09Zbla.js.map +0 -1
  128. package/dist/buildAndBroadcastRefund-PmJMNrhO.cjs +0 -2
  129. package/dist/buildAndBroadcastRefund-PmJMNrhO.cjs.map +0 -1
  130. package/dist/challengeAssert-D3tHnLWb.js +0 -298
  131. package/dist/challengeAssert-D3tHnLWb.js.map +0 -1
  132. package/dist/challengeAssert-Dp9d1bg1.cjs +0 -2
  133. package/dist/challengeAssert-Dp9d1bg1.cjs.map +0 -1
  134. package/dist/noPayout-BnsetBKW.js +0 -327
  135. package/dist/noPayout-BnsetBKW.js.map +0 -1
  136. package/dist/noPayout-DWaCtpMU.cjs +0 -2
  137. package/dist/noPayout-DWaCtpMU.cjs.map +0 -1
  138. package/dist/psbtInputFields-6sRcZqdb.cjs +0 -2
  139. package/dist/psbtInputFields-6sRcZqdb.cjs.map +0 -1
  140. package/dist/psbtInputFields-C5QPn1YK.js +0 -264
  141. package/dist/psbtInputFields-C5QPn1YK.js.map +0 -1
  142. package/dist/signing-BZigafm0.js +0 -64
  143. package/dist/signing-BZigafm0.js.map +0 -1
  144. package/dist/signing-DHSXjhLM.cjs +0 -2
  145. package/dist/signing-DHSXjhLM.cjs.map +0 -1
  146. package/dist/types-PthVHz5q.cjs +0 -2
  147. package/dist/types-PthVHz5q.cjs.map +0 -1
  148. package/dist/types-SYvSQWnc.js +0 -732
  149. package/dist/types-SYvSQWnc.js.map +0 -1
  150. package/dist/vault-registry-reader-CKe9TbX6.cjs +0 -2
  151. package/dist/vault-registry-reader-CKe9TbX6.cjs.map +0 -1
  152. package/dist/vault-registry-reader-CWGbw_wZ.js +0 -1567
  153. package/dist/vault-registry-reader-CWGbw_wZ.js.map +0 -1
@@ -1,965 +0,0 @@
1
- var G = Object.defineProperty;
2
- var Q = (e, t, r) => t in e ? G(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
3
- var x = (e, t, r) => Q(e, typeof t != "symbol" ? t + "" : t, r);
4
- import { B as Y, c as j, a as q, R as J, d as O, D as y } from "./types-SYvSQWnc.js";
5
- import { e as Z, s as l, u as K, v as H, p as w, f } from "./bitcoin-B0S8SHCX.js";
6
- import { s as ee } from "./sha2-6wN58S6R.js";
7
- import * as te from "bitcoinjs-lib";
8
- import { Transaction as m, Psbt as re } from "bitcoinjs-lib";
9
- import { Buffer as oe } from "buffer";
10
- import { c as b } from "./signing-BZigafm0.js";
11
- import "@babylonlabs-io/babylon-tbv-rust-wasm";
12
- import { a as V, c as S, e as v, g as ne, d as se, b as ie } from "./noPayout-BnsetBKW.js";
13
- const $ = 66;
14
- function L(e) {
15
- if (!e.startsWith("0x") && !e.startsWith("0X"))
16
- throw new Error("Expected 0x-prefixed hex string");
17
- const t = e.slice(2);
18
- if (t.length % 2 !== 0)
19
- throw new Error(`Hex string has odd length: ${t.length}`);
20
- if (!/^[0-9a-fA-F]*$/.test(t))
21
- throw new Error("Hex string contains non-hex characters");
22
- const r = new Uint8Array(t.length / 2);
23
- for (let o = 0; o < r.length; o++)
24
- r[o] = parseInt(t.slice(o * 2, o * 2 + 2), 16);
25
- return r;
26
- }
27
- function ae(e) {
28
- return `0x${Array.from(e).map((t) => t.toString(16).padStart(2, "0")).join("")}`;
29
- }
30
- function A(e, t) {
31
- if (e.length !== $)
32
- throw new Error(
33
- `${t} must be exactly 32 bytes (${$} hex chars with 0x prefix), got ${e.length}`
34
- );
35
- }
36
- function ue(e) {
37
- A(e, "Secret");
38
- const t = L(e), r = ee(t);
39
- return ae(r);
40
- }
41
- function ce(e, t) {
42
- return A(e, "Secret"), A(t, "Hashlock"), L(t), ue(e).toLowerCase() === t.toLowerCase();
43
- }
44
- const le = /^0x[0-9a-fA-F]{64}$/, he = /^0x[0-9a-fA-F]{40}$/, de = /^0x([0-9a-fA-F]{2})*$/;
45
- function E(e, t) {
46
- if (e.length !== 66)
47
- throw new Error(
48
- `${t} must be 32 bytes (66 hex chars with 0x prefix), got length ${e.length}`
49
- );
50
- if (!le.test(e))
51
- throw new Error(
52
- `${t} must contain only hex characters after the 0x prefix`
53
- );
54
- }
55
- function pe(e, t) {
56
- if (!he.test(e))
57
- throw new Error(
58
- `${t} must be a 20-byte 0x-prefixed hex address (42 chars)`
59
- );
60
- }
61
- function fe(e, t) {
62
- if (!de.test(e))
63
- throw new Error(
64
- `${t} must be a 0x-prefixed hex string with an even number of hex chars`
65
- );
66
- }
67
- async function rt(e) {
68
- const {
69
- btcVaultRegistryAddress: t,
70
- vaultId: r,
71
- hashlock: o,
72
- activationMetadata: i,
73
- writeContract: u,
74
- signal: s
75
- } = e;
76
- s == null || s.throwIfAborted(), pe(t, "btcVaultRegistryAddress"), E(r, "vaultId");
77
- const a = Z(e.secret);
78
- if (E(a, "secret"), o !== void 0 && (E(o, "hashlock"), !ce(a, o)))
79
- throw new Error(
80
- "Invalid secret: SHA256(secret) does not match the provided hashlock"
81
- );
82
- return fe(i, "activationMetadata"), u({
83
- address: t,
84
- abi: Y,
85
- functionName: "activateVaultWithSecret",
86
- args: [r, a, i]
87
- });
88
- }
89
- const ge = 1e4;
90
- async function D(e) {
91
- const {
92
- statusReader: t,
93
- peginTxid: r,
94
- targetStatuses: o,
95
- timeoutMs: i,
96
- pollIntervalMs: u = ge,
97
- signal: s
98
- } = e, a = Date.now();
99
- for (; ; ) {
100
- if (s != null && s.aborted)
101
- throw new Error(
102
- `Polling aborted for pegin ${r.slice(0, 8)}… (target: ${[...o].join(", ")})`
103
- );
104
- if (Date.now() - a >= i)
105
- throw new Error(
106
- `Polling timeout after ${i}ms for pegin ${r.slice(0, 8)}… (target: ${[...o].join(", ")})`
107
- );
108
- try {
109
- const c = (await t.getPeginStatus(
110
- { pegin_txid: r },
111
- s
112
- )).status;
113
- if (o.has(c))
114
- return c;
115
- if (j.has(c) && !o.has(c))
116
- throw new Error(
117
- `Pegin ${r.slice(0, 8)}… reached terminal status "${c}" while waiting for ${[...o].join(", ")}`
118
- );
119
- } catch (n) {
120
- if (!(n instanceof q && n.code === J.NOT_FOUND || n instanceof Error && n.message.includes("PegIn not found")))
121
- throw n;
122
- }
123
- await new Promise((n, c) => {
124
- const h = () => {
125
- clearTimeout(d), c(
126
- new Error(
127
- `Polling aborted for pegin ${r.slice(0, 8)}… (target: ${[...o].join(", ")})`
128
- )
129
- );
130
- }, d = setTimeout(() => {
131
- s == null || s.removeEventListener("abort", h), n();
132
- }, u);
133
- s == null || s.addEventListener("abort", h, { once: !0 });
134
- });
135
- }
136
- }
137
- const Pe = 300 * 1e3, ye = /* @__PURE__ */ new Set([
138
- y.PENDING_DEPOSITOR_WOTS_PK,
139
- ...O
140
- ]);
141
- async function ot(e) {
142
- const {
143
- statusReader: t,
144
- wotsSubmitter: r,
145
- peginTxid: o,
146
- depositorPk: i,
147
- wotsPublicKeys: u,
148
- timeoutMs: s = Pe,
149
- signal: a
150
- } = e;
151
- a == null || a.throwIfAborted();
152
- const n = await D({
153
- statusReader: t,
154
- peginTxid: o,
155
- targetStatuses: ye,
156
- timeoutMs: s,
157
- signal: a
158
- });
159
- O.has(n) || (a == null || a.throwIfAborted(), await r.submitDepositorWotsKey(
160
- {
161
- pegin_txid: o,
162
- depositor_pk: i,
163
- wots_public_keys: u
164
- },
165
- a
166
- ));
167
- }
168
- const I = 1;
169
- function be(e, t, r) {
170
- const o = l(r).toLowerCase(), u = [e, ...t].map(
171
- (s) => l(s).toLowerCase()
172
- ).filter((s) => s !== o);
173
- if (u.length === 0)
174
- throw new Error(
175
- "Cannot derive localChallengers: removing depositor from {vaultProvider, vaultKeepers} left an empty set"
176
- );
177
- return u;
178
- }
179
- function xe(e, t) {
180
- const r = e.ins[t];
181
- return K(new Uint8Array(r.hash).slice().reverse());
182
- }
183
- function B(e, t, r, o, i) {
184
- const u = e.ins[t];
185
- if (u.index !== 0)
186
- throw new Error(
187
- `NoPayout (challenger ${i}) input ${t} expected to spend ${o} vout 0, got vout ${u.index}`
188
- );
189
- const s = r.getId(), a = xe(e, t);
190
- if (a !== s)
191
- throw new Error(
192
- `NoPayout (challenger ${i}) input ${t} does not reference ${o} (expected txid ${s}, got ${a})`
193
- );
194
- }
195
- async function me(e, t, r) {
196
- const o = [], i = [], u = [];
197
- V(
198
- e.payout_tx.tx_hex,
199
- r.registeredPayoutScriptPubKey
200
- );
201
- const s = await S({
202
- payoutTxHex: e.payout_tx.tx_hex,
203
- peginTxHex: r.peginTxHex,
204
- assertTxHex: e.assert_tx.tx_hex,
205
- depositorBtcPubkey: r.depositorBtcPubkey,
206
- vaultProviderBtcPubkey: r.vaultProviderBtcPubkey,
207
- vaultKeeperBtcPubkeys: r.vaultKeeperBtcPubkeys,
208
- universalChallengerBtcPubkeys: r.universalChallengerBtcPubkeys,
209
- timelockPegin: r.timelockPegin,
210
- network: r.network
211
- });
212
- o.push(s.psbtHex), i.push(
213
- b(
214
- t,
215
- I
216
- )
217
- );
218
- const a = be(
219
- r.vaultProviderBtcPubkey,
220
- r.vaultKeeperBtcPubkeys,
221
- r.depositorBtcPubkey
222
- ), n = l(r.depositorBtcPubkey), c = m.fromHex(
223
- l(e.assert_tx.tx_hex)
224
- );
225
- for (const h of e.challenger_presign_data) {
226
- const d = l(h.challenger_pubkey), g = o.length, P = await we({
227
- challenger: h,
228
- challengerPubkey: d,
229
- claimerPubkey: n,
230
- localChallengers: a,
231
- assertTxParsed: c,
232
- ctx: r
233
- });
234
- o.push(P), i.push(
235
- b(
236
- t,
237
- I
238
- )
239
- ), u.push({
240
- challengerPubkey: d,
241
- noPayoutIdx: g
242
- });
243
- }
244
- return { psbtHexes: o, signOptions: i, challengerEntries: u };
245
- }
246
- async function we(e) {
247
- const {
248
- challenger: t,
249
- challengerPubkey: r,
250
- claimerPubkey: o,
251
- localChallengers: i,
252
- assertTxParsed: u,
253
- ctx: s
254
- } = e;
255
- ne(
256
- t.nopayout_tx.tx_hex,
257
- r,
258
- s.network
259
- );
260
- const a = m.fromHex(
261
- l(t.nopayout_tx.tx_hex)
262
- ), n = m.fromHex(
263
- l(t.challenge_assert_x_tx.tx_hex)
264
- ), c = m.fromHex(
265
- l(t.challenge_assert_y_tx.tx_hex)
266
- );
267
- if (a.ins.length !== 3)
268
- throw new Error(
269
- `NoPayout (challenger ${r}) must have exactly 3 inputs, got ${a.ins.length}`
270
- );
271
- B(
272
- a,
273
- 0,
274
- u,
275
- "Assert",
276
- r
277
- ), B(
278
- a,
279
- 1,
280
- n,
281
- "ChallengeAssertX",
282
- r
283
- ), B(
284
- a,
285
- 2,
286
- c,
287
- "ChallengeAssertY",
288
- r
289
- );
290
- const h = [
291
- u.outs[0],
292
- n.outs[0],
293
- c.outs[0]
294
- ].map((d) => ({
295
- script_pubkey: K(new Uint8Array(d.script)),
296
- value: d.value
297
- }));
298
- return se({
299
- noPayoutTxHex: t.nopayout_tx.tx_hex,
300
- challengerPubkey: r,
301
- prevouts: h,
302
- connectorParams: {
303
- claimer: o,
304
- localChallengers: i,
305
- universalChallengers: s.universalChallengerBtcPubkeys,
306
- timelockAssert: s.timelockAssert,
307
- councilMembers: s.councilMembers,
308
- councilQuorum: s.councilQuorum
309
- }
310
- });
311
- }
312
- function ve(e, t, r) {
313
- const o = v(
314
- e[0],
315
- r
316
- ), i = {};
317
- for (const u of t)
318
- i[u.challengerPubkey] = {
319
- nopayout_signature: v(
320
- e[u.noPayoutIdx],
321
- r
322
- )
323
- };
324
- return {
325
- payout_signatures: {
326
- payout_signature: o
327
- },
328
- per_challenger: i
329
- };
330
- }
331
- async function ke(e, t, r) {
332
- if (typeof e.signPsbts == "function")
333
- return e.signPsbts(t, r);
334
- const o = [];
335
- for (let i = 0; i < t.length; i++)
336
- o.push(await e.signPsbt(t[i], r == null ? void 0 : r[i]));
337
- return o;
338
- }
339
- async function Te(e) {
340
- const { depositorGraph: t, btcWallet: r, signingContext: o } = e, i = l(o.depositorBtcPubkey), u = await r.getPublicKeyHex(), { psbtHexes: s, signOptions: a, challengerEntries: n } = await me(
341
- t,
342
- u,
343
- o
344
- ), c = await ke(
345
- r,
346
- s,
347
- a
348
- );
349
- if (c.length !== s.length)
350
- throw new Error(
351
- `Wallet returned ${c.length} signed PSBTs, expected ${s.length}`
352
- );
353
- return ve(
354
- c,
355
- n,
356
- i
357
- );
358
- }
359
- class Ee {
360
- /**
361
- * Creates a new PayoutManager instance.
362
- *
363
- * @param config - Manager configuration including wallet
364
- */
365
- constructor(t) {
366
- x(this, "config");
367
- this.config = t;
368
- }
369
- /**
370
- * Signs a Payout transaction and extracts the Schnorr signature.
371
- *
372
- * Flow:
373
- * 1. Vault provider submits Claim transaction
374
- * 2. Claimer submits Assert transaction to prove validity
375
- * 3. Payout can be executed (references Assert tx)
376
- *
377
- * This method orchestrates the following steps:
378
- * 1. Get wallet's public key and convert to x-only format
379
- * 2. Validate wallet pubkey matches on-chain depositor pubkey (if provided)
380
- * 3. Build unsigned PSBT using primitives
381
- * 4. Sign PSBT via btcWallet.signPsbt()
382
- * 5. Extract 64-byte Schnorr signature using primitives
383
- *
384
- * The returned signature can be submitted to the vault provider API.
385
- *
386
- * @param params - Payout signing parameters
387
- * @returns Signature result with 64-byte Schnorr signature and depositor pubkey
388
- * @throws Error if wallet pubkey doesn't match depositor pubkey
389
- * @throws Error if wallet operations fail or signature extraction fails
390
- */
391
- async signPayoutTransaction(t) {
392
- this.validatePayoutOutputs(
393
- t.payoutTxHex,
394
- t.registeredPayoutScriptPubKey
395
- );
396
- const r = await this.config.btcWallet.getPublicKeyHex(), { depositorPubkey: o } = H(
397
- r,
398
- t.depositorBtcPubkey
399
- ), i = await S({
400
- payoutTxHex: t.payoutTxHex,
401
- peginTxHex: t.peginTxHex,
402
- assertTxHex: t.assertTxHex,
403
- depositorBtcPubkey: o,
404
- vaultProviderBtcPubkey: t.vaultProviderBtcPubkey,
405
- vaultKeeperBtcPubkeys: t.vaultKeeperBtcPubkeys,
406
- universalChallengerBtcPubkeys: t.universalChallengerBtcPubkeys,
407
- timelockPegin: t.timelockPegin,
408
- network: this.config.network
409
- }), u = await this.config.btcWallet.signPsbt(
410
- i.psbtHex,
411
- b(r, 1)
412
- );
413
- return {
414
- signature: v(u, o),
415
- depositorBtcPubkey: o
416
- };
417
- }
418
- /**
419
- * Gets the configured Bitcoin network.
420
- *
421
- * @returns The Bitcoin network (mainnet, testnet, signet, regtest)
422
- */
423
- getNetwork() {
424
- return this.config.network;
425
- }
426
- /**
427
- * Checks if the wallet supports batch signing (signPsbts).
428
- *
429
- * @returns true if batch signing is supported
430
- */
431
- supportsBatchSigning() {
432
- return typeof this.config.btcWallet.signPsbts == "function";
433
- }
434
- /**
435
- * Batch signs multiple payout transactions (1 per claimer).
436
- * This allows signing all transactions with a single wallet interaction.
437
- *
438
- * @param transactions - Array of payout params to sign
439
- * @returns Array of signature results matching input order
440
- * @throws Error if wallet doesn't support batch signing
441
- * @throws Error if any signing operation fails
442
- */
443
- async signPayoutTransactionsBatch(t) {
444
- if (!this.supportsBatchSigning())
445
- throw new Error(
446
- "Wallet does not support batch signing (signPsbts method not available)"
447
- );
448
- const r = await this.config.btcWallet.getPublicKeyHex(), o = [], i = [], u = [];
449
- for (const n of t) {
450
- this.validatePayoutOutputs(
451
- n.payoutTxHex,
452
- n.registeredPayoutScriptPubKey
453
- );
454
- const { depositorPubkey: c } = H(
455
- r,
456
- n.depositorBtcPubkey
457
- );
458
- u.push(c);
459
- const h = await S({
460
- payoutTxHex: n.payoutTxHex,
461
- peginTxHex: n.peginTxHex,
462
- assertTxHex: n.assertTxHex,
463
- depositorBtcPubkey: c,
464
- vaultProviderBtcPubkey: n.vaultProviderBtcPubkey,
465
- vaultKeeperBtcPubkeys: n.vaultKeeperBtcPubkeys,
466
- universalChallengerBtcPubkeys: n.universalChallengerBtcPubkeys,
467
- timelockPegin: n.timelockPegin,
468
- network: this.config.network
469
- });
470
- o.push(h.psbtHex), i.push(b(r, 1));
471
- }
472
- const s = await this.config.btcWallet.signPsbts(
473
- o,
474
- i
475
- );
476
- if (s.length !== t.length)
477
- throw new Error(
478
- `Expected ${t.length} signed PSBTs but received ${s.length}`
479
- );
480
- const a = [];
481
- for (let n = 0; n < t.length; n++) {
482
- const c = u[n], h = v(
483
- s[n],
484
- c
485
- );
486
- a.push({
487
- payoutSignature: h,
488
- depositorBtcPubkey: c
489
- });
490
- }
491
- return a;
492
- }
493
- /**
494
- * Validates that the payout transaction's largest output pays to the
495
- * registered depositor payout address (scriptPubKey).
496
- *
497
- * This prevents two attack vectors from a malicious vault provider:
498
- * 1. Substituting a completely different payout address
499
- * 2. Including a dust output to the correct address while routing
500
- * the actual funds to an attacker-controlled address
501
- *
502
- * @param payoutTxHex - Raw payout transaction hex
503
- * @param registeredPayoutScriptPubKey - On-chain registered scriptPubKey (hex, with or without 0x prefix)
504
- * @throws Error if scriptPubKey is invalid hex
505
- * @throws Error if the largest output does not pay to the registered address
506
- */
507
- validatePayoutOutputs(t, r) {
508
- V(
509
- t,
510
- r
511
- );
512
- }
513
- }
514
- const Be = 1200 * 1e3, M = /* @__PURE__ */ new Set([
515
- y.PENDING_ACKS,
516
- y.PENDING_ACTIVATION,
517
- y.ACTIVATED
518
- ]), _e = /* @__PURE__ */ new Set([
519
- y.PENDING_DEPOSITOR_SIGNATURES,
520
- ...M
521
- ]);
522
- function Se(e) {
523
- return e.map((t) => ({
524
- claimerPubkeyXOnly: w(t.claimer_pubkey),
525
- payoutTxHex: t.payout_tx.tx_hex,
526
- assertTxHex: t.assert_tx.tx_hex
527
- }));
528
- }
529
- function Ae(e) {
530
- const { output: t } = te.payments.p2tr({
531
- internalPubkey: oe.from(e, "hex")
532
- });
533
- if (!t)
534
- throw new Error("Failed to derive BIP-86 P2TR scriptPubKey");
535
- return t.toString("hex");
536
- }
537
- function Ce(e, t) {
538
- const r = l(e).toLowerCase(), o = l(
539
- t.vaultProviderBtcPubkey
540
- ).toLowerCase(), i = l(
541
- t.depositorBtcPubkey
542
- ).toLowerCase();
543
- if (r === o || r === i)
544
- return t.registeredPayoutScriptPubKey;
545
- if (!t.vaultKeeperBtcPubkeys.some(
546
- (a) => l(a).toLowerCase() === r
547
- ))
548
- throw new Error(
549
- `Unknown claimer pubkey ${r}: not VP, depositor, or a registered vault keeper`
550
- );
551
- return `0x${Ae(r)}`;
552
- }
553
- function R(e, t) {
554
- return {
555
- payoutTxHex: e.payoutTxHex,
556
- peginTxHex: t.peginTxHex,
557
- assertTxHex: e.assertTxHex,
558
- vaultProviderBtcPubkey: t.vaultProviderBtcPubkey,
559
- vaultKeeperBtcPubkeys: t.vaultKeeperBtcPubkeys,
560
- universalChallengerBtcPubkeys: t.universalChallengerBtcPubkeys,
561
- depositorBtcPubkey: t.depositorBtcPubkey,
562
- timelockPegin: t.timelockPegin,
563
- registeredPayoutScriptPubKey: Ce(
564
- e.claimerPubkeyXOnly,
565
- t
566
- )
567
- };
568
- }
569
- async function He(e, t, r, o) {
570
- const i = new Ee({
571
- network: t.network,
572
- btcWallet: e
573
- }), u = r.length;
574
- o == null || o(0, u);
575
- let s;
576
- if (i.supportsBatchSigning())
577
- s = (await i.signPayoutTransactionsBatch(
578
- r.map((c) => R(c, t))
579
- )).map((c) => c.payoutSignature);
580
- else {
581
- s = [];
582
- for (let n = 0; n < r.length; n++) {
583
- o == null || o(n, u);
584
- const c = await i.signPayoutTransaction(
585
- R(r[n], t)
586
- );
587
- s.push(c.signature);
588
- }
589
- }
590
- const a = {};
591
- for (let n = 0; n < r.length; n++)
592
- a[r[n].claimerPubkeyXOnly] = {
593
- payout_signature: s[n]
594
- };
595
- return o == null || o(u, u), a;
596
- }
597
- async function nt(e) {
598
- const {
599
- statusReader: t,
600
- presignClient: r,
601
- btcWallet: o,
602
- peginTxid: i,
603
- depositorPk: u,
604
- signingContext: s,
605
- timeoutMs: a = Be,
606
- signal: n,
607
- onProgress: c
608
- } = e, h = await D({
609
- statusReader: t,
610
- peginTxid: i,
611
- targetStatuses: _e,
612
- timeoutMs: a,
613
- signal: n
614
- });
615
- if (M.has(h))
616
- return;
617
- n == null || n.throwIfAborted();
618
- const d = await r.requestDepositorPresignTransactions(
619
- {
620
- pegin_txid: i,
621
- depositor_pk: u
622
- },
623
- n
624
- );
625
- n == null || n.throwIfAborted();
626
- const g = w(u), P = d.txs.filter(
627
- (z) => w(z.claimer_pubkey) !== g
628
- ), k = Se(P), T = await He(
629
- o,
630
- s,
631
- k,
632
- c
633
- );
634
- n == null || n.throwIfAborted();
635
- const p = await Te({
636
- depositorGraph: d.depositor_graph,
637
- btcWallet: o,
638
- signingContext: {
639
- peginTxHex: s.peginTxHex,
640
- depositorBtcPubkey: u,
641
- vaultProviderBtcPubkey: s.vaultProviderBtcPubkey,
642
- vaultKeeperBtcPubkeys: s.vaultKeeperBtcPubkeys,
643
- universalChallengerBtcPubkeys: s.universalChallengerBtcPubkeys,
644
- timelockPegin: s.timelockPegin,
645
- timelockAssert: s.timelockAssert,
646
- councilMembers: s.councilMembers,
647
- councilQuorum: s.councilQuorum,
648
- network: s.network,
649
- registeredPayoutScriptPubKey: s.registeredPayoutScriptPubKey
650
- }
651
- });
652
- n == null || n.throwIfAborted();
653
- const C = { ...T };
654
- C[l(u)] = p.payout_signatures, await r.submitDepositorPresignatures(
655
- {
656
- pegin_txid: i,
657
- depositor_pk: u,
658
- signatures: C,
659
- depositor_claimer_presignatures: p
660
- },
661
- n
662
- );
663
- }
664
- function $e(e) {
665
- return /^[0-9a-fA-F]{64}$/.test(e);
666
- }
667
- function st(e) {
668
- const {
669
- amountSats: t,
670
- minDeposit: r,
671
- maxDeposit: o,
672
- btcBalance: i,
673
- estimatedFeeSats: u,
674
- depositorClaimValue: s
675
- } = e;
676
- return !(t <= 0n || t < r || o && o > 0n && t > o || u == null || s == null || t + u + s > i);
677
- }
678
- function it(e, t, r) {
679
- return e <= 0n ? {
680
- valid: !1,
681
- error: "Deposit amount must be greater than zero"
682
- } : e < t ? {
683
- valid: !1,
684
- error: `Minimum deposit is ${f(t)} BTC`
685
- } : r && r > 0n && e > r ? {
686
- valid: !1,
687
- error: `Maximum deposit is ${f(r)} BTC`
688
- } : { valid: !0 };
689
- }
690
- function at(e) {
691
- const { amount: t, effectiveRemaining: r } = e;
692
- return r === null ? { valid: !0 } : r === 0n ? {
693
- valid: !1,
694
- error: "Supply cap reached — deposits temporarily paused"
695
- } : t > r ? {
696
- valid: !1,
697
- error: `Vault size exceeds remaining capacity (${f(r)} BTC)`
698
- } : { valid: !0 };
699
- }
700
- function ut(e, t) {
701
- if (!e || e.length === 0)
702
- return {
703
- valid: !1,
704
- error: "At least one vault provider must be selected"
705
- };
706
- const r = t.map(
707
- (i) => i.toLowerCase()
708
- );
709
- return e.filter(
710
- (i) => !r.includes(i.toLowerCase())
711
- ).length > 0 ? {
712
- valid: !1,
713
- error: "Invalid vault provider selected"
714
- } : { valid: !0 };
715
- }
716
- function Ie(e, t, r) {
717
- if (!e || e.length === 0)
718
- return {
719
- valid: !1,
720
- error: "At least one vault amount required"
721
- };
722
- for (let o = 0; o < e.length; o++) {
723
- const i = e[o];
724
- if (i <= 0n)
725
- return {
726
- valid: !1,
727
- error: `Vault ${o + 1} amount must be positive`
728
- };
729
- if (t && i < t)
730
- return {
731
- valid: !1,
732
- error: `Vault ${o + 1} amount ${f(i)} BTC is below minimum deposit ${f(t)} BTC`
733
- };
734
- if (r && i > r)
735
- return {
736
- valid: !1,
737
- error: `Vault ${o + 1} amount ${f(i)} BTC exceeds maximum deposit ${f(r)} BTC`
738
- };
739
- }
740
- return { valid: !0 };
741
- }
742
- function Re(e) {
743
- const t = l(e);
744
- return $e(t) ? { valid: !0 } : {
745
- valid: !1,
746
- error: "Invalid pubkey format: must be 64 hex characters (32-byte x-only public key, no 0x prefix)"
747
- };
748
- }
749
- function Ne(e) {
750
- if (!e || e.length === 0)
751
- throw new Error(
752
- "No vault keepers available. The system requires at least one vault keeper to create a deposit."
753
- );
754
- }
755
- function Oe(e) {
756
- if (!e || e.length === 0)
757
- throw new Error(
758
- "No universal challengers available. The system requires at least one universal challenger to create a deposit."
759
- );
760
- }
761
- function Ke(e) {
762
- if (e.length === 0)
763
- throw new Error("No spendable UTXOs available");
764
- }
765
- function ct(e) {
766
- const {
767
- vaultAmounts: t,
768
- confirmedUTXOs: r,
769
- vaultProviderBtcPubkey: o,
770
- vaultKeeperBtcPubkeys: i,
771
- universalChallengerBtcPubkeys: u,
772
- minDeposit: s,
773
- maxDeposit: a
774
- } = e, n = Ie(
775
- t,
776
- s,
777
- a
778
- );
779
- if (!n.valid)
780
- throw new Error(n.error);
781
- const c = Re(o);
782
- if (!c.valid)
783
- throw new Error(c.error);
784
- Ne(i), Oe(u), Ke(r);
785
- }
786
- var U = /* @__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))(U || {});
787
- const Ve = /* @__PURE__ */ new Set([
788
- "PayoutBroadcast",
789
- "Failed"
790
- /* FAILED */
791
- ]);
792
- function lt(e) {
793
- return Object.values(U).includes(
794
- e
795
- );
796
- }
797
- function ht(e) {
798
- return !!e && Ve.has(e);
799
- }
800
- class Le extends Error {
801
- constructor(r, o) {
802
- super(`Refund not yet mature (BIP68 not final): ${o.message}`);
803
- x(this, "vaultId");
804
- x(this, "cause");
805
- this.name = "BIP68NotMatureError", this.vaultId = r, this.cause = o;
806
- }
807
- }
808
- const De = /^0x[0-9a-fA-F]{64}$/, Me = /^(?:0x)?(?:[0-9a-fA-F]{2})+$/, F = /^(?:0x)?(?:[0-9a-fA-F]{64}|[0-9a-fA-F]{66})$/, W = 160;
809
- function dt(e) {
810
- if (!Number.isFinite(e) || e <= 0)
811
- throw new Error(
812
- `feeRateSatsVb must be a positive finite number, got ${e}`
813
- );
814
- return BigInt(Math.ceil(e * W));
815
- }
816
- const Ue = 1, N = 65535, Fe = /non-BIP68-final/i;
817
- function X(e, t) {
818
- if (e.length !== 66)
819
- throw new Error(
820
- `${t} must be 32 bytes (66 hex chars with 0x prefix), got length ${e.length}`
821
- );
822
- if (!De.test(e))
823
- throw new Error(
824
- `${t} must contain only hex characters after the 0x prefix`
825
- );
826
- }
827
- function _(e, t) {
828
- if (!Number.isInteger(e) || e < 0)
829
- throw new Error(`${t} must be a non-negative integer, got ${e}`);
830
- }
831
- function We(e) {
832
- if (X(e.hashlock, "hashlock"), !Number.isInteger(e.htlcVout) || e.htlcVout < 0 || e.htlcVout > N)
833
- throw new Error(
834
- `htlcVout must be an integer 0-${N}, got ${e.htlcVout}`
835
- );
836
- if (_(e.offchainParamsVersion, "offchainParamsVersion"), _(e.appVaultKeepersVersion, "appVaultKeepersVersion"), _(
837
- e.universalChallengersVersion,
838
- "universalChallengersVersion"
839
- ), typeof e.unsignedPrePeginTxHex != "string" || e.unsignedPrePeginTxHex.length === 0)
840
- throw new Error("unsignedPrePeginTxHex must be a non-empty hex string");
841
- if (!Me.test(e.unsignedPrePeginTxHex))
842
- throw new Error(
843
- "unsignedPrePeginTxHex must be a hex byte string (optional 0x prefix, even length)"
844
- );
845
- if (!e.depositorBtcPubkey || !F.test(e.depositorBtcPubkey))
846
- throw new Error(
847
- "depositorBtcPubkey must be 32 or 33 bytes of hex (optional 0x prefix)"
848
- );
849
- if (typeof e.amount != "bigint" || e.amount <= 0n)
850
- throw new Error(`amount must be a positive bigint, got ${e.amount}`);
851
- }
852
- function Xe(e) {
853
- if (!e.vaultProviderPubkey || !F.test(e.vaultProviderPubkey))
854
- throw new Error("vaultProviderPubkey must be 32 or 33 bytes of hex");
855
- if (e.vaultKeeperPubkeys.length === 0)
856
- throw new Error("vaultKeeperPubkeys must be non-empty");
857
- if (e.universalChallengerPubkeys.length === 0)
858
- throw new Error("universalChallengerPubkeys must be non-empty");
859
- if (!Number.isInteger(e.timelockRefund) || e.timelockRefund <= 0)
860
- throw new Error(
861
- `timelockRefund must be a positive integer, got ${e.timelockRefund}`
862
- );
863
- if (typeof e.feeRate != "bigint" || e.feeRate <= 0n)
864
- throw new Error(
865
- `protocol feeRate must be a positive bigint, got ${e.feeRate}`
866
- );
867
- if (!Number.isInteger(e.numLocalChallengers) || e.numLocalChallengers < 0)
868
- throw new Error("numLocalChallengers must be a non-negative integer");
869
- if (!Number.isInteger(e.councilQuorum) || !Number.isInteger(e.councilSize) || e.councilQuorum <= 0 || e.councilSize <= 0 || e.councilQuorum > e.councilSize)
870
- throw new Error(
871
- `councilQuorum (${e.councilQuorum}) must be in [1, councilSize=${e.councilSize}]`
872
- );
873
- }
874
- function ze(e) {
875
- const t = re.fromHex(e);
876
- try {
877
- t.finalizeAllInputs();
878
- } catch (r) {
879
- const o = r instanceof Error ? r.message : String(r);
880
- if (!o.includes("already finalized"))
881
- throw new Error(`Failed to finalize refund PSBT: ${o}`);
882
- }
883
- return t.extractTransaction().toHex();
884
- }
885
- async function pt(e) {
886
- const {
887
- vaultId: t,
888
- readVault: r,
889
- readPrePeginContext: o,
890
- feeRate: i,
891
- signPsbt: u,
892
- broadcastTx: s,
893
- signal: a
894
- } = e;
895
- a == null || a.throwIfAborted(), X(t, "vaultId");
896
- const n = await r();
897
- We(n), a == null || a.throwIfAborted();
898
- const c = await o(n);
899
- if (Xe(c), a == null || a.throwIfAborted(), !Number.isFinite(i) || i <= 0)
900
- throw new Error(`feeRate must be a positive number, got ${i}`);
901
- const h = BigInt(Math.ceil(i * W));
902
- a == null || a.throwIfAborted();
903
- const d = w(
904
- n.depositorBtcPubkey
905
- ), { psbtHex: g } = await ie({
906
- prePeginParams: {
907
- depositorPubkey: d,
908
- vaultProviderPubkey: l(c.vaultProviderPubkey),
909
- vaultKeeperPubkeys: c.vaultKeeperPubkeys.map(l),
910
- universalChallengerPubkeys: c.universalChallengerPubkeys.map(l),
911
- hashlocks: [l(n.hashlock)],
912
- timelockRefund: c.timelockRefund,
913
- pegInAmounts: [n.amount],
914
- feeRate: c.feeRate,
915
- numLocalChallengers: c.numLocalChallengers,
916
- councilQuorum: c.councilQuorum,
917
- councilSize: c.councilSize,
918
- network: c.network
919
- },
920
- fundedPrePeginTxHex: l(n.unsignedPrePeginTxHex),
921
- htlcVout: n.htlcVout,
922
- refundFee: h,
923
- // buildRefundPsbt's top-level `hashlock` param is documented as "no 0x
924
- // prefix" and flows into the WASM HTLC connector derivation; a prefixed
925
- // value would derive the wrong refund script leaf and yield an
926
- // unspendable PSBT. Match the `hashlocks` array handling above.
927
- hashlock: l(n.hashlock)
928
- });
929
- a == null || a.throwIfAborted();
930
- const P = b(
931
- n.depositorBtcPubkey,
932
- Ue
933
- ), k = await u(g, P), T = ze(k);
934
- a == null || a.throwIfAborted();
935
- try {
936
- return await s(T);
937
- } catch (p) {
938
- throw p instanceof Error && Fe.test(p.message) ? new Le(t, p) : p;
939
- }
940
- }
941
- export {
942
- Le as B,
943
- U as C,
944
- Ee as P,
945
- W as R,
946
- rt as a,
947
- Te as b,
948
- at as c,
949
- ut as d,
950
- Ie as e,
951
- Re as f,
952
- ct as g,
953
- ue as h,
954
- st as i,
955
- ce as j,
956
- lt as k,
957
- ht as l,
958
- pt as m,
959
- dt as n,
960
- nt as r,
961
- ot as s,
962
- it as v,
963
- D as w
964
- };
965
- //# sourceMappingURL=buildAndBroadcastRefund-Dx09Zbla.js.map