@babylonlabs-io/ts-sdk 0.12.0 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/challengeAssert-BpzES0KW.cjs +2 -0
  2. package/dist/challengeAssert-BpzES0KW.cjs.map +1 -0
  3. package/dist/challengeAssert-CenzzhZU.js +724 -0
  4. package/dist/challengeAssert-CenzzhZU.js.map +1 -0
  5. package/dist/errors-CPd0qvSk.cjs +2 -0
  6. package/dist/errors-CPd0qvSk.cjs.map +1 -0
  7. package/dist/{errors-DOJdtNTD.js → errors-Ci-2cgfS.js} +331 -285
  8. package/dist/errors-Ci-2cgfS.js.map +1 -0
  9. package/dist/index.cjs +1 -1
  10. package/dist/index.js +59 -58
  11. package/dist/tbv/core/contracts/abis/BTCVaultRegistry.abi.d.ts +15 -7
  12. package/dist/tbv/core/contracts/abis/BTCVaultRegistry.abi.d.ts.map +1 -1
  13. package/dist/tbv/core/index.cjs +1 -1
  14. package/dist/tbv/core/index.d.ts +1 -1
  15. package/dist/tbv/core/index.d.ts.map +1 -1
  16. package/dist/tbv/core/index.js +59 -58
  17. package/dist/tbv/core/managers/PayoutManager.d.ts +21 -0
  18. package/dist/tbv/core/managers/PayoutManager.d.ts.map +1 -1
  19. package/dist/tbv/core/managers/PeginManager.d.ts +2 -2
  20. package/dist/tbv/core/managers/PeginManager.d.ts.map +1 -1
  21. package/dist/tbv/core/primitives/index.cjs +1 -1
  22. package/dist/tbv/core/primitives/index.d.ts +2 -0
  23. package/dist/tbv/core/primitives/index.d.ts.map +1 -1
  24. package/dist/tbv/core/primitives/index.js +21 -20
  25. package/dist/tbv/core/primitives/psbt/refund.d.ts +42 -0
  26. package/dist/tbv/core/primitives/psbt/refund.d.ts.map +1 -0
  27. package/dist/tbv/core/wots/__tests__/derivation.test.d.ts.map +1 -0
  28. package/dist/tbv/core/{lamport → wots}/derivation.d.ts +16 -16
  29. package/dist/tbv/core/wots/derivation.d.ts.map +1 -0
  30. package/dist/tbv/core/wots/deriveWotsPkHash.d.ts +9 -0
  31. package/dist/tbv/core/wots/deriveWotsPkHash.d.ts.map +1 -0
  32. package/dist/tbv/core/wots/errors.d.ts +7 -0
  33. package/dist/tbv/core/wots/errors.d.ts.map +1 -0
  34. package/dist/tbv/core/wots/index.d.ts +5 -0
  35. package/dist/tbv/core/wots/index.d.ts.map +1 -0
  36. package/dist/tbv/core/{lamport → wots}/types.d.ts +8 -8
  37. package/dist/tbv/core/wots/types.d.ts.map +1 -0
  38. package/dist/tbv/index.cjs +1 -1
  39. package/dist/tbv/index.js +59 -58
  40. package/dist/tbv/integrations/aave/clients/abis/AaveIntegrationAdapter.abi.json.d.ts +459 -485
  41. package/dist/tbv/integrations/aave/index.cjs +1 -1
  42. package/dist/tbv/integrations/aave/index.cjs.map +1 -1
  43. package/dist/tbv/integrations/aave/index.js +91 -91
  44. package/package.json +3 -3
  45. package/dist/challengeAssert-DW0sEUgn.cjs +0 -2
  46. package/dist/challengeAssert-DW0sEUgn.cjs.map +0 -1
  47. package/dist/challengeAssert-DnnliaRt.js +0 -645
  48. package/dist/challengeAssert-DnnliaRt.js.map +0 -1
  49. package/dist/errors-DOJdtNTD.js.map +0 -1
  50. package/dist/errors-DbATYfYA.cjs +0 -2
  51. package/dist/errors-DbATYfYA.cjs.map +0 -1
  52. package/dist/tbv/core/lamport/__tests__/derivation.test.d.ts.map +0 -1
  53. package/dist/tbv/core/lamport/derivation.d.ts.map +0 -1
  54. package/dist/tbv/core/lamport/deriveLamportPkHash.d.ts +0 -9
  55. package/dist/tbv/core/lamport/deriveLamportPkHash.d.ts.map +0 -1
  56. package/dist/tbv/core/lamport/errors.d.ts +0 -7
  57. package/dist/tbv/core/lamport/errors.d.ts.map +0 -1
  58. package/dist/tbv/core/lamport/index.d.ts +0 -5
  59. package/dist/tbv/core/lamport/index.d.ts.map +0 -1
  60. package/dist/tbv/core/lamport/types.d.ts.map +0 -1
  61. /package/dist/tbv/core/{lamport → wots}/__tests__/derivation.test.d.ts +0 -0
