@babylonlabs-io/ts-sdk 0.22.0 → 0.23.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 (49) hide show
  1. package/dist/{buildAndBroadcastRefund-DWEQvj9T.cjs → buildAndBroadcastRefund-BDGGbGeK.cjs} +2 -2
  2. package/dist/buildAndBroadcastRefund-BDGGbGeK.cjs.map +1 -0
  3. package/dist/{buildAndBroadcastRefund-DnTQkCgG.js → buildAndBroadcastRefund-D7NCbgtv.js} +79 -77
  4. package/dist/buildAndBroadcastRefund-D7NCbgtv.js.map +1 -0
  5. package/dist/{challengeAssert-D1lpvuMv.js → challengeAssert-CH32j5cZ.js} +35 -29
  6. package/dist/challengeAssert-CH32j5cZ.js.map +1 -0
  7. package/dist/challengeAssert-X7V3Ik_Q.cjs +2 -0
  8. package/dist/challengeAssert-X7V3Ik_Q.cjs.map +1 -0
  9. package/dist/errors-B3TOmE31.cjs +2 -0
  10. package/dist/errors-B3TOmE31.cjs.map +1 -0
  11. package/dist/errors-rJcuN2m_.js +1275 -0
  12. package/dist/errors-rJcuN2m_.js.map +1 -0
  13. package/dist/index.cjs +1 -1
  14. package/dist/index.js +3 -3
  15. package/dist/tbv/core/index.cjs +1 -1
  16. package/dist/tbv/core/index.js +3 -3
  17. package/dist/tbv/core/managers/PayoutManager.d.ts +1 -1
  18. package/dist/tbv/core/managers/PeginManager.d.ts +57 -51
  19. package/dist/tbv/core/managers/PeginManager.d.ts.map +1 -1
  20. package/dist/tbv/core/managers/index.d.ts +7 -6
  21. package/dist/tbv/core/managers/index.d.ts.map +1 -1
  22. package/dist/tbv/core/primitives/index.cjs +1 -1
  23. package/dist/tbv/core/primitives/index.js +1 -1
  24. package/dist/tbv/core/primitives/psbt/__tests__/peginInput.test.d.ts +10 -0
  25. package/dist/tbv/core/primitives/psbt/__tests__/peginInput.test.d.ts.map +1 -0
  26. package/dist/tbv/core/primitives/psbt/peginInput.d.ts +8 -0
  27. package/dist/tbv/core/primitives/psbt/peginInput.d.ts.map +1 -1
  28. package/dist/tbv/core/services/index.cjs +1 -1
  29. package/dist/tbv/core/services/index.js +1 -1
  30. package/dist/tbv/core/services/refund/buildAndBroadcastRefund.d.ts +5 -1
  31. package/dist/tbv/core/services/refund/buildAndBroadcastRefund.d.ts.map +1 -1
  32. package/dist/tbv/index.cjs +1 -1
  33. package/dist/tbv/index.js +3 -3
  34. package/dist/testing/MockBitcoinWallet.d.ts +2 -2
  35. package/dist/testing/MockBitcoinWallet.d.ts.map +1 -1
  36. package/dist/testing/index.cjs +1 -1
  37. package/dist/testing/index.cjs.map +1 -1
  38. package/dist/testing/index.js +14 -14
  39. package/dist/testing/index.js.map +1 -1
  40. package/package.json +1 -1
  41. package/dist/buildAndBroadcastRefund-DWEQvj9T.cjs.map +0 -1
  42. package/dist/buildAndBroadcastRefund-DnTQkCgG.js.map +0 -1
  43. package/dist/challengeAssert-D1lpvuMv.js.map +0 -1
  44. package/dist/challengeAssert-nYlgeAI8.cjs +0 -2
  45. package/dist/challengeAssert-nYlgeAI8.cjs.map +0 -1
  46. package/dist/errors-CkkL1ztr.js +0 -1236
  47. package/dist/errors-CkkL1ztr.js.map +0 -1
  48. package/dist/errors-D2H46Yut.cjs +0 -2
  49. package/dist/errors-D2H46Yut.cjs.map +0 -1
