@babylonlabs-io/ts-sdk 0.37.1 → 0.37.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{PeginManager-C7-XYrkK.js → PeginManager-B1Mh8dJ3.js} +325 -315
- package/dist/{PeginManager-C7-XYrkK.js.map → PeginManager-B1Mh8dJ3.js.map} +1 -1
- package/dist/PeginManager-BbHPyz-G.cjs +2 -0
- package/dist/PeginManager-BbHPyz-G.cjs.map +1 -0
- package/dist/{buildAndBroadcastRefund-C1eOhIdo.js → buildAndBroadcastRefund-Ck_ddlLk.js} +20 -15
- package/dist/{buildAndBroadcastRefund-C1eOhIdo.js.map → buildAndBroadcastRefund-Ck_ddlLk.js.map} +1 -1
- package/dist/{buildAndBroadcastRefund-_CEDUU5H.cjs → buildAndBroadcastRefund-DyPQyghx.cjs} +2 -2
- package/dist/{buildAndBroadcastRefund-_CEDUU5H.cjs.map → buildAndBroadcastRefund-DyPQyghx.cjs.map} +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +2 -2
- package/dist/tbv/core/index.cjs +1 -1
- package/dist/tbv/core/index.js +2 -2
- package/dist/tbv/core/managers/PeginManager.d.ts.map +1 -1
- package/dist/tbv/core/managers/index.cjs +1 -1
- package/dist/tbv/core/managers/index.js +1 -1
- package/dist/tbv/core/services/index.cjs +1 -1
- package/dist/tbv/core/services/index.js +1 -1
- package/dist/tbv/core/services/refund/buildAndBroadcastRefund.d.ts.map +1 -1
- package/dist/tbv/index.cjs +1 -1
- package/dist/tbv/index.js +2 -2
- package/package.json +1 -1
- package/dist/PeginManager-CRuwG4I-.cjs +0 -2
- package/dist/PeginManager-CRuwG4I-.cjs.map +0 -1
|
@@ -1,46 +1,47 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { s as nt, c as
|
|
5
|
-
import * as
|
|
6
|
-
import { Transaction as
|
|
1
|
+
var Xt = Object.defineProperty;
|
|
2
|
+
var Kt = (n, t, e) => t in n ? Xt(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var y = (n, t, e) => Kt(n, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
import { s as nt, c as It, H as Dt, r as U, a as F, b as rt, u as zt, d as it, e as at, f as ct, g as Gt } from "./sha2-BYVxyZzX.js";
|
|
5
|
+
import * as St from "bitcoinjs-lib";
|
|
6
|
+
import { Transaction as jt, Psbt as lt } from "bitcoinjs-lib";
|
|
7
7
|
import { Buffer as Q } from "buffer";
|
|
8
|
-
import { isAddressEqual as ut, encodeFunctionData as ht, zeroAddress as
|
|
8
|
+
import { isAddressEqual as ut, encodeFunctionData as ht, zeroAddress as qt } from "viem";
|
|
9
9
|
import { B as O } from "./BTCVaultRegistry.abi-DbJ5lsFJ.js";
|
|
10
10
|
import { h as A } from "./errors-CznAK5NB.js";
|
|
11
|
-
import { expandWotsSeed as
|
|
12
|
-
import { b as gt, a as
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import { p as
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return t ? { h: Number(n & N), l: Number(n >> Pt & N) } : { h: Number(n >> Pt & N) | 0, l: Number(n & N) | 0 };
|
|
23
|
-
}
|
|
11
|
+
import { expandWotsSeed as Zt, expandHashlockSecret as Qt, expandAuthAnchor as Yt, deriveVaultId as dt } from "@babylonlabs-io/babylon-tbv-rust-wasm";
|
|
12
|
+
import { b as gt, a as Jt, c as te, e as ee, f as ne } from "./peginInput-CYJzbuwA.js";
|
|
13
|
+
import { c as ft } from "./assertPsbtUnsignedTxMatches-D7RxpR4A.js";
|
|
14
|
+
import { s as T, u as M, h as Y, e as I, p as se, i as pt, j as mt } from "./bitcoin-B5aNKtsk.js";
|
|
15
|
+
import { c as oe } from "./signing-DaLvGwQe.js";
|
|
16
|
+
import { M as wt } from "./validation-CxqROCno.js";
|
|
17
|
+
import { c as re } from "./PayoutManager-sfxuOBGq.js";
|
|
18
|
+
import { s as ie, f as z, i as ae, w as Pt } from "./waitForTransactionReceiptSmartAware-CmgFXFza.js";
|
|
19
|
+
import { p as ce, f as le } from "./fundPeginTransaction-t-6TsHAY.js";
|
|
20
|
+
import { p as ue, f as he } from "./mempoolApi-CAIge7Nj.js";
|
|
21
|
+
const N = /* @__PURE__ */ BigInt(2 ** 32 - 1), bt = /* @__PURE__ */ BigInt(32);
|
|
24
22
|
function de(n, t = !1) {
|
|
23
|
+
return t ? { h: Number(n & N), l: Number(n >> bt & N) } : { h: Number(n >> bt & N) | 0, l: Number(n & N) | 0 };
|
|
24
|
+
}
|
|
25
|
+
function ge(n, t = !1) {
|
|
25
26
|
const e = n.length;
|
|
26
27
|
let s = new Uint32Array(e), o = new Uint32Array(e);
|
|
27
28
|
for (let r = 0; r < e; r++) {
|
|
28
|
-
const { h: i, l: a } =
|
|
29
|
+
const { h: i, l: a } = de(n[r], t);
|
|
29
30
|
[s[r], o[r]] = [i, a];
|
|
30
31
|
}
|
|
31
32
|
return [s, o];
|
|
32
33
|
}
|
|
33
|
-
const
|
|
34
|
-
function
|
|
35
|
-
const s = T(n), o =
|
|
34
|
+
const fe = (n, t, e) => n << e | t >>> 32 - e, pe = (n, t, e) => t << e | n >>> 32 - e, me = (n, t, e) => t << e - 32 | n >>> 64 - e, we = (n, t, e) => n << e - 32 | t >>> 64 - e, Pe = 106, be = 32, xt = 34;
|
|
35
|
+
function xe(n, t, e) {
|
|
36
|
+
const s = T(n), o = St.Transaction.fromHex(s);
|
|
36
37
|
if (o.outs.length <= t)
|
|
37
38
|
throw new Error(
|
|
38
39
|
`Pre-PegIn auth-anchor OP_RETURN missing: tx has ${o.outs.length} outputs, expected at least ${t + 1} (vault outputs + OP_RETURN)`
|
|
39
40
|
);
|
|
40
41
|
const r = o.outs[t], i = r.script;
|
|
41
|
-
if (i.length !==
|
|
42
|
+
if (i.length !== xt || i[0] !== Pe || i[1] !== be)
|
|
42
43
|
throw new Error(
|
|
43
|
-
`Pre-PegIn auth-anchor OP_RETURN at vout ${t} has unexpected script encoding (got ${i.length}-byte script with prefix 0x${i.slice(0, Math.min(2, i.length)).toString("hex")}; expected ${
|
|
44
|
+
`Pre-PegIn auth-anchor OP_RETURN at vout ${t} has unexpected script encoding (got ${i.length}-byte script with prefix 0x${i.slice(0, Math.min(2, i.length)).toString("hex")}; expected ${xt}-byte OP_RETURN + PUSH32 layout)`
|
|
44
45
|
);
|
|
45
46
|
const a = i.slice(2).toString("hex").toLowerCase();
|
|
46
47
|
if (a !== e.toLowerCase())
|
|
@@ -52,11 +53,11 @@ function be(n, t, e) {
|
|
|
52
53
|
`Pre-PegIn auth-anchor OP_RETURN at vout ${t} has non-zero value ${r.value}; OP_RETURN outputs must be 0-value`
|
|
53
54
|
);
|
|
54
55
|
}
|
|
55
|
-
const C = 32, G = 32, J = 36,
|
|
56
|
+
const C = 32, G = 32, J = 36, At = 32, X = 4, ye = X + C + X + At;
|
|
56
57
|
function tt(n, t, e) {
|
|
57
58
|
n[t] = e >>> 24 & 255, n[t + 1] = e >>> 16 & 255, n[t + 2] = e >>> 8 & 255, n[t + 3] = e & 255;
|
|
58
59
|
}
|
|
59
|
-
function
|
|
60
|
+
function ke(n) {
|
|
60
61
|
if (n.txid.length !== G)
|
|
61
62
|
throw new Error(
|
|
62
63
|
`outpoint.txid must be exactly ${G} bytes, got ${n.txid.length}`
|
|
@@ -72,12 +73,12 @@ function yt(n, t) {
|
|
|
72
73
|
if (n[s] !== t[s]) return n[s] - t[s];
|
|
73
74
|
return n.length - t.length;
|
|
74
75
|
}
|
|
75
|
-
function
|
|
76
|
+
function Ee(n) {
|
|
76
77
|
if (n.length === 0)
|
|
77
78
|
throw new Error(
|
|
78
79
|
"buildFundingOutpointsCommitment: outpoints must be non-empty"
|
|
79
80
|
);
|
|
80
|
-
const t = n.map(
|
|
81
|
+
const t = n.map(ke);
|
|
81
82
|
t.sort(yt);
|
|
82
83
|
for (let s = 1; s < t.length; s++)
|
|
83
84
|
if (yt(t[s - 1], t[s]) === 0)
|
|
@@ -89,33 +90,33 @@ function ke(n) {
|
|
|
89
90
|
e.set(t[s], s * J);
|
|
90
91
|
return nt(e);
|
|
91
92
|
}
|
|
92
|
-
function
|
|
93
|
+
function Te(n) {
|
|
93
94
|
if (n.depositorBtcPubkey.length !== C)
|
|
94
95
|
throw new Error(
|
|
95
96
|
`vaultContext: depositorBtcPubkey must be exactly ${C} bytes, got ${n.depositorBtcPubkey.length}`
|
|
96
97
|
);
|
|
97
|
-
const t =
|
|
98
|
+
const t = Ee(n.fundingOutpoints), e = new Uint8Array(ye);
|
|
98
99
|
let s = 0;
|
|
99
|
-
return tt(e, s, C), s += X, e.set(n.depositorBtcPubkey, s), s += C, tt(e, s,
|
|
100
|
+
return tt(e, s, C), s += X, e.set(n.depositorBtcPubkey, s), s += C, tt(e, s, At), s += X, e.set(t, s), e;
|
|
100
101
|
}
|
|
101
|
-
const
|
|
102
|
-
async function
|
|
103
|
-
const e =
|
|
102
|
+
const _e = "babylon-btc-vault", Rt = 32, kt = Rt * 2, ve = /^[0-9a-f]+$/;
|
|
103
|
+
async function Be(n, t) {
|
|
104
|
+
const e = Te(t), s = M(e), o = await n.deriveContextHash(_e, s);
|
|
104
105
|
if (typeof o != "string")
|
|
105
106
|
throw new Error(
|
|
106
107
|
`deriveVaultRoot: wallet must return a string, got ${typeof o}`
|
|
107
108
|
);
|
|
108
|
-
if (o.length !==
|
|
109
|
+
if (o.length !== kt)
|
|
109
110
|
throw new Error(
|
|
110
|
-
`deriveVaultRoot: wallet must return a ${
|
|
111
|
+
`deriveVaultRoot: wallet must return a ${kt}-character hex string (${Rt} bytes), got length ${o.length}`
|
|
111
112
|
);
|
|
112
|
-
if (!
|
|
113
|
+
if (!ve.test(o))
|
|
113
114
|
throw new Error(
|
|
114
115
|
"deriveVaultRoot: wallet must return lowercase hex per derive-context-hash.md §2.1; got value with non-lowercase or non-hex characters"
|
|
115
116
|
);
|
|
116
117
|
return Y(o);
|
|
117
118
|
}
|
|
118
|
-
const
|
|
119
|
+
const Ie = /* @__PURE__ */ Uint8Array.from([
|
|
119
120
|
7,
|
|
120
121
|
4,
|
|
121
122
|
13,
|
|
@@ -132,43 +133,43 @@ const Be = /* @__PURE__ */ Uint8Array.from([
|
|
|
132
133
|
14,
|
|
133
134
|
11,
|
|
134
135
|
8
|
|
135
|
-
]),
|
|
136
|
-
const e = [[
|
|
136
|
+
]), Ot = Uint8Array.from(new Array(16).fill(0).map((n, t) => t)), Se = Ot.map((n) => (9 * n + 5) % 16), Ht = /* @__PURE__ */ (() => {
|
|
137
|
+
const e = [[Ot], [Se]];
|
|
137
138
|
for (let s = 0; s < 4; s++)
|
|
138
139
|
for (let o of e)
|
|
139
|
-
o.push(o[s].map((r) =>
|
|
140
|
+
o.push(o[s].map((r) => Ie[r]));
|
|
140
141
|
return e;
|
|
141
|
-
})(), $t =
|
|
142
|
+
})(), $t = Ht[0], Ct = Ht[1], Ut = /* @__PURE__ */ [
|
|
142
143
|
[11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8],
|
|
143
144
|
[12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7],
|
|
144
145
|
[13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9],
|
|
145
146
|
[14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6],
|
|
146
147
|
[15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5]
|
|
147
|
-
].map((n) => Uint8Array.from(n)),
|
|
148
|
+
].map((n) => Uint8Array.from(n)), Ae = /* @__PURE__ */ $t.map((n, t) => n.map((e) => Ut[t][e])), Re = /* @__PURE__ */ Ct.map((n, t) => n.map((e) => Ut[t][e])), Oe = /* @__PURE__ */ Uint32Array.from([
|
|
148
149
|
0,
|
|
149
150
|
1518500249,
|
|
150
151
|
1859775393,
|
|
151
152
|
2400959708,
|
|
152
153
|
2840853838
|
|
153
|
-
]),
|
|
154
|
+
]), He = /* @__PURE__ */ Uint32Array.from([
|
|
154
155
|
1352829926,
|
|
155
156
|
1548603684,
|
|
156
157
|
1836072691,
|
|
157
158
|
2053994217,
|
|
158
159
|
0
|
|
159
160
|
]);
|
|
160
|
-
function
|
|
161
|
+
function Et(n, t, e, s) {
|
|
161
162
|
return n === 0 ? t ^ e ^ s : n === 1 ? t & e | ~t & s : n === 2 ? (t | ~e) ^ s : n === 3 ? t & s | e & ~s : t ^ (e | ~s);
|
|
162
163
|
}
|
|
163
164
|
const V = /* @__PURE__ */ new Uint32Array(16);
|
|
164
|
-
class $e extends
|
|
165
|
+
class $e extends Dt {
|
|
165
166
|
constructor() {
|
|
166
167
|
super(64, 20, 8, !0);
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
168
|
+
y(this, "h0", 1732584193);
|
|
169
|
+
y(this, "h1", -271733879);
|
|
170
|
+
y(this, "h2", -1732584194);
|
|
171
|
+
y(this, "h3", 271733878);
|
|
172
|
+
y(this, "h4", -1009589776);
|
|
172
173
|
}
|
|
173
174
|
get() {
|
|
174
175
|
const { h0: e, h1: s, h2: o, h3: r, h4: i } = this;
|
|
@@ -180,19 +181,19 @@ class $e extends Kt {
|
|
|
180
181
|
process(e, s) {
|
|
181
182
|
for (let d = 0; d < 16; d++, s += 4)
|
|
182
183
|
V[d] = e.getUint32(s, !0);
|
|
183
|
-
let o = this.h0 | 0, r = o, i = this.h1 | 0, a = i, c = this.h2 | 0,
|
|
184
|
+
let o = this.h0 | 0, r = o, i = this.h1 | 0, a = i, c = this.h2 | 0, h = c, l = this.h3 | 0, m = l, b = this.h4 | 0, x = b;
|
|
184
185
|
for (let d = 0; d < 5; d++) {
|
|
185
|
-
const
|
|
186
|
-
for (let
|
|
187
|
-
const
|
|
188
|
-
o =
|
|
186
|
+
const f = 4 - d, k = Oe[d], v = He[d], E = $t[d], p = Ct[d], w = Ae[d], g = Re[d];
|
|
187
|
+
for (let P = 0; P < 16; P++) {
|
|
188
|
+
const u = U(o + Et(d, i, c, l) + V[E[P]] + k, w[P]) + b | 0;
|
|
189
|
+
o = b, b = l, l = U(c, 10) | 0, c = i, i = u;
|
|
189
190
|
}
|
|
190
|
-
for (let
|
|
191
|
-
const
|
|
192
|
-
r =
|
|
191
|
+
for (let P = 0; P < 16; P++) {
|
|
192
|
+
const u = U(r + Et(f, a, h, m) + V[p[P]] + v, g[P]) + x | 0;
|
|
193
|
+
r = x, x = m, m = U(h, 10) | 0, h = a, a = u;
|
|
193
194
|
}
|
|
194
195
|
}
|
|
195
|
-
this.set(this.h1 + c + m | 0, this.h2 + l +
|
|
196
|
+
this.set(this.h1 + c + m | 0, this.h2 + l + x | 0, this.h3 + b + r | 0, this.h4 + o + a | 0, this.h0 + i + h | 0);
|
|
196
197
|
}
|
|
197
198
|
roundClean() {
|
|
198
199
|
F(V);
|
|
@@ -201,29 +202,29 @@ class $e extends Kt {
|
|
|
201
202
|
this.destroyed = !0, F(this.buffer), this.set(0, 0, 0, 0, 0);
|
|
202
203
|
}
|
|
203
204
|
}
|
|
204
|
-
const
|
|
205
|
-
for (let n = 0, t =
|
|
206
|
-
[e, s] = [s, (2 * e + 3 * s) % 5],
|
|
207
|
-
let o =
|
|
205
|
+
const Ce = /* @__PURE__ */ It(() => new $e()), Ue = BigInt(0), H = BigInt(1), Ne = BigInt(2), Ve = BigInt(7), Le = BigInt(256), We = BigInt(113), Nt = [], Vt = [], Lt = [];
|
|
206
|
+
for (let n = 0, t = H, e = 1, s = 0; n < 24; n++) {
|
|
207
|
+
[e, s] = [s, (2 * e + 3 * s) % 5], Nt.push(2 * (5 * s + e)), Vt.push((n + 1) * (n + 2) / 2 % 64);
|
|
208
|
+
let o = Ue;
|
|
208
209
|
for (let r = 0; r < 7; r++)
|
|
209
|
-
t = (t <<
|
|
210
|
-
|
|
210
|
+
t = (t << H ^ (t >> Ve) * We) % Le, t & Ne && (o ^= H << (H << BigInt(r)) - H);
|
|
211
|
+
Lt.push(o);
|
|
211
212
|
}
|
|
212
|
-
const
|
|
213
|
-
function
|
|
213
|
+
const Wt = ge(Lt, !0), Fe = Wt[0], Me = Wt[1], Tt = (n, t, e) => e > 32 ? me(n, t, e) : fe(n, t, e), _t = (n, t, e) => e > 32 ? we(n, t, e) : pe(n, t, e);
|
|
214
|
+
function Xe(n, t = 24) {
|
|
214
215
|
const e = new Uint32Array(10);
|
|
215
216
|
for (let s = 24 - t; s < 24; s++) {
|
|
216
217
|
for (let i = 0; i < 10; i++)
|
|
217
218
|
e[i] = n[i] ^ n[i + 10] ^ n[i + 20] ^ n[i + 30] ^ n[i + 40];
|
|
218
219
|
for (let i = 0; i < 10; i += 2) {
|
|
219
|
-
const a = (i + 8) % 10, c = (i + 2) % 10,
|
|
220
|
-
for (let
|
|
221
|
-
n[i +
|
|
220
|
+
const a = (i + 8) % 10, c = (i + 2) % 10, h = e[c], l = e[c + 1], m = Tt(h, l, 1) ^ e[a], b = _t(h, l, 1) ^ e[a + 1];
|
|
221
|
+
for (let x = 0; x < 50; x += 10)
|
|
222
|
+
n[i + x] ^= m, n[i + x + 1] ^= b;
|
|
222
223
|
}
|
|
223
224
|
let o = n[2], r = n[3];
|
|
224
225
|
for (let i = 0; i < 24; i++) {
|
|
225
|
-
const a =
|
|
226
|
-
o = n[l], r = n[l + 1], n[l] = c, n[l + 1] =
|
|
226
|
+
const a = Vt[i], c = Tt(o, r, a), h = _t(o, r, a), l = Nt[i];
|
|
227
|
+
o = n[l], r = n[l + 1], n[l] = c, n[l + 1] = h;
|
|
227
228
|
}
|
|
228
229
|
for (let i = 0; i < 50; i += 10) {
|
|
229
230
|
for (let a = 0; a < 10; a++)
|
|
@@ -231,33 +232,33 @@ function Me(n, t = 24) {
|
|
|
231
232
|
for (let a = 0; a < 10; a++)
|
|
232
233
|
n[i + a] ^= ~e[(a + 2) % 10] & e[(a + 4) % 10];
|
|
233
234
|
}
|
|
234
|
-
n[0] ^=
|
|
235
|
+
n[0] ^= Fe[s], n[1] ^= Me[s];
|
|
235
236
|
}
|
|
236
237
|
F(e);
|
|
237
238
|
}
|
|
238
239
|
class st {
|
|
239
240
|
// NOTE: we accept arguments in bytes instead of bits here.
|
|
240
241
|
constructor(t, e, s, o = !1, r = 24) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
242
|
+
y(this, "state");
|
|
243
|
+
y(this, "pos", 0);
|
|
244
|
+
y(this, "posOut", 0);
|
|
245
|
+
y(this, "finished", !1);
|
|
246
|
+
y(this, "state32");
|
|
247
|
+
y(this, "destroyed", !1);
|
|
248
|
+
y(this, "blockLen");
|
|
249
|
+
y(this, "suffix");
|
|
250
|
+
y(this, "outputLen");
|
|
251
|
+
y(this, "enableXOF", !1);
|
|
252
|
+
y(this, "rounds");
|
|
252
253
|
if (this.blockLen = t, this.suffix = e, this.outputLen = s, this.enableXOF = o, this.rounds = r, rt(s, "outputLen"), !(0 < t && t < 200))
|
|
253
254
|
throw new Error("only keccak-f1600 function is supported");
|
|
254
|
-
this.state = new Uint8Array(200), this.state32 =
|
|
255
|
+
this.state = new Uint8Array(200), this.state32 = zt(this.state);
|
|
255
256
|
}
|
|
256
257
|
clone() {
|
|
257
258
|
return this._cloneInto();
|
|
258
259
|
}
|
|
259
260
|
keccak() {
|
|
260
|
-
it(this.state32),
|
|
261
|
+
it(this.state32), Xe(this.state32, this.rounds), it(this.state32), this.posOut = 0, this.pos = 0;
|
|
261
262
|
}
|
|
262
263
|
update(t) {
|
|
263
264
|
at(this), ct(t);
|
|
@@ -296,7 +297,7 @@ class st {
|
|
|
296
297
|
return rt(t), this.xofInto(new Uint8Array(t));
|
|
297
298
|
}
|
|
298
299
|
digestInto(t) {
|
|
299
|
-
if (
|
|
300
|
+
if (Gt(t, this), this.finished)
|
|
300
301
|
throw new Error("digest() was already called");
|
|
301
302
|
return this.writeInto(t), this.destroy(), t;
|
|
302
303
|
}
|
|
@@ -311,21 +312,21 @@ class st {
|
|
|
311
312
|
return t || (t = new st(e, s, 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 = s, t.outputLen = o, t.enableXOF = i, t.destroyed = this.destroyed, t;
|
|
312
313
|
}
|
|
313
314
|
}
|
|
314
|
-
const
|
|
315
|
+
const Ke = (n, t, e, s = {}) => It(() => new st(t, n, e), s), De = /* @__PURE__ */ Ke(1, 136, 32), vt = 64, _ = 20, et = 4, Ft = 2, ze = 0, Ge = 1, L = [64, 64], je = (n) => Array.from(n).map((t) => t.toString(16).padStart(2, "0")).join("");
|
|
315
316
|
function ot(n) {
|
|
316
|
-
return
|
|
317
|
+
return Ce(nt(n));
|
|
317
318
|
}
|
|
318
|
-
function
|
|
319
|
+
function Mt(n) {
|
|
319
320
|
return (1 << n) - 1;
|
|
320
321
|
}
|
|
321
|
-
function
|
|
322
|
+
function qe(n) {
|
|
322
323
|
let t = 1;
|
|
323
324
|
for (; t * t < n + 1; ) t++;
|
|
324
325
|
return Math.max(t, 2);
|
|
325
326
|
}
|
|
326
|
-
function
|
|
327
|
-
const t = et, e = n *
|
|
328
|
-
return { d: t, n, checksum_radix:
|
|
327
|
+
function Ze(n) {
|
|
328
|
+
const t = et, e = n * Mt(t);
|
|
329
|
+
return { d: t, n, checksum_radix: qe(e) };
|
|
329
330
|
}
|
|
330
331
|
function j(n, t) {
|
|
331
332
|
const e = [];
|
|
@@ -344,45 +345,45 @@ function q(n, t) {
|
|
|
344
345
|
e = ot(e);
|
|
345
346
|
return e;
|
|
346
347
|
}
|
|
347
|
-
function
|
|
348
|
-
const e =
|
|
348
|
+
function Qe(n, t) {
|
|
349
|
+
const e = Mt(t.d), s = t.checksum_radix - 1, o = Math.floor(t.n * e / t.checksum_radix), r = [];
|
|
349
350
|
for (let l = 0; l < t.n; l++) {
|
|
350
|
-
const m = j(n, l +
|
|
351
|
-
r.push(Array.from(
|
|
351
|
+
const m = j(n, l + Ft), b = q(m, e);
|
|
352
|
+
r.push(Array.from(b));
|
|
352
353
|
}
|
|
353
354
|
const i = j(
|
|
354
355
|
n,
|
|
355
|
-
|
|
356
|
+
ze
|
|
356
357
|
), a = q(
|
|
357
358
|
i,
|
|
358
359
|
s
|
|
359
360
|
), c = j(
|
|
360
361
|
n,
|
|
361
|
-
|
|
362
|
-
),
|
|
362
|
+
Ge
|
|
363
|
+
), h = q(
|
|
363
364
|
c,
|
|
364
365
|
o
|
|
365
366
|
);
|
|
366
367
|
return {
|
|
367
368
|
config: t,
|
|
368
369
|
message_terminals: r,
|
|
369
|
-
checksum_major_terminal: Array.from(
|
|
370
|
+
checksum_major_terminal: Array.from(h),
|
|
370
371
|
checksum_minor_terminal: Array.from(a)
|
|
371
372
|
};
|
|
372
373
|
}
|
|
373
|
-
async function
|
|
374
|
+
async function Ye(n) {
|
|
374
375
|
try {
|
|
375
|
-
if (n.length !==
|
|
376
|
+
if (n.length !== vt)
|
|
376
377
|
throw new Error(
|
|
377
|
-
`WOTS seed must be exactly ${
|
|
378
|
+
`WOTS seed must be exactly ${vt} bytes, got ${n.length}`
|
|
378
379
|
);
|
|
379
380
|
const t = [];
|
|
380
381
|
for (let e = 0; e < L.length; e++) {
|
|
381
|
-
const s = L[e], o =
|
|
382
|
+
const s = L[e], o = Ze(s), r = new Uint8Array(n.length + 1);
|
|
382
383
|
r.set(n), r[n.length] = e;
|
|
383
384
|
const i = ot(r);
|
|
384
385
|
try {
|
|
385
|
-
const a =
|
|
386
|
+
const a = Qe(i, o);
|
|
386
387
|
if (a.config.d !== et)
|
|
387
388
|
throw new Error(
|
|
388
389
|
`Block ${e}: expected d=${et}, got d=${a.config.d}`
|
|
@@ -435,7 +436,7 @@ function Z(n, t, e) {
|
|
|
435
436
|
);
|
|
436
437
|
}
|
|
437
438
|
}
|
|
438
|
-
function
|
|
439
|
+
function Je(n) {
|
|
439
440
|
if (n.length === 0)
|
|
440
441
|
throw new Error("Public keys array must not be empty");
|
|
441
442
|
for (let r = 0; r < n.length; r++) {
|
|
@@ -446,7 +447,7 @@ function Ye(n) {
|
|
|
446
447
|
}
|
|
447
448
|
let t = 0;
|
|
448
449
|
for (const r of n)
|
|
449
|
-
t +=
|
|
450
|
+
t += Ft + r.message_terminals.length;
|
|
450
451
|
const e = new Uint8Array(t * _);
|
|
451
452
|
let s = 0;
|
|
452
453
|
for (const r of n) {
|
|
@@ -454,24 +455,24 @@ function Ye(n) {
|
|
|
454
455
|
for (const i of r.message_terminals)
|
|
455
456
|
e.set(i, s), s += _;
|
|
456
457
|
}
|
|
457
|
-
const o =
|
|
458
|
-
return `0x${
|
|
458
|
+
const o = De(e);
|
|
459
|
+
return `0x${je(o)}`;
|
|
459
460
|
}
|
|
460
|
-
async function
|
|
461
|
+
async function tn(n, t) {
|
|
461
462
|
const e = [], s = [], o = [], r = [];
|
|
462
463
|
try {
|
|
463
464
|
for (let i = 0; i < t; i++) {
|
|
464
|
-
const a = await
|
|
465
|
+
const a = await Zt(n, i);
|
|
465
466
|
try {
|
|
466
|
-
const
|
|
467
|
-
e.push(
|
|
467
|
+
const h = await Ye(a);
|
|
468
|
+
e.push(h), s.push(Je(h));
|
|
468
469
|
} finally {
|
|
469
470
|
a.fill(0);
|
|
470
471
|
}
|
|
471
|
-
const c = await
|
|
472
|
+
const c = await Qt(n, i);
|
|
472
473
|
try {
|
|
473
|
-
const
|
|
474
|
-
o.push(
|
|
474
|
+
const h = M(c);
|
|
475
|
+
o.push(h), r.push(re(I(h)).slice(2));
|
|
475
476
|
} finally {
|
|
476
477
|
c.fill(0);
|
|
477
478
|
}
|
|
@@ -481,33 +482,33 @@ async function Je(n, t) {
|
|
|
481
482
|
}
|
|
482
483
|
return { perVaultWotsKeys: e, wotsPkHashes: s, htlcSecretHexes: o, hashlocks: r };
|
|
483
484
|
}
|
|
484
|
-
const
|
|
485
|
-
function
|
|
485
|
+
const en = /^0x[0-9a-f]+$/i, nn = /^[0-9a-f]+$/i, sn = /^[A-Za-z0-9+/]+={0,2}$/;
|
|
486
|
+
function $(n) {
|
|
486
487
|
if (typeof n != "string" || n.length === 0)
|
|
487
488
|
throw new Error("BTC wallet returned empty public key");
|
|
488
|
-
return
|
|
489
|
+
return se(n).toLowerCase();
|
|
489
490
|
}
|
|
490
|
-
function
|
|
491
|
+
function on(n) {
|
|
491
492
|
if (typeof n != "string" || n.length === 0)
|
|
492
493
|
throw new Error("BTC wallet returned empty BIP-322 signature");
|
|
493
494
|
if (n.startsWith("0x") || n.startsWith("0X")) {
|
|
494
|
-
if (!
|
|
495
|
+
if (!en.test(n) || n.length < 4 || n.length % 2 !== 0)
|
|
495
496
|
throw new Error("BTC wallet returned malformed hex BIP-322 signature");
|
|
496
497
|
return n.toLowerCase();
|
|
497
498
|
}
|
|
498
|
-
if (
|
|
499
|
+
if (nn.test(n)) {
|
|
499
500
|
if (n.length % 2 !== 0)
|
|
500
501
|
throw new Error("BTC wallet returned malformed hex BIP-322 signature");
|
|
501
502
|
return `0x${n.toLowerCase()}`;
|
|
502
503
|
}
|
|
503
|
-
if (!
|
|
504
|
+
if (!sn.test(n) || n.length % 4 !== 0)
|
|
504
505
|
throw new Error("BTC wallet returned malformed base64 BIP-322 signature");
|
|
505
506
|
const t = Q.from(n, "base64");
|
|
506
507
|
if (t.length === 0 || t.toString("base64") !== n)
|
|
507
508
|
throw new Error("BTC wallet returned malformed base64 BIP-322 signature");
|
|
508
509
|
return `0x${t.toString("hex")}`;
|
|
509
510
|
}
|
|
510
|
-
async function
|
|
511
|
+
async function rn(n, t, e) {
|
|
511
512
|
if (typeof n.signPsbts == "function") {
|
|
512
513
|
const o = await n.signPsbts(t, e);
|
|
513
514
|
if (o.length !== t.length)
|
|
@@ -523,25 +524,25 @@ async function on(n, t, e) {
|
|
|
523
524
|
}
|
|
524
525
|
return s;
|
|
525
526
|
}
|
|
526
|
-
const
|
|
527
|
-
function
|
|
527
|
+
const an = 0, W = "00".repeat(32);
|
|
528
|
+
function cn(n, t, e, s) {
|
|
528
529
|
const o = e == null ? void 0 : e[`${n}:${t}`];
|
|
529
530
|
return o ? Promise.resolve({
|
|
530
531
|
txid: n,
|
|
531
532
|
vout: t,
|
|
532
533
|
value: o.value,
|
|
533
534
|
scriptPubKey: o.scriptPubKey
|
|
534
|
-
}) :
|
|
535
|
+
}) : he(n, t, s);
|
|
535
536
|
}
|
|
536
|
-
const
|
|
537
|
-
class
|
|
537
|
+
const Bt = 12e4;
|
|
538
|
+
class vn {
|
|
538
539
|
/**
|
|
539
540
|
* Creates a new PeginManager instance.
|
|
540
541
|
*
|
|
541
542
|
* @param config - Manager configuration including wallets and contract addresses
|
|
542
543
|
*/
|
|
543
544
|
constructor(t) {
|
|
544
|
-
|
|
545
|
+
y(this, "config");
|
|
545
546
|
this.config = t;
|
|
546
547
|
}
|
|
547
548
|
/**
|
|
@@ -556,8 +557,8 @@ class Tn {
|
|
|
556
557
|
async preparePegin(t) {
|
|
557
558
|
if (t.amounts.length === 0)
|
|
558
559
|
throw new Error("amounts must contain at least one entry");
|
|
559
|
-
const e = await this.config.btcWallet.getPublicKeyHex(), s =
|
|
560
|
-
if (!
|
|
560
|
+
const e = await this.config.btcWallet.getPublicKeyHex(), s = $(e);
|
|
561
|
+
if (!pt(
|
|
561
562
|
t.changeAddress,
|
|
562
563
|
e,
|
|
563
564
|
this.config.btcNetwork
|
|
@@ -566,39 +567,39 @@ class Tn {
|
|
|
566
567
|
`Pre-PegIn changeAddress "${t.changeAddress}" is not derived from the connected wallet's public key. Refusing to build a tx that would send change to an address the signing key doesn't control.`
|
|
567
568
|
);
|
|
568
569
|
const o = await this.prepareSizing(s, t), r = o.selectedUTXOs.map(
|
|
569
|
-
(
|
|
570
|
-
txid: Y(
|
|
571
|
-
vout:
|
|
570
|
+
(f) => ({
|
|
571
|
+
txid: Y(f.txid),
|
|
572
|
+
vout: f.vout
|
|
572
573
|
})
|
|
573
|
-
), i = await
|
|
574
|
+
), i = await Be(this.config.btcWallet, {
|
|
574
575
|
depositorBtcPubkey: Y(s),
|
|
575
576
|
fundingOutpoints: r
|
|
576
577
|
});
|
|
577
578
|
let a, c;
|
|
578
579
|
try {
|
|
579
|
-
const
|
|
580
|
+
const f = await Yt(i);
|
|
580
581
|
try {
|
|
581
|
-
a = M(
|
|
582
|
+
a = M(f), c = M(nt(f));
|
|
582
583
|
} finally {
|
|
583
|
-
|
|
584
|
+
f.fill(0);
|
|
584
585
|
}
|
|
585
|
-
} catch (
|
|
586
|
-
throw i.fill(0),
|
|
586
|
+
} catch (f) {
|
|
587
|
+
throw i.fill(0), f;
|
|
587
588
|
}
|
|
588
|
-
const
|
|
589
|
+
const h = await tn(i, t.amounts.length), { perVaultWotsKeys: l, wotsPkHashes: m, htlcSecretHexes: b, hashlocks: x } = h, d = await this.preparePeginCommit({
|
|
589
590
|
depositorBtcPubkeyRaw: e,
|
|
590
591
|
depositorBtcPubkey: s,
|
|
591
|
-
hashlocks:
|
|
592
|
+
hashlocks: x,
|
|
592
593
|
authAnchorHash: c,
|
|
593
594
|
sizing: o,
|
|
594
595
|
params: t
|
|
595
596
|
});
|
|
596
|
-
for (let
|
|
597
|
-
if (d.perVault[
|
|
597
|
+
for (let f = 0; f < d.perVault.length; f++)
|
|
598
|
+
if (d.perVault[f].htlcVout !== f)
|
|
598
599
|
throw new Error(
|
|
599
|
-
`Internal invariant violation: htlcVout/index mismatch at vault ${
|
|
600
|
+
`Internal invariant violation: htlcVout/index mismatch at vault ${f} (expected ${f}, got ${d.perVault[f].htlcVout})`
|
|
600
601
|
);
|
|
601
|
-
return
|
|
602
|
+
return xe(
|
|
602
603
|
d.fundedPrePeginTxHex,
|
|
603
604
|
t.amounts.length,
|
|
604
605
|
c
|
|
@@ -613,7 +614,7 @@ class Tn {
|
|
|
613
614
|
derivedSecrets: {
|
|
614
615
|
perVaultWotsKeys: l,
|
|
615
616
|
wotsPkHashes: m,
|
|
616
|
-
htlcSecretHexes:
|
|
617
|
+
htlcSecretHexes: b,
|
|
617
618
|
authAnchorHex: a
|
|
618
619
|
}
|
|
619
620
|
};
|
|
@@ -651,11 +652,11 @@ class Tn {
|
|
|
651
652
|
councilSize: e.councilSize,
|
|
652
653
|
network: this.config.btcNetwork,
|
|
653
654
|
authAnchorHash: W
|
|
654
|
-
}), i =
|
|
655
|
+
}), i = ie(
|
|
655
656
|
[...e.availableUTXOs],
|
|
656
657
|
r.totalOutputValue,
|
|
657
658
|
e.mempoolFeeRate,
|
|
658
|
-
|
|
659
|
+
ce(
|
|
659
660
|
r.htlcValues.length,
|
|
660
661
|
W
|
|
661
662
|
)
|
|
@@ -676,80 +677,84 @@ class Tn {
|
|
|
676
677
|
sizing: i,
|
|
677
678
|
params: a
|
|
678
679
|
} = t, c = W.toLowerCase();
|
|
679
|
-
for (let
|
|
680
|
-
if (o[
|
|
680
|
+
for (let u = 0; u < o.length; u++)
|
|
681
|
+
if (o[u].toLowerCase() === c)
|
|
681
682
|
throw new Error(
|
|
682
|
-
`preparePeginCommit refusing to build with sizing-pass placeholder hashlock at vault ${
|
|
683
|
+
`preparePeginCommit refusing to build with sizing-pass placeholder hashlock at vault ${u} — internal substitution bug`
|
|
683
684
|
);
|
|
684
685
|
if (r.toLowerCase() === c)
|
|
685
686
|
throw new Error(
|
|
686
687
|
"preparePeginCommit refusing to build with sizing-pass placeholder auth-anchor hash — internal substitution bug"
|
|
687
688
|
);
|
|
688
|
-
const
|
|
689
|
+
const h = T(a.vaultProviderBtcPubkey), l = a.vaultKeeperBtcPubkeys.map(T), m = a.universalChallengerBtcPubkeys.map(T), b = l.length, x = {
|
|
689
690
|
depositorPubkey: s,
|
|
690
|
-
vaultProviderPubkey:
|
|
691
|
+
vaultProviderPubkey: h,
|
|
691
692
|
vaultKeeperPubkeys: l,
|
|
692
693
|
universalChallengerPubkeys: m,
|
|
693
694
|
hashlocks: o,
|
|
694
695
|
timelockRefund: a.timelockRefund,
|
|
695
696
|
pegInAmounts: a.amounts,
|
|
696
697
|
feeRate: a.protocolFeeRate,
|
|
697
|
-
numLocalChallengers:
|
|
698
|
+
numLocalChallengers: b,
|
|
698
699
|
councilQuorum: a.councilQuorum,
|
|
699
700
|
councilSize: a.councilSize,
|
|
700
701
|
network: this.config.btcNetwork,
|
|
701
702
|
authAnchorHash: r
|
|
702
|
-
}, d = await gt(
|
|
703
|
+
}, d = await gt(x), f = mt(this.config.btcNetwork), k = le({
|
|
703
704
|
unfundedTxHex: d.psbtHex,
|
|
704
705
|
selectedUTXOs: i.selectedUTXOs,
|
|
705
706
|
changeAddress: a.changeAddress,
|
|
706
707
|
changeAmount: i.changeAmount,
|
|
707
|
-
network:
|
|
708
|
-
}),
|
|
709
|
-
for (let
|
|
710
|
-
const S = await
|
|
711
|
-
prePeginParams:
|
|
708
|
+
network: f
|
|
709
|
+
}), v = T(z(k)), E = [], p = [], w = [];
|
|
710
|
+
for (let u = 0; u < o.length; u++) {
|
|
711
|
+
const S = await Jt({
|
|
712
|
+
prePeginParams: x,
|
|
712
713
|
timelockPegin: a.timelockPegin,
|
|
713
|
-
fundedPrePeginTxHex:
|
|
714
|
-
htlcVout:
|
|
715
|
-
}),
|
|
714
|
+
fundedPrePeginTxHex: k,
|
|
715
|
+
htlcVout: u
|
|
716
|
+
}), B = await te({
|
|
716
717
|
peginTxHex: S.txHex,
|
|
717
|
-
fundedPrePeginTxHex:
|
|
718
|
+
fundedPrePeginTxHex: k,
|
|
718
719
|
depositorPubkey: s,
|
|
719
|
-
vaultProviderPubkey:
|
|
720
|
+
vaultProviderPubkey: h,
|
|
720
721
|
vaultKeeperPubkeys: l,
|
|
721
722
|
universalChallengerPubkeys: m,
|
|
722
|
-
hashlock: o[
|
|
723
|
+
hashlock: o[u],
|
|
723
724
|
timelockRefund: a.timelockRefund,
|
|
724
725
|
network: this.config.btcNetwork
|
|
725
726
|
});
|
|
726
|
-
|
|
727
|
-
|
|
727
|
+
E.push(S), p.push(B.psbtHex), w.push(
|
|
728
|
+
oe(e, 1)
|
|
728
729
|
);
|
|
729
730
|
}
|
|
730
|
-
const
|
|
731
|
+
const g = await rn(
|
|
731
732
|
this.config.btcWallet,
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
),
|
|
735
|
-
for (let
|
|
736
|
-
|
|
737
|
-
p[
|
|
733
|
+
p,
|
|
734
|
+
w
|
|
735
|
+
), P = [];
|
|
736
|
+
for (let u = 0; u < g.length; u++) {
|
|
737
|
+
ft({
|
|
738
|
+
requestedPsbtHex: p[u],
|
|
739
|
+
returnedPsbtHex: g[u]
|
|
740
|
+
});
|
|
741
|
+
const S = ee(
|
|
742
|
+
g[u],
|
|
738
743
|
s
|
|
739
|
-
),
|
|
740
|
-
|
|
741
|
-
htlcVout:
|
|
742
|
-
htlcValue: d.htlcValues[
|
|
743
|
-
peginTxHex:
|
|
744
|
-
peginTxid:
|
|
744
|
+
), B = ne(g[u]);
|
|
745
|
+
P.push({
|
|
746
|
+
htlcVout: u,
|
|
747
|
+
htlcValue: d.htlcValues[u],
|
|
748
|
+
peginTxHex: B,
|
|
749
|
+
peginTxid: E[u].txid,
|
|
745
750
|
peginInputSignature: S,
|
|
746
|
-
vaultScriptPubKey:
|
|
751
|
+
vaultScriptPubKey: E[u].vaultScriptPubKey
|
|
747
752
|
});
|
|
748
753
|
}
|
|
749
754
|
return {
|
|
750
|
-
fundedPrePeginTxHex:
|
|
751
|
-
prePeginTxid:
|
|
752
|
-
perVault:
|
|
755
|
+
fundedPrePeginTxHex: k,
|
|
756
|
+
prePeginTxid: v,
|
|
757
|
+
perVault: P
|
|
753
758
|
};
|
|
754
759
|
}
|
|
755
760
|
/**
|
|
@@ -768,68 +773,73 @@ class Tn {
|
|
|
768
773
|
* @throws Error if signing or broadcasting fails
|
|
769
774
|
*/
|
|
770
775
|
async signAndBroadcast(t) {
|
|
771
|
-
const { fundedPrePeginTxHex: e, depositorBtcPubkey: s } = t, o = e.startsWith("0x") ? e.slice(2) : e, r =
|
|
776
|
+
const { fundedPrePeginTxHex: e, depositorBtcPubkey: s } = t, o = e.startsWith("0x") ? e.slice(2) : e, r = jt.fromHex(o);
|
|
772
777
|
if (r.ins.length === 0)
|
|
773
778
|
throw new Error("Transaction has no inputs");
|
|
774
779
|
const i = new lt();
|
|
775
780
|
i.setVersion(r.version), i.setLocktime(r.locktime);
|
|
776
781
|
const a = Q.from(
|
|
777
|
-
|
|
782
|
+
$(s),
|
|
778
783
|
"hex"
|
|
779
|
-
), c = this.config.mempoolApiUrl,
|
|
780
|
-
const
|
|
781
|
-
return
|
|
782
|
-
(
|
|
784
|
+
), c = this.config.mempoolApiUrl, h = r.ins.map((p) => {
|
|
785
|
+
const w = Q.from(p.hash).reverse().toString("hex"), g = p.index;
|
|
786
|
+
return cn(w, g, t.localPrevouts, c).then(
|
|
787
|
+
(P) => ({ input: p, utxoData: P, txid: w, vout: g })
|
|
783
788
|
);
|
|
784
|
-
}), l = await Promise.all(
|
|
785
|
-
(
|
|
789
|
+
}), l = await Promise.all(h), m = l.reduce(
|
|
790
|
+
(p, w) => p + BigInt(w.utxoData.value),
|
|
786
791
|
0n
|
|
787
|
-
),
|
|
788
|
-
(
|
|
792
|
+
), b = r.outs.reduce(
|
|
793
|
+
(p, w) => p + BigInt(w.value),
|
|
789
794
|
0n
|
|
790
795
|
);
|
|
791
|
-
if (m <
|
|
796
|
+
if (m < b)
|
|
792
797
|
throw new Error(
|
|
793
|
-
`UTXO value mismatch: total input value (${m} sat) is less than total output value (${
|
|
798
|
+
`UTXO value mismatch: total input value (${m} sat) is less than total output value (${b} sat). This may indicate the mempool API returned manipulated UTXO data.`
|
|
794
799
|
);
|
|
795
|
-
const
|
|
796
|
-
if (
|
|
800
|
+
const x = m - b;
|
|
801
|
+
if (x > wt)
|
|
797
802
|
throw new Error(
|
|
798
|
-
`Implied transaction fee (${
|
|
803
|
+
`Implied transaction fee (${x} sat) exceeds maximum reasonable fee (${wt} sat). This may indicate manipulated UTXO data.`
|
|
799
804
|
);
|
|
800
|
-
for (const { input:
|
|
801
|
-
const
|
|
805
|
+
for (const { input: p, utxoData: w, txid: g, vout: P } of l) {
|
|
806
|
+
const u = ae(
|
|
802
807
|
{
|
|
803
|
-
value:
|
|
804
|
-
scriptPubKey:
|
|
808
|
+
value: w.value,
|
|
809
|
+
scriptPubKey: w.scriptPubKey
|
|
805
810
|
},
|
|
806
811
|
a
|
|
807
812
|
);
|
|
808
813
|
i.addInput({
|
|
809
|
-
hash:
|
|
810
|
-
index:
|
|
811
|
-
sequence:
|
|
812
|
-
...
|
|
814
|
+
hash: p.hash,
|
|
815
|
+
index: p.index,
|
|
816
|
+
sequence: p.sequence,
|
|
817
|
+
...u
|
|
813
818
|
});
|
|
814
819
|
}
|
|
815
|
-
for (const
|
|
820
|
+
for (const p of r.outs)
|
|
816
821
|
i.addOutput({
|
|
817
|
-
script:
|
|
818
|
-
value:
|
|
822
|
+
script: p.script,
|
|
823
|
+
value: p.value
|
|
819
824
|
});
|
|
820
|
-
const d =
|
|
825
|
+
const d = i.toHex(), f = await this.config.btcWallet.signPsbt(d);
|
|
826
|
+
ft({
|
|
827
|
+
requestedPsbtHex: d,
|
|
828
|
+
returnedPsbtHex: f
|
|
829
|
+
});
|
|
830
|
+
const k = lt.fromHex(f);
|
|
821
831
|
try {
|
|
822
|
-
|
|
823
|
-
} catch (
|
|
824
|
-
if (!
|
|
825
|
-
(
|
|
832
|
+
k.finalizeAllInputs();
|
|
833
|
+
} catch (p) {
|
|
834
|
+
if (!k.data.inputs.every(
|
|
835
|
+
(g) => g.finalScriptWitness || g.finalScriptSig
|
|
826
836
|
))
|
|
827
837
|
throw new Error(
|
|
828
|
-
`PSBT finalization failed and wallet did not auto-finalize: ${
|
|
838
|
+
`PSBT finalization failed and wallet did not auto-finalize: ${p}`
|
|
829
839
|
);
|
|
830
840
|
}
|
|
831
|
-
const
|
|
832
|
-
return await
|
|
841
|
+
const v = k.extractTransaction().toHex();
|
|
842
|
+
return await ue(v, c);
|
|
833
843
|
}
|
|
834
844
|
/**
|
|
835
845
|
* Registers a peg-in on Ethereum by calling the BTCVaultRegistry contract.
|
|
@@ -864,30 +874,30 @@ class Tn {
|
|
|
864
874
|
htlcVout: i,
|
|
865
875
|
depositorPayoutBtcAddress: a,
|
|
866
876
|
depositorWotsPkHash: c,
|
|
867
|
-
popSignature:
|
|
877
|
+
popSignature: h
|
|
868
878
|
} = t;
|
|
869
879
|
if (!this.config.ethWallet.account)
|
|
870
880
|
throw new Error("Ethereum wallet account not found");
|
|
871
881
|
const l = this.config.ethWallet.account.address;
|
|
872
|
-
if (!ut(
|
|
882
|
+
if (!ut(h.depositorEthAddress, l))
|
|
873
883
|
throw new Error(
|
|
874
|
-
`Proof of possession was signed for ${
|
|
884
|
+
`Proof of possession was signed for ${h.depositorEthAddress} but the Ethereum wallet is currently connected to ${l}. Reconnect the original account or call signProofOfPossession() again.`
|
|
875
885
|
);
|
|
876
|
-
const m = await this.assertPopMatchesBtcWallet(
|
|
886
|
+
const m = await this.assertPopMatchesBtcWallet(h), b = h.btcPopSignature, x = I(h.depositorBtcPubkey), d = I(e), f = I(s), k = a ?? await this.config.btcWallet.getAddress(), v = this.resolvePayoutScriptPubKey(
|
|
877
887
|
m,
|
|
878
|
-
|
|
879
|
-
),
|
|
880
|
-
T(
|
|
888
|
+
k
|
|
889
|
+
), E = z(f), p = await dt(
|
|
890
|
+
T(E),
|
|
881
891
|
T(l)
|
|
882
|
-
),
|
|
883
|
-
if (await this.checkVaultExists(
|
|
892
|
+
), w = I(p);
|
|
893
|
+
if (await this.checkVaultExists(w))
|
|
884
894
|
throw new Error(
|
|
885
|
-
`Vault already exists (ID: ${
|
|
895
|
+
`Vault already exists (ID: ${w}, peginTxHash: ${E}). 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.`
|
|
886
896
|
);
|
|
887
|
-
const
|
|
888
|
-
let
|
|
897
|
+
const P = this.config.publicClient;
|
|
898
|
+
let u;
|
|
889
899
|
try {
|
|
890
|
-
|
|
900
|
+
u = await P.readContract({
|
|
891
901
|
address: this.config.vaultContracts.btcVaultRegistry,
|
|
892
902
|
abi: O,
|
|
893
903
|
functionName: "getPegInFee",
|
|
@@ -904,23 +914,23 @@ class Tn {
|
|
|
904
914
|
functionName: "submitPeginRequest",
|
|
905
915
|
args: [
|
|
906
916
|
l,
|
|
907
|
-
|
|
908
|
-
|
|
917
|
+
x,
|
|
918
|
+
b,
|
|
909
919
|
d,
|
|
910
|
-
|
|
920
|
+
f,
|
|
911
921
|
o,
|
|
912
922
|
r,
|
|
913
923
|
i,
|
|
914
|
-
|
|
924
|
+
v,
|
|
915
925
|
c
|
|
916
926
|
]
|
|
917
927
|
});
|
|
918
|
-
let
|
|
928
|
+
let B;
|
|
919
929
|
try {
|
|
920
|
-
|
|
930
|
+
B = await P.estimateGas({
|
|
921
931
|
to: this.config.vaultContracts.btcVaultRegistry,
|
|
922
932
|
data: S,
|
|
923
|
-
value:
|
|
933
|
+
value: u,
|
|
924
934
|
account: this.config.ethWallet.account.address
|
|
925
935
|
});
|
|
926
936
|
} catch (R) {
|
|
@@ -931,19 +941,19 @@ class Tn {
|
|
|
931
941
|
K = await this.config.ethWallet.sendTransaction({
|
|
932
942
|
to: this.config.vaultContracts.btcVaultRegistry,
|
|
933
943
|
data: S,
|
|
934
|
-
value:
|
|
944
|
+
value: u,
|
|
935
945
|
account: this.config.ethWallet.account,
|
|
936
946
|
chain: this.config.ethChain,
|
|
937
|
-
gas:
|
|
947
|
+
gas: B
|
|
938
948
|
});
|
|
939
949
|
} catch (R) {
|
|
940
950
|
A(R);
|
|
941
951
|
}
|
|
942
|
-
const D = await
|
|
943
|
-
publicClient:
|
|
952
|
+
const D = await Pt({
|
|
953
|
+
publicClient: P,
|
|
944
954
|
walletAddress: this.config.ethWallet.account.address,
|
|
945
955
|
hash: K,
|
|
946
|
-
timeout:
|
|
956
|
+
timeout: Bt
|
|
947
957
|
});
|
|
948
958
|
return D.status === "reverted" && A(
|
|
949
959
|
new Error(
|
|
@@ -951,8 +961,8 @@ class Tn {
|
|
|
951
961
|
)
|
|
952
962
|
), {
|
|
953
963
|
ethTxHash: D.transactionHash,
|
|
954
|
-
vaultId:
|
|
955
|
-
peginTxHash:
|
|
964
|
+
vaultId: w,
|
|
965
|
+
peginTxHash: E
|
|
956
966
|
};
|
|
957
967
|
}
|
|
958
968
|
/**
|
|
@@ -976,95 +986,95 @@ class Tn {
|
|
|
976
986
|
throw new Error(
|
|
977
987
|
`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.`
|
|
978
988
|
);
|
|
979
|
-
const a = await this.assertPopMatchesBtcWallet(r), c = r.btcPopSignature,
|
|
980
|
-
(
|
|
989
|
+
const a = await this.assertPopMatchesBtcWallet(r), c = r.btcPopSignature, h = o.map(
|
|
990
|
+
(g) => this.resolvePayoutScriptPubKey(
|
|
981
991
|
a,
|
|
982
|
-
|
|
992
|
+
g.depositorPayoutBtcAddress
|
|
983
993
|
)
|
|
984
994
|
), l = [];
|
|
985
|
-
for (const
|
|
986
|
-
const
|
|
987
|
-
|
|
988
|
-
),
|
|
989
|
-
T(
|
|
995
|
+
for (const g of o) {
|
|
996
|
+
const P = I(
|
|
997
|
+
g.depositorSignedPeginTx
|
|
998
|
+
), u = z(P), S = await dt(
|
|
999
|
+
T(u),
|
|
990
1000
|
T(i)
|
|
991
|
-
),
|
|
992
|
-
if (await this.checkVaultExists(
|
|
1001
|
+
), B = I(S);
|
|
1002
|
+
if (await this.checkVaultExists(B))
|
|
993
1003
|
throw new Error(
|
|
994
|
-
`Vault already exists (ID: ${
|
|
1004
|
+
`Vault already exists (ID: ${B}, peginTxHash: ${u}). To create a new vault, use different UTXOs or a different amount.`
|
|
995
1005
|
);
|
|
996
|
-
l.push({ vaultId:
|
|
1006
|
+
l.push({ vaultId: B, peginTxHash: u });
|
|
997
1007
|
}
|
|
998
1008
|
const m = this.config.publicClient;
|
|
999
|
-
let
|
|
1009
|
+
let b;
|
|
1000
1010
|
try {
|
|
1001
|
-
|
|
1011
|
+
b = await m.readContract({
|
|
1002
1012
|
address: this.config.vaultContracts.btcVaultRegistry,
|
|
1003
1013
|
abi: O,
|
|
1004
1014
|
functionName: "getPegInFee",
|
|
1005
1015
|
args: [e]
|
|
1006
1016
|
});
|
|
1007
|
-
} catch (
|
|
1017
|
+
} catch (g) {
|
|
1008
1018
|
throw new Error(
|
|
1009
1019
|
"Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct.",
|
|
1010
|
-
{ cause:
|
|
1020
|
+
{ cause: g }
|
|
1011
1021
|
);
|
|
1012
1022
|
}
|
|
1013
|
-
const
|
|
1023
|
+
const x = b * BigInt(o.length), d = I(
|
|
1014
1024
|
r.depositorBtcPubkey
|
|
1015
|
-
),
|
|
1025
|
+
), f = I(s), k = o.map((g, P) => ({
|
|
1016
1026
|
depositorBtcPubKey: d,
|
|
1017
1027
|
btcPopSignature: c,
|
|
1018
|
-
unsignedPrePeginTx:
|
|
1019
|
-
depositorSignedPeginTx:
|
|
1020
|
-
|
|
1028
|
+
unsignedPrePeginTx: f,
|
|
1029
|
+
depositorSignedPeginTx: I(
|
|
1030
|
+
g.depositorSignedPeginTx
|
|
1021
1031
|
),
|
|
1022
|
-
hashlock:
|
|
1023
|
-
htlcVout:
|
|
1024
|
-
referralCode:
|
|
1025
|
-
depositorPayoutBtcAddress:
|
|
1026
|
-
depositorWotsPkHash:
|
|
1027
|
-
})),
|
|
1032
|
+
hashlock: g.hashlock,
|
|
1033
|
+
htlcVout: g.htlcVout,
|
|
1034
|
+
referralCode: an,
|
|
1035
|
+
depositorPayoutBtcAddress: h[P],
|
|
1036
|
+
depositorWotsPkHash: g.depositorWotsPkHash
|
|
1037
|
+
})), v = ht({
|
|
1028
1038
|
abi: O,
|
|
1029
1039
|
functionName: "submitPeginRequestBatch",
|
|
1030
|
-
args: [i, e,
|
|
1040
|
+
args: [i, e, k]
|
|
1031
1041
|
});
|
|
1032
|
-
let
|
|
1042
|
+
let E;
|
|
1033
1043
|
try {
|
|
1034
|
-
|
|
1044
|
+
E = await m.estimateGas({
|
|
1035
1045
|
to: this.config.vaultContracts.btcVaultRegistry,
|
|
1036
|
-
data:
|
|
1037
|
-
value:
|
|
1046
|
+
data: v,
|
|
1047
|
+
value: x,
|
|
1038
1048
|
account: this.config.ethWallet.account.address
|
|
1039
1049
|
});
|
|
1040
|
-
} catch (
|
|
1041
|
-
A(
|
|
1050
|
+
} catch (g) {
|
|
1051
|
+
A(g);
|
|
1042
1052
|
}
|
|
1043
|
-
let
|
|
1053
|
+
let p;
|
|
1044
1054
|
try {
|
|
1045
|
-
|
|
1055
|
+
p = await this.config.ethWallet.sendTransaction({
|
|
1046
1056
|
to: this.config.vaultContracts.btcVaultRegistry,
|
|
1047
|
-
data:
|
|
1048
|
-
value:
|
|
1057
|
+
data: v,
|
|
1058
|
+
value: x,
|
|
1049
1059
|
account: this.config.ethWallet.account,
|
|
1050
1060
|
chain: this.config.ethChain,
|
|
1051
|
-
gas:
|
|
1061
|
+
gas: E
|
|
1052
1062
|
});
|
|
1053
|
-
} catch (
|
|
1054
|
-
A(
|
|
1063
|
+
} catch (g) {
|
|
1064
|
+
A(g);
|
|
1055
1065
|
}
|
|
1056
|
-
const
|
|
1066
|
+
const w = await Pt({
|
|
1057
1067
|
publicClient: m,
|
|
1058
1068
|
walletAddress: this.config.ethWallet.account.address,
|
|
1059
|
-
hash:
|
|
1060
|
-
timeout:
|
|
1069
|
+
hash: p,
|
|
1070
|
+
timeout: Bt
|
|
1061
1071
|
});
|
|
1062
|
-
return
|
|
1072
|
+
return w.status === "reverted" && A(
|
|
1063
1073
|
new Error(
|
|
1064
|
-
`Batch transaction reverted. Hash: ${
|
|
1074
|
+
`Batch transaction reverted. Hash: ${w.transactionHash}. Check the transaction on block explorer for details.`
|
|
1065
1075
|
)
|
|
1066
1076
|
), {
|
|
1067
|
-
ethTxHash:
|
|
1077
|
+
ethTxHash: w.transactionHash,
|
|
1068
1078
|
vaults: l
|
|
1069
1079
|
};
|
|
1070
1080
|
}
|
|
@@ -1087,7 +1097,7 @@ class Tn {
|
|
|
1087
1097
|
abi: O,
|
|
1088
1098
|
functionName: "getBtcVaultBasicInfo",
|
|
1089
1099
|
args: [t]
|
|
1090
|
-
})).depositor !==
|
|
1100
|
+
})).depositor !== qt;
|
|
1091
1101
|
}
|
|
1092
1102
|
/**
|
|
1093
1103
|
* Resolve the BTC scriptPubKey to register as the depositor's payout sink.
|
|
@@ -1106,7 +1116,7 @@ class Tn {
|
|
|
1106
1116
|
* on-chain payout-script registration.
|
|
1107
1117
|
*/
|
|
1108
1118
|
resolvePayoutScriptPubKey(t, e) {
|
|
1109
|
-
if (!
|
|
1119
|
+
if (!pt(
|
|
1110
1120
|
e,
|
|
1111
1121
|
t,
|
|
1112
1122
|
this.config.btcNetwork
|
|
@@ -1114,9 +1124,9 @@ class Tn {
|
|
|
1114
1124
|
throw new Error(
|
|
1115
1125
|
`BTC payout address "${e}" is not derived from the connected wallet's public key. The payout sink must be controlled by the same key that signs the pegin; refusing to register a mismatched address.`
|
|
1116
1126
|
);
|
|
1117
|
-
const s =
|
|
1127
|
+
const s = mt(this.config.btcNetwork);
|
|
1118
1128
|
try {
|
|
1119
|
-
return `0x${
|
|
1129
|
+
return `0x${St.address.toOutputScript(e, s).toString("hex")}`;
|
|
1120
1130
|
} catch {
|
|
1121
1131
|
throw new Error(
|
|
1122
1132
|
`Invalid BTC payout address: "${e}". Please provide a valid Bitcoin address for the ${this.config.btcNetwork} network.`
|
|
@@ -1132,14 +1142,14 @@ class Tn {
|
|
|
1132
1142
|
async signProofOfPossession() {
|
|
1133
1143
|
if (!this.config.ethWallet.account)
|
|
1134
1144
|
throw new Error("Ethereum wallet account not found");
|
|
1135
|
-
const t = this.config.ethWallet.account.address, e =
|
|
1145
|
+
const t = this.config.ethWallet.account.address, e = $(
|
|
1136
1146
|
await this.config.btcWallet.getPublicKeyHex()
|
|
1137
1147
|
), s = this.config.vaultContracts.btcVaultRegistry, o = `${t.toLowerCase()}:${this.config.ethChain.id}:pegin:${s.toLowerCase()}`, r = await this.config.btcWallet.signMessage(
|
|
1138
1148
|
o,
|
|
1139
1149
|
"bip322-simple"
|
|
1140
1150
|
);
|
|
1141
1151
|
return {
|
|
1142
|
-
btcPopSignature:
|
|
1152
|
+
btcPopSignature: on(r),
|
|
1143
1153
|
depositorEthAddress: t,
|
|
1144
1154
|
depositorBtcPubkey: e
|
|
1145
1155
|
};
|
|
@@ -1153,7 +1163,7 @@ class Tn {
|
|
|
1153
1163
|
* substitute the opposite-parity P2WPKH address.
|
|
1154
1164
|
*/
|
|
1155
1165
|
async assertPopMatchesBtcWallet(t) {
|
|
1156
|
-
const e = await this.config.btcWallet.getPublicKeyHex(), s =
|
|
1166
|
+
const e = await this.config.btcWallet.getPublicKeyHex(), s = $(e), o = $(t.depositorBtcPubkey);
|
|
1157
1167
|
if (s !== o)
|
|
1158
1168
|
throw new Error(
|
|
1159
1169
|
`Proof of possession was signed with BTC pubkey ${o} but the BTC wallet is currently connected to ${s}. Reconnect the original wallet or call signProofOfPossession() again.`
|
|
@@ -1178,12 +1188,12 @@ class Tn {
|
|
|
1178
1188
|
}
|
|
1179
1189
|
}
|
|
1180
1190
|
export {
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1191
|
+
vn as P,
|
|
1192
|
+
_e as V,
|
|
1193
|
+
Te as a,
|
|
1194
|
+
Ee as b,
|
|
1195
|
+
Je as c,
|
|
1196
|
+
Ye as d,
|
|
1197
|
+
Be as e
|
|
1188
1198
|
};
|
|
1189
|
-
//# sourceMappingURL=PeginManager-
|
|
1199
|
+
//# sourceMappingURL=PeginManager-B1Mh8dJ3.js.map
|