@babylonlabs-io/ts-sdk 0.48.5 → 0.49.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.
- package/dist/buildAndBroadcastRefund-Bmz9WJuN.js +1011 -0
- package/dist/{buildAndBroadcastRefund-o9Byvkut.js.map → buildAndBroadcastRefund-Bmz9WJuN.js.map} +1 -1
- package/dist/buildAndBroadcastRefund-ob1n2iB3.cjs +2 -0
- package/dist/{buildAndBroadcastRefund-BOtxUi05.cjs.map → buildAndBroadcastRefund-ob1n2iB3.cjs.map} +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +2 -2
- package/dist/{primeVpAuth-CZsFLrHX.js → primeVpAuth-BdrwraAe.js} +767 -381
- package/dist/primeVpAuth-BdrwraAe.js.map +1 -0
- package/dist/primeVpAuth-Brl_bnBH.cjs +2 -0
- package/dist/primeVpAuth-Brl_bnBH.cjs.map +1 -0
- package/dist/tbv/core/clients/index.cjs +1 -1
- package/dist/tbv/core/clients/index.js +1 -1
- package/dist/tbv/core/clients/vault-provider/auth/__tests__/cborDecode.test.d.ts +2 -0
- package/dist/tbv/core/clients/vault-provider/auth/__tests__/cborDecode.test.d.ts.map +1 -0
- package/dist/tbv/core/clients/vault-provider/auth/__tests__/goldenVectors.d.ts +14 -0
- package/dist/tbv/core/clients/vault-provider/auth/__tests__/goldenVectors.d.ts.map +1 -1
- package/dist/tbv/core/clients/vault-provider/auth/__tests__/mintTestCwt.d.ts +43 -0
- package/dist/tbv/core/clients/vault-provider/auth/__tests__/mintTestCwt.d.ts.map +1 -0
- package/dist/tbv/core/clients/vault-provider/auth/__tests__/verifyDepositorCwt.test.d.ts +2 -0
- package/dist/tbv/core/clients/vault-provider/auth/__tests__/verifyDepositorCwt.test.d.ts.map +1 -0
- package/dist/tbv/core/clients/vault-provider/auth/cborDecode.d.ts +80 -0
- package/dist/tbv/core/clients/vault-provider/auth/cborDecode.d.ts.map +1 -0
- package/dist/tbv/core/clients/vault-provider/auth/createAuthenticatedVpClient.d.ts +5 -0
- package/dist/tbv/core/clients/vault-provider/auth/createAuthenticatedVpClient.d.ts.map +1 -1
- package/dist/tbv/core/clients/vault-provider/auth/primeVpAuth.d.ts +5 -0
- package/dist/tbv/core/clients/vault-provider/auth/primeVpAuth.d.ts.map +1 -1
- package/dist/tbv/core/clients/vault-provider/auth/tokenProvider.d.ts +8 -0
- package/dist/tbv/core/clients/vault-provider/auth/tokenProvider.d.ts.map +1 -1
- package/dist/tbv/core/clients/vault-provider/auth/tokenRegistry.d.ts +2 -0
- package/dist/tbv/core/clients/vault-provider/auth/tokenRegistry.d.ts.map +1 -1
- package/dist/tbv/core/clients/vault-provider/auth/verifyDepositorCwt.d.ts +80 -0
- package/dist/tbv/core/clients/vault-provider/auth/verifyDepositorCwt.d.ts.map +1 -0
- package/dist/tbv/core/index.cjs +1 -1
- package/dist/tbv/core/index.js +2 -2
- package/dist/tbv/core/services/index.cjs +1 -1
- package/dist/tbv/core/services/index.js +1 -1
- package/dist/tbv/core/services/refund/buildAndBroadcastRefund.d.ts +8 -22
- package/dist/tbv/core/services/refund/buildAndBroadcastRefund.d.ts.map +1 -1
- package/dist/tbv/index.cjs +1 -1
- package/dist/tbv/index.js +2 -2
- package/package.json +1 -1
- package/dist/buildAndBroadcastRefund-BOtxUi05.cjs +0 -2
- package/dist/buildAndBroadcastRefund-o9Byvkut.js +0 -1029
- package/dist/primeVpAuth-CZsFLrHX.js.map +0 -1
- package/dist/primeVpAuth-DhbTaCKm.cjs +0 -2
- package/dist/primeVpAuth-DhbTaCKm.cjs.map +0 -1
|
@@ -1,1029 +0,0 @@
|
|
|
1
|
-
var Z = Object.defineProperty;
|
|
2
|
-
var ee = (e, t, r) => t in e ? Z(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
|
|
3
|
-
var $ = (e, t, r) => ee(e, typeof t != "symbol" ? t + "" : t, r);
|
|
4
|
-
import { B as te } from "./BTCVaultRegistry.abi-Chs4AFBj.js";
|
|
5
|
-
import { e as re, v as ne, s as p, u as X, p as A, f as T } from "./bitcoin-B5aNKtsk.js";
|
|
6
|
-
import { v as oe, P as ae, f as se } from "./PayoutManager-D26nGR-e.js";
|
|
7
|
-
import { D as w, c as ie, a as ue, R as le, d as z } from "./types-CQDRQvV-.js";
|
|
8
|
-
import { Transaction as S, Psbt as ce } from "bitcoinjs-lib";
|
|
9
|
-
import { c as he, d as V, e as N, f as H } from "./verifyScriptPathSchnorrSignature-CeZp6tMw.js";
|
|
10
|
-
import { c as de, a as pe, b as ge } from "./noPayout-C1WCsqfd.js";
|
|
11
|
-
import { c as O } from "./signing-DaLvGwQe.js";
|
|
12
|
-
import { computeMinClaimValue as fe, computeMinPeginFee as me } from "@babylonlabs-io/babylon-tbv-rust-wasm";
|
|
13
|
-
const Pe = /^0x[0-9a-fA-F]{64}$/, be = /^0x[0-9a-fA-F]{40}$/, ye = /^0x([0-9a-fA-F]{2})*$/;
|
|
14
|
-
function R(e, t) {
|
|
15
|
-
if (e.length !== 66)
|
|
16
|
-
throw new Error(
|
|
17
|
-
`${t} must be 32 bytes (66 hex chars with 0x prefix), got length ${e.length}`
|
|
18
|
-
);
|
|
19
|
-
if (!Pe.test(e))
|
|
20
|
-
throw new Error(
|
|
21
|
-
`${t} must contain only hex characters after the 0x prefix`
|
|
22
|
-
);
|
|
23
|
-
}
|
|
24
|
-
function we(e, t) {
|
|
25
|
-
if (!be.test(e))
|
|
26
|
-
throw new Error(
|
|
27
|
-
`${t} must be a 20-byte 0x-prefixed hex address (42 chars)`
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
function xe(e, t) {
|
|
31
|
-
if (!ye.test(e))
|
|
32
|
-
throw new Error(
|
|
33
|
-
`${t} must be a 0x-prefixed hex string with an even number of hex chars`
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
async function ht(e) {
|
|
37
|
-
const {
|
|
38
|
-
btcVaultRegistryAddress: t,
|
|
39
|
-
vaultId: r,
|
|
40
|
-
hashlock: n,
|
|
41
|
-
activationMetadata: i,
|
|
42
|
-
writeContract: u,
|
|
43
|
-
signal: s
|
|
44
|
-
} = e;
|
|
45
|
-
s == null || s.throwIfAborted(), we(t, "btcVaultRegistryAddress"), R(r, "vaultId");
|
|
46
|
-
const o = re(e.secret);
|
|
47
|
-
if (R(o, "secret"), n !== void 0 && (R(n, "hashlock"), !oe(o, n)))
|
|
48
|
-
throw new Error(
|
|
49
|
-
"Invalid secret: SHA256(secret) does not match the provided hashlock"
|
|
50
|
-
);
|
|
51
|
-
return xe(i, "activationMetadata"), u({
|
|
52
|
-
address: t,
|
|
53
|
-
abi: te,
|
|
54
|
-
functionName: "activateVaultWithSecret",
|
|
55
|
-
args: [r, o, i]
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
const ve = 1e4;
|
|
59
|
-
async function G(e) {
|
|
60
|
-
const {
|
|
61
|
-
statusReader: t,
|
|
62
|
-
peginTxid: r,
|
|
63
|
-
targetStatuses: n,
|
|
64
|
-
timeoutMs: i,
|
|
65
|
-
pollIntervalMs: u = ve,
|
|
66
|
-
signal: s
|
|
67
|
-
} = e, o = Date.now();
|
|
68
|
-
for (; ; ) {
|
|
69
|
-
if (s != null && s.aborted)
|
|
70
|
-
throw new Error(
|
|
71
|
-
`Polling aborted for pegin ${r.slice(0, 8)}… (target: ${[...n].join(", ")})`
|
|
72
|
-
);
|
|
73
|
-
if (Date.now() - o >= i)
|
|
74
|
-
throw new Error(
|
|
75
|
-
`Polling timeout after ${i}ms for pegin ${r.slice(0, 8)}… (target: ${[...n].join(", ")})`
|
|
76
|
-
);
|
|
77
|
-
try {
|
|
78
|
-
const a = await t.getPeginStatus(
|
|
79
|
-
{ pegin_txid: r },
|
|
80
|
-
s
|
|
81
|
-
);
|
|
82
|
-
if (a.pegin_txid.toLowerCase() !== r.toLowerCase())
|
|
83
|
-
throw new Error(
|
|
84
|
-
`getPeginStatus returned status for pegin ${a.pegin_txid.slice(0, 8)}…, requested ${r.slice(0, 8)}…`
|
|
85
|
-
);
|
|
86
|
-
const l = a.status;
|
|
87
|
-
if (n.has(l) || l === w.ACTIVATED)
|
|
88
|
-
return l;
|
|
89
|
-
if (l === w.EXPIRED || ie.has(l))
|
|
90
|
-
throw new Error(
|
|
91
|
-
`Pegin ${r.slice(0, 8)}… reached terminal status "${l}" while waiting for ${[...n].join(", ")}`
|
|
92
|
-
);
|
|
93
|
-
} catch (a) {
|
|
94
|
-
if (!(a instanceof ue && a.code === le.PEGIN_NOT_FOUND))
|
|
95
|
-
throw a;
|
|
96
|
-
}
|
|
97
|
-
await new Promise((a, l) => {
|
|
98
|
-
const h = () => {
|
|
99
|
-
clearTimeout(c), l(
|
|
100
|
-
new Error(
|
|
101
|
-
`Polling aborted for pegin ${r.slice(0, 8)}… (target: ${[...n].join(", ")})`
|
|
102
|
-
)
|
|
103
|
-
);
|
|
104
|
-
}, c = setTimeout(() => {
|
|
105
|
-
s == null || s.removeEventListener("abort", h), a();
|
|
106
|
-
}, u);
|
|
107
|
-
s == null || s.addEventListener("abort", h, { once: !0 });
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
const Ee = 300 * 1e3, Te = /* @__PURE__ */ new Set([
|
|
112
|
-
w.PENDING_DEPOSITOR_WOTS_PK,
|
|
113
|
-
...z
|
|
114
|
-
]);
|
|
115
|
-
async function dt(e) {
|
|
116
|
-
const {
|
|
117
|
-
statusReader: t,
|
|
118
|
-
wotsSubmitter: r,
|
|
119
|
-
peginTxid: n,
|
|
120
|
-
depositorPk: i,
|
|
121
|
-
wotsPublicKeys: u,
|
|
122
|
-
timeoutMs: s = Ee,
|
|
123
|
-
signal: o
|
|
124
|
-
} = e;
|
|
125
|
-
o == null || o.throwIfAborted();
|
|
126
|
-
const a = await G({
|
|
127
|
-
statusReader: t,
|
|
128
|
-
peginTxid: n,
|
|
129
|
-
targetStatuses: Te,
|
|
130
|
-
timeoutMs: s,
|
|
131
|
-
signal: o
|
|
132
|
-
});
|
|
133
|
-
z.has(a) || (o == null || o.throwIfAborted(), await r.submitDepositorWotsKey(
|
|
134
|
-
{
|
|
135
|
-
pegin_txid: n,
|
|
136
|
-
depositor_pk: i,
|
|
137
|
-
wots_public_keys: u
|
|
138
|
-
},
|
|
139
|
-
o
|
|
140
|
-
));
|
|
141
|
-
}
|
|
142
|
-
const K = 1, ke = 1;
|
|
143
|
-
function _e(e, t) {
|
|
144
|
-
const r = p(t).toLowerCase(), i = e.map((u) => p(u).toLowerCase()).filter((u) => u !== r);
|
|
145
|
-
if (i.length === 0)
|
|
146
|
-
throw new Error(
|
|
147
|
-
"Cannot derive localChallengers: vault keeper set is empty (or contains only the depositor)"
|
|
148
|
-
);
|
|
149
|
-
if (new Set(i).size !== i.length)
|
|
150
|
-
throw new Error(
|
|
151
|
-
"Cannot derive localChallengers: duplicate vaultKeeper key — signing context is misconfigured"
|
|
152
|
-
);
|
|
153
|
-
return i;
|
|
154
|
-
}
|
|
155
|
-
function Se(e, t, r) {
|
|
156
|
-
const n = r.map(
|
|
157
|
-
(c) => p(c).toLowerCase()
|
|
158
|
-
), i = t.filter((c) => n.includes(c));
|
|
159
|
-
if (i.length > 0)
|
|
160
|
-
throw new Error(
|
|
161
|
-
`Cannot validate challenger set: vault keepers and universal challengers overlap (${i.join(", ")})`
|
|
162
|
-
);
|
|
163
|
-
const u = [...t, ...n], s = e.map(
|
|
164
|
-
(c) => p(c.challenger_pubkey).toLowerCase()
|
|
165
|
-
), o = new Set(s);
|
|
166
|
-
if (o.size !== s.length)
|
|
167
|
-
throw new Error(
|
|
168
|
-
"Depositor graph contains duplicate challenger entries in challenger_presign_data"
|
|
169
|
-
);
|
|
170
|
-
const a = new Set(u), l = u.filter((c) => !o.has(c)), h = s.filter((c) => !a.has(c));
|
|
171
|
-
if (l.length > 0 || h.length > 0)
|
|
172
|
-
throw new Error(
|
|
173
|
-
"Depositor graph challenger set does not match expected (local ∪ universal)" + (l.length > 0 ? ` (missing: ${l.join(", ")})` : "") + (h.length > 0 ? ` (unexpected: ${h.join(", ")})` : "")
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
function Ce(e, t) {
|
|
177
|
-
const r = e.ins[t];
|
|
178
|
-
return X(new Uint8Array(r.hash).slice().reverse());
|
|
179
|
-
}
|
|
180
|
-
function B(e, t, r, n, i) {
|
|
181
|
-
const u = e.ins[t];
|
|
182
|
-
if (u.index !== 0)
|
|
183
|
-
throw new Error(
|
|
184
|
-
`NoPayout (challenger ${i}) input ${t} expected to spend ${n} vout 0, got vout ${u.index}`
|
|
185
|
-
);
|
|
186
|
-
const s = r.getId(), o = Ce(e, t);
|
|
187
|
-
if (o !== s)
|
|
188
|
-
throw new Error(
|
|
189
|
-
`NoPayout (challenger ${i}) input ${t} does not reference ${n} (expected txid ${s}, got ${o})`
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
async function Ae(e, t, r) {
|
|
193
|
-
const n = [], i = [], u = [], s = _e(
|
|
194
|
-
r.vaultKeeperBtcPubkeys,
|
|
195
|
-
r.depositorBtcPubkey
|
|
196
|
-
);
|
|
197
|
-
Se(
|
|
198
|
-
e.challenger_presign_data,
|
|
199
|
-
s,
|
|
200
|
-
r.universalChallengerBtcPubkeys
|
|
201
|
-
);
|
|
202
|
-
const o = await he({
|
|
203
|
-
payoutTxHex: e.payout_tx.tx_hex,
|
|
204
|
-
peginTxHex: r.peginTxHex,
|
|
205
|
-
assertTxHex: e.assert_tx.tx_hex,
|
|
206
|
-
depositorBtcPubkey: r.depositorBtcPubkey,
|
|
207
|
-
vaultProviderBtcPubkey: r.vaultProviderBtcPubkey,
|
|
208
|
-
vaultKeeperBtcPubkeys: r.vaultKeeperBtcPubkeys,
|
|
209
|
-
universalChallengerBtcPubkeys: r.universalChallengerBtcPubkeys,
|
|
210
|
-
timelockPegin: r.timelockPegin,
|
|
211
|
-
network: r.network,
|
|
212
|
-
claimerBtcPubkey: r.depositorBtcPubkey,
|
|
213
|
-
registeredPayoutScriptPubKey: r.registeredPayoutScriptPubKey,
|
|
214
|
-
commissionBps: ke
|
|
215
|
-
});
|
|
216
|
-
n.push(o.psbtHex), i.push(
|
|
217
|
-
O(
|
|
218
|
-
t,
|
|
219
|
-
K
|
|
220
|
-
)
|
|
221
|
-
);
|
|
222
|
-
const a = p(r.depositorBtcPubkey), l = S.fromHex(
|
|
223
|
-
p(e.assert_tx.tx_hex)
|
|
224
|
-
);
|
|
225
|
-
for (const h of e.challenger_presign_data) {
|
|
226
|
-
const c = p(h.challenger_pubkey), d = n.length, P = await $e({
|
|
227
|
-
challenger: h,
|
|
228
|
-
challengerPubkey: c,
|
|
229
|
-
claimerPubkey: a,
|
|
230
|
-
localChallengers: s,
|
|
231
|
-
assertTxParsed: l,
|
|
232
|
-
ctx: r
|
|
233
|
-
});
|
|
234
|
-
n.push(P), i.push(
|
|
235
|
-
O(
|
|
236
|
-
t,
|
|
237
|
-
K
|
|
238
|
-
)
|
|
239
|
-
), u.push({
|
|
240
|
-
challengerPubkey: c,
|
|
241
|
-
noPayoutIdx: d
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
return { psbtHexes: n, signOptions: i, challengerEntries: u };
|
|
245
|
-
}
|
|
246
|
-
async function $e(e) {
|
|
247
|
-
const {
|
|
248
|
-
challenger: t,
|
|
249
|
-
challengerPubkey: r,
|
|
250
|
-
claimerPubkey: n,
|
|
251
|
-
localChallengers: i,
|
|
252
|
-
assertTxParsed: u,
|
|
253
|
-
ctx: s
|
|
254
|
-
} = e;
|
|
255
|
-
de(
|
|
256
|
-
t.nopayout_tx.tx_hex,
|
|
257
|
-
r,
|
|
258
|
-
s.network
|
|
259
|
-
);
|
|
260
|
-
const o = S.fromHex(
|
|
261
|
-
p(t.nopayout_tx.tx_hex)
|
|
262
|
-
), a = S.fromHex(
|
|
263
|
-
p(t.challenge_assert_x_tx.tx_hex)
|
|
264
|
-
), l = S.fromHex(
|
|
265
|
-
p(t.challenge_assert_y_tx.tx_hex)
|
|
266
|
-
);
|
|
267
|
-
if (o.ins.length !== 3)
|
|
268
|
-
throw new Error(
|
|
269
|
-
`NoPayout (challenger ${r}) must have exactly 3 inputs, got ${o.ins.length}`
|
|
270
|
-
);
|
|
271
|
-
B(
|
|
272
|
-
o,
|
|
273
|
-
0,
|
|
274
|
-
u,
|
|
275
|
-
"Assert",
|
|
276
|
-
r
|
|
277
|
-
), B(
|
|
278
|
-
o,
|
|
279
|
-
1,
|
|
280
|
-
a,
|
|
281
|
-
"ChallengeAssertX",
|
|
282
|
-
r
|
|
283
|
-
), B(
|
|
284
|
-
o,
|
|
285
|
-
2,
|
|
286
|
-
l,
|
|
287
|
-
"ChallengeAssertY",
|
|
288
|
-
r
|
|
289
|
-
);
|
|
290
|
-
const h = [
|
|
291
|
-
u.outs[0],
|
|
292
|
-
a.outs[0],
|
|
293
|
-
l.outs[0]
|
|
294
|
-
].map((c) => ({
|
|
295
|
-
script_pubkey: X(new Uint8Array(c.script)),
|
|
296
|
-
value: c.value
|
|
297
|
-
}));
|
|
298
|
-
return pe({
|
|
299
|
-
noPayoutTxHex: t.nopayout_tx.tx_hex,
|
|
300
|
-
challengerPubkey: r,
|
|
301
|
-
prevouts: h,
|
|
302
|
-
connectorParams: {
|
|
303
|
-
claimer: n,
|
|
304
|
-
localChallengers: i,
|
|
305
|
-
universalChallengers: s.universalChallengerBtcPubkeys,
|
|
306
|
-
timelockAssert: s.timelockAssert,
|
|
307
|
-
councilMembers: s.councilMembers,
|
|
308
|
-
councilQuorum: s.councilQuorum
|
|
309
|
-
}
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
function Re(e, t, r) {
|
|
313
|
-
V(e[0]);
|
|
314
|
-
const i = N(
|
|
315
|
-
e[0].returnedPsbtHex,
|
|
316
|
-
r
|
|
317
|
-
);
|
|
318
|
-
H({
|
|
319
|
-
requestedPsbtHex: e[0].requestedPsbtHex,
|
|
320
|
-
signatureHex: i,
|
|
321
|
-
signerXOnlyPubkeyHex: r,
|
|
322
|
-
inputIndex: 0
|
|
323
|
-
});
|
|
324
|
-
const u = {};
|
|
325
|
-
for (const s of t) {
|
|
326
|
-
V(e[s.noPayoutIdx]);
|
|
327
|
-
const o = N(
|
|
328
|
-
e[s.noPayoutIdx].returnedPsbtHex,
|
|
329
|
-
r
|
|
330
|
-
);
|
|
331
|
-
H({
|
|
332
|
-
requestedPsbtHex: e[s.noPayoutIdx].requestedPsbtHex,
|
|
333
|
-
signatureHex: o,
|
|
334
|
-
signerXOnlyPubkeyHex: r,
|
|
335
|
-
inputIndex: 0
|
|
336
|
-
}), u[s.challengerPubkey] = {
|
|
337
|
-
nopayout_signature: o
|
|
338
|
-
};
|
|
339
|
-
}
|
|
340
|
-
return {
|
|
341
|
-
payout_signatures: {
|
|
342
|
-
payout_signature: i
|
|
343
|
-
},
|
|
344
|
-
per_challenger: u
|
|
345
|
-
};
|
|
346
|
-
}
|
|
347
|
-
async function Be(e, t, r) {
|
|
348
|
-
if (typeof e.signPsbts == "function")
|
|
349
|
-
return e.signPsbts(t, r);
|
|
350
|
-
const n = [];
|
|
351
|
-
for (let i = 0; i < t.length; i++)
|
|
352
|
-
n.push(await e.signPsbt(t[i], r == null ? void 0 : r[i]));
|
|
353
|
-
return n;
|
|
354
|
-
}
|
|
355
|
-
async function Ie(e) {
|
|
356
|
-
const { depositorGraph: t, btcWallet: r, signingContext: n } = e, i = await r.getPublicKeyHex(), { depositorPubkey: u } = ne(
|
|
357
|
-
i,
|
|
358
|
-
p(n.depositorBtcPubkey)
|
|
359
|
-
), { psbtHexes: s, signOptions: o, challengerEntries: a } = await Ae(
|
|
360
|
-
t,
|
|
361
|
-
i,
|
|
362
|
-
n
|
|
363
|
-
), l = await Be(
|
|
364
|
-
r,
|
|
365
|
-
s,
|
|
366
|
-
o
|
|
367
|
-
);
|
|
368
|
-
if (l.length !== s.length)
|
|
369
|
-
throw new Error(
|
|
370
|
-
`Wallet returned ${l.length} signed PSBTs, expected ${s.length}`
|
|
371
|
-
);
|
|
372
|
-
const h = s.map((c, d) => ({
|
|
373
|
-
requestedPsbtHex: c,
|
|
374
|
-
returnedPsbtHex: l[d]
|
|
375
|
-
}));
|
|
376
|
-
return Re(
|
|
377
|
-
h,
|
|
378
|
-
a,
|
|
379
|
-
u
|
|
380
|
-
);
|
|
381
|
-
}
|
|
382
|
-
const Ve = 1200 * 1e3, q = /* @__PURE__ */ new Set([
|
|
383
|
-
w.PENDING_ACKS,
|
|
384
|
-
w.PENDING_ACTIVATION,
|
|
385
|
-
w.ACTIVATED_PENDING_BROADCAST,
|
|
386
|
-
w.ACTIVATED
|
|
387
|
-
]), Ne = /* @__PURE__ */ new Set([
|
|
388
|
-
w.PENDING_DEPOSITOR_SIGNATURES,
|
|
389
|
-
...q
|
|
390
|
-
]);
|
|
391
|
-
function He(e) {
|
|
392
|
-
return e.map((t) => ({
|
|
393
|
-
claimerPubkeyXOnly: A(t.claimer_pubkey),
|
|
394
|
-
payoutTxHex: t.payout_tx.tx_hex,
|
|
395
|
-
assertTxHex: t.assert_tx.tx_hex
|
|
396
|
-
}));
|
|
397
|
-
}
|
|
398
|
-
function k(e) {
|
|
399
|
-
return A(e).toLowerCase();
|
|
400
|
-
}
|
|
401
|
-
function Oe(e, t, r, n) {
|
|
402
|
-
const i = k(n), u = [
|
|
403
|
-
k(t),
|
|
404
|
-
...r.map(k)
|
|
405
|
-
], s = new Set(u);
|
|
406
|
-
if (s.size !== u.length)
|
|
407
|
-
throw new Error(
|
|
408
|
-
"Cannot validate claimer set: signing context contains duplicate vault provider or vault keeper key"
|
|
409
|
-
);
|
|
410
|
-
if (s.has(i))
|
|
411
|
-
throw new Error(
|
|
412
|
-
"Cannot validate claimer set: depositor key overlaps with vault provider or vault keeper set"
|
|
413
|
-
);
|
|
414
|
-
const o = e.map(
|
|
415
|
-
(d) => k(d.claimer_pubkey)
|
|
416
|
-
);
|
|
417
|
-
if (new Set(o).size !== o.length)
|
|
418
|
-
throw new Error(
|
|
419
|
-
"Presign response contains duplicate claimer entries"
|
|
420
|
-
);
|
|
421
|
-
const a = o.filter((d) => d !== i), l = new Set(a), h = u.filter((d) => !l.has(d)), c = a.filter((d) => !s.has(d));
|
|
422
|
-
if (h.length > 0 || c.length > 0)
|
|
423
|
-
throw new Error(
|
|
424
|
-
"Presign response claimer set does not match expected (vault provider ∪ vault keepers)" + (h.length > 0 ? ` (missing: ${h.join(", ")})` : "") + (c.length > 0 ? ` (unexpected: ${c.join(", ")})` : "")
|
|
425
|
-
);
|
|
426
|
-
}
|
|
427
|
-
function F(e, t) {
|
|
428
|
-
return {
|
|
429
|
-
payoutTxHex: e.payoutTxHex,
|
|
430
|
-
peginTxHex: t.peginTxHex,
|
|
431
|
-
assertTxHex: e.assertTxHex,
|
|
432
|
-
vaultProviderBtcPubkey: t.vaultProviderBtcPubkey,
|
|
433
|
-
vaultKeeperBtcPubkeys: t.vaultKeeperBtcPubkeys,
|
|
434
|
-
universalChallengerBtcPubkeys: t.universalChallengerBtcPubkeys,
|
|
435
|
-
depositorBtcPubkey: t.depositorBtcPubkey,
|
|
436
|
-
timelockPegin: t.timelockPegin,
|
|
437
|
-
registeredPayoutScriptPubKey: t.registeredPayoutScriptPubKey,
|
|
438
|
-
claimerBtcPubkey: e.claimerPubkeyXOnly,
|
|
439
|
-
commissionBps: t.commissionBps
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
async function De(e, t, r, n) {
|
|
443
|
-
const i = new ae({
|
|
444
|
-
network: t.network,
|
|
445
|
-
btcWallet: e
|
|
446
|
-
}), u = r.length;
|
|
447
|
-
n == null || n(0, u);
|
|
448
|
-
let s;
|
|
449
|
-
if (i.supportsBatchSigning())
|
|
450
|
-
s = (await i.signPayoutTransactionsBatch(
|
|
451
|
-
r.map((l) => F(l, t))
|
|
452
|
-
)).map((l) => l.payoutSignature);
|
|
453
|
-
else {
|
|
454
|
-
s = [];
|
|
455
|
-
for (let a = 0; a < r.length; a++) {
|
|
456
|
-
n == null || n(a, u);
|
|
457
|
-
const l = await i.signPayoutTransaction(
|
|
458
|
-
F(r[a], t)
|
|
459
|
-
);
|
|
460
|
-
s.push(l.signature);
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
const o = {};
|
|
464
|
-
for (let a = 0; a < r.length; a++)
|
|
465
|
-
o[r[a].claimerPubkeyXOnly] = {
|
|
466
|
-
payout_signature: s[a]
|
|
467
|
-
};
|
|
468
|
-
return n == null || n(u, u), o;
|
|
469
|
-
}
|
|
470
|
-
async function pt(e) {
|
|
471
|
-
const {
|
|
472
|
-
statusReader: t,
|
|
473
|
-
presignClient: r,
|
|
474
|
-
btcWallet: n,
|
|
475
|
-
peginTxid: i,
|
|
476
|
-
depositorPk: u,
|
|
477
|
-
signingContext: s,
|
|
478
|
-
timeoutMs: o = Ve,
|
|
479
|
-
signal: a,
|
|
480
|
-
onProgress: l
|
|
481
|
-
} = e, h = await G({
|
|
482
|
-
statusReader: t,
|
|
483
|
-
peginTxid: i,
|
|
484
|
-
targetStatuses: Ne,
|
|
485
|
-
timeoutMs: o,
|
|
486
|
-
signal: a
|
|
487
|
-
});
|
|
488
|
-
if (q.has(h))
|
|
489
|
-
return;
|
|
490
|
-
a == null || a.throwIfAborted();
|
|
491
|
-
const c = await r.requestDepositorPresignTransactions(
|
|
492
|
-
{
|
|
493
|
-
pegin_txid: i,
|
|
494
|
-
depositor_pk: u
|
|
495
|
-
},
|
|
496
|
-
a
|
|
497
|
-
);
|
|
498
|
-
a == null || a.throwIfAborted();
|
|
499
|
-
const d = k(u);
|
|
500
|
-
Oe(
|
|
501
|
-
c.txs,
|
|
502
|
-
s.vaultProviderBtcPubkey,
|
|
503
|
-
s.vaultKeeperBtcPubkeys,
|
|
504
|
-
u
|
|
505
|
-
);
|
|
506
|
-
const P = c.txs.filter(
|
|
507
|
-
(b) => k(b.claimer_pubkey) !== d
|
|
508
|
-
), f = He(P), y = await De(
|
|
509
|
-
n,
|
|
510
|
-
s,
|
|
511
|
-
f,
|
|
512
|
-
l
|
|
513
|
-
);
|
|
514
|
-
a == null || a.throwIfAborted();
|
|
515
|
-
const x = await Ie({
|
|
516
|
-
depositorGraph: c.depositor_graph,
|
|
517
|
-
btcWallet: n,
|
|
518
|
-
signingContext: {
|
|
519
|
-
peginTxHex: s.peginTxHex,
|
|
520
|
-
depositorBtcPubkey: u,
|
|
521
|
-
vaultProviderBtcPubkey: s.vaultProviderBtcPubkey,
|
|
522
|
-
vaultKeeperBtcPubkeys: s.vaultKeeperBtcPubkeys,
|
|
523
|
-
universalChallengerBtcPubkeys: s.universalChallengerBtcPubkeys,
|
|
524
|
-
timelockPegin: s.timelockPegin,
|
|
525
|
-
timelockAssert: s.timelockAssert,
|
|
526
|
-
councilMembers: s.councilMembers,
|
|
527
|
-
councilQuorum: s.councilQuorum,
|
|
528
|
-
network: s.network,
|
|
529
|
-
registeredPayoutScriptPubKey: s.registeredPayoutScriptPubKey
|
|
530
|
-
}
|
|
531
|
-
});
|
|
532
|
-
a == null || a.throwIfAborted();
|
|
533
|
-
const v = { ...y };
|
|
534
|
-
v[p(u)] = x.payout_signatures, await r.submitDepositorPresignatures(
|
|
535
|
-
{
|
|
536
|
-
pegin_txid: i,
|
|
537
|
-
depositor_pk: u,
|
|
538
|
-
signatures: v,
|
|
539
|
-
depositor_claimer_presignatures: x
|
|
540
|
-
},
|
|
541
|
-
a
|
|
542
|
-
);
|
|
543
|
-
}
|
|
544
|
-
function Ke(e) {
|
|
545
|
-
return /^[0-9a-fA-F]{64}$/.test(e);
|
|
546
|
-
}
|
|
547
|
-
function gt(e) {
|
|
548
|
-
const {
|
|
549
|
-
amountSats: t,
|
|
550
|
-
minDeposit: r,
|
|
551
|
-
maxDeposit: n,
|
|
552
|
-
btcBalance: i,
|
|
553
|
-
estimatedFeeSats: u,
|
|
554
|
-
depositorClaimValue: s
|
|
555
|
-
} = e;
|
|
556
|
-
return !(t <= 0n || t < r || n && n > 0n && t > n || u == null || s == null || t + u + s > i);
|
|
557
|
-
}
|
|
558
|
-
function ft(e, t, r) {
|
|
559
|
-
return e <= 0n ? {
|
|
560
|
-
valid: !1,
|
|
561
|
-
error: "Deposit amount must be greater than zero"
|
|
562
|
-
} : e < t ? {
|
|
563
|
-
valid: !1,
|
|
564
|
-
error: `Minimum deposit is ${T(t)} BTC`
|
|
565
|
-
} : r && r > 0n && e > r ? {
|
|
566
|
-
valid: !1,
|
|
567
|
-
error: `Maximum deposit is ${T(r)} BTC`
|
|
568
|
-
} : { valid: !0 };
|
|
569
|
-
}
|
|
570
|
-
function mt(e) {
|
|
571
|
-
const { amount: t, effectiveRemaining: r } = e;
|
|
572
|
-
return r === null ? { valid: !0 } : r === 0n ? {
|
|
573
|
-
valid: !1,
|
|
574
|
-
error: "Supply cap reached — deposits temporarily paused"
|
|
575
|
-
} : t > r ? {
|
|
576
|
-
valid: !1,
|
|
577
|
-
error: `Vault size exceeds remaining capacity (${T(r)} BTC)`
|
|
578
|
-
} : { valid: !0 };
|
|
579
|
-
}
|
|
580
|
-
function Pt(e, t) {
|
|
581
|
-
if (!e || e.length === 0)
|
|
582
|
-
return {
|
|
583
|
-
valid: !1,
|
|
584
|
-
error: "At least one vault provider must be selected"
|
|
585
|
-
};
|
|
586
|
-
const r = t.map(
|
|
587
|
-
(i) => i.toLowerCase()
|
|
588
|
-
);
|
|
589
|
-
return e.filter(
|
|
590
|
-
(i) => !r.includes(i.toLowerCase())
|
|
591
|
-
).length > 0 ? {
|
|
592
|
-
valid: !1,
|
|
593
|
-
error: "Invalid vault provider selected"
|
|
594
|
-
} : { valid: !0 };
|
|
595
|
-
}
|
|
596
|
-
function Fe(e, t, r) {
|
|
597
|
-
if (!e || e.length === 0)
|
|
598
|
-
return {
|
|
599
|
-
valid: !1,
|
|
600
|
-
error: "At least one vault amount required"
|
|
601
|
-
};
|
|
602
|
-
for (let n = 0; n < e.length; n++) {
|
|
603
|
-
const i = e[n];
|
|
604
|
-
if (i <= 0n)
|
|
605
|
-
return {
|
|
606
|
-
valid: !1,
|
|
607
|
-
error: `Vault ${n + 1} amount must be positive`
|
|
608
|
-
};
|
|
609
|
-
if (t && i < t)
|
|
610
|
-
return {
|
|
611
|
-
valid: !1,
|
|
612
|
-
error: `Vault ${n + 1} amount ${T(i)} BTC is below minimum deposit ${T(t)} BTC`
|
|
613
|
-
};
|
|
614
|
-
if (r && i > r)
|
|
615
|
-
return {
|
|
616
|
-
valid: !1,
|
|
617
|
-
error: `Vault ${n + 1} amount ${T(i)} BTC exceeds maximum deposit ${T(r)} BTC`
|
|
618
|
-
};
|
|
619
|
-
}
|
|
620
|
-
return { valid: !0 };
|
|
621
|
-
}
|
|
622
|
-
function Ue(e) {
|
|
623
|
-
const t = p(e);
|
|
624
|
-
return Ke(t) ? { valid: !0 } : {
|
|
625
|
-
valid: !1,
|
|
626
|
-
error: "Invalid pubkey format: must be 64 hex characters (32-byte x-only public key, no 0x prefix)"
|
|
627
|
-
};
|
|
628
|
-
}
|
|
629
|
-
function Me(e) {
|
|
630
|
-
if (!e || e.length === 0)
|
|
631
|
-
throw new Error(
|
|
632
|
-
"No vault keepers available. The system requires at least one vault keeper to create a deposit."
|
|
633
|
-
);
|
|
634
|
-
}
|
|
635
|
-
function Le(e) {
|
|
636
|
-
if (!e || e.length === 0)
|
|
637
|
-
throw new Error(
|
|
638
|
-
"No universal challengers available. The system requires at least one universal challenger to create a deposit."
|
|
639
|
-
);
|
|
640
|
-
}
|
|
641
|
-
function Xe(e) {
|
|
642
|
-
if (e.length === 0)
|
|
643
|
-
throw new Error("No spendable UTXOs available");
|
|
644
|
-
}
|
|
645
|
-
function bt(e) {
|
|
646
|
-
const {
|
|
647
|
-
vaultAmounts: t,
|
|
648
|
-
confirmedUTXOs: r,
|
|
649
|
-
vaultProviderBtcPubkey: n,
|
|
650
|
-
vaultKeeperBtcPubkeys: i,
|
|
651
|
-
universalChallengerBtcPubkeys: u,
|
|
652
|
-
minDeposit: s,
|
|
653
|
-
maxDeposit: o
|
|
654
|
-
} = e, a = Fe(
|
|
655
|
-
t,
|
|
656
|
-
s,
|
|
657
|
-
o
|
|
658
|
-
);
|
|
659
|
-
if (!a.valid)
|
|
660
|
-
throw new Error(a.error);
|
|
661
|
-
const l = Ue(n);
|
|
662
|
-
if (!l.valid)
|
|
663
|
-
throw new Error(l.error);
|
|
664
|
-
Me(i), Le(u), Xe(r);
|
|
665
|
-
}
|
|
666
|
-
async function yt(e) {
|
|
667
|
-
const {
|
|
668
|
-
vaultRegistryReader: t,
|
|
669
|
-
vaultKeeperReader: r,
|
|
670
|
-
universalChallengerReader: n,
|
|
671
|
-
vaultProviderEthAddress: i,
|
|
672
|
-
applicationEntryPoint: u,
|
|
673
|
-
expectedVaultProviderBtcPubkey: s,
|
|
674
|
-
expectedVaultKeeperBtcPubkeys: o,
|
|
675
|
-
expectedUniversalChallengerBtcPubkeys: a
|
|
676
|
-
} = e, [
|
|
677
|
-
l,
|
|
678
|
-
h,
|
|
679
|
-
c
|
|
680
|
-
] = await Promise.all([
|
|
681
|
-
t.getVaultProviderBtcPubKey(i),
|
|
682
|
-
r.getCurrentVaultKeepersVersion(u),
|
|
683
|
-
n.getLatestUniversalChallengersVersion()
|
|
684
|
-
]), [d, P] = await Promise.all([
|
|
685
|
-
r.getVaultKeepersByVersion(
|
|
686
|
-
u,
|
|
687
|
-
h
|
|
688
|
-
),
|
|
689
|
-
n.getUniversalChallengersByVersion(
|
|
690
|
-
c
|
|
691
|
-
)
|
|
692
|
-
]), f = (g) => A(g).toLowerCase(), y = (g) => g.map(f).sort();
|
|
693
|
-
if (f(s) !== l)
|
|
694
|
-
throw new Error(
|
|
695
|
-
`Vault provider BTC pubkey indexer hint does not match BTCVaultRegistry for ${i}. Refresh and try again.`
|
|
696
|
-
);
|
|
697
|
-
const v = y(o), b = y(
|
|
698
|
-
d.map((g) => g.btcPubKey)
|
|
699
|
-
);
|
|
700
|
-
if (v.length !== b.length || v.some((g, _) => g !== b[_]))
|
|
701
|
-
throw new Error(
|
|
702
|
-
`Vault keeper BTC pubkeys (v${h}) indexer set does not match ApplicationRegistry on-chain set. Refresh and try again.`
|
|
703
|
-
);
|
|
704
|
-
const C = y(a), E = y(
|
|
705
|
-
P.map((g) => g.btcPubKey)
|
|
706
|
-
);
|
|
707
|
-
if (C.length !== E.length || C.some((g, _) => g !== E[_]))
|
|
708
|
-
throw new Error(
|
|
709
|
-
`Universal challenger BTC pubkeys (v${c}) indexer set does not match ProtocolParams on-chain set. Refresh and try again.`
|
|
710
|
-
);
|
|
711
|
-
return {
|
|
712
|
-
vaultProviderBtcPubkeyXOnly: l,
|
|
713
|
-
vaultKeeperBtcPubkeysSorted: b,
|
|
714
|
-
universalChallengerBtcPubkeysSorted: E,
|
|
715
|
-
expectedAppVaultKeepersVersion: h,
|
|
716
|
-
expectedUniversalChallengersVersion: c
|
|
717
|
-
};
|
|
718
|
-
}
|
|
719
|
-
class j extends Error {
|
|
720
|
-
constructor(t) {
|
|
721
|
-
super(t), this.name = "RegisteredVaultVersionMismatchError";
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
function wt(e) {
|
|
725
|
-
return e instanceof j || e instanceof Error && e.name === "RegisteredVaultVersionMismatchError";
|
|
726
|
-
}
|
|
727
|
-
async function xt(e) {
|
|
728
|
-
const {
|
|
729
|
-
vaultRegistryReader: t,
|
|
730
|
-
vaultIds: r,
|
|
731
|
-
expectedOffchainParamsVersion: n,
|
|
732
|
-
expectedAppVaultKeepersVersion: i,
|
|
733
|
-
expectedUniversalChallengersVersion: u
|
|
734
|
-
} = e, s = await t.getProtocolInfoBatch(r), o = [];
|
|
735
|
-
if (s.forEach((a, l) => {
|
|
736
|
-
const h = r[l];
|
|
737
|
-
a.offchainParamsVersion !== n && o.push(
|
|
738
|
-
`vault ${h}: offchainParams expected v${n}, got v${a.offchainParamsVersion}`
|
|
739
|
-
), a.appVaultKeepersVersion !== i && o.push(
|
|
740
|
-
`vault ${h}: appVaultKeepers expected v${i}, got v${a.appVaultKeepersVersion}`
|
|
741
|
-
), a.universalChallengersVersion !== u && o.push(
|
|
742
|
-
`vault ${h}: universalChallengers expected v${u}, got v${a.universalChallengersVersion}`
|
|
743
|
-
);
|
|
744
|
-
}), o.length > 0)
|
|
745
|
-
throw new j(
|
|
746
|
-
`Aborting BTC broadcast: signer-set or offchain-params versions changed during registration (${o.join("; ")}). The Pre-PegIn was not broadcast; the registered ETH vault will time out per protocol rules.`
|
|
747
|
-
);
|
|
748
|
-
}
|
|
749
|
-
var Q = /* @__PURE__ */ ((e) => (e.CLAIM_EVENT_RECEIVED = "ClaimEventReceived", e.CLAIM_BROADCAST = "ClaimBroadcast", e.ASSERT_BROADCAST = "AssertBroadcast", e.PAYOUT_BROADCAST = "PayoutBroadcast", e.PAYOUT_BLOCKED = "PayoutBlocked", e))(Q || {});
|
|
750
|
-
const ze = /* @__PURE__ */ new Set([
|
|
751
|
-
"PayoutBroadcast",
|
|
752
|
-
"PayoutBlocked"
|
|
753
|
-
/* PAYOUT_BLOCKED */
|
|
754
|
-
]);
|
|
755
|
-
function vt(e) {
|
|
756
|
-
return Object.values(Q).includes(
|
|
757
|
-
e
|
|
758
|
-
);
|
|
759
|
-
}
|
|
760
|
-
function Et(e) {
|
|
761
|
-
return !!e && ze.has(e);
|
|
762
|
-
}
|
|
763
|
-
class Ge extends Error {
|
|
764
|
-
constructor(r, n) {
|
|
765
|
-
super(`Refund not yet mature (BIP68 not final): ${n.message}`);
|
|
766
|
-
$(this, "vaultId");
|
|
767
|
-
$(this, "cause");
|
|
768
|
-
this.name = "BIP68NotMatureError", this.vaultId = r, this.cause = n;
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
|
-
const qe = /^0x[0-9a-fA-F]{64}$/, je = /^(?:0x)?(?:[0-9a-fA-F]{2})+$/, W = /^(?:0x)?(?:[0-9a-fA-F]{64}|[0-9a-fA-F]{66})$/, Y = 160, U = 2e3, M = 10n, L = 100n;
|
|
772
|
-
function Tt(e) {
|
|
773
|
-
if (!Number.isFinite(e) || e <= 0)
|
|
774
|
-
throw new Error(
|
|
775
|
-
`feeRateSatsVb must be a positive finite number, got ${e}`
|
|
776
|
-
);
|
|
777
|
-
return BigInt(Math.ceil(e * Y));
|
|
778
|
-
}
|
|
779
|
-
const Qe = 1, We = /non-BIP68-final/i;
|
|
780
|
-
function D(e, t) {
|
|
781
|
-
if (e.length !== 66)
|
|
782
|
-
throw new Error(
|
|
783
|
-
`${t} must be 32 bytes (66 hex chars with 0x prefix), got length ${e.length}`
|
|
784
|
-
);
|
|
785
|
-
if (!qe.test(e))
|
|
786
|
-
throw new Error(
|
|
787
|
-
`${t} must contain only hex characters after the 0x prefix`
|
|
788
|
-
);
|
|
789
|
-
}
|
|
790
|
-
function I(e, t) {
|
|
791
|
-
if (!Number.isInteger(e) || e < 0)
|
|
792
|
-
throw new Error(`${t} must be a non-negative integer, got ${e}`);
|
|
793
|
-
}
|
|
794
|
-
function Ye(e) {
|
|
795
|
-
if (D(e.hashlock, "hashlock"), !Number.isInteger(e.htlcVout) || e.htlcVout < 0)
|
|
796
|
-
throw new Error(
|
|
797
|
-
`htlcVout must be a non-negative integer, got ${e.htlcVout}`
|
|
798
|
-
);
|
|
799
|
-
if (!Array.isArray(e.batch) || e.batch.length === 0)
|
|
800
|
-
throw new Error("batch must be a non-empty array of HTLC entries");
|
|
801
|
-
if (e.htlcVout >= e.batch.length)
|
|
802
|
-
throw new Error(
|
|
803
|
-
`htlcVout ${e.htlcVout} is out of range for batch of size ${e.batch.length}`
|
|
804
|
-
);
|
|
805
|
-
for (let r = 0; r < e.batch.length; r++) {
|
|
806
|
-
const n = e.batch[r];
|
|
807
|
-
if (D(n.hashlock, `batch[${r}].hashlock`), !Number.isInteger(n.htlcVout) || n.htlcVout !== r)
|
|
808
|
-
throw new Error(
|
|
809
|
-
`batch[${r}].htlcVout must equal ${r} (contiguous vout-ordered vector), got ${n.htlcVout}`
|
|
810
|
-
);
|
|
811
|
-
if (typeof n.amount != "bigint" || n.amount <= 0n)
|
|
812
|
-
throw new Error(
|
|
813
|
-
`batch[${r}].amount must be a positive bigint, got ${n.amount}`
|
|
814
|
-
);
|
|
815
|
-
}
|
|
816
|
-
const t = e.batch[e.htlcVout];
|
|
817
|
-
if (t.hashlock.toLowerCase() !== e.hashlock.toLowerCase())
|
|
818
|
-
throw new Error(
|
|
819
|
-
`batch[${e.htlcVout}].hashlock (${t.hashlock}) does not match target hashlock (${e.hashlock})`
|
|
820
|
-
);
|
|
821
|
-
if (t.amount !== e.amount)
|
|
822
|
-
throw new Error(
|
|
823
|
-
`batch[${e.htlcVout}].amount (${t.amount}) does not match target amount (${e.amount})`
|
|
824
|
-
);
|
|
825
|
-
if (I(e.offchainParamsVersion, "offchainParamsVersion"), I(e.appVaultKeepersVersion, "appVaultKeepersVersion"), I(
|
|
826
|
-
e.universalChallengersVersion,
|
|
827
|
-
"universalChallengersVersion"
|
|
828
|
-
), typeof e.unsignedPrePeginTxHex != "string" || e.unsignedPrePeginTxHex.length === 0)
|
|
829
|
-
throw new Error("unsignedPrePeginTxHex must be a non-empty hex string");
|
|
830
|
-
if (!je.test(e.unsignedPrePeginTxHex))
|
|
831
|
-
throw new Error(
|
|
832
|
-
"unsignedPrePeginTxHex must be a hex byte string (optional 0x prefix, even length)"
|
|
833
|
-
);
|
|
834
|
-
if (!e.depositorBtcPubkey || !W.test(e.depositorBtcPubkey))
|
|
835
|
-
throw new Error(
|
|
836
|
-
"depositorBtcPubkey must be 32 or 33 bytes of hex (optional 0x prefix)"
|
|
837
|
-
);
|
|
838
|
-
if (typeof e.amount != "bigint" || e.amount <= 0n)
|
|
839
|
-
throw new Error(`amount must be a positive bigint, got ${e.amount}`);
|
|
840
|
-
}
|
|
841
|
-
function Je(e) {
|
|
842
|
-
if (!e.vaultProviderPubkey || !W.test(e.vaultProviderPubkey))
|
|
843
|
-
throw new Error("vaultProviderPubkey must be 32 or 33 bytes of hex");
|
|
844
|
-
if (e.vaultKeeperPubkeys.length === 0)
|
|
845
|
-
throw new Error("vaultKeeperPubkeys must be non-empty");
|
|
846
|
-
if (e.universalChallengerPubkeys.length === 0)
|
|
847
|
-
throw new Error("universalChallengerPubkeys must be non-empty");
|
|
848
|
-
if (!Number.isInteger(e.timelockRefund) || e.timelockRefund <= 0)
|
|
849
|
-
throw new Error(
|
|
850
|
-
`timelockRefund must be a positive integer, got ${e.timelockRefund}`
|
|
851
|
-
);
|
|
852
|
-
if (typeof e.feeRate != "bigint" || e.feeRate <= 0n)
|
|
853
|
-
throw new Error(
|
|
854
|
-
`protocol feeRate must be a positive bigint, got ${e.feeRate}`
|
|
855
|
-
);
|
|
856
|
-
if (typeof e.minPeginFeeRate != "bigint" || e.minPeginFeeRate <= 0n)
|
|
857
|
-
throw new Error(
|
|
858
|
-
`minPeginFeeRate must be a positive bigint, got ${e.minPeginFeeRate}`
|
|
859
|
-
);
|
|
860
|
-
if (!Number.isInteger(e.numLocalChallengers) || e.numLocalChallengers < 0)
|
|
861
|
-
throw new Error("numLocalChallengers must be a non-negative integer");
|
|
862
|
-
if (!Number.isInteger(e.councilQuorum) || !Number.isInteger(e.councilSize) || e.councilQuorum <= 0 || e.councilSize <= 0 || e.councilQuorum > e.councilSize)
|
|
863
|
-
throw new Error(
|
|
864
|
-
`councilQuorum (${e.councilQuorum}) must be in [1, councilSize=${e.councilSize}]`
|
|
865
|
-
);
|
|
866
|
-
}
|
|
867
|
-
async function Ze(e, t) {
|
|
868
|
-
const r = await fe(
|
|
869
|
-
t.numLocalChallengers,
|
|
870
|
-
t.universalChallengerPubkeys.length,
|
|
871
|
-
t.councilQuorum,
|
|
872
|
-
t.councilSize,
|
|
873
|
-
t.feeRate
|
|
874
|
-
), n = await me(
|
|
875
|
-
t.vaultKeeperPubkeys.length,
|
|
876
|
-
t.universalChallengerPubkeys.length,
|
|
877
|
-
t.minPeginFeeRate
|
|
878
|
-
), i = r + n;
|
|
879
|
-
return e.map((u, s) => {
|
|
880
|
-
const o = u.amount - i;
|
|
881
|
-
if (o <= 0n)
|
|
882
|
-
throw new Error(
|
|
883
|
-
`Re-derived peginAmount for batch[${s}] is non-positive (${o}): HTLC value ${u.amount} does not exceed depositorClaimValue ${r} + minPeginFee ${n}. Refusing to build a refund from an inconsistent (amount, protocol params) pair.`
|
|
884
|
-
);
|
|
885
|
-
return o;
|
|
886
|
-
});
|
|
887
|
-
}
|
|
888
|
-
function et(e) {
|
|
889
|
-
const t = ce.fromHex(e);
|
|
890
|
-
try {
|
|
891
|
-
t.finalizeAllInputs();
|
|
892
|
-
} catch (r) {
|
|
893
|
-
const n = r instanceof Error ? r.message : String(r);
|
|
894
|
-
if (!n.includes("already finalized"))
|
|
895
|
-
throw new Error(`Failed to finalize refund PSBT: ${n}`);
|
|
896
|
-
}
|
|
897
|
-
return t.extractTransaction().toHex();
|
|
898
|
-
}
|
|
899
|
-
async function kt(e) {
|
|
900
|
-
const {
|
|
901
|
-
vaultId: t,
|
|
902
|
-
readVault: r,
|
|
903
|
-
readPrePeginContext: n,
|
|
904
|
-
feeRate: i,
|
|
905
|
-
signPsbt: u,
|
|
906
|
-
broadcastTx: s,
|
|
907
|
-
signal: o
|
|
908
|
-
} = e;
|
|
909
|
-
o == null || o.throwIfAborted(), D(t, "vaultId");
|
|
910
|
-
const a = await r();
|
|
911
|
-
Ye(a), o == null || o.throwIfAborted();
|
|
912
|
-
const l = await n(a);
|
|
913
|
-
if (Je(l), o == null || o.throwIfAborted(), !Number.isFinite(i) || i <= 0)
|
|
914
|
-
throw new Error(`feeRate must be a positive number, got ${i}`);
|
|
915
|
-
if (i > U)
|
|
916
|
-
throw new Error(
|
|
917
|
-
`feeRate ${i} sat/vB exceeds refund safety cap ${U} sat/vB; refusing to sign refund.`
|
|
918
|
-
);
|
|
919
|
-
const h = BigInt(Math.ceil(i * Y)), c = a.amount * M / L;
|
|
920
|
-
if (h > c)
|
|
921
|
-
throw new Error(
|
|
922
|
-
`Refund fee ${h} sats exceeds the per-vault safety cap of ${c} sats (${M}/${L} of vault.amount=${a.amount}); refusing to sign refund.`
|
|
923
|
-
);
|
|
924
|
-
o == null || o.throwIfAborted();
|
|
925
|
-
const d = A(
|
|
926
|
-
a.depositorBtcPubkey
|
|
927
|
-
), P = p(a.unsignedPrePeginTxHex), f = se(P);
|
|
928
|
-
if (f !== void 0 && f.vout !== a.batch.length)
|
|
929
|
-
throw new Error(
|
|
930
|
-
`Auth-anchor OP_RETURN at vout ${f.vout} does not match batch size (${a.batch.length} HTLC outputs expect the anchor at vout ${a.batch.length}). Refund refused — sibling HTLC vector is incomplete.`
|
|
931
|
-
);
|
|
932
|
-
const y = f == null ? void 0 : f.hash;
|
|
933
|
-
let x;
|
|
934
|
-
try {
|
|
935
|
-
x = S.fromHex(P);
|
|
936
|
-
} catch (m) {
|
|
937
|
-
throw new Error(
|
|
938
|
-
`Failed to parse funded Pre-PegIn transaction hex: ${m instanceof Error ? m.message : String(m)}`
|
|
939
|
-
);
|
|
940
|
-
}
|
|
941
|
-
if (x.outs.length < a.batch.length)
|
|
942
|
-
throw new Error(
|
|
943
|
-
`Funded Pre-PegIn tx has ${x.outs.length} outputs but batch requires at least ${a.batch.length} HTLC outputs. Refund refused — funded tx shape disagrees with sibling vector.`
|
|
944
|
-
);
|
|
945
|
-
const v = await Ze(a.batch, l);
|
|
946
|
-
o == null || o.throwIfAborted();
|
|
947
|
-
const { psbtHex: b } = await ge({
|
|
948
|
-
prePeginParams: {
|
|
949
|
-
depositorPubkey: d,
|
|
950
|
-
vaultProviderPubkey: p(l.vaultProviderPubkey),
|
|
951
|
-
vaultKeeperPubkeys: l.vaultKeeperPubkeys.map(p),
|
|
952
|
-
universalChallengerPubkeys: l.universalChallengerPubkeys.map(p),
|
|
953
|
-
hashlocks: a.batch.map((m) => p(m.hashlock)),
|
|
954
|
-
timelockRefund: l.timelockRefund,
|
|
955
|
-
pegInAmounts: v,
|
|
956
|
-
feeRate: l.feeRate,
|
|
957
|
-
minPeginFeeRate: l.minPeginFeeRate,
|
|
958
|
-
numLocalChallengers: l.numLocalChallengers,
|
|
959
|
-
councilQuorum: l.councilQuorum,
|
|
960
|
-
councilSize: l.councilSize,
|
|
961
|
-
network: l.network,
|
|
962
|
-
authAnchorHash: y
|
|
963
|
-
},
|
|
964
|
-
fundedPrePeginTxHex: P,
|
|
965
|
-
htlcVout: a.htlcVout,
|
|
966
|
-
refundFee: h,
|
|
967
|
-
// buildRefundPsbt's top-level `hashlock` param is documented as "no 0x
|
|
968
|
-
// prefix" and flows into the WASM HTLC connector derivation; a prefixed
|
|
969
|
-
// value would derive the wrong refund script leaf and yield an
|
|
970
|
-
// unspendable PSBT. Match the `hashlocks` array handling above.
|
|
971
|
-
hashlock: p(a.hashlock)
|
|
972
|
-
});
|
|
973
|
-
o == null || o.throwIfAborted();
|
|
974
|
-
const C = O(
|
|
975
|
-
a.depositorBtcPubkey,
|
|
976
|
-
Qe
|
|
977
|
-
), E = await u(b, C);
|
|
978
|
-
V({
|
|
979
|
-
requestedPsbtHex: b,
|
|
980
|
-
returnedPsbtHex: E
|
|
981
|
-
});
|
|
982
|
-
const g = 0, _ = N(
|
|
983
|
-
E,
|
|
984
|
-
d,
|
|
985
|
-
g
|
|
986
|
-
);
|
|
987
|
-
H({
|
|
988
|
-
requestedPsbtHex: b,
|
|
989
|
-
signatureHex: _,
|
|
990
|
-
signerXOnlyPubkeyHex: d,
|
|
991
|
-
inputIndex: g
|
|
992
|
-
});
|
|
993
|
-
const J = et(E);
|
|
994
|
-
o == null || o.throwIfAborted();
|
|
995
|
-
try {
|
|
996
|
-
return await s(J);
|
|
997
|
-
} catch (m) {
|
|
998
|
-
throw m instanceof Error && We.test(m.message) ? new Ge(t, m) : m;
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
export {
|
|
1002
|
-
Ge as B,
|
|
1003
|
-
Q as C,
|
|
1004
|
-
j as R,
|
|
1005
|
-
ht as a,
|
|
1006
|
-
Ie as b,
|
|
1007
|
-
mt as c,
|
|
1008
|
-
Pt as d,
|
|
1009
|
-
Fe as e,
|
|
1010
|
-
Ue as f,
|
|
1011
|
-
bt as g,
|
|
1012
|
-
yt as h,
|
|
1013
|
-
gt as i,
|
|
1014
|
-
wt as j,
|
|
1015
|
-
xt as k,
|
|
1016
|
-
vt as l,
|
|
1017
|
-
Et as m,
|
|
1018
|
-
kt as n,
|
|
1019
|
-
Tt as o,
|
|
1020
|
-
L as p,
|
|
1021
|
-
M as q,
|
|
1022
|
-
pt as r,
|
|
1023
|
-
dt as s,
|
|
1024
|
-
U as t,
|
|
1025
|
-
Y as u,
|
|
1026
|
-
ft as v,
|
|
1027
|
-
G as w
|
|
1028
|
-
};
|
|
1029
|
-
//# sourceMappingURL=buildAndBroadcastRefund-o9Byvkut.js.map
|