@@ -0,0 +1,724 @@
1
+ import { createPrePeginTransaction as W, buildPeginTxFromPrePegin as K, getPrePeginHtlcConnectorInfo as O, tapInternalPubkey as b, initWasm as q, WasmPrePeginTx as N, createPayoutConnector as z, getPeginPayoutScript as M, getAssertNoPayoutScriptInfo as D, getChallengeAssertScriptInfo as X } from "@babylonlabs-io/babylon-tbv-rust-wasm";
2
+ import * as U from "bitcoinjs-lib";
3
+ import { payments as T, networks as I, initEccLib as A, Transaction as x, Psbt as v } from "bitcoinjs-lib";
4
+ import { Buffer as a } from "buffer";
5
+ import * as $ from "@bitcoin-js/tiny-secp256k1-asmjs";
6
+ function g(t) {
7
+ return t.startsWith("0x") ? t.slice(2) : t;
8
+ }
9
+ function pt(t) {
10
+ return t.startsWith("0x") ? t : `0x${t}`;
11
+ }
12
+ function h(t) {
13
+ const e = g(t);
14
+ if (!B(e))
15
+ throw new Error(`Invalid hex string: ${t}`);
16
+ const n = new Uint8Array(e.length / 2);
17
+ for (let r = 0; r < e.length; r += 2)
18
+ n[r / 2] = parseInt(e.slice(r, r + 2), 16);
19
+ return n;
20
+ }
21
+ function w(t) {
22
+ return Array.from(t).map((e) => e.toString(16).padStart(2, "0")).join("");
23
+ }
24
+ function G(t) {
25
+ return t.length === 32 ? t : t.slice(1, 33);
26
+ }
27
+ function B(t) {
28
+ return /^[0-9a-fA-F]*$/.test(t) && t.length % 2 === 0;
29
+ }
30
+ function C(t) {
31
+ const e = g(t);
32
+ if (!B(e))
33
+ throw new Error(`Invalid hex characters in public key: ${t}`);
34
+ if (e.length === 64)
35
+ return e;
36
+ if (e.length !== 66 && e.length !== 130)
37
+ throw new Error(
38
+ `Invalid public key length: ${e.length} (expected 64, 66, or 130 hex chars)`
39
+ );
40
+ const n = h(e);
41
+ return w(G(n));
42
+ }
43
+ function ft(t) {
44
+ const e = g(t);
45
+ return B(e);
46
+ }
47
+ function dt(t, e) {
48
+ const n = C(t), r = e ?? n;
49
+ if (n.toLowerCase() !== r.toLowerCase())
50
+ throw new Error(
51
+ `Wallet public key does not match vault depositor. Expected: ${r}, Got: ${n}. Please connect the wallet that was used to create this vault.`
52
+ );
53
+ return { walletPubkeyRaw: t, walletPubkeyXOnly: n, depositorPubkey: r };
54
+ }
55
+ let _ = !1;
56
+ function Q() {
57
+ _ || (A($), _ = !0);
58
+ }
59
+ function V(t) {
60
+ switch (t) {
61
+ case "bitcoin":
62
+ return I.bitcoin;
63
+ case "testnet":
64
+ case "signet":
65
+ return I.testnet;
66
+ case "regtest":
67
+ return I.regtest;
68
+ default:
69
+ throw new Error(`Unknown network: ${t}`);
70
+ }
71
+ }
72
+ function Y(t, e) {
73
+ Q();
74
+ const n = h(C(t)), { address: r } = T.p2tr({
75
+ internalPubkey: a.from(n),
76
+ network: V(e)
77
+ });
78
+ if (!r)
79
+ throw new Error("Failed to derive taproot address from public key");
80
+ return r;
81
+ }
82
+ function Z(t, e) {
83
+ const n = g(t);
84
+ if (n.length !== 66)
85
+ throw new Error(
86
+ `Native SegWit requires a compressed public key (66 hex chars), got ${n.length}`
87
+ );
88
+ const { address: r } = T.p2wpkh({
89
+ pubkey: a.from(h(n)),
90
+ network: V(e)
91
+ });
92
+ if (!r)
93
+ throw new Error(
94
+ "Failed to derive native segwit address from public key"
95
+ );
96
+ return r;
97
+ }
98
+ function ht(t, e, n) {
99
+ const r = g(e);
100
+ try {
101
+ if (t === Y(r, n))
102
+ return !0;
103
+ } catch {
104
+ }
105
+ const o = [];
106
+ r.length === 66 ? o.push(r) : r.length === 64 && o.push(`02${r}`, `03${r}`);
107
+ for (const s of o)
108
+ try {
109
+ if (t === Z(s, n))
110
+ return !0;
111
+ } catch {
112
+ }
113
+ return !1;
114
+ }
115
+ function H(t) {
116
+ return C(t).toLowerCase();
117
+ }
118
+ function gt(t, e, n) {
119
+ const r = /* @__PURE__ */ new Set();
120
+ r.add(H(t));
121
+ for (const o of e)
122
+ r.add(H(o));
123
+ return r.delete(H(n)), r.size;
124
+ }
125
+ const Pt = 58, yt = 43, xt = 11, j = 546, J = BigInt(j), tt = 30, et = 2, kt = 1.1;
126
+ function wt(t) {
127
+ return t <= et ? tt : 0;
128
+ }
129
+ const nt = 1;
130
+ function vt(t) {
131
+ return t + nt;
132
+ }
133
+ const bt = 5;
134
+ function F(t) {
135
+ const n = t.substring(8, 12) === "0001" ? 12 : 8, r = parseInt(
136
+ t.substring(n, n + 2),
137
+ 16
138
+ ), o = parseInt(
139
+ t.substring(n + 2, n + 4),
140
+ 16
141
+ );
142
+ if (r !== 0)
143
+ throw new Error(`Expected 0 inputs from WASM, got ${r}`);
144
+ if (o === 0)
145
+ throw new Error("Expected at least 1 output from WASM, got 0");
146
+ const s = a.from(t.substring(0, 8), "hex").readUInt32LE(0), c = a.from(
147
+ t.substring(t.length - 8),
148
+ "hex"
149
+ ).readUInt32LE(0), i = [];
150
+ let u = n + 4;
151
+ for (let p = 0; p < o; p++) {
152
+ const l = t.substring(u, u + 16), f = Number(a.from(l, "hex").readBigUInt64LE(0));
153
+ u += 16;
154
+ const d = parseInt(t.substring(u, u + 2), 16);
155
+ u += 2;
156
+ const P = t.substring(u, u + d * 2), y = a.from(P, "hex");
157
+ u += d * 2, i.push({ value: f, script: y });
158
+ }
159
+ return { version: s, locktime: c, outputs: i };
160
+ }
161
+ function mt(t) {
162
+ const { unfundedTxHex: e, selectedUTXOs: n, changeAddress: r, changeAmount: o, network: s } = t, { version: c, locktime: i, outputs: u } = F(e), p = new U.Transaction();
163
+ p.version = c, p.locktime = i;
164
+ for (const l of n) {
165
+ const f = a.from(l.txid, "hex").reverse();
166
+ p.addInput(f, l.vout);
167
+ }
168
+ for (const l of u)
169
+ p.addOutput(l.script, l.value);
170
+ if (o > J) {
171
+ const l = U.address.toOutputScript(r, s);
172
+ p.addOutput(l, Number(o));
173
+ }
174
+ return p.toHex();
175
+ }
176
+ async function St(t) {
177
+ const e = await W({
178
+ depositorPubkey: t.depositorPubkey,
179
+ vaultProviderPubkey: t.vaultProviderPubkey,
180
+ vaultKeeperPubkeys: t.vaultKeeperPubkeys,
181
+ universalChallengerPubkeys: t.universalChallengerPubkeys,
182
+ hashlocks: [...t.hashlocks],
183
+ timelockRefund: t.timelockRefund,
184
+ pegInAmounts: [...t.pegInAmounts],
185
+ feeRate: t.feeRate,
186
+ numLocalChallengers: t.numLocalChallengers,
187
+ councilQuorum: t.councilQuorum,
188
+ councilSize: t.councilSize,
189
+ network: t.network
190
+ }), r = F(e.txHex).outputs.reduce(
191
+ (o, s) => o + BigInt(s.value),
192
+ 0n
193
+ );
194
+ return {
195
+ psbtHex: e.txHex,
196
+ totalOutputValue: r,
197
+ htlcValues: e.htlcValues,
198
+ htlcScriptPubKeys: e.htlcScriptPubKeys,
199
+ htlcAddresses: e.htlcAddresses,
200
+ peginAmounts: e.peginAmounts,
201
+ depositorClaimValue: e.depositorClaimValue
202
+ };
203
+ }
204
+ async function Et(t) {
205
+ const e = await K(
206
+ {
207
+ depositorPubkey: t.prePeginParams.depositorPubkey,
208
+ vaultProviderPubkey: t.prePeginParams.vaultProviderPubkey,
209
+ vaultKeeperPubkeys: t.prePeginParams.vaultKeeperPubkeys,
210
+ universalChallengerPubkeys: t.prePeginParams.universalChallengerPubkeys,
211
+ hashlocks: [...t.prePeginParams.hashlocks],
212
+ timelockRefund: t.prePeginParams.timelockRefund,
213
+ pegInAmounts: [...t.prePeginParams.pegInAmounts],
214
+ feeRate: t.prePeginParams.feeRate,
215
+ numLocalChallengers: t.prePeginParams.numLocalChallengers,
216
+ councilQuorum: t.prePeginParams.councilQuorum,
217
+ councilSize: t.prePeginParams.councilSize,
218
+ network: t.prePeginParams.network
219
+ },
220
+ t.timelockPegin,
221
+ t.fundedPrePeginTxHex,
222
+ t.htlcVout
223
+ );
224
+ return {
225
+ txHex: e.txHex,
226
+ txid: e.txid,
227
+ vaultScriptPubKey: e.vaultScriptPubKey,
228
+ vaultValue: e.vaultValue
229
+ };
230
+ }
231
+ async function Tt(t) {
232
+ const e = g(t.peginTxHex), n = g(t.fundedPrePeginTxHex), r = await O({
233
+ depositorPubkey: t.depositorPubkey,
234
+ vaultProviderPubkey: t.vaultProviderPubkey,
235
+ vaultKeeperPubkeys: t.vaultKeeperPubkeys,
236
+ universalChallengerPubkeys: t.universalChallengerPubkeys,
237
+ hashlock: t.hashlock,
238
+ timelockRefund: t.timelockRefund,
239
+ network: t.network
240
+ }), o = x.fromHex(e), s = x.fromHex(n);
241
+ if (o.ins.length !== 1)
242
+ throw new Error(
243
+ `PegIn transaction must have exactly 1 input, got ${o.ins.length}`
244
+ );
245
+ const c = o.ins[0], i = s.getId(), u = w(
246
+ new Uint8Array(c.hash).slice().reverse()
247
+ );
248
+ if (u !== i)
249
+ throw new Error(
250
+ `PegIn input does not reference the Pre-PegIn transaction. Expected ${i}, got ${u}`
251
+ );
252
+ const p = s.outs[c.index];
253
+ if (!p)
254
+ throw new Error(
255
+ `Pre-PegIn output ${c.index} not found (Pre-PegIn has ${s.outs.length} outputs)`
256
+ );
257
+ const l = h(r.hashlockScript), f = h(r.hashlockControlBlock), d = new v();
258
+ d.setVersion(o.version), d.setLocktime(o.locktime), d.addInput({
259
+ hash: c.hash,
260
+ index: c.index,
261
+ sequence: c.sequence,
262
+ witnessUtxo: {
263
+ script: p.script,
264
+ value: p.value
265
+ },
266
+ tapLeafScript: [
267
+ {
268
+ leafVersion: 192,
269
+ script: a.from(l),
270
+ controlBlock: a.from(f)
271
+ }
272
+ ],
273
+ tapInternalKey: a.from(b)
274
+ // sighashType omitted — defaults to SIGHASH_DEFAULT (0x00) for Taproot
275
+ });
276
+ for (const P of o.outs)
277
+ d.addOutput({
278
+ script: P.script,
279
+ value: P.value
280
+ });
281
+ return { psbtHex: d.toHex() };
282
+ }
283
+ function It(t, e) {
284
+ const r = v.fromHex(t).data.inputs[0];
285
+ if (!r)
286
+ throw new Error("PegIn PSBT has no inputs");
287
+ if (r.tapScriptSig && r.tapScriptSig.length > 0) {
288
+ const o = a.from(
289
+ h(e)
290
+ );
291
+ for (const s of r.tapScriptSig)
292
+ if (s.pubkey.equals(o))
293
+ return rt(s.signature);
294
+ throw new Error(
295
+ `No PegIn input signature found for depositor pubkey: ${e}`
296
+ );
297
+ }
298
+ throw r.finalScriptWitness && r.finalScriptWitness.length > 0 ? new Error(
299
+ "PegIn input PSBT is already finalized. Cannot reliably extract the depositor signature from the witness stack. Ensure the wallet returns a non-finalized PSBT with tapScriptSig entries."
300
+ ) : new Error(
301
+ "No tapScriptSig or finalScriptWitness found in signed PegIn input PSBT"
302
+ );
303
+ }
304
+ function Ht(t) {
305
+ const e = v.fromHex(t);
306
+ try {
307
+ e.finalizeAllInputs();
308
+ } catch (n) {
309
+ if (!e.data.inputs.every(
310
+ (o) => o.finalScriptWitness || o.finalScriptSig
311
+ ))
312
+ throw new Error(
313
+ `PSBT finalization failed and wallet did not auto-finalize: ${n}`
314
+ );
315
+ }
316
+ return e.extractTransaction().toHex();
317
+ }
318
+ function rt(t) {
319
+ if (t.length === 64)
320
+ return w(new Uint8Array(t));
321
+ if (t.length === 65)
322
+ return w(new Uint8Array(t.subarray(0, 64)));
323
+ throw new Error(`Unexpected PegIn input signature length: ${t.length}`);
324
+ }
325
+ async function At(t) {
326
+ await q();
327
+ const { prePeginParams: e, fundedPrePeginTxHex: n, htlcVout: r, refundFee: o, hashlock: s } = t, c = new N(
328
+ e.depositorPubkey,
329
+ e.vaultProviderPubkey,
330
+ e.vaultKeeperPubkeys,
331
+ e.universalChallengerPubkeys,
332
+ [...e.hashlocks],
333
+ new BigUint64Array(e.pegInAmounts),
334
+ e.timelockRefund,
335
+ e.feeRate,
336
+ e.numLocalChallengers,
337
+ e.councilQuorum,
338
+ e.councilSize,
339
+ e.network
340
+ );
341
+ let i = null;
342
+ try {
343
+ i = c.fromFundedTransaction(n);
344
+ const u = i.buildRefundTx(o, r), p = await O({
345
+ depositorPubkey: e.depositorPubkey,
346
+ vaultProviderPubkey: e.vaultProviderPubkey,
347
+ vaultKeeperPubkeys: e.vaultKeeperPubkeys,
348
+ universalChallengerPubkeys: e.universalChallengerPubkeys,
349
+ hashlock: s,
350
+ timelockRefund: e.timelockRefund,
351
+ network: e.network
352
+ }), l = n.startsWith("0x") ? n.slice(2) : n, f = x.fromHex(l), d = f.outs[r];
353
+ if (!d)
354
+ throw new Error(
355
+ `HTLC output at vout ${r} not found in funded Pre-PegIn tx (tx has ${f.outs.length} outputs)`
356
+ );
357
+ const P = x.fromHex(u);
358
+ if (P.ins.length !== 1)
359
+ throw new Error(
360
+ `Refund transaction must have exactly 1 input, got ${P.ins.length}`
361
+ );
362
+ const y = P.ins[0], m = f.getId(), E = w(
363
+ new Uint8Array(y.hash).slice().reverse()
364
+ );
365
+ if (E !== m)
366
+ throw new Error(
367
+ `Refund input does not reference the Pre-PegIn transaction. Expected ${m}, got ${E}`
368
+ );
369
+ if (y.index !== r)
370
+ throw new Error(
371
+ `Refund input index ${y.index} does not match expected htlcVout ${r}`
372
+ );
373
+ const k = new v();
374
+ k.setVersion(P.version), k.setLocktime(P.locktime), k.addInput({
375
+ hash: y.hash,
376
+ index: y.index,
377
+ sequence: y.sequence,
378
+ witnessUtxo: {
379
+ script: d.script,
380
+ value: d.value
381
+ },
382
+ tapLeafScript: [
383
+ {
384
+ leafVersion: 192,
385
+ script: a.from(h(p.refundScript)),
386
+ controlBlock: a.from(
387
+ h(p.refundControlBlock)
388
+ )
389
+ }
390
+ ],
391
+ tapInternalKey: a.from(b)
392
+ });
393
+ for (const S of P.outs)
394
+ k.addOutput({
395
+ script: S.script,
396
+ value: S.value
397
+ });
398
+ return { psbtHex: k.toHex() };
399
+ } finally {
400
+ i == null || i.free(), c.free();
401
+ }
402
+ }
403
+ async function ot(t) {
404
+ const e = await z(
405
+ {
406
+ depositor: t.depositor,
407
+ vaultProvider: t.vaultProvider,
408
+ vaultKeepers: t.vaultKeepers,
409
+ universalChallengers: t.universalChallengers,
410
+ timelockPegin: t.timelockPegin
411
+ },
412
+ t.network
413
+ );
414
+ return {
415
+ payoutScript: e.payoutScript,
416
+ taprootScriptHash: e.taprootScriptHash,
417
+ scriptPubKey: e.scriptPubKey,
418
+ address: e.address
419
+ };
420
+ }
421
+ A($);
422
+ async function $t(t) {
423
+ const e = g(t.payoutTxHex), n = g(t.peginTxHex), r = g(t.assertTxHex), o = await ot({
424
+ depositor: t.depositorBtcPubkey,
425
+ vaultProvider: t.vaultProviderBtcPubkey,
426
+ vaultKeepers: t.vaultKeeperBtcPubkeys,
427
+ universalChallengers: t.universalChallengerBtcPubkeys,
428
+ timelockPegin: t.timelockPegin,
429
+ network: t.network
430
+ }), s = h(o.payoutScript), c = it(b, s), i = x.fromHex(e), u = x.fromHex(n), p = x.fromHex(r), l = new v();
431
+ if (l.setVersion(i.version), l.setLocktime(i.locktime), i.ins.length !== 2)
432
+ throw new Error(
433
+ `Payout transaction must have exactly 2 inputs, got ${i.ins.length}`
434
+ );
435
+ const f = i.ins[0], d = i.ins[1], P = w(
436
+ new Uint8Array(f.hash).slice().reverse()
437
+ ), y = u.getId();
438
+ if (P !== y)
439
+ throw new Error(
440
+ `Input 0 does not reference pegin transaction. Expected ${y}, got ${P}`
441
+ );
442
+ const m = w(
443
+ new Uint8Array(d.hash).slice().reverse()
444
+ ), E = p.getId();
445
+ if (m !== E)
446
+ throw new Error(
447
+ `Input 1 does not reference assert transaction. Expected ${E}, got ${m}`
448
+ );
449
+ const k = u.outs[f.index];
450
+ if (!k)
451
+ throw new Error(
452
+ `Previous output not found for input 0 (txid: ${P}, index: ${f.index})`
453
+ );
454
+ const S = p.outs[d.index];
455
+ if (!S)
456
+ throw new Error(
457
+ `Previous output not found for input 1 (txid: ${m}, index: ${d.index})`
458
+ );
459
+ l.addInput({
460
+ hash: f.hash,
461
+ index: f.index,
462
+ sequence: f.sequence,
463
+ witnessUtxo: {
464
+ script: k.script,
465
+ value: k.value
466
+ },
467
+ tapLeafScript: [
468
+ {
469
+ leafVersion: 192,
470
+ script: a.from(s),
471
+ controlBlock: a.from(c)
472
+ }
473
+ ],
474
+ tapInternalKey: a.from(b)
475
+ // sighashType omitted - defaults to SIGHASH_DEFAULT (0x00) for Taproot
476
+ }), l.addInput({
477
+ hash: d.hash,
478
+ index: d.index,
479
+ sequence: d.sequence,
480
+ witnessUtxo: {
481
+ script: S.script,
482
+ value: S.value
483
+ }
484
+ // No tapLeafScript - depositor doesn't sign this input
485
+ });
486
+ for (const L of i.outs)
487
+ l.addOutput({
488
+ script: L.script,
489
+ value: L.value
490
+ });
491
+ return {
492
+ psbtHex: l.toHex()
493
+ };
494
+ }
495
+ function Bt(t, e, n = 0) {
496
+ const r = v.fromHex(t);
497
+ if (n >= r.data.inputs.length)
498
+ throw new Error(
499
+ `Input index ${n} out of range (${r.data.inputs.length} inputs)`
500
+ );
501
+ const o = r.data.inputs[n];
502
+ if (o.tapScriptSig && o.tapScriptSig.length > 0) {
503
+ const s = h(e);
504
+ for (const c of o.tapScriptSig)
505
+ if (c.pubkey.equals(a.from(s)))
506
+ return R(c.signature, n);
507
+ throw new Error(
508
+ `No signature found for depositor pubkey: ${e} at input ${n}`
509
+ );
510
+ }
511
+ if (o.finalScriptWitness && o.finalScriptWitness.length > 0) {
512
+ const s = st(o.finalScriptWitness);
513
+ if (s.length >= 1)
514
+ return R(s[0], n);
515
+ }
516
+ throw new Error(
517
+ `No tapScriptSig or finalScriptWitness found in signed PSBT at input ${n}`
518
+ );
519
+ }
520
+ function R(t, e) {
521
+ if (t.length === 64)
522
+ return w(new Uint8Array(t));
523
+ if (t.length === 65) {
524
+ const n = t[64];
525
+ if (n !== x.SIGHASH_ALL)
526
+ throw new Error(
527
+ `Unexpected sighash type 0x${n.toString(16).padStart(2, "0")} at input ${e}. Expected SIGHASH_ALL (0x01).`
528
+ );
529
+ return w(new Uint8Array(t.subarray(0, 64)));
530
+ }
531
+ throw new Error(
532
+ `Unexpected signature length at input ${e}: ${t.length}`
533
+ );
534
+ }
535
+ function st(t) {
536
+ const e = [];
537
+ let n = 0;
538
+ const r = () => {
539
+ const s = t[n++];
540
+ if (s < 253) return s;
541
+ if (s === 253) {
542
+ const c = t[n] | t[n + 1] << 8;
543
+ return n += 2, c;
544
+ }
545
+ if (s === 254) {
546
+ const c = t[n] | t[n + 1] << 8 | t[n + 2] << 16 | t[n + 3] << 24;
547
+ return n += 4, c;
548
+ }
549
+ return n += 8, 0;
550
+ }, o = r();
551
+ for (let s = 0; s < o; s++) {
552
+ const c = r();
553
+ e.push(t.subarray(n, n + c)), n += c;
554
+ }
555
+ return e;
556
+ }
557
+ function it(t, e) {
558
+ const n = { output: a.from(e) }, o = T.p2tr({
559
+ internalPubkey: a.from(t),
560
+ scriptTree: n
561
+ }).pubkey;
562
+ if (!o)
563
+ throw new Error("Failed to compute output key");
564
+ const s = 192, c = o[0] === 3 ? 1 : 0, i = s | c, u = new Uint8Array(1 + t.length);
565
+ return u[0] = i, u.set(t, 1), u;
566
+ }
567
+ A($);
568
+ async function Ct(t) {
569
+ const e = g(t.payoutTxHex), n = x.fromHex(e), r = await M(t.connectorParams), o = h(r), s = ut(b, o), c = new v();
570
+ c.setVersion(n.version), c.setLocktime(n.locktime);
571
+ for (let i = 0; i < n.ins.length; i++) {
572
+ const u = n.ins[i], p = t.prevouts[i];
573
+ if (!p)
574
+ throw new Error(`Missing prevout data for input ${i}`);
575
+ const l = {
576
+ hash: u.hash,
577
+ index: u.index,
578
+ sequence: u.sequence,
579
+ witnessUtxo: {
580
+ script: a.from(h(g(p.script_pubkey))),
581
+ value: p.value
582
+ }
583
+ };
584
+ i === 0 && (l.tapLeafScript = [
585
+ {
586
+ leafVersion: 192,
587
+ script: a.from(o),
588
+ controlBlock: a.from(s)
589
+ }
590
+ ], l.tapInternalKey = a.from(b)), c.addInput(l);
591
+ }
592
+ for (const i of n.outs)
593
+ c.addOutput({
594
+ script: i.script,
595
+ value: i.value
596
+ });
597
+ return c.toHex();
598
+ }
599
+ function ut(t, e) {
600
+ const n = { output: a.from(e) }, o = T.p2tr({
601
+ internalPubkey: a.from(t),
602
+ scriptTree: n
603
+ }).pubkey;
604
+ if (!o)
605
+ throw new Error("Failed to compute output key");
606
+ const s = 192, c = o[0] === 3 ? 1 : 0, i = s | c, u = new Uint8Array(1 + t.length);
607
+ return u[0] = i, u.set(t, 1), u;
608
+ }
609
+ async function Lt(t) {
610
+ const e = g(t.noPayoutTxHex), n = x.fromHex(e), { noPayoutScript: r, noPayoutControlBlock: o } = await D(
611
+ t.connectorParams,
612
+ t.challengerPubkey
613
+ ), s = h(r), c = h(o), i = new v();
614
+ i.setVersion(n.version), i.setLocktime(n.locktime);
615
+ for (let u = 0; u < n.ins.length; u++) {
616
+ const p = n.ins[u], l = t.prevouts[u];
617
+ if (!l)
618
+ throw new Error(`Missing prevout data for input ${u}`);
619
+ const f = {
620
+ hash: p.hash,
621
+ index: p.index,
622
+ sequence: p.sequence,
623
+ witnessUtxo: {
624
+ script: a.from(h(g(l.script_pubkey))),
625
+ value: l.value
626
+ }
627
+ };
628
+ u === 0 && (f.tapLeafScript = [
629
+ {
630
+ leafVersion: 192,
631
+ script: a.from(s),
632
+ controlBlock: a.from(c)
633
+ }
634
+ ], f.tapInternalKey = a.from(b)), i.addInput(f);
635
+ }
636
+ for (const u of n.outs)
637
+ i.addOutput({
638
+ script: u.script,
639
+ value: u.value
640
+ });
641
+ return i.toHex();
642
+ }
643
+ async function Ut(t) {
644
+ const e = g(t.challengeAssertTxHex), n = x.fromHex(e);
645
+ if (t.connectorParamsPerInput.length !== n.ins.length)
646
+ throw new Error(
647
+ `Expected ${n.ins.length} connector params, got ${t.connectorParamsPerInput.length}`
648
+ );
649
+ const r = await Promise.all(
650
+ t.connectorParamsPerInput.map((s) => X(s))
651
+ ), o = new v();
652
+ o.setVersion(n.version), o.setLocktime(n.locktime);
653
+ for (let s = 0; s < n.ins.length; s++) {
654
+ const c = n.ins[s], i = t.prevouts[s];
655
+ if (!i)
656
+ throw new Error(`Missing prevout data for input ${s}`);
657
+ const { script: u, controlBlock: p } = r[s], l = h(u), f = h(p);
658
+ o.addInput({
659
+ hash: c.hash,
660
+ index: c.index,
661
+ sequence: c.sequence,
662
+ witnessUtxo: {
663
+ script: a.from(h(g(i.script_pubkey))),
664
+ value: i.value
665
+ },
666
+ tapLeafScript: [
667
+ {
668
+ leafVersion: 192,
669
+ script: a.from(l),
670
+ controlBlock: a.from(f)
671
+ }
672
+ ],
673
+ tapInternalKey: a.from(b)
674
+ });
675
+ }
676
+ for (const s of n.outs)
677
+ o.addOutput({
678
+ script: s.script,
679
+ value: s.value
680
+ });
681
+ return o.toHex();
682
+ }
683
+ export {
684
+ vt as A,
685
+ j as B,
686
+ F as C,
687
+ J as D,
688
+ mt as E,
689
+ kt as F,
690
+ V as G,
691
+ tt as L,
692
+ yt as M,
693
+ Pt as P,
694
+ bt as S,
695
+ xt as T,
696
+ et as W,
697
+ Et as a,
698
+ St as b,
699
+ gt as c,
700
+ Tt as d,
701
+ It as e,
702
+ Ht as f,
703
+ At as g,
704
+ $t as h,
705
+ Bt as i,
706
+ Ct as j,
707
+ Lt as k,
708
+ Ut as l,
709
+ ot as m,
710
+ Z as n,
711
+ Y as o,
712
+ h as p,
713
+ ht as q,
714
+ ft as r,
715
+ pt as s,
716
+ C as t,
717
+ g as u,
718
+ G as v,
719
+ w,
720
+ dt as x,
721
+ wt as y,
722
+ nt as z
723
+ };
724
+ //# sourceMappingURL=challengeAssert-CenzzhZU.js.map