@@ -0,0 +1,1275 @@
1
+ var qt = Object.defineProperty;
2
+ var zt = (e, t, s) => t in e ? qt(e, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : e[t] = s;
3
+ var x = (e, t, s) => zt(e, typeof t != "symbol" ? t + "" : t, s);
4
+ import * as Zt from "bitcoinjs-lib";
5
+ import { Transaction as Jt, Psbt as pt } from "bitcoinjs-lib";
6
+ import { Buffer as at } from "buffer";
7
+ import { isAddressEqual as mt, createPublicClient as G, http as q, encodeFunctionData as yt, zeroAddress as Yt } from "viem";
8
+ import { c as Qt } from "./signing-DeWVBl7m.js";
9
+ import { deriveVaultId as wt } from "@babylonlabs-io/babylon-tbv-rust-wasm";
10
+ import { b as te, a as ee, d as se, e as ne, f as oe } from "./challengeAssert-CH32j5cZ.js";
11
+ import { s as _, g as bt, e as H, i as re, p as ie } from "./bitcoin-nOOgeRyl.js";
12
+ import { M as xt } from "./validation-CxqROCno.js";
13
+ import { b as ae, f as z, i as ce } from "./psbtInputFields-r1ss6WLU.js";
14
+ import { p as le, f as ue } from "./fundPeginTransaction-BLYXxLBv.js";
15
+ import { p as he, f as fe } from "./index-DLtpqdlu.js";
16
+ import { B as $ } from "./types-DWjaqVfP.js";
17
+ import { n as St, o as M, q as C, r as N, t as Ct, H as de, u as W, x as ge, y as L, z as pe, A as Pt, D as me, E as ye, F as we, G as be, I as xe, J as Pe, K as ke, L as kt, M as ht, N as At } from "./buildAndBroadcastRefund-D7NCbgtv.js";
18
+ const A = {
19
+ // VaultAlreadyExists()
20
+ "0x04aabf33": "Vault already exists: This Bitcoin transaction has already been registered. Please select different UTXOs or use a different amount to create a unique transaction.",
21
+ // ScriptPubKeyMismatch() - taproot output doesn't match expected script
22
+ "0x4fec082d": "Script mismatch: The Bitcoin transaction's taproot output does not match the expected vault script. This may be caused by incorrect vault participants or key configuration.",
23
+ // InvalidBTCProofOfPossession()
24
+ "0x6cc363a5": "Invalid BTC proof of possession: The signature could not be verified. Please ensure you're signing with the correct Bitcoin wallet.",
25
+ // InvalidBTCPublicKey()
26
+ "0x6c3f2bf6": "Invalid BTC public key: The Bitcoin public key format is invalid.",
27
+ // InvalidAmount()
28
+ "0x2c5211c6": "Invalid amount: The deposit amount is invalid or below the minimum required.",
29
+ // ApplicationNotRegistered()
30
+ "0x0405f772": "Application not registered: The application controller is not registered in the system.",
31
+ // InvalidProviderStatus()
32
+ "0x24e165cc": "Invalid provider status: The vault provider is not in a valid state to accept deposits.",
33
+ // ZeroAddress()
34
+ "0xd92e233d": "Zero address: One of the required addresses is the zero address.",
35
+ // BtcKeyMismatch()
36
+ "0x65aa7007": "BTC key mismatch: The Bitcoin public key does not match the expected key.",
37
+ // Unauthorized()
38
+ "0x82b42900": "Unauthorized: You must be the depositor or vault provider to submit this transaction.",
39
+ // InvalidSignature() - common signature verification error
40
+ "0x8baa579f": "Invalid signature: The BTC proof of possession signature could not be verified.",
41
+ // InvalidBtcTransaction()
42
+ "0x2f9d01e9": "Invalid BTC transaction: The Bitcoin transaction format is invalid.",
43
+ // VaultProviderNotRegistered()
44
+ "0x5a3c6b3e": "Vault provider not registered: The selected vault provider is not registered.",
45
+ // InvalidPeginFee(uint256,uint256)
46
+ "0x979f4518": "Invalid pegin fee: The ETH fee sent does not match the required amount. This may indicate a fee rate change during the transaction.",
47
+ // PrePeginOutputAlreadyUsed()
48
+ "0x5fad9694": "This pre-pegin output has already been used to activate another vault.",
49
+ // PeginTransactionAlreadyUsed()
50
+ "0x7ed061c9": "This pegin transaction has already been used to activate another vault."
51
+ };
52
+ function ft(e) {
53
+ if (!e || typeof e != "object") return;
54
+ const t = e;
55
+ if (typeof t.data == "string" && t.data.startsWith("0x"))
56
+ return t.data;
57
+ if (typeof t.details == "string" && t.details.startsWith("0x"))
58
+ return t.details;
59
+ let s = t.cause, n = 0;
60
+ const o = 5;
61
+ for (; s && typeof s == "object" && n < o; ) {
62
+ const a = s;
63
+ if (typeof a.data == "string" && a.data.startsWith("0x"))
64
+ return a.data;
65
+ s = a.cause, n++;
66
+ }
67
+ const i = (typeof t.message == "string" ? t.message : "").match(/\b(0x[a-fA-F0-9]{8})\b/);
68
+ if (i)
69
+ return i[1];
70
+ }
71
+ function Es(e) {
72
+ const t = ft(e);
73
+ if (t) {
74
+ const s = t.substring(0, 10);
75
+ return A[t] ?? A[s];
76
+ }
77
+ }
78
+ function Is(e) {
79
+ const t = ft(e);
80
+ if (t === void 0) return !1;
81
+ const s = t.substring(0, 10);
82
+ return t in A || s in A;
83
+ }
84
+ function S(e) {
85
+ console.error("[Contract Error] Raw error:", e);
86
+ const t = ft(e);
87
+ if (console.error("[Contract Error] Extracted error data:", t), t) {
88
+ const n = t.substring(0, 10), o = A[t] ?? A[n];
89
+ if (o)
90
+ throw console.error("[Contract Error] Known error:", o), new Error(o);
91
+ }
92
+ const s = (e == null ? void 0 : e.message) || "";
93
+ if (s.includes("gas limit too high") || s.includes("21000000") || s.includes("Internal JSON-RPC error")) {
94
+ const n = t ? ` (error code: ${t})` : "";
95
+ throw console.error(
96
+ "[Contract Error] Transaction rejected. Error code:",
97
+ t,
98
+ "Message:",
99
+ s
100
+ ), new Error(
101
+ `Transaction failed: The contract rejected this transaction${n}. Possible causes: (1) Vault already exists for this transaction, (2) Invalid signature, (3) Unauthorized caller. Please check your transaction parameters and try again.`
102
+ );
103
+ }
104
+ throw e instanceof Error ? (console.error("[Contract Error] Unhandled error:", e.message), e) : new Error(`Contract call failed: ${String(e)}`);
105
+ }
106
+ const Te = 0, Ee = /^0x[0-9a-f]+$/i, Ie = /^[0-9a-f]+$/i, ve = /^[A-Za-z0-9+/]+={0,2}$/;
107
+ function R(e) {
108
+ if (typeof e != "string" || e.length === 0)
109
+ throw new Error("BTC wallet returned empty public key");
110
+ return ie(e).toLowerCase();
111
+ }
112
+ function _e(e) {
113
+ if (typeof e != "string" || e.length === 0)
114
+ throw new Error("BTC wallet returned empty BIP-322 signature");
115
+ if (e.startsWith("0x") || e.startsWith("0X")) {
116
+ if (!Ee.test(e) || e.length < 4 || e.length % 2 !== 0)
117
+ throw new Error("BTC wallet returned malformed hex BIP-322 signature");
118
+ return e.toLowerCase();
119
+ }
120
+ if (Ie.test(e)) {
121
+ if (e.length % 2 !== 0)
122
+ throw new Error("BTC wallet returned malformed hex BIP-322 signature");
123
+ return `0x${e.toLowerCase()}`;
124
+ }
125
+ if (!ve.test(e) || e.length % 4 !== 0)
126
+ throw new Error("BTC wallet returned malformed base64 BIP-322 signature");
127
+ const t = at.from(e, "base64");
128
+ if (t.length === 0 || t.toString("base64") !== e)
129
+ throw new Error("BTC wallet returned malformed base64 BIP-322 signature");
130
+ return `0x${t.toString("hex")}`;
131
+ }
132
+ function He(e, t, s, n) {
133
+ const o = s == null ? void 0 : s[`${e}:${t}`];
134
+ return o ? Promise.resolve({
135
+ txid: e,
136
+ vout: t,
137
+ value: o.value,
138
+ scriptPubKey: o.scriptPubKey
139
+ }) : fe(e, t, n);
140
+ }
141
+ const Tt = 12e4;
142
+ class vs {
143
+ /**
144
+ * Creates a new PeginManager instance.
145
+ *
146
+ * @param config - Manager configuration including wallets and contract addresses
147
+ */
148
+ constructor(t) {
149
+ x(this, "config");
150
+ this.config = t;
151
+ }
152
+ /**
153
+ * Prepares a peg-in by building the Pre-PegIn HTLC transaction,
154
+ * funding it, constructing the PegIn transaction, and signing the PegIn input.
155
+ *
156
+ * This method orchestrates the following steps:
157
+ * 1. Get depositor BTC public key from wallet
158
+ * 2. Build unfunded Pre-PegIn transaction (HTLC output) using primitives
159
+ * 3. Select UTXOs to cover the HTLC value
160
+ * 4. Fund the Pre-PegIn transaction
161
+ * 5. Derive the PegIn transaction from the funded Pre-PegIn tx
162
+ * 6. Build PSBT for signing the PegIn input (HTLC leaf 0)
163
+ * 7. Sign via BTC wallet and extract depositor signature
164
+ *
165
+ * The returned `fundedPrePeginTxHex` is funded but unsigned (inputs unsigned).
166
+ * Use `signAndBroadcast()` AFTER registering on Ethereum to broadcast it.
167
+ *
168
+ * @param params - Pegin parameters including amount, HTLC params, UTXOs
169
+ * @returns Pegin result with funded Pre-PegIn tx, signed PegIn input, and signatures
170
+ * @throws Error if wallet operations fail or insufficient funds
171
+ */
172
+ async preparePegin(t) {
173
+ const s = await this.config.btcWallet.getPublicKeyHex(), n = R(s), o = _(t.vaultProviderBtcPubkey), r = t.vaultKeeperBtcPubkeys.map(_), i = t.universalChallengerBtcPubkeys.map(_);
174
+ if (t.hashlocks.length !== t.amounts.length)
175
+ throw new Error(
176
+ `hashlocks.length (${t.hashlocks.length}) must equal amounts.length (${t.amounts.length})`
177
+ );
178
+ if (t.hashlocks.length === 0)
179
+ throw new Error("hashlocks must contain at least one entry");
180
+ const a = r.length, g = {
181
+ depositorPubkey: n,
182
+ vaultProviderPubkey: o,
183
+ vaultKeeperPubkeys: r,
184
+ universalChallengerPubkeys: i,
185
+ hashlocks: t.hashlocks,
186
+ timelockRefund: t.timelockRefund,
187
+ pegInAmounts: t.amounts,
188
+ feeRate: t.protocolFeeRate,
189
+ numLocalChallengers: a,
190
+ councilQuorum: t.councilQuorum,
191
+ councilSize: t.councilSize,
192
+ network: this.config.btcNetwork
193
+ }, u = await te(g), c = ae(
194
+ [...t.availableUTXOs],
195
+ u.totalOutputValue,
196
+ t.mempoolFeeRate,
197
+ le(u.htlcValues.length)
198
+ ), m = bt(this.config.btcNetwork), l = ue({
199
+ unfundedTxHex: u.psbtHex,
200
+ selectedUTXOs: c.selectedUTXOs,
201
+ changeAddress: t.changeAddress,
202
+ changeAmount: c.changeAmount,
203
+ network: m
204
+ }), w = _(z(l)), d = [], b = [], P = [];
205
+ for (let p = 0; p < t.hashlocks.length; p++) {
206
+ const y = await ee({
207
+ prePeginParams: g,
208
+ timelockPegin: t.timelockPegin,
209
+ fundedPrePeginTxHex: l,
210
+ htlcVout: p
211
+ }), T = await se({
212
+ peginTxHex: y.txHex,
213
+ fundedPrePeginTxHex: l,
214
+ depositorPubkey: n,
215
+ vaultProviderPubkey: o,
216
+ vaultKeeperPubkeys: r,
217
+ universalChallengerPubkeys: i,
218
+ hashlock: t.hashlocks[p],
219
+ timelockRefund: t.timelockRefund,
220
+ network: this.config.btcNetwork
221
+ });
222
+ d.push(y), b.push(T.psbtHex), P.push(
223
+ Qt(s, 1)
224
+ );
225
+ }
226
+ const f = await this.signPsbtsWithFallback(
227
+ b,
228
+ P
229
+ ), h = [];
230
+ for (let p = 0; p < f.length; p++) {
231
+ const y = ne(
232
+ f[p],
233
+ n
234
+ ), T = oe(f[p]);
235
+ h.push({
236
+ htlcVout: p,
237
+ htlcValue: u.htlcValues[p],
238
+ peginTxHex: T,
239
+ peginTxid: d[p].txid,
240
+ peginInputSignature: y,
241
+ vaultScriptPubKey: d[p].vaultScriptPubKey
242
+ });
243
+ }
244
+ return {
245
+ fundedPrePeginTxHex: l,
246
+ prePeginTxid: w,
247
+ perVault: h,
248
+ selectedUTXOs: c.selectedUTXOs,
249
+ fee: c.fee,
250
+ changeAmount: c.changeAmount
251
+ };
252
+ }
253
+ /**
254
+ * Signs multiple PSBTs using batch signing if available, falling back to sequential signing.
255
+ *
256
+ * Wallets that support native batch signing (e.g. UniSat) will sign all PSBTs
257
+ * in a single interaction. Others (e.g. Ledger, AppKit) implement signPsbts
258
+ * by looping signPsbt internally, so the UX depends on the wallet adapter.
259
+ */
260
+ async signPsbtsWithFallback(t, s) {
261
+ if (typeof this.config.btcWallet.signPsbts == "function") {
262
+ const o = await this.config.btcWallet.signPsbts(
263
+ t,
264
+ s
265
+ );
266
+ if (o.length !== t.length)
267
+ throw new Error(
268
+ `Expected ${t.length} signed PSBTs but received ${o.length}`
269
+ );
270
+ return o;
271
+ }
272
+ const n = [];
273
+ for (let o = 0; o < t.length; o++) {
274
+ const r = await this.config.btcWallet.signPsbt(
275
+ t[o],
276
+ s[o]
277
+ );
278
+ n.push(r);
279
+ }
280
+ return n;
281
+ }
282
+ /**
283
+ * Signs and broadcasts a funded peg-in transaction to the Bitcoin network.
284
+ *
285
+ * This method:
286
+ * 1. Parses the funded transaction hex
287
+ * 2. Fetches UTXO data from mempool for each input
288
+ * 3. Creates a PSBT with proper witnessUtxo/tapInternalKey
289
+ * 4. Signs via btcWallet.signPsbt()
290
+ * 5. Finalizes and extracts the transaction
291
+ * 6. Broadcasts via mempool API
292
+ *
293
+ * @param params - Transaction hex and depositor public key
294
+ * @returns The broadcasted Bitcoin transaction ID
295
+ * @throws Error if signing or broadcasting fails
296
+ */
297
+ async signAndBroadcast(t) {
298
+ const { fundedPrePeginTxHex: s, depositorBtcPubkey: n } = t, o = s.startsWith("0x") ? s.slice(2) : s, r = Jt.fromHex(o);
299
+ if (r.ins.length === 0)
300
+ throw new Error("Transaction has no inputs");
301
+ const i = new pt();
302
+ i.setVersion(r.version), i.setLocktime(r.locktime);
303
+ const a = at.from(
304
+ R(n),
305
+ "hex"
306
+ ), g = this.config.mempoolApiUrl, u = r.ins.map((h) => {
307
+ const p = at.from(h.hash).reverse().toString("hex"), y = h.index;
308
+ return He(p, y, t.localPrevouts, g).then(
309
+ (T) => ({ input: h, utxoData: T, txid: p, vout: y })
310
+ );
311
+ }), c = await Promise.all(u), m = c.reduce(
312
+ (h, p) => h + BigInt(p.utxoData.value),
313
+ 0n
314
+ ), l = r.outs.reduce(
315
+ (h, p) => h + BigInt(p.value),
316
+ 0n
317
+ );
318
+ if (m < l)
319
+ throw new Error(
320
+ `UTXO value mismatch: total input value (${m} sat) is less than total output value (${l} sat). This may indicate the mempool API returned manipulated UTXO data.`
321
+ );
322
+ const w = m - l;
323
+ if (w > xt)
324
+ throw new Error(
325
+ `Implied transaction fee (${w} sat) exceeds maximum reasonable fee (${xt} sat). This may indicate manipulated UTXO data.`
326
+ );
327
+ for (const { input: h, utxoData: p, txid: y, vout: T } of c) {
328
+ const k = ce(
329
+ {
330
+ value: p.value,
331
+ scriptPubKey: p.scriptPubKey
332
+ },
333
+ a
334
+ );
335
+ i.addInput({
336
+ hash: h.hash,
337
+ index: h.index,
338
+ sequence: h.sequence,
339
+ ...k
340
+ });
341
+ }
342
+ for (const h of r.outs)
343
+ i.addOutput({
344
+ script: h.script,
345
+ value: h.value
346
+ });
347
+ const d = await this.config.btcWallet.signPsbt(i.toHex()), b = pt.fromHex(d);
348
+ try {
349
+ b.finalizeAllInputs();
350
+ } catch (h) {
351
+ if (!b.data.inputs.every(
352
+ (y) => y.finalScriptWitness || y.finalScriptSig
353
+ ))
354
+ throw new Error(
355
+ `PSBT finalization failed and wallet did not auto-finalize: ${h}`
356
+ );
357
+ }
358
+ const P = b.extractTransaction().toHex();
359
+ return await he(P, g);
360
+ }
361
+ /**
362
+ * Registers a peg-in on Ethereum by calling the BTCVaultRegistry contract.
363
+ *
364
+ * This method:
365
+ * 1. Re-verifies the PopSignature against the currently connected ETH
366
+ * and BTC wallets — refuses to proceed if either has changed
367
+ * 2. Derives vault ID and checks if it already exists (pre-flight)
368
+ * 3. Encodes the contract call using viem
369
+ * 4. Estimates gas (catches contract errors early with proper revert
370
+ * reasons)
371
+ * 5. Sends transaction with pre-estimated gas via
372
+ * ethWallet.sendTransaction()
373
+ *
374
+ * The PopSignature must be obtained via
375
+ * {@link signProofOfPossession} before this call.
376
+ *
377
+ * @param params - Registration parameters including the PopSignature
378
+ * and the prepared Pre-PegIn / PegIn transactions
379
+ * @returns Result containing Ethereum transaction hash and vault ID
380
+ * @throws Error if the PopSignature does not match the connected wallets
381
+ * @throws Error if the vault already exists
382
+ * @throws Error if contract simulation fails (e.g., invalid signature,
383
+ * unauthorized)
384
+ */
385
+ async registerPeginOnChain(t) {
386
+ const {
387
+ unsignedPrePeginTx: s,
388
+ depositorSignedPeginTx: n,
389
+ vaultProvider: o,
390
+ hashlock: r,
391
+ htlcVout: i,
392
+ depositorPayoutBtcAddress: a,
393
+ depositorWotsPkHash: g,
394
+ popSignature: u
395
+ } = t;
396
+ if (!this.config.ethWallet.account)
397
+ throw new Error("Ethereum wallet account not found");
398
+ const c = this.config.ethWallet.account.address;
399
+ if (!mt(u.depositorEthAddress, c))
400
+ throw new Error(
401
+ `Proof of possession was signed for ${u.depositorEthAddress} but the Ethereum wallet is currently connected to ${c}. Reconnect the original account or call signProofOfPossession() again.`
402
+ );
403
+ await this.assertPopMatchesBtcWallet(u);
404
+ const m = u.btcPopSignature, l = H(u.depositorBtcPubkey), w = H(s), d = H(n), b = await this.resolvePayoutScriptPubKey(
405
+ a
406
+ ), P = z(d), f = await wt(
407
+ _(P),
408
+ _(c)
409
+ ), h = H(f);
410
+ if (await this.checkVaultExists(h))
411
+ throw new Error(
412
+ `Vault already exists (ID: ${h}, peginTxHash: ${P}). Vault IDs are derived from the pegin transaction hash and depositor address. To create a new vault, use different UTXOs or a different amount to generate a unique transaction.`
413
+ );
414
+ const y = G({
415
+ chain: this.config.ethChain,
416
+ transport: q()
417
+ });
418
+ let T;
419
+ try {
420
+ T = await y.readContract({
421
+ address: this.config.vaultContracts.btcVaultRegistry,
422
+ abi: $,
423
+ functionName: "getPegInFee",
424
+ args: [o]
425
+ });
426
+ } catch {
427
+ throw new Error(
428
+ "Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct."
429
+ );
430
+ }
431
+ const k = yt({
432
+ abi: $,
433
+ functionName: "submitPeginRequest",
434
+ args: [
435
+ c,
436
+ l,
437
+ m,
438
+ w,
439
+ d,
440
+ o,
441
+ r,
442
+ i,
443
+ b,
444
+ g
445
+ ]
446
+ });
447
+ let I;
448
+ try {
449
+ I = await y.estimateGas({
450
+ to: this.config.vaultContracts.btcVaultRegistry,
451
+ data: k,
452
+ value: T,
453
+ account: this.config.ethWallet.account.address
454
+ });
455
+ } catch (j) {
456
+ S(j);
457
+ }
458
+ let v;
459
+ try {
460
+ v = await this.config.ethWallet.sendTransaction({
461
+ to: this.config.vaultContracts.btcVaultRegistry,
462
+ data: k,
463
+ value: T,
464
+ account: this.config.ethWallet.account,
465
+ chain: this.config.ethChain,
466
+ gas: I
467
+ });
468
+ } catch (j) {
469
+ S(j);
470
+ }
471
+ const B = await y.waitForTransactionReceipt({
472
+ hash: v,
473
+ timeout: Tt
474
+ });
475
+ return B.status === "reverted" && S(
476
+ new Error(
477
+ `Transaction reverted. Hash: ${v}. Check the transaction on block explorer for details.`
478
+ )
479
+ ), {
480
+ ethTxHash: B.transactionHash,
481
+ vaultId: h,
482
+ peginTxHash: P
483
+ };
484
+ }
485
+ /**
486
+ * Register multiple pegins on Ethereum in a single transaction.
487
+ *
488
+ * Uses the contract's submitPeginRequestBatch() to submit all vault
489
+ * registrations atomically. All vaults must share the same vault provider.
490
+ * The PoP signature is signed once and included in each request.
491
+ *
492
+ * @param params - Batch registration parameters
493
+ * @returns Batch result with per-vault IDs and single ETH tx hash
494
+ */
495
+ async registerPeginBatchOnChain(t) {
496
+ const { vaultProvider: s, unsignedPrePeginTx: n, requests: o, popSignature: r } = t;
497
+ if (o.length === 0)
498
+ throw new Error("Batch pegin requires at least one request");
499
+ if (!this.config.ethWallet.account)
500
+ throw new Error("Ethereum wallet account not found");
501
+ const i = this.config.ethWallet.account.address;
502
+ if (!mt(r.depositorEthAddress, i))
503
+ throw new Error(
504
+ `Proof of possession was signed for ${r.depositorEthAddress} but the Ethereum wallet is currently connected to ${i}. Reconnect the original account or call signProofOfPossession() again.`
505
+ );
506
+ await this.assertPopMatchesBtcWallet(r);
507
+ const a = r.btcPopSignature, g = [];
508
+ for (const y of o)
509
+ g.push(
510
+ await this.resolvePayoutScriptPubKey(y.depositorPayoutBtcAddress)
511
+ );
512
+ const u = [];
513
+ for (const y of o) {
514
+ const T = H(
515
+ y.depositorSignedPeginTx
516
+ ), k = z(T), I = await wt(
517
+ _(k),
518
+ _(i)
519
+ ), v = H(I);
520
+ if (await this.checkVaultExists(v))
521
+ throw new Error(
522
+ `Vault already exists (ID: ${v}, peginTxHash: ${k}). To create a new vault, use different UTXOs or a different amount.`
523
+ );
524
+ u.push({ vaultId: v, peginTxHash: k });
525
+ }
526
+ const c = G({
527
+ chain: this.config.ethChain,
528
+ transport: q()
529
+ });
530
+ let m;
531
+ try {
532
+ m = await c.readContract({
533
+ address: this.config.vaultContracts.btcVaultRegistry,
534
+ abi: $,
535
+ functionName: "getPegInFee",
536
+ args: [s]
537
+ });
538
+ } catch {
539
+ throw new Error(
540
+ "Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct."
541
+ );
542
+ }
543
+ const l = m * BigInt(o.length), w = H(
544
+ r.depositorBtcPubkey
545
+ ), d = H(n), b = o.map((y, T) => ({
546
+ depositorBtcPubKey: w,
547
+ btcPopSignature: a,
548
+ unsignedPrePeginTx: d,
549
+ depositorSignedPeginTx: H(
550
+ y.depositorSignedPeginTx
551
+ ),
552
+ hashlock: y.hashlock,
553
+ htlcVout: y.htlcVout,
554
+ referralCode: Te,
555
+ depositorPayoutBtcAddress: g[T],
556
+ depositorWotsPkHash: y.depositorWotsPkHash
557
+ })), P = yt({
558
+ abi: $,
559
+ functionName: "submitPeginRequestBatch",
560
+ args: [i, s, b]
561
+ });
562
+ let f;
563
+ try {
564
+ f = await c.estimateGas({
565
+ to: this.config.vaultContracts.btcVaultRegistry,
566
+ data: P,
567
+ value: l,
568
+ account: this.config.ethWallet.account.address
569
+ });
570
+ } catch (y) {
571
+ S(y);
572
+ }
573
+ let h;
574
+ try {
575
+ h = await this.config.ethWallet.sendTransaction({
576
+ to: this.config.vaultContracts.btcVaultRegistry,
577
+ data: P,
578
+ value: l,
579
+ account: this.config.ethWallet.account,
580
+ chain: this.config.ethChain,
581
+ gas: f
582
+ });
583
+ } catch (y) {
584
+ S(y);
585
+ }
586
+ const p = await c.waitForTransactionReceipt({
587
+ hash: h,
588
+ timeout: Tt
589
+ });
590
+ return p.status === "reverted" && S(
591
+ new Error(
592
+ `Batch transaction reverted. Hash: ${h}. Check the transaction on block explorer for details.`
593
+ )
594
+ ), {
595
+ ethTxHash: p.transactionHash,
596
+ vaults: u
597
+ };
598
+ }
599
+ /**
600
+ * Check if a vault already exists for a given vault ID.
601
+ *
602
+ * @param vaultId - The Bitcoin transaction hash (vault ID)
603
+ * @returns True if vault exists, false otherwise
604
+ */
605
+ async checkVaultExists(t) {
606
+ try {
607
+ return (await G({
608
+ chain: this.config.ethChain,
609
+ transport: q()
610
+ }).readContract({
611
+ address: this.config.vaultContracts.btcVaultRegistry,
612
+ abi: $,
613
+ functionName: "getBtcVaultBasicInfo",
614
+ args: [t]
615
+ }))[0] !== Yt;
616
+ } catch {
617
+ return !1;
618
+ }
619
+ }
620
+ /**
621
+ * Resolve the BTC payout address to a scriptPubKey hex for the contract.
622
+ *
623
+ * If a payout address is provided, converts it directly.
624
+ * If omitted, uses the wallet's address and validates it against the
625
+ * wallet's public key to guard against a compromised wallet provider.
626
+ */
627
+ async resolvePayoutScriptPubKey(t) {
628
+ let s;
629
+ if (t)
630
+ s = t;
631
+ else {
632
+ s = await this.config.btcWallet.getAddress();
633
+ const o = await this.config.btcWallet.getPublicKeyHex();
634
+ if (!re(
635
+ s,
636
+ o,
637
+ this.config.btcNetwork
638
+ ))
639
+ throw new Error(
640
+ "The BTC address from your wallet does not match the wallet's public key. Please ensure your wallet is using a supported address type (Taproot or Native SegWit)."
641
+ );
642
+ }
643
+ const n = bt(this.config.btcNetwork);
644
+ try {
645
+ return `0x${Zt.address.toOutputScript(s, n).toString("hex")}`;
646
+ } catch {
647
+ throw new Error(
648
+ `Invalid BTC payout address: "${s}". Please provide a valid Bitcoin address for the ${this.config.btcNetwork} network.`
649
+ );
650
+ }
651
+ }
652
+ /**
653
+ * Sign a BIP-322 BTC Proof-of-Possession binding the connected BTC
654
+ * wallet to the connected ETH account for this chain and vault
655
+ * registry. The returned {@link PopSignature} can be reused across
656
+ * every register call in the same session.
657
+ */
658
+ async signProofOfPossession() {
659
+ if (!this.config.ethWallet.account)
660
+ throw new Error("Ethereum wallet account not found");
661
+ const t = this.config.ethWallet.account.address, s = R(
662
+ await this.config.btcWallet.getPublicKeyHex()
663
+ ), n = this.config.vaultContracts.btcVaultRegistry, o = `${t.toLowerCase()}:${this.config.ethChain.id}:pegin:${n.toLowerCase()}`, r = await this.config.btcWallet.signMessage(
664
+ o,
665
+ "bip322-simple"
666
+ );
667
+ return {
668
+ btcPopSignature: _e(r),
669
+ depositorEthAddress: t,
670
+ depositorBtcPubkey: s
671
+ };
672
+ }
673
+ async assertPopMatchesBtcWallet(t) {
674
+ const s = R(
675
+ await this.config.btcWallet.getPublicKeyHex()
676
+ ), n = R(t.depositorBtcPubkey);
677
+ if (s !== n)
678
+ throw new Error(
679
+ `Proof of possession was signed with BTC pubkey ${n} but the BTC wallet is currently connected to ${s}. Reconnect the original wallet or call signProofOfPossession() again.`
680
+ );
681
+ }
682
+ /**
683
+ * Gets the configured Bitcoin network.
684
+ *
685
+ * @returns The Bitcoin network (mainnet, testnet, signet, regtest)
686
+ */
687
+ getNetwork() {
688
+ return this.config.btcNetwork;
689
+ }
690
+ /**
691
+ * Gets the configured BTCVaultRegistry contract address.
692
+ *
693
+ * @returns The Ethereum address of the BTCVaultRegistry contract
694
+ */
695
+ getVaultContractAddress() {
696
+ return this.config.vaultContracts.btcVaultRegistry;
697
+ }
698
+ }
699
+ class $t {
700
+ constructor(t, s) {
701
+ x(this, "oHash");
702
+ x(this, "iHash");
703
+ x(this, "blockLen");
704
+ x(this, "outputLen");
705
+ x(this, "finished", !1);
706
+ x(this, "destroyed", !1);
707
+ if (St(t), M(s, void 0, "key"), this.iHash = t.create(), typeof this.iHash.update != "function")
708
+ throw new Error("Expected instance of class which extends utils.Hash");
709
+ this.blockLen = this.iHash.blockLen, this.outputLen = this.iHash.outputLen;
710
+ const n = this.blockLen, o = new Uint8Array(n);
711
+ o.set(s.length > n ? t.create().update(s).digest() : s);
712
+ for (let r = 0; r < o.length; r++)
713
+ o[r] ^= 54;
714
+ this.iHash.update(o), this.oHash = t.create();
715
+ for (let r = 0; r < o.length; r++)
716
+ o[r] ^= 106;
717
+ this.oHash.update(o), C(o);
718
+ }
719
+ update(t) {
720
+ return N(this), this.iHash.update(t), this;
721
+ }
722
+ digestInto(t) {
723
+ N(this), M(t, this.outputLen, "output"), this.finished = !0, this.iHash.digestInto(t), this.oHash.update(t), this.oHash.digestInto(t), this.destroy();
724
+ }
725
+ digest() {
726
+ const t = new Uint8Array(this.oHash.outputLen);
727
+ return this.digestInto(t), t;
728
+ }
729
+ _cloneInto(t) {
730
+ t || (t = Object.create(Object.getPrototypeOf(this), {}));
731
+ const { oHash: s, iHash: n, finished: o, destroyed: r, blockLen: i, outputLen: a } = this;
732
+ return t = t, t.finished = o, t.destroyed = r, t.blockLen = i, t.outputLen = a, t.oHash = s._cloneInto(t.oHash), t.iHash = n._cloneInto(t.iHash), t;
733
+ }
734
+ clone() {
735
+ return this._cloneInto();
736
+ }
737
+ destroy() {
738
+ this.destroyed = !0, this.oHash.destroy(), this.iHash.destroy();
739
+ }
740
+ }
741
+ const X = (e, t, s) => new $t(e, t).update(s).digest();
742
+ X.create = (e, t) => new $t(e, t);
743
+ const Be = /* @__PURE__ */ Uint8Array.from([
744
+ 7,
745
+ 4,
746
+ 13,
747
+ 1,
748
+ 10,
749
+ 6,
750
+ 15,
751
+ 3,
752
+ 12,
753
+ 0,
754
+ 9,
755
+ 5,
756
+ 2,
757
+ 14,
758
+ 11,
759
+ 8
760
+ ]), Rt = Uint8Array.from(new Array(16).fill(0).map((e, t) => t)), Se = Rt.map((e) => (9 * e + 5) % 16), Ot = /* @__PURE__ */ (() => {
761
+ const s = [[Rt], [Se]];
762
+ for (let n = 0; n < 4; n++)
763
+ for (let o of s)
764
+ o.push(o[n].map((r) => Be[r]));
765
+ return s;
766
+ })(), Ut = Ot[0], Lt = Ot[1], Wt = /* @__PURE__ */ [
767
+ [11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8],
768
+ [12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7],
769
+ [13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9],
770
+ [14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6],
771
+ [15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5]
772
+ ].map((e) => Uint8Array.from(e)), Ce = /* @__PURE__ */ Ut.map((e, t) => e.map((s) => Wt[t][s])), Ae = /* @__PURE__ */ Lt.map((e, t) => e.map((s) => Wt[t][s])), $e = /* @__PURE__ */ Uint32Array.from([
773
+ 0,
774
+ 1518500249,
775
+ 1859775393,
776
+ 2400959708,
777
+ 2840853838
778
+ ]), Re = /* @__PURE__ */ Uint32Array.from([
779
+ 1352829926,
780
+ 1548603684,
781
+ 1836072691,
782
+ 2053994217,
783
+ 0
784
+ ]);
785
+ function Et(e, t, s, n) {
786
+ return e === 0 ? t ^ s ^ n : e === 1 ? t & s | ~t & n : e === 2 ? (t | ~s) ^ n : e === 3 ? t & n | s & ~n : t ^ (s | ~n);
787
+ }
788
+ const F = /* @__PURE__ */ new Uint32Array(16);
789
+ class Oe extends de {
790
+ constructor() {
791
+ super(64, 20, 8, !0);
792
+ x(this, "h0", 1732584193);
793
+ x(this, "h1", -271733879);
794
+ x(this, "h2", -1732584194);
795
+ x(this, "h3", 271733878);
796
+ x(this, "h4", -1009589776);
797
+ }
798
+ get() {
799
+ const { h0: s, h1: n, h2: o, h3: r, h4: i } = this;
800
+ return [s, n, o, r, i];
801
+ }
802
+ set(s, n, o, r, i) {
803
+ this.h0 = s | 0, this.h1 = n | 0, this.h2 = o | 0, this.h3 = r | 0, this.h4 = i | 0;
804
+ }
805
+ process(s, n) {
806
+ for (let d = 0; d < 16; d++, n += 4)
807
+ F[d] = s.getUint32(n, !0);
808
+ let o = this.h0 | 0, r = o, i = this.h1 | 0, a = i, g = this.h2 | 0, u = g, c = this.h3 | 0, m = c, l = this.h4 | 0, w = l;
809
+ for (let d = 0; d < 5; d++) {
810
+ const b = 4 - d, P = $e[d], f = Re[d], h = Ut[d], p = Lt[d], y = Ce[d], T = Ae[d];
811
+ for (let k = 0; k < 16; k++) {
812
+ const I = W(o + Et(d, i, g, c) + F[h[k]] + P, y[k]) + l | 0;
813
+ o = l, l = c, c = W(g, 10) | 0, g = i, i = I;
814
+ }
815
+ for (let k = 0; k < 16; k++) {
816
+ const I = W(r + Et(b, a, u, m) + F[p[k]] + f, T[k]) + w | 0;
817
+ r = w, w = m, m = W(u, 10) | 0, u = a, a = I;
818
+ }
819
+ }
820
+ this.set(this.h1 + g + m | 0, this.h2 + c + w | 0, this.h3 + l + r | 0, this.h4 + o + a | 0, this.h0 + i + u | 0);
821
+ }
822
+ roundClean() {
823
+ C(F);
824
+ }
825
+ destroy() {
826
+ this.destroyed = !0, C(this.buffer), this.set(0, 0, 0, 0, 0);
827
+ }
828
+ }
829
+ const Ft = /* @__PURE__ */ Ct(() => new Oe()), Ue = BigInt(0), O = BigInt(1), Le = BigInt(2), We = BigInt(7), Fe = BigInt(256), Ve = BigInt(113), Vt = [], Kt = [], Dt = [];
830
+ for (let e = 0, t = O, s = 1, n = 0; e < 24; e++) {
831
+ [s, n] = [n, (2 * s + 3 * n) % 5], Vt.push(2 * (5 * n + s)), Kt.push((e + 1) * (e + 2) / 2 % 64);
832
+ let o = Ue;
833
+ for (let r = 0; r < 7; r++)
834
+ t = (t << O ^ (t >> We) * Ve) % Fe, t & Le && (o ^= O << (O << BigInt(r)) - O);
835
+ Dt.push(o);
836
+ }
837
+ const Mt = ge(Dt, !0), Ke = Mt[0], De = Mt[1], It = (e, t, s) => s > 32 ? be(e, t, s) : ye(e, t, s), vt = (e, t, s) => s > 32 ? xe(e, t, s) : we(e, t, s);
838
+ function Me(e, t = 24) {
839
+ const s = new Uint32Array(10);
840
+ for (let n = 24 - t; n < 24; n++) {
841
+ for (let i = 0; i < 10; i++)
842
+ s[i] = e[i] ^ e[i + 10] ^ e[i + 20] ^ e[i + 30] ^ e[i + 40];
843
+ for (let i = 0; i < 10; i += 2) {
844
+ const a = (i + 8) % 10, g = (i + 2) % 10, u = s[g], c = s[g + 1], m = It(u, c, 1) ^ s[a], l = vt(u, c, 1) ^ s[a + 1];
845
+ for (let w = 0; w < 50; w += 10)
846
+ e[i + w] ^= m, e[i + w + 1] ^= l;
847
+ }
848
+ let o = e[2], r = e[3];
849
+ for (let i = 0; i < 24; i++) {
850
+ const a = Kt[i], g = It(o, r, a), u = vt(o, r, a), c = Vt[i];
851
+ o = e[c], r = e[c + 1], e[c] = g, e[c + 1] = u;
852
+ }
853
+ for (let i = 0; i < 50; i += 10) {
854
+ for (let a = 0; a < 10; a++)
855
+ s[a] = e[i + a];
856
+ for (let a = 0; a < 10; a++)
857
+ e[i + a] ^= ~s[(a + 2) % 10] & s[(a + 4) % 10];
858
+ }
859
+ e[0] ^= Ke[n], e[1] ^= De[n];
860
+ }
861
+ C(s);
862
+ }
863
+ class dt {
864
+ // NOTE: we accept arguments in bytes instead of bits here.
865
+ constructor(t, s, n, o = !1, r = 24) {
866
+ x(this, "state");
867
+ x(this, "pos", 0);
868
+ x(this, "posOut", 0);
869
+ x(this, "finished", !1);
870
+ x(this, "state32");
871
+ x(this, "destroyed", !1);
872
+ x(this, "blockLen");
873
+ x(this, "suffix");
874
+ x(this, "outputLen");
875
+ x(this, "enableXOF", !1);
876
+ x(this, "rounds");
877
+ if (this.blockLen = t, this.suffix = s, this.outputLen = n, this.enableXOF = o, this.rounds = r, L(n, "outputLen"), !(0 < t && t < 200))
878
+ throw new Error("only keccak-f1600 function is supported");
879
+ this.state = new Uint8Array(200), this.state32 = pe(this.state);
880
+ }
881
+ clone() {
882
+ return this._cloneInto();
883
+ }
884
+ keccak() {
885
+ Pt(this.state32), Me(this.state32, this.rounds), Pt(this.state32), this.posOut = 0, this.pos = 0;
886
+ }
887
+ update(t) {
888
+ N(this), M(t);
889
+ const { blockLen: s, state: n } = this, o = t.length;
890
+ for (let r = 0; r < o; ) {
891
+ const i = Math.min(s - this.pos, o - r);
892
+ for (let a = 0; a < i; a++)
893
+ n[this.pos++] ^= t[r++];
894
+ this.pos === s && this.keccak();
895
+ }
896
+ return this;
897
+ }
898
+ finish() {
899
+ if (this.finished)
900
+ return;
901
+ this.finished = !0;
902
+ const { state: t, suffix: s, pos: n, blockLen: o } = this;
903
+ t[n] ^= s, (s & 128) !== 0 && n === o - 1 && this.keccak(), t[o - 1] ^= 128, this.keccak();
904
+ }
905
+ writeInto(t) {
906
+ N(this, !1), M(t), this.finish();
907
+ const s = this.state, { blockLen: n } = this;
908
+ for (let o = 0, r = t.length; o < r; ) {
909
+ this.posOut >= n && this.keccak();
910
+ const i = Math.min(n - this.posOut, r - o);
911
+ t.set(s.subarray(this.posOut, this.posOut + i), o), this.posOut += i, o += i;
912
+ }
913
+ return t;
914
+ }
915
+ xofInto(t) {
916
+ if (!this.enableXOF)
917
+ throw new Error("XOF is not possible for this instance");
918
+ return this.writeInto(t);
919
+ }
920
+ xof(t) {
921
+ return L(t), this.xofInto(new Uint8Array(t));
922
+ }
923
+ digestInto(t) {
924
+ if (me(t, this), this.finished)
925
+ throw new Error("digest() was already called");
926
+ return this.writeInto(t), this.destroy(), t;
927
+ }
928
+ digest() {
929
+ return this.digestInto(new Uint8Array(this.outputLen));
930
+ }
931
+ destroy() {
932
+ this.destroyed = !0, C(this.state);
933
+ }
934
+ _cloneInto(t) {
935
+ const { blockLen: s, suffix: n, outputLen: o, rounds: r, enableXOF: i } = this;
936
+ return t || (t = new dt(s, n, o, i, r)), t.state32.set(this.state32), t.pos = this.pos, t.posOut = this.posOut, t.finished = this.finished, t.rounds = r, t.suffix = n, t.outputLen = o, t.enableXOF = i, t.destroyed = this.destroyed, t;
937
+ }
938
+ }
939
+ const Ne = (e, t, s, n = {}) => Ct(() => new dt(t, e, s), n), Nt = /* @__PURE__ */ Ne(1, 136, 32);
940
+ function Xe(e, t, s, n) {
941
+ St(e);
942
+ const o = ke({ dkLen: 32, asyncTick: 10 }, n), { c: r, dkLen: i, asyncTick: a } = o;
943
+ if (L(r, "c"), L(i, "dkLen"), L(a, "asyncTick"), r < 1)
944
+ throw new Error("iterations (c) must be >= 1");
945
+ const g = kt(t, "password"), u = kt(s, "salt"), c = new Uint8Array(i), m = X.create(e, g), l = m._cloneInto().update(u);
946
+ return { c: r, dkLen: i, asyncTick: a, DK: c, PRF: m, PRFSalt: l };
947
+ }
948
+ function je(e, t, s, n, o) {
949
+ return e.destroy(), t.destroy(), n && n.destroy(), C(o), s;
950
+ }
951
+ function Ge(e, t, s, n) {
952
+ const { c: o, dkLen: r, DK: i, PRF: a, PRFSalt: g } = Xe(e, t, s, n);
953
+ let u;
954
+ const c = new Uint8Array(4), m = Pe(c), l = new Uint8Array(a.outputLen);
955
+ for (let w = 1, d = 0; d < r; w++, d += a.outputLen) {
956
+ const b = i.subarray(d, d + a.outputLen);
957
+ m.setInt32(0, w, !1), (u = g._cloneInto(u)).update(c).digestInto(l), b.set(l.subarray(0, b.length));
958
+ for (let P = 1; P < o; P++) {
959
+ a._cloneInto(u).update(l).digestInto(l);
960
+ for (let f = 0; f < b.length; f++)
961
+ b[f] ^= l[f];
962
+ }
963
+ }
964
+ return je(a, g, i, u, l);
965
+ }
966
+ function Xt(e) {
967
+ if (typeof e != "string")
968
+ throw new TypeError("invalid mnemonic type: " + typeof e);
969
+ return e.normalize("NFKD");
970
+ }
971
+ function qe(e) {
972
+ const t = Xt(e), s = t.split(" ");
973
+ if (![12, 15, 18, 21, 24].includes(s.length))
974
+ throw new Error("Invalid mnemonic");
975
+ return { nfkd: t, words: s };
976
+ }
977
+ const ze = (e) => Xt("mnemonic" + e);
978
+ function Ze(e, t = "") {
979
+ return Ge(ht, qe(e).nfkd, ze(t), { c: 2048, dkLen: 64 });
980
+ }
981
+ const Je = 508, _t = 16, V = 32, K = 64, Ht = 5, Ye = 4;
982
+ function U(...e) {
983
+ const t = e.reduce((o, r) => o + r.length, 0), s = new Uint8Array(t);
984
+ let n = 0;
985
+ for (const o of e)
986
+ s.set(o, n), n += o.length;
987
+ return s;
988
+ }
989
+ function Z(e) {
990
+ return new TextEncoder().encode(e);
991
+ }
992
+ function J(e) {
993
+ const t = new Uint8Array(Ye);
994
+ return new DataView(t.buffer).setUint32(0, e.length, !1), U(t, e);
995
+ }
996
+ function Y(e, t) {
997
+ return X(ht, e, t);
998
+ }
999
+ function Bt(e) {
1000
+ return Ft(At(e));
1001
+ }
1002
+ const ct = (e) => Array.from(e).map((t) => t.toString(16).padStart(2, "0")).join("");
1003
+ function Qe(e) {
1004
+ const t = Ze(e), s = new Uint8Array(t);
1005
+ return t.fill(0), s;
1006
+ }
1007
+ async function ts(e, t, s, n) {
1008
+ if (e.length !== K)
1009
+ throw new Error(
1010
+ `WOTS seed must be ${K} bytes, got ${e.length}`
1011
+ );
1012
+ t = _(t), s = _(s);
1013
+ const o = e.slice(V, K), r = e.slice(0, V), i = [o, r];
1014
+ try {
1015
+ const a = U(
1016
+ J(Z(t)),
1017
+ J(Z(s)),
1018
+ J(Z(n))
1019
+ ), g = U(r, a);
1020
+ i.push(g);
1021
+ const u = Y(o, g);
1022
+ i.push(u);
1023
+ const c = u.slice(0, V), m = u.slice(V, K);
1024
+ i.push(c, m);
1025
+ const l = [], w = [], d = [], b = [];
1026
+ let P = !1;
1027
+ try {
1028
+ for (let f = 0; f < Je; f++) {
1029
+ const h = new Uint8Array(Ht);
1030
+ h[0] = 0, new DataView(h.buffer).setUint32(1, f, !1);
1031
+ const p = new Uint8Array(Ht);
1032
+ p[0] = 1, new DataView(p.buffer).setUint32(1, f, !1);
1033
+ const y = U(c, h), T = U(c, p), k = Y(m, y), I = Y(m, T);
1034
+ try {
1035
+ const v = k.slice(0, _t), B = I.slice(0, _t);
1036
+ l.push(v), w.push(B), d.push(Bt(v)), b.push(Bt(B));
1037
+ } finally {
1038
+ y.fill(0), T.fill(0), k.fill(0), I.fill(0);
1039
+ }
1040
+ }
1041
+ return P = !0, { falsePreimages: l, truePreimages: w, falseHashes: d, trueHashes: b };
1042
+ } finally {
1043
+ if (!P) {
1044
+ for (const f of l) f.fill(0);
1045
+ for (const f of w) f.fill(0);
1046
+ }
1047
+ }
1048
+ } finally {
1049
+ for (const a of i)
1050
+ a.fill(0);
1051
+ }
1052
+ }
1053
+ function _s(e) {
1054
+ return {
1055
+ false_list: e.falseHashes.map(ct),
1056
+ true_list: e.trueHashes.map(ct)
1057
+ };
1058
+ }
1059
+ function es(e) {
1060
+ if (e.falseHashes.length === 0 || e.trueHashes.length === 0)
1061
+ throw new Error(
1062
+ "computeWotsPkHash: keypair hash arrays must not be empty"
1063
+ );
1064
+ const t = e.falseHashes[0].length, s = (e.falseHashes.length + e.trueHashes.length) * t, n = new Uint8Array(s);
1065
+ let o = 0;
1066
+ for (const i of e.falseHashes)
1067
+ n.set(i, o), o += t;
1068
+ for (const i of e.trueHashes)
1069
+ n.set(i, o), o += t;
1070
+ const r = Nt(n);
1071
+ return `0x${ct(r)}`;
1072
+ }
1073
+ const Q = 32, tt = 64, ss = 4, E = 20, lt = 4, jt = 2, ns = 0, os = 1, D = [64, 64];
1074
+ function ut(...e) {
1075
+ const t = e.reduce((o, r) => o + r.length, 0), s = new Uint8Array(t);
1076
+ let n = 0;
1077
+ for (const o of e)
1078
+ s.set(o, n), n += o.length;
1079
+ return s;
1080
+ }
1081
+ function et(e) {
1082
+ return new TextEncoder().encode(e);
1083
+ }
1084
+ function st(e) {
1085
+ const t = new Uint8Array(ss);
1086
+ return new DataView(t.buffer).setUint32(0, e.length, !1), ut(t, e);
1087
+ }
1088
+ function nt(e) {
1089
+ return e.startsWith("0x") || e.startsWith("0X") ? e.slice(2) : e;
1090
+ }
1091
+ const rs = (e) => Array.from(e).map((t) => t.toString(16).padStart(2, "0")).join("");
1092
+ function is(e, t) {
1093
+ return X(ht, e, t);
1094
+ }
1095
+ function gt(e) {
1096
+ return Ft(At(e));
1097
+ }
1098
+ function Gt(e) {
1099
+ return (1 << e) - 1;
1100
+ }
1101
+ function as(e) {
1102
+ let t = 1;
1103
+ for (; t * t < e + 1; ) t++;
1104
+ return Math.max(t, 2);
1105
+ }
1106
+ function cs(e) {
1107
+ const t = lt, s = Gt(t), n = e * s;
1108
+ return { d: t, n: e, checksum_radix: as(n) };
1109
+ }
1110
+ function ot(e, t) {
1111
+ const s = [];
1112
+ let n = t;
1113
+ for (; n > 0; )
1114
+ s.push(n & 255), n >>>= 8;
1115
+ const o = new Uint8Array(e.length + s.length);
1116
+ o.set(e);
1117
+ for (let r = 0; r < s.length; r++)
1118
+ o[e.length + r] = s[r];
1119
+ return gt(o);
1120
+ }
1121
+ function rt(e, t) {
1122
+ let s = e;
1123
+ for (let n = 0; n < t; n++)
1124
+ s = gt(s);
1125
+ return s;
1126
+ }
1127
+ function ls(e, t) {
1128
+ const s = Gt(t.d), n = t.checksum_radix - 1, o = Math.floor(t.n * s / t.checksum_radix), r = [];
1129
+ for (let c = 0; c < t.n; c++) {
1130
+ const m = ot(e, c + jt), l = rt(m, s);
1131
+ r.push(Array.from(l));
1132
+ }
1133
+ const i = ot(
1134
+ e,
1135
+ ns
1136
+ ), a = rt(
1137
+ i,
1138
+ n
1139
+ ), g = ot(
1140
+ e,
1141
+ os
1142
+ ), u = rt(
1143
+ g,
1144
+ o
1145
+ );
1146
+ return {
1147
+ config: t,
1148
+ message_terminals: r,
1149
+ checksum_major_terminal: Array.from(u),
1150
+ checksum_minor_terminal: Array.from(a)
1151
+ };
1152
+ }
1153
+ async function Hs(e, t, s, n) {
1154
+ if (e.length !== tt)
1155
+ throw new Error(
1156
+ `WOTS seed must be exactly ${tt} bytes, got ${e.length}`
1157
+ );
1158
+ const o = nt(t), r = nt(s), i = e.slice(Q, tt), a = e.slice(0, Q), g = ut(
1159
+ a,
1160
+ ut(
1161
+ st(et(o)),
1162
+ st(et(r)),
1163
+ st(et(nt(n)))
1164
+ )
1165
+ ), u = is(i, g), c = u.slice(0, Q);
1166
+ try {
1167
+ const m = [];
1168
+ for (let l = 0; l < D.length; l++) {
1169
+ const w = D[l], d = cs(w), b = new Uint8Array(c.length + 1);
1170
+ b.set(c), b[c.length] = l;
1171
+ const P = gt(b);
1172
+ try {
1173
+ const f = ls(P, d);
1174
+ if (f.config.d !== lt)
1175
+ throw new Error(`Block ${l}: expected d=${lt}, got d=${f.config.d}`);
1176
+ if (f.config.n !== w)
1177
+ throw new Error(`Block ${l}: expected n=${w}, got n=${f.config.n}`);
1178
+ if (f.message_terminals.length !== w)
1179
+ throw new Error(`Block ${l}: expected ${w} message terminals, got ${f.message_terminals.length}`);
1180
+ for (let h = 0; h < f.message_terminals.length; h++)
1181
+ if (f.message_terminals[h].length !== E)
1182
+ throw new Error(`Block ${l} terminal ${h}: expected ${E} bytes, got ${f.message_terminals[h].length}`);
1183
+ if (f.checksum_minor_terminal.length !== E)
1184
+ throw new Error(`Block ${l} checksum_minor: expected ${E} bytes`);
1185
+ if (f.checksum_major_terminal.length !== E)
1186
+ throw new Error(`Block ${l} checksum_major: expected ${E} bytes`);
1187
+ m.push(f);
1188
+ } finally {
1189
+ b.fill(0), P.fill(0);
1190
+ }
1191
+ }
1192
+ if (m.length !== D.length)
1193
+ throw new Error(
1194
+ `Expected ${D.length} blocks, got ${m.length}`
1195
+ );
1196
+ return m;
1197
+ } finally {
1198
+ g.fill(0), i.fill(0), a.fill(0), u.fill(0), c.fill(0), e.fill(0);
1199
+ }
1200
+ }
1201
+ function it(e, t, s) {
1202
+ if (e.length !== E)
1203
+ throw new Error(
1204
+ `Block ${t} ${s}: expected ${E} bytes, got ${e.length}`
1205
+ );
1206
+ for (let n = 0; n < e.length; n++) {
1207
+ const o = e[n];
1208
+ if (!Number.isInteger(o) || o < 0 || o > 255)
1209
+ throw new Error(
1210
+ `Block ${t} ${s}[${n}]: invalid byte value ${o}`
1211
+ );
1212
+ }
1213
+ }
1214
+ function Bs(e) {
1215
+ if (e.length === 0)
1216
+ throw new Error("Public keys array must not be empty");
1217
+ for (let r = 0; r < e.length; r++) {
1218
+ const i = e[r];
1219
+ it(i.checksum_minor_terminal, r, "checksum_minor_terminal"), it(i.checksum_major_terminal, r, "checksum_major_terminal");
1220
+ for (let a = 0; a < i.message_terminals.length; a++)
1221
+ it(i.message_terminals[a], r, `message_terminal[${a}]`);
1222
+ }
1223
+ let t = 0;
1224
+ for (const r of e)
1225
+ t += jt + r.message_terminals.length;
1226
+ const s = new Uint8Array(t * E);
1227
+ let n = 0;
1228
+ for (const r of e) {
1229
+ s.set(r.checksum_minor_terminal, n), n += E, s.set(r.checksum_major_terminal, n), n += E;
1230
+ for (const i of r.message_terminals)
1231
+ s.set(i, n), n += E;
1232
+ }
1233
+ const o = Nt(s);
1234
+ return `0x${rs(o)}`;
1235
+ }
1236
+ async function Ss(e, t, s, n) {
1237
+ const o = Qe(e);
1238
+ try {
1239
+ const r = await ts(
1240
+ o,
1241
+ t,
1242
+ s,
1243
+ n
1244
+ );
1245
+ try {
1246
+ return es(r);
1247
+ } finally {
1248
+ for (const i of r.falsePreimages) i.fill(0);
1249
+ for (const i of r.truePreimages) i.fill(0);
1250
+ }
1251
+ } finally {
1252
+ o.fill(0);
1253
+ }
1254
+ }
1255
+ function Cs(e) {
1256
+ const t = (e instanceof Error ? e.message : typeof e == "string" ? e : "").toLowerCase();
1257
+ return t.includes("wots") && t.includes("hash") && t.includes("does not match");
1258
+ }
1259
+ export {
1260
+ A as C,
1261
+ vs as P,
1262
+ Hs as a,
1263
+ Bs as b,
1264
+ es as c,
1265
+ ts as d,
1266
+ ft as e,
1267
+ Ss as f,
1268
+ Es as g,
1269
+ S as h,
1270
+ Is as i,
1271
+ Cs as j,
1272
+ _s as k,
1273
+ Qe as m
1274
+ };
1275
+ //# sourceMappingURL=errors-rJcuN2m_.js.map