@babylonlabs-io/ts-sdk 0.55.2 → 0.56.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 +0 -1
- package/dist/{PayoutManager-BJmPnyRP.js → PayoutManager-BVgmuKuK.js} +117 -105
- package/dist/PayoutManager-BVgmuKuK.js.map +1 -0
- package/dist/PayoutManager-Cu4_l-_Y.cjs +2 -0
- package/dist/PayoutManager-Cu4_l-_Y.cjs.map +1 -0
- package/dist/PeginManager-BkWdZI7P.cjs +2 -0
- package/dist/PeginManager-BkWdZI7P.cjs.map +1 -0
- package/dist/{PeginManager-CW03-M3T.js → PeginManager-aYBkb-Qx.js} +520 -460
- package/dist/PeginManager-aYBkb-Qx.js.map +1 -0
- package/dist/{buildAndBroadcastRefund-BiSO4zg2.js → buildAndBroadcastRefund-BtXUqBkb.js} +444 -430
- package/dist/buildAndBroadcastRefund-BtXUqBkb.js.map +1 -0
- package/dist/buildAndBroadcastRefund-Cq01wyZX.cjs +2 -0
- package/dist/buildAndBroadcastRefund-Cq01wyZX.cjs.map +1 -0
- package/dist/challengeAssert-BVQq50su.js +75 -0
- package/dist/challengeAssert-BVQq50su.js.map +1 -0
- package/dist/challengeAssert-Cu-PtJ1g.cjs +2 -0
- package/dist/challengeAssert-Cu-PtJ1g.cjs.map +1 -0
- package/dist/constants-BqU1Uz15.cjs +2 -0
- package/dist/{constants-CvbARMl-.cjs.map → constants-BqU1Uz15.cjs.map} +1 -1
- package/dist/{constants-BhclpREj.js → constants-CDQE812z.js} +8 -6
- package/dist/{constants-BhclpREj.js.map → constants-CDQE812z.js.map} +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +192 -190
- package/dist/{mempoolApi-CB7bHuwi.js → mempoolApi-P_ApODLd.js} +2 -2
- package/dist/{mempoolApi-CB7bHuwi.js.map → mempoolApi-P_ApODLd.js.map} +1 -1
- package/dist/{mempoolApi-DqJByvqR.cjs → mempoolApi-emec8bHK.cjs} +2 -2
- package/dist/{mempoolApi-DqJByvqR.cjs.map → mempoolApi-emec8bHK.cjs.map} +1 -1
- package/dist/{noPayout-CNMqNNpc.cjs → noPayout-Cb8vFrkY.cjs} +2 -2
- package/dist/{noPayout-CNMqNNpc.cjs.map → noPayout-Cb8vFrkY.cjs.map} +1 -1
- package/dist/{noPayout-CfBg3HzC.js → noPayout-CihCk1HE.js} +2 -2
- package/dist/{noPayout-CfBg3HzC.js.map → noPayout-CihCk1HE.js.map} +1 -1
- package/dist/{primeVpAuth-l-5x_2UV.js → primeVpAuth-DZOaC_Ui.js} +3 -3
- package/dist/{primeVpAuth-l-5x_2UV.js.map → primeVpAuth-DZOaC_Ui.js.map} +1 -1
- package/dist/{primeVpAuth-CF4cH5_e.cjs → primeVpAuth-j2getl3f.cjs} +2 -2
- package/dist/{primeVpAuth-CF4cH5_e.cjs.map → primeVpAuth-j2getl3f.cjs.map} +1 -1
- package/dist/tbv/core/clients/index.cjs +1 -1
- package/dist/tbv/core/clients/index.js +2 -2
- package/dist/tbv/core/deposit-terms/__tests__/buildDepositTerms.test.d.ts +2 -0
- package/dist/tbv/core/deposit-terms/__tests__/buildDepositTerms.test.d.ts.map +1 -0
- package/dist/tbv/core/deposit-terms/buildDepositTerms.d.ts +8 -0
- package/dist/tbv/core/deposit-terms/buildDepositTerms.d.ts.map +1 -0
- package/dist/tbv/core/deposit-terms/depositTerms.d.ts +92 -0
- package/dist/tbv/core/deposit-terms/depositTerms.d.ts.map +1 -0
- package/dist/tbv/core/deposit-terms/index.d.ts +12 -0
- package/dist/tbv/core/deposit-terms/index.d.ts.map +1 -0
- package/dist/tbv/core/index.cjs +1 -1
- package/dist/tbv/core/index.d.ts +1 -0
- package/dist/tbv/core/index.d.ts.map +1 -1
- package/dist/tbv/core/index.js +190 -188
- package/dist/tbv/core/managers/PayoutManager.d.ts +7 -0
- package/dist/tbv/core/managers/PayoutManager.d.ts.map +1 -1
- package/dist/tbv/core/managers/PeginManager.d.ts +13 -0
- 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 +2 -2
- package/dist/tbv/core/primitives/index.cjs +1 -1
- package/dist/tbv/core/primitives/index.d.ts +0 -3
- package/dist/tbv/core/primitives/index.d.ts.map +1 -1
- package/dist/tbv/core/primitives/index.js +37 -38
- package/dist/tbv/core/primitives/psbt/__tests__/assertPayoutFeeBand.test.d.ts +7 -0
- package/dist/tbv/core/primitives/psbt/__tests__/assertPayoutFeeBand.test.d.ts.map +1 -0
- package/dist/tbv/core/primitives/psbt/assertPayoutFeeBand.d.ts +63 -0
- package/dist/tbv/core/primitives/psbt/assertPayoutFeeBand.d.ts.map +1 -0
- package/dist/tbv/core/primitives/psbt/assertWasmPeginSizing.d.ts +4 -1
- package/dist/tbv/core/primitives/psbt/assertWasmPeginSizing.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/constants.d.ts +18 -4
- package/dist/tbv/core/primitives/psbt/constants.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/payout.d.ts +21 -2
- package/dist/tbv/core/primitives/psbt/payout.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/pegin.d.ts +6 -0
- package/dist/tbv/core/primitives/psbt/pegin.d.ts.map +1 -1
- package/dist/tbv/core/services/deposit/runDepositorPresignFlow.d.ts +12 -0
- package/dist/tbv/core/services/deposit/runDepositorPresignFlow.d.ts.map +1 -1
- package/dist/tbv/core/services/deposit/signDepositorGraph.d.ts +5 -0
- package/dist/tbv/core/services/deposit/signDepositorGraph.d.ts.map +1 -1
- package/dist/tbv/core/services/index.cjs +1 -1
- package/dist/tbv/core/services/index.js +2 -2
- package/dist/tbv/index.cjs +1 -1
- package/dist/tbv/index.js +190 -188
- package/dist/tbv/integrations/aave/clients/index.d.ts +1 -1
- package/dist/tbv/integrations/aave/clients/index.d.ts.map +1 -1
- package/dist/tbv/integrations/aave/clients/spoke.d.ts +0 -20
- package/dist/tbv/integrations/aave/clients/spoke.d.ts.map +1 -1
- package/dist/tbv/integrations/aave/constants.d.ts +0 -22
- package/dist/tbv/integrations/aave/constants.d.ts.map +1 -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.d.ts +5 -6
- package/dist/tbv/integrations/aave/index.d.ts.map +1 -1
- package/dist/tbv/integrations/aave/index.js +236 -339
- package/dist/tbv/integrations/aave/index.js.map +1 -1
- package/dist/tbv/integrations/aave/types.d.ts +0 -7
- package/dist/tbv/integrations/aave/types.d.ts.map +1 -1
- package/dist/tbv/integrations/aave/utils/healthFactor.d.ts +0 -7
- package/dist/tbv/integrations/aave/utils/healthFactor.d.ts.map +1 -1
- package/dist/tbv/integrations/aave/utils/index.d.ts +3 -8
- package/dist/tbv/integrations/aave/utils/index.d.ts.map +1 -1
- package/dist/tbv/integrations/aave/utils/vaultSplit.d.ts +0 -65
- package/dist/tbv/integrations/aave/utils/vaultSplit.d.ts.map +1 -1
- package/dist/verifyScriptPathSchnorrSignature-BZhX-SOd.cjs +2 -0
- package/dist/verifyScriptPathSchnorrSignature-BZhX-SOd.cjs.map +1 -0
- package/dist/verifyScriptPathSchnorrSignature-CgrnAjMW.js +724 -0
- package/dist/verifyScriptPathSchnorrSignature-CgrnAjMW.js.map +1 -0
- package/package.json +4 -4
- package/dist/PayoutManager-BJmPnyRP.js.map +0 -1
- package/dist/PayoutManager-aqtsj60t.cjs +0 -2
- package/dist/PayoutManager-aqtsj60t.cjs.map +0 -1
- package/dist/PeginManager-CW03-M3T.js.map +0 -1
- package/dist/PeginManager-C_akOB1E.cjs +0 -2
- package/dist/PeginManager-C_akOB1E.cjs.map +0 -1
- package/dist/buildAndBroadcastRefund-BiSO4zg2.js.map +0 -1
- package/dist/buildAndBroadcastRefund-Bwi1I5we.cjs +0 -2
- package/dist/buildAndBroadcastRefund-Bwi1I5we.cjs.map +0 -1
- package/dist/challengeAssert-DLQfNcfU.cjs +0 -2
- package/dist/challengeAssert-DLQfNcfU.cjs.map +0 -1
- package/dist/challengeAssert-DvdbpEDY.js +0 -128
- package/dist/challengeAssert-DvdbpEDY.js.map +0 -1
- package/dist/constants-CvbARMl-.cjs +0 -2
- package/dist/tbv/core/primitives/psbt/depositorPayout.d.ts +0 -34
- package/dist/tbv/core/primitives/psbt/depositorPayout.d.ts.map +0 -1
- package/dist/tbv/integrations/aave/utils/__tests__/borrowRatio.test.d.ts +0 -5
- package/dist/tbv/integrations/aave/utils/__tests__/borrowRatio.test.d.ts.map +0 -1
- package/dist/tbv/integrations/aave/utils/__tests__/seizureSimulation.test.d.ts +0 -5
- package/dist/tbv/integrations/aave/utils/__tests__/seizureSimulation.test.d.ts.map +0 -1
- package/dist/tbv/integrations/aave/utils/__tests__/vaultSelection.test.d.ts +0 -2
- package/dist/tbv/integrations/aave/utils/__tests__/vaultSelection.test.d.ts.map +0 -1
- package/dist/tbv/integrations/aave/utils/borrowRatio.d.ts +0 -15
- package/dist/tbv/integrations/aave/utils/borrowRatio.d.ts.map +0 -1
- package/dist/tbv/integrations/aave/utils/seizureSimulation.d.ts +0 -109
- package/dist/tbv/integrations/aave/utils/seizureSimulation.d.ts.map +0 -1
- package/dist/tbv/integrations/aave/utils/vaultSelection.d.ts +0 -33
- package/dist/tbv/integrations/aave/utils/vaultSelection.d.ts.map +0 -1
- package/dist/verifyScriptPathSchnorrSignature-BDznSCOR.cjs +0 -2
- package/dist/verifyScriptPathSchnorrSignature-BDznSCOR.cjs.map +0 -1
- package/dist/verifyScriptPathSchnorrSignature-DA4Z4yZ3.js +0 -658
- package/dist/verifyScriptPathSchnorrSignature-DA4Z4yZ3.js.map +0 -1
|
@@ -1,38 +1,73 @@
|
|
|
1
|
-
var
|
|
2
|
-
var Nt = (s, e, t) => e in s ?
|
|
3
|
-
var
|
|
4
|
-
import { c as
|
|
1
|
+
var Ft = Object.defineProperty;
|
|
2
|
+
var Nt = (s, e, t) => e in s ? Ft(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
|
|
3
|
+
var y = (s, e, t) => Nt(s, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
import { c as Tt, H as Wt, r as M, a as L, b as st, u as Lt, d as ot, e as nt, f as rt, g as Dt, s as Bt } from "./sha2-BYVxyZzX.js";
|
|
5
5
|
import * as Et from "bitcoinjs-lib";
|
|
6
6
|
import { Transaction as Xt, Psbt as it } from "bitcoinjs-lib";
|
|
7
7
|
import { Buffer as Y } from "buffer";
|
|
8
8
|
import { isAddressEqual as at, encodeFunctionData as j, zeroAddress as Kt } from "viem";
|
|
9
|
-
import { B as
|
|
10
|
-
import { h as
|
|
11
|
-
import { expandWotsSeed as
|
|
12
|
-
import { a as lt, b as
|
|
13
|
-
import { b as
|
|
14
|
-
import { u as Q, e as
|
|
15
|
-
import { c as
|
|
16
|
-
import { M as
|
|
17
|
-
import { d as
|
|
18
|
-
import { c as ne, a as
|
|
19
|
-
import { s as
|
|
20
|
-
import { p as
|
|
21
|
-
import {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
9
|
+
import { B as H } from "./BTCVaultRegistry.abi-Chs4AFBj.js";
|
|
10
|
+
import { h as R } from "./errors-Blc-JWnI.js";
|
|
11
|
+
import { expandWotsSeed as Gt, expandHashlockSecret as qt, expandAuthAnchor as zt, deriveVaultId as ct } from "@babylonlabs-io/babylon-tbv-rust-wasm";
|
|
12
|
+
import { a as lt, b as Yt, d as ut, f as jt } from "./verifyScriptPathSchnorrSignature-CgrnAjMW.js";
|
|
13
|
+
import { b as Qt, e as Zt, f as Jt } from "./peginInput-BKD_b3gq.js";
|
|
14
|
+
import { u as Q, e as A, p as te, i as ht, h as dt, s as v, j as gt, X as ee } from "./bitcoin-B5aNKtsk.js";
|
|
15
|
+
import { c as se } from "./signing-DaLvGwQe.js";
|
|
16
|
+
import { M as ft } from "./validation-CxqROCno.js";
|
|
17
|
+
import { d as oe } from "./deriveVaultRoot-B_gEOTD6.js";
|
|
18
|
+
import { c as ne, a as re, s as ie, b as ae } from "./PayoutManager-BVgmuKuK.js";
|
|
19
|
+
import { s as ce, f as X, i as le, w as pt } from "./waitForTransactionReceiptSmartAware-lobLm0q0.js";
|
|
20
|
+
import { p as ue, f as he } from "./fundPeginTransaction-B2wyjqp0.js";
|
|
21
|
+
import { M as mt, B as de } from "./constants-CDQE812z.js";
|
|
22
|
+
import { p as ge, V as fe, i as pe } from "./mempoolApi-P_ApODLd.js";
|
|
23
|
+
const U = /* @__PURE__ */ BigInt(2 ** 32 - 1), Pt = /* @__PURE__ */ BigInt(32);
|
|
24
|
+
function me(s, e = !1) {
|
|
25
|
+
return e ? { h: Number(s & U), l: Number(s >> Pt & U) } : { h: Number(s >> Pt & U) | 0, l: Number(s & U) | 0 };
|
|
25
26
|
}
|
|
26
|
-
function
|
|
27
|
+
function Pe(s, e = !1) {
|
|
27
28
|
const t = s.length;
|
|
28
|
-
let
|
|
29
|
+
let o = new Uint32Array(t), n = new Uint32Array(t);
|
|
29
30
|
for (let r = 0; r < t; r++) {
|
|
30
|
-
const { h: i, l: a } =
|
|
31
|
-
[
|
|
31
|
+
const { h: i, l: a } = me(s[r], e);
|
|
32
|
+
[o[r], n[r]] = [i, a];
|
|
32
33
|
}
|
|
33
|
-
return [
|
|
34
|
+
return [o, n];
|
|
34
35
|
}
|
|
35
|
-
const
|
|
36
|
+
const be = (s, e, t) => s << t | e >>> 32 - t, we = (s, e, t) => e << t | s >>> 32 - t, xe = (s, e, t) => e << t - 32 | s >>> 64 - t, ye = (s, e, t) => s << t - 32 | e >>> 64 - t, ke = 64;
|
|
37
|
+
function Te(s) {
|
|
38
|
+
const e = s.prepeginTxid.toLowerCase();
|
|
39
|
+
if (!/^[0-9a-f]+$/.test(e) || e.length !== ke)
|
|
40
|
+
throw new Error(`buildDepositTerms: prepeginTxid must be 64 hex chars, got "${s.prepeginTxid}"`);
|
|
41
|
+
if (s.vaultAmounts.length === 0)
|
|
42
|
+
throw new Error("buildDepositTerms: at least one vault amount is required");
|
|
43
|
+
if (s.timelockPegin <= 0 || s.timelockRefund <= 0)
|
|
44
|
+
throw new Error("buildDepositTerms: timelocks must be positive");
|
|
45
|
+
if (!Number.isInteger(s.commissionBps) || s.commissionBps < 0 || s.commissionBps >= mt)
|
|
46
|
+
throw new Error(
|
|
47
|
+
`buildDepositTerms: commissionBps must be an integer in [0, ${mt}), got ${s.commissionBps}`
|
|
48
|
+
);
|
|
49
|
+
const t = BigInt(de), o = s.vaultAmounts.map((n, r) => ({
|
|
50
|
+
htlcVout: r,
|
|
51
|
+
vaultProviderPk: s.vaultProviderPk,
|
|
52
|
+
vaultAmount: n,
|
|
53
|
+
// floor(vaultAmount * bps / 10_000) — matches the vault provider's commission math.
|
|
54
|
+
commissionFee: n * BigInt(s.commissionBps) / t,
|
|
55
|
+
depositorClaimValue: s.depositorClaimValue,
|
|
56
|
+
peginMaxFee: s.peginMaxFee
|
|
57
|
+
}));
|
|
58
|
+
return {
|
|
59
|
+
baseFeeRate: s.protocolFeeRate,
|
|
60
|
+
peginCsvTimelock: s.timelockPegin,
|
|
61
|
+
payoutTimelock: s.timelockPegin,
|
|
62
|
+
htlcRefundTimelock: s.timelockRefund,
|
|
63
|
+
prepeginTxid: e,
|
|
64
|
+
prepeginMaxFee: s.prepeginMaxFee,
|
|
65
|
+
keeperPks: [...s.keeperPks],
|
|
66
|
+
challengerPks: [...s.challengerPks],
|
|
67
|
+
vaults: o
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
const Be = /* @__PURE__ */ Uint8Array.from([
|
|
36
71
|
7,
|
|
37
72
|
4,
|
|
38
73
|
13,
|
|
@@ -49,98 +84,98 @@ const pe = (s, e, t) => s << t | e >>> 32 - t, me = (s, e, t) => e << t | s >>>
|
|
|
49
84
|
14,
|
|
50
85
|
11,
|
|
51
86
|
8
|
|
52
|
-
]),
|
|
53
|
-
const t = [[
|
|
54
|
-
for (let
|
|
55
|
-
for (let
|
|
56
|
-
|
|
87
|
+
]), vt = Uint8Array.from(new Array(16).fill(0).map((s, e) => e)), Ee = vt.map((s) => (9 * s + 5) % 16), _t = /* @__PURE__ */ (() => {
|
|
88
|
+
const t = [[vt], [Ee]];
|
|
89
|
+
for (let o = 0; o < 4; o++)
|
|
90
|
+
for (let n of t)
|
|
91
|
+
n.push(n[o].map((r) => Be[r]));
|
|
57
92
|
return t;
|
|
58
|
-
})(),
|
|
93
|
+
})(), St = _t[0], At = _t[1], It = /* @__PURE__ */ [
|
|
59
94
|
[11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8],
|
|
60
95
|
[12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7],
|
|
61
96
|
[13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9],
|
|
62
97
|
[14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6],
|
|
63
98
|
[15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5]
|
|
64
|
-
].map((s) => Uint8Array.from(s)),
|
|
99
|
+
].map((s) => Uint8Array.from(s)), ve = /* @__PURE__ */ St.map((s, e) => s.map((t) => It[e][t])), _e = /* @__PURE__ */ At.map((s, e) => s.map((t) => It[e][t])), Se = /* @__PURE__ */ Uint32Array.from([
|
|
65
100
|
0,
|
|
66
101
|
1518500249,
|
|
67
102
|
1859775393,
|
|
68
103
|
2400959708,
|
|
69
104
|
2840853838
|
|
70
|
-
]),
|
|
105
|
+
]), Ae = /* @__PURE__ */ Uint32Array.from([
|
|
71
106
|
1352829926,
|
|
72
107
|
1548603684,
|
|
73
108
|
1836072691,
|
|
74
109
|
2053994217,
|
|
75
110
|
0
|
|
76
111
|
]);
|
|
77
|
-
function
|
|
78
|
-
return s === 0 ? e ^ t ^
|
|
112
|
+
function bt(s, e, t, o) {
|
|
113
|
+
return s === 0 ? e ^ t ^ o : s === 1 ? e & t | ~e & o : s === 2 ? (e | ~t) ^ o : s === 3 ? e & o | t & ~o : e ^ (t | ~o);
|
|
79
114
|
}
|
|
80
|
-
const
|
|
81
|
-
class
|
|
115
|
+
const F = /* @__PURE__ */ new Uint32Array(16);
|
|
116
|
+
class Ie extends Wt {
|
|
82
117
|
constructor() {
|
|
83
118
|
super(64, 20, 8, !0);
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
119
|
+
y(this, "h0", 1732584193);
|
|
120
|
+
y(this, "h1", -271733879);
|
|
121
|
+
y(this, "h2", -1732584194);
|
|
122
|
+
y(this, "h3", 271733878);
|
|
123
|
+
y(this, "h4", -1009589776);
|
|
89
124
|
}
|
|
90
125
|
get() {
|
|
91
|
-
const { h0: t, h1:
|
|
92
|
-
return [t,
|
|
126
|
+
const { h0: t, h1: o, h2: n, h3: r, h4: i } = this;
|
|
127
|
+
return [t, o, n, r, i];
|
|
93
128
|
}
|
|
94
|
-
set(t,
|
|
95
|
-
this.h0 = t | 0, this.h1 =
|
|
129
|
+
set(t, o, n, r, i) {
|
|
130
|
+
this.h0 = t | 0, this.h1 = o | 0, this.h2 = n | 0, this.h3 = r | 0, this.h4 = i | 0;
|
|
96
131
|
}
|
|
97
|
-
process(t,
|
|
98
|
-
for (let
|
|
99
|
-
|
|
100
|
-
let
|
|
101
|
-
for (let
|
|
102
|
-
const
|
|
103
|
-
for (let
|
|
104
|
-
const
|
|
105
|
-
|
|
132
|
+
process(t, o) {
|
|
133
|
+
for (let d = 0; d < 16; d++, o += 4)
|
|
134
|
+
F[d] = t.getUint32(o, !0);
|
|
135
|
+
let n = this.h0 | 0, r = n, i = this.h1 | 0, a = i, c = this.h2 | 0, u = c, l = this.h3 | 0, m = l, b = this.h4 | 0, w = b;
|
|
136
|
+
for (let d = 0; d < 5; d++) {
|
|
137
|
+
const E = 4 - d, k = Se[d], f = Ae[d], _ = St[d], p = At[d], P = ve[d], T = _e[d];
|
|
138
|
+
for (let h = 0; h < 16; h++) {
|
|
139
|
+
const x = M(n + bt(d, i, c, l) + F[_[h]] + k, P[h]) + b | 0;
|
|
140
|
+
n = b, b = l, l = M(c, 10) | 0, c = i, i = x;
|
|
106
141
|
}
|
|
107
|
-
for (let
|
|
108
|
-
const
|
|
109
|
-
r =
|
|
142
|
+
for (let h = 0; h < 16; h++) {
|
|
143
|
+
const x = M(r + bt(E, a, u, m) + F[p[h]] + f, T[h]) + w | 0;
|
|
144
|
+
r = w, w = m, m = M(u, 10) | 0, u = a, a = x;
|
|
110
145
|
}
|
|
111
146
|
}
|
|
112
|
-
this.set(this.h1 + c + m | 0, this.h2 + l +
|
|
147
|
+
this.set(this.h1 + c + m | 0, this.h2 + l + w | 0, this.h3 + b + r | 0, this.h4 + n + a | 0, this.h0 + i + u | 0);
|
|
113
148
|
}
|
|
114
149
|
roundClean() {
|
|
115
|
-
F
|
|
150
|
+
L(F);
|
|
116
151
|
}
|
|
117
152
|
destroy() {
|
|
118
|
-
this.destroyed = !0,
|
|
153
|
+
this.destroyed = !0, L(this.buffer), this.set(0, 0, 0, 0, 0);
|
|
119
154
|
}
|
|
120
155
|
}
|
|
121
|
-
const
|
|
122
|
-
for (let s = 0, e =
|
|
123
|
-
[t,
|
|
124
|
-
let
|
|
156
|
+
const Ce = /* @__PURE__ */ Tt(() => new Ie()), He = BigInt(0), $ = BigInt(1), Re = BigInt(2), Oe = BigInt(7), $e = BigInt(256), Ve = BigInt(113), Ct = [], Ht = [], Rt = [];
|
|
157
|
+
for (let s = 0, e = $, t = 1, o = 0; s < 24; s++) {
|
|
158
|
+
[t, o] = [o, (2 * t + 3 * o) % 5], Ct.push(2 * (5 * o + t)), Ht.push((s + 1) * (s + 2) / 2 % 64);
|
|
159
|
+
let n = He;
|
|
125
160
|
for (let r = 0; r < 7; r++)
|
|
126
|
-
e = (e <<
|
|
127
|
-
|
|
161
|
+
e = (e << $ ^ (e >> Oe) * Ve) % $e, e & Re && (n ^= $ << ($ << BigInt(r)) - $);
|
|
162
|
+
Rt.push(n);
|
|
128
163
|
}
|
|
129
|
-
const
|
|
130
|
-
function
|
|
164
|
+
const Ot = Pe(Rt, !0), Me = Ot[0], Ue = Ot[1], wt = (s, e, t) => t > 32 ? xe(s, e, t) : be(s, e, t), xt = (s, e, t) => t > 32 ? ye(s, e, t) : we(s, e, t);
|
|
165
|
+
function Fe(s, e = 24) {
|
|
131
166
|
const t = new Uint32Array(10);
|
|
132
|
-
for (let
|
|
167
|
+
for (let o = 24 - e; o < 24; o++) {
|
|
133
168
|
for (let i = 0; i < 10; i++)
|
|
134
169
|
t[i] = s[i] ^ s[i + 10] ^ s[i + 20] ^ s[i + 30] ^ s[i + 40];
|
|
135
170
|
for (let i = 0; i < 10; i += 2) {
|
|
136
|
-
const a = (i + 8) % 10, c = (i + 2) % 10,
|
|
137
|
-
for (let
|
|
138
|
-
s[i +
|
|
171
|
+
const a = (i + 8) % 10, c = (i + 2) % 10, u = t[c], l = t[c + 1], m = wt(u, l, 1) ^ t[a], b = xt(u, l, 1) ^ t[a + 1];
|
|
172
|
+
for (let w = 0; w < 50; w += 10)
|
|
173
|
+
s[i + w] ^= m, s[i + w + 1] ^= b;
|
|
139
174
|
}
|
|
140
|
-
let
|
|
175
|
+
let n = s[2], r = s[3];
|
|
141
176
|
for (let i = 0; i < 24; i++) {
|
|
142
|
-
const a =
|
|
143
|
-
|
|
177
|
+
const a = Ht[i], c = wt(n, r, a), u = xt(n, r, a), l = Ct[i];
|
|
178
|
+
n = s[l], r = s[l + 1], s[l] = c, s[l + 1] = u;
|
|
144
179
|
}
|
|
145
180
|
for (let i = 0; i < 50; i += 10) {
|
|
146
181
|
for (let a = 0; a < 10; a++)
|
|
@@ -148,41 +183,41 @@ function Oe(s, e = 24) {
|
|
|
148
183
|
for (let a = 0; a < 10; a++)
|
|
149
184
|
s[i + a] ^= ~t[(a + 2) % 10] & t[(a + 4) % 10];
|
|
150
185
|
}
|
|
151
|
-
s[0] ^=
|
|
186
|
+
s[0] ^= Me[o], s[1] ^= Ue[o];
|
|
152
187
|
}
|
|
153
|
-
|
|
188
|
+
L(t);
|
|
154
189
|
}
|
|
155
190
|
class tt {
|
|
156
191
|
// NOTE: we accept arguments in bytes instead of bits here.
|
|
157
|
-
constructor(e, t,
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
if (this.blockLen = e, this.suffix = t, this.outputLen =
|
|
192
|
+
constructor(e, t, o, n = !1, r = 24) {
|
|
193
|
+
y(this, "state");
|
|
194
|
+
y(this, "pos", 0);
|
|
195
|
+
y(this, "posOut", 0);
|
|
196
|
+
y(this, "finished", !1);
|
|
197
|
+
y(this, "state32");
|
|
198
|
+
y(this, "destroyed", !1);
|
|
199
|
+
y(this, "blockLen");
|
|
200
|
+
y(this, "suffix");
|
|
201
|
+
y(this, "outputLen");
|
|
202
|
+
y(this, "enableXOF", !1);
|
|
203
|
+
y(this, "rounds");
|
|
204
|
+
if (this.blockLen = e, this.suffix = t, this.outputLen = o, this.enableXOF = n, this.rounds = r, st(o, "outputLen"), !(0 < e && e < 200))
|
|
170
205
|
throw new Error("only keccak-f1600 function is supported");
|
|
171
|
-
this.state = new Uint8Array(200), this.state32 =
|
|
206
|
+
this.state = new Uint8Array(200), this.state32 = Lt(this.state);
|
|
172
207
|
}
|
|
173
208
|
clone() {
|
|
174
209
|
return this._cloneInto();
|
|
175
210
|
}
|
|
176
211
|
keccak() {
|
|
177
|
-
|
|
212
|
+
ot(this.state32), Fe(this.state32, this.rounds), ot(this.state32), this.posOut = 0, this.pos = 0;
|
|
178
213
|
}
|
|
179
214
|
update(e) {
|
|
180
|
-
|
|
181
|
-
const { blockLen: t, state:
|
|
182
|
-
for (let r = 0; r <
|
|
183
|
-
const i = Math.min(t - this.pos,
|
|
215
|
+
nt(this), rt(e);
|
|
216
|
+
const { blockLen: t, state: o } = this, n = e.length;
|
|
217
|
+
for (let r = 0; r < n; ) {
|
|
218
|
+
const i = Math.min(t - this.pos, n - r);
|
|
184
219
|
for (let a = 0; a < i; a++)
|
|
185
|
-
|
|
220
|
+
o[this.pos++] ^= e[r++];
|
|
186
221
|
this.pos === t && this.keccak();
|
|
187
222
|
}
|
|
188
223
|
return this;
|
|
@@ -191,16 +226,16 @@ class tt {
|
|
|
191
226
|
if (this.finished)
|
|
192
227
|
return;
|
|
193
228
|
this.finished = !0;
|
|
194
|
-
const { state: e, suffix: t, pos:
|
|
195
|
-
e[
|
|
229
|
+
const { state: e, suffix: t, pos: o, blockLen: n } = this;
|
|
230
|
+
e[o] ^= t, (t & 128) !== 0 && o === n - 1 && this.keccak(), e[n - 1] ^= 128, this.keccak();
|
|
196
231
|
}
|
|
197
232
|
writeInto(e) {
|
|
198
|
-
|
|
199
|
-
const t = this.state, { blockLen:
|
|
200
|
-
for (let
|
|
201
|
-
this.posOut >=
|
|
202
|
-
const i = Math.min(
|
|
203
|
-
e.set(t.subarray(this.posOut, this.posOut + i),
|
|
233
|
+
nt(this, !1), rt(e), this.finish();
|
|
234
|
+
const t = this.state, { blockLen: o } = this;
|
|
235
|
+
for (let n = 0, r = e.length; n < r; ) {
|
|
236
|
+
this.posOut >= o && this.keccak();
|
|
237
|
+
const i = Math.min(o - this.posOut, r - n);
|
|
238
|
+
e.set(t.subarray(this.posOut, this.posOut + i), n), this.posOut += i, n += i;
|
|
204
239
|
}
|
|
205
240
|
return e;
|
|
206
241
|
}
|
|
@@ -213,7 +248,7 @@ class tt {
|
|
|
213
248
|
return st(e), this.xofInto(new Uint8Array(e));
|
|
214
249
|
}
|
|
215
250
|
digestInto(e) {
|
|
216
|
-
if (
|
|
251
|
+
if (Dt(e, this), this.finished)
|
|
217
252
|
throw new Error("digest() was already called");
|
|
218
253
|
return this.writeInto(e), this.destroy(), e;
|
|
219
254
|
}
|
|
@@ -221,73 +256,73 @@ class tt {
|
|
|
221
256
|
return this.digestInto(new Uint8Array(this.outputLen));
|
|
222
257
|
}
|
|
223
258
|
destroy() {
|
|
224
|
-
this.destroyed = !0,
|
|
259
|
+
this.destroyed = !0, L(this.state);
|
|
225
260
|
}
|
|
226
261
|
_cloneInto(e) {
|
|
227
|
-
const { blockLen: t, suffix:
|
|
228
|
-
return e || (e = new tt(t,
|
|
262
|
+
const { blockLen: t, suffix: o, outputLen: n, rounds: r, enableXOF: i } = this;
|
|
263
|
+
return e || (e = new tt(t, o, n, i, r)), e.state32.set(this.state32), e.pos = this.pos, e.posOut = this.posOut, e.finished = this.finished, e.rounds = r, e.suffix = o, e.outputLen = n, e.enableXOF = i, e.destroyed = this.destroyed, e;
|
|
229
264
|
}
|
|
230
265
|
}
|
|
231
|
-
const
|
|
266
|
+
const Ne = (s, e, t, o = {}) => Tt(() => new tt(e, s, t), o), We = /* @__PURE__ */ Ne(1, 136, 32), yt = 64, S = 20, Z = 4, $t = 2, Le = 0, De = 1, N = [64, 64], Xe = (s) => Array.from(s).map((e) => e.toString(16).padStart(2, "0")).join("");
|
|
232
267
|
function et(s) {
|
|
233
|
-
return
|
|
268
|
+
return Ce(Bt(s));
|
|
234
269
|
}
|
|
235
|
-
function
|
|
270
|
+
function Vt(s) {
|
|
236
271
|
return (1 << s) - 1;
|
|
237
272
|
}
|
|
238
|
-
function
|
|
273
|
+
function Ke(s) {
|
|
239
274
|
let e = 1;
|
|
240
275
|
for (; e * e < s + 1; ) e++;
|
|
241
276
|
return Math.max(e, 2);
|
|
242
277
|
}
|
|
243
|
-
function
|
|
244
|
-
const e = Z, t = s *
|
|
245
|
-
return { d: e, n: s, checksum_radix:
|
|
278
|
+
function Ge(s) {
|
|
279
|
+
const e = Z, t = s * Vt(e);
|
|
280
|
+
return { d: e, n: s, checksum_radix: Ke(t) };
|
|
246
281
|
}
|
|
247
|
-
function
|
|
282
|
+
function K(s, e) {
|
|
248
283
|
const t = [];
|
|
249
|
-
let
|
|
250
|
-
for (;
|
|
251
|
-
t.push(
|
|
252
|
-
const
|
|
253
|
-
|
|
284
|
+
let o = e;
|
|
285
|
+
for (; o > 0; )
|
|
286
|
+
t.push(o & 255), o >>>= 8;
|
|
287
|
+
const n = new Uint8Array(s.length + t.length);
|
|
288
|
+
n.set(s);
|
|
254
289
|
for (let r = 0; r < t.length; r++)
|
|
255
|
-
|
|
256
|
-
return et(
|
|
290
|
+
n[s.length + r] = t[r];
|
|
291
|
+
return et(n);
|
|
257
292
|
}
|
|
258
293
|
function G(s, e) {
|
|
259
294
|
let t = s;
|
|
260
|
-
for (let
|
|
295
|
+
for (let o = 0; o < e; o++)
|
|
261
296
|
t = et(t);
|
|
262
297
|
return t;
|
|
263
298
|
}
|
|
264
|
-
function
|
|
265
|
-
const t =
|
|
299
|
+
function qe(s, e) {
|
|
300
|
+
const t = Vt(e.d), o = e.checksum_radix - 1, n = Math.floor(e.n * t / e.checksum_radix), r = [];
|
|
266
301
|
for (let l = 0; l < e.n; l++) {
|
|
267
|
-
const m =
|
|
268
|
-
r.push(Array.from(
|
|
302
|
+
const m = K(s, l + $t), b = G(m, t);
|
|
303
|
+
r.push(Array.from(b));
|
|
269
304
|
}
|
|
270
|
-
const i =
|
|
305
|
+
const i = K(
|
|
271
306
|
s,
|
|
272
|
-
|
|
307
|
+
Le
|
|
273
308
|
), a = G(
|
|
274
309
|
i,
|
|
275
|
-
|
|
276
|
-
), c =
|
|
310
|
+
o
|
|
311
|
+
), c = K(
|
|
277
312
|
s,
|
|
278
|
-
|
|
279
|
-
),
|
|
313
|
+
De
|
|
314
|
+
), u = G(
|
|
280
315
|
c,
|
|
281
|
-
|
|
316
|
+
n
|
|
282
317
|
);
|
|
283
318
|
return {
|
|
284
319
|
config: e,
|
|
285
320
|
message_terminals: r,
|
|
286
|
-
checksum_major_terminal: Array.from(
|
|
321
|
+
checksum_major_terminal: Array.from(u),
|
|
287
322
|
checksum_minor_terminal: Array.from(a)
|
|
288
323
|
};
|
|
289
324
|
}
|
|
290
|
-
async function
|
|
325
|
+
async function ze(s) {
|
|
291
326
|
try {
|
|
292
327
|
if (s.length !== yt)
|
|
293
328
|
throw new Error(
|
|
@@ -295,35 +330,35 @@ async function Xe(s) {
|
|
|
295
330
|
);
|
|
296
331
|
const e = [];
|
|
297
332
|
for (let t = 0; t < N.length; t++) {
|
|
298
|
-
const
|
|
333
|
+
const o = N[t], n = Ge(o), r = new Uint8Array(s.length + 1);
|
|
299
334
|
r.set(s), r[s.length] = t;
|
|
300
335
|
const i = et(r);
|
|
301
336
|
try {
|
|
302
|
-
const a =
|
|
337
|
+
const a = qe(i, n);
|
|
303
338
|
if (a.config.d !== Z)
|
|
304
339
|
throw new Error(
|
|
305
340
|
`Block ${t}: expected d=${Z}, got d=${a.config.d}`
|
|
306
341
|
);
|
|
307
|
-
if (a.config.n !==
|
|
342
|
+
if (a.config.n !== o)
|
|
308
343
|
throw new Error(
|
|
309
|
-
`Block ${t}: expected n=${
|
|
344
|
+
`Block ${t}: expected n=${o}, got n=${a.config.n}`
|
|
310
345
|
);
|
|
311
|
-
if (a.message_terminals.length !==
|
|
346
|
+
if (a.message_terminals.length !== o)
|
|
312
347
|
throw new Error(
|
|
313
|
-
`Block ${t}: expected ${
|
|
348
|
+
`Block ${t}: expected ${o} message terminals, got ${a.message_terminals.length}`
|
|
314
349
|
);
|
|
315
350
|
for (let c = 0; c < a.message_terminals.length; c++)
|
|
316
|
-
if (a.message_terminals[c].length !==
|
|
351
|
+
if (a.message_terminals[c].length !== S)
|
|
317
352
|
throw new Error(
|
|
318
|
-
`Block ${t} terminal ${c}: expected ${
|
|
353
|
+
`Block ${t} terminal ${c}: expected ${S} bytes, got ${a.message_terminals[c].length}`
|
|
319
354
|
);
|
|
320
|
-
if (a.checksum_minor_terminal.length !==
|
|
355
|
+
if (a.checksum_minor_terminal.length !== S)
|
|
321
356
|
throw new Error(
|
|
322
|
-
`Block ${t} checksum_minor: expected ${
|
|
357
|
+
`Block ${t} checksum_minor: expected ${S} bytes`
|
|
323
358
|
);
|
|
324
|
-
if (a.checksum_major_terminal.length !==
|
|
359
|
+
if (a.checksum_major_terminal.length !== S)
|
|
325
360
|
throw new Error(
|
|
326
|
-
`Block ${t} checksum_major: expected ${
|
|
361
|
+
`Block ${t} checksum_major: expected ${S} bytes`
|
|
327
362
|
);
|
|
328
363
|
e.push(a);
|
|
329
364
|
} finally {
|
|
@@ -340,19 +375,19 @@ async function Xe(s) {
|
|
|
340
375
|
}
|
|
341
376
|
}
|
|
342
377
|
function q(s, e, t) {
|
|
343
|
-
if (s.length !==
|
|
378
|
+
if (s.length !== S)
|
|
344
379
|
throw new Error(
|
|
345
|
-
`Block ${e} ${t}: expected ${
|
|
380
|
+
`Block ${e} ${t}: expected ${S} bytes, got ${s.length}`
|
|
346
381
|
);
|
|
347
|
-
for (let
|
|
348
|
-
const
|
|
349
|
-
if (!Number.isInteger(
|
|
382
|
+
for (let o = 0; o < s.length; o++) {
|
|
383
|
+
const n = s[o];
|
|
384
|
+
if (!Number.isInteger(n) || n < 0 || n > 255)
|
|
350
385
|
throw new Error(
|
|
351
|
-
`Block ${e} ${t}[${
|
|
386
|
+
`Block ${e} ${t}[${o}]: invalid byte value ${n}`
|
|
352
387
|
);
|
|
353
388
|
}
|
|
354
389
|
}
|
|
355
|
-
function
|
|
390
|
+
function Ye(s) {
|
|
356
391
|
if (s.length === 0)
|
|
357
392
|
throw new Error("Public keys array must not be empty");
|
|
358
393
|
for (let r = 0; r < s.length; r++) {
|
|
@@ -363,32 +398,32 @@ function Ke(s) {
|
|
|
363
398
|
}
|
|
364
399
|
let e = 0;
|
|
365
400
|
for (const r of s)
|
|
366
|
-
e +=
|
|
367
|
-
const t = new Uint8Array(e *
|
|
368
|
-
let
|
|
401
|
+
e += $t + r.message_terminals.length;
|
|
402
|
+
const t = new Uint8Array(e * S);
|
|
403
|
+
let o = 0;
|
|
369
404
|
for (const r of s) {
|
|
370
|
-
t.set(r.checksum_minor_terminal,
|
|
405
|
+
t.set(r.checksum_minor_terminal, o), o += S, t.set(r.checksum_major_terminal, o), o += S;
|
|
371
406
|
for (const i of r.message_terminals)
|
|
372
|
-
t.set(i,
|
|
407
|
+
t.set(i, o), o += S;
|
|
373
408
|
}
|
|
374
|
-
const
|
|
375
|
-
return `0x${
|
|
409
|
+
const n = We(t);
|
|
410
|
+
return `0x${Xe(n)}`;
|
|
376
411
|
}
|
|
377
|
-
async function
|
|
378
|
-
const t = [],
|
|
412
|
+
async function je(s, e) {
|
|
413
|
+
const t = [], o = [], n = [], r = [];
|
|
379
414
|
try {
|
|
380
415
|
for (let i = 0; i < e; i++) {
|
|
381
|
-
const a = await
|
|
416
|
+
const a = await Gt(s, i);
|
|
382
417
|
try {
|
|
383
|
-
const
|
|
384
|
-
t.push(
|
|
418
|
+
const u = await ze(a);
|
|
419
|
+
t.push(u), o.push(Ye(u));
|
|
385
420
|
} finally {
|
|
386
421
|
a.fill(0);
|
|
387
422
|
}
|
|
388
|
-
const c = await
|
|
423
|
+
const c = await qt(s, i);
|
|
389
424
|
try {
|
|
390
|
-
const
|
|
391
|
-
|
|
425
|
+
const u = Q(c);
|
|
426
|
+
n.push(u), r.push(ne(A(u)).slice(2));
|
|
392
427
|
} finally {
|
|
393
428
|
c.fill(0);
|
|
394
429
|
}
|
|
@@ -396,36 +431,36 @@ async function De(s, e) {
|
|
|
396
431
|
} finally {
|
|
397
432
|
s.fill(0);
|
|
398
433
|
}
|
|
399
|
-
return { perVaultWotsKeys: t, wotsPkHashes:
|
|
434
|
+
return { perVaultWotsKeys: t, wotsPkHashes: o, htlcSecretHexes: n, hashlocks: r };
|
|
400
435
|
}
|
|
401
|
-
const
|
|
402
|
-
function
|
|
436
|
+
const Qe = /^0x[0-9a-f]+$/i, Ze = /^[0-9a-f]+$/i, Je = /^[A-Za-z0-9+/]+={0,2}$/;
|
|
437
|
+
function V(s) {
|
|
403
438
|
if (typeof s != "string" || s.length === 0)
|
|
404
439
|
throw new Error("BTC wallet returned empty public key");
|
|
405
|
-
return
|
|
440
|
+
return te(s).toLowerCase();
|
|
406
441
|
}
|
|
407
|
-
function
|
|
442
|
+
function ts(s) {
|
|
408
443
|
if (typeof s != "string" || s.length === 0)
|
|
409
444
|
throw new Error("BTC wallet returned empty BIP-322 signature");
|
|
410
445
|
if (s.startsWith("0x") || s.startsWith("0X")) {
|
|
411
|
-
if (!
|
|
446
|
+
if (!Qe.test(s) || s.length < 4 || s.length % 2 !== 0)
|
|
412
447
|
throw new Error("BTC wallet returned malformed hex BIP-322 signature");
|
|
413
448
|
return s.toLowerCase();
|
|
414
449
|
}
|
|
415
|
-
if (
|
|
450
|
+
if (Ze.test(s)) {
|
|
416
451
|
if (s.length % 2 !== 0)
|
|
417
452
|
throw new Error("BTC wallet returned malformed hex BIP-322 signature");
|
|
418
453
|
return `0x${s.toLowerCase()}`;
|
|
419
454
|
}
|
|
420
|
-
if (!
|
|
455
|
+
if (!Je.test(s) || s.length % 4 !== 0)
|
|
421
456
|
throw new Error("BTC wallet returned malformed base64 BIP-322 signature");
|
|
422
457
|
const e = Y.from(s, "base64");
|
|
423
458
|
if (e.length === 0 || e.toString("base64") !== s)
|
|
424
459
|
throw new Error("BTC wallet returned malformed base64 BIP-322 signature");
|
|
425
460
|
return `0x${e.toString("hex")}`;
|
|
426
461
|
}
|
|
427
|
-
const
|
|
428
|
-
function
|
|
462
|
+
const Mt = 0, W = 25, J = 9999, z = "00".repeat(32);
|
|
463
|
+
function es(s, e) {
|
|
429
464
|
const t = {
|
|
430
465
|
bitcoin: "bc",
|
|
431
466
|
testnet: "tb",
|
|
@@ -433,30 +468,30 @@ function je(s, e) {
|
|
|
433
468
|
regtest: "bcrt"
|
|
434
469
|
};
|
|
435
470
|
try {
|
|
436
|
-
const
|
|
437
|
-
return
|
|
471
|
+
const o = Et.address.fromBech32(s);
|
|
472
|
+
return o.prefix === t[e] && o.version === 0 && o.data.length === 20;
|
|
438
473
|
} catch {
|
|
439
474
|
return !1;
|
|
440
475
|
}
|
|
441
476
|
}
|
|
442
|
-
function
|
|
443
|
-
const
|
|
444
|
-
return
|
|
477
|
+
function ss(s, e, t, o) {
|
|
478
|
+
const n = t == null ? void 0 : t[`${s}:${e}`];
|
|
479
|
+
return n ? Promise.resolve({
|
|
445
480
|
txid: s,
|
|
446
481
|
vout: e,
|
|
447
|
-
value:
|
|
448
|
-
scriptPubKey:
|
|
449
|
-
}) :
|
|
482
|
+
value: n.value,
|
|
483
|
+
scriptPubKey: n.scriptPubKey
|
|
484
|
+
}) : pe(s, e, o);
|
|
450
485
|
}
|
|
451
|
-
const
|
|
452
|
-
class
|
|
486
|
+
const kt = 12e4;
|
|
487
|
+
class Ss {
|
|
453
488
|
/**
|
|
454
489
|
* Creates a new PeginManager instance.
|
|
455
490
|
*
|
|
456
491
|
* @param config - Manager configuration including wallets and contract addresses
|
|
457
492
|
*/
|
|
458
493
|
constructor(e) {
|
|
459
|
-
|
|
494
|
+
y(this, "config");
|
|
460
495
|
this.config = e;
|
|
461
496
|
}
|
|
462
497
|
/**
|
|
@@ -472,7 +507,7 @@ class ks {
|
|
|
472
507
|
async preparePegin(e) {
|
|
473
508
|
if (e.amounts.length === 0)
|
|
474
509
|
throw new Error("amounts must contain at least one entry");
|
|
475
|
-
const t = await this.config.btcWallet.getPublicKeyHex(),
|
|
510
|
+
const t = await this.config.btcWallet.getPublicKeyHex(), o = V(t);
|
|
476
511
|
if (!ht(
|
|
477
512
|
e.changeAddress,
|
|
478
513
|
t,
|
|
@@ -481,57 +516,60 @@ class ks {
|
|
|
481
516
|
throw new Error(
|
|
482
517
|
`Pre-PegIn changeAddress "${e.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.`
|
|
483
518
|
);
|
|
484
|
-
const
|
|
485
|
-
(
|
|
486
|
-
txid: dt(
|
|
487
|
-
vout:
|
|
519
|
+
const n = await this.prepareSizing(o, e), r = n.selectedUTXOs.map(
|
|
520
|
+
(f) => ({
|
|
521
|
+
txid: dt(f.txid),
|
|
522
|
+
vout: f.vout
|
|
488
523
|
})
|
|
489
|
-
), i = await
|
|
490
|
-
depositorBtcPubkey: dt(
|
|
524
|
+
), i = await oe(this.config.btcWallet, {
|
|
525
|
+
depositorBtcPubkey: dt(o),
|
|
491
526
|
fundingOutpoints: r
|
|
492
527
|
});
|
|
493
528
|
let a, c;
|
|
494
529
|
try {
|
|
495
|
-
const
|
|
530
|
+
const f = await zt(i);
|
|
496
531
|
try {
|
|
497
|
-
a = Q(
|
|
532
|
+
a = Q(f), c = Q(Bt(f));
|
|
498
533
|
} finally {
|
|
499
|
-
|
|
534
|
+
f.fill(0);
|
|
500
535
|
}
|
|
501
|
-
} catch (
|
|
502
|
-
throw i.fill(0),
|
|
536
|
+
} catch (f) {
|
|
537
|
+
throw i.fill(0), f;
|
|
503
538
|
}
|
|
504
|
-
const
|
|
539
|
+
const u = await je(i, e.amounts.length), { perVaultWotsKeys: l, wotsPkHashes: m, htlcSecretHexes: b, hashlocks: w } = u, d = await this.preparePeginCommit({
|
|
505
540
|
depositorBtcPubkeyRaw: t,
|
|
506
|
-
depositorBtcPubkey:
|
|
507
|
-
hashlocks:
|
|
541
|
+
depositorBtcPubkey: o,
|
|
542
|
+
hashlocks: w,
|
|
508
543
|
authAnchorHash: c,
|
|
509
|
-
sizing:
|
|
544
|
+
sizing: n,
|
|
510
545
|
params: e
|
|
511
546
|
});
|
|
512
|
-
for (let
|
|
513
|
-
if (
|
|
547
|
+
for (let f = 0; f < d.perVault.length; f++)
|
|
548
|
+
if (d.perVault[f].htlcVout !== f)
|
|
514
549
|
throw new Error(
|
|
515
|
-
`Internal invariant violation: htlcVout/index mismatch at vault ${
|
|
550
|
+
`Internal invariant violation: htlcVout/index mismatch at vault ${f} (expected ${f}, got ${d.perVault[f].htlcVout})`
|
|
516
551
|
);
|
|
517
|
-
|
|
518
|
-
|
|
552
|
+
re(
|
|
553
|
+
d.fundedPrePeginTxHex,
|
|
519
554
|
e.amounts.length,
|
|
520
555
|
c
|
|
521
|
-
)
|
|
556
|
+
);
|
|
557
|
+
const { depositTerms: E, ...k } = d;
|
|
558
|
+
return {
|
|
522
559
|
transaction: {
|
|
523
|
-
...
|
|
524
|
-
selectedUTXOs:
|
|
525
|
-
fee:
|
|
526
|
-
changeAmount:
|
|
560
|
+
...k,
|
|
561
|
+
selectedUTXOs: n.selectedUTXOs,
|
|
562
|
+
fee: n.fee,
|
|
563
|
+
changeAmount: n.changeAmount
|
|
527
564
|
},
|
|
528
|
-
depositorBtcPubkey:
|
|
565
|
+
depositorBtcPubkey: o,
|
|
529
566
|
derivedSecrets: {
|
|
530
567
|
perVaultWotsKeys: l,
|
|
531
568
|
wotsPkHashes: m,
|
|
532
|
-
htlcSecretHexes:
|
|
569
|
+
htlcSecretHexes: b,
|
|
533
570
|
authAnchorHex: a
|
|
534
|
-
}
|
|
571
|
+
},
|
|
572
|
+
depositTerms: E
|
|
535
573
|
};
|
|
536
574
|
}
|
|
537
575
|
/**
|
|
@@ -551,29 +589,29 @@ class ks {
|
|
|
551
589
|
* content), so UTXO selection is invariant under substitution.
|
|
552
590
|
*/
|
|
553
591
|
async prepareSizing(e, t) {
|
|
554
|
-
const
|
|
592
|
+
const o = t.amounts.map(
|
|
555
593
|
() => z
|
|
556
|
-
),
|
|
594
|
+
), n = t.vaultKeeperBtcPubkeys.length, r = await lt({
|
|
557
595
|
vaultCoreVersion: t.vaultCoreVersion,
|
|
558
596
|
depositorPubkey: e,
|
|
559
|
-
vaultProviderPubkey:
|
|
560
|
-
vaultKeeperPubkeys: t.vaultKeeperBtcPubkeys.map(
|
|
561
|
-
universalChallengerPubkeys: t.universalChallengerBtcPubkeys.map(
|
|
562
|
-
hashlocks:
|
|
597
|
+
vaultProviderPubkey: v(t.vaultProviderBtcPubkey),
|
|
598
|
+
vaultKeeperPubkeys: t.vaultKeeperBtcPubkeys.map(v),
|
|
599
|
+
universalChallengerPubkeys: t.universalChallengerBtcPubkeys.map(v),
|
|
600
|
+
hashlocks: o,
|
|
563
601
|
timelockRefund: t.timelockRefund,
|
|
564
602
|
pegInAmounts: t.amounts,
|
|
565
603
|
feeRate: t.protocolFeeRate,
|
|
566
604
|
minPeginFeeRate: t.minPeginFeeRate,
|
|
567
|
-
numLocalChallengers:
|
|
605
|
+
numLocalChallengers: n,
|
|
568
606
|
councilQuorum: t.councilQuorum,
|
|
569
607
|
councilSize: t.councilSize,
|
|
570
608
|
network: this.config.btcNetwork,
|
|
571
609
|
authAnchorHash: z
|
|
572
|
-
}), i =
|
|
610
|
+
}), i = ce(
|
|
573
611
|
[...t.availableUTXOs],
|
|
574
612
|
r.totalOutputValue,
|
|
575
613
|
t.mempoolFeeRate,
|
|
576
|
-
|
|
614
|
+
ue(r.htlcValues.length, !0)
|
|
577
615
|
);
|
|
578
616
|
return {
|
|
579
617
|
selectedUTXOs: i.selectedUTXOs,
|
|
@@ -585,104 +623,125 @@ class ks {
|
|
|
585
623
|
async preparePeginCommit(e) {
|
|
586
624
|
const {
|
|
587
625
|
depositorBtcPubkeyRaw: t,
|
|
588
|
-
depositorBtcPubkey:
|
|
589
|
-
hashlocks:
|
|
626
|
+
depositorBtcPubkey: o,
|
|
627
|
+
hashlocks: n,
|
|
590
628
|
authAnchorHash: r,
|
|
591
629
|
sizing: i,
|
|
592
630
|
params: a
|
|
593
631
|
} = e, c = z.toLowerCase();
|
|
594
|
-
for (let
|
|
595
|
-
if (
|
|
632
|
+
for (let g = 0; g < n.length; g++)
|
|
633
|
+
if (n[g].toLowerCase() === c)
|
|
596
634
|
throw new Error(
|
|
597
|
-
`preparePeginCommit refusing to build with sizing-pass placeholder hashlock at vault ${
|
|
635
|
+
`preparePeginCommit refusing to build with sizing-pass placeholder hashlock at vault ${g} — internal substitution bug`
|
|
598
636
|
);
|
|
599
637
|
if (r.toLowerCase() === c)
|
|
600
638
|
throw new Error(
|
|
601
639
|
"preparePeginCommit refusing to build with sizing-pass placeholder auth-anchor hash — internal substitution bug"
|
|
602
640
|
);
|
|
603
|
-
const
|
|
641
|
+
const u = v(
|
|
604
642
|
a.vaultProviderBtcPubkey
|
|
605
|
-
), l = a.vaultKeeperBtcPubkeys.map(
|
|
643
|
+
), l = a.vaultKeeperBtcPubkeys.map(v), m = a.universalChallengerBtcPubkeys.map(v), b = l.length, w = {
|
|
606
644
|
vaultCoreVersion: a.vaultCoreVersion,
|
|
607
|
-
depositorPubkey:
|
|
608
|
-
vaultProviderPubkey:
|
|
645
|
+
depositorPubkey: o,
|
|
646
|
+
vaultProviderPubkey: u,
|
|
609
647
|
vaultKeeperPubkeys: l,
|
|
610
648
|
universalChallengerPubkeys: m,
|
|
611
|
-
hashlocks:
|
|
649
|
+
hashlocks: n,
|
|
612
650
|
timelockRefund: a.timelockRefund,
|
|
613
651
|
pegInAmounts: a.amounts,
|
|
614
652
|
feeRate: a.protocolFeeRate,
|
|
615
653
|
minPeginFeeRate: a.minPeginFeeRate,
|
|
616
|
-
numLocalChallengers:
|
|
654
|
+
numLocalChallengers: b,
|
|
617
655
|
councilQuorum: a.councilQuorum,
|
|
618
656
|
councilSize: a.councilSize,
|
|
619
657
|
network: this.config.btcNetwork,
|
|
620
658
|
authAnchorHash: r
|
|
621
|
-
},
|
|
622
|
-
unfundedTxHex:
|
|
659
|
+
}, d = await lt(w), E = gt(this.config.btcNetwork), k = he({
|
|
660
|
+
unfundedTxHex: d.psbtHex,
|
|
623
661
|
selectedUTXOs: i.selectedUTXOs,
|
|
624
662
|
changeAddress: a.changeAddress,
|
|
625
663
|
changeAmount: i.changeAmount,
|
|
626
|
-
network:
|
|
627
|
-
}),
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
664
|
+
network: E
|
|
665
|
+
}), f = i.selectedUTXOs.reduce((g, B) => g + BigInt(B.value), 0n) - d.totalOutputValue - i.changeAmount;
|
|
666
|
+
if (f !== i.fee)
|
|
667
|
+
throw new Error(
|
|
668
|
+
`Pre-PegIn funded fee ${f} does not match the sizing-pass fee ${i.fee}; refusing to publish a deposit-terms fee bound the funded transaction does not pay.`
|
|
669
|
+
);
|
|
670
|
+
const _ = v(
|
|
671
|
+
X(k)
|
|
672
|
+
), p = [], P = [], T = [];
|
|
673
|
+
for (let g = 0; g < n.length; g++) {
|
|
674
|
+
const B = await Yt({
|
|
675
|
+
prePeginParams: w,
|
|
633
676
|
timelockPegin: a.timelockPegin,
|
|
634
677
|
fundedPrePeginTxHex: k,
|
|
635
|
-
htlcVout:
|
|
636
|
-
}),
|
|
678
|
+
htlcVout: g
|
|
679
|
+
}), C = await Qt({
|
|
637
680
|
vaultCoreVersion: a.vaultCoreVersion,
|
|
638
|
-
peginTxHex:
|
|
681
|
+
peginTxHex: B.txHex,
|
|
639
682
|
fundedPrePeginTxHex: k,
|
|
640
|
-
depositorPubkey:
|
|
641
|
-
vaultProviderPubkey:
|
|
683
|
+
depositorPubkey: o,
|
|
684
|
+
vaultProviderPubkey: u,
|
|
642
685
|
vaultKeeperPubkeys: l,
|
|
643
686
|
universalChallengerPubkeys: m,
|
|
644
|
-
hashlock:
|
|
687
|
+
hashlock: n[g],
|
|
645
688
|
timelockRefund: a.timelockRefund,
|
|
646
689
|
network: this.config.btcNetwork
|
|
647
690
|
});
|
|
648
|
-
|
|
649
|
-
|
|
691
|
+
p.push(B), P.push(C.psbtHex), T.push(
|
|
692
|
+
se(t, 1)
|
|
650
693
|
);
|
|
651
694
|
}
|
|
652
|
-
const
|
|
695
|
+
const h = Te({
|
|
696
|
+
protocolFeeRate: a.protocolFeeRate,
|
|
697
|
+
timelockPegin: a.timelockPegin,
|
|
698
|
+
timelockRefund: a.timelockRefund,
|
|
699
|
+
prepeginTxid: _,
|
|
700
|
+
prepeginMaxFee: i.fee,
|
|
701
|
+
vaultProviderPk: u,
|
|
702
|
+
keeperPks: l,
|
|
703
|
+
challengerPks: m,
|
|
704
|
+
commissionBps: a.commissionBps,
|
|
705
|
+
vaultAmounts: a.amounts,
|
|
706
|
+
depositorClaimValue: d.depositorClaimValue,
|
|
707
|
+
peginMaxFee: d.minPeginFee
|
|
708
|
+
});
|
|
709
|
+
ie(this.config.btcWallet) && await this.config.btcWallet.approveDepositTerms(h);
|
|
710
|
+
const x = await ae(
|
|
653
711
|
this.config.btcWallet,
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
),
|
|
657
|
-
for (let
|
|
712
|
+
P,
|
|
713
|
+
T
|
|
714
|
+
), I = [];
|
|
715
|
+
for (let g = 0; g < x.length; g++) {
|
|
658
716
|
ut({
|
|
659
|
-
requestedPsbtHex:
|
|
660
|
-
returnedPsbtHex:
|
|
717
|
+
requestedPsbtHex: P[g],
|
|
718
|
+
returnedPsbtHex: x[g]
|
|
661
719
|
});
|
|
662
|
-
const
|
|
663
|
-
|
|
664
|
-
|
|
720
|
+
const B = Zt(
|
|
721
|
+
x[g],
|
|
722
|
+
o
|
|
665
723
|
);
|
|
666
|
-
|
|
667
|
-
requestedPsbtHex:
|
|
668
|
-
signatureHex:
|
|
669
|
-
signerXOnlyPubkeyHex:
|
|
724
|
+
jt({
|
|
725
|
+
requestedPsbtHex: P[g],
|
|
726
|
+
signatureHex: B,
|
|
727
|
+
signerXOnlyPubkeyHex: o,
|
|
670
728
|
inputIndex: 0
|
|
671
729
|
});
|
|
672
|
-
const
|
|
673
|
-
|
|
674
|
-
htlcVout:
|
|
675
|
-
htlcValue:
|
|
676
|
-
peginTxHex:
|
|
677
|
-
peginTxid:
|
|
678
|
-
peginInputSignature:
|
|
679
|
-
vaultScriptPubKey:
|
|
730
|
+
const C = Jt(x[g]);
|
|
731
|
+
I.push({
|
|
732
|
+
htlcVout: g,
|
|
733
|
+
htlcValue: d.htlcValues[g],
|
|
734
|
+
peginTxHex: C,
|
|
735
|
+
peginTxid: p[g].txid,
|
|
736
|
+
peginInputSignature: B,
|
|
737
|
+
vaultScriptPubKey: p[g].vaultScriptPubKey
|
|
680
738
|
});
|
|
681
739
|
}
|
|
682
740
|
return {
|
|
683
741
|
fundedPrePeginTxHex: k,
|
|
684
|
-
prePeginTxid:
|
|
685
|
-
perVault:
|
|
742
|
+
prePeginTxid: _,
|
|
743
|
+
perVault: I,
|
|
744
|
+
depositTerms: h
|
|
686
745
|
};
|
|
687
746
|
}
|
|
688
747
|
/**
|
|
@@ -701,40 +760,40 @@ class ks {
|
|
|
701
760
|
* @throws Error if signing or broadcasting fails
|
|
702
761
|
*/
|
|
703
762
|
async signAndBroadcast(e) {
|
|
704
|
-
const { fundedPrePeginTxHex: t, depositorBtcPubkey:
|
|
763
|
+
const { fundedPrePeginTxHex: t, depositorBtcPubkey: o } = e, n = t.startsWith("0x") ? t.slice(2) : t, r = Xt.fromHex(n);
|
|
705
764
|
if (r.ins.length === 0)
|
|
706
765
|
throw new Error("Transaction has no inputs");
|
|
707
766
|
const i = new it();
|
|
708
767
|
i.setVersion(r.version), i.setLocktime(r.locktime);
|
|
709
768
|
const a = Y.from(
|
|
710
|
-
|
|
769
|
+
V(o),
|
|
711
770
|
"hex"
|
|
712
|
-
), c = this.config.mempoolApiUrl,
|
|
713
|
-
const
|
|
714
|
-
return
|
|
715
|
-
(
|
|
771
|
+
), c = this.config.mempoolApiUrl, u = r.ins.map((p) => {
|
|
772
|
+
const P = Y.from(p.hash).reverse().toString("hex"), T = p.index;
|
|
773
|
+
return ss(P, T, e.localPrevouts, c).then(
|
|
774
|
+
(h) => ({ input: p, utxoData: h, txid: P, vout: T })
|
|
716
775
|
);
|
|
717
|
-
}), l = await Promise.all(
|
|
718
|
-
(p,
|
|
776
|
+
}), l = await Promise.all(u), m = l.reduce(
|
|
777
|
+
(p, P) => p + BigInt(P.utxoData.value),
|
|
719
778
|
0n
|
|
720
|
-
),
|
|
721
|
-
(p,
|
|
779
|
+
), b = r.outs.reduce(
|
|
780
|
+
(p, P) => p + BigInt(P.value),
|
|
722
781
|
0n
|
|
723
782
|
);
|
|
724
|
-
if (m <
|
|
783
|
+
if (m < b)
|
|
725
784
|
throw new Error(
|
|
726
|
-
`UTXO value mismatch: total input value (${m} sat) is less than total output value (${
|
|
785
|
+
`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.`
|
|
727
786
|
);
|
|
728
|
-
const
|
|
729
|
-
if (
|
|
787
|
+
const w = m - b;
|
|
788
|
+
if (w > ft)
|
|
730
789
|
throw new Error(
|
|
731
|
-
`Implied transaction fee (${
|
|
790
|
+
`Implied transaction fee (${w} sat) exceeds maximum reasonable fee (${ft} sat). This may indicate manipulated UTXO data.`
|
|
732
791
|
);
|
|
733
|
-
for (const { input: p, utxoData:
|
|
734
|
-
const
|
|
792
|
+
for (const { input: p, utxoData: P, txid: T, vout: h } of l) {
|
|
793
|
+
const x = le(
|
|
735
794
|
{
|
|
736
|
-
value:
|
|
737
|
-
scriptPubKey:
|
|
795
|
+
value: P.value,
|
|
796
|
+
scriptPubKey: P.scriptPubKey
|
|
738
797
|
},
|
|
739
798
|
a
|
|
740
799
|
);
|
|
@@ -742,7 +801,7 @@ class ks {
|
|
|
742
801
|
hash: p.hash,
|
|
743
802
|
index: p.index,
|
|
744
803
|
sequence: p.sequence,
|
|
745
|
-
...
|
|
804
|
+
...x
|
|
746
805
|
});
|
|
747
806
|
}
|
|
748
807
|
for (const p of r.outs)
|
|
@@ -750,24 +809,24 @@ class ks {
|
|
|
750
809
|
script: p.script,
|
|
751
810
|
value: p.value
|
|
752
811
|
});
|
|
753
|
-
const
|
|
812
|
+
const d = i.toHex(), E = await this.config.btcWallet.signPsbt(d);
|
|
754
813
|
ut({
|
|
755
|
-
requestedPsbtHex:
|
|
756
|
-
returnedPsbtHex:
|
|
814
|
+
requestedPsbtHex: d,
|
|
815
|
+
returnedPsbtHex: E
|
|
757
816
|
});
|
|
758
|
-
const k = it.fromHex(
|
|
817
|
+
const k = it.fromHex(E);
|
|
759
818
|
try {
|
|
760
819
|
k.finalizeAllInputs();
|
|
761
820
|
} catch (p) {
|
|
762
821
|
if (!k.data.inputs.every(
|
|
763
|
-
(
|
|
822
|
+
(T) => T.finalScriptWitness || T.finalScriptSig
|
|
764
823
|
))
|
|
765
824
|
throw new Error(
|
|
766
825
|
`PSBT finalization failed and wallet did not auto-finalize: ${p}`
|
|
767
826
|
);
|
|
768
827
|
}
|
|
769
|
-
const
|
|
770
|
-
return await
|
|
828
|
+
const f = k.extractTransaction().toHex();
|
|
829
|
+
return await ge(f, c);
|
|
771
830
|
}
|
|
772
831
|
/**
|
|
773
832
|
* Registers a peg-in on Ethereum by calling the BTCVaultRegistry contract.
|
|
@@ -796,107 +855,107 @@ class ks {
|
|
|
796
855
|
async registerPeginOnChain(e) {
|
|
797
856
|
const {
|
|
798
857
|
unsignedPrePeginTx: t,
|
|
799
|
-
depositorSignedPeginTx:
|
|
800
|
-
vaultProvider:
|
|
858
|
+
depositorSignedPeginTx: o,
|
|
859
|
+
vaultProvider: n,
|
|
801
860
|
hashlock: r,
|
|
802
861
|
htlcVout: i,
|
|
803
862
|
depositorPayoutBtcAddress: a,
|
|
804
863
|
depositorWotsPkHash: c,
|
|
805
|
-
popSignature:
|
|
864
|
+
popSignature: u
|
|
806
865
|
} = e;
|
|
807
866
|
if (!this.config.ethWallet.account)
|
|
808
867
|
throw new Error("Ethereum wallet account not found");
|
|
809
868
|
const l = this.config.ethWallet.account.address;
|
|
810
|
-
if (!at(
|
|
869
|
+
if (!at(u.depositorEthAddress, l))
|
|
811
870
|
throw new Error(
|
|
812
|
-
`Proof of possession was signed for ${
|
|
871
|
+
`Proof of possession was signed for ${u.depositorEthAddress} but the Ethereum wallet is currently connected to ${l}. Reconnect the original account or call signProofOfPossession() again.`
|
|
813
872
|
);
|
|
814
|
-
const m = await this.assertPopMatchesBtcWallet(
|
|
815
|
-
|
|
816
|
-
),
|
|
873
|
+
const m = await this.assertPopMatchesBtcWallet(u), b = u.btcPopSignature, w = A(
|
|
874
|
+
u.depositorBtcPubkey
|
|
875
|
+
), d = A(t), E = A(o), k = a ?? await this.config.btcWallet.getAddress(), f = this.resolvePayoutScriptPubKey(
|
|
817
876
|
m,
|
|
818
877
|
k
|
|
819
|
-
),
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
),
|
|
823
|
-
if (await this.checkVaultExists(
|
|
878
|
+
), _ = X(E), p = await ct(
|
|
879
|
+
v(_),
|
|
880
|
+
v(l)
|
|
881
|
+
), P = A(p);
|
|
882
|
+
if (await this.checkVaultExists(P))
|
|
824
883
|
throw new Error(
|
|
825
|
-
`Vault already exists (ID: ${
|
|
884
|
+
`Vault already exists (ID: ${P}, peginTxHash: ${_}). 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.`
|
|
826
885
|
);
|
|
827
|
-
const
|
|
828
|
-
let
|
|
886
|
+
const h = this.config.publicClient;
|
|
887
|
+
let x;
|
|
829
888
|
try {
|
|
830
|
-
|
|
889
|
+
x = await h.readContract({
|
|
831
890
|
address: this.config.vaultContracts.btcVaultRegistry,
|
|
832
|
-
abi:
|
|
891
|
+
abi: H,
|
|
833
892
|
functionName: "getPegInFee",
|
|
834
|
-
args: [
|
|
893
|
+
args: [n]
|
|
835
894
|
});
|
|
836
|
-
} catch (
|
|
895
|
+
} catch (O) {
|
|
837
896
|
throw new Error(
|
|
838
897
|
"Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct.",
|
|
839
|
-
{ cause:
|
|
898
|
+
{ cause: O }
|
|
840
899
|
);
|
|
841
900
|
}
|
|
842
|
-
const
|
|
843
|
-
|
|
901
|
+
const I = await this.resolveMaxAcceptableCommissionBps(
|
|
902
|
+
n,
|
|
844
903
|
e.quotedCommissionBps
|
|
845
|
-
),
|
|
846
|
-
abi:
|
|
904
|
+
), g = j({
|
|
905
|
+
abi: H,
|
|
847
906
|
functionName: "submitPeginRequest",
|
|
848
907
|
args: [
|
|
849
908
|
l,
|
|
909
|
+
w,
|
|
850
910
|
b,
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
_,
|
|
911
|
+
d,
|
|
912
|
+
E,
|
|
913
|
+
n,
|
|
914
|
+
I,
|
|
856
915
|
r,
|
|
857
916
|
i,
|
|
858
|
-
|
|
917
|
+
f,
|
|
859
918
|
c
|
|
860
919
|
]
|
|
861
920
|
});
|
|
862
|
-
let
|
|
921
|
+
let B;
|
|
863
922
|
try {
|
|
864
|
-
|
|
923
|
+
B = await h.estimateGas({
|
|
865
924
|
to: this.config.vaultContracts.btcVaultRegistry,
|
|
866
|
-
data:
|
|
867
|
-
value:
|
|
925
|
+
data: g,
|
|
926
|
+
value: x,
|
|
868
927
|
account: this.config.ethWallet.account.address
|
|
869
928
|
});
|
|
870
|
-
} catch (
|
|
871
|
-
|
|
929
|
+
} catch (O) {
|
|
930
|
+
R(O);
|
|
872
931
|
}
|
|
873
|
-
let
|
|
932
|
+
let C;
|
|
874
933
|
try {
|
|
875
|
-
|
|
934
|
+
C = await this.config.ethWallet.sendTransaction({
|
|
876
935
|
to: this.config.vaultContracts.btcVaultRegistry,
|
|
877
|
-
data:
|
|
878
|
-
value:
|
|
936
|
+
data: g,
|
|
937
|
+
value: x,
|
|
879
938
|
account: this.config.ethWallet.account,
|
|
880
939
|
chain: this.config.ethChain,
|
|
881
|
-
gas:
|
|
940
|
+
gas: B
|
|
882
941
|
});
|
|
883
|
-
} catch (
|
|
884
|
-
|
|
942
|
+
} catch (O) {
|
|
943
|
+
R(O);
|
|
885
944
|
}
|
|
886
|
-
const
|
|
887
|
-
publicClient:
|
|
945
|
+
const D = await pt({
|
|
946
|
+
publicClient: h,
|
|
888
947
|
walletAddress: this.config.ethWallet.account.address,
|
|
889
|
-
hash:
|
|
890
|
-
timeout:
|
|
948
|
+
hash: C,
|
|
949
|
+
timeout: kt
|
|
891
950
|
});
|
|
892
|
-
return
|
|
951
|
+
return D.status === "reverted" && R(
|
|
893
952
|
new Error(
|
|
894
|
-
`Transaction reverted. Hash: ${
|
|
953
|
+
`Transaction reverted. Hash: ${D.transactionHash}. Check the transaction on block explorer for details.`
|
|
895
954
|
)
|
|
896
955
|
), {
|
|
897
|
-
ethTxHash:
|
|
898
|
-
vaultId:
|
|
899
|
-
peginTxHash:
|
|
956
|
+
ethTxHash: D.transactionHash,
|
|
957
|
+
vaultId: P,
|
|
958
|
+
peginTxHash: _
|
|
900
959
|
};
|
|
901
960
|
}
|
|
902
961
|
/**
|
|
@@ -910,8 +969,8 @@ class ks {
|
|
|
910
969
|
* @returns Batch result with per-vault IDs and single ETH tx hash
|
|
911
970
|
*/
|
|
912
971
|
async registerPeginBatchOnChain(e) {
|
|
913
|
-
const { vaultProvider: t, unsignedPrePeginTx:
|
|
914
|
-
if (
|
|
972
|
+
const { vaultProvider: t, unsignedPrePeginTx: o, requests: n, popSignature: r } = e;
|
|
973
|
+
if (n.length === 0)
|
|
915
974
|
throw new Error("Batch pegin requires at least one request");
|
|
916
975
|
if (!this.config.ethWallet.account)
|
|
917
976
|
throw new Error("Ethereum wallet account not found");
|
|
@@ -920,125 +979,125 @@ class ks {
|
|
|
920
979
|
throw new Error(
|
|
921
980
|
`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.`
|
|
922
981
|
);
|
|
923
|
-
const a = await this.assertPopMatchesBtcWallet(r), c = r.btcPopSignature,
|
|
924
|
-
(
|
|
982
|
+
const a = await this.assertPopMatchesBtcWallet(r), c = r.btcPopSignature, u = n.map(
|
|
983
|
+
(h) => this.resolvePayoutScriptPubKey(
|
|
925
984
|
a,
|
|
926
|
-
|
|
985
|
+
h.depositorPayoutBtcAddress
|
|
927
986
|
)
|
|
928
987
|
), l = [];
|
|
929
|
-
for (const
|
|
930
|
-
const
|
|
931
|
-
|
|
932
|
-
),
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
),
|
|
936
|
-
if (await this.checkVaultExists(
|
|
988
|
+
for (const h of n) {
|
|
989
|
+
const x = A(
|
|
990
|
+
h.depositorSignedPeginTx
|
|
991
|
+
), I = X(x), g = await ct(
|
|
992
|
+
v(I),
|
|
993
|
+
v(i)
|
|
994
|
+
), B = A(g);
|
|
995
|
+
if (await this.checkVaultExists(B))
|
|
937
996
|
throw new Error(
|
|
938
|
-
`Vault already exists (ID: ${
|
|
997
|
+
`Vault already exists (ID: ${B}, peginTxHash: ${I}). To create a new vault, use different UTXOs or a different amount.`
|
|
939
998
|
);
|
|
940
|
-
l.push({ vaultId:
|
|
999
|
+
l.push({ vaultId: B, peginTxHash: I });
|
|
941
1000
|
}
|
|
942
1001
|
const m = this.config.publicClient;
|
|
943
|
-
let
|
|
1002
|
+
let b;
|
|
944
1003
|
try {
|
|
945
|
-
|
|
1004
|
+
b = await m.readContract({
|
|
946
1005
|
address: this.config.vaultContracts.btcVaultRegistry,
|
|
947
|
-
abi:
|
|
1006
|
+
abi: H,
|
|
948
1007
|
functionName: "getPegInFee",
|
|
949
1008
|
args: [t]
|
|
950
1009
|
});
|
|
951
|
-
} catch (
|
|
1010
|
+
} catch (h) {
|
|
952
1011
|
throw new Error(
|
|
953
1012
|
"Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct.",
|
|
954
|
-
{ cause:
|
|
1013
|
+
{ cause: h }
|
|
955
1014
|
);
|
|
956
1015
|
}
|
|
957
|
-
const
|
|
1016
|
+
const w = b * BigInt(n.length), d = await this.resolveMaxAcceptableCommissionBps(
|
|
958
1017
|
t,
|
|
959
1018
|
e.quotedCommissionBps
|
|
960
|
-
),
|
|
1019
|
+
), E = A(
|
|
961
1020
|
r.depositorBtcPubkey
|
|
962
|
-
), k =
|
|
963
|
-
depositorBtcPubKey:
|
|
1021
|
+
), k = A(o), f = n.map((h, x) => ({
|
|
1022
|
+
depositorBtcPubKey: E,
|
|
964
1023
|
btcPopSignature: c,
|
|
965
1024
|
unsignedPrePeginTx: k,
|
|
966
|
-
depositorSignedPeginTx:
|
|
967
|
-
|
|
1025
|
+
depositorSignedPeginTx: A(
|
|
1026
|
+
h.depositorSignedPeginTx
|
|
968
1027
|
),
|
|
969
|
-
hashlock:
|
|
970
|
-
htlcVout:
|
|
971
|
-
referralCode:
|
|
972
|
-
depositorPayoutBtcAddress:
|
|
973
|
-
depositorWotsPkHash:
|
|
974
|
-
})),
|
|
975
|
-
abi:
|
|
1028
|
+
hashlock: h.hashlock,
|
|
1029
|
+
htlcVout: h.htlcVout,
|
|
1030
|
+
referralCode: Mt,
|
|
1031
|
+
depositorPayoutBtcAddress: u[x],
|
|
1032
|
+
depositorWotsPkHash: h.depositorWotsPkHash
|
|
1033
|
+
})), _ = j({
|
|
1034
|
+
abi: H,
|
|
976
1035
|
functionName: "submitPeginRequestBatch",
|
|
977
1036
|
args: [
|
|
978
1037
|
i,
|
|
979
1038
|
t,
|
|
980
|
-
|
|
981
|
-
|
|
1039
|
+
d,
|
|
1040
|
+
f
|
|
982
1041
|
]
|
|
983
1042
|
});
|
|
984
1043
|
let p;
|
|
985
1044
|
try {
|
|
986
1045
|
p = await m.estimateGas({
|
|
987
1046
|
to: this.config.vaultContracts.btcVaultRegistry,
|
|
988
|
-
data:
|
|
989
|
-
value:
|
|
1047
|
+
data: _,
|
|
1048
|
+
value: w,
|
|
990
1049
|
account: this.config.ethWallet.account.address
|
|
991
1050
|
});
|
|
992
|
-
} catch (
|
|
993
|
-
|
|
1051
|
+
} catch (h) {
|
|
1052
|
+
R(h);
|
|
994
1053
|
}
|
|
995
|
-
let
|
|
1054
|
+
let P;
|
|
996
1055
|
try {
|
|
997
|
-
|
|
1056
|
+
P = await this.config.ethWallet.sendTransaction({
|
|
998
1057
|
to: this.config.vaultContracts.btcVaultRegistry,
|
|
999
|
-
data:
|
|
1000
|
-
value:
|
|
1058
|
+
data: _,
|
|
1059
|
+
value: w,
|
|
1001
1060
|
account: this.config.ethWallet.account,
|
|
1002
1061
|
chain: this.config.ethChain,
|
|
1003
1062
|
gas: p
|
|
1004
1063
|
});
|
|
1005
|
-
} catch (
|
|
1006
|
-
|
|
1064
|
+
} catch (h) {
|
|
1065
|
+
R(h);
|
|
1007
1066
|
}
|
|
1008
|
-
const
|
|
1067
|
+
const T = await pt({
|
|
1009
1068
|
publicClient: m,
|
|
1010
1069
|
walletAddress: this.config.ethWallet.account.address,
|
|
1011
|
-
hash:
|
|
1012
|
-
timeout:
|
|
1070
|
+
hash: P,
|
|
1071
|
+
timeout: kt
|
|
1013
1072
|
});
|
|
1014
|
-
return
|
|
1073
|
+
return T.status === "reverted" && R(
|
|
1015
1074
|
new Error(
|
|
1016
|
-
`Batch transaction reverted. Hash: ${
|
|
1075
|
+
`Batch transaction reverted. Hash: ${T.transactionHash}. Check the transaction on block explorer for details.`
|
|
1017
1076
|
)
|
|
1018
1077
|
), {
|
|
1019
|
-
ethTxHash:
|
|
1078
|
+
ethTxHash: T.transactionHash,
|
|
1020
1079
|
vaults: l
|
|
1021
1080
|
};
|
|
1022
1081
|
}
|
|
1023
1082
|
// Anchor to quoted+headroom when supplied (refuse if chain drifted past it);
|
|
1024
1083
|
// otherwise fall back to chain-current+headroom — see #1691.
|
|
1025
1084
|
async resolveMaxAcceptableCommissionBps(e, t) {
|
|
1026
|
-
let
|
|
1085
|
+
let o;
|
|
1027
1086
|
try {
|
|
1028
|
-
|
|
1087
|
+
o = await new fe(
|
|
1029
1088
|
this.config.publicClient,
|
|
1030
1089
|
this.config.vaultContracts.btcVaultRegistry
|
|
1031
1090
|
).getVaultProviderCommission(e);
|
|
1032
|
-
} catch (
|
|
1091
|
+
} catch (n) {
|
|
1033
1092
|
throw new Error(
|
|
1034
1093
|
"Failed to query vault provider commission from the contract. Please check your network connection and that the contract address is correct.",
|
|
1035
|
-
{ cause:
|
|
1094
|
+
{ cause: n }
|
|
1036
1095
|
);
|
|
1037
1096
|
}
|
|
1038
1097
|
if (t !== void 0) {
|
|
1039
|
-
if (
|
|
1098
|
+
if (o > t + W)
|
|
1040
1099
|
throw new Error(
|
|
1041
|
-
`Vault provider commission changed since quote: quoted ${t} bps, chain currently reports ${
|
|
1100
|
+
`Vault provider commission changed since quote: quoted ${t} bps, chain currently reports ${o} bps (allowed drift ${W} bps). Please refresh to see the new commission and try again.`
|
|
1042
1101
|
);
|
|
1043
1102
|
return Math.min(
|
|
1044
1103
|
t + W,
|
|
@@ -1046,7 +1105,7 @@ class ks {
|
|
|
1046
1105
|
);
|
|
1047
1106
|
}
|
|
1048
1107
|
return Math.min(
|
|
1049
|
-
|
|
1108
|
+
o + W,
|
|
1050
1109
|
J
|
|
1051
1110
|
);
|
|
1052
1111
|
}
|
|
@@ -1066,7 +1125,7 @@ class ks {
|
|
|
1066
1125
|
async checkVaultExists(e) {
|
|
1067
1126
|
return (await this.config.publicClient.readContract({
|
|
1068
1127
|
address: this.config.vaultContracts.btcVaultRegistry,
|
|
1069
|
-
abi:
|
|
1128
|
+
abi: H,
|
|
1070
1129
|
functionName: "getBtcVaultBasicInfo",
|
|
1071
1130
|
args: [e]
|
|
1072
1131
|
})).depositor !== Kt;
|
|
@@ -1093,14 +1152,14 @@ class ks {
|
|
|
1093
1152
|
e,
|
|
1094
1153
|
this.config.btcNetwork
|
|
1095
1154
|
))
|
|
1096
|
-
throw
|
|
1155
|
+
throw v(e).length === ee && es(t, this.config.btcNetwork) ? new Error(
|
|
1097
1156
|
`BTC payout address "${t}" is a P2WPKH (Native SegWit) address, but the connected wallet only exposes an x-only public key. P2WPKH validation requires a compressed key with known y-parity. Use a P2TR (Taproot) payout address instead.`
|
|
1098
1157
|
) : new Error(
|
|
1099
1158
|
`BTC payout address "${t}" 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.`
|
|
1100
1159
|
);
|
|
1101
|
-
const
|
|
1160
|
+
const o = gt(this.config.btcNetwork);
|
|
1102
1161
|
try {
|
|
1103
|
-
return `0x${Et.address.toOutputScript(t,
|
|
1162
|
+
return `0x${Et.address.toOutputScript(t, o).toString("hex")}`;
|
|
1104
1163
|
} catch {
|
|
1105
1164
|
throw new Error(
|
|
1106
1165
|
`Invalid BTC payout address: "${t}". Please provide a valid Bitcoin address for the ${this.config.btcNetwork} network.`
|
|
@@ -1116,14 +1175,14 @@ class ks {
|
|
|
1116
1175
|
async signProofOfPossession() {
|
|
1117
1176
|
if (!this.config.ethWallet.account)
|
|
1118
1177
|
throw new Error("Ethereum wallet account not found");
|
|
1119
|
-
const e = this.config.ethWallet.account.address, t =
|
|
1178
|
+
const e = this.config.ethWallet.account.address, t = V(
|
|
1120
1179
|
await this.config.btcWallet.getPublicKeyHex()
|
|
1121
|
-
),
|
|
1122
|
-
|
|
1180
|
+
), o = this.config.vaultContracts.btcVaultRegistry, n = `${e.toLowerCase()}:${this.config.ethChain.id}:pegin:${o.toLowerCase()}`, r = await this.config.btcWallet.signMessage(
|
|
1181
|
+
n,
|
|
1123
1182
|
"bip322-simple"
|
|
1124
1183
|
);
|
|
1125
1184
|
return {
|
|
1126
|
-
btcPopSignature:
|
|
1185
|
+
btcPopSignature: ts(r),
|
|
1127
1186
|
depositorEthAddress: e,
|
|
1128
1187
|
depositorBtcPubkey: t
|
|
1129
1188
|
};
|
|
@@ -1137,10 +1196,10 @@ class ks {
|
|
|
1137
1196
|
* substitute the opposite-parity P2WPKH address.
|
|
1138
1197
|
*/
|
|
1139
1198
|
async assertPopMatchesBtcWallet(e) {
|
|
1140
|
-
const t = await this.config.btcWallet.getPublicKeyHex(),
|
|
1141
|
-
if (
|
|
1199
|
+
const t = await this.config.btcWallet.getPublicKeyHex(), o = V(t), n = V(e.depositorBtcPubkey);
|
|
1200
|
+
if (o !== n)
|
|
1142
1201
|
throw new Error(
|
|
1143
|
-
`Proof of possession was signed with BTC pubkey ${
|
|
1202
|
+
`Proof of possession was signed with BTC pubkey ${n} but the BTC wallet is currently connected to ${o}. Reconnect the original wallet or call signProofOfPossession() again.`
|
|
1144
1203
|
);
|
|
1145
1204
|
return t;
|
|
1146
1205
|
}
|
|
@@ -1161,34 +1220,34 @@ class ks {
|
|
|
1161
1220
|
return this.config.vaultContracts.btcVaultRegistry;
|
|
1162
1221
|
}
|
|
1163
1222
|
}
|
|
1164
|
-
const
|
|
1165
|
-
function
|
|
1166
|
-
const t =
|
|
1223
|
+
const os = 80, ns = 250, rs = 300, is = 22, Ut = "ab";
|
|
1224
|
+
function as(s, e) {
|
|
1225
|
+
const t = Ut.repeat(rs), o = v(s).toLowerCase(), n = e.toString(16).padStart(8, "0"), r = `${o}${n}`, i = t.slice(r.length);
|
|
1167
1226
|
return `0x${r}${i}`;
|
|
1168
1227
|
}
|
|
1169
|
-
function
|
|
1170
|
-
const t = (
|
|
1228
|
+
function cs(s, e) {
|
|
1229
|
+
const t = (o) => `0x${Ut.repeat(o)}`;
|
|
1171
1230
|
return {
|
|
1172
1231
|
depositorBtcPubKey: t(32),
|
|
1173
|
-
btcPopSignature: t(
|
|
1174
|
-
unsignedPrePeginTx: t(
|
|
1175
|
-
depositorSignedPeginTx:
|
|
1232
|
+
btcPopSignature: t(os),
|
|
1233
|
+
unsignedPrePeginTx: t(ns),
|
|
1234
|
+
depositorSignedPeginTx: as(
|
|
1176
1235
|
s,
|
|
1177
1236
|
e
|
|
1178
1237
|
),
|
|
1179
1238
|
hashlock: t(32),
|
|
1180
1239
|
htlcVout: e,
|
|
1181
|
-
referralCode:
|
|
1182
|
-
depositorPayoutBtcAddress: t(
|
|
1240
|
+
referralCode: Mt,
|
|
1241
|
+
depositorPayoutBtcAddress: t(is),
|
|
1183
1242
|
depositorWotsPkHash: t(32)
|
|
1184
1243
|
};
|
|
1185
1244
|
}
|
|
1186
|
-
async function
|
|
1245
|
+
async function As(s) {
|
|
1187
1246
|
const {
|
|
1188
1247
|
publicClient: e,
|
|
1189
1248
|
btcVaultRegistry: t,
|
|
1190
|
-
depositorEthAddress:
|
|
1191
|
-
vaultProvider:
|
|
1249
|
+
depositorEthAddress: o,
|
|
1250
|
+
vaultProvider: n,
|
|
1192
1251
|
batchSize: r
|
|
1193
1252
|
} = s;
|
|
1194
1253
|
if (r <= 0)
|
|
@@ -1197,33 +1256,34 @@ async function Ts(s) {
|
|
|
1197
1256
|
);
|
|
1198
1257
|
const a = await e.readContract({
|
|
1199
1258
|
address: t,
|
|
1200
|
-
abi:
|
|
1259
|
+
abi: H,
|
|
1201
1260
|
functionName: "getPegInFee",
|
|
1202
|
-
args: [
|
|
1261
|
+
args: [n]
|
|
1203
1262
|
}) * BigInt(r), c = Array.from(
|
|
1204
1263
|
{ length: r },
|
|
1205
|
-
(l, m) =>
|
|
1206
|
-
),
|
|
1207
|
-
abi:
|
|
1264
|
+
(l, m) => cs(o, m)
|
|
1265
|
+
), u = j({
|
|
1266
|
+
abi: H,
|
|
1208
1267
|
functionName: "submitPeginRequestBatch",
|
|
1209
1268
|
args: [
|
|
1210
|
-
n,
|
|
1211
1269
|
o,
|
|
1270
|
+
n,
|
|
1212
1271
|
J,
|
|
1213
1272
|
c
|
|
1214
1273
|
]
|
|
1215
1274
|
});
|
|
1216
1275
|
return e.estimateGas({
|
|
1217
1276
|
to: t,
|
|
1218
|
-
data:
|
|
1277
|
+
data: u,
|
|
1219
1278
|
value: a,
|
|
1220
|
-
account:
|
|
1279
|
+
account: o
|
|
1221
1280
|
});
|
|
1222
1281
|
}
|
|
1223
1282
|
export {
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1283
|
+
Ss as P,
|
|
1284
|
+
Te as b,
|
|
1285
|
+
Ye as c,
|
|
1286
|
+
ze as d,
|
|
1287
|
+
As as e
|
|
1228
1288
|
};
|
|
1229
|
-
//# sourceMappingURL=PeginManager-
|
|
1289
|
+
//# sourceMappingURL=PeginManager-aYBkb-Qx.js.map
|