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