@babylonlabs-io/ts-sdk 0.6.0 → 0.7.0
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/README.md +1 -1
- package/dist/{PayoutManager-CgS9Y0KB.js → PayoutManager-BVmU4UsM.js} +202 -159
- package/dist/PayoutManager-BVmU4UsM.js.map +1 -0
- package/dist/PayoutManager-Ba6cNgHC.cjs +2 -0
- package/dist/PayoutManager-Ba6cNgHC.cjs.map +1 -0
- package/dist/{challengeAssert-BpX9FQ-i.js → challengeAssert-D50t_Qyo.js} +102 -101
- package/dist/challengeAssert-D50t_Qyo.js.map +1 -0
- package/dist/challengeAssert-DDceAi0r.cjs +2 -0
- package/dist/challengeAssert-DDceAi0r.cjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +36 -35
- package/dist/tbv/core/contracts/abis/{BTCVaultsManager.abi.d.ts → BTCVaultRegistry.abi.d.ts} +41 -9
- package/dist/tbv/core/contracts/abis/BTCVaultRegistry.abi.d.ts.map +1 -0
- package/dist/tbv/core/contracts/index.d.ts +1 -1
- package/dist/tbv/core/index.cjs +1 -1
- package/dist/tbv/core/index.js +38 -37
- package/dist/tbv/core/managers/PeginManager.d.ts +21 -20
- package/dist/tbv/core/managers/PeginManager.d.ts.map +1 -1
- package/dist/tbv/core/managers/__tests__/PeginManager.test.d.ts +1 -1
- package/dist/tbv/core/managers/index.d.ts +4 -4
- package/dist/tbv/core/managers/index.d.ts.map +1 -1
- package/dist/tbv/core/primitives/index.cjs +1 -1
- package/dist/tbv/core/primitives/index.d.ts +1 -1
- package/dist/tbv/core/primitives/index.d.ts.map +1 -1
- package/dist/tbv/core/primitives/index.js +10 -9
- package/dist/tbv/core/primitives/psbt/pegin.d.ts +11 -8
- package/dist/tbv/core/primitives/psbt/pegin.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/peginInput.d.ts +1 -1
- package/dist/tbv/core/primitives/psbt/peginInput.d.ts.map +1 -1
- package/dist/tbv/core/utils/transaction/btcTxHash.d.ts +1 -1
- package/dist/tbv/index.cjs +1 -1
- package/dist/tbv/index.js +38 -37
- package/dist/tbv/integrations/aave/clients/abis/AaveIntegrationController.abi.json.d.ts +7 -2
- package/dist/tbv/integrations/aave/index.cjs +1 -1
- package/dist/tbv/integrations/aave/index.cjs.map +1 -1
- package/dist/tbv/integrations/aave/index.js +3 -3
- package/package.json +3 -3
- package/dist/PayoutManager-AJJ-3vvu.cjs +0 -2
- package/dist/PayoutManager-AJJ-3vvu.cjs.map +0 -1
- package/dist/PayoutManager-CgS9Y0KB.js.map +0 -1
- package/dist/challengeAssert-BMSvVtzu.cjs +0 -2
- package/dist/challengeAssert-BMSvVtzu.cjs.map +0 -1
- package/dist/challengeAssert-BpX9FQ-i.js.map +0 -1
- package/dist/tbv/core/contracts/abis/BTCVaultsManager.abi.d.ts.map +0 -1
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
var q = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import * as
|
|
5
|
-
import { script as
|
|
6
|
-
import { Buffer as
|
|
7
|
-
import { P as
|
|
8
|
-
import { createPublicClient as
|
|
9
|
-
import "@babylonlabs-io/babylon-tbv-rust-wasm";
|
|
10
|
-
function
|
|
2
|
+
var L = (n, t, e) => t in n ? q(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var B = (n, t, e) => L(n, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
import * as j from "bitcoinjs-lib";
|
|
5
|
+
import { script as J, Transaction as I, address as Z, Psbt as C } from "bitcoinjs-lib";
|
|
6
|
+
import { Buffer as g } from "buffer";
|
|
7
|
+
import { P as G, x as Q, D as _, M as R, B as Y, T as tt, A as U, t as k, b as et, z as nt, a as st, d as rt, e as at, f as ot, r as H, p as it, w as V, g as W, h as K } from "./challengeAssert-D50t_Qyo.js";
|
|
8
|
+
import { createPublicClient as N, http as M, encodeFunctionData as ct, zeroAddress as ut } from "viem";
|
|
9
|
+
import { SINGLE_DEPOSIT_HTLC_VOUT as X } from "@babylonlabs-io/babylon-tbv-rust-wasm";
|
|
10
|
+
function lt(n, t, e) {
|
|
11
11
|
if (n.length === 0)
|
|
12
12
|
throw new Error("Insufficient funds: no UTXOs available");
|
|
13
13
|
const s = n.filter((a) => {
|
|
14
|
-
const u =
|
|
15
|
-
return !!
|
|
14
|
+
const u = g.from(a.scriptPubKey, "hex");
|
|
15
|
+
return !!J.decompile(u);
|
|
16
16
|
});
|
|
17
17
|
if (s.length === 0)
|
|
18
18
|
throw new Error(
|
|
@@ -22,20 +22,20 @@ function ut(n, t, e) {
|
|
|
22
22
|
let c = 0n, i = 0n;
|
|
23
23
|
for (const a of o) {
|
|
24
24
|
r.push(a), c += BigInt(a.value);
|
|
25
|
-
const u = r.length *
|
|
26
|
-
if (i = BigInt(Math.ceil(p * e)) + BigInt(
|
|
27
|
-
const
|
|
28
|
-
Math.ceil(
|
|
25
|
+
const u = r.length * G, l = 2 * R, p = u + l + tt;
|
|
26
|
+
if (i = BigInt(Math.ceil(p * e)) + BigInt(Q(e)), c - t - i > _) {
|
|
27
|
+
const f = BigInt(
|
|
28
|
+
Math.ceil(R * e)
|
|
29
29
|
);
|
|
30
|
-
i +=
|
|
30
|
+
i += f;
|
|
31
31
|
}
|
|
32
32
|
if (c >= t + i) {
|
|
33
|
-
const
|
|
33
|
+
const f = c - t - i;
|
|
34
34
|
return {
|
|
35
35
|
selectedUTXOs: r,
|
|
36
36
|
totalValue: c,
|
|
37
37
|
fee: i,
|
|
38
|
-
changeAmount:
|
|
38
|
+
changeAmount: f
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
41
|
}
|
|
@@ -43,17 +43,17 @@ function ut(n, t, e) {
|
|
|
43
43
|
`Insufficient funds: need ${t + i} sats (${t} pegin + ${i} fee), have ${c} sats`
|
|
44
44
|
);
|
|
45
45
|
}
|
|
46
|
-
function
|
|
47
|
-
return n >
|
|
46
|
+
function vt(n) {
|
|
47
|
+
return n > _;
|
|
48
48
|
}
|
|
49
|
-
function
|
|
50
|
-
return
|
|
49
|
+
function kt() {
|
|
50
|
+
return Y;
|
|
51
51
|
}
|
|
52
|
-
function
|
|
52
|
+
function D(n) {
|
|
53
53
|
const t = n.startsWith("0x") ? n.slice(2) : n;
|
|
54
|
-
return `0x${
|
|
54
|
+
return `0x${I.fromHex(t).getId()}`;
|
|
55
55
|
}
|
|
56
|
-
function
|
|
56
|
+
function Et(n, t, e) {
|
|
57
57
|
if (n.length === 0)
|
|
58
58
|
throw new Error("No input UTXOs provided for split transaction");
|
|
59
59
|
if (t.length === 0)
|
|
@@ -63,10 +63,10 @@ function kt(n, t, e) {
|
|
|
63
63
|
throw new Error(
|
|
64
64
|
`Invalid output amount for ${a.address}: ${a.amount} satoshis. Amount must be greater than zero.`
|
|
65
65
|
);
|
|
66
|
-
const s = U(e), o = new
|
|
66
|
+
const s = U(e), o = new I();
|
|
67
67
|
o.version = 2;
|
|
68
68
|
for (const a of n) {
|
|
69
|
-
const u =
|
|
69
|
+
const u = g.from(a.txid, "hex").reverse();
|
|
70
70
|
o.addInput(u, a.vout);
|
|
71
71
|
}
|
|
72
72
|
const r = [];
|
|
@@ -74,7 +74,7 @@ function kt(n, t, e) {
|
|
|
74
74
|
const u = t[a];
|
|
75
75
|
let l;
|
|
76
76
|
try {
|
|
77
|
-
l =
|
|
77
|
+
l = Z.toOutputScript(u.address, s);
|
|
78
78
|
} catch (p) {
|
|
79
79
|
throw new Error(
|
|
80
80
|
`Failed to decode address "${u.address}": ${p instanceof Error ? p.message : String(p)}`
|
|
@@ -97,11 +97,11 @@ function kt(n, t, e) {
|
|
|
97
97
|
outputs: r
|
|
98
98
|
};
|
|
99
99
|
}
|
|
100
|
-
function
|
|
101
|
-
const s =
|
|
102
|
-
if (o.setVersion(s.version), o.setLocktime(s.locktime), !
|
|
100
|
+
function St(n, t, e) {
|
|
101
|
+
const s = I.fromHex(n), o = new C();
|
|
102
|
+
if (o.setVersion(s.version), o.setLocktime(s.locktime), !g.isBuffer(e) || e.length !== 32)
|
|
103
103
|
throw new Error(
|
|
104
|
-
`Invalid publicKeyNoCoord: expected 32-byte Buffer (x-only pubkey), got ${
|
|
104
|
+
`Invalid publicKeyNoCoord: expected 32-byte Buffer (x-only pubkey), got ${g.isBuffer(e) ? `${e.length}-byte Buffer` : typeof e}`
|
|
105
105
|
);
|
|
106
106
|
if (t.length !== s.ins.length)
|
|
107
107
|
throw new Error(
|
|
@@ -111,26 +111,26 @@ function Bt(n, t, e) {
|
|
|
111
111
|
const c = s.ins[r], i = t[r];
|
|
112
112
|
if (!i)
|
|
113
113
|
throw new Error(`Missing UTXO data for input ${r}`);
|
|
114
|
-
const a =
|
|
114
|
+
const a = g.from(c.hash).reverse().toString("hex"), u = c.index;
|
|
115
115
|
if (i.txid !== a || i.vout !== u)
|
|
116
116
|
throw new Error(
|
|
117
117
|
`Input ${r} outpoint mismatch: transaction expects ${a}:${u}, but UTXO ${i.txid}:${i.vout} was provided. Ensure inputs array matches the order used in createSplitTransaction().`
|
|
118
118
|
);
|
|
119
|
-
const l =
|
|
119
|
+
const l = g.from(i.scriptPubKey, "hex");
|
|
120
120
|
if (!(l.length === 34 && l[0] === 81 && // OP_1 (witness version 1)
|
|
121
121
|
l[1] === 32))
|
|
122
122
|
throw new Error(
|
|
123
123
|
`Input ${r} must be P2TR (Taproot). createSplitTransactionPsbt() requires P2TR inputs because it uses tapInternalKey for Taproot signing. ScriptPubKey: ${i.scriptPubKey.substring(0, 20)}...`
|
|
124
124
|
);
|
|
125
|
-
const
|
|
126
|
-
script:
|
|
125
|
+
const h = {
|
|
126
|
+
script: g.from(i.scriptPubKey, "hex"),
|
|
127
127
|
value: i.value
|
|
128
128
|
};
|
|
129
129
|
o.addInput({
|
|
130
130
|
hash: c.hash,
|
|
131
131
|
index: c.index,
|
|
132
132
|
sequence: c.sequence,
|
|
133
|
-
witnessUtxo:
|
|
133
|
+
witnessUtxo: h,
|
|
134
134
|
tapInternalKey: e
|
|
135
135
|
});
|
|
136
136
|
}
|
|
@@ -141,8 +141,8 @@ function Bt(n, t, e) {
|
|
|
141
141
|
});
|
|
142
142
|
return o.toHex();
|
|
143
143
|
}
|
|
144
|
-
var
|
|
145
|
-
function
|
|
144
|
+
var E = /* @__PURE__ */ ((n) => (n.P2PKH = "P2PKH", n.P2SH = "P2SH", n.P2WPKH = "P2WPKH", n.P2WSH = "P2WSH", n.P2TR = "P2TR", n.UNKNOWN = "UNKNOWN", n))(E || {});
|
|
145
|
+
function dt(n) {
|
|
146
146
|
const t = n.length;
|
|
147
147
|
return t === 25 && n[0] === 118 && // OP_DUP
|
|
148
148
|
n[1] === 169 && // OP_HASH160
|
|
@@ -155,17 +155,17 @@ function lt(n) {
|
|
|
155
155
|
n[1] === 32 ? "P2WSH" : t === 34 && n[0] === 81 && // OP_1
|
|
156
156
|
n[1] === 32 ? "P2TR" : "UNKNOWN";
|
|
157
157
|
}
|
|
158
|
-
function
|
|
159
|
-
const e =
|
|
158
|
+
function pt(n, t) {
|
|
159
|
+
const e = g.from(n.scriptPubKey, "hex"), s = dt(e);
|
|
160
160
|
switch (s) {
|
|
161
|
-
case
|
|
161
|
+
case E.P2WPKH:
|
|
162
162
|
return {
|
|
163
163
|
witnessUtxo: {
|
|
164
164
|
script: e,
|
|
165
165
|
value: n.value
|
|
166
166
|
}
|
|
167
167
|
};
|
|
168
|
-
case
|
|
168
|
+
case E.P2WSH: {
|
|
169
169
|
if (!n.witnessScript)
|
|
170
170
|
throw new Error("Missing witnessScript for P2WSH input");
|
|
171
171
|
return {
|
|
@@ -173,10 +173,10 @@ function dt(n, t) {
|
|
|
173
173
|
script: e,
|
|
174
174
|
value: n.value
|
|
175
175
|
},
|
|
176
|
-
witnessScript:
|
|
176
|
+
witnessScript: g.from(n.witnessScript, "hex")
|
|
177
177
|
};
|
|
178
178
|
}
|
|
179
|
-
case
|
|
179
|
+
case E.P2TR: {
|
|
180
180
|
if (t && t.length !== 32)
|
|
181
181
|
throw new Error(
|
|
182
182
|
`Invalid tapInternalKey length: expected 32 bytes, got ${t.length}`
|
|
@@ -194,12 +194,12 @@ function dt(n, t) {
|
|
|
194
194
|
throw new Error(`Unsupported script type: ${s}`);
|
|
195
195
|
}
|
|
196
196
|
}
|
|
197
|
-
const
|
|
197
|
+
const ht = {
|
|
198
198
|
mainnet: "https://mempool.space/api",
|
|
199
199
|
testnet: "https://mempool.space/testnet/api",
|
|
200
200
|
signet: "https://mempool.space/signet/api"
|
|
201
201
|
};
|
|
202
|
-
async function
|
|
202
|
+
async function S(n, t) {
|
|
203
203
|
try {
|
|
204
204
|
const e = await fetch(n, t);
|
|
205
205
|
if (!e.ok) {
|
|
@@ -214,7 +214,7 @@ async function E(n, t) {
|
|
|
214
214
|
throw e instanceof Error ? new Error(`Failed to fetch from mempool API: ${e.message}`) : new Error("Failed to fetch from mempool API: Unknown error");
|
|
215
215
|
}
|
|
216
216
|
}
|
|
217
|
-
async function
|
|
217
|
+
async function yt(n, t) {
|
|
218
218
|
try {
|
|
219
219
|
const e = await fetch(`${t}/tx`, {
|
|
220
220
|
method: "POST",
|
|
@@ -241,9 +241,9 @@ async function ht(n, t) {
|
|
|
241
241
|
}
|
|
242
242
|
}
|
|
243
243
|
async function gt(n, t) {
|
|
244
|
-
return
|
|
244
|
+
return S(`${t}/tx/${n}`);
|
|
245
245
|
}
|
|
246
|
-
async function
|
|
246
|
+
async function It(n, t) {
|
|
247
247
|
try {
|
|
248
248
|
const e = await fetch(`${t}/tx/${n}/hex`);
|
|
249
249
|
if (!e.ok) {
|
|
@@ -271,9 +271,9 @@ async function ft(n, t, e) {
|
|
|
271
271
|
scriptPubKey: o.scriptpubkey
|
|
272
272
|
};
|
|
273
273
|
}
|
|
274
|
-
async function
|
|
274
|
+
async function Bt(n, t) {
|
|
275
275
|
try {
|
|
276
|
-
const e = await
|
|
276
|
+
const e = await S(`${t}/address/${n}/utxo`), s = await S(`${t}/v1/validate-address/${n}`);
|
|
277
277
|
if (!s.isvalid)
|
|
278
278
|
throw new Error(
|
|
279
279
|
`Invalid Bitcoin address: ${n}. Mempool API validation failed.`
|
|
@@ -294,12 +294,12 @@ async function St(n, t) {
|
|
|
294
294
|
}
|
|
295
295
|
}
|
|
296
296
|
function Ht(n) {
|
|
297
|
-
return
|
|
297
|
+
return ht[n];
|
|
298
298
|
}
|
|
299
|
-
async function
|
|
300
|
-
return
|
|
299
|
+
async function $t(n, t) {
|
|
300
|
+
return S(`${t}/address/${n}/txs`);
|
|
301
301
|
}
|
|
302
|
-
async function
|
|
302
|
+
async function Ct(n) {
|
|
303
303
|
const t = await fetch(`${n}/v1/fees/recommended`);
|
|
304
304
|
if (!t.ok)
|
|
305
305
|
throw new Error(
|
|
@@ -352,6 +352,11 @@ const $ = [
|
|
|
352
352
|
type: "bytes32",
|
|
353
353
|
internalType: "bytes32"
|
|
354
354
|
},
|
|
355
|
+
{
|
|
356
|
+
name: "htlcVout",
|
|
357
|
+
type: "uint8",
|
|
358
|
+
internalType: "uint8"
|
|
359
|
+
},
|
|
355
360
|
{
|
|
356
361
|
name: "depositorPayoutBtcAddress",
|
|
357
362
|
type: "bytes",
|
|
@@ -411,6 +416,11 @@ const $ = [
|
|
|
411
416
|
type: "bytes32",
|
|
412
417
|
internalType: "bytes32"
|
|
413
418
|
},
|
|
419
|
+
{
|
|
420
|
+
name: "htlcVout",
|
|
421
|
+
type: "uint8",
|
|
422
|
+
internalType: "uint8"
|
|
423
|
+
},
|
|
414
424
|
{
|
|
415
425
|
name: "referralCode",
|
|
416
426
|
type: "uint32",
|
|
@@ -487,23 +497,24 @@ const $ = [
|
|
|
487
497
|
{
|
|
488
498
|
name: "vault",
|
|
489
499
|
type: "tuple",
|
|
490
|
-
internalType: "struct
|
|
500
|
+
internalType: "struct IBTCVaultRegistry.BTCVault",
|
|
491
501
|
components: [
|
|
492
502
|
{ name: "depositor", type: "address", internalType: "address" },
|
|
493
503
|
{ name: "depositorBtcPubKey", type: "bytes32", internalType: "bytes32" },
|
|
494
504
|
{ name: "depositorSignedPeginTx", type: "bytes", internalType: "bytes" },
|
|
495
505
|
{ name: "amount", type: "uint256", internalType: "uint256" },
|
|
496
506
|
{ name: "vaultProvider", type: "address", internalType: "address" },
|
|
497
|
-
{ name: "status", type: "uint8", internalType: "enum
|
|
498
|
-
{ name: "
|
|
507
|
+
{ name: "status", type: "uint8", internalType: "enum IBTCVaultRegistry.BTCVaultStatus" },
|
|
508
|
+
{ name: "applicationEntryPoint", type: "address", internalType: "address" },
|
|
499
509
|
{ name: "universalChallengersVersion", type: "uint16", internalType: "uint16" },
|
|
500
510
|
{ name: "appVaultKeepersVersion", type: "uint16", internalType: "uint16" },
|
|
501
511
|
{ name: "offchainParamsVersion", type: "uint16", internalType: "uint16" },
|
|
502
|
-
{ name: "
|
|
512
|
+
{ name: "proverProgramVersion", type: "uint16", internalType: "uint16" },
|
|
503
513
|
{ name: "createdAt", type: "uint256", internalType: "uint256" },
|
|
504
514
|
{ name: "verifiedAt", type: "uint256", internalType: "uint256" },
|
|
505
515
|
{ name: "depositorLamportPkHash", type: "bytes32", internalType: "bytes32" },
|
|
506
|
-
{ name: "hashlock", type: "bytes32", internalType: "bytes32" }
|
|
516
|
+
{ name: "hashlock", type: "bytes32", internalType: "bytes32" },
|
|
517
|
+
{ name: "htlcVout", type: "uint8", internalType: "uint8" }
|
|
507
518
|
]
|
|
508
519
|
}
|
|
509
520
|
],
|
|
@@ -534,8 +545,33 @@ const $ = [
|
|
|
534
545
|
type: "error",
|
|
535
546
|
name: "ActivationDeadlineExpired",
|
|
536
547
|
inputs: []
|
|
548
|
+
},
|
|
549
|
+
{
|
|
550
|
+
type: "error",
|
|
551
|
+
name: "InvalidHashlock",
|
|
552
|
+
inputs: []
|
|
553
|
+
},
|
|
554
|
+
{
|
|
555
|
+
type: "error",
|
|
556
|
+
name: "DuplicateHashlock",
|
|
557
|
+
inputs: []
|
|
558
|
+
},
|
|
559
|
+
{
|
|
560
|
+
type: "error",
|
|
561
|
+
name: "CapExceeded",
|
|
562
|
+
inputs: []
|
|
563
|
+
},
|
|
564
|
+
{
|
|
565
|
+
type: "error",
|
|
566
|
+
name: "InvalidOutputIndex",
|
|
567
|
+
inputs: []
|
|
568
|
+
},
|
|
569
|
+
{
|
|
570
|
+
type: "error",
|
|
571
|
+
name: "PeginSignaturesIncomplete",
|
|
572
|
+
inputs: []
|
|
537
573
|
}
|
|
538
|
-
],
|
|
574
|
+
], m = {
|
|
539
575
|
// VaultAlreadyExists()
|
|
540
576
|
"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.",
|
|
541
577
|
// ScriptPubKeyMismatch() - taproot output doesn't match expected script
|
|
@@ -584,24 +620,24 @@ function O(n) {
|
|
|
584
620
|
if (c)
|
|
585
621
|
return c[1];
|
|
586
622
|
}
|
|
587
|
-
function
|
|
623
|
+
function Ut(n) {
|
|
588
624
|
const t = O(n);
|
|
589
625
|
if (t) {
|
|
590
626
|
const e = t.substring(0, 10);
|
|
591
|
-
return
|
|
627
|
+
return m[t] ?? m[e];
|
|
592
628
|
}
|
|
593
629
|
}
|
|
594
|
-
function
|
|
630
|
+
function Ot(n) {
|
|
595
631
|
const t = O(n);
|
|
596
632
|
if (t === void 0) return !1;
|
|
597
633
|
const e = t.substring(0, 10);
|
|
598
|
-
return t in
|
|
634
|
+
return t in m || e in m;
|
|
599
635
|
}
|
|
600
636
|
function z(n) {
|
|
601
637
|
console.error("[Contract Error] Raw error:", n);
|
|
602
638
|
const t = O(n);
|
|
603
639
|
if (console.error("[Contract Error] Extracted error data:", t), t) {
|
|
604
|
-
const s = t.substring(0, 10), o =
|
|
640
|
+
const s = t.substring(0, 10), o = m[t] ?? m[s];
|
|
605
641
|
if (o)
|
|
606
642
|
throw console.error("[Contract Error] Known error:", o), new Error(o);
|
|
607
643
|
}
|
|
@@ -619,18 +655,18 @@ function z(n) {
|
|
|
619
655
|
}
|
|
620
656
|
throw n instanceof Error ? (console.error("[Contract Error] Unhandled error:", n.message), n) : new Error(`Contract call failed: ${String(n)}`);
|
|
621
657
|
}
|
|
622
|
-
class
|
|
658
|
+
class Ft {
|
|
623
659
|
/**
|
|
624
660
|
* Creates a new PeginManager instance.
|
|
625
661
|
*
|
|
626
662
|
* @param config - Manager configuration including wallets and contract addresses
|
|
627
663
|
*/
|
|
628
664
|
constructor(t) {
|
|
629
|
-
|
|
665
|
+
B(this, "config");
|
|
630
666
|
this.config = t;
|
|
631
667
|
}
|
|
632
668
|
/**
|
|
633
|
-
* Prepares
|
|
669
|
+
* Prepares a peg-in by building the Pre-PegIn HTLC transaction,
|
|
634
670
|
* funding it, constructing the PegIn transaction, and signing the PegIn input.
|
|
635
671
|
*
|
|
636
672
|
* This method orchestrates the following steps:
|
|
@@ -645,17 +681,22 @@ class Ot {
|
|
|
645
681
|
* The returned `fundedPrePeginTxHex` is funded but unsigned (inputs unsigned).
|
|
646
682
|
* Use `signAndBroadcast()` AFTER registering on Ethereum to broadcast it.
|
|
647
683
|
*
|
|
648
|
-
* @param params -
|
|
649
|
-
* @returns
|
|
684
|
+
* @param params - Pegin parameters including amount, HTLC params, UTXOs
|
|
685
|
+
* @returns Pegin result with funded Pre-PegIn tx, signed PegIn input, and signatures
|
|
650
686
|
* @throws Error if wallet operations fail or insufficient funds
|
|
651
687
|
*/
|
|
652
|
-
async
|
|
653
|
-
const e = await this.config.btcWallet.getPublicKeyHex(), s = e.length === 66 ? e.slice(2) : e, o = k(t.vaultProviderBtcPubkey), r = t.vaultKeeperBtcPubkeys.map(k), c = t.universalChallengerBtcPubkeys.map(k)
|
|
688
|
+
async preparePegin(t) {
|
|
689
|
+
const e = await this.config.btcWallet.getPublicKeyHex(), s = e.length === 66 ? e.slice(2) : e, o = k(t.vaultProviderBtcPubkey), r = t.vaultKeeperBtcPubkeys.map(k), c = t.universalChallengerBtcPubkeys.map(k);
|
|
690
|
+
if (t.hashlocks.length !== 1)
|
|
691
|
+
throw new Error(
|
|
692
|
+
"hashlocks must contain exactly one entry (batched deposits not yet supported)"
|
|
693
|
+
);
|
|
694
|
+
const i = r.length, a = {
|
|
654
695
|
depositorPubkey: s,
|
|
655
696
|
vaultProviderPubkey: o,
|
|
656
697
|
vaultKeeperPubkeys: r,
|
|
657
698
|
universalChallengerPubkeys: c,
|
|
658
|
-
|
|
699
|
+
hashlocks: t.hashlocks,
|
|
659
700
|
timelockRefund: t.timelockRefund,
|
|
660
701
|
pegInAmount: t.amount,
|
|
661
702
|
feeRate: t.protocolFeeRate,
|
|
@@ -663,32 +704,33 @@ class Ot {
|
|
|
663
704
|
councilQuorum: t.councilQuorum,
|
|
664
705
|
councilSize: t.councilSize,
|
|
665
706
|
network: this.config.btcNetwork
|
|
666
|
-
}, u = await
|
|
707
|
+
}, u = await et(a), l = lt(
|
|
667
708
|
[...t.availableUTXOs],
|
|
668
709
|
u.totalOutputValue,
|
|
669
710
|
t.mempoolFeeRate
|
|
670
|
-
), p = U(this.config.btcNetwork),
|
|
711
|
+
), p = U(this.config.btcNetwork), h = nt({
|
|
671
712
|
unfundedTxHex: u.psbtHex,
|
|
672
713
|
selectedUTXOs: l.selectedUTXOs,
|
|
673
714
|
changeAddress: t.changeAddress,
|
|
674
715
|
changeAmount: l.changeAmount,
|
|
675
716
|
network: p
|
|
676
|
-
}),
|
|
717
|
+
}), f = k(D(h)), P = await st({
|
|
677
718
|
prePeginParams: a,
|
|
678
719
|
timelockPegin: t.timelockPegin,
|
|
679
|
-
|
|
680
|
-
|
|
720
|
+
fundedPrePeginTxHex: h,
|
|
721
|
+
htlcVout: X
|
|
722
|
+
}), x = await rt({
|
|
681
723
|
peginTxHex: P.txHex,
|
|
682
|
-
fundedPrePeginTxHex:
|
|
724
|
+
fundedPrePeginTxHex: h,
|
|
683
725
|
depositorPubkey: s,
|
|
684
726
|
vaultProviderPubkey: o,
|
|
685
727
|
vaultKeeperPubkeys: r,
|
|
686
728
|
universalChallengerPubkeys: c,
|
|
687
|
-
|
|
729
|
+
hashlock: t.hashlocks[0],
|
|
688
730
|
timelockRefund: t.timelockRefund,
|
|
689
731
|
network: this.config.btcNetwork
|
|
690
732
|
}), d = await this.config.btcWallet.signPsbt(
|
|
691
|
-
|
|
733
|
+
x.psbtHex,
|
|
692
734
|
{
|
|
693
735
|
autoFinalized: !1,
|
|
694
736
|
signInputs: [
|
|
@@ -699,18 +741,18 @@ class Ot {
|
|
|
699
741
|
}
|
|
700
742
|
]
|
|
701
743
|
}
|
|
702
|
-
),
|
|
744
|
+
), y = at(
|
|
703
745
|
d,
|
|
704
746
|
s
|
|
705
|
-
), b =
|
|
747
|
+
), b = ot(d);
|
|
706
748
|
return {
|
|
707
|
-
fundedPrePeginTxHex:
|
|
749
|
+
fundedPrePeginTxHex: h,
|
|
708
750
|
htlcValue: u.htlcValue,
|
|
709
751
|
signedPeginInputPsbtHex: d,
|
|
710
|
-
peginInputSignature:
|
|
752
|
+
peginInputSignature: y,
|
|
711
753
|
vaultScriptPubKey: P.vaultScriptPubKey,
|
|
712
754
|
peginTxHex: b,
|
|
713
|
-
prePeginTxid:
|
|
755
|
+
prePeginTxid: f,
|
|
714
756
|
peginTxid: P.txid,
|
|
715
757
|
selectedUTXOs: l.selectedUTXOs,
|
|
716
758
|
fee: l.fee,
|
|
@@ -733,7 +775,7 @@ class Ot {
|
|
|
733
775
|
* @throws Error if signing or broadcasting fails
|
|
734
776
|
*/
|
|
735
777
|
async signAndBroadcast(t) {
|
|
736
|
-
const { fundedPrePeginTxHex: e, depositorBtcPubkey: s } = t, o = e.startsWith("0x") ? e.slice(2) : e, r =
|
|
778
|
+
const { fundedPrePeginTxHex: e, depositorBtcPubkey: s } = t, o = e.startsWith("0x") ? e.slice(2) : e, r = I.fromHex(o);
|
|
737
779
|
if (r.ins.length === 0)
|
|
738
780
|
throw new Error("Transaction has no inputs");
|
|
739
781
|
const c = new C();
|
|
@@ -743,25 +785,25 @@ class Ot {
|
|
|
743
785
|
throw new Error(
|
|
744
786
|
"Invalid depositorBtcPubkey: expected 64 hex characters (x-only pubkey)"
|
|
745
787
|
);
|
|
746
|
-
const a =
|
|
788
|
+
const a = g.from(i, "hex");
|
|
747
789
|
if (a.length !== 32)
|
|
748
790
|
throw new Error(
|
|
749
791
|
`Invalid depositorBtcPubkey length: expected 32 bytes, got ${a.length}`
|
|
750
792
|
);
|
|
751
793
|
const u = this.config.mempoolApiUrl, l = r.ins.map((d) => {
|
|
752
|
-
const
|
|
753
|
-
return ft(
|
|
794
|
+
const y = g.from(d.hash).reverse().toString("hex"), b = d.index;
|
|
795
|
+
return ft(y, b, u).then((w) => ({
|
|
754
796
|
input: d,
|
|
755
797
|
utxoData: w,
|
|
756
|
-
txid:
|
|
798
|
+
txid: y,
|
|
757
799
|
vout: b
|
|
758
800
|
}));
|
|
759
801
|
}), p = await Promise.all(l);
|
|
760
|
-
for (const { input: d, utxoData:
|
|
761
|
-
const T =
|
|
802
|
+
for (const { input: d, utxoData: y, txid: b, vout: w } of p) {
|
|
803
|
+
const T = pt(
|
|
762
804
|
{
|
|
763
|
-
value:
|
|
764
|
-
scriptPubKey:
|
|
805
|
+
value: y.value,
|
|
806
|
+
scriptPubKey: y.scriptPubKey
|
|
765
807
|
},
|
|
766
808
|
a
|
|
767
809
|
);
|
|
@@ -777,22 +819,22 @@ class Ot {
|
|
|
777
819
|
script: d.script,
|
|
778
820
|
value: d.value
|
|
779
821
|
});
|
|
780
|
-
const
|
|
822
|
+
const h = await this.config.btcWallet.signPsbt(c.toHex()), f = C.fromHex(h);
|
|
781
823
|
try {
|
|
782
|
-
|
|
824
|
+
f.finalizeAllInputs();
|
|
783
825
|
} catch (d) {
|
|
784
|
-
if (!
|
|
826
|
+
if (!f.data.inputs.every(
|
|
785
827
|
(b) => b.finalScriptWitness || b.finalScriptSig
|
|
786
828
|
))
|
|
787
829
|
throw new Error(
|
|
788
830
|
`PSBT finalization failed and wallet did not auto-finalize: ${d}`
|
|
789
831
|
);
|
|
790
832
|
}
|
|
791
|
-
const P =
|
|
792
|
-
return await
|
|
833
|
+
const P = f.extractTransaction().toHex();
|
|
834
|
+
return await yt(P, u);
|
|
793
835
|
}
|
|
794
836
|
/**
|
|
795
|
-
* Registers a peg-in on Ethereum by calling the
|
|
837
|
+
* Registers a peg-in on Ethereum by calling the BTCVaultRegistry contract.
|
|
796
838
|
*
|
|
797
839
|
* This method:
|
|
798
840
|
* 1. Gets depositor ETH address from wallet
|
|
@@ -822,26 +864,26 @@ class Ot {
|
|
|
822
864
|
} = t;
|
|
823
865
|
if (!this.config.ethWallet.account)
|
|
824
866
|
throw new Error("Ethereum wallet account not found");
|
|
825
|
-
const p = this.config.ethWallet.account.address,
|
|
867
|
+
const p = this.config.ethWallet.account.address, h = await this.resolvePopSignature(
|
|
826
868
|
p,
|
|
827
869
|
l
|
|
828
870
|
);
|
|
829
871
|
i && await i();
|
|
830
|
-
const
|
|
872
|
+
const f = H(e), P = H(s), x = H(o), d = await this.resolvePayoutScriptPubKey(
|
|
831
873
|
a
|
|
832
|
-
),
|
|
833
|
-
if (await this.checkVaultExists(
|
|
874
|
+
), y = D(x);
|
|
875
|
+
if (await this.checkVaultExists(y))
|
|
834
876
|
throw new Error(
|
|
835
|
-
`Vault already exists for this transaction (ID: ${
|
|
877
|
+
`Vault already exists for this transaction (ID: ${y}). Vault IDs are deterministically derived from the unsigned Bitcoin transaction, so using the same UTXOs and amount will always produce the same vault. To create a new vault, please use different UTXOs or a different amount to generate a unique transaction.`
|
|
836
878
|
);
|
|
837
|
-
const w =
|
|
879
|
+
const w = N({
|
|
838
880
|
chain: this.config.ethChain,
|
|
839
|
-
transport:
|
|
881
|
+
transport: M()
|
|
840
882
|
});
|
|
841
883
|
let T;
|
|
842
884
|
try {
|
|
843
885
|
T = await w.readContract({
|
|
844
|
-
address: this.config.vaultContracts.
|
|
886
|
+
address: this.config.vaultContracts.btcVaultRegistry,
|
|
845
887
|
abi: $,
|
|
846
888
|
functionName: "getPegInFee",
|
|
847
889
|
args: [r]
|
|
@@ -851,26 +893,27 @@ class Ot {
|
|
|
851
893
|
"Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct."
|
|
852
894
|
);
|
|
853
895
|
}
|
|
854
|
-
const
|
|
896
|
+
const F = ct({
|
|
855
897
|
abi: $,
|
|
856
898
|
functionName: "submitPeginRequest",
|
|
857
899
|
args: [
|
|
858
900
|
p,
|
|
901
|
+
f,
|
|
859
902
|
h,
|
|
860
|
-
y,
|
|
861
903
|
P,
|
|
862
|
-
|
|
904
|
+
x,
|
|
863
905
|
r,
|
|
864
906
|
c,
|
|
907
|
+
X,
|
|
865
908
|
d,
|
|
866
909
|
u
|
|
867
910
|
]
|
|
868
911
|
});
|
|
869
|
-
let
|
|
912
|
+
let A;
|
|
870
913
|
try {
|
|
871
|
-
|
|
872
|
-
to: this.config.vaultContracts.
|
|
873
|
-
data:
|
|
914
|
+
A = await w.estimateGas({
|
|
915
|
+
to: this.config.vaultContracts.btcVaultRegistry,
|
|
916
|
+
data: F,
|
|
874
917
|
value: T,
|
|
875
918
|
account: this.config.ethWallet.account.address
|
|
876
919
|
});
|
|
@@ -880,15 +923,15 @@ class Ot {
|
|
|
880
923
|
try {
|
|
881
924
|
return {
|
|
882
925
|
ethTxHash: await this.config.ethWallet.sendTransaction({
|
|
883
|
-
to: this.config.vaultContracts.
|
|
884
|
-
data:
|
|
926
|
+
to: this.config.vaultContracts.btcVaultRegistry,
|
|
927
|
+
data: F,
|
|
885
928
|
value: T,
|
|
886
929
|
account: this.config.ethWallet.account,
|
|
887
930
|
chain: this.config.ethChain,
|
|
888
|
-
gas:
|
|
931
|
+
gas: A
|
|
889
932
|
}),
|
|
890
|
-
vaultId:
|
|
891
|
-
btcPopSignature:
|
|
933
|
+
vaultId: y,
|
|
934
|
+
btcPopSignature: h
|
|
892
935
|
};
|
|
893
936
|
} catch (v) {
|
|
894
937
|
z(v);
|
|
@@ -902,15 +945,15 @@ class Ot {
|
|
|
902
945
|
*/
|
|
903
946
|
async checkVaultExists(t) {
|
|
904
947
|
try {
|
|
905
|
-
return (await
|
|
948
|
+
return (await N({
|
|
906
949
|
chain: this.config.ethChain,
|
|
907
|
-
transport:
|
|
950
|
+
transport: M()
|
|
908
951
|
}).readContract({
|
|
909
|
-
address: this.config.vaultContracts.
|
|
952
|
+
address: this.config.vaultContracts.btcVaultRegistry,
|
|
910
953
|
abi: $,
|
|
911
954
|
functionName: "getBTCVault",
|
|
912
955
|
args: [t]
|
|
913
|
-
})).depositor !==
|
|
956
|
+
})).depositor !== ut;
|
|
914
957
|
} catch {
|
|
915
958
|
return !1;
|
|
916
959
|
}
|
|
@@ -929,7 +972,7 @@ class Ot {
|
|
|
929
972
|
else {
|
|
930
973
|
e = await this.config.btcWallet.getAddress();
|
|
931
974
|
const o = await this.config.btcWallet.getPublicKeyHex();
|
|
932
|
-
if (!
|
|
975
|
+
if (!it(
|
|
933
976
|
e,
|
|
934
977
|
o,
|
|
935
978
|
this.config.btcNetwork
|
|
@@ -940,7 +983,7 @@ class Ot {
|
|
|
940
983
|
}
|
|
941
984
|
const s = U(this.config.btcNetwork);
|
|
942
985
|
try {
|
|
943
|
-
return `0x${
|
|
986
|
+
return `0x${j.address.toOutputScript(e, s).toString("hex")}`;
|
|
944
987
|
} catch {
|
|
945
988
|
throw new Error(
|
|
946
989
|
`Invalid BTC payout address: "${e}". Please provide a valid Bitcoin address for the ${this.config.btcNetwork} network.`
|
|
@@ -956,11 +999,11 @@ class Ot {
|
|
|
956
999
|
async resolvePopSignature(t, e) {
|
|
957
1000
|
if (e)
|
|
958
1001
|
return e;
|
|
959
|
-
const s = this.config.vaultContracts.
|
|
1002
|
+
const s = this.config.vaultContracts.btcVaultRegistry, o = `${t.toLowerCase()}:${this.config.ethChain.id}:pegin:${s.toLowerCase()}`, r = await this.config.btcWallet.signMessage(
|
|
960
1003
|
o,
|
|
961
1004
|
"bip322-simple"
|
|
962
1005
|
);
|
|
963
|
-
return r.startsWith("0x") ? r : `0x${
|
|
1006
|
+
return r.startsWith("0x") ? r : `0x${g.from(r, "base64").toString("hex")}`;
|
|
964
1007
|
}
|
|
965
1008
|
/**
|
|
966
1009
|
* Gets the configured Bitcoin network.
|
|
@@ -971,12 +1014,12 @@ class Ot {
|
|
|
971
1014
|
return this.config.btcNetwork;
|
|
972
1015
|
}
|
|
973
1016
|
/**
|
|
974
|
-
* Gets the configured
|
|
1017
|
+
* Gets the configured BTCVaultRegistry contract address.
|
|
975
1018
|
*
|
|
976
|
-
* @returns The Ethereum address of the
|
|
1019
|
+
* @returns The Ethereum address of the BTCVaultRegistry contract
|
|
977
1020
|
*/
|
|
978
1021
|
getVaultContractAddress() {
|
|
979
|
-
return this.config.vaultContracts.
|
|
1022
|
+
return this.config.vaultContracts.btcVaultRegistry;
|
|
980
1023
|
}
|
|
981
1024
|
}
|
|
982
1025
|
class At {
|
|
@@ -986,7 +1029,7 @@ class At {
|
|
|
986
1029
|
* @param config - Manager configuration including wallet
|
|
987
1030
|
*/
|
|
988
1031
|
constructor(t) {
|
|
989
|
-
|
|
1032
|
+
B(this, "config");
|
|
990
1033
|
this.config = t;
|
|
991
1034
|
}
|
|
992
1035
|
/**
|
|
@@ -1015,7 +1058,7 @@ class At {
|
|
|
1015
1058
|
const e = await this.config.btcWallet.getPublicKeyHex(), { depositorPubkey: s } = V(
|
|
1016
1059
|
e,
|
|
1017
1060
|
t.depositorBtcPubkey
|
|
1018
|
-
), o = await
|
|
1061
|
+
), o = await W({
|
|
1019
1062
|
payoutTxHex: t.payoutTxHex,
|
|
1020
1063
|
peginTxHex: t.peginTxHex,
|
|
1021
1064
|
assertTxHex: t.assertTxHex,
|
|
@@ -1039,7 +1082,7 @@ class At {
|
|
|
1039
1082
|
}
|
|
1040
1083
|
);
|
|
1041
1084
|
return {
|
|
1042
|
-
signature:
|
|
1085
|
+
signature: K(r, s),
|
|
1043
1086
|
depositorBtcPubkey: s
|
|
1044
1087
|
};
|
|
1045
1088
|
}
|
|
@@ -1080,7 +1123,7 @@ class At {
|
|
|
1080
1123
|
a.depositorBtcPubkey
|
|
1081
1124
|
);
|
|
1082
1125
|
r.push(u);
|
|
1083
|
-
const l = await
|
|
1126
|
+
const l = await W({
|
|
1084
1127
|
payoutTxHex: a.payoutTxHex,
|
|
1085
1128
|
peginTxHex: a.peginTxHex,
|
|
1086
1129
|
assertTxHex: a.assertTxHex,
|
|
@@ -1112,7 +1155,7 @@ class At {
|
|
|
1112
1155
|
);
|
|
1113
1156
|
const i = [];
|
|
1114
1157
|
for (let a = 0; a < t.length; a++) {
|
|
1115
|
-
const u = r[a], l =
|
|
1158
|
+
const u = r[a], l = K(
|
|
1116
1159
|
c[a],
|
|
1117
1160
|
u
|
|
1118
1161
|
);
|
|
@@ -1125,31 +1168,31 @@ class At {
|
|
|
1125
1168
|
}
|
|
1126
1169
|
}
|
|
1127
1170
|
export {
|
|
1128
|
-
B,
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1171
|
+
E as B,
|
|
1172
|
+
m as C,
|
|
1173
|
+
ht as M,
|
|
1174
|
+
Ft as P,
|
|
1175
|
+
vt as a,
|
|
1176
|
+
Et as b,
|
|
1177
|
+
D as c,
|
|
1178
|
+
St as d,
|
|
1179
|
+
dt as e,
|
|
1180
|
+
pt as f,
|
|
1181
|
+
kt as g,
|
|
1139
1182
|
At as h,
|
|
1140
|
-
|
|
1141
|
-
|
|
1183
|
+
$t as i,
|
|
1184
|
+
Bt as j,
|
|
1142
1185
|
Ht as k,
|
|
1143
|
-
|
|
1144
|
-
|
|
1186
|
+
Ct as l,
|
|
1187
|
+
It as m,
|
|
1145
1188
|
gt as n,
|
|
1146
1189
|
ft as o,
|
|
1147
|
-
|
|
1190
|
+
yt as p,
|
|
1148
1191
|
$ as q,
|
|
1149
1192
|
O as r,
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1193
|
+
lt as s,
|
|
1194
|
+
Ut as t,
|
|
1195
|
+
Ot as u,
|
|
1153
1196
|
z as v
|
|
1154
1197
|
};
|
|
1155
|
-
//# sourceMappingURL=PayoutManager-
|
|
1198
|
+
//# sourceMappingURL=PayoutManager-BVmU4UsM.js.map
|