@babylonlabs-io/ts-sdk 0.2.1-rc.3 → 0.4.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/LICENSE +169 -0
- package/dist/{PayoutManager-rMj54HDh.js → PayoutManager-BK1jKgL5.js} +404 -361
- package/dist/PayoutManager-BK1jKgL5.js.map +1 -0
- package/dist/PayoutManager-CzKMN6xT.cjs +2 -0
- package/dist/PayoutManager-CzKMN6xT.cjs.map +1 -0
- package/dist/challengeAssert-DOa5L0kv.cjs +2 -0
- package/dist/challengeAssert-DOa5L0kv.cjs.map +1 -0
- package/dist/challengeAssert-Dvcg38_y.js +606 -0
- package/dist/challengeAssert-Dvcg38_y.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +62 -57
- package/dist/tbv/core/contracts/abis/BTCVaultsManager.abi.d.ts +50 -4
- package/dist/tbv/core/contracts/abis/BTCVaultsManager.abi.d.ts.map +1 -1
- package/dist/tbv/core/index.cjs +1 -1
- package/dist/tbv/core/index.js +62 -57
- package/dist/tbv/core/managers/PeginManager.d.ts +89 -48
- 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 +5 -3
- package/dist/tbv/core/primitives/index.d.ts.map +1 -1
- package/dist/tbv/core/primitives/index.js +22 -18
- package/dist/tbv/core/primitives/psbt/__tests__/pegin.test.d.ts +1 -1
- package/dist/tbv/core/primitives/psbt/index.d.ts +8 -3
- package/dist/tbv/core/primitives/psbt/index.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/pegin.d.ts +76 -59
- package/dist/tbv/core/primitives/psbt/pegin.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/peginInput.d.ts +69 -0
- package/dist/tbv/core/primitives/psbt/peginInput.d.ts.map +1 -0
- package/dist/tbv/core/primitives/utils/bitcoin.d.ts +11 -0
- package/dist/tbv/core/primitives/utils/bitcoin.d.ts.map +1 -1
- package/dist/tbv/core/primitives/utils/index.d.ts +1 -1
- package/dist/tbv/core/primitives/utils/index.d.ts.map +1 -1
- package/dist/tbv/core/utils/fee/constants.d.ts +7 -0
- package/dist/tbv/core/utils/fee/constants.d.ts.map +1 -1
- package/dist/tbv/index.cjs +1 -1
- package/dist/tbv/index.js +62 -57
- package/dist/tbv/integrations/aave/clients/abis/AaveIntegrationController.abi.json.d.ts +16 -1
- 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 +19 -19
- package/dist/PayoutManager-Dg_i_S7w.cjs +0 -2
- package/dist/PayoutManager-Dg_i_S7w.cjs.map +0 -1
- package/dist/PayoutManager-rMj54HDh.js.map +0 -1
- package/dist/challengeAssert-34HqeVFH.cjs +0 -2
- package/dist/challengeAssert-34HqeVFH.cjs.map +0 -1
- package/dist/challengeAssert-DVErOd8l.js +0 -423
- package/dist/challengeAssert-DVErOd8l.js.map +0 -1
|
@@ -1,195 +1,148 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import * as
|
|
5
|
-
import { script as
|
|
6
|
-
import { Buffer as
|
|
7
|
-
import {
|
|
8
|
-
import { createPublicClient as
|
|
1
|
+
var q = Object.defineProperty;
|
|
2
|
+
var _ = (n, t, e) => t in n ? q(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var H = (n, t, e) => _(n, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
import * as L from "bitcoinjs-lib";
|
|
5
|
+
import { script as j, Transaction as S, address as J, Psbt as C } from "bitcoinjs-lib";
|
|
6
|
+
import { Buffer as g } from "buffer";
|
|
7
|
+
import { P as Z, r as G, D as z, M as W, B as Q, T as Y, y as U, s as k, b as tt, x as et, a as nt, c as st, e as rt, p as I, n as at, v as V, d as M, f as R } from "./challengeAssert-Dvcg38_y.js";
|
|
8
|
+
import { createPublicClient as K, http as N, encodeFunctionData as ot, zeroAddress as it } from "viem";
|
|
9
9
|
import "@babylonlabs-io/babylon-tbv-rust-wasm";
|
|
10
|
-
|
|
11
|
-
const Z = 58, V = 43, G = 11, N = 546, H = BigInt(N), Q = 30, tt = 2, mt = 1.1;
|
|
12
|
-
function et(n) {
|
|
13
|
-
return n <= tt ? Q : 0;
|
|
14
|
-
}
|
|
15
|
-
function nt(n, t, e) {
|
|
10
|
+
function ct(n, t, e) {
|
|
16
11
|
if (n.length === 0)
|
|
17
12
|
throw new Error("Insufficient funds: no UTXOs available");
|
|
18
|
-
const s = n.filter((
|
|
19
|
-
const
|
|
20
|
-
return !!
|
|
13
|
+
const s = n.filter((a) => {
|
|
14
|
+
const u = g.from(a.scriptPubKey, "hex");
|
|
15
|
+
return !!j.decompile(u);
|
|
21
16
|
});
|
|
22
17
|
if (s.length === 0)
|
|
23
18
|
throw new Error(
|
|
24
19
|
"Insufficient funds: no valid UTXOs available (all have invalid scripts)"
|
|
25
20
|
);
|
|
26
|
-
const
|
|
27
|
-
let
|
|
28
|
-
for (const
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
if (i = BigInt(Math.ceil(d * e)) + BigInt(
|
|
32
|
-
const
|
|
33
|
-
Math.ceil(
|
|
21
|
+
const o = [...s].sort((a, u) => u.value - a.value), r = [];
|
|
22
|
+
let c = 0n, i = 0n;
|
|
23
|
+
for (const a of o) {
|
|
24
|
+
r.push(a), c += BigInt(a.value);
|
|
25
|
+
const u = r.length * Z, l = 2 * W, d = u + l + Y;
|
|
26
|
+
if (i = BigInt(Math.ceil(d * e)) + BigInt(G(e)), c - t - i > z) {
|
|
27
|
+
const h = BigInt(
|
|
28
|
+
Math.ceil(W * e)
|
|
34
29
|
);
|
|
35
|
-
i +=
|
|
30
|
+
i += h;
|
|
36
31
|
}
|
|
37
|
-
if (
|
|
38
|
-
const
|
|
32
|
+
if (c >= t + i) {
|
|
33
|
+
const h = c - t - i;
|
|
39
34
|
return {
|
|
40
|
-
selectedUTXOs:
|
|
41
|
-
totalValue:
|
|
35
|
+
selectedUTXOs: r,
|
|
36
|
+
totalValue: c,
|
|
42
37
|
fee: i,
|
|
43
|
-
changeAmount:
|
|
38
|
+
changeAmount: h
|
|
44
39
|
};
|
|
45
40
|
}
|
|
46
41
|
}
|
|
47
42
|
throw new Error(
|
|
48
|
-
`Insufficient funds: need ${t + i} sats (${t} pegin + ${i} fee), have ${
|
|
43
|
+
`Insufficient funds: need ${t + i} sats (${t} pegin + ${i} fee), have ${c} sats`
|
|
49
44
|
);
|
|
50
45
|
}
|
|
51
46
|
function wt(n) {
|
|
52
|
-
return n >
|
|
47
|
+
return n > z;
|
|
53
48
|
}
|
|
54
|
-
function
|
|
55
|
-
return
|
|
49
|
+
function Tt() {
|
|
50
|
+
return Q;
|
|
56
51
|
}
|
|
57
|
-
function
|
|
58
|
-
const e = n.substring(8, 12) === "0001" ? 12 : 8, s = parseInt(
|
|
59
|
-
n.substring(e, e + 2),
|
|
60
|
-
16
|
|
61
|
-
), a = parseInt(
|
|
62
|
-
n.substring(e + 2, e + 4),
|
|
63
|
-
16
|
|
64
|
-
);
|
|
65
|
-
if (s !== 0)
|
|
66
|
-
throw new Error(`Expected 0 inputs from WASM, got ${s}`);
|
|
67
|
-
if (a === 0)
|
|
68
|
-
throw new Error("Expected at least 1 output from WASM, got 0");
|
|
69
|
-
const o = p.from(n.substring(0, 8), "hex").readUInt32LE(0), u = p.from(
|
|
70
|
-
n.substring(n.length - 8),
|
|
71
|
-
"hex"
|
|
72
|
-
).readUInt32LE(0), i = [];
|
|
73
|
-
let r = e + 4;
|
|
74
|
-
for (let c = 0; c < a; c++) {
|
|
75
|
-
const l = n.substring(r, r + 16), d = Number(p.from(l, "hex").readBigUInt64LE(0));
|
|
76
|
-
r += 16;
|
|
77
|
-
const f = parseInt(n.substring(r, r + 2), 16);
|
|
78
|
-
r += 2;
|
|
79
|
-
const g = n.substring(r, r + f * 2), b = p.from(g, "hex");
|
|
80
|
-
r += f * 2, i.push({ value: d, script: b });
|
|
81
|
-
}
|
|
82
|
-
return { version: o, locktime: u, outputs: i };
|
|
83
|
-
}
|
|
84
|
-
function rt(n) {
|
|
85
|
-
const { unfundedTxHex: t, selectedUTXOs: e, changeAddress: s, changeAmount: a, network: o } = n, { version: u, locktime: i, outputs: r } = st(t), c = new $.Transaction();
|
|
86
|
-
c.version = u, c.locktime = i;
|
|
87
|
-
for (const l of e) {
|
|
88
|
-
const d = p.from(l.txid, "hex").reverse();
|
|
89
|
-
c.addInput(d, l.vout);
|
|
90
|
-
}
|
|
91
|
-
for (const l of r)
|
|
92
|
-
c.addOutput(l.script, l.value);
|
|
93
|
-
if (a > H) {
|
|
94
|
-
const l = $.address.toOutputScript(s, o);
|
|
95
|
-
c.addOutput(l, Number(a));
|
|
96
|
-
}
|
|
97
|
-
return c.toHex();
|
|
98
|
-
}
|
|
99
|
-
function R(n) {
|
|
52
|
+
function X(n) {
|
|
100
53
|
const t = n.startsWith("0x") ? n.slice(2) : n;
|
|
101
|
-
return `0x${
|
|
54
|
+
return `0x${S.fromHex(t).getId()}`;
|
|
102
55
|
}
|
|
103
|
-
function
|
|
56
|
+
function vt(n, t, e) {
|
|
104
57
|
if (n.length === 0)
|
|
105
58
|
throw new Error("No input UTXOs provided for split transaction");
|
|
106
59
|
if (t.length === 0)
|
|
107
60
|
throw new Error("No outputs specified for split transaction");
|
|
108
|
-
for (const
|
|
109
|
-
if (
|
|
61
|
+
for (const a of t)
|
|
62
|
+
if (a.amount <= 0n)
|
|
110
63
|
throw new Error(
|
|
111
|
-
`Invalid output amount for ${
|
|
64
|
+
`Invalid output amount for ${a.address}: ${a.amount} satoshis. Amount must be greater than zero.`
|
|
112
65
|
);
|
|
113
|
-
const s =
|
|
114
|
-
|
|
115
|
-
for (const
|
|
116
|
-
const
|
|
117
|
-
|
|
66
|
+
const s = U(e), o = new S();
|
|
67
|
+
o.version = 2;
|
|
68
|
+
for (const a of n) {
|
|
69
|
+
const u = g.from(a.txid, "hex").reverse();
|
|
70
|
+
o.addInput(u, a.vout);
|
|
118
71
|
}
|
|
119
|
-
const
|
|
120
|
-
for (let
|
|
121
|
-
const
|
|
72
|
+
const r = [];
|
|
73
|
+
for (let a = 0; a < t.length; a++) {
|
|
74
|
+
const u = t[a];
|
|
122
75
|
let l;
|
|
123
76
|
try {
|
|
124
|
-
l =
|
|
77
|
+
l = J.toOutputScript(u.address, s);
|
|
125
78
|
} catch (d) {
|
|
126
79
|
throw new Error(
|
|
127
|
-
`Failed to decode address "${
|
|
80
|
+
`Failed to decode address "${u.address}": ${d instanceof Error ? d.message : String(d)}`
|
|
128
81
|
);
|
|
129
82
|
}
|
|
130
|
-
|
|
83
|
+
o.addOutput(l, Number(u.amount)), r.push({
|
|
131
84
|
txid: "",
|
|
132
85
|
// Will be set after txid calculation
|
|
133
|
-
vout:
|
|
134
|
-
value: Number(
|
|
86
|
+
vout: a,
|
|
87
|
+
value: Number(u.amount),
|
|
135
88
|
scriptPubKey: l.toString("hex")
|
|
136
89
|
});
|
|
137
90
|
}
|
|
138
|
-
const
|
|
139
|
-
for (const
|
|
140
|
-
|
|
91
|
+
const c = o.toHex(), i = o.getId();
|
|
92
|
+
for (const a of r)
|
|
93
|
+
a.txid = i;
|
|
141
94
|
return {
|
|
142
|
-
txHex:
|
|
95
|
+
txHex: c,
|
|
143
96
|
txid: i,
|
|
144
|
-
outputs:
|
|
97
|
+
outputs: r
|
|
145
98
|
};
|
|
146
99
|
}
|
|
147
|
-
function
|
|
148
|
-
const s =
|
|
149
|
-
if (
|
|
100
|
+
function kt(n, t, e) {
|
|
101
|
+
const s = S.fromHex(n), o = new C();
|
|
102
|
+
if (o.setVersion(s.version), o.setLocktime(s.locktime), !g.isBuffer(e) || e.length !== 32)
|
|
150
103
|
throw new Error(
|
|
151
|
-
`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}`
|
|
152
105
|
);
|
|
153
106
|
if (t.length !== s.ins.length)
|
|
154
107
|
throw new Error(
|
|
155
108
|
`UTXO count mismatch: transaction has ${s.ins.length} input${s.ins.length !== 1 ? "s" : ""}, but ${t.length} UTXO${t.length !== 1 ? "s were" : " was"} provided`
|
|
156
109
|
);
|
|
157
|
-
for (let
|
|
158
|
-
const
|
|
110
|
+
for (let r = 0; r < s.ins.length; r++) {
|
|
111
|
+
const c = s.ins[r], i = t[r];
|
|
159
112
|
if (!i)
|
|
160
|
-
throw new Error(`Missing UTXO data for input ${
|
|
161
|
-
const
|
|
162
|
-
if (i.txid !==
|
|
113
|
+
throw new Error(`Missing UTXO data for input ${r}`);
|
|
114
|
+
const a = g.from(c.hash).reverse().toString("hex"), u = c.index;
|
|
115
|
+
if (i.txid !== a || i.vout !== u)
|
|
163
116
|
throw new Error(
|
|
164
|
-
`Input ${
|
|
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().`
|
|
165
118
|
);
|
|
166
|
-
const l =
|
|
119
|
+
const l = g.from(i.scriptPubKey, "hex");
|
|
167
120
|
if (!(l.length === 34 && l[0] === 81 && // OP_1 (witness version 1)
|
|
168
121
|
l[1] === 32))
|
|
169
122
|
throw new Error(
|
|
170
|
-
`Input ${
|
|
123
|
+
`Input ${r} must be P2TR (Taproot). createSplitTransactionPsbt() requires P2TR inputs because it uses tapInternalKey for Taproot signing. ScriptPubKey: ${i.scriptPubKey.substring(0, 20)}...`
|
|
171
124
|
);
|
|
172
125
|
const f = {
|
|
173
|
-
script:
|
|
126
|
+
script: g.from(i.scriptPubKey, "hex"),
|
|
174
127
|
value: i.value
|
|
175
128
|
};
|
|
176
|
-
|
|
177
|
-
hash:
|
|
178
|
-
index:
|
|
179
|
-
sequence:
|
|
129
|
+
o.addInput({
|
|
130
|
+
hash: c.hash,
|
|
131
|
+
index: c.index,
|
|
132
|
+
sequence: c.sequence,
|
|
180
133
|
witnessUtxo: f,
|
|
181
134
|
tapInternalKey: e
|
|
182
135
|
});
|
|
183
136
|
}
|
|
184
|
-
for (const
|
|
185
|
-
|
|
186
|
-
script:
|
|
187
|
-
value:
|
|
137
|
+
for (const r of s.outs)
|
|
138
|
+
o.addOutput({
|
|
139
|
+
script: r.script,
|
|
140
|
+
value: r.value
|
|
188
141
|
});
|
|
189
|
-
return
|
|
142
|
+
return o.toHex();
|
|
190
143
|
}
|
|
191
|
-
var
|
|
192
|
-
function
|
|
144
|
+
var B = /* @__PURE__ */ ((n) => (n.P2PKH = "P2PKH", n.P2SH = "P2SH", n.P2WPKH = "P2WPKH", n.P2WSH = "P2WSH", n.P2TR = "P2TR", n.UNKNOWN = "UNKNOWN", n))(B || {});
|
|
145
|
+
function ut(n) {
|
|
193
146
|
const t = n.length;
|
|
194
147
|
return t === 25 && n[0] === 118 && // OP_DUP
|
|
195
148
|
n[1] === 169 && // OP_HASH160
|
|
@@ -202,17 +155,17 @@ function ot(n) {
|
|
|
202
155
|
n[1] === 32 ? "P2WSH" : t === 34 && n[0] === 81 && // OP_1
|
|
203
156
|
n[1] === 32 ? "P2TR" : "UNKNOWN";
|
|
204
157
|
}
|
|
205
|
-
function
|
|
206
|
-
const e =
|
|
158
|
+
function lt(n, t) {
|
|
159
|
+
const e = g.from(n.scriptPubKey, "hex"), s = ut(e);
|
|
207
160
|
switch (s) {
|
|
208
|
-
case
|
|
161
|
+
case B.P2WPKH:
|
|
209
162
|
return {
|
|
210
163
|
witnessUtxo: {
|
|
211
164
|
script: e,
|
|
212
165
|
value: n.value
|
|
213
166
|
}
|
|
214
167
|
};
|
|
215
|
-
case
|
|
168
|
+
case B.P2WSH: {
|
|
216
169
|
if (!n.witnessScript)
|
|
217
170
|
throw new Error("Missing witnessScript for P2WSH input");
|
|
218
171
|
return {
|
|
@@ -220,10 +173,10 @@ function at(n, t) {
|
|
|
220
173
|
script: e,
|
|
221
174
|
value: n.value
|
|
222
175
|
},
|
|
223
|
-
witnessScript:
|
|
176
|
+
witnessScript: g.from(n.witnessScript, "hex")
|
|
224
177
|
};
|
|
225
178
|
}
|
|
226
|
-
case
|
|
179
|
+
case B.P2TR: {
|
|
227
180
|
if (t && t.length !== 32)
|
|
228
181
|
throw new Error(
|
|
229
182
|
`Invalid tapInternalKey length: expected 32 bytes, got ${t.length}`
|
|
@@ -241,18 +194,18 @@ function at(n, t) {
|
|
|
241
194
|
throw new Error(`Unsupported script type: ${s}`);
|
|
242
195
|
}
|
|
243
196
|
}
|
|
244
|
-
const
|
|
197
|
+
const dt = {
|
|
245
198
|
mainnet: "https://mempool.space/api",
|
|
246
199
|
testnet: "https://mempool.space/testnet/api",
|
|
247
200
|
signet: "https://mempool.space/signet/api"
|
|
248
201
|
};
|
|
249
|
-
async function
|
|
202
|
+
async function E(n, t) {
|
|
250
203
|
try {
|
|
251
204
|
const e = await fetch(n, t);
|
|
252
205
|
if (!e.ok) {
|
|
253
|
-
const
|
|
206
|
+
const o = await e.text();
|
|
254
207
|
throw new Error(
|
|
255
|
-
`Mempool API error (${e.status}): ${
|
|
208
|
+
`Mempool API error (${e.status}): ${o || e.statusText}`
|
|
256
209
|
);
|
|
257
210
|
}
|
|
258
211
|
const s = e.headers.get("content-type");
|
|
@@ -261,7 +214,7 @@ async function k(n, t) {
|
|
|
261
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");
|
|
262
215
|
}
|
|
263
216
|
}
|
|
264
|
-
async function
|
|
217
|
+
async function pt(n, t) {
|
|
265
218
|
try {
|
|
266
219
|
const e = await fetch(`${t}/tx`, {
|
|
267
220
|
method: "POST",
|
|
@@ -271,15 +224,15 @@ async function ct(n, t) {
|
|
|
271
224
|
}
|
|
272
225
|
});
|
|
273
226
|
if (!e.ok) {
|
|
274
|
-
const
|
|
275
|
-
let
|
|
227
|
+
const o = await e.text();
|
|
228
|
+
let r;
|
|
276
229
|
try {
|
|
277
|
-
|
|
230
|
+
r = JSON.parse(o).message;
|
|
278
231
|
} catch {
|
|
279
|
-
|
|
232
|
+
r = o;
|
|
280
233
|
}
|
|
281
234
|
throw new Error(
|
|
282
|
-
|
|
235
|
+
r || `Failed to broadcast transaction: ${e.statusText}`
|
|
283
236
|
);
|
|
284
237
|
}
|
|
285
238
|
return await e.text();
|
|
@@ -287,10 +240,10 @@ async function ct(n, t) {
|
|
|
287
240
|
throw e instanceof Error ? new Error(`Failed to broadcast BTC transaction: ${e.message}`) : new Error("Failed to broadcast BTC transaction: Unknown error");
|
|
288
241
|
}
|
|
289
242
|
}
|
|
290
|
-
async function
|
|
291
|
-
return
|
|
243
|
+
async function ht(n, t) {
|
|
244
|
+
return E(`${t}/tx/${n}`);
|
|
292
245
|
}
|
|
293
|
-
async function
|
|
246
|
+
async function Bt(n, t) {
|
|
294
247
|
try {
|
|
295
248
|
const e = await fetch(`${t}/tx/${n}/hex`);
|
|
296
249
|
if (!e.ok) {
|
|
@@ -304,33 +257,33 @@ async function vt(n, t) {
|
|
|
304
257
|
throw e instanceof Error ? new Error(`Failed to get transaction hex for ${n}: ${e.message}`) : new Error(`Failed to get transaction hex for ${n}: Unknown error`);
|
|
305
258
|
}
|
|
306
259
|
}
|
|
307
|
-
async function
|
|
308
|
-
const s = await
|
|
260
|
+
async function gt(n, t, e) {
|
|
261
|
+
const s = await ht(n, e);
|
|
309
262
|
if (t >= s.vout.length)
|
|
310
263
|
throw new Error(
|
|
311
264
|
`Invalid vout ${t} for transaction ${n} (has ${s.vout.length} outputs)`
|
|
312
265
|
);
|
|
313
|
-
const
|
|
266
|
+
const o = s.vout[t];
|
|
314
267
|
return {
|
|
315
268
|
txid: n,
|
|
316
269
|
vout: t,
|
|
317
|
-
value:
|
|
318
|
-
scriptPubKey:
|
|
270
|
+
value: o.value,
|
|
271
|
+
scriptPubKey: o.scriptpubkey
|
|
319
272
|
};
|
|
320
273
|
}
|
|
321
|
-
async function
|
|
274
|
+
async function Et(n, t) {
|
|
322
275
|
try {
|
|
323
|
-
const e = await
|
|
276
|
+
const e = await E(`${t}/address/${n}/utxo`), s = await E(`${t}/v1/validate-address/${n}`);
|
|
324
277
|
if (!s.isvalid)
|
|
325
278
|
throw new Error(
|
|
326
279
|
`Invalid Bitcoin address: ${n}. Mempool API validation failed.`
|
|
327
280
|
);
|
|
328
|
-
return e.sort((
|
|
329
|
-
txid:
|
|
330
|
-
vout:
|
|
331
|
-
value:
|
|
281
|
+
return e.sort((r, c) => c.value - r.value).map((r) => ({
|
|
282
|
+
txid: r.txid,
|
|
283
|
+
vout: r.vout,
|
|
284
|
+
value: r.value,
|
|
332
285
|
scriptPubKey: s.scriptPubKey,
|
|
333
|
-
confirmed:
|
|
286
|
+
confirmed: r.status.confirmed
|
|
334
287
|
}));
|
|
335
288
|
} catch (e) {
|
|
336
289
|
throw e instanceof Error ? new Error(
|
|
@@ -340,13 +293,13 @@ async function kt(n, t) {
|
|
|
340
293
|
);
|
|
341
294
|
}
|
|
342
295
|
}
|
|
343
|
-
function
|
|
344
|
-
return
|
|
296
|
+
function St(n) {
|
|
297
|
+
return dt[n];
|
|
345
298
|
}
|
|
346
|
-
async function
|
|
347
|
-
return
|
|
299
|
+
async function Ht(n, t) {
|
|
300
|
+
return E(`${t}/address/${n}/txs`);
|
|
348
301
|
}
|
|
349
|
-
async function
|
|
302
|
+
async function It(n) {
|
|
350
303
|
const t = await fetch(`${n}/v1/fees/recommended`);
|
|
351
304
|
if (!t.ok)
|
|
352
305
|
throw new Error(
|
|
@@ -359,7 +312,7 @@ async function Bt(n) {
|
|
|
359
312
|
);
|
|
360
313
|
return e;
|
|
361
314
|
}
|
|
362
|
-
const
|
|
315
|
+
const $ = [
|
|
363
316
|
{
|
|
364
317
|
type: "function",
|
|
365
318
|
name: "submitPeginRequest",
|
|
@@ -380,7 +333,12 @@ const B = [
|
|
|
380
333
|
internalType: "bytes"
|
|
381
334
|
},
|
|
382
335
|
{
|
|
383
|
-
name: "
|
|
336
|
+
name: "unsignedPrePeginTx",
|
|
337
|
+
type: "bytes",
|
|
338
|
+
internalType: "bytes"
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
name: "depositorSignedPeginTx",
|
|
384
342
|
type: "bytes",
|
|
385
343
|
internalType: "bytes"
|
|
386
344
|
},
|
|
@@ -389,6 +347,11 @@ const B = [
|
|
|
389
347
|
type: "address",
|
|
390
348
|
internalType: "address"
|
|
391
349
|
},
|
|
350
|
+
{
|
|
351
|
+
name: "hashlock",
|
|
352
|
+
type: "bytes32",
|
|
353
|
+
internalType: "bytes32"
|
|
354
|
+
},
|
|
392
355
|
{
|
|
393
356
|
name: "depositorPayoutBtcAddress",
|
|
394
357
|
type: "bytes",
|
|
@@ -429,7 +392,12 @@ const B = [
|
|
|
429
392
|
internalType: "bytes"
|
|
430
393
|
},
|
|
431
394
|
{
|
|
432
|
-
name: "
|
|
395
|
+
name: "unsignedPrePeginTx",
|
|
396
|
+
type: "bytes",
|
|
397
|
+
internalType: "bytes"
|
|
398
|
+
},
|
|
399
|
+
{
|
|
400
|
+
name: "depositorSignedPeginTx",
|
|
433
401
|
type: "bytes",
|
|
434
402
|
internalType: "bytes"
|
|
435
403
|
},
|
|
@@ -438,6 +406,11 @@ const B = [
|
|
|
438
406
|
type: "address",
|
|
439
407
|
internalType: "address"
|
|
440
408
|
},
|
|
409
|
+
{
|
|
410
|
+
name: "hashlock",
|
|
411
|
+
type: "bytes32",
|
|
412
|
+
internalType: "bytes32"
|
|
413
|
+
},
|
|
441
414
|
{
|
|
442
415
|
name: "referralCode",
|
|
443
416
|
type: "uint32",
|
|
@@ -463,6 +436,24 @@ const B = [
|
|
|
463
436
|
],
|
|
464
437
|
stateMutability: "payable"
|
|
465
438
|
},
|
|
439
|
+
{
|
|
440
|
+
type: "function",
|
|
441
|
+
name: "activateVaultWithSecret",
|
|
442
|
+
inputs: [
|
|
443
|
+
{
|
|
444
|
+
name: "vaultId",
|
|
445
|
+
type: "bytes32",
|
|
446
|
+
internalType: "bytes32"
|
|
447
|
+
},
|
|
448
|
+
{
|
|
449
|
+
name: "s",
|
|
450
|
+
type: "bytes32",
|
|
451
|
+
internalType: "bytes32"
|
|
452
|
+
}
|
|
453
|
+
],
|
|
454
|
+
outputs: [],
|
|
455
|
+
stateMutability: "nonpayable"
|
|
456
|
+
},
|
|
466
457
|
{
|
|
467
458
|
type: "function",
|
|
468
459
|
name: "getPegInFee",
|
|
@@ -500,7 +491,7 @@ const B = [
|
|
|
500
491
|
components: [
|
|
501
492
|
{ name: "depositor", type: "address", internalType: "address" },
|
|
502
493
|
{ name: "depositorBtcPubKey", type: "bytes32", internalType: "bytes32" },
|
|
503
|
-
{ name: "
|
|
494
|
+
{ name: "depositorSignedPeginTx", type: "bytes", internalType: "bytes" },
|
|
504
495
|
{ name: "amount", type: "uint256", internalType: "uint256" },
|
|
505
496
|
{ name: "vaultProvider", type: "address", internalType: "address" },
|
|
506
497
|
{ name: "status", type: "uint8", internalType: "enum IBTCVaultsManager.BTCVaultStatus" },
|
|
@@ -508,9 +499,11 @@ const B = [
|
|
|
508
499
|
{ name: "universalChallengersVersion", type: "uint16", internalType: "uint16" },
|
|
509
500
|
{ name: "appVaultKeepersVersion", type: "uint16", internalType: "uint16" },
|
|
510
501
|
{ name: "offchainParamsVersion", type: "uint16", internalType: "uint16" },
|
|
502
|
+
{ name: "vkVersion", type: "uint16", internalType: "uint16" },
|
|
511
503
|
{ name: "createdAt", type: "uint256", internalType: "uint256" },
|
|
512
504
|
{ name: "verifiedAt", type: "uint256", internalType: "uint256" },
|
|
513
|
-
{ name: "depositorLamportPkHash", type: "bytes32", internalType: "bytes32" }
|
|
505
|
+
{ name: "depositorLamportPkHash", type: "bytes32", internalType: "bytes32" },
|
|
506
|
+
{ name: "hashlock", type: "bytes32", internalType: "bytes32" }
|
|
514
507
|
]
|
|
515
508
|
}
|
|
516
509
|
],
|
|
@@ -531,6 +524,16 @@ const B = [
|
|
|
531
524
|
internalType: "uint256"
|
|
532
525
|
}
|
|
533
526
|
]
|
|
527
|
+
},
|
|
528
|
+
{
|
|
529
|
+
type: "error",
|
|
530
|
+
name: "InvalidSecret",
|
|
531
|
+
inputs: []
|
|
532
|
+
},
|
|
533
|
+
{
|
|
534
|
+
type: "error",
|
|
535
|
+
name: "ActivationDeadlineExpired",
|
|
536
|
+
inputs: []
|
|
534
537
|
}
|
|
535
538
|
], x = {
|
|
536
539
|
// VaultAlreadyExists()
|
|
@@ -562,7 +565,7 @@ const B = [
|
|
|
562
565
|
// InvalidPeginFee(uint256,uint256)
|
|
563
566
|
"0x979f4518": "Invalid pegin fee: The ETH fee sent does not match the required amount. This may indicate a fee rate change during the transaction."
|
|
564
567
|
};
|
|
565
|
-
function
|
|
568
|
+
function O(n) {
|
|
566
569
|
if (!n || typeof n != "object") return;
|
|
567
570
|
const t = n;
|
|
568
571
|
if (typeof t.data == "string" && t.data.startsWith("0x"))
|
|
@@ -570,37 +573,37 @@ function U(n) {
|
|
|
570
573
|
if (typeof t.details == "string" && t.details.startsWith("0x"))
|
|
571
574
|
return t.details;
|
|
572
575
|
let e = t.cause, s = 0;
|
|
573
|
-
const
|
|
574
|
-
for (; e && typeof e == "object" && s <
|
|
576
|
+
const o = 5;
|
|
577
|
+
for (; e && typeof e == "object" && s < o; ) {
|
|
575
578
|
const i = e;
|
|
576
579
|
if (typeof i.data == "string" && i.data.startsWith("0x"))
|
|
577
580
|
return i.data;
|
|
578
581
|
e = i.cause, s++;
|
|
579
582
|
}
|
|
580
|
-
const
|
|
581
|
-
if (
|
|
582
|
-
return
|
|
583
|
+
const c = (typeof t.message == "string" ? t.message : "").match(/\b(0x[a-fA-F0-9]{8})\b/);
|
|
584
|
+
if (c)
|
|
585
|
+
return c[1];
|
|
583
586
|
}
|
|
584
587
|
function $t(n) {
|
|
585
|
-
const t =
|
|
588
|
+
const t = O(n);
|
|
586
589
|
if (t) {
|
|
587
590
|
const e = t.substring(0, 10);
|
|
588
591
|
return x[t] ?? x[e];
|
|
589
592
|
}
|
|
590
593
|
}
|
|
591
|
-
function
|
|
592
|
-
const t =
|
|
594
|
+
function Ct(n) {
|
|
595
|
+
const t = O(n);
|
|
593
596
|
if (t === void 0) return !1;
|
|
594
597
|
const e = t.substring(0, 10);
|
|
595
598
|
return t in x || e in x;
|
|
596
599
|
}
|
|
597
|
-
function
|
|
600
|
+
function D(n) {
|
|
598
601
|
console.error("[Contract Error] Raw error:", n);
|
|
599
|
-
const t =
|
|
602
|
+
const t = O(n);
|
|
600
603
|
if (console.error("[Contract Error] Extracted error data:", t), t) {
|
|
601
|
-
const s = t.substring(0, 10),
|
|
602
|
-
if (
|
|
603
|
-
throw console.error("[Contract Error] Known error:",
|
|
604
|
+
const s = t.substring(0, 10), o = x[t] ?? x[s];
|
|
605
|
+
if (o)
|
|
606
|
+
throw console.error("[Contract Error] Known error:", o), new Error(o);
|
|
604
607
|
}
|
|
605
608
|
const e = (n == null ? void 0 : n.message) || "";
|
|
606
609
|
if (e.includes("gas limit too high") || e.includes("21000000") || e.includes("Internal JSON-RPC error")) {
|
|
@@ -616,61 +619,102 @@ function K(n) {
|
|
|
616
619
|
}
|
|
617
620
|
throw n instanceof Error ? (console.error("[Contract Error] Unhandled error:", n.message), n) : new Error(`Contract call failed: ${String(n)}`);
|
|
618
621
|
}
|
|
619
|
-
class
|
|
622
|
+
class Ut {
|
|
620
623
|
/**
|
|
621
624
|
* Creates a new PeginManager instance.
|
|
622
625
|
*
|
|
623
626
|
* @param config - Manager configuration including wallets and contract addresses
|
|
624
627
|
*/
|
|
625
628
|
constructor(t) {
|
|
626
|
-
|
|
629
|
+
H(this, "config");
|
|
627
630
|
this.config = t;
|
|
628
631
|
}
|
|
629
632
|
/**
|
|
630
|
-
* Prepares
|
|
633
|
+
* Prepares an atomic swap peg-in by building the Pre-PegIn HTLC transaction,
|
|
634
|
+
* funding it, constructing the PegIn transaction, and signing the PegIn input.
|
|
631
635
|
*
|
|
632
636
|
* This method orchestrates the following steps:
|
|
633
637
|
* 1. Get depositor BTC public key from wallet
|
|
634
|
-
* 2. Build unfunded
|
|
635
|
-
* 3. Select UTXOs
|
|
636
|
-
* 4. Fund
|
|
638
|
+
* 2. Build unfunded Pre-PegIn transaction (HTLC output) using primitives
|
|
639
|
+
* 3. Select UTXOs to cover the HTLC value
|
|
640
|
+
* 4. Fund the Pre-PegIn transaction
|
|
641
|
+
* 5. Derive the PegIn transaction from the funded Pre-PegIn txid
|
|
642
|
+
* 6. Build PSBT for signing the PegIn input (HTLC leaf 0)
|
|
643
|
+
* 7. Sign via BTC wallet and extract depositor signature
|
|
637
644
|
*
|
|
638
|
-
* The returned
|
|
639
|
-
*
|
|
645
|
+
* The returned `fundedPrePeginTxHex` is funded but unsigned (inputs unsigned).
|
|
646
|
+
* Use `signAndBroadcast()` AFTER registering on Ethereum to broadcast it.
|
|
640
647
|
*
|
|
641
|
-
* @param params -
|
|
642
|
-
* @returns
|
|
648
|
+
* @param params - Atomic pegin parameters including amount, HTLC params, UTXOs
|
|
649
|
+
* @returns Atomic pegin result with funded Pre-PegIn tx, signed PegIn input, and signatures
|
|
643
650
|
* @throws Error if wallet operations fail or insufficient funds
|
|
644
651
|
*/
|
|
645
|
-
async
|
|
646
|
-
const e = await this.config.btcWallet.getPublicKeyHex(), s = e.length === 66 ? e.slice(2) : e,
|
|
652
|
+
async prepareAtomicPegin(t) {
|
|
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), i = {
|
|
647
654
|
depositorPubkey: s,
|
|
648
|
-
vaultProviderPubkey:
|
|
649
|
-
vaultKeeperPubkeys:
|
|
650
|
-
universalChallengerPubkeys:
|
|
651
|
-
|
|
655
|
+
vaultProviderPubkey: o,
|
|
656
|
+
vaultKeeperPubkeys: r,
|
|
657
|
+
universalChallengerPubkeys: c,
|
|
658
|
+
hashH: t.hashH,
|
|
659
|
+
timelockRefund: t.timelockRefund,
|
|
652
660
|
pegInAmount: t.amount,
|
|
653
|
-
|
|
661
|
+
feeRate: BigInt(t.feeRate),
|
|
662
|
+
numLocalChallengers: t.numLocalChallengers,
|
|
663
|
+
councilQuorum: t.councilQuorum,
|
|
664
|
+
councilSize: t.councilSize,
|
|
654
665
|
network: this.config.btcNetwork
|
|
655
|
-
}
|
|
656
|
-
t.availableUTXOs,
|
|
657
|
-
|
|
666
|
+
}, a = await tt(i), u = ct(
|
|
667
|
+
[...t.availableUTXOs],
|
|
668
|
+
a.totalOutputValue,
|
|
658
669
|
t.feeRate
|
|
659
|
-
), l =
|
|
660
|
-
unfundedTxHex:
|
|
661
|
-
selectedUTXOs:
|
|
670
|
+
), l = U(this.config.btcNetwork), d = et({
|
|
671
|
+
unfundedTxHex: a.psbtHex,
|
|
672
|
+
selectedUTXOs: u.selectedUTXOs,
|
|
662
673
|
changeAddress: t.changeAddress,
|
|
663
|
-
changeAmount:
|
|
674
|
+
changeAmount: u.changeAmount,
|
|
664
675
|
network: l
|
|
665
|
-
}), f =
|
|
676
|
+
}), f = k(X(d)), h = await nt({
|
|
677
|
+
prePeginParams: i,
|
|
678
|
+
timelockPegin: t.timelockPegin,
|
|
679
|
+
fundedPrePeginTxid: f
|
|
680
|
+
}), m = await st({
|
|
681
|
+
peginTxHex: h.txHex,
|
|
682
|
+
fundedPrePeginTxHex: d,
|
|
683
|
+
depositorPubkey: s,
|
|
684
|
+
vaultProviderPubkey: o,
|
|
685
|
+
vaultKeeperPubkeys: r,
|
|
686
|
+
universalChallengerPubkeys: c,
|
|
687
|
+
hashH: t.hashH,
|
|
688
|
+
timelockRefund: t.timelockRefund,
|
|
689
|
+
network: this.config.btcNetwork
|
|
690
|
+
}), P = await this.config.btcWallet.signPsbt(
|
|
691
|
+
m.psbtHex,
|
|
692
|
+
{
|
|
693
|
+
autoFinalized: !1,
|
|
694
|
+
signInputs: [
|
|
695
|
+
{
|
|
696
|
+
index: 0,
|
|
697
|
+
publicKey: e,
|
|
698
|
+
disableTweakSigner: !0
|
|
699
|
+
}
|
|
700
|
+
]
|
|
701
|
+
}
|
|
702
|
+
), p = rt(
|
|
703
|
+
P,
|
|
704
|
+
s
|
|
705
|
+
);
|
|
666
706
|
return {
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
707
|
+
fundedPrePeginTxHex: d,
|
|
708
|
+
htlcValue: a.htlcValue,
|
|
709
|
+
signedPeginInputPsbtHex: P,
|
|
710
|
+
peginInputSignature: p,
|
|
711
|
+
vaultScriptPubKey: h.vaultScriptPubKey,
|
|
712
|
+
peginTxHex: h.txHex,
|
|
713
|
+
prePeginTxid: f,
|
|
714
|
+
peginTxid: h.txid,
|
|
715
|
+
selectedUTXOs: u.selectedUTXOs,
|
|
716
|
+
fee: u.fee,
|
|
717
|
+
changeAmount: u.changeAmount
|
|
674
718
|
};
|
|
675
719
|
}
|
|
676
720
|
/**
|
|
@@ -689,57 +733,63 @@ class Ct {
|
|
|
689
733
|
* @throws Error if signing or broadcasting fails
|
|
690
734
|
*/
|
|
691
735
|
async signAndBroadcast(t) {
|
|
692
|
-
const {
|
|
693
|
-
if (
|
|
736
|
+
const { fundedPrePeginTxHex: e, depositorBtcPubkey: s } = t, o = e.startsWith("0x") ? e.slice(2) : e, r = S.fromHex(o);
|
|
737
|
+
if (r.ins.length === 0)
|
|
694
738
|
throw new Error("Transaction has no inputs");
|
|
695
|
-
const
|
|
696
|
-
|
|
739
|
+
const c = new C();
|
|
740
|
+
c.setVersion(r.version), c.setLocktime(r.locktime);
|
|
697
741
|
const i = s.startsWith("0x") ? s.slice(2) : s;
|
|
698
742
|
if (i.length !== 64 || !/^[0-9a-fA-F]+$/.test(i))
|
|
699
743
|
throw new Error(
|
|
700
744
|
"Invalid depositorBtcPubkey: expected 64 hex characters (x-only pubkey)"
|
|
701
745
|
);
|
|
702
|
-
const
|
|
703
|
-
if (
|
|
746
|
+
const a = g.from(i, "hex");
|
|
747
|
+
if (a.length !== 32)
|
|
704
748
|
throw new Error(
|
|
705
|
-
`Invalid depositorBtcPubkey length: expected 32 bytes, got ${
|
|
749
|
+
`Invalid depositorBtcPubkey length: expected 32 bytes, got ${a.length}`
|
|
706
750
|
);
|
|
707
|
-
const
|
|
708
|
-
const y =
|
|
709
|
-
return
|
|
710
|
-
input:
|
|
711
|
-
utxoData:
|
|
751
|
+
const u = this.config.mempoolApiUrl, l = r.ins.map((p) => {
|
|
752
|
+
const y = g.from(p.hash).reverse().toString("hex"), b = p.index;
|
|
753
|
+
return gt(y, b, u).then((w) => ({
|
|
754
|
+
input: p,
|
|
755
|
+
utxoData: w,
|
|
712
756
|
txid: y,
|
|
713
|
-
vout:
|
|
757
|
+
vout: b
|
|
714
758
|
}));
|
|
715
759
|
}), d = await Promise.all(l);
|
|
716
|
-
for (const { input:
|
|
717
|
-
const
|
|
760
|
+
for (const { input: p, utxoData: y, txid: b, vout: w } of d) {
|
|
761
|
+
const T = lt(
|
|
718
762
|
{
|
|
719
763
|
value: y.value,
|
|
720
764
|
scriptPubKey: y.scriptPubKey
|
|
721
765
|
},
|
|
722
|
-
|
|
766
|
+
a
|
|
723
767
|
);
|
|
724
|
-
|
|
725
|
-
hash:
|
|
726
|
-
index:
|
|
727
|
-
sequence:
|
|
728
|
-
...
|
|
768
|
+
c.addInput({
|
|
769
|
+
hash: p.hash,
|
|
770
|
+
index: p.index,
|
|
771
|
+
sequence: p.sequence,
|
|
772
|
+
...T
|
|
729
773
|
});
|
|
730
774
|
}
|
|
731
|
-
for (const
|
|
732
|
-
|
|
733
|
-
script:
|
|
734
|
-
value:
|
|
775
|
+
for (const p of r.outs)
|
|
776
|
+
c.addOutput({
|
|
777
|
+
script: p.script,
|
|
778
|
+
value: p.value
|
|
735
779
|
});
|
|
736
|
-
const f = await this.config.btcWallet.signPsbt(
|
|
780
|
+
const f = await this.config.btcWallet.signPsbt(c.toHex()), h = C.fromHex(f);
|
|
737
781
|
try {
|
|
738
|
-
|
|
739
|
-
} catch {
|
|
782
|
+
h.finalizeAllInputs();
|
|
783
|
+
} catch (p) {
|
|
784
|
+
if (!h.data.inputs.every(
|
|
785
|
+
(b) => b.finalScriptWitness || b.finalScriptSig
|
|
786
|
+
))
|
|
787
|
+
throw new Error(
|
|
788
|
+
`PSBT finalization failed and wallet did not auto-finalize: ${p}`
|
|
789
|
+
);
|
|
740
790
|
}
|
|
741
|
-
const
|
|
742
|
-
return await
|
|
791
|
+
const m = h.extractTransaction().toHex();
|
|
792
|
+
return await pt(m, u);
|
|
743
793
|
}
|
|
744
794
|
/**
|
|
745
795
|
* Registers a peg-in on Ethereum by calling the BTCVaultsManager contract.
|
|
@@ -761,83 +811,87 @@ class Ct {
|
|
|
761
811
|
async registerPeginOnChain(t) {
|
|
762
812
|
const {
|
|
763
813
|
depositorBtcPubkey: e,
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
814
|
+
unsignedPrePeginTx: s,
|
|
815
|
+
depositorSignedPeginTx: o,
|
|
816
|
+
vaultProvider: r,
|
|
817
|
+
hashlock: c,
|
|
818
|
+
onPopSigned: i,
|
|
819
|
+
depositorPayoutBtcAddress: a,
|
|
820
|
+
depositorLamportPkHash: u,
|
|
821
|
+
preSignedBtcPopSignature: l
|
|
770
822
|
} = t;
|
|
771
823
|
if (!this.config.ethWallet.account)
|
|
772
824
|
throw new Error("Ethereum wallet account not found");
|
|
773
|
-
const
|
|
774
|
-
|
|
775
|
-
|
|
825
|
+
const d = this.config.ethWallet.account.address, f = await this.resolvePopSignature(
|
|
826
|
+
d,
|
|
827
|
+
l
|
|
776
828
|
);
|
|
777
|
-
|
|
778
|
-
const
|
|
779
|
-
|
|
780
|
-
),
|
|
781
|
-
if (await this.checkVaultExists(
|
|
829
|
+
i && await i();
|
|
830
|
+
const h = I(e), m = I(s), P = I(o), p = await this.resolvePayoutScriptPubKey(
|
|
831
|
+
a
|
|
832
|
+
), y = X(P);
|
|
833
|
+
if (await this.checkVaultExists(y))
|
|
782
834
|
throw new Error(
|
|
783
|
-
`Vault already exists for this transaction (ID: ${
|
|
835
|
+
`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.`
|
|
784
836
|
);
|
|
785
|
-
const
|
|
837
|
+
const w = K({
|
|
786
838
|
chain: this.config.ethChain,
|
|
787
|
-
transport:
|
|
839
|
+
transport: N()
|
|
788
840
|
});
|
|
789
|
-
let
|
|
841
|
+
let T;
|
|
790
842
|
try {
|
|
791
|
-
|
|
843
|
+
T = await w.readContract({
|
|
792
844
|
address: this.config.vaultContracts.btcVaultsManager,
|
|
793
|
-
abi:
|
|
845
|
+
abi: $,
|
|
794
846
|
functionName: "getPegInFee",
|
|
795
|
-
args: [
|
|
847
|
+
args: [r]
|
|
796
848
|
});
|
|
797
849
|
} catch {
|
|
798
850
|
throw new Error(
|
|
799
851
|
"Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct."
|
|
800
852
|
);
|
|
801
853
|
}
|
|
802
|
-
const
|
|
803
|
-
abi:
|
|
854
|
+
const A = ot({
|
|
855
|
+
abi: $,
|
|
804
856
|
functionName: "submitPeginRequest",
|
|
805
857
|
args: [
|
|
806
|
-
c,
|
|
807
858
|
d,
|
|
808
|
-
|
|
859
|
+
h,
|
|
809
860
|
f,
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
861
|
+
m,
|
|
862
|
+
P,
|
|
863
|
+
r,
|
|
864
|
+
c,
|
|
865
|
+
p,
|
|
866
|
+
u
|
|
813
867
|
]
|
|
814
868
|
});
|
|
815
|
-
let
|
|
869
|
+
let F;
|
|
816
870
|
try {
|
|
817
|
-
|
|
871
|
+
F = await w.estimateGas({
|
|
818
872
|
to: this.config.vaultContracts.btcVaultsManager,
|
|
819
|
-
data:
|
|
820
|
-
value:
|
|
873
|
+
data: A,
|
|
874
|
+
value: T,
|
|
821
875
|
account: this.config.ethWallet.account.address
|
|
822
876
|
});
|
|
823
|
-
} catch (
|
|
824
|
-
|
|
877
|
+
} catch (v) {
|
|
878
|
+
D(v);
|
|
825
879
|
}
|
|
826
880
|
try {
|
|
827
881
|
return {
|
|
828
882
|
ethTxHash: await this.config.ethWallet.sendTransaction({
|
|
829
883
|
to: this.config.vaultContracts.btcVaultsManager,
|
|
830
|
-
data:
|
|
831
|
-
value:
|
|
884
|
+
data: A,
|
|
885
|
+
value: T,
|
|
832
886
|
account: this.config.ethWallet.account,
|
|
833
887
|
chain: this.config.ethChain,
|
|
834
|
-
gas:
|
|
888
|
+
gas: F
|
|
835
889
|
}),
|
|
836
|
-
vaultId:
|
|
837
|
-
btcPopSignature:
|
|
890
|
+
vaultId: y,
|
|
891
|
+
btcPopSignature: f
|
|
838
892
|
};
|
|
839
|
-
} catch (
|
|
840
|
-
|
|
893
|
+
} catch (v) {
|
|
894
|
+
D(v);
|
|
841
895
|
}
|
|
842
896
|
}
|
|
843
897
|
/**
|
|
@@ -848,15 +902,15 @@ class Ct {
|
|
|
848
902
|
*/
|
|
849
903
|
async checkVaultExists(t) {
|
|
850
904
|
try {
|
|
851
|
-
return (await
|
|
905
|
+
return (await K({
|
|
852
906
|
chain: this.config.ethChain,
|
|
853
|
-
transport:
|
|
907
|
+
transport: N()
|
|
854
908
|
}).readContract({
|
|
855
909
|
address: this.config.vaultContracts.btcVaultsManager,
|
|
856
|
-
abi:
|
|
910
|
+
abi: $,
|
|
857
911
|
functionName: "getBTCVault",
|
|
858
912
|
args: [t]
|
|
859
|
-
})).depositor !==
|
|
913
|
+
})).depositor !== it;
|
|
860
914
|
} catch {
|
|
861
915
|
return !1;
|
|
862
916
|
}
|
|
@@ -874,19 +928,19 @@ class Ct {
|
|
|
874
928
|
e = t;
|
|
875
929
|
else {
|
|
876
930
|
e = await this.config.btcWallet.getAddress();
|
|
877
|
-
const
|
|
878
|
-
if (!
|
|
931
|
+
const o = await this.config.btcWallet.getPublicKeyHex();
|
|
932
|
+
if (!at(
|
|
879
933
|
e,
|
|
880
|
-
|
|
934
|
+
o,
|
|
881
935
|
this.config.btcNetwork
|
|
882
936
|
))
|
|
883
937
|
throw new Error(
|
|
884
938
|
"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)."
|
|
885
939
|
);
|
|
886
940
|
}
|
|
887
|
-
const s =
|
|
941
|
+
const s = U(this.config.btcNetwork);
|
|
888
942
|
try {
|
|
889
|
-
return `0x${
|
|
943
|
+
return `0x${L.address.toOutputScript(e, s).toString("hex")}`;
|
|
890
944
|
} catch {
|
|
891
945
|
throw new Error(
|
|
892
946
|
`Invalid BTC payout address: "${e}". Please provide a valid Bitcoin address for the ${this.config.btcNetwork} network.`
|
|
@@ -902,11 +956,11 @@ class Ct {
|
|
|
902
956
|
async resolvePopSignature(t, e) {
|
|
903
957
|
if (e)
|
|
904
958
|
return e;
|
|
905
|
-
const s = this.config.vaultContracts.btcVaultsManager,
|
|
906
|
-
|
|
959
|
+
const s = this.config.vaultContracts.btcVaultsManager, o = `${t.toLowerCase()}:${this.config.ethChain.id}:pegin:${s.toLowerCase()}`, r = await this.config.btcWallet.signMessage(
|
|
960
|
+
o,
|
|
907
961
|
"bip322-simple"
|
|
908
962
|
);
|
|
909
|
-
return
|
|
963
|
+
return r.startsWith("0x") ? r : `0x${g.from(r, "base64").toString("hex")}`;
|
|
910
964
|
}
|
|
911
965
|
/**
|
|
912
966
|
* Gets the configured Bitcoin network.
|
|
@@ -925,14 +979,14 @@ class Ct {
|
|
|
925
979
|
return this.config.vaultContracts.btcVaultsManager;
|
|
926
980
|
}
|
|
927
981
|
}
|
|
928
|
-
class
|
|
982
|
+
class Ot {
|
|
929
983
|
/**
|
|
930
984
|
* Creates a new PayoutManager instance.
|
|
931
985
|
*
|
|
932
986
|
* @param config - Manager configuration including wallet
|
|
933
987
|
*/
|
|
934
988
|
constructor(t) {
|
|
935
|
-
|
|
989
|
+
H(this, "config");
|
|
936
990
|
this.config = t;
|
|
937
991
|
}
|
|
938
992
|
/**
|
|
@@ -958,10 +1012,10 @@ class Ht {
|
|
|
958
1012
|
* @throws Error if wallet operations fail or signature extraction fails
|
|
959
1013
|
*/
|
|
960
1014
|
async signPayoutTransaction(t) {
|
|
961
|
-
const e = await this.config.btcWallet.getPublicKeyHex(), { depositorPubkey: s } =
|
|
1015
|
+
const e = await this.config.btcWallet.getPublicKeyHex(), { depositorPubkey: s } = V(
|
|
962
1016
|
e,
|
|
963
1017
|
t.depositorBtcPubkey
|
|
964
|
-
),
|
|
1018
|
+
), o = await M({
|
|
965
1019
|
payoutTxHex: t.payoutTxHex,
|
|
966
1020
|
peginTxHex: t.peginTxHex,
|
|
967
1021
|
assertTxHex: t.assertTxHex,
|
|
@@ -971,8 +1025,8 @@ class Ht {
|
|
|
971
1025
|
universalChallengerBtcPubkeys: t.universalChallengerBtcPubkeys,
|
|
972
1026
|
timelockPegin: t.timelockPegin,
|
|
973
1027
|
network: this.config.network
|
|
974
|
-
}),
|
|
975
|
-
|
|
1028
|
+
}), r = await this.config.btcWallet.signPsbt(
|
|
1029
|
+
o.psbtHex,
|
|
976
1030
|
{
|
|
977
1031
|
autoFinalized: !1,
|
|
978
1032
|
signInputs: [
|
|
@@ -985,7 +1039,7 @@ class Ht {
|
|
|
985
1039
|
}
|
|
986
1040
|
);
|
|
987
1041
|
return {
|
|
988
|
-
signature:
|
|
1042
|
+
signature: R(r, s),
|
|
989
1043
|
depositorBtcPubkey: s
|
|
990
1044
|
};
|
|
991
1045
|
}
|
|
@@ -1019,25 +1073,25 @@ class Ht {
|
|
|
1019
1073
|
throw new Error(
|
|
1020
1074
|
"Wallet does not support batch signing (signPsbts method not available)"
|
|
1021
1075
|
);
|
|
1022
|
-
const e = await this.config.btcWallet.getPublicKeyHex(), s = [],
|
|
1023
|
-
for (const
|
|
1024
|
-
const { depositorPubkey:
|
|
1076
|
+
const e = await this.config.btcWallet.getPublicKeyHex(), s = [], o = [], r = [];
|
|
1077
|
+
for (const a of t) {
|
|
1078
|
+
const { depositorPubkey: u } = V(
|
|
1025
1079
|
e,
|
|
1026
|
-
|
|
1080
|
+
a.depositorBtcPubkey
|
|
1027
1081
|
);
|
|
1028
|
-
|
|
1029
|
-
const l = await
|
|
1030
|
-
payoutTxHex:
|
|
1031
|
-
peginTxHex:
|
|
1032
|
-
assertTxHex:
|
|
1033
|
-
depositorBtcPubkey:
|
|
1034
|
-
vaultProviderBtcPubkey:
|
|
1035
|
-
vaultKeeperBtcPubkeys:
|
|
1036
|
-
universalChallengerBtcPubkeys:
|
|
1037
|
-
timelockPegin:
|
|
1082
|
+
r.push(u);
|
|
1083
|
+
const l = await M({
|
|
1084
|
+
payoutTxHex: a.payoutTxHex,
|
|
1085
|
+
peginTxHex: a.peginTxHex,
|
|
1086
|
+
assertTxHex: a.assertTxHex,
|
|
1087
|
+
depositorBtcPubkey: u,
|
|
1088
|
+
vaultProviderBtcPubkey: a.vaultProviderBtcPubkey,
|
|
1089
|
+
vaultKeeperBtcPubkeys: a.vaultKeeperBtcPubkeys,
|
|
1090
|
+
universalChallengerBtcPubkeys: a.universalChallengerBtcPubkeys,
|
|
1091
|
+
timelockPegin: a.timelockPegin,
|
|
1038
1092
|
network: this.config.network
|
|
1039
1093
|
});
|
|
1040
|
-
s.push(l.psbtHex),
|
|
1094
|
+
s.push(l.psbtHex), o.push({
|
|
1041
1095
|
autoFinalized: !1,
|
|
1042
1096
|
signInputs: [
|
|
1043
1097
|
{
|
|
@@ -1048,65 +1102,54 @@ class Ht {
|
|
|
1048
1102
|
]
|
|
1049
1103
|
});
|
|
1050
1104
|
}
|
|
1051
|
-
const
|
|
1105
|
+
const c = await this.config.btcWallet.signPsbts(
|
|
1052
1106
|
s,
|
|
1053
|
-
|
|
1107
|
+
o
|
|
1054
1108
|
);
|
|
1055
|
-
if (
|
|
1109
|
+
if (c.length !== t.length)
|
|
1056
1110
|
throw new Error(
|
|
1057
|
-
`Expected ${t.length} signed PSBTs but received ${
|
|
1111
|
+
`Expected ${t.length} signed PSBTs but received ${c.length}`
|
|
1058
1112
|
);
|
|
1059
1113
|
const i = [];
|
|
1060
|
-
for (let
|
|
1061
|
-
const
|
|
1062
|
-
|
|
1063
|
-
|
|
1114
|
+
for (let a = 0; a < t.length; a++) {
|
|
1115
|
+
const u = r[a], l = R(
|
|
1116
|
+
c[a],
|
|
1117
|
+
u
|
|
1064
1118
|
);
|
|
1065
1119
|
i.push({
|
|
1066
1120
|
payoutSignature: l,
|
|
1067
|
-
depositorBtcPubkey:
|
|
1121
|
+
depositorBtcPubkey: u
|
|
1068
1122
|
});
|
|
1069
1123
|
}
|
|
1070
1124
|
return i;
|
|
1071
1125
|
}
|
|
1072
1126
|
}
|
|
1073
1127
|
export {
|
|
1074
|
-
|
|
1075
|
-
N as B,
|
|
1128
|
+
B,
|
|
1076
1129
|
x as C,
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
mt as F,
|
|
1080
|
-
Q as L,
|
|
1081
|
-
V as M,
|
|
1082
|
-
Z as P,
|
|
1083
|
-
G as T,
|
|
1084
|
-
tt as W,
|
|
1130
|
+
dt as M,
|
|
1131
|
+
Ut as P,
|
|
1085
1132
|
wt as a,
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1133
|
+
vt as b,
|
|
1134
|
+
X as c,
|
|
1135
|
+
kt as d,
|
|
1136
|
+
ut as e,
|
|
1137
|
+
lt as f,
|
|
1138
|
+
Tt as g,
|
|
1139
|
+
Ot as h,
|
|
1140
|
+
Ht as i,
|
|
1141
|
+
Et as j,
|
|
1142
|
+
St as k,
|
|
1096
1143
|
It as l,
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
ct as w,
|
|
1108
|
-
B as x,
|
|
1109
|
-
U as y,
|
|
1110
|
-
$t as z
|
|
1144
|
+
Bt as m,
|
|
1145
|
+
ht as n,
|
|
1146
|
+
gt as o,
|
|
1147
|
+
pt as p,
|
|
1148
|
+
$ as q,
|
|
1149
|
+
O as r,
|
|
1150
|
+
ct as s,
|
|
1151
|
+
$t as t,
|
|
1152
|
+
Ct as u,
|
|
1153
|
+
D as v
|
|
1111
1154
|
};
|
|
1112
|
-
//# sourceMappingURL=PayoutManager-
|
|
1155
|
+
//# sourceMappingURL=PayoutManager-BK1jKgL5.js.map
|