@babylonlabs-io/ts-sdk 0.72.0 → 0.73.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 +56 -13
- package/dist/BTCVaultRegistryKeyEpochs.abi-CrQiTLfA.cjs +2 -0
- package/dist/BTCVaultRegistryKeyEpochs.abi-CrQiTLfA.cjs.map +1 -0
- package/dist/BTCVaultRegistryKeyEpochs.abi-D8cIbMtH.js +229 -0
- package/dist/BTCVaultRegistryKeyEpochs.abi-D8cIbMtH.js.map +1 -0
- package/dist/PeginManager-C9RPOd0L.js +1216 -0
- package/dist/PeginManager-C9RPOd0L.js.map +1 -0
- package/dist/PeginManager-E-UHA1un.cjs +2 -0
- package/dist/PeginManager-E-UHA1un.cjs.map +1 -0
- package/dist/bip322Verify-B7_gIhRk.cjs +2 -0
- package/dist/bip322Verify-B7_gIhRk.cjs.map +1 -0
- package/dist/bip322Verify-D6sBBUHE.js +79 -0
- package/dist/bip322Verify-D6sBBUHE.js.map +1 -0
- package/dist/{buildAndBroadcastReclaim-YvWDRAN8.js → buildAndBroadcastReclaim-BXclIcu_.js} +366 -357
- package/dist/buildAndBroadcastReclaim-BXclIcu_.js.map +1 -0
- package/dist/buildAndBroadcastReclaim-U-PyFfDg.cjs +2 -0
- package/dist/buildAndBroadcastReclaim-U-PyFfDg.cjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +209 -205
- package/dist/index.js.map +1 -1
- package/dist/onChainBtcPubkey-BbxRrbvg.cjs +2 -0
- package/dist/onChainBtcPubkey-BbxRrbvg.cjs.map +1 -0
- package/dist/onChainBtcPubkey-C-6UWpD8.js +34 -0
- package/dist/onChainBtcPubkey-C-6UWpD8.js.map +1 -0
- package/dist/{parseFundingOutpoints-BkgGihaY.cjs → parseFundingOutpoints-B4iQeX6q.cjs} +2 -2
- package/dist/{parseFundingOutpoints-BkgGihaY.cjs.map → parseFundingOutpoints-B4iQeX6q.cjs.map} +1 -1
- package/dist/{parseFundingOutpoints-DlINgQ3X.js → parseFundingOutpoints-B67CoXpF.js} +2 -2
- package/dist/{parseFundingOutpoints-DlINgQ3X.js.map → parseFundingOutpoints-B67CoXpF.js.map} +1 -1
- package/dist/pegin-registration-client-6vLk2itL.cjs +2 -0
- package/dist/pegin-registration-client-6vLk2itL.cjs.map +1 -0
- package/dist/pegin-registration-client-BdRiR8br.js +2635 -0
- package/dist/pegin-registration-client-BdRiR8br.js.map +1 -0
- package/dist/primeVpAuth-DBHohVKM.cjs +2 -0
- package/dist/primeVpAuth-DBHohVKM.cjs.map +1 -0
- package/dist/primeVpAuth-DvaD45q_.js +745 -0
- package/dist/primeVpAuth-DvaD45q_.js.map +1 -0
- package/dist/psbtInputFields-B3e05Xe6.js +171 -0
- package/dist/psbtInputFields-B3e05Xe6.js.map +1 -0
- package/dist/psbtInputFields-BIxBpPj2.cjs +2 -0
- package/dist/psbtInputFields-BIxBpPj2.cjs.map +1 -0
- package/dist/sha2-CrlWM5S8.js +312 -0
- package/dist/{sha2-BYVxyZzX.js.map → sha2-CrlWM5S8.js.map} +1 -1
- package/dist/sha2-DI0-0SHr.cjs +2 -0
- package/dist/{sha2-DsrLC4NM.cjs.map → sha2-DI0-0SHr.cjs.map} +1 -1
- package/dist/{signPsbtsWithFallback-CCFTzE0o.js → signPsbtsWithFallback-0VBALhjS.js} +2 -2
- package/dist/{signPsbtsWithFallback-CCFTzE0o.js.map → signPsbtsWithFallback-0VBALhjS.js.map} +1 -1
- package/dist/{signPsbtsWithFallback-CrkvpSJz.cjs → signPsbtsWithFallback-DMSpIQWH.cjs} +2 -2
- package/dist/{signPsbtsWithFallback-CrkvpSJz.cjs.map → signPsbtsWithFallback-DMSpIQWH.cjs.map} +1 -1
- package/dist/tbv/core/clients/eth/__tests__/eth-import-boundary.test.d.ts +2 -0
- package/dist/tbv/core/clients/eth/__tests__/eth-import-boundary.test.d.ts.map +1 -0
- package/dist/tbv/core/clients/eth/__tests__/onChainBtcPubkey.test.d.ts +2 -0
- package/dist/tbv/core/clients/eth/__tests__/onChainBtcPubkey.test.d.ts.map +1 -0
- package/dist/tbv/core/clients/eth/__tests__/payout-script.test.d.ts +2 -0
- package/dist/tbv/core/clients/eth/__tests__/payout-script.test.d.ts.map +1 -0
- package/dist/tbv/core/clients/eth/__tests__/pegin-registration-calldata.test.d.ts +2 -0
- package/dist/tbv/core/clients/eth/__tests__/pegin-registration-calldata.test.d.ts.map +1 -0
- package/dist/tbv/core/clients/eth/__tests__/pegin-registration-client.test.d.ts +2 -0
- package/dist/tbv/core/clients/eth/__tests__/pegin-registration-client.test.d.ts.map +1 -0
- package/dist/tbv/core/clients/eth/__tests__/pegin-transaction.test.d.ts +2 -0
- package/dist/tbv/core/clients/eth/__tests__/pegin-transaction.test.d.ts.map +1 -0
- package/dist/tbv/core/clients/eth/index.cjs +2 -0
- package/dist/tbv/core/clients/eth/index.cjs.map +1 -0
- package/dist/tbv/core/clients/eth/index.d.ts +4 -0
- package/dist/tbv/core/clients/eth/index.d.ts.map +1 -1
- package/dist/tbv/core/clients/eth/index.js +22 -0
- package/dist/tbv/core/clients/eth/index.js.map +1 -0
- package/dist/tbv/core/clients/eth/onChainBtcPubkey.d.ts.map +1 -1
- package/dist/tbv/core/clients/eth/operation-key-reader.d.ts +1 -1
- package/dist/tbv/core/clients/eth/operation-key-reader.d.ts.map +1 -1
- package/dist/tbv/core/clients/eth/payout-script.d.ts +4 -0
- package/dist/tbv/core/clients/eth/payout-script.d.ts.map +1 -0
- package/dist/tbv/core/clients/eth/pegin-registration-client.d.ts +99 -0
- package/dist/tbv/core/clients/eth/pegin-registration-client.d.ts.map +1 -0
- package/dist/tbv/core/clients/eth/pegin-transaction.d.ts +14 -0
- package/dist/tbv/core/clients/eth/pegin-transaction.d.ts.map +1 -0
- package/dist/tbv/core/clients/eth/protocol-params-reader.d.ts +6 -1
- package/dist/tbv/core/clients/eth/protocol-params-reader.d.ts.map +1 -1
- package/dist/tbv/core/clients/eth/signer-set-reader.d.ts +4 -4
- package/dist/tbv/core/clients/eth/signer-set-reader.d.ts.map +1 -1
- package/dist/tbv/core/clients/eth/types.d.ts +29 -8
- package/dist/tbv/core/clients/eth/types.d.ts.map +1 -1
- package/dist/tbv/core/clients/eth/vault-registry-reader.d.ts +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 +55 -48
- package/dist/tbv/core/clients/index.js.map +1 -1
- package/dist/tbv/core/clients/mempool/index.cjs +2 -0
- package/dist/tbv/core/clients/mempool/index.cjs.map +1 -0
- package/dist/tbv/core/clients/mempool/index.js +222 -0
- package/dist/tbv/core/clients/mempool/index.js.map +1 -0
- package/dist/tbv/core/contracts/index.cjs +1 -1
- package/dist/tbv/core/contracts/index.cjs.map +1 -1
- package/dist/tbv/core/contracts/index.js +491 -12
- package/dist/tbv/core/contracts/index.js.map +1 -1
- 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 +207 -203
- package/dist/tbv/core/index.js.map +1 -1
- package/dist/tbv/core/managers/PeginManager.d.ts +38 -30
- 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.d.ts +2 -1
- package/dist/tbv/core/managers/index.d.ts.map +1 -1
- package/dist/tbv/core/managers/index.js +2 -2
- package/dist/tbv/core/services/deposit/validateOnChainParticipantKeys.d.ts +20 -0
- package/dist/tbv/core/services/deposit/validateOnChainParticipantKeys.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/participants/resolveParticipantKeys.d.ts +8 -0
- package/dist/tbv/core/services/participants/resolveParticipantKeys.d.ts.map +1 -1
- package/dist/tbv/core/utils/eth/index.cjs +2 -0
- package/dist/tbv/core/utils/eth/index.cjs.map +1 -0
- package/dist/tbv/core/utils/eth/index.js +109 -0
- package/dist/tbv/core/utils/eth/index.js.map +1 -0
- package/dist/tbv/core/utils/index.cjs +1 -1
- package/dist/tbv/core/utils/index.js +41 -40
- package/dist/tbv/core/utils/index.js.map +1 -1
- package/dist/tbv/core/vault-secrets/index.cjs +1 -1
- package/dist/tbv/core/vault-secrets/index.js +1 -1
- package/dist/tbv/index.cjs +1 -1
- package/dist/tbv/index.js +207 -203
- package/dist/tbv/index.js.map +1 -1
- package/dist/testing/index.cjs +1 -1
- package/dist/testing/index.js +1 -1
- package/dist/{toRefundInputs-Df1vi23S.js → toRefundInputs-BkcFhvDp.js} +5 -5
- package/dist/{toRefundInputs-Df1vi23S.js.map → toRefundInputs-BkcFhvDp.js.map} +1 -1
- package/dist/{toRefundInputs-CY9u84U9.cjs → toRefundInputs-CatCKniS.cjs} +2 -2
- package/dist/{toRefundInputs-CY9u84U9.cjs.map → toRefundInputs-CatCKniS.cjs.map} +1 -1
- package/dist/types-CPDLZm3l.js +430 -0
- package/dist/types-CPDLZm3l.js.map +1 -0
- package/dist/types-DSlcXtog.cjs +2 -0
- package/dist/types-DSlcXtog.cjs.map +1 -0
- package/package.json +32 -3
- package/dist/BTCVaultRegistryKeyEpochs.abi-CXjhRZbm.js +0 -111
- package/dist/BTCVaultRegistryKeyEpochs.abi-CXjhRZbm.js.map +0 -1
- package/dist/BTCVaultRegistryKeyEpochs.abi-_NERAdc5.cjs +0 -2
- package/dist/BTCVaultRegistryKeyEpochs.abi-_NERAdc5.cjs.map +0 -1
- package/dist/PeginManager-Bn8FxM35.cjs +0 -2
- package/dist/PeginManager-Bn8FxM35.cjs.map +0 -1
- package/dist/PeginManager-C3ojNTlv.js +0 -1503
- package/dist/PeginManager-C3ojNTlv.js.map +0 -1
- package/dist/ProtocolParams.abi-C0TSRaHV.js +0 -486
- package/dist/ProtocolParams.abi-C0TSRaHV.js.map +0 -1
- package/dist/ProtocolParams.abi-CYZoV2EI.cjs +0 -2
- package/dist/ProtocolParams.abi-CYZoV2EI.cjs.map +0 -1
- package/dist/buildAndBroadcastReclaim-YvWDRAN8.js.map +0 -1
- package/dist/buildAndBroadcastReclaim-zfjDI5__.cjs +0 -2
- package/dist/buildAndBroadcastReclaim-zfjDI5__.cjs.map +0 -1
- package/dist/errors-Blc-JWnI.js +0 -121
- package/dist/errors-Blc-JWnI.js.map +0 -1
- package/dist/errors-CGcNP0rV.cjs +0 -2
- package/dist/errors-CGcNP0rV.cjs.map +0 -1
- package/dist/mempoolApi-CKD_IbrM.js +0 -611
- package/dist/mempoolApi-CKD_IbrM.js.map +0 -1
- package/dist/mempoolApi-DronQ5xn.cjs +0 -2
- package/dist/mempoolApi-DronQ5xn.cjs.map +0 -1
- package/dist/onChainBtcPubkey-B5ZvWCCF.cjs +0 -2
- package/dist/onChainBtcPubkey-B5ZvWCCF.cjs.map +0 -1
- package/dist/onChainBtcPubkey-DATNfAAl.js +0 -19
- package/dist/onChainBtcPubkey-DATNfAAl.js.map +0 -1
- package/dist/primeVpAuth-Cjq6qFHq.cjs +0 -2
- package/dist/primeVpAuth-Cjq6qFHq.cjs.map +0 -1
- package/dist/primeVpAuth-tLRjdHwZ.js +0 -1159
- package/dist/primeVpAuth-tLRjdHwZ.js.map +0 -1
- package/dist/sha2-BYVxyZzX.js +0 -274
- package/dist/sha2-DsrLC4NM.cjs +0 -2
- package/dist/waitForTransactionReceiptSmartAware-DpQ69ySe.cjs +0 -2
- package/dist/waitForTransactionReceiptSmartAware-DpQ69ySe.cjs.map +0 -1
- package/dist/waitForTransactionReceiptSmartAware-z3Xfmo44.js +0 -277
- package/dist/waitForTransactionReceiptSmartAware-z3Xfmo44.js.map +0 -1
|
@@ -0,0 +1,2635 @@
|
|
|
1
|
+
var Cn = Object.defineProperty;
|
|
2
|
+
var Tn = (t, e, r) => e in t ? Cn(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
|
|
3
|
+
var lt = (t, e, r) => Tn(t, typeof e != "symbol" ? e + "" : e, r);
|
|
4
|
+
import { isAddress as $n, keccak256 as _n, encodeAbiParameters as Vn, encodeFunctionData as ye, isAddressEqual as Rn, zeroAddress as Nn } from "viem";
|
|
5
|
+
import { B as F } from "./BTCVaultRegistry.abi-CJwWI2EO.js";
|
|
6
|
+
import { B as be, h as Mt } from "./BTCVaultRegistryKeyEpochs.abi-D8cIbMtH.js";
|
|
7
|
+
import { M as Ee, d as xe } from "./constants-CPzppOdq.js";
|
|
8
|
+
import { waitForTransactionReceiptSmartAware as Hn } from "./tbv/core/utils/eth/index.js";
|
|
9
|
+
import { a as xt } from "./onChainBtcPubkey-C-6UWpD8.js";
|
|
10
|
+
import { c as kn, H as On, r as At, g as Be, h as Ze, i as ue, s as Jt } from "./sha2-CrlWM5S8.js";
|
|
11
|
+
import { a as qn } from "./vaultCoreVersion-D_fP9P3C.js";
|
|
12
|
+
const te = 25, Un = 9999;
|
|
13
|
+
function ve(t) {
|
|
14
|
+
if (!Number.isInteger(t) || t < 0 || t >= Ee)
|
|
15
|
+
throw new Error(
|
|
16
|
+
`Quoted commissionBps must be an integer in [0, ${Ee}), got ${t}`
|
|
17
|
+
);
|
|
18
|
+
return Math.min(
|
|
19
|
+
t + te,
|
|
20
|
+
Un
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
const Kn = /* @__PURE__ */ Uint8Array.from([
|
|
24
|
+
7,
|
|
25
|
+
4,
|
|
26
|
+
13,
|
|
27
|
+
1,
|
|
28
|
+
10,
|
|
29
|
+
6,
|
|
30
|
+
15,
|
|
31
|
+
3,
|
|
32
|
+
12,
|
|
33
|
+
0,
|
|
34
|
+
9,
|
|
35
|
+
5,
|
|
36
|
+
2,
|
|
37
|
+
14,
|
|
38
|
+
11,
|
|
39
|
+
8
|
|
40
|
+
]), De = Uint8Array.from(new Array(16).fill(0).map((t, e) => e)), Ln = De.map((t) => (9 * t + 5) % 16), ze = /* @__PURE__ */ (() => {
|
|
41
|
+
const r = [[De], [Ln]];
|
|
42
|
+
for (let n = 0; n < 4; n++)
|
|
43
|
+
for (let o of r)
|
|
44
|
+
o.push(o[n].map((s) => Kn[s]));
|
|
45
|
+
return r;
|
|
46
|
+
})(), Xe = ze[0], Ye = ze[1], je = /* @__PURE__ */ [
|
|
47
|
+
[11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8],
|
|
48
|
+
[12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7],
|
|
49
|
+
[13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9],
|
|
50
|
+
[14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6],
|
|
51
|
+
[15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5]
|
|
52
|
+
].map((t) => Uint8Array.from(t)), Mn = /* @__PURE__ */ Xe.map((t, e) => t.map((r) => je[e][r])), Fn = /* @__PURE__ */ Ye.map((t, e) => t.map((r) => je[e][r])), Zn = /* @__PURE__ */ Uint32Array.from([
|
|
53
|
+
0,
|
|
54
|
+
1518500249,
|
|
55
|
+
1859775393,
|
|
56
|
+
2400959708,
|
|
57
|
+
2840853838
|
|
58
|
+
]), Dn = /* @__PURE__ */ Uint32Array.from([
|
|
59
|
+
1352829926,
|
|
60
|
+
1548603684,
|
|
61
|
+
1836072691,
|
|
62
|
+
2053994217,
|
|
63
|
+
0
|
|
64
|
+
]);
|
|
65
|
+
function Pe(t, e, r, n) {
|
|
66
|
+
return t === 0 ? e ^ r ^ n : t === 1 ? e & r | ~e & n : t === 2 ? (e | ~r) ^ n : t === 3 ? e & n | r & ~n : e ^ (r | ~n);
|
|
67
|
+
}
|
|
68
|
+
const St = /* @__PURE__ */ new Uint32Array(16);
|
|
69
|
+
class zn extends On {
|
|
70
|
+
constructor() {
|
|
71
|
+
super(64, 20, 8, !0);
|
|
72
|
+
lt(this, "h0", 1732584193);
|
|
73
|
+
lt(this, "h1", -271733879);
|
|
74
|
+
lt(this, "h2", -1732584194);
|
|
75
|
+
lt(this, "h3", 271733878);
|
|
76
|
+
lt(this, "h4", -1009589776);
|
|
77
|
+
}
|
|
78
|
+
get() {
|
|
79
|
+
const { h0: r, h1: n, h2: o, h3: s, h4: i } = this;
|
|
80
|
+
return [r, n, o, s, i];
|
|
81
|
+
}
|
|
82
|
+
set(r, n, o, s, i) {
|
|
83
|
+
this.h0 = r | 0, this.h1 = n | 0, this.h2 = o | 0, this.h3 = s | 0, this.h4 = i | 0;
|
|
84
|
+
}
|
|
85
|
+
process(r, n) {
|
|
86
|
+
for (let h = 0; h < 16; h++, n += 4)
|
|
87
|
+
St[h] = r.getUint32(n, !0);
|
|
88
|
+
let o = this.h0 | 0, s = o, i = this.h1 | 0, a = i, u = this.h2 | 0, l = u, m = this.h3 | 0, y = m, c = this.h4 | 0, f = c;
|
|
89
|
+
for (let h = 0; h < 5; h++) {
|
|
90
|
+
const w = 4 - h, A = Zn[h], _ = Dn[h], nt = Xe[h], V = Ye[h], j = Mn[h], z = Fn[h];
|
|
91
|
+
for (let q = 0; q < 16; q++) {
|
|
92
|
+
const J = At(o + Pe(h, i, u, m) + St[nt[q]] + A, j[q]) + c | 0;
|
|
93
|
+
o = c, c = m, m = At(u, 10) | 0, u = i, i = J;
|
|
94
|
+
}
|
|
95
|
+
for (let q = 0; q < 16; q++) {
|
|
96
|
+
const J = At(s + Pe(w, a, l, y) + St[V[q]] + _, z[q]) + f | 0;
|
|
97
|
+
s = f, f = y, y = At(l, 10) | 0, l = a, a = J;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
this.set(this.h1 + u + y | 0, this.h2 + m + f | 0, this.h3 + c + s | 0, this.h4 + o + a | 0, this.h0 + i + l | 0);
|
|
101
|
+
}
|
|
102
|
+
roundClean() {
|
|
103
|
+
Be(St);
|
|
104
|
+
}
|
|
105
|
+
destroy() {
|
|
106
|
+
this.destroyed = !0, Be(this.buffer), this.set(0, 0, 0, 0, 0);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const Xn = /* @__PURE__ */ kn(() => new zn()), dt = typeof globalThis == "object" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
110
|
+
function qt(t) {
|
|
111
|
+
return t instanceof Uint8Array || ArrayBuffer.isView(t) && t.constructor.name === "Uint8Array";
|
|
112
|
+
}
|
|
113
|
+
function ee(t) {
|
|
114
|
+
if (!Number.isSafeInteger(t) || t < 0)
|
|
115
|
+
throw new Error("positive integer expected, got " + t);
|
|
116
|
+
}
|
|
117
|
+
function ut(t, ...e) {
|
|
118
|
+
if (!qt(t))
|
|
119
|
+
throw new Error("Uint8Array expected");
|
|
120
|
+
if (e.length > 0 && !e.includes(t.length))
|
|
121
|
+
throw new Error("Uint8Array expected of length " + e + ", got length=" + t.length);
|
|
122
|
+
}
|
|
123
|
+
function We(t) {
|
|
124
|
+
if (typeof t != "function" || typeof t.create != "function")
|
|
125
|
+
throw new Error("Hash should be wrapped by utils.createHasher");
|
|
126
|
+
ee(t.outputLen), ee(t.blockLen);
|
|
127
|
+
}
|
|
128
|
+
function $t(t, e = !0) {
|
|
129
|
+
if (t.destroyed)
|
|
130
|
+
throw new Error("Hash instance has been destroyed");
|
|
131
|
+
if (e && t.finished)
|
|
132
|
+
throw new Error("Hash#digest() has already been called");
|
|
133
|
+
}
|
|
134
|
+
function Yn(t, e) {
|
|
135
|
+
ut(t);
|
|
136
|
+
const r = e.outputLen;
|
|
137
|
+
if (t.length < r)
|
|
138
|
+
throw new Error("digestInto() expects output buffer of length at least " + r);
|
|
139
|
+
}
|
|
140
|
+
function _t(...t) {
|
|
141
|
+
for (let e = 0; e < t.length; e++)
|
|
142
|
+
t[e].fill(0);
|
|
143
|
+
}
|
|
144
|
+
function Ft(t) {
|
|
145
|
+
return new DataView(t.buffer, t.byteOffset, t.byteLength);
|
|
146
|
+
}
|
|
147
|
+
function X(t, e) {
|
|
148
|
+
return t << 32 - e | t >>> e;
|
|
149
|
+
}
|
|
150
|
+
const Ge = /* @ts-ignore */ typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", jn = /* @__PURE__ */ Array.from({ length: 256 }, (t, e) => e.toString(16).padStart(2, "0"));
|
|
151
|
+
function ht(t) {
|
|
152
|
+
if (ut(t), Ge)
|
|
153
|
+
return t.toHex();
|
|
154
|
+
let e = "";
|
|
155
|
+
for (let r = 0; r < t.length; r++)
|
|
156
|
+
e += jn[t[r]];
|
|
157
|
+
return e;
|
|
158
|
+
}
|
|
159
|
+
const W = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
160
|
+
function Ie(t) {
|
|
161
|
+
if (t >= W._0 && t <= W._9)
|
|
162
|
+
return t - W._0;
|
|
163
|
+
if (t >= W.A && t <= W.F)
|
|
164
|
+
return t - (W.A - 10);
|
|
165
|
+
if (t >= W.a && t <= W.f)
|
|
166
|
+
return t - (W.a - 10);
|
|
167
|
+
}
|
|
168
|
+
function Vt(t) {
|
|
169
|
+
if (typeof t != "string")
|
|
170
|
+
throw new Error("hex string expected, got " + typeof t);
|
|
171
|
+
if (Ge)
|
|
172
|
+
return Uint8Array.fromHex(t);
|
|
173
|
+
const e = t.length, r = e / 2;
|
|
174
|
+
if (e % 2)
|
|
175
|
+
throw new Error("hex string expected, got unpadded hex of length " + e);
|
|
176
|
+
const n = new Uint8Array(r);
|
|
177
|
+
for (let o = 0, s = 0; o < r; o++, s += 2) {
|
|
178
|
+
const i = Ie(t.charCodeAt(s)), a = Ie(t.charCodeAt(s + 1));
|
|
179
|
+
if (i === void 0 || a === void 0) {
|
|
180
|
+
const u = t[s] + t[s + 1];
|
|
181
|
+
throw new Error('hex string expected, got non-hex character "' + u + '" at index ' + s);
|
|
182
|
+
}
|
|
183
|
+
n[o] = i * 16 + a;
|
|
184
|
+
}
|
|
185
|
+
return n;
|
|
186
|
+
}
|
|
187
|
+
function Qe(t) {
|
|
188
|
+
if (typeof t != "string")
|
|
189
|
+
throw new Error("string expected");
|
|
190
|
+
return new Uint8Array(new TextEncoder().encode(t));
|
|
191
|
+
}
|
|
192
|
+
function fe(t) {
|
|
193
|
+
return typeof t == "string" && (t = Qe(t)), ut(t), t;
|
|
194
|
+
}
|
|
195
|
+
function Y(...t) {
|
|
196
|
+
let e = 0;
|
|
197
|
+
for (let n = 0; n < t.length; n++) {
|
|
198
|
+
const o = t[n];
|
|
199
|
+
ut(o), e += o.length;
|
|
200
|
+
}
|
|
201
|
+
const r = new Uint8Array(e);
|
|
202
|
+
for (let n = 0, o = 0; n < t.length; n++) {
|
|
203
|
+
const s = t[n];
|
|
204
|
+
r.set(s, o), o += s.length;
|
|
205
|
+
}
|
|
206
|
+
return r;
|
|
207
|
+
}
|
|
208
|
+
class Je {
|
|
209
|
+
}
|
|
210
|
+
function Wn(t) {
|
|
211
|
+
const e = (n) => t().update(fe(n)).digest(), r = t();
|
|
212
|
+
return e.outputLen = r.outputLen, e.blockLen = r.blockLen, e.create = () => t(), e;
|
|
213
|
+
}
|
|
214
|
+
function Ut(t = 32) {
|
|
215
|
+
if (dt && typeof dt.getRandomValues == "function")
|
|
216
|
+
return dt.getRandomValues(new Uint8Array(t));
|
|
217
|
+
if (dt && typeof dt.randomBytes == "function")
|
|
218
|
+
return Uint8Array.from(dt.randomBytes(t));
|
|
219
|
+
throw new Error("crypto.getRandomValues must be defined");
|
|
220
|
+
}
|
|
221
|
+
function Gn(t, e, r, n) {
|
|
222
|
+
if (typeof t.setBigUint64 == "function")
|
|
223
|
+
return t.setBigUint64(e, r, n);
|
|
224
|
+
const o = BigInt(32), s = BigInt(4294967295), i = Number(r >> o & s), a = Number(r & s), u = n ? 4 : 0, l = n ? 0 : 4;
|
|
225
|
+
t.setUint32(e + u, i, n), t.setUint32(e + l, a, n);
|
|
226
|
+
}
|
|
227
|
+
function Qn(t, e, r) {
|
|
228
|
+
return t & e ^ ~t & r;
|
|
229
|
+
}
|
|
230
|
+
function Jn(t, e, r) {
|
|
231
|
+
return t & e ^ t & r ^ e & r;
|
|
232
|
+
}
|
|
233
|
+
class tr extends Je {
|
|
234
|
+
constructor(e, r, n, o) {
|
|
235
|
+
super(), this.finished = !1, this.length = 0, this.pos = 0, this.destroyed = !1, this.blockLen = e, this.outputLen = r, this.padOffset = n, this.isLE = o, this.buffer = new Uint8Array(e), this.view = Ft(this.buffer);
|
|
236
|
+
}
|
|
237
|
+
update(e) {
|
|
238
|
+
$t(this), e = fe(e), ut(e);
|
|
239
|
+
const { view: r, buffer: n, blockLen: o } = this, s = e.length;
|
|
240
|
+
for (let i = 0; i < s; ) {
|
|
241
|
+
const a = Math.min(o - this.pos, s - i);
|
|
242
|
+
if (a === o) {
|
|
243
|
+
const u = Ft(e);
|
|
244
|
+
for (; o <= s - i; i += o)
|
|
245
|
+
this.process(u, i);
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
n.set(e.subarray(i, i + a), this.pos), this.pos += a, i += a, this.pos === o && (this.process(r, 0), this.pos = 0);
|
|
249
|
+
}
|
|
250
|
+
return this.length += e.length, this.roundClean(), this;
|
|
251
|
+
}
|
|
252
|
+
digestInto(e) {
|
|
253
|
+
$t(this), Yn(e, this), this.finished = !0;
|
|
254
|
+
const { buffer: r, view: n, blockLen: o, isLE: s } = this;
|
|
255
|
+
let { pos: i } = this;
|
|
256
|
+
r[i++] = 128, _t(this.buffer.subarray(i)), this.padOffset > o - i && (this.process(n, 0), i = 0);
|
|
257
|
+
for (let y = i; y < o; y++)
|
|
258
|
+
r[y] = 0;
|
|
259
|
+
Gn(n, o - 8, BigInt(this.length * 8), s), this.process(n, 0);
|
|
260
|
+
const a = Ft(e), u = this.outputLen;
|
|
261
|
+
if (u % 4)
|
|
262
|
+
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
263
|
+
const l = u / 4, m = this.get();
|
|
264
|
+
if (l > m.length)
|
|
265
|
+
throw new Error("_sha2: outputLen bigger than state");
|
|
266
|
+
for (let y = 0; y < l; y++)
|
|
267
|
+
a.setUint32(4 * y, m[y], s);
|
|
268
|
+
}
|
|
269
|
+
digest() {
|
|
270
|
+
const { buffer: e, outputLen: r } = this;
|
|
271
|
+
this.digestInto(e);
|
|
272
|
+
const n = e.slice(0, r);
|
|
273
|
+
return this.destroy(), n;
|
|
274
|
+
}
|
|
275
|
+
_cloneInto(e) {
|
|
276
|
+
e || (e = new this.constructor()), e.set(...this.get());
|
|
277
|
+
const { blockLen: r, buffer: n, length: o, finished: s, destroyed: i, pos: a } = this;
|
|
278
|
+
return e.destroyed = i, e.finished = s, e.length = o, e.pos = a, o % r && e.buffer.set(n), e;
|
|
279
|
+
}
|
|
280
|
+
clone() {
|
|
281
|
+
return this._cloneInto();
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const tt = /* @__PURE__ */ Uint32Array.from([
|
|
285
|
+
1779033703,
|
|
286
|
+
3144134277,
|
|
287
|
+
1013904242,
|
|
288
|
+
2773480762,
|
|
289
|
+
1359893119,
|
|
290
|
+
2600822924,
|
|
291
|
+
528734635,
|
|
292
|
+
1541459225
|
|
293
|
+
]), er = /* @__PURE__ */ Uint32Array.from([
|
|
294
|
+
1116352408,
|
|
295
|
+
1899447441,
|
|
296
|
+
3049323471,
|
|
297
|
+
3921009573,
|
|
298
|
+
961987163,
|
|
299
|
+
1508970993,
|
|
300
|
+
2453635748,
|
|
301
|
+
2870763221,
|
|
302
|
+
3624381080,
|
|
303
|
+
310598401,
|
|
304
|
+
607225278,
|
|
305
|
+
1426881987,
|
|
306
|
+
1925078388,
|
|
307
|
+
2162078206,
|
|
308
|
+
2614888103,
|
|
309
|
+
3248222580,
|
|
310
|
+
3835390401,
|
|
311
|
+
4022224774,
|
|
312
|
+
264347078,
|
|
313
|
+
604807628,
|
|
314
|
+
770255983,
|
|
315
|
+
1249150122,
|
|
316
|
+
1555081692,
|
|
317
|
+
1996064986,
|
|
318
|
+
2554220882,
|
|
319
|
+
2821834349,
|
|
320
|
+
2952996808,
|
|
321
|
+
3210313671,
|
|
322
|
+
3336571891,
|
|
323
|
+
3584528711,
|
|
324
|
+
113926993,
|
|
325
|
+
338241895,
|
|
326
|
+
666307205,
|
|
327
|
+
773529912,
|
|
328
|
+
1294757372,
|
|
329
|
+
1396182291,
|
|
330
|
+
1695183700,
|
|
331
|
+
1986661051,
|
|
332
|
+
2177026350,
|
|
333
|
+
2456956037,
|
|
334
|
+
2730485921,
|
|
335
|
+
2820302411,
|
|
336
|
+
3259730800,
|
|
337
|
+
3345764771,
|
|
338
|
+
3516065817,
|
|
339
|
+
3600352804,
|
|
340
|
+
4094571909,
|
|
341
|
+
275423344,
|
|
342
|
+
430227734,
|
|
343
|
+
506948616,
|
|
344
|
+
659060556,
|
|
345
|
+
883997877,
|
|
346
|
+
958139571,
|
|
347
|
+
1322822218,
|
|
348
|
+
1537002063,
|
|
349
|
+
1747873779,
|
|
350
|
+
1955562222,
|
|
351
|
+
2024104815,
|
|
352
|
+
2227730452,
|
|
353
|
+
2361852424,
|
|
354
|
+
2428436474,
|
|
355
|
+
2756734187,
|
|
356
|
+
3204031479,
|
|
357
|
+
3329325298
|
|
358
|
+
]), et = /* @__PURE__ */ new Uint32Array(64);
|
|
359
|
+
class nr extends tr {
|
|
360
|
+
constructor(e = 32) {
|
|
361
|
+
super(64, e, 8, !1), this.A = tt[0] | 0, this.B = tt[1] | 0, this.C = tt[2] | 0, this.D = tt[3] | 0, this.E = tt[4] | 0, this.F = tt[5] | 0, this.G = tt[6] | 0, this.H = tt[7] | 0;
|
|
362
|
+
}
|
|
363
|
+
get() {
|
|
364
|
+
const { A: e, B: r, C: n, D: o, E: s, F: i, G: a, H: u } = this;
|
|
365
|
+
return [e, r, n, o, s, i, a, u];
|
|
366
|
+
}
|
|
367
|
+
// prettier-ignore
|
|
368
|
+
set(e, r, n, o, s, i, a, u) {
|
|
369
|
+
this.A = e | 0, this.B = r | 0, this.C = n | 0, this.D = o | 0, this.E = s | 0, this.F = i | 0, this.G = a | 0, this.H = u | 0;
|
|
370
|
+
}
|
|
371
|
+
process(e, r) {
|
|
372
|
+
for (let y = 0; y < 16; y++, r += 4)
|
|
373
|
+
et[y] = e.getUint32(r, !1);
|
|
374
|
+
for (let y = 16; y < 64; y++) {
|
|
375
|
+
const c = et[y - 15], f = et[y - 2], h = X(c, 7) ^ X(c, 18) ^ c >>> 3, w = X(f, 17) ^ X(f, 19) ^ f >>> 10;
|
|
376
|
+
et[y] = w + et[y - 7] + h + et[y - 16] | 0;
|
|
377
|
+
}
|
|
378
|
+
let { A: n, B: o, C: s, D: i, E: a, F: u, G: l, H: m } = this;
|
|
379
|
+
for (let y = 0; y < 64; y++) {
|
|
380
|
+
const c = X(a, 6) ^ X(a, 11) ^ X(a, 25), f = m + c + Qn(a, u, l) + er[y] + et[y] | 0, w = (X(n, 2) ^ X(n, 13) ^ X(n, 22)) + Jn(n, o, s) | 0;
|
|
381
|
+
m = l, l = u, u = a, a = i + f | 0, i = s, s = o, o = n, n = f + w | 0;
|
|
382
|
+
}
|
|
383
|
+
n = n + this.A | 0, o = o + this.B | 0, s = s + this.C | 0, i = i + this.D | 0, a = a + this.E | 0, u = u + this.F | 0, l = l + this.G | 0, m = m + this.H | 0, this.set(n, o, s, i, a, u, l, m);
|
|
384
|
+
}
|
|
385
|
+
roundClean() {
|
|
386
|
+
_t(et);
|
|
387
|
+
}
|
|
388
|
+
destroy() {
|
|
389
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0), _t(this.buffer);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
const ne = /* @__PURE__ */ Wn(() => new nr());
|
|
393
|
+
class tn extends Je {
|
|
394
|
+
constructor(e, r) {
|
|
395
|
+
super(), this.finished = !1, this.destroyed = !1, We(e);
|
|
396
|
+
const n = fe(r);
|
|
397
|
+
if (this.iHash = e.create(), typeof this.iHash.update != "function")
|
|
398
|
+
throw new Error("Expected instance of class which extends utils.Hash");
|
|
399
|
+
this.blockLen = this.iHash.blockLen, this.outputLen = this.iHash.outputLen;
|
|
400
|
+
const o = this.blockLen, s = new Uint8Array(o);
|
|
401
|
+
s.set(n.length > o ? e.create().update(n).digest() : n);
|
|
402
|
+
for (let i = 0; i < s.length; i++)
|
|
403
|
+
s[i] ^= 54;
|
|
404
|
+
this.iHash.update(s), this.oHash = e.create();
|
|
405
|
+
for (let i = 0; i < s.length; i++)
|
|
406
|
+
s[i] ^= 106;
|
|
407
|
+
this.oHash.update(s), _t(s);
|
|
408
|
+
}
|
|
409
|
+
update(e) {
|
|
410
|
+
return $t(this), this.iHash.update(e), this;
|
|
411
|
+
}
|
|
412
|
+
digestInto(e) {
|
|
413
|
+
$t(this), ut(e, this.outputLen), this.finished = !0, this.iHash.digestInto(e), this.oHash.update(e), this.oHash.digestInto(e), this.destroy();
|
|
414
|
+
}
|
|
415
|
+
digest() {
|
|
416
|
+
const e = new Uint8Array(this.oHash.outputLen);
|
|
417
|
+
return this.digestInto(e), e;
|
|
418
|
+
}
|
|
419
|
+
_cloneInto(e) {
|
|
420
|
+
e || (e = Object.create(Object.getPrototypeOf(this), {}));
|
|
421
|
+
const { oHash: r, iHash: n, finished: o, destroyed: s, blockLen: i, outputLen: a } = this;
|
|
422
|
+
return e = e, e.finished = o, e.destroyed = s, e.blockLen = i, e.outputLen = a, e.oHash = r._cloneInto(e.oHash), e.iHash = n._cloneInto(e.iHash), e;
|
|
423
|
+
}
|
|
424
|
+
clone() {
|
|
425
|
+
return this._cloneInto();
|
|
426
|
+
}
|
|
427
|
+
destroy() {
|
|
428
|
+
this.destroyed = !0, this.oHash.destroy(), this.iHash.destroy();
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
const en = (t, e, r) => new tn(t, e).update(r).digest();
|
|
432
|
+
en.create = (t, e) => new tn(t, e);
|
|
433
|
+
const le = /* @__PURE__ */ BigInt(0), re = /* @__PURE__ */ BigInt(1);
|
|
434
|
+
function Rt(t, e = "") {
|
|
435
|
+
if (typeof t != "boolean") {
|
|
436
|
+
const r = e && `"${e}"`;
|
|
437
|
+
throw new Error(r + "expected boolean, got type=" + typeof t);
|
|
438
|
+
}
|
|
439
|
+
return t;
|
|
440
|
+
}
|
|
441
|
+
function st(t, e, r = "") {
|
|
442
|
+
const n = qt(t), o = t == null ? void 0 : t.length, s = e !== void 0;
|
|
443
|
+
if (!n || s && o !== e) {
|
|
444
|
+
const i = r && `"${r}" `, a = s ? ` of length ${e}` : "", u = n ? `length=${o}` : `type=${typeof t}`;
|
|
445
|
+
throw new Error(i + "expected Uint8Array" + a + ", got " + u);
|
|
446
|
+
}
|
|
447
|
+
return t;
|
|
448
|
+
}
|
|
449
|
+
function Ct(t) {
|
|
450
|
+
const e = t.toString(16);
|
|
451
|
+
return e.length & 1 ? "0" + e : e;
|
|
452
|
+
}
|
|
453
|
+
function nn(t) {
|
|
454
|
+
if (typeof t != "string")
|
|
455
|
+
throw new Error("hex string expected, got " + typeof t);
|
|
456
|
+
return t === "" ? le : BigInt("0x" + t);
|
|
457
|
+
}
|
|
458
|
+
function wt(t) {
|
|
459
|
+
return nn(ht(t));
|
|
460
|
+
}
|
|
461
|
+
function rn(t) {
|
|
462
|
+
return ut(t), nn(ht(Uint8Array.from(t).reverse()));
|
|
463
|
+
}
|
|
464
|
+
function Kt(t, e) {
|
|
465
|
+
return Vt(t.toString(16).padStart(e * 2, "0"));
|
|
466
|
+
}
|
|
467
|
+
function on(t, e) {
|
|
468
|
+
return Kt(t, e).reverse();
|
|
469
|
+
}
|
|
470
|
+
function O(t, e, r) {
|
|
471
|
+
let n;
|
|
472
|
+
if (typeof e == "string")
|
|
473
|
+
try {
|
|
474
|
+
n = Vt(e);
|
|
475
|
+
} catch (s) {
|
|
476
|
+
throw new Error(t + " must be hex string or Uint8Array, cause: " + s);
|
|
477
|
+
}
|
|
478
|
+
else if (qt(e))
|
|
479
|
+
n = Uint8Array.from(e);
|
|
480
|
+
else
|
|
481
|
+
throw new Error(t + " must be hex string or Uint8Array");
|
|
482
|
+
const o = n.length;
|
|
483
|
+
if (typeof r == "number" && o !== r)
|
|
484
|
+
throw new Error(t + " of length " + r + " expected, got " + o);
|
|
485
|
+
return n;
|
|
486
|
+
}
|
|
487
|
+
const Zt = (t) => typeof t == "bigint" && le <= t;
|
|
488
|
+
function oe(t, e, r) {
|
|
489
|
+
return Zt(t) && Zt(e) && Zt(r) && e <= t && t < r;
|
|
490
|
+
}
|
|
491
|
+
function rr(t, e, r, n) {
|
|
492
|
+
if (!oe(e, r, n))
|
|
493
|
+
throw new Error("expected valid " + t + ": " + r + " <= n < " + n + ", got " + e);
|
|
494
|
+
}
|
|
495
|
+
function sn(t) {
|
|
496
|
+
let e;
|
|
497
|
+
for (e = 0; t > le; t >>= re, e += 1)
|
|
498
|
+
;
|
|
499
|
+
return e;
|
|
500
|
+
}
|
|
501
|
+
const Bt = (t) => (re << BigInt(t)) - re;
|
|
502
|
+
function or(t, e, r) {
|
|
503
|
+
if (typeof t != "number" || t < 2)
|
|
504
|
+
throw new Error("hashLen must be a number");
|
|
505
|
+
if (typeof e != "number" || e < 2)
|
|
506
|
+
throw new Error("qByteLen must be a number");
|
|
507
|
+
if (typeof r != "function")
|
|
508
|
+
throw new Error("hmacFn must be a function");
|
|
509
|
+
const n = (f) => new Uint8Array(f), o = (f) => Uint8Array.of(f);
|
|
510
|
+
let s = n(t), i = n(t), a = 0;
|
|
511
|
+
const u = () => {
|
|
512
|
+
s.fill(1), i.fill(0), a = 0;
|
|
513
|
+
}, l = (...f) => r(i, s, ...f), m = (f = n(0)) => {
|
|
514
|
+
i = l(o(0), f), s = l(), f.length !== 0 && (i = l(o(1), f), s = l());
|
|
515
|
+
}, y = () => {
|
|
516
|
+
if (a++ >= 1e3)
|
|
517
|
+
throw new Error("drbg: tried 1000 values");
|
|
518
|
+
let f = 0;
|
|
519
|
+
const h = [];
|
|
520
|
+
for (; f < e; ) {
|
|
521
|
+
s = l();
|
|
522
|
+
const w = s.slice();
|
|
523
|
+
h.push(w), f += s.length;
|
|
524
|
+
}
|
|
525
|
+
return Y(...h);
|
|
526
|
+
};
|
|
527
|
+
return (f, h) => {
|
|
528
|
+
u(), m(f);
|
|
529
|
+
let w;
|
|
530
|
+
for (; !(w = h(y())); )
|
|
531
|
+
m();
|
|
532
|
+
return u(), w;
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
function de(t, e, r = {}) {
|
|
536
|
+
if (!t || typeof t != "object")
|
|
537
|
+
throw new Error("expected valid options object");
|
|
538
|
+
function n(o, s, i) {
|
|
539
|
+
const a = t[o];
|
|
540
|
+
if (i && a === void 0)
|
|
541
|
+
return;
|
|
542
|
+
const u = typeof a;
|
|
543
|
+
if (u !== s || a === null)
|
|
544
|
+
throw new Error(`param "${o}" is invalid: expected ${s}, got ${u}`);
|
|
545
|
+
}
|
|
546
|
+
Object.entries(e).forEach(([o, s]) => n(o, s, !1)), Object.entries(r).forEach(([o, s]) => n(o, s, !0));
|
|
547
|
+
}
|
|
548
|
+
function Ae(t) {
|
|
549
|
+
const e = /* @__PURE__ */ new WeakMap();
|
|
550
|
+
return (r, ...n) => {
|
|
551
|
+
const o = e.get(r);
|
|
552
|
+
if (o !== void 0)
|
|
553
|
+
return o;
|
|
554
|
+
const s = t(r, ...n);
|
|
555
|
+
return e.set(r, s), s;
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
const M = BigInt(0), K = BigInt(1), it = /* @__PURE__ */ BigInt(2), cn = /* @__PURE__ */ BigInt(3), an = /* @__PURE__ */ BigInt(4), un = /* @__PURE__ */ BigInt(5), sr = /* @__PURE__ */ BigInt(7), fn = /* @__PURE__ */ BigInt(8), ir = /* @__PURE__ */ BigInt(9), ln = /* @__PURE__ */ BigInt(16);
|
|
559
|
+
function D(t, e) {
|
|
560
|
+
const r = t % e;
|
|
561
|
+
return r >= M ? r : e + r;
|
|
562
|
+
}
|
|
563
|
+
function Z(t, e, r) {
|
|
564
|
+
let n = t;
|
|
565
|
+
for (; e-- > M; )
|
|
566
|
+
n *= n, n %= r;
|
|
567
|
+
return n;
|
|
568
|
+
}
|
|
569
|
+
function Se(t, e) {
|
|
570
|
+
if (t === M)
|
|
571
|
+
throw new Error("invert: expected non-zero number");
|
|
572
|
+
if (e <= M)
|
|
573
|
+
throw new Error("invert: expected positive modulus, got " + e);
|
|
574
|
+
let r = D(t, e), n = e, o = M, s = K;
|
|
575
|
+
for (; r !== M; ) {
|
|
576
|
+
const a = n / r, u = n % r, l = o - s * a;
|
|
577
|
+
n = r, r = u, o = s, s = l;
|
|
578
|
+
}
|
|
579
|
+
if (n !== K)
|
|
580
|
+
throw new Error("invert: does not exist");
|
|
581
|
+
return D(o, e);
|
|
582
|
+
}
|
|
583
|
+
function he(t, e, r) {
|
|
584
|
+
if (!t.eql(t.sqr(e), r))
|
|
585
|
+
throw new Error("Cannot find square root");
|
|
586
|
+
}
|
|
587
|
+
function dn(t, e) {
|
|
588
|
+
const r = (t.ORDER + K) / an, n = t.pow(e, r);
|
|
589
|
+
return he(t, n, e), n;
|
|
590
|
+
}
|
|
591
|
+
function cr(t, e) {
|
|
592
|
+
const r = (t.ORDER - un) / fn, n = t.mul(e, it), o = t.pow(n, r), s = t.mul(e, o), i = t.mul(t.mul(s, it), o), a = t.mul(s, t.sub(i, t.ONE));
|
|
593
|
+
return he(t, a, e), a;
|
|
594
|
+
}
|
|
595
|
+
function ar(t) {
|
|
596
|
+
const e = vt(t), r = hn(t), n = r(e, e.neg(e.ONE)), o = r(e, n), s = r(e, e.neg(n)), i = (t + sr) / ln;
|
|
597
|
+
return (a, u) => {
|
|
598
|
+
let l = a.pow(u, i), m = a.mul(l, n);
|
|
599
|
+
const y = a.mul(l, o), c = a.mul(l, s), f = a.eql(a.sqr(m), u), h = a.eql(a.sqr(y), u);
|
|
600
|
+
l = a.cmov(l, m, f), m = a.cmov(c, y, h);
|
|
601
|
+
const w = a.eql(a.sqr(m), u), A = a.cmov(l, m, w);
|
|
602
|
+
return he(a, A, u), A;
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
function hn(t) {
|
|
606
|
+
if (t < cn)
|
|
607
|
+
throw new Error("sqrt is not defined for small field");
|
|
608
|
+
let e = t - K, r = 0;
|
|
609
|
+
for (; e % it === M; )
|
|
610
|
+
e /= it, r++;
|
|
611
|
+
let n = it;
|
|
612
|
+
const o = vt(t);
|
|
613
|
+
for (; Ce(o, n) === 1; )
|
|
614
|
+
if (n++ > 1e3)
|
|
615
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
616
|
+
if (r === 1)
|
|
617
|
+
return dn;
|
|
618
|
+
let s = o.pow(n, e);
|
|
619
|
+
const i = (e + K) / it;
|
|
620
|
+
return function(u, l) {
|
|
621
|
+
if (u.is0(l))
|
|
622
|
+
return l;
|
|
623
|
+
if (Ce(u, l) !== 1)
|
|
624
|
+
throw new Error("Cannot find square root");
|
|
625
|
+
let m = r, y = u.mul(u.ONE, s), c = u.pow(l, e), f = u.pow(l, i);
|
|
626
|
+
for (; !u.eql(c, u.ONE); ) {
|
|
627
|
+
if (u.is0(c))
|
|
628
|
+
return u.ZERO;
|
|
629
|
+
let h = 1, w = u.sqr(c);
|
|
630
|
+
for (; !u.eql(w, u.ONE); )
|
|
631
|
+
if (h++, w = u.sqr(w), h === m)
|
|
632
|
+
throw new Error("Cannot find square root");
|
|
633
|
+
const A = K << BigInt(m - h - 1), _ = u.pow(y, A);
|
|
634
|
+
m = h, y = u.sqr(_), c = u.mul(c, y), f = u.mul(f, _);
|
|
635
|
+
}
|
|
636
|
+
return f;
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
function ur(t) {
|
|
640
|
+
return t % an === cn ? dn : t % fn === un ? cr : t % ln === ir ? ar(t) : hn(t);
|
|
641
|
+
}
|
|
642
|
+
const fr = [
|
|
643
|
+
"create",
|
|
644
|
+
"isValid",
|
|
645
|
+
"is0",
|
|
646
|
+
"neg",
|
|
647
|
+
"inv",
|
|
648
|
+
"sqrt",
|
|
649
|
+
"sqr",
|
|
650
|
+
"eql",
|
|
651
|
+
"add",
|
|
652
|
+
"sub",
|
|
653
|
+
"mul",
|
|
654
|
+
"pow",
|
|
655
|
+
"div",
|
|
656
|
+
"addN",
|
|
657
|
+
"subN",
|
|
658
|
+
"mulN",
|
|
659
|
+
"sqrN"
|
|
660
|
+
];
|
|
661
|
+
function lr(t) {
|
|
662
|
+
const e = {
|
|
663
|
+
ORDER: "bigint",
|
|
664
|
+
MASK: "bigint",
|
|
665
|
+
BYTES: "number",
|
|
666
|
+
BITS: "number"
|
|
667
|
+
}, r = fr.reduce((n, o) => (n[o] = "function", n), e);
|
|
668
|
+
return de(t, r), t;
|
|
669
|
+
}
|
|
670
|
+
function dr(t, e, r) {
|
|
671
|
+
if (r < M)
|
|
672
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
673
|
+
if (r === M)
|
|
674
|
+
return t.ONE;
|
|
675
|
+
if (r === K)
|
|
676
|
+
return e;
|
|
677
|
+
let n = t.ONE, o = e;
|
|
678
|
+
for (; r > M; )
|
|
679
|
+
r & K && (n = t.mul(n, o)), o = t.sqr(o), r >>= K;
|
|
680
|
+
return n;
|
|
681
|
+
}
|
|
682
|
+
function gn(t, e, r = !1) {
|
|
683
|
+
const n = new Array(e.length).fill(r ? t.ZERO : void 0), o = e.reduce((i, a, u) => t.is0(a) ? i : (n[u] = i, t.mul(i, a)), t.ONE), s = t.inv(o);
|
|
684
|
+
return e.reduceRight((i, a, u) => t.is0(a) ? i : (n[u] = t.mul(i, n[u]), t.mul(i, a)), s), n;
|
|
685
|
+
}
|
|
686
|
+
function Ce(t, e) {
|
|
687
|
+
const r = (t.ORDER - K) / it, n = t.pow(e, r), o = t.eql(n, t.ONE), s = t.eql(n, t.ZERO), i = t.eql(n, t.neg(t.ONE));
|
|
688
|
+
if (!o && !s && !i)
|
|
689
|
+
throw new Error("invalid Legendre symbol result");
|
|
690
|
+
return o ? 1 : s ? 0 : -1;
|
|
691
|
+
}
|
|
692
|
+
function pn(t, e) {
|
|
693
|
+
e !== void 0 && ee(e);
|
|
694
|
+
const r = e !== void 0 ? e : t.toString(2).length, n = Math.ceil(r / 8);
|
|
695
|
+
return { nBitLength: r, nByteLength: n };
|
|
696
|
+
}
|
|
697
|
+
function vt(t, e, r = !1, n = {}) {
|
|
698
|
+
if (t <= M)
|
|
699
|
+
throw new Error("invalid field: expected ORDER > 0, got " + t);
|
|
700
|
+
let o, s, i = !1, a;
|
|
701
|
+
if (typeof e == "object" && e != null) {
|
|
702
|
+
if (n.sqrt || r)
|
|
703
|
+
throw new Error("cannot specify opts in two arguments");
|
|
704
|
+
const c = e;
|
|
705
|
+
c.BITS && (o = c.BITS), c.sqrt && (s = c.sqrt), typeof c.isLE == "boolean" && (r = c.isLE), typeof c.modFromBytes == "boolean" && (i = c.modFromBytes), a = c.allowedLengths;
|
|
706
|
+
} else
|
|
707
|
+
typeof e == "number" && (o = e), n.sqrt && (s = n.sqrt);
|
|
708
|
+
const { nBitLength: u, nByteLength: l } = pn(t, o);
|
|
709
|
+
if (l > 2048)
|
|
710
|
+
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
711
|
+
let m;
|
|
712
|
+
const y = Object.freeze({
|
|
713
|
+
ORDER: t,
|
|
714
|
+
isLE: r,
|
|
715
|
+
BITS: u,
|
|
716
|
+
BYTES: l,
|
|
717
|
+
MASK: Bt(u),
|
|
718
|
+
ZERO: M,
|
|
719
|
+
ONE: K,
|
|
720
|
+
allowedLengths: a,
|
|
721
|
+
create: (c) => D(c, t),
|
|
722
|
+
isValid: (c) => {
|
|
723
|
+
if (typeof c != "bigint")
|
|
724
|
+
throw new Error("invalid field element: expected bigint, got " + typeof c);
|
|
725
|
+
return M <= c && c < t;
|
|
726
|
+
},
|
|
727
|
+
is0: (c) => c === M,
|
|
728
|
+
// is valid and invertible
|
|
729
|
+
isValidNot0: (c) => !y.is0(c) && y.isValid(c),
|
|
730
|
+
isOdd: (c) => (c & K) === K,
|
|
731
|
+
neg: (c) => D(-c, t),
|
|
732
|
+
eql: (c, f) => c === f,
|
|
733
|
+
sqr: (c) => D(c * c, t),
|
|
734
|
+
add: (c, f) => D(c + f, t),
|
|
735
|
+
sub: (c, f) => D(c - f, t),
|
|
736
|
+
mul: (c, f) => D(c * f, t),
|
|
737
|
+
pow: (c, f) => dr(y, c, f),
|
|
738
|
+
div: (c, f) => D(c * Se(f, t), t),
|
|
739
|
+
// Same as above, but doesn't normalize
|
|
740
|
+
sqrN: (c) => c * c,
|
|
741
|
+
addN: (c, f) => c + f,
|
|
742
|
+
subN: (c, f) => c - f,
|
|
743
|
+
mulN: (c, f) => c * f,
|
|
744
|
+
inv: (c) => Se(c, t),
|
|
745
|
+
sqrt: s || ((c) => (m || (m = ur(t)), m(y, c))),
|
|
746
|
+
toBytes: (c) => r ? on(c, l) : Kt(c, l),
|
|
747
|
+
fromBytes: (c, f = !0) => {
|
|
748
|
+
if (a) {
|
|
749
|
+
if (!a.includes(c.length) || c.length > l)
|
|
750
|
+
throw new Error("Field.fromBytes: expected " + a + " bytes, got " + c.length);
|
|
751
|
+
const w = new Uint8Array(l);
|
|
752
|
+
w.set(c, r ? 0 : w.length - c.length), c = w;
|
|
753
|
+
}
|
|
754
|
+
if (c.length !== l)
|
|
755
|
+
throw new Error("Field.fromBytes: expected " + l + " bytes, got " + c.length);
|
|
756
|
+
let h = r ? rn(c) : wt(c);
|
|
757
|
+
if (i && (h = D(h, t)), !f && !y.isValid(h))
|
|
758
|
+
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
759
|
+
return h;
|
|
760
|
+
},
|
|
761
|
+
// TODO: we don't need it here, move out to separate fn
|
|
762
|
+
invertBatch: (c) => gn(y, c),
|
|
763
|
+
// We can't move this out because Fp6, Fp12 implement it
|
|
764
|
+
// and it's unclear what to return in there.
|
|
765
|
+
cmov: (c, f, h) => h ? f : c
|
|
766
|
+
});
|
|
767
|
+
return Object.freeze(y);
|
|
768
|
+
}
|
|
769
|
+
function mn(t) {
|
|
770
|
+
if (typeof t != "bigint")
|
|
771
|
+
throw new Error("field order must be bigint");
|
|
772
|
+
const e = t.toString(2).length;
|
|
773
|
+
return Math.ceil(e / 8);
|
|
774
|
+
}
|
|
775
|
+
function wn(t) {
|
|
776
|
+
const e = mn(t);
|
|
777
|
+
return e + Math.ceil(e / 2);
|
|
778
|
+
}
|
|
779
|
+
function yn(t, e, r = !1) {
|
|
780
|
+
const n = t.length, o = mn(e), s = wn(e);
|
|
781
|
+
if (n < 16 || n < s || n > 1024)
|
|
782
|
+
throw new Error("expected " + s + "-1024 bytes of input, got " + n);
|
|
783
|
+
const i = r ? rn(t) : wt(t), a = D(i, e - K) + K;
|
|
784
|
+
return r ? on(a, o) : Kt(a, o);
|
|
785
|
+
}
|
|
786
|
+
const pt = BigInt(0), ct = BigInt(1);
|
|
787
|
+
function Nt(t, e) {
|
|
788
|
+
const r = e.negate();
|
|
789
|
+
return t ? r : e;
|
|
790
|
+
}
|
|
791
|
+
function Dt(t, e) {
|
|
792
|
+
const r = gn(t.Fp, e.map((n) => n.Z));
|
|
793
|
+
return e.map((n, o) => t.fromAffine(n.toAffine(r[o])));
|
|
794
|
+
}
|
|
795
|
+
function bn(t, e) {
|
|
796
|
+
if (!Number.isSafeInteger(t) || t <= 0 || t > e)
|
|
797
|
+
throw new Error("invalid window size, expected [1.." + e + "], got W=" + t);
|
|
798
|
+
}
|
|
799
|
+
function zt(t, e) {
|
|
800
|
+
bn(t, e);
|
|
801
|
+
const r = Math.ceil(e / t) + 1, n = 2 ** (t - 1), o = 2 ** t, s = Bt(t), i = BigInt(t);
|
|
802
|
+
return { windows: r, windowSize: n, mask: s, maxNumber: o, shiftBy: i };
|
|
803
|
+
}
|
|
804
|
+
function Te(t, e, r) {
|
|
805
|
+
const { windowSize: n, mask: o, maxNumber: s, shiftBy: i } = r;
|
|
806
|
+
let a = Number(t & o), u = t >> i;
|
|
807
|
+
a > n && (a -= s, u += ct);
|
|
808
|
+
const l = e * n, m = l + Math.abs(a) - 1, y = a === 0, c = a < 0, f = e % 2 !== 0;
|
|
809
|
+
return { nextN: u, offset: m, isZero: y, isNeg: c, isNegF: f, offsetF: l };
|
|
810
|
+
}
|
|
811
|
+
function hr(t, e) {
|
|
812
|
+
if (!Array.isArray(t))
|
|
813
|
+
throw new Error("array expected");
|
|
814
|
+
t.forEach((r, n) => {
|
|
815
|
+
if (!(r instanceof e))
|
|
816
|
+
throw new Error("invalid point at index " + n);
|
|
817
|
+
});
|
|
818
|
+
}
|
|
819
|
+
function gr(t, e) {
|
|
820
|
+
if (!Array.isArray(t))
|
|
821
|
+
throw new Error("array of scalars expected");
|
|
822
|
+
t.forEach((r, n) => {
|
|
823
|
+
if (!e.isValid(r))
|
|
824
|
+
throw new Error("invalid scalar at index " + n);
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
const Xt = /* @__PURE__ */ new WeakMap(), En = /* @__PURE__ */ new WeakMap();
|
|
828
|
+
function Yt(t) {
|
|
829
|
+
return En.get(t) || 1;
|
|
830
|
+
}
|
|
831
|
+
function $e(t) {
|
|
832
|
+
if (t !== pt)
|
|
833
|
+
throw new Error("invalid wNAF");
|
|
834
|
+
}
|
|
835
|
+
class pr {
|
|
836
|
+
// Parametrized with a given Point class (not individual point)
|
|
837
|
+
constructor(e, r) {
|
|
838
|
+
this.BASE = e.BASE, this.ZERO = e.ZERO, this.Fn = e.Fn, this.bits = r;
|
|
839
|
+
}
|
|
840
|
+
// non-const time multiplication ladder
|
|
841
|
+
_unsafeLadder(e, r, n = this.ZERO) {
|
|
842
|
+
let o = e;
|
|
843
|
+
for (; r > pt; )
|
|
844
|
+
r & ct && (n = n.add(o)), o = o.double(), r >>= ct;
|
|
845
|
+
return n;
|
|
846
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
849
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
850
|
+
* Number of precomputed points depends on the curve size:
|
|
851
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
852
|
+
* - 𝑊 is the window size
|
|
853
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
854
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
855
|
+
* @param point Point instance
|
|
856
|
+
* @param W window size
|
|
857
|
+
* @returns precomputed point tables flattened to a single array
|
|
858
|
+
*/
|
|
859
|
+
precomputeWindow(e, r) {
|
|
860
|
+
const { windows: n, windowSize: o } = zt(r, this.bits), s = [];
|
|
861
|
+
let i = e, a = i;
|
|
862
|
+
for (let u = 0; u < n; u++) {
|
|
863
|
+
a = i, s.push(a);
|
|
864
|
+
for (let l = 1; l < o; l++)
|
|
865
|
+
a = a.add(i), s.push(a);
|
|
866
|
+
i = a.double();
|
|
867
|
+
}
|
|
868
|
+
return s;
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
872
|
+
* More compact implementation:
|
|
873
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
874
|
+
* @returns real and fake (for const-time) points
|
|
875
|
+
*/
|
|
876
|
+
wNAF(e, r, n) {
|
|
877
|
+
if (!this.Fn.isValid(n))
|
|
878
|
+
throw new Error("invalid scalar");
|
|
879
|
+
let o = this.ZERO, s = this.BASE;
|
|
880
|
+
const i = zt(e, this.bits);
|
|
881
|
+
for (let a = 0; a < i.windows; a++) {
|
|
882
|
+
const { nextN: u, offset: l, isZero: m, isNeg: y, isNegF: c, offsetF: f } = Te(n, a, i);
|
|
883
|
+
n = u, m ? s = s.add(Nt(c, r[f])) : o = o.add(Nt(y, r[l]));
|
|
884
|
+
}
|
|
885
|
+
return $e(n), { p: o, f: s };
|
|
886
|
+
}
|
|
887
|
+
/**
|
|
888
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
889
|
+
* @param acc accumulator point to add result of multiplication
|
|
890
|
+
* @returns point
|
|
891
|
+
*/
|
|
892
|
+
wNAFUnsafe(e, r, n, o = this.ZERO) {
|
|
893
|
+
const s = zt(e, this.bits);
|
|
894
|
+
for (let i = 0; i < s.windows && n !== pt; i++) {
|
|
895
|
+
const { nextN: a, offset: u, isZero: l, isNeg: m } = Te(n, i, s);
|
|
896
|
+
if (n = a, !l) {
|
|
897
|
+
const y = r[u];
|
|
898
|
+
o = o.add(m ? y.negate() : y);
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
return $e(n), o;
|
|
902
|
+
}
|
|
903
|
+
getPrecomputes(e, r, n) {
|
|
904
|
+
let o = Xt.get(r);
|
|
905
|
+
return o || (o = this.precomputeWindow(r, e), e !== 1 && (typeof n == "function" && (o = n(o)), Xt.set(r, o))), o;
|
|
906
|
+
}
|
|
907
|
+
cached(e, r, n) {
|
|
908
|
+
const o = Yt(e);
|
|
909
|
+
return this.wNAF(o, this.getPrecomputes(o, e, n), r);
|
|
910
|
+
}
|
|
911
|
+
unsafe(e, r, n, o) {
|
|
912
|
+
const s = Yt(e);
|
|
913
|
+
return s === 1 ? this._unsafeLadder(e, r, o) : this.wNAFUnsafe(s, this.getPrecomputes(s, e, n), r, o);
|
|
914
|
+
}
|
|
915
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
916
|
+
// using windowed method. This specifies window size and
|
|
917
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
918
|
+
createCache(e, r) {
|
|
919
|
+
bn(r, this.bits), En.set(e, r), Xt.delete(e);
|
|
920
|
+
}
|
|
921
|
+
hasCache(e) {
|
|
922
|
+
return Yt(e) !== 1;
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
function mr(t, e, r, n) {
|
|
926
|
+
let o = e, s = t.ZERO, i = t.ZERO;
|
|
927
|
+
for (; r > pt || n > pt; )
|
|
928
|
+
r & ct && (s = s.add(o)), n & ct && (i = i.add(o)), o = o.double(), r >>= ct, n >>= ct;
|
|
929
|
+
return { p1: s, p2: i };
|
|
930
|
+
}
|
|
931
|
+
function wr(t, e, r, n) {
|
|
932
|
+
hr(r, t), gr(n, e);
|
|
933
|
+
const o = r.length, s = n.length;
|
|
934
|
+
if (o !== s)
|
|
935
|
+
throw new Error("arrays of points and scalars must have equal length");
|
|
936
|
+
const i = t.ZERO, a = sn(BigInt(o));
|
|
937
|
+
let u = 1;
|
|
938
|
+
a > 12 ? u = a - 3 : a > 4 ? u = a - 2 : a > 0 && (u = 2);
|
|
939
|
+
const l = Bt(u), m = new Array(Number(l) + 1).fill(i), y = Math.floor((e.BITS - 1) / u) * u;
|
|
940
|
+
let c = i;
|
|
941
|
+
for (let f = y; f >= 0; f -= u) {
|
|
942
|
+
m.fill(i);
|
|
943
|
+
for (let w = 0; w < s; w++) {
|
|
944
|
+
const A = n[w], _ = Number(A >> BigInt(f) & l);
|
|
945
|
+
m[_] = m[_].add(r[w]);
|
|
946
|
+
}
|
|
947
|
+
let h = i;
|
|
948
|
+
for (let w = m.length - 1, A = i; w > 0; w--)
|
|
949
|
+
A = A.add(m[w]), h = h.add(A);
|
|
950
|
+
if (c = c.add(h), f !== 0)
|
|
951
|
+
for (let w = 0; w < u; w++)
|
|
952
|
+
c = c.double();
|
|
953
|
+
}
|
|
954
|
+
return c;
|
|
955
|
+
}
|
|
956
|
+
function _e(t, e, r) {
|
|
957
|
+
if (e) {
|
|
958
|
+
if (e.ORDER !== t)
|
|
959
|
+
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
960
|
+
return lr(e), e;
|
|
961
|
+
} else
|
|
962
|
+
return vt(t, { isLE: r });
|
|
963
|
+
}
|
|
964
|
+
function yr(t, e, r = {}, n) {
|
|
965
|
+
if (n === void 0 && (n = t === "edwards"), !e || typeof e != "object")
|
|
966
|
+
throw new Error(`expected valid ${t} CURVE object`);
|
|
967
|
+
for (const u of ["p", "n", "h"]) {
|
|
968
|
+
const l = e[u];
|
|
969
|
+
if (!(typeof l == "bigint" && l > pt))
|
|
970
|
+
throw new Error(`CURVE.${u} must be positive bigint`);
|
|
971
|
+
}
|
|
972
|
+
const o = _e(e.p, r.Fp, n), s = _e(e.n, r.Fn, n), a = ["Gx", "Gy", "a", "b"];
|
|
973
|
+
for (const u of a)
|
|
974
|
+
if (!o.isValid(e[u]))
|
|
975
|
+
throw new Error(`CURVE.${u} must be valid field element of CURVE.Fp`);
|
|
976
|
+
return e = Object.freeze(Object.assign({}, e)), { CURVE: e, Fp: o, Fn: s };
|
|
977
|
+
}
|
|
978
|
+
const Ve = (t, e) => (t + (t >= 0 ? e : -e) / xn) / e;
|
|
979
|
+
function br(t, e, r) {
|
|
980
|
+
const [[n, o], [s, i]] = e, a = Ve(i * t, r), u = Ve(-o * t, r);
|
|
981
|
+
let l = t - a * n - u * s, m = -a * o - u * i;
|
|
982
|
+
const y = l < Q, c = m < Q;
|
|
983
|
+
y && (l = -l), c && (m = -m);
|
|
984
|
+
const f = Bt(Math.ceil(sn(r) / 2)) + gt;
|
|
985
|
+
if (l < Q || l >= f || m < Q || m >= f)
|
|
986
|
+
throw new Error("splitScalar (endomorphism): failed, k=" + t);
|
|
987
|
+
return { k1neg: y, k1: l, k2neg: c, k2: m };
|
|
988
|
+
}
|
|
989
|
+
function se(t) {
|
|
990
|
+
if (!["compact", "recovered", "der"].includes(t))
|
|
991
|
+
throw new Error('Signature format must be "compact", "recovered", or "der"');
|
|
992
|
+
return t;
|
|
993
|
+
}
|
|
994
|
+
function jt(t, e) {
|
|
995
|
+
const r = {};
|
|
996
|
+
for (let n of Object.keys(e))
|
|
997
|
+
r[n] = t[n] === void 0 ? e[n] : t[n];
|
|
998
|
+
return Rt(r.lowS, "lowS"), Rt(r.prehash, "prehash"), r.format !== void 0 && se(r.format), r;
|
|
999
|
+
}
|
|
1000
|
+
class Er extends Error {
|
|
1001
|
+
constructor(e = "") {
|
|
1002
|
+
super(e);
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
const G = {
|
|
1006
|
+
// asn.1 DER encoding utils
|
|
1007
|
+
Err: Er,
|
|
1008
|
+
// Basic building block is TLV (Tag-Length-Value)
|
|
1009
|
+
_tlv: {
|
|
1010
|
+
encode: (t, e) => {
|
|
1011
|
+
const { Err: r } = G;
|
|
1012
|
+
if (t < 0 || t > 256)
|
|
1013
|
+
throw new r("tlv.encode: wrong tag");
|
|
1014
|
+
if (e.length & 1)
|
|
1015
|
+
throw new r("tlv.encode: unpadded data");
|
|
1016
|
+
const n = e.length / 2, o = Ct(n);
|
|
1017
|
+
if (o.length / 2 & 128)
|
|
1018
|
+
throw new r("tlv.encode: long form length too big");
|
|
1019
|
+
const s = n > 127 ? Ct(o.length / 2 | 128) : "";
|
|
1020
|
+
return Ct(t) + s + o + e;
|
|
1021
|
+
},
|
|
1022
|
+
// v - value, l - left bytes (unparsed)
|
|
1023
|
+
decode(t, e) {
|
|
1024
|
+
const { Err: r } = G;
|
|
1025
|
+
let n = 0;
|
|
1026
|
+
if (t < 0 || t > 256)
|
|
1027
|
+
throw new r("tlv.encode: wrong tag");
|
|
1028
|
+
if (e.length < 2 || e[n++] !== t)
|
|
1029
|
+
throw new r("tlv.decode: wrong tlv");
|
|
1030
|
+
const o = e[n++], s = !!(o & 128);
|
|
1031
|
+
let i = 0;
|
|
1032
|
+
if (!s)
|
|
1033
|
+
i = o;
|
|
1034
|
+
else {
|
|
1035
|
+
const u = o & 127;
|
|
1036
|
+
if (!u)
|
|
1037
|
+
throw new r("tlv.decode(long): indefinite length not supported");
|
|
1038
|
+
if (u > 4)
|
|
1039
|
+
throw new r("tlv.decode(long): byte length is too big");
|
|
1040
|
+
const l = e.subarray(n, n + u);
|
|
1041
|
+
if (l.length !== u)
|
|
1042
|
+
throw new r("tlv.decode: length bytes not complete");
|
|
1043
|
+
if (l[0] === 0)
|
|
1044
|
+
throw new r("tlv.decode(long): zero leftmost byte");
|
|
1045
|
+
for (const m of l)
|
|
1046
|
+
i = i << 8 | m;
|
|
1047
|
+
if (n += u, i < 128)
|
|
1048
|
+
throw new r("tlv.decode(long): not minimal encoding");
|
|
1049
|
+
}
|
|
1050
|
+
const a = e.subarray(n, n + i);
|
|
1051
|
+
if (a.length !== i)
|
|
1052
|
+
throw new r("tlv.decode: wrong value length");
|
|
1053
|
+
return { v: a, l: e.subarray(n + i) };
|
|
1054
|
+
}
|
|
1055
|
+
},
|
|
1056
|
+
// https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
|
|
1057
|
+
// since we always use positive integers here. It must always be empty:
|
|
1058
|
+
// - add zero byte if exists
|
|
1059
|
+
// - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
|
|
1060
|
+
_int: {
|
|
1061
|
+
encode(t) {
|
|
1062
|
+
const { Err: e } = G;
|
|
1063
|
+
if (t < Q)
|
|
1064
|
+
throw new e("integer: negative integers are not allowed");
|
|
1065
|
+
let r = Ct(t);
|
|
1066
|
+
if (Number.parseInt(r[0], 16) & 8 && (r = "00" + r), r.length & 1)
|
|
1067
|
+
throw new e("unexpected DER parsing assertion: unpadded hex");
|
|
1068
|
+
return r;
|
|
1069
|
+
},
|
|
1070
|
+
decode(t) {
|
|
1071
|
+
const { Err: e } = G;
|
|
1072
|
+
if (t[0] & 128)
|
|
1073
|
+
throw new e("invalid signature integer: negative");
|
|
1074
|
+
if (t[0] === 0 && !(t[1] & 128))
|
|
1075
|
+
throw new e("invalid signature integer: unnecessary leading zero");
|
|
1076
|
+
return wt(t);
|
|
1077
|
+
}
|
|
1078
|
+
},
|
|
1079
|
+
toSig(t) {
|
|
1080
|
+
const { Err: e, _int: r, _tlv: n } = G, o = O("signature", t), { v: s, l: i } = n.decode(48, o);
|
|
1081
|
+
if (i.length)
|
|
1082
|
+
throw new e("invalid signature: left bytes after parsing");
|
|
1083
|
+
const { v: a, l: u } = n.decode(2, s), { v: l, l: m } = n.decode(2, u);
|
|
1084
|
+
if (m.length)
|
|
1085
|
+
throw new e("invalid signature: left bytes after parsing");
|
|
1086
|
+
return { r: r.decode(a), s: r.decode(l) };
|
|
1087
|
+
},
|
|
1088
|
+
hexFromSig(t) {
|
|
1089
|
+
const { _tlv: e, _int: r } = G, n = e.encode(2, r.encode(t.r)), o = e.encode(2, r.encode(t.s)), s = n + o;
|
|
1090
|
+
return e.encode(48, s);
|
|
1091
|
+
}
|
|
1092
|
+
}, Q = BigInt(0), gt = BigInt(1), xn = BigInt(2), Tt = BigInt(3), xr = BigInt(4);
|
|
1093
|
+
function at(t, e) {
|
|
1094
|
+
const { BYTES: r } = t;
|
|
1095
|
+
let n;
|
|
1096
|
+
if (typeof e == "bigint")
|
|
1097
|
+
n = e;
|
|
1098
|
+
else {
|
|
1099
|
+
let o = O("private key", e);
|
|
1100
|
+
try {
|
|
1101
|
+
n = t.fromBytes(o);
|
|
1102
|
+
} catch {
|
|
1103
|
+
throw new Error(`invalid private key: expected ui8a of size ${r}, got ${typeof e}`);
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
if (!t.isValidNot0(n))
|
|
1107
|
+
throw new Error("invalid private key: out of range [1..N-1]");
|
|
1108
|
+
return n;
|
|
1109
|
+
}
|
|
1110
|
+
function Br(t, e = {}) {
|
|
1111
|
+
const r = yr("weierstrass", t, e), { Fp: n, Fn: o } = r;
|
|
1112
|
+
let s = r.CURVE;
|
|
1113
|
+
const { h: i, n: a } = s;
|
|
1114
|
+
de(e, {}, {
|
|
1115
|
+
allowInfinityPoint: "boolean",
|
|
1116
|
+
clearCofactor: "function",
|
|
1117
|
+
isTorsionFree: "function",
|
|
1118
|
+
fromBytes: "function",
|
|
1119
|
+
toBytes: "function",
|
|
1120
|
+
endo: "object",
|
|
1121
|
+
wrapPrivateKey: "boolean"
|
|
1122
|
+
});
|
|
1123
|
+
const { endo: u } = e;
|
|
1124
|
+
if (u && (!n.is0(s.a) || typeof u.beta != "bigint" || !Array.isArray(u.basises)))
|
|
1125
|
+
throw new Error('invalid endo: expected "beta": bigint and "basises": array');
|
|
1126
|
+
const l = vn(n, o);
|
|
1127
|
+
function m() {
|
|
1128
|
+
if (!n.isOdd)
|
|
1129
|
+
throw new Error("compression is not supported: Field does not have .isOdd()");
|
|
1130
|
+
}
|
|
1131
|
+
function y(T, p, g) {
|
|
1132
|
+
const { x: d, y: b } = p.toAffine(), E = n.toBytes(d);
|
|
1133
|
+
if (Rt(g, "isCompressed"), g) {
|
|
1134
|
+
m();
|
|
1135
|
+
const v = !n.isOdd(b);
|
|
1136
|
+
return Y(Bn(v), E);
|
|
1137
|
+
} else
|
|
1138
|
+
return Y(Uint8Array.of(4), E, n.toBytes(b));
|
|
1139
|
+
}
|
|
1140
|
+
function c(T) {
|
|
1141
|
+
st(T, void 0, "Point");
|
|
1142
|
+
const { publicKey: p, publicKeyUncompressed: g } = l, d = T.length, b = T[0], E = T.subarray(1);
|
|
1143
|
+
if (d === p && (b === 2 || b === 3)) {
|
|
1144
|
+
const v = n.fromBytes(E);
|
|
1145
|
+
if (!n.isValid(v))
|
|
1146
|
+
throw new Error("bad point: is not on curve, wrong x");
|
|
1147
|
+
const B = w(v);
|
|
1148
|
+
let x;
|
|
1149
|
+
try {
|
|
1150
|
+
x = n.sqrt(B);
|
|
1151
|
+
} catch (H) {
|
|
1152
|
+
const $ = H instanceof Error ? ": " + H.message : "";
|
|
1153
|
+
throw new Error("bad point: is not on curve, sqrt error" + $);
|
|
1154
|
+
}
|
|
1155
|
+
m();
|
|
1156
|
+
const P = n.isOdd(x);
|
|
1157
|
+
return (b & 1) === 1 !== P && (x = n.neg(x)), { x: v, y: x };
|
|
1158
|
+
} else if (d === g && b === 4) {
|
|
1159
|
+
const v = n.BYTES, B = n.fromBytes(E.subarray(0, v)), x = n.fromBytes(E.subarray(v, v * 2));
|
|
1160
|
+
if (!A(B, x))
|
|
1161
|
+
throw new Error("bad point: is not on curve");
|
|
1162
|
+
return { x: B, y: x };
|
|
1163
|
+
} else
|
|
1164
|
+
throw new Error(`bad point: got length ${d}, expected compressed=${p} or uncompressed=${g}`);
|
|
1165
|
+
}
|
|
1166
|
+
const f = e.toBytes || y, h = e.fromBytes || c;
|
|
1167
|
+
function w(T) {
|
|
1168
|
+
const p = n.sqr(T), g = n.mul(p, T);
|
|
1169
|
+
return n.add(n.add(g, n.mul(T, s.a)), s.b);
|
|
1170
|
+
}
|
|
1171
|
+
function A(T, p) {
|
|
1172
|
+
const g = n.sqr(p), d = w(T);
|
|
1173
|
+
return n.eql(g, d);
|
|
1174
|
+
}
|
|
1175
|
+
if (!A(s.Gx, s.Gy))
|
|
1176
|
+
throw new Error("bad curve params: generator point");
|
|
1177
|
+
const _ = n.mul(n.pow(s.a, Tt), xr), nt = n.mul(n.sqr(s.b), BigInt(27));
|
|
1178
|
+
if (n.is0(n.add(_, nt)))
|
|
1179
|
+
throw new Error("bad curve params: a or b");
|
|
1180
|
+
function V(T, p, g = !1) {
|
|
1181
|
+
if (!n.isValid(p) || g && n.is0(p))
|
|
1182
|
+
throw new Error(`bad point coordinate ${T}`);
|
|
1183
|
+
return p;
|
|
1184
|
+
}
|
|
1185
|
+
function j(T) {
|
|
1186
|
+
if (!(T instanceof S))
|
|
1187
|
+
throw new Error("ProjectivePoint expected");
|
|
1188
|
+
}
|
|
1189
|
+
function z(T) {
|
|
1190
|
+
if (!u || !u.basises)
|
|
1191
|
+
throw new Error("no endo");
|
|
1192
|
+
return br(T, u.basises, o.ORDER);
|
|
1193
|
+
}
|
|
1194
|
+
const q = Ae((T, p) => {
|
|
1195
|
+
const { X: g, Y: d, Z: b } = T;
|
|
1196
|
+
if (n.eql(b, n.ONE))
|
|
1197
|
+
return { x: g, y: d };
|
|
1198
|
+
const E = T.is0();
|
|
1199
|
+
p == null && (p = E ? n.ONE : n.inv(b));
|
|
1200
|
+
const v = n.mul(g, p), B = n.mul(d, p), x = n.mul(b, p);
|
|
1201
|
+
if (E)
|
|
1202
|
+
return { x: n.ZERO, y: n.ZERO };
|
|
1203
|
+
if (!n.eql(x, n.ONE))
|
|
1204
|
+
throw new Error("invZ was invalid");
|
|
1205
|
+
return { x: v, y: B };
|
|
1206
|
+
}), J = Ae((T) => {
|
|
1207
|
+
if (T.is0()) {
|
|
1208
|
+
if (e.allowInfinityPoint && !n.is0(T.Y))
|
|
1209
|
+
return;
|
|
1210
|
+
throw new Error("bad point: ZERO");
|
|
1211
|
+
}
|
|
1212
|
+
const { x: p, y: g } = T.toAffine();
|
|
1213
|
+
if (!n.isValid(p) || !n.isValid(g))
|
|
1214
|
+
throw new Error("bad point: x or y not field elements");
|
|
1215
|
+
if (!A(p, g))
|
|
1216
|
+
throw new Error("bad point: equation left != right");
|
|
1217
|
+
if (!T.isTorsionFree())
|
|
1218
|
+
throw new Error("bad point: not in prime-order subgroup");
|
|
1219
|
+
return !0;
|
|
1220
|
+
});
|
|
1221
|
+
function ft(T, p, g, d, b) {
|
|
1222
|
+
return g = new S(n.mul(g.X, T), g.Y, g.Z), p = Nt(d, p), g = Nt(b, g), p.add(g);
|
|
1223
|
+
}
|
|
1224
|
+
class S {
|
|
1225
|
+
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
1226
|
+
constructor(p, g, d) {
|
|
1227
|
+
this.X = V("x", p), this.Y = V("y", g, !0), this.Z = V("z", d), Object.freeze(this);
|
|
1228
|
+
}
|
|
1229
|
+
static CURVE() {
|
|
1230
|
+
return s;
|
|
1231
|
+
}
|
|
1232
|
+
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
1233
|
+
static fromAffine(p) {
|
|
1234
|
+
const { x: g, y: d } = p || {};
|
|
1235
|
+
if (!p || !n.isValid(g) || !n.isValid(d))
|
|
1236
|
+
throw new Error("invalid affine point");
|
|
1237
|
+
if (p instanceof S)
|
|
1238
|
+
throw new Error("projective point not allowed");
|
|
1239
|
+
return n.is0(g) && n.is0(d) ? S.ZERO : new S(g, d, n.ONE);
|
|
1240
|
+
}
|
|
1241
|
+
static fromBytes(p) {
|
|
1242
|
+
const g = S.fromAffine(h(st(p, void 0, "point")));
|
|
1243
|
+
return g.assertValidity(), g;
|
|
1244
|
+
}
|
|
1245
|
+
static fromHex(p) {
|
|
1246
|
+
return S.fromBytes(O("pointHex", p));
|
|
1247
|
+
}
|
|
1248
|
+
get x() {
|
|
1249
|
+
return this.toAffine().x;
|
|
1250
|
+
}
|
|
1251
|
+
get y() {
|
|
1252
|
+
return this.toAffine().y;
|
|
1253
|
+
}
|
|
1254
|
+
/**
|
|
1255
|
+
*
|
|
1256
|
+
* @param windowSize
|
|
1257
|
+
* @param isLazy true will defer table computation until the first multiplication
|
|
1258
|
+
* @returns
|
|
1259
|
+
*/
|
|
1260
|
+
precompute(p = 8, g = !0) {
|
|
1261
|
+
return rt.createCache(this, p), g || this.multiply(Tt), this;
|
|
1262
|
+
}
|
|
1263
|
+
// TODO: return `this`
|
|
1264
|
+
/** A point on curve is valid if it conforms to equation. */
|
|
1265
|
+
assertValidity() {
|
|
1266
|
+
J(this);
|
|
1267
|
+
}
|
|
1268
|
+
hasEvenY() {
|
|
1269
|
+
const { y: p } = this.toAffine();
|
|
1270
|
+
if (!n.isOdd)
|
|
1271
|
+
throw new Error("Field doesn't support isOdd");
|
|
1272
|
+
return !n.isOdd(p);
|
|
1273
|
+
}
|
|
1274
|
+
/** Compare one point to another. */
|
|
1275
|
+
equals(p) {
|
|
1276
|
+
j(p);
|
|
1277
|
+
const { X: g, Y: d, Z: b } = this, { X: E, Y: v, Z: B } = p, x = n.eql(n.mul(g, B), n.mul(E, b)), P = n.eql(n.mul(d, B), n.mul(v, b));
|
|
1278
|
+
return x && P;
|
|
1279
|
+
}
|
|
1280
|
+
/** Flips point to one corresponding to (x, -y) in Affine coordinates. */
|
|
1281
|
+
negate() {
|
|
1282
|
+
return new S(this.X, n.neg(this.Y), this.Z);
|
|
1283
|
+
}
|
|
1284
|
+
// Renes-Costello-Batina exception-free doubling formula.
|
|
1285
|
+
// There is 30% faster Jacobian formula, but it is not complete.
|
|
1286
|
+
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
1287
|
+
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
1288
|
+
double() {
|
|
1289
|
+
const { a: p, b: g } = s, d = n.mul(g, Tt), { X: b, Y: E, Z: v } = this;
|
|
1290
|
+
let B = n.ZERO, x = n.ZERO, P = n.ZERO, I = n.mul(b, b), H = n.mul(E, E), $ = n.mul(v, v), C = n.mul(b, E);
|
|
1291
|
+
return C = n.add(C, C), P = n.mul(b, v), P = n.add(P, P), B = n.mul(p, P), x = n.mul(d, $), x = n.add(B, x), B = n.sub(H, x), x = n.add(H, x), x = n.mul(B, x), B = n.mul(C, B), P = n.mul(d, P), $ = n.mul(p, $), C = n.sub(I, $), C = n.mul(p, C), C = n.add(C, P), P = n.add(I, I), I = n.add(P, I), I = n.add(I, $), I = n.mul(I, C), x = n.add(x, I), $ = n.mul(E, v), $ = n.add($, $), I = n.mul($, C), B = n.sub(B, I), P = n.mul($, H), P = n.add(P, P), P = n.add(P, P), new S(B, x, P);
|
|
1292
|
+
}
|
|
1293
|
+
// Renes-Costello-Batina exception-free addition formula.
|
|
1294
|
+
// There is 30% faster Jacobian formula, but it is not complete.
|
|
1295
|
+
// https://eprint.iacr.org/2015/1060, algorithm 1
|
|
1296
|
+
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
1297
|
+
add(p) {
|
|
1298
|
+
j(p);
|
|
1299
|
+
const { X: g, Y: d, Z: b } = this, { X: E, Y: v, Z: B } = p;
|
|
1300
|
+
let x = n.ZERO, P = n.ZERO, I = n.ZERO;
|
|
1301
|
+
const H = s.a, $ = n.mul(s.b, Tt);
|
|
1302
|
+
let C = n.mul(g, E), R = n.mul(d, v), k = n.mul(b, B), L = n.add(g, d), N = n.add(E, v);
|
|
1303
|
+
L = n.mul(L, N), N = n.add(C, R), L = n.sub(L, N), N = n.add(g, b);
|
|
1304
|
+
let U = n.add(E, B);
|
|
1305
|
+
return N = n.mul(N, U), U = n.add(C, k), N = n.sub(N, U), U = n.add(d, b), x = n.add(v, B), U = n.mul(U, x), x = n.add(R, k), U = n.sub(U, x), I = n.mul(H, N), x = n.mul($, k), I = n.add(x, I), x = n.sub(R, I), I = n.add(R, I), P = n.mul(x, I), R = n.add(C, C), R = n.add(R, C), k = n.mul(H, k), N = n.mul($, N), R = n.add(R, k), k = n.sub(C, k), k = n.mul(H, k), N = n.add(N, k), C = n.mul(R, N), P = n.add(P, C), C = n.mul(U, N), x = n.mul(L, x), x = n.sub(x, C), C = n.mul(L, R), I = n.mul(U, I), I = n.add(I, C), new S(x, P, I);
|
|
1306
|
+
}
|
|
1307
|
+
subtract(p) {
|
|
1308
|
+
return this.add(p.negate());
|
|
1309
|
+
}
|
|
1310
|
+
is0() {
|
|
1311
|
+
return this.equals(S.ZERO);
|
|
1312
|
+
}
|
|
1313
|
+
/**
|
|
1314
|
+
* Constant time multiplication.
|
|
1315
|
+
* Uses wNAF method. Windowed method may be 10% faster,
|
|
1316
|
+
* but takes 2x longer to generate and consumes 2x memory.
|
|
1317
|
+
* Uses precomputes when available.
|
|
1318
|
+
* Uses endomorphism for Koblitz curves.
|
|
1319
|
+
* @param scalar by which the point would be multiplied
|
|
1320
|
+
* @returns New point
|
|
1321
|
+
*/
|
|
1322
|
+
multiply(p) {
|
|
1323
|
+
const { endo: g } = e;
|
|
1324
|
+
if (!o.isValidNot0(p))
|
|
1325
|
+
throw new Error("invalid scalar: out of range");
|
|
1326
|
+
let d, b;
|
|
1327
|
+
const E = (v) => rt.cached(this, v, (B) => Dt(S, B));
|
|
1328
|
+
if (g) {
|
|
1329
|
+
const { k1neg: v, k1: B, k2neg: x, k2: P } = z(p), { p: I, f: H } = E(B), { p: $, f: C } = E(P);
|
|
1330
|
+
b = H.add(C), d = ft(g.beta, I, $, v, x);
|
|
1331
|
+
} else {
|
|
1332
|
+
const { p: v, f: B } = E(p);
|
|
1333
|
+
d = v, b = B;
|
|
1334
|
+
}
|
|
1335
|
+
return Dt(S, [d, b])[0];
|
|
1336
|
+
}
|
|
1337
|
+
/**
|
|
1338
|
+
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
1339
|
+
* It's faster, but should only be used when you don't care about
|
|
1340
|
+
* an exposed secret key e.g. sig verification, which works over *public* keys.
|
|
1341
|
+
*/
|
|
1342
|
+
multiplyUnsafe(p) {
|
|
1343
|
+
const { endo: g } = e, d = this;
|
|
1344
|
+
if (!o.isValid(p))
|
|
1345
|
+
throw new Error("invalid scalar: out of range");
|
|
1346
|
+
if (p === Q || d.is0())
|
|
1347
|
+
return S.ZERO;
|
|
1348
|
+
if (p === gt)
|
|
1349
|
+
return d;
|
|
1350
|
+
if (rt.hasCache(this))
|
|
1351
|
+
return this.multiply(p);
|
|
1352
|
+
if (g) {
|
|
1353
|
+
const { k1neg: b, k1: E, k2neg: v, k2: B } = z(p), { p1: x, p2: P } = mr(S, d, E, B);
|
|
1354
|
+
return ft(g.beta, x, P, b, v);
|
|
1355
|
+
} else
|
|
1356
|
+
return rt.unsafe(d, p);
|
|
1357
|
+
}
|
|
1358
|
+
multiplyAndAddUnsafe(p, g, d) {
|
|
1359
|
+
const b = this.multiplyUnsafe(g).add(p.multiplyUnsafe(d));
|
|
1360
|
+
return b.is0() ? void 0 : b;
|
|
1361
|
+
}
|
|
1362
|
+
/**
|
|
1363
|
+
* Converts Projective point to affine (x, y) coordinates.
|
|
1364
|
+
* @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
|
|
1365
|
+
*/
|
|
1366
|
+
toAffine(p) {
|
|
1367
|
+
return q(this, p);
|
|
1368
|
+
}
|
|
1369
|
+
/**
|
|
1370
|
+
* Checks whether Point is free of torsion elements (is in prime subgroup).
|
|
1371
|
+
* Always torsion-free for cofactor=1 curves.
|
|
1372
|
+
*/
|
|
1373
|
+
isTorsionFree() {
|
|
1374
|
+
const { isTorsionFree: p } = e;
|
|
1375
|
+
return i === gt ? !0 : p ? p(S, this) : rt.unsafe(this, a).is0();
|
|
1376
|
+
}
|
|
1377
|
+
clearCofactor() {
|
|
1378
|
+
const { clearCofactor: p } = e;
|
|
1379
|
+
return i === gt ? this : p ? p(S, this) : this.multiplyUnsafe(i);
|
|
1380
|
+
}
|
|
1381
|
+
isSmallOrder() {
|
|
1382
|
+
return this.multiplyUnsafe(i).is0();
|
|
1383
|
+
}
|
|
1384
|
+
toBytes(p = !0) {
|
|
1385
|
+
return Rt(p, "isCompressed"), this.assertValidity(), f(S, this, p);
|
|
1386
|
+
}
|
|
1387
|
+
toHex(p = !0) {
|
|
1388
|
+
return ht(this.toBytes(p));
|
|
1389
|
+
}
|
|
1390
|
+
toString() {
|
|
1391
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
1392
|
+
}
|
|
1393
|
+
// TODO: remove
|
|
1394
|
+
get px() {
|
|
1395
|
+
return this.X;
|
|
1396
|
+
}
|
|
1397
|
+
get py() {
|
|
1398
|
+
return this.X;
|
|
1399
|
+
}
|
|
1400
|
+
get pz() {
|
|
1401
|
+
return this.Z;
|
|
1402
|
+
}
|
|
1403
|
+
toRawBytes(p = !0) {
|
|
1404
|
+
return this.toBytes(p);
|
|
1405
|
+
}
|
|
1406
|
+
_setWindowSize(p) {
|
|
1407
|
+
this.precompute(p);
|
|
1408
|
+
}
|
|
1409
|
+
static normalizeZ(p) {
|
|
1410
|
+
return Dt(S, p);
|
|
1411
|
+
}
|
|
1412
|
+
static msm(p, g) {
|
|
1413
|
+
return wr(S, o, p, g);
|
|
1414
|
+
}
|
|
1415
|
+
static fromPrivateKey(p) {
|
|
1416
|
+
return S.BASE.multiply(at(o, p));
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
S.BASE = new S(s.Gx, s.Gy, n.ONE), S.ZERO = new S(n.ZERO, n.ONE, n.ZERO), S.Fp = n, S.Fn = o;
|
|
1420
|
+
const Pt = o.BITS, rt = new pr(S, e.endo ? Math.ceil(Pt / 2) : Pt);
|
|
1421
|
+
return S.BASE.precompute(8), S;
|
|
1422
|
+
}
|
|
1423
|
+
function Bn(t) {
|
|
1424
|
+
return Uint8Array.of(t ? 2 : 3);
|
|
1425
|
+
}
|
|
1426
|
+
function vn(t, e) {
|
|
1427
|
+
return {
|
|
1428
|
+
secretKey: e.BYTES,
|
|
1429
|
+
publicKey: 1 + t.BYTES,
|
|
1430
|
+
publicKeyUncompressed: 1 + 2 * t.BYTES,
|
|
1431
|
+
publicKeyHasPrefix: !0,
|
|
1432
|
+
signature: 2 * e.BYTES
|
|
1433
|
+
};
|
|
1434
|
+
}
|
|
1435
|
+
function vr(t, e = {}) {
|
|
1436
|
+
const { Fn: r } = t, n = e.randomBytes || Ut, o = Object.assign(vn(t.Fp, r), { seed: wn(r.ORDER) });
|
|
1437
|
+
function s(f) {
|
|
1438
|
+
try {
|
|
1439
|
+
return !!at(r, f);
|
|
1440
|
+
} catch {
|
|
1441
|
+
return !1;
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
function i(f, h) {
|
|
1445
|
+
const { publicKey: w, publicKeyUncompressed: A } = o;
|
|
1446
|
+
try {
|
|
1447
|
+
const _ = f.length;
|
|
1448
|
+
return h === !0 && _ !== w || h === !1 && _ !== A ? !1 : !!t.fromBytes(f);
|
|
1449
|
+
} catch {
|
|
1450
|
+
return !1;
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
function a(f = n(o.seed)) {
|
|
1454
|
+
return yn(st(f, o.seed, "seed"), r.ORDER);
|
|
1455
|
+
}
|
|
1456
|
+
function u(f, h = !0) {
|
|
1457
|
+
return t.BASE.multiply(at(r, f)).toBytes(h);
|
|
1458
|
+
}
|
|
1459
|
+
function l(f) {
|
|
1460
|
+
const h = a(f);
|
|
1461
|
+
return { secretKey: h, publicKey: u(h) };
|
|
1462
|
+
}
|
|
1463
|
+
function m(f) {
|
|
1464
|
+
if (typeof f == "bigint")
|
|
1465
|
+
return !1;
|
|
1466
|
+
if (f instanceof t)
|
|
1467
|
+
return !0;
|
|
1468
|
+
const { secretKey: h, publicKey: w, publicKeyUncompressed: A } = o;
|
|
1469
|
+
if (r.allowedLengths || h === w)
|
|
1470
|
+
return;
|
|
1471
|
+
const _ = O("key", f).length;
|
|
1472
|
+
return _ === w || _ === A;
|
|
1473
|
+
}
|
|
1474
|
+
function y(f, h, w = !0) {
|
|
1475
|
+
if (m(f) === !0)
|
|
1476
|
+
throw new Error("first arg must be private key");
|
|
1477
|
+
if (m(h) === !1)
|
|
1478
|
+
throw new Error("second arg must be public key");
|
|
1479
|
+
const A = at(r, f);
|
|
1480
|
+
return t.fromHex(h).multiply(A).toBytes(w);
|
|
1481
|
+
}
|
|
1482
|
+
return Object.freeze({ getPublicKey: u, getSharedSecret: y, keygen: l, Point: t, utils: {
|
|
1483
|
+
isValidSecretKey: s,
|
|
1484
|
+
isValidPublicKey: i,
|
|
1485
|
+
randomSecretKey: a,
|
|
1486
|
+
// TODO: remove
|
|
1487
|
+
isValidPrivateKey: s,
|
|
1488
|
+
randomPrivateKey: a,
|
|
1489
|
+
normPrivateKeyToScalar: (f) => at(r, f),
|
|
1490
|
+
precompute(f = 8, h = t.BASE) {
|
|
1491
|
+
return h.precompute(f, !1);
|
|
1492
|
+
}
|
|
1493
|
+
}, lengths: o });
|
|
1494
|
+
}
|
|
1495
|
+
function Pr(t, e, r = {}) {
|
|
1496
|
+
We(e), de(r, {}, {
|
|
1497
|
+
hmac: "function",
|
|
1498
|
+
lowS: "boolean",
|
|
1499
|
+
randomBytes: "function",
|
|
1500
|
+
bits2int: "function",
|
|
1501
|
+
bits2int_modN: "function"
|
|
1502
|
+
});
|
|
1503
|
+
const n = r.randomBytes || Ut, o = r.hmac || ((g, ...d) => en(e, g, Y(...d))), { Fp: s, Fn: i } = t, { ORDER: a, BITS: u } = i, { keygen: l, getPublicKey: m, getSharedSecret: y, utils: c, lengths: f } = vr(t, r), h = {
|
|
1504
|
+
prehash: !1,
|
|
1505
|
+
lowS: typeof r.lowS == "boolean" ? r.lowS : !1,
|
|
1506
|
+
format: void 0,
|
|
1507
|
+
//'compact' as ECDSASigFormat,
|
|
1508
|
+
extraEntropy: !1
|
|
1509
|
+
}, w = "compact";
|
|
1510
|
+
function A(g) {
|
|
1511
|
+
const d = a >> gt;
|
|
1512
|
+
return g > d;
|
|
1513
|
+
}
|
|
1514
|
+
function _(g, d) {
|
|
1515
|
+
if (!i.isValidNot0(d))
|
|
1516
|
+
throw new Error(`invalid signature ${g}: out of range 1..Point.Fn.ORDER`);
|
|
1517
|
+
return d;
|
|
1518
|
+
}
|
|
1519
|
+
function nt(g, d) {
|
|
1520
|
+
se(d);
|
|
1521
|
+
const b = f.signature, E = d === "compact" ? b : d === "recovered" ? b + 1 : void 0;
|
|
1522
|
+
return st(g, E, `${d} signature`);
|
|
1523
|
+
}
|
|
1524
|
+
class V {
|
|
1525
|
+
constructor(d, b, E) {
|
|
1526
|
+
this.r = _("r", d), this.s = _("s", b), E != null && (this.recovery = E), Object.freeze(this);
|
|
1527
|
+
}
|
|
1528
|
+
static fromBytes(d, b = w) {
|
|
1529
|
+
nt(d, b);
|
|
1530
|
+
let E;
|
|
1531
|
+
if (b === "der") {
|
|
1532
|
+
const { r: P, s: I } = G.toSig(st(d));
|
|
1533
|
+
return new V(P, I);
|
|
1534
|
+
}
|
|
1535
|
+
b === "recovered" && (E = d[0], b = "compact", d = d.subarray(1));
|
|
1536
|
+
const v = i.BYTES, B = d.subarray(0, v), x = d.subarray(v, v * 2);
|
|
1537
|
+
return new V(i.fromBytes(B), i.fromBytes(x), E);
|
|
1538
|
+
}
|
|
1539
|
+
static fromHex(d, b) {
|
|
1540
|
+
return this.fromBytes(Vt(d), b);
|
|
1541
|
+
}
|
|
1542
|
+
addRecoveryBit(d) {
|
|
1543
|
+
return new V(this.r, this.s, d);
|
|
1544
|
+
}
|
|
1545
|
+
recoverPublicKey(d) {
|
|
1546
|
+
const b = s.ORDER, { r: E, s: v, recovery: B } = this;
|
|
1547
|
+
if (B == null || ![0, 1, 2, 3].includes(B))
|
|
1548
|
+
throw new Error("recovery id invalid");
|
|
1549
|
+
if (a * xn < b && B > 1)
|
|
1550
|
+
throw new Error("recovery id is ambiguous for h>1 curve");
|
|
1551
|
+
const P = B === 2 || B === 3 ? E + a : E;
|
|
1552
|
+
if (!s.isValid(P))
|
|
1553
|
+
throw new Error("recovery id 2 or 3 invalid");
|
|
1554
|
+
const I = s.toBytes(P), H = t.fromBytes(Y(Bn((B & 1) === 0), I)), $ = i.inv(P), C = z(O("msgHash", d)), R = i.create(-C * $), k = i.create(v * $), L = t.BASE.multiplyUnsafe(R).add(H.multiplyUnsafe(k));
|
|
1555
|
+
if (L.is0())
|
|
1556
|
+
throw new Error("point at infinify");
|
|
1557
|
+
return L.assertValidity(), L;
|
|
1558
|
+
}
|
|
1559
|
+
// Signatures should be low-s, to prevent malleability.
|
|
1560
|
+
hasHighS() {
|
|
1561
|
+
return A(this.s);
|
|
1562
|
+
}
|
|
1563
|
+
toBytes(d = w) {
|
|
1564
|
+
if (se(d), d === "der")
|
|
1565
|
+
return Vt(G.hexFromSig(this));
|
|
1566
|
+
const b = i.toBytes(this.r), E = i.toBytes(this.s);
|
|
1567
|
+
if (d === "recovered") {
|
|
1568
|
+
if (this.recovery == null)
|
|
1569
|
+
throw new Error("recovery bit must be present");
|
|
1570
|
+
return Y(Uint8Array.of(this.recovery), b, E);
|
|
1571
|
+
}
|
|
1572
|
+
return Y(b, E);
|
|
1573
|
+
}
|
|
1574
|
+
toHex(d) {
|
|
1575
|
+
return ht(this.toBytes(d));
|
|
1576
|
+
}
|
|
1577
|
+
// TODO: remove
|
|
1578
|
+
assertValidity() {
|
|
1579
|
+
}
|
|
1580
|
+
static fromCompact(d) {
|
|
1581
|
+
return V.fromBytes(O("sig", d), "compact");
|
|
1582
|
+
}
|
|
1583
|
+
static fromDER(d) {
|
|
1584
|
+
return V.fromBytes(O("sig", d), "der");
|
|
1585
|
+
}
|
|
1586
|
+
normalizeS() {
|
|
1587
|
+
return this.hasHighS() ? new V(this.r, i.neg(this.s), this.recovery) : this;
|
|
1588
|
+
}
|
|
1589
|
+
toDERRawBytes() {
|
|
1590
|
+
return this.toBytes("der");
|
|
1591
|
+
}
|
|
1592
|
+
toDERHex() {
|
|
1593
|
+
return ht(this.toBytes("der"));
|
|
1594
|
+
}
|
|
1595
|
+
toCompactRawBytes() {
|
|
1596
|
+
return this.toBytes("compact");
|
|
1597
|
+
}
|
|
1598
|
+
toCompactHex() {
|
|
1599
|
+
return ht(this.toBytes("compact"));
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
const j = r.bits2int || function(d) {
|
|
1603
|
+
if (d.length > 8192)
|
|
1604
|
+
throw new Error("input is too large");
|
|
1605
|
+
const b = wt(d), E = d.length * 8 - u;
|
|
1606
|
+
return E > 0 ? b >> BigInt(E) : b;
|
|
1607
|
+
}, z = r.bits2int_modN || function(d) {
|
|
1608
|
+
return i.create(j(d));
|
|
1609
|
+
}, q = Bt(u);
|
|
1610
|
+
function J(g) {
|
|
1611
|
+
return rr("num < 2^" + u, g, Q, q), i.toBytes(g);
|
|
1612
|
+
}
|
|
1613
|
+
function ft(g, d) {
|
|
1614
|
+
return st(g, void 0, "message"), d ? st(e(g), void 0, "prehashed message") : g;
|
|
1615
|
+
}
|
|
1616
|
+
function S(g, d, b) {
|
|
1617
|
+
if (["recovered", "canonical"].some((R) => R in b))
|
|
1618
|
+
throw new Error("sign() legacy options not supported");
|
|
1619
|
+
const { lowS: E, prehash: v, extraEntropy: B } = jt(b, h);
|
|
1620
|
+
g = ft(g, v);
|
|
1621
|
+
const x = z(g), P = at(i, d), I = [J(P), J(x)];
|
|
1622
|
+
if (B != null && B !== !1) {
|
|
1623
|
+
const R = B === !0 ? n(f.secretKey) : B;
|
|
1624
|
+
I.push(O("extraEntropy", R));
|
|
1625
|
+
}
|
|
1626
|
+
const H = Y(...I), $ = x;
|
|
1627
|
+
function C(R) {
|
|
1628
|
+
const k = j(R);
|
|
1629
|
+
if (!i.isValidNot0(k))
|
|
1630
|
+
return;
|
|
1631
|
+
const L = i.inv(k), N = t.BASE.multiply(k).toAffine(), U = i.create(N.x);
|
|
1632
|
+
if (U === Q)
|
|
1633
|
+
return;
|
|
1634
|
+
const It = i.create(L * i.create($ + U * P));
|
|
1635
|
+
if (It === Q)
|
|
1636
|
+
return;
|
|
1637
|
+
let me = (N.x === U ? 0 : 2) | Number(N.y & gt), we = It;
|
|
1638
|
+
return E && A(It) && (we = i.neg(It), me ^= 1), new V(U, we, me);
|
|
1639
|
+
}
|
|
1640
|
+
return { seed: H, k2sig: C };
|
|
1641
|
+
}
|
|
1642
|
+
function Pt(g, d, b = {}) {
|
|
1643
|
+
g = O("message", g);
|
|
1644
|
+
const { seed: E, k2sig: v } = S(g, d, b);
|
|
1645
|
+
return or(e.outputLen, i.BYTES, o)(E, v);
|
|
1646
|
+
}
|
|
1647
|
+
function rt(g) {
|
|
1648
|
+
let d;
|
|
1649
|
+
const b = typeof g == "string" || qt(g), E = !b && g !== null && typeof g == "object" && typeof g.r == "bigint" && typeof g.s == "bigint";
|
|
1650
|
+
if (!b && !E)
|
|
1651
|
+
throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
|
|
1652
|
+
if (E)
|
|
1653
|
+
d = new V(g.r, g.s);
|
|
1654
|
+
else if (b) {
|
|
1655
|
+
try {
|
|
1656
|
+
d = V.fromBytes(O("sig", g), "der");
|
|
1657
|
+
} catch (v) {
|
|
1658
|
+
if (!(v instanceof G.Err))
|
|
1659
|
+
throw v;
|
|
1660
|
+
}
|
|
1661
|
+
if (!d)
|
|
1662
|
+
try {
|
|
1663
|
+
d = V.fromBytes(O("sig", g), "compact");
|
|
1664
|
+
} catch {
|
|
1665
|
+
return !1;
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
return d || !1;
|
|
1669
|
+
}
|
|
1670
|
+
function T(g, d, b, E = {}) {
|
|
1671
|
+
const { lowS: v, prehash: B, format: x } = jt(E, h);
|
|
1672
|
+
if (b = O("publicKey", b), d = ft(O("message", d), B), "strict" in E)
|
|
1673
|
+
throw new Error("options.strict was renamed to lowS");
|
|
1674
|
+
const P = x === void 0 ? rt(g) : V.fromBytes(O("sig", g), x);
|
|
1675
|
+
if (P === !1)
|
|
1676
|
+
return !1;
|
|
1677
|
+
try {
|
|
1678
|
+
const I = t.fromBytes(b);
|
|
1679
|
+
if (v && P.hasHighS())
|
|
1680
|
+
return !1;
|
|
1681
|
+
const { r: H, s: $ } = P, C = z(d), R = i.inv($), k = i.create(C * R), L = i.create(H * R), N = t.BASE.multiplyUnsafe(k).add(I.multiplyUnsafe(L));
|
|
1682
|
+
return N.is0() ? !1 : i.create(N.x) === H;
|
|
1683
|
+
} catch {
|
|
1684
|
+
return !1;
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
function p(g, d, b = {}) {
|
|
1688
|
+
const { prehash: E } = jt(b, h);
|
|
1689
|
+
return d = ft(d, E), V.fromBytes(g, "recovered").recoverPublicKey(d).toBytes();
|
|
1690
|
+
}
|
|
1691
|
+
return Object.freeze({
|
|
1692
|
+
keygen: l,
|
|
1693
|
+
getPublicKey: m,
|
|
1694
|
+
getSharedSecret: y,
|
|
1695
|
+
utils: c,
|
|
1696
|
+
lengths: f,
|
|
1697
|
+
Point: t,
|
|
1698
|
+
sign: Pt,
|
|
1699
|
+
verify: T,
|
|
1700
|
+
recoverPublicKey: p,
|
|
1701
|
+
Signature: V,
|
|
1702
|
+
hash: e
|
|
1703
|
+
});
|
|
1704
|
+
}
|
|
1705
|
+
function Ir(t) {
|
|
1706
|
+
const e = {
|
|
1707
|
+
a: t.a,
|
|
1708
|
+
b: t.b,
|
|
1709
|
+
p: t.Fp.ORDER,
|
|
1710
|
+
n: t.n,
|
|
1711
|
+
h: t.h,
|
|
1712
|
+
Gx: t.Gx,
|
|
1713
|
+
Gy: t.Gy
|
|
1714
|
+
}, r = t.Fp;
|
|
1715
|
+
let n = t.allowedPrivateKeyLengths ? Array.from(new Set(t.allowedPrivateKeyLengths.map((i) => Math.ceil(i / 2)))) : void 0;
|
|
1716
|
+
const o = vt(e.n, {
|
|
1717
|
+
BITS: t.nBitLength,
|
|
1718
|
+
allowedLengths: n,
|
|
1719
|
+
modFromBytes: t.wrapPrivateKey
|
|
1720
|
+
}), s = {
|
|
1721
|
+
Fp: r,
|
|
1722
|
+
Fn: o,
|
|
1723
|
+
allowInfinityPoint: t.allowInfinityPoint,
|
|
1724
|
+
endo: t.endo,
|
|
1725
|
+
isTorsionFree: t.isTorsionFree,
|
|
1726
|
+
clearCofactor: t.clearCofactor,
|
|
1727
|
+
fromBytes: t.fromBytes,
|
|
1728
|
+
toBytes: t.toBytes
|
|
1729
|
+
};
|
|
1730
|
+
return { CURVE: e, curveOpts: s };
|
|
1731
|
+
}
|
|
1732
|
+
function Ar(t) {
|
|
1733
|
+
const { CURVE: e, curveOpts: r } = Ir(t), n = {
|
|
1734
|
+
hmac: t.hmac,
|
|
1735
|
+
randomBytes: t.randomBytes,
|
|
1736
|
+
lowS: t.lowS,
|
|
1737
|
+
bits2int: t.bits2int,
|
|
1738
|
+
bits2int_modN: t.bits2int_modN
|
|
1739
|
+
};
|
|
1740
|
+
return { CURVE: e, curveOpts: r, hash: t.hash, ecdsaOpts: n };
|
|
1741
|
+
}
|
|
1742
|
+
function Sr(t, e) {
|
|
1743
|
+
const r = e.Point;
|
|
1744
|
+
return Object.assign({}, e, {
|
|
1745
|
+
ProjectivePoint: r,
|
|
1746
|
+
CURVE: Object.assign({}, t, pn(r.Fn.ORDER, r.Fn.BITS))
|
|
1747
|
+
});
|
|
1748
|
+
}
|
|
1749
|
+
function Cr(t) {
|
|
1750
|
+
const { CURVE: e, curveOpts: r, hash: n, ecdsaOpts: o } = Ar(t), s = Br(e, r), i = Pr(s, n, o);
|
|
1751
|
+
return Sr(t, i);
|
|
1752
|
+
}
|
|
1753
|
+
function Tr(t, e) {
|
|
1754
|
+
const r = (n) => Cr({ ...t, hash: n });
|
|
1755
|
+
return { ...r(e), create: r };
|
|
1756
|
+
}
|
|
1757
|
+
const mt = {
|
|
1758
|
+
p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),
|
|
1759
|
+
n: BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),
|
|
1760
|
+
h: BigInt(1),
|
|
1761
|
+
a: BigInt(0),
|
|
1762
|
+
b: BigInt(7),
|
|
1763
|
+
Gx: BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),
|
|
1764
|
+
Gy: BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")
|
|
1765
|
+
}, $r = {
|
|
1766
|
+
beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),
|
|
1767
|
+
basises: [
|
|
1768
|
+
[BigInt("0x3086d221a7d46bcde86c90e49284eb15"), -BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],
|
|
1769
|
+
[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"), BigInt("0x3086d221a7d46bcde86c90e49284eb15")]
|
|
1770
|
+
]
|
|
1771
|
+
}, _r = /* @__PURE__ */ BigInt(0), Re = /* @__PURE__ */ BigInt(1), ie = /* @__PURE__ */ BigInt(2);
|
|
1772
|
+
function Vr(t) {
|
|
1773
|
+
const e = mt.p, r = BigInt(3), n = BigInt(6), o = BigInt(11), s = BigInt(22), i = BigInt(23), a = BigInt(44), u = BigInt(88), l = t * t * t % e, m = l * l * t % e, y = Z(m, r, e) * m % e, c = Z(y, r, e) * m % e, f = Z(c, ie, e) * l % e, h = Z(f, o, e) * f % e, w = Z(h, s, e) * h % e, A = Z(w, a, e) * w % e, _ = Z(A, u, e) * A % e, nt = Z(_, a, e) * w % e, V = Z(nt, r, e) * m % e, j = Z(V, i, e) * h % e, z = Z(j, n, e) * l % e, q = Z(z, ie, e);
|
|
1774
|
+
if (!Ht.eql(Ht.sqr(q), t))
|
|
1775
|
+
throw new Error("Cannot find square root");
|
|
1776
|
+
return q;
|
|
1777
|
+
}
|
|
1778
|
+
const Ht = vt(mt.p, { sqrt: Vr }), Lt = Tr({ ...mt, Fp: Ht, lowS: !0, endo: $r }, ne), Ne = {};
|
|
1779
|
+
function kt(t, ...e) {
|
|
1780
|
+
let r = Ne[t];
|
|
1781
|
+
if (r === void 0) {
|
|
1782
|
+
const n = ne(Qe(t));
|
|
1783
|
+
r = Y(n, n), Ne[t] = r;
|
|
1784
|
+
}
|
|
1785
|
+
return ne(Y(r, ...e));
|
|
1786
|
+
}
|
|
1787
|
+
const ge = (t) => t.toBytes(!0).slice(1), yt = Lt.Point, pe = (t) => t % ie === _r;
|
|
1788
|
+
function ce(t) {
|
|
1789
|
+
const { Fn: e, BASE: r } = yt, n = at(e, t), o = r.multiply(n);
|
|
1790
|
+
return { scalar: pe(o.y) ? n : e.neg(n), bytes: ge(o) };
|
|
1791
|
+
}
|
|
1792
|
+
function Pn(t) {
|
|
1793
|
+
const e = Ht;
|
|
1794
|
+
if (!e.isValidNot0(t))
|
|
1795
|
+
throw new Error("invalid x: Fail if x ≥ p");
|
|
1796
|
+
const r = e.create(t * t), n = e.create(r * t + BigInt(7));
|
|
1797
|
+
let o = e.sqrt(n);
|
|
1798
|
+
pe(o) || (o = e.neg(o));
|
|
1799
|
+
const s = yt.fromAffine({ x: t, y: o });
|
|
1800
|
+
return s.assertValidity(), s;
|
|
1801
|
+
}
|
|
1802
|
+
const Et = wt;
|
|
1803
|
+
function In(...t) {
|
|
1804
|
+
return yt.Fn.create(Et(kt("BIP0340/challenge", ...t)));
|
|
1805
|
+
}
|
|
1806
|
+
function He(t) {
|
|
1807
|
+
return ce(t).bytes;
|
|
1808
|
+
}
|
|
1809
|
+
function Rr(t, e, r = Ut(32)) {
|
|
1810
|
+
const { Fn: n } = yt, o = O("message", t), { bytes: s, scalar: i } = ce(e), a = O("auxRand", r, 32), u = n.toBytes(i ^ Et(kt("BIP0340/aux", a))), l = kt("BIP0340/nonce", u, s, o), { bytes: m, scalar: y } = ce(l), c = In(m, s, o), f = new Uint8Array(64);
|
|
1811
|
+
if (f.set(m, 0), f.set(n.toBytes(n.create(y + c * i)), 32), !An(f, o, s))
|
|
1812
|
+
throw new Error("sign: Invalid signature produced");
|
|
1813
|
+
return f;
|
|
1814
|
+
}
|
|
1815
|
+
function An(t, e, r) {
|
|
1816
|
+
const { Fn: n, BASE: o } = yt, s = O("signature", t, 64), i = O("message", e), a = O("publicKey", r, 32);
|
|
1817
|
+
try {
|
|
1818
|
+
const u = Pn(Et(a)), l = Et(s.subarray(0, 32));
|
|
1819
|
+
if (!oe(l, Re, mt.p))
|
|
1820
|
+
return !1;
|
|
1821
|
+
const m = Et(s.subarray(32, 64));
|
|
1822
|
+
if (!oe(m, Re, mt.n))
|
|
1823
|
+
return !1;
|
|
1824
|
+
const y = In(n.toBytes(l), ge(u), i), c = o.multiplyUnsafe(m).add(u.multiplyUnsafe(n.neg(y))), { x: f, y: h } = c.toAffine();
|
|
1825
|
+
return !(c.is0() || !pe(h) || f !== l);
|
|
1826
|
+
} catch {
|
|
1827
|
+
return !1;
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
const bt = /* @__PURE__ */ (() => {
|
|
1831
|
+
const r = (o = Ut(48)) => yn(o, mt.n);
|
|
1832
|
+
Lt.utils.randomSecretKey;
|
|
1833
|
+
function n(o) {
|
|
1834
|
+
const s = r(o);
|
|
1835
|
+
return { secretKey: s, publicKey: He(s) };
|
|
1836
|
+
}
|
|
1837
|
+
return {
|
|
1838
|
+
keygen: n,
|
|
1839
|
+
getPublicKey: He,
|
|
1840
|
+
sign: Rr,
|
|
1841
|
+
verify: An,
|
|
1842
|
+
Point: yt,
|
|
1843
|
+
utils: {
|
|
1844
|
+
randomSecretKey: r,
|
|
1845
|
+
randomPrivateKey: r,
|
|
1846
|
+
taggedHash: kt,
|
|
1847
|
+
// TODO: remove
|
|
1848
|
+
lift_x: Pn,
|
|
1849
|
+
pointToBytes: ge,
|
|
1850
|
+
numberToBytesBE: Kt,
|
|
1851
|
+
bytesToNumberBE: wt,
|
|
1852
|
+
mod: D
|
|
1853
|
+
},
|
|
1854
|
+
lengths: {
|
|
1855
|
+
secretKey: 32,
|
|
1856
|
+
publicKey: 32,
|
|
1857
|
+
publicKeyHasPrefix: !1,
|
|
1858
|
+
signature: 64,
|
|
1859
|
+
seed: 48
|
|
1860
|
+
}
|
|
1861
|
+
};
|
|
1862
|
+
})(), Nr = "5120", ae = "0014", Hr = 20, ke = 33, kr = 65, Or = 2, qr = 3, Ur = 4;
|
|
1863
|
+
function Sn(t, e) {
|
|
1864
|
+
const r = t.startsWith("0x") || t.startsWith("0X") ? t.slice(2) : t;
|
|
1865
|
+
if (!/^(?:[0-9a-fA-F]{2})+$/.test(r))
|
|
1866
|
+
throw new Error(`${e} must be non-empty, even-length hex`);
|
|
1867
|
+
return `0x${r.toLowerCase()}`;
|
|
1868
|
+
}
|
|
1869
|
+
function Kr(t) {
|
|
1870
|
+
const e = Ze(t), r = bt.utils.lift_x(BigInt(`0x${t}`)), n = bt.utils.bytesToNumberBE(
|
|
1871
|
+
bt.utils.taggedHash("TapTweak", e)
|
|
1872
|
+
);
|
|
1873
|
+
if (n >= Lt.CURVE.n)
|
|
1874
|
+
throw new Error("Proof of possession BTC pubkey has an invalid TapTweak");
|
|
1875
|
+
const o = n === 0n ? r : r.add(bt.Point.BASE.multiply(n));
|
|
1876
|
+
return `0x${Nr}${ue(
|
|
1877
|
+
bt.utils.pointToBytes(o)
|
|
1878
|
+
)}`;
|
|
1879
|
+
}
|
|
1880
|
+
function Lr(t) {
|
|
1881
|
+
const e = Sn(t, "depositorBtcPubkeyRaw"), r = Ze(e.slice(2));
|
|
1882
|
+
if (r.length === 32)
|
|
1883
|
+
return {
|
|
1884
|
+
xOnlyPubkey: xt(e, "Payout BTC pubkey")
|
|
1885
|
+
};
|
|
1886
|
+
const n = r.length === ke && (r[0] === Or || r[0] === qr), o = r.length === kr && r[0] === Ur;
|
|
1887
|
+
if (!n && !o)
|
|
1888
|
+
throw new Error(
|
|
1889
|
+
"depositorBtcPubkeyRaw must be x-only, compressed, or uncompressed"
|
|
1890
|
+
);
|
|
1891
|
+
try {
|
|
1892
|
+
Lt.Point.fromBytes(r);
|
|
1893
|
+
} catch {
|
|
1894
|
+
throw new Error("depositorBtcPubkeyRaw is not a valid BTC pubkey");
|
|
1895
|
+
}
|
|
1896
|
+
return {
|
|
1897
|
+
xOnlyPubkey: ue(r.subarray(1, ke)),
|
|
1898
|
+
compressedPubkey: n ? r : void 0
|
|
1899
|
+
};
|
|
1900
|
+
}
|
|
1901
|
+
function Mr(t) {
|
|
1902
|
+
return `0x${ae}${ue(Xn(Jt(t)))}`;
|
|
1903
|
+
}
|
|
1904
|
+
function Fr(t, e, r) {
|
|
1905
|
+
const n = Sn(
|
|
1906
|
+
t,
|
|
1907
|
+
"depositorPayoutScriptPubKey"
|
|
1908
|
+
);
|
|
1909
|
+
if ((n.length - 2) / 2 > xe)
|
|
1910
|
+
throw new Error(
|
|
1911
|
+
`depositorPayoutScriptPubKey exceeds ${xe} bytes`
|
|
1912
|
+
);
|
|
1913
|
+
const o = e.startsWith("0x") || e.startsWith("0X") ? e.slice(2) : e, s = xt(
|
|
1914
|
+
`0x${o}`,
|
|
1915
|
+
"Proof of possession BTC pubkey"
|
|
1916
|
+
), i = Lr(r);
|
|
1917
|
+
if (i.xOnlyPubkey !== s)
|
|
1918
|
+
throw new Error(
|
|
1919
|
+
"depositorBtcPubkeyRaw does not match the proof of possession BTC pubkey"
|
|
1920
|
+
);
|
|
1921
|
+
if (n === Kr(s))
|
|
1922
|
+
return n;
|
|
1923
|
+
if (n.startsWith(`0x${ae}`) && n.length === 2 + ae.length + Hr * 2) {
|
|
1924
|
+
if (!i.compressedPubkey)
|
|
1925
|
+
throw new Error("A P2WPKH payout requires a compressed BTC pubkey");
|
|
1926
|
+
if (n === Mr(i.compressedPubkey))
|
|
1927
|
+
return n;
|
|
1928
|
+
}
|
|
1929
|
+
throw new Error(
|
|
1930
|
+
"depositorPayoutScriptPubKey does not match the proof of possession BTC pubkey"
|
|
1931
|
+
);
|
|
1932
|
+
}
|
|
1933
|
+
function Zr(t, e) {
|
|
1934
|
+
const r = t.startsWith("0x") ? t.slice(2) : t;
|
|
1935
|
+
if (r.length === 0 || r.length % 2 !== 0)
|
|
1936
|
+
throw new Error(`${e} must be non-empty, even-length hex`);
|
|
1937
|
+
if (!/^[0-9a-fA-F]+$/.test(r))
|
|
1938
|
+
throw new Error(`${e} contains non-hex characters`);
|
|
1939
|
+
const n = new Uint8Array(r.length / 2);
|
|
1940
|
+
for (let o = 0; o < n.length; o++)
|
|
1941
|
+
n[o] = Number.parseInt(r.slice(o * 2, o * 2 + 2), 16);
|
|
1942
|
+
return n;
|
|
1943
|
+
}
|
|
1944
|
+
function Dr(t) {
|
|
1945
|
+
return Array.from(t, (e) => e.toString(16).padStart(2, "0")).join(
|
|
1946
|
+
""
|
|
1947
|
+
);
|
|
1948
|
+
}
|
|
1949
|
+
function zr(t) {
|
|
1950
|
+
const e = new Uint8Array(
|
|
1951
|
+
t.reduce((n, o) => n + o.length, 0)
|
|
1952
|
+
);
|
|
1953
|
+
let r = 0;
|
|
1954
|
+
for (const n of t)
|
|
1955
|
+
e.set(n, r), r += n.length;
|
|
1956
|
+
return e;
|
|
1957
|
+
}
|
|
1958
|
+
function Xr(t) {
|
|
1959
|
+
let e = 0;
|
|
1960
|
+
const r = (c, f) => {
|
|
1961
|
+
if (!Number.isSafeInteger(c) || c < 0)
|
|
1962
|
+
throw new Error(`Invalid ${f} length ${c}`);
|
|
1963
|
+
if (e + c > t.length)
|
|
1964
|
+
throw new Error(`Truncated Bitcoin transaction while reading ${f}`);
|
|
1965
|
+
const h = t.slice(e, e + c);
|
|
1966
|
+
return e += c, h;
|
|
1967
|
+
}, n = (c, f) => {
|
|
1968
|
+
const h = r(c, f);
|
|
1969
|
+
let w = 0n;
|
|
1970
|
+
for (let A = h.length - 1; A >= 0; A--)
|
|
1971
|
+
w = w << 8n | BigInt(h[A]);
|
|
1972
|
+
return w;
|
|
1973
|
+
}, o = (c) => {
|
|
1974
|
+
const f = e, h = r(1, c)[0];
|
|
1975
|
+
let w;
|
|
1976
|
+
if (h < 253)
|
|
1977
|
+
w = BigInt(h);
|
|
1978
|
+
else if (h === 253) {
|
|
1979
|
+
if (w = n(2, c), w < 0xfdn) throw new Error(`Non-canonical ${c}`);
|
|
1980
|
+
} else if (h === 254) {
|
|
1981
|
+
if (w = n(4, c), w <= 0xffffn) throw new Error(`Non-canonical ${c}`);
|
|
1982
|
+
} else if (w = n(8, c), w <= 0xffffffffn) throw new Error(`Non-canonical ${c}`);
|
|
1983
|
+
if (w > BigInt(Number.MAX_SAFE_INTEGER))
|
|
1984
|
+
throw new Error(`${c} exceeds JavaScript's safe integer range`);
|
|
1985
|
+
return {
|
|
1986
|
+
encoded: t.slice(f, e),
|
|
1987
|
+
value: Number(w)
|
|
1988
|
+
};
|
|
1989
|
+
}, s = r(4, "version"), i = t[e] === 0 && t[e + 1] === 1;
|
|
1990
|
+
i && (e += 2);
|
|
1991
|
+
const a = o("input count"), u = [];
|
|
1992
|
+
for (let c = 0; c < a.value; c++) {
|
|
1993
|
+
const f = e;
|
|
1994
|
+
r(32, `input ${c} previous transaction hash`), r(4, `input ${c} previous output index`);
|
|
1995
|
+
const h = o(`input ${c} script length`);
|
|
1996
|
+
r(h.value, `input ${c} script`), r(4, `input ${c} sequence`), u.push(t.slice(f, e));
|
|
1997
|
+
}
|
|
1998
|
+
const l = o("output count"), m = [];
|
|
1999
|
+
for (let c = 0; c < l.value; c++) {
|
|
2000
|
+
const f = e;
|
|
2001
|
+
r(8, `output ${c} value`);
|
|
2002
|
+
const h = o(`output ${c} script length`);
|
|
2003
|
+
r(h.value, `output ${c} script`), m.push(t.slice(f, e));
|
|
2004
|
+
}
|
|
2005
|
+
if (i) {
|
|
2006
|
+
let c = !1;
|
|
2007
|
+
for (let f = 0; f < a.value; f++) {
|
|
2008
|
+
const h = o(`input ${f} witness item count`);
|
|
2009
|
+
c || (c = h.value > 0);
|
|
2010
|
+
for (let w = 0; w < h.value; w++) {
|
|
2011
|
+
const A = o(
|
|
2012
|
+
`input ${f} witness item ${w} length`
|
|
2013
|
+
);
|
|
2014
|
+
r(A.value, `input ${f} witness item ${w}`);
|
|
2015
|
+
}
|
|
2016
|
+
}
|
|
2017
|
+
if (!c)
|
|
2018
|
+
throw new Error("Bitcoin transaction has superfluous witness marker");
|
|
2019
|
+
}
|
|
2020
|
+
const y = r(4, "locktime");
|
|
2021
|
+
if (e !== t.length)
|
|
2022
|
+
throw new Error(
|
|
2023
|
+
`Bitcoin transaction has ${t.length - e} trailing byte(s)`
|
|
2024
|
+
);
|
|
2025
|
+
return zr([
|
|
2026
|
+
s,
|
|
2027
|
+
a.encoded,
|
|
2028
|
+
...u,
|
|
2029
|
+
l.encoded,
|
|
2030
|
+
...m,
|
|
2031
|
+
y
|
|
2032
|
+
]);
|
|
2033
|
+
}
|
|
2034
|
+
function Yr(t) {
|
|
2035
|
+
const e = Xr(
|
|
2036
|
+
Zr(t, "transactionHex")
|
|
2037
|
+
), r = Jt(Jt(e));
|
|
2038
|
+
return r.reverse(), `0x${Dr(r)}`;
|
|
2039
|
+
}
|
|
2040
|
+
function jr(t, e) {
|
|
2041
|
+
const r = t.startsWith("0x") ? t : `0x${t}`;
|
|
2042
|
+
if (!/^0x[0-9a-fA-F]{64}$/.test(r))
|
|
2043
|
+
throw new Error("peginTxHash must be a 32-byte hex value");
|
|
2044
|
+
const n = e.startsWith("0x") ? e : `0x${e}`;
|
|
2045
|
+
if (!$n(n, { strict: !0 }))
|
|
2046
|
+
throw new Error(`Invalid depositor Ethereum address: ${e}`);
|
|
2047
|
+
return _n(
|
|
2048
|
+
Vn(
|
|
2049
|
+
[{ type: "bytes32" }, { type: "address" }],
|
|
2050
|
+
[r, n]
|
|
2051
|
+
)
|
|
2052
|
+
).slice(2);
|
|
2053
|
+
}
|
|
2054
|
+
const Wt = 65535, Oe = 1e4, Ot = 4294967295, qe = 255;
|
|
2055
|
+
function Wr(t) {
|
|
2056
|
+
if (!Number.isInteger(t) || t < 0 || t > Ot)
|
|
2057
|
+
throw new Error(
|
|
2058
|
+
`Invalid offchainParamsVersion from contract: must be a uint32, got ${t}`
|
|
2059
|
+
);
|
|
2060
|
+
}
|
|
2061
|
+
function Gr(t) {
|
|
2062
|
+
const e = [];
|
|
2063
|
+
if (t.timelockAssert <= 0n && e.push(
|
|
2064
|
+
`timelockAssert must be positive, got ${t.timelockAssert}`
|
|
2065
|
+
), t.timelockAssert > BigInt(Wt) && e.push(
|
|
2066
|
+
`timelockAssert ${t.timelockAssert} exceeds uint16 max (${Wt})`
|
|
2067
|
+
), t.timelockChallengeAssert <= 0n && e.push(
|
|
2068
|
+
`timelockChallengeAssert must be positive, got ${t.timelockChallengeAssert}`
|
|
2069
|
+
), t.tRefund <= 0 && e.push(`tRefund must be positive, got ${t.tRefund}`), t.tStale <= 0 && e.push(`tStale must be positive, got ${t.tStale}`), t.securityCouncilKeys.length === 0 && e.push("securityCouncilKeys must not be empty"), t.councilQuorum <= 0 && e.push(`councilQuorum must be positive, got ${t.councilQuorum}`), t.councilQuorum > t.securityCouncilKeys.length && e.push(
|
|
2070
|
+
`councilQuorum (${t.councilQuorum}) exceeds securityCouncilKeys count (${t.securityCouncilKeys.length})`
|
|
2071
|
+
), t.feeRate <= 0n && e.push(`feeRate must be positive, got ${t.feeRate}`), t.minPeginFeeRate <= 0n && e.push(
|
|
2072
|
+
`minPeginFeeRate must be positive, got ${t.minPeginFeeRate}`
|
|
2073
|
+
), (!Number.isInteger(t.proverCircuitVersion) || t.proverCircuitVersion < 0 || t.proverCircuitVersion > Wt) && e.push(
|
|
2074
|
+
`proverCircuitVersion must be a uint16, got ${t.proverCircuitVersion}`
|
|
2075
|
+
), (!Number.isInteger(t.minPrepeginDepth) || t.minPrepeginDepth <= 0 || t.minPrepeginDepth > Ot) && e.push(
|
|
2076
|
+
`minPrepeginDepth must be a uint32 in [1, ${Ot}], got ${t.minPrepeginDepth}`
|
|
2077
|
+
), t.babeTotalInstances <= 0 && e.push(
|
|
2078
|
+
`babeTotalInstances must be positive, got ${t.babeTotalInstances}`
|
|
2079
|
+
), t.babeInstancesToFinalize <= 0 && e.push(
|
|
2080
|
+
`babeInstancesToFinalize must be positive, got ${t.babeInstancesToFinalize}`
|
|
2081
|
+
), t.babeInstancesToFinalize > t.babeTotalInstances && e.push(
|
|
2082
|
+
`babeInstancesToFinalize (${t.babeInstancesToFinalize}) exceeds babeTotalInstances (${t.babeTotalInstances})`
|
|
2083
|
+
), (t.minVpCommissionBps < 0 || t.minVpCommissionBps > Oe) && e.push(
|
|
2084
|
+
`minVpCommissionBps must be in [0, ${Oe}], got ${t.minVpCommissionBps}`
|
|
2085
|
+
), e.length > 0)
|
|
2086
|
+
throw new Error(
|
|
2087
|
+
`Invalid offchain protocol parameters: ${e.join("; ")}`
|
|
2088
|
+
);
|
|
2089
|
+
}
|
|
2090
|
+
function Qr(t) {
|
|
2091
|
+
const e = [];
|
|
2092
|
+
if (t.minimumPegInAmount <= 0n && e.push(
|
|
2093
|
+
`minimumPegInAmount must be positive, got ${t.minimumPegInAmount}`
|
|
2094
|
+
), t.maxPegInAmount < t.minimumPegInAmount && e.push(
|
|
2095
|
+
`maxPegInAmount (${t.maxPegInAmount}) must be >= minimumPegInAmount (${t.minimumPegInAmount})`
|
|
2096
|
+
), t.pegInAckTimeout <= 0n && e.push(
|
|
2097
|
+
`pegInAckTimeout must be positive, got ${t.pegInAckTimeout}`
|
|
2098
|
+
), t.pegInActivationTimeout <= 0n && e.push(
|
|
2099
|
+
`pegInActivationTimeout must be positive, got ${t.pegInActivationTimeout}`
|
|
2100
|
+
), (!Number.isInteger(t.maxHtlcOutputCount) || t.maxHtlcOutputCount <= 0 || t.maxHtlcOutputCount > qe) && e.push(
|
|
2101
|
+
`maxHtlcOutputCount must be an integer in [1, ${qe}], got ${t.maxHtlcOutputCount}`
|
|
2102
|
+
), (typeof t.expiredPegInGraceBlocks != "bigint" || t.expiredPegInGraceBlocks <= 0n) && e.push(
|
|
2103
|
+
`expiredPegInGraceBlocks must be a positive bigint, got ${t.expiredPegInGraceBlocks}`
|
|
2104
|
+
), e.length > 0)
|
|
2105
|
+
throw new Error(`Invalid TBV protocol parameters: ${e.join("; ")}`);
|
|
2106
|
+
}
|
|
2107
|
+
function po(t) {
|
|
2108
|
+
if (Qr(t), Gr(t.offchainParams), !Number.isInteger(t.offchainParamsVersion) || t.offchainParamsVersion < 0 || t.offchainParamsVersion > Ot)
|
|
2109
|
+
throw new Error(
|
|
2110
|
+
`Invalid peg-in configuration: offchainParamsVersion must be a uint32, got ${t.offchainParamsVersion}`
|
|
2111
|
+
);
|
|
2112
|
+
qn(
|
|
2113
|
+
t.activeVaultCoreVersion,
|
|
2114
|
+
"ProtocolParams.activeVaultCoreVersion()"
|
|
2115
|
+
);
|
|
2116
|
+
}
|
|
2117
|
+
const Ue = 9999;
|
|
2118
|
+
function Ke(t) {
|
|
2119
|
+
return {
|
|
2120
|
+
depositor: t.depositor,
|
|
2121
|
+
depositorBtcPubKey: t.depositorBtcPubKey,
|
|
2122
|
+
amount: t.amount,
|
|
2123
|
+
vaultProvider: t.vaultProvider,
|
|
2124
|
+
status: t.status,
|
|
2125
|
+
applicationEntryPoint: t.applicationEntryPoint,
|
|
2126
|
+
createdAt: t.createdAt
|
|
2127
|
+
};
|
|
2128
|
+
}
|
|
2129
|
+
function Gt(t) {
|
|
2130
|
+
const e = Number(t.offchainParamsVersion);
|
|
2131
|
+
return Wr(e), {
|
|
2132
|
+
depositorSignedPeginTx: t.depositorSignedPeginTx,
|
|
2133
|
+
universalChallengersVersion: t.universalChallengersVersion,
|
|
2134
|
+
appVaultKeepersVersion: t.appVaultKeepersVersion,
|
|
2135
|
+
offchainParamsVersion: e,
|
|
2136
|
+
verifiedAt: t.verifiedAt,
|
|
2137
|
+
depositorWotsPkHash: t.depositorWotsPkHash,
|
|
2138
|
+
hashlock: t.hashlock,
|
|
2139
|
+
htlcVout: t.htlcVout,
|
|
2140
|
+
depositorPopSignature: t.depositorPopSignature,
|
|
2141
|
+
prePeginTxHash: t.prePeginTxHash,
|
|
2142
|
+
vaultProviderCommissionBps: t.vaultProviderCommissionBps,
|
|
2143
|
+
vaultCoreVersion: t.vaultCoreVersion
|
|
2144
|
+
};
|
|
2145
|
+
}
|
|
2146
|
+
const Jr = 1n << 64n, Le = 0n;
|
|
2147
|
+
function Qt(t, e, r) {
|
|
2148
|
+
if (t < 0n || t >= Jr)
|
|
2149
|
+
throw new Error(
|
|
2150
|
+
`getBtcVaultProtocolInfo returned ${e}=${t} for vault ${r}, outside the uint64 range — the registry may predate RFC-006`
|
|
2151
|
+
);
|
|
2152
|
+
return t;
|
|
2153
|
+
}
|
|
2154
|
+
function Me(t, e) {
|
|
2155
|
+
return {
|
|
2156
|
+
vpKeyEpoch: Qt(t.vpKeyEpoch, "vpKeyEpoch", e),
|
|
2157
|
+
appKeeperKeyEpoch: Qt(
|
|
2158
|
+
t.appKeeperKeyEpoch,
|
|
2159
|
+
"appKeeperKeyEpoch",
|
|
2160
|
+
e
|
|
2161
|
+
),
|
|
2162
|
+
ucKeyEpoch: Qt(t.ucKeyEpoch, "ucKeyEpoch", e)
|
|
2163
|
+
};
|
|
2164
|
+
}
|
|
2165
|
+
class to {
|
|
2166
|
+
constructor(e, r) {
|
|
2167
|
+
this.publicClient = e, this.contractAddress = r;
|
|
2168
|
+
}
|
|
2169
|
+
/**
|
|
2170
|
+
* Read the VP's **genesis** (registration) x-only BTC pubkey — the key bonded
|
|
2171
|
+
* at version 0, which never moves when the operator rotates.
|
|
2172
|
+
*
|
|
2173
|
+
* Resolved as "the operation key at epoch 0" rather than through the
|
|
2174
|
+
* dedicated `getVaultProviderBTCKey` getter, which
|
|
2175
|
+
* https://github.com/babylonlabs-io/vault-contracts-aave-v4/pull/539 removes.
|
|
2176
|
+
* Epoch 0 predates any rotation — appended versions are stamped at epoch 1 or
|
|
2177
|
+
* later — so it resolves to the registration key, and the contracts team has
|
|
2178
|
+
* confirmed that is a property we can rely on rather than an implementation
|
|
2179
|
+
* detail. The devnet comparison behind that claim — both getters returning the
|
|
2180
|
+
* identical key for a provider that *has* rotated, while
|
|
2181
|
+
* `getCurrentOperationBtcKey` differed — is recorded in
|
|
2182
|
+
* https://github.com/babylonlabs-io/babylon-toolkit/issues/2188.
|
|
2183
|
+
*
|
|
2184
|
+
* This makes the read RFC-006-only, where the removed getter also existed on a
|
|
2185
|
+
* legacy registry. That costs nothing: every caller of this method already
|
|
2186
|
+
* resolves participant keys through `OperationKeyReader`, so all of them
|
|
2187
|
+
* require an RFC-006 registry regardless.
|
|
2188
|
+
*
|
|
2189
|
+
* Validates length, hex form, and secp256k1 curve membership before minting
|
|
2190
|
+
* the brand. Returns 64-char lowercase hex without the `0x` prefix.
|
|
2191
|
+
*/
|
|
2192
|
+
async getVaultProviderGenesisBtcPubKey(e, r) {
|
|
2193
|
+
const n = await this.publicClient.readContract({
|
|
2194
|
+
address: this.contractAddress,
|
|
2195
|
+
abi: F,
|
|
2196
|
+
functionName: "getOperationBtcKeyAtEpoch",
|
|
2197
|
+
args: [e, Le],
|
|
2198
|
+
blockNumber: r
|
|
2199
|
+
});
|
|
2200
|
+
return xt(
|
|
2201
|
+
n,
|
|
2202
|
+
`getOperationBtcKeyAtEpoch (vp=${e}, epoch=${Le})`
|
|
2203
|
+
);
|
|
2204
|
+
}
|
|
2205
|
+
/**
|
|
2206
|
+
* Read a vault provider's *current* RFC-006 operation BTC key.
|
|
2207
|
+
*
|
|
2208
|
+
* Falls back on-chain to the registration key when the provider has never
|
|
2209
|
+
* rotated, so this returns the same value as
|
|
2210
|
+
* `getVaultProviderGenesisBtcPubKey` until the first rotation.
|
|
2211
|
+
*
|
|
2212
|
+
* This is the key the VP's server signs its BIP-322 auth tokens with — a
|
|
2213
|
+
* live per-operator identity, not a per-vault binding, so the auth pin uses
|
|
2214
|
+
* the current key rather than any vault's frozen epoch.
|
|
2215
|
+
*/
|
|
2216
|
+
async getCurrentVaultProviderOperationBtcKey(e) {
|
|
2217
|
+
const r = await this.publicClient.readContract({
|
|
2218
|
+
address: this.contractAddress,
|
|
2219
|
+
abi: F,
|
|
2220
|
+
functionName: "getCurrentOperationBtcKey",
|
|
2221
|
+
args: [e]
|
|
2222
|
+
});
|
|
2223
|
+
return xt(
|
|
2224
|
+
r,
|
|
2225
|
+
`getCurrentOperationBtcKey (vp=${e})`
|
|
2226
|
+
);
|
|
2227
|
+
}
|
|
2228
|
+
/**
|
|
2229
|
+
* Read a vault's frozen RFC-006 operation-key epochs.
|
|
2230
|
+
*
|
|
2231
|
+
* Reads `getBtcVaultProtocolInfo` through the **extended** ABI, which is only
|
|
2232
|
+
* valid against an RFC-006 registry: against one whose `BTCVaultProtocolInfo`
|
|
2233
|
+
* struct is not extended this call does not fail for a populated vault, it
|
|
2234
|
+
* silently returns three words of tail data as epochs. Nothing here can detect
|
|
2235
|
+
* that, so the guarantee is a deployment one — every network this ships to has
|
|
2236
|
+
* the RFC-006 getters, and mainnet is a fresh RFC-006 deploy.
|
|
2237
|
+
*
|
|
2238
|
+
* A registry missing the operation-key getters entirely is the *safer* of the
|
|
2239
|
+
* two cases: `resolveParticipantKeysAtEpochs` reverts downstream and these
|
|
2240
|
+
* epochs never reach key resolution. See {@link UINT64_EXCLUSIVE_UPPER_BOUND}
|
|
2241
|
+
* for which state the range check actually guards, and
|
|
2242
|
+
* `BTCVaultRegistryKeyEpochs.abi.ts` for the decode hazard.
|
|
2243
|
+
*/
|
|
2244
|
+
async getVaultKeyEpochs(e) {
|
|
2245
|
+
const r = await this.publicClient.readContract({
|
|
2246
|
+
address: this.contractAddress,
|
|
2247
|
+
abi: be,
|
|
2248
|
+
functionName: "getBtcVaultProtocolInfo",
|
|
2249
|
+
args: [e]
|
|
2250
|
+
});
|
|
2251
|
+
return Me(r, e);
|
|
2252
|
+
}
|
|
2253
|
+
async getVaultKeyEpochsBatch(e) {
|
|
2254
|
+
return e.length === 0 ? [] : (await this.publicClient.multicall({
|
|
2255
|
+
contracts: e.map((n) => ({
|
|
2256
|
+
address: this.contractAddress,
|
|
2257
|
+
abi: be,
|
|
2258
|
+
functionName: "getBtcVaultProtocolInfo",
|
|
2259
|
+
args: [n]
|
|
2260
|
+
})),
|
|
2261
|
+
allowFailure: !1
|
|
2262
|
+
})).map(
|
|
2263
|
+
(n, o) => Me(n, e[o])
|
|
2264
|
+
);
|
|
2265
|
+
}
|
|
2266
|
+
async getVaultBasicInfo(e) {
|
|
2267
|
+
const r = await this.publicClient.readContract({
|
|
2268
|
+
address: this.contractAddress,
|
|
2269
|
+
abi: F,
|
|
2270
|
+
functionName: "getBtcVaultBasicInfo",
|
|
2271
|
+
args: [e]
|
|
2272
|
+
});
|
|
2273
|
+
return Ke(r);
|
|
2274
|
+
}
|
|
2275
|
+
async getVaultProtocolInfo(e) {
|
|
2276
|
+
const r = await this.publicClient.readContract({
|
|
2277
|
+
address: this.contractAddress,
|
|
2278
|
+
abi: F,
|
|
2279
|
+
functionName: "getBtcVaultProtocolInfo",
|
|
2280
|
+
args: [e]
|
|
2281
|
+
});
|
|
2282
|
+
return Gt(r);
|
|
2283
|
+
}
|
|
2284
|
+
async getProtocolInfoBatch(e) {
|
|
2285
|
+
return e.length === 0 ? [] : (await this.publicClient.multicall({
|
|
2286
|
+
contracts: e.map((n) => ({
|
|
2287
|
+
address: this.contractAddress,
|
|
2288
|
+
abi: F,
|
|
2289
|
+
functionName: "getBtcVaultProtocolInfo",
|
|
2290
|
+
args: [n]
|
|
2291
|
+
})),
|
|
2292
|
+
allowFailure: !1
|
|
2293
|
+
})).map((n, o) => {
|
|
2294
|
+
const s = n;
|
|
2295
|
+
if (!s.depositorSignedPeginTx || s.depositorSignedPeginTx === "0x")
|
|
2296
|
+
throw new Error(
|
|
2297
|
+
`Vault ${e[o]} not found on-chain or has no pegin transaction`
|
|
2298
|
+
);
|
|
2299
|
+
return Gt(s);
|
|
2300
|
+
});
|
|
2301
|
+
}
|
|
2302
|
+
/**
|
|
2303
|
+
* Read the protocol pegin fee (in wei) for a given vault provider.
|
|
2304
|
+
* Mirrors the `getPegInFee(address)` view on BTCVaultRegistry.
|
|
2305
|
+
*/
|
|
2306
|
+
async getPegInFee(e) {
|
|
2307
|
+
return await this.publicClient.readContract({
|
|
2308
|
+
address: this.contractAddress,
|
|
2309
|
+
abi: F,
|
|
2310
|
+
functionName: "getPegInFee",
|
|
2311
|
+
args: [e]
|
|
2312
|
+
});
|
|
2313
|
+
}
|
|
2314
|
+
/**
|
|
2315
|
+
* Read a vault provider's current commission in basis points from
|
|
2316
|
+
* BTCVaultRegistry. The contract enforces `commissionBps < 10000`, so the
|
|
2317
|
+
* legitimate range is `[0, 9999]`; anything outside indicates a wrong
|
|
2318
|
+
* contract address or ABI drift and is surfaced as an error rather than
|
|
2319
|
+
* trusted.
|
|
2320
|
+
*/
|
|
2321
|
+
async getVaultProviderCommission(e) {
|
|
2322
|
+
const r = await this.publicClient.readContract({
|
|
2323
|
+
address: this.contractAddress,
|
|
2324
|
+
abi: F,
|
|
2325
|
+
functionName: "getVaultProviderCommission",
|
|
2326
|
+
args: [e]
|
|
2327
|
+
});
|
|
2328
|
+
if (!Number.isInteger(r) || r < 0 || r > Ue)
|
|
2329
|
+
throw new Error(
|
|
2330
|
+
`getVaultProviderCommission returned ${r} bps for ${e}, outside the protocol range [0, ${Ue}]`
|
|
2331
|
+
);
|
|
2332
|
+
return r;
|
|
2333
|
+
}
|
|
2334
|
+
async getVaultData(e) {
|
|
2335
|
+
const [r, n] = await this.publicClient.multicall({
|
|
2336
|
+
contracts: [
|
|
2337
|
+
{
|
|
2338
|
+
address: this.contractAddress,
|
|
2339
|
+
abi: F,
|
|
2340
|
+
functionName: "getBtcVaultBasicInfo",
|
|
2341
|
+
args: [e]
|
|
2342
|
+
},
|
|
2343
|
+
{
|
|
2344
|
+
address: this.contractAddress,
|
|
2345
|
+
abi: F,
|
|
2346
|
+
functionName: "getBtcVaultProtocolInfo",
|
|
2347
|
+
args: [e]
|
|
2348
|
+
}
|
|
2349
|
+
],
|
|
2350
|
+
allowFailure: !1
|
|
2351
|
+
}), o = Ke(r), s = Gt(n);
|
|
2352
|
+
if (!s.depositorSignedPeginTx || s.depositorSignedPeginTx === "0x")
|
|
2353
|
+
throw new Error(
|
|
2354
|
+
`Vault ${e} not found on-chain or has no pegin transaction`
|
|
2355
|
+
);
|
|
2356
|
+
return { basic: o, protocol: s };
|
|
2357
|
+
}
|
|
2358
|
+
}
|
|
2359
|
+
const eo = 0, no = 255, ro = 12e4;
|
|
2360
|
+
function ot(t, e) {
|
|
2361
|
+
const r = t.startsWith("0x") ? t : t.startsWith("0X") ? `0x${t.slice(2)}` : `0x${t}`;
|
|
2362
|
+
if (!/^0x(?:[0-9a-fA-F]{2})+$/.test(r))
|
|
2363
|
+
throw new Error(`${e} must be non-empty, even-length hex`);
|
|
2364
|
+
return r;
|
|
2365
|
+
}
|
|
2366
|
+
function Fe(t, e) {
|
|
2367
|
+
if (!/^0x[0-9a-fA-F]{64}$/.test(t))
|
|
2368
|
+
throw new Error(`${e} must be a 32-byte hex value`);
|
|
2369
|
+
}
|
|
2370
|
+
function oo(t) {
|
|
2371
|
+
if (!Number.isInteger(t) || t < 0 || t > no)
|
|
2372
|
+
throw new Error(`htlcVout must be a uint8, got ${t}`);
|
|
2373
|
+
}
|
|
2374
|
+
class mo {
|
|
2375
|
+
constructor(e) {
|
|
2376
|
+
this.config = e;
|
|
2377
|
+
}
|
|
2378
|
+
async registerPeginOnChain(e) {
|
|
2379
|
+
const r = {
|
|
2380
|
+
...e,
|
|
2381
|
+
popSignature: { ...e.popSignature }
|
|
2382
|
+
};
|
|
2383
|
+
this.assertQuotedCommissionPresentWhenRequired(r.quotedCommissionBps);
|
|
2384
|
+
const n = this.assertPopMatchesEthAccount(r.popSignature), o = this.normalizeBtcPubkey(r.popSignature), s = ot(
|
|
2385
|
+
r.popSignature.btcPopSignature,
|
|
2386
|
+
"btcPopSignature"
|
|
2387
|
+
), i = this.normalizeRequest(
|
|
2388
|
+
r,
|
|
2389
|
+
n,
|
|
2390
|
+
o
|
|
2391
|
+
);
|
|
2392
|
+
await this.assertVaultDoesNotExist(
|
|
2393
|
+
i.vaultId,
|
|
2394
|
+
i.peginTxHash
|
|
2395
|
+
);
|
|
2396
|
+
const a = await this.readPeginFee(r.vaultProvider), u = await this.resolveMaxAcceptableCommissionBps(
|
|
2397
|
+
r.vaultProvider,
|
|
2398
|
+
r.quotedCommissionBps
|
|
2399
|
+
), l = ye({
|
|
2400
|
+
abi: F,
|
|
2401
|
+
functionName: "submitPeginRequest",
|
|
2402
|
+
args: [
|
|
2403
|
+
n,
|
|
2404
|
+
o,
|
|
2405
|
+
s,
|
|
2406
|
+
i.unsignedPrePeginTx,
|
|
2407
|
+
i.depositorSignedPeginTx,
|
|
2408
|
+
r.vaultProvider,
|
|
2409
|
+
u,
|
|
2410
|
+
r.hashlock,
|
|
2411
|
+
r.htlcVout,
|
|
2412
|
+
i.payoutScript,
|
|
2413
|
+
r.depositorWotsPkHash
|
|
2414
|
+
]
|
|
2415
|
+
});
|
|
2416
|
+
return {
|
|
2417
|
+
ethTxHash: await this.sendAndWait(
|
|
2418
|
+
l,
|
|
2419
|
+
a,
|
|
2420
|
+
!1,
|
|
2421
|
+
n
|
|
2422
|
+
),
|
|
2423
|
+
vaultId: i.vaultId,
|
|
2424
|
+
peginTxHash: i.peginTxHash
|
|
2425
|
+
};
|
|
2426
|
+
}
|
|
2427
|
+
async registerPeginBatchOnChain(e) {
|
|
2428
|
+
const r = {
|
|
2429
|
+
...e,
|
|
2430
|
+
popSignature: { ...e.popSignature },
|
|
2431
|
+
requests: e.requests.map((h) => ({ ...h }))
|
|
2432
|
+
};
|
|
2433
|
+
if (r.requests.length === 0)
|
|
2434
|
+
throw new Error("Batch pegin requires at least one request");
|
|
2435
|
+
this.assertQuotedCommissionPresentWhenRequired(r.quotedCommissionBps);
|
|
2436
|
+
const n = this.assertPopMatchesEthAccount(r.popSignature), o = ot(
|
|
2437
|
+
r.unsignedPrePeginTx,
|
|
2438
|
+
"unsignedPrePeginTx"
|
|
2439
|
+
), s = this.normalizeBtcPubkey(r.popSignature), i = ot(
|
|
2440
|
+
r.popSignature.btcPopSignature,
|
|
2441
|
+
"btcPopSignature"
|
|
2442
|
+
), a = [], u = [], l = /* @__PURE__ */ new Set();
|
|
2443
|
+
for (const h of r.requests) {
|
|
2444
|
+
const w = this.normalizeRequest(
|
|
2445
|
+
{
|
|
2446
|
+
...h,
|
|
2447
|
+
unsignedPrePeginTx: r.unsignedPrePeginTx,
|
|
2448
|
+
vaultProvider: r.vaultProvider,
|
|
2449
|
+
depositorBtcPubkeyRaw: r.depositorBtcPubkeyRaw
|
|
2450
|
+
},
|
|
2451
|
+
n,
|
|
2452
|
+
s
|
|
2453
|
+
);
|
|
2454
|
+
if (l.has(w.vaultId))
|
|
2455
|
+
throw new Error(`Duplicate vault in batch: ${w.vaultId}`);
|
|
2456
|
+
l.add(w.vaultId), a.push({
|
|
2457
|
+
vaultId: w.vaultId,
|
|
2458
|
+
peginTxHash: w.peginTxHash
|
|
2459
|
+
}), u.push({
|
|
2460
|
+
depositorBtcPubKey: s,
|
|
2461
|
+
btcPopSignature: i,
|
|
2462
|
+
unsignedPrePeginTx: o,
|
|
2463
|
+
depositorSignedPeginTx: w.depositorSignedPeginTx,
|
|
2464
|
+
hashlock: h.hashlock,
|
|
2465
|
+
htlcVout: h.htlcVout,
|
|
2466
|
+
referralCode: eo,
|
|
2467
|
+
depositorPayoutBtcAddress: w.payoutScript,
|
|
2468
|
+
depositorWotsPkHash: h.depositorWotsPkHash
|
|
2469
|
+
});
|
|
2470
|
+
}
|
|
2471
|
+
for (const h of a)
|
|
2472
|
+
await this.assertVaultDoesNotExist(h.vaultId, h.peginTxHash);
|
|
2473
|
+
const m = await this.readPeginFee(r.vaultProvider), y = await this.resolveMaxAcceptableCommissionBps(
|
|
2474
|
+
r.vaultProvider,
|
|
2475
|
+
r.quotedCommissionBps
|
|
2476
|
+
), c = ye({
|
|
2477
|
+
abi: F,
|
|
2478
|
+
functionName: "submitPeginRequestBatch",
|
|
2479
|
+
args: [n, r.vaultProvider, y, u]
|
|
2480
|
+
});
|
|
2481
|
+
return { ethTxHash: await this.sendAndWait(
|
|
2482
|
+
c,
|
|
2483
|
+
m * BigInt(r.requests.length),
|
|
2484
|
+
!0,
|
|
2485
|
+
n
|
|
2486
|
+
), vaults: a };
|
|
2487
|
+
}
|
|
2488
|
+
assertQuotedCommissionPresentWhenRequired(e) {
|
|
2489
|
+
if (e === void 0 && this.config.requireQuotedCommissionBps)
|
|
2490
|
+
throw new Error(
|
|
2491
|
+
"quotedCommissionBps is required when the wallet approved deposit terms: the registration ceiling must anchor to the approved quote."
|
|
2492
|
+
);
|
|
2493
|
+
}
|
|
2494
|
+
assertPopMatchesEthAccount(e) {
|
|
2495
|
+
return this.assertEthAccount(e.depositorEthAddress);
|
|
2496
|
+
}
|
|
2497
|
+
assertEthAccount(e) {
|
|
2498
|
+
const r = this.config.ethWallet.account;
|
|
2499
|
+
if (!r) throw new Error("Ethereum wallet account not found");
|
|
2500
|
+
if (!Rn(e, r.address))
|
|
2501
|
+
throw new Error(
|
|
2502
|
+
`Proof of possession was signed for ${e} but the Ethereum wallet is currently connected to ${r.address}. Reconnect the original account or sign a new proof of possession.`
|
|
2503
|
+
);
|
|
2504
|
+
return r.address;
|
|
2505
|
+
}
|
|
2506
|
+
normalizeBtcPubkey(e) {
|
|
2507
|
+
return `0x${xt(
|
|
2508
|
+
ot(e.depositorBtcPubkey, "Proof of possession BTC pubkey"),
|
|
2509
|
+
"Proof of possession BTC pubkey"
|
|
2510
|
+
)}`;
|
|
2511
|
+
}
|
|
2512
|
+
normalizeRequest(e, r, n) {
|
|
2513
|
+
Fe(e.hashlock, "hashlock"), Fe(e.depositorWotsPkHash, "depositorWotsPkHash"), oo(e.htlcVout);
|
|
2514
|
+
const o = ot(
|
|
2515
|
+
e.unsignedPrePeginTx,
|
|
2516
|
+
"unsignedPrePeginTx"
|
|
2517
|
+
), s = ot(
|
|
2518
|
+
e.depositorSignedPeginTx,
|
|
2519
|
+
"depositorSignedPeginTx"
|
|
2520
|
+
), i = Yr(s);
|
|
2521
|
+
return {
|
|
2522
|
+
unsignedPrePeginTx: o,
|
|
2523
|
+
depositorSignedPeginTx: s,
|
|
2524
|
+
payoutScript: Fr(
|
|
2525
|
+
e.depositorPayoutScriptPubKey,
|
|
2526
|
+
n,
|
|
2527
|
+
e.depositorBtcPubkeyRaw
|
|
2528
|
+
),
|
|
2529
|
+
peginTxHash: i,
|
|
2530
|
+
vaultId: ot(jr(i, r), "vaultId")
|
|
2531
|
+
};
|
|
2532
|
+
}
|
|
2533
|
+
async assertVaultDoesNotExist(e, r) {
|
|
2534
|
+
if ((await this.config.publicClient.readContract({
|
|
2535
|
+
address: this.config.btcVaultRegistry,
|
|
2536
|
+
abi: F,
|
|
2537
|
+
functionName: "getBtcVaultBasicInfo",
|
|
2538
|
+
args: [e]
|
|
2539
|
+
})).depositor !== Nn)
|
|
2540
|
+
throw new Error(
|
|
2541
|
+
`Vault already exists (ID: ${e}, peginTxHash: ${r}). 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.`
|
|
2542
|
+
);
|
|
2543
|
+
}
|
|
2544
|
+
async readPeginFee(e) {
|
|
2545
|
+
try {
|
|
2546
|
+
return await this.config.publicClient.readContract({
|
|
2547
|
+
address: this.config.btcVaultRegistry,
|
|
2548
|
+
abi: F,
|
|
2549
|
+
functionName: "getPegInFee",
|
|
2550
|
+
args: [e]
|
|
2551
|
+
});
|
|
2552
|
+
} catch (r) {
|
|
2553
|
+
throw new Error(
|
|
2554
|
+
"Failed to query pegin fee from the contract. Please check your network connection and contract address.",
|
|
2555
|
+
{ cause: r }
|
|
2556
|
+
);
|
|
2557
|
+
}
|
|
2558
|
+
}
|
|
2559
|
+
async resolveMaxAcceptableCommissionBps(e, r) {
|
|
2560
|
+
let n;
|
|
2561
|
+
try {
|
|
2562
|
+
n = await new to(
|
|
2563
|
+
this.config.publicClient,
|
|
2564
|
+
this.config.btcVaultRegistry
|
|
2565
|
+
).getVaultProviderCommission(e);
|
|
2566
|
+
} catch (o) {
|
|
2567
|
+
throw new Error(
|
|
2568
|
+
"Failed to query vault provider commission from the contract. Please check your network connection and contract address.",
|
|
2569
|
+
{ cause: o }
|
|
2570
|
+
);
|
|
2571
|
+
}
|
|
2572
|
+
if (r !== void 0) {
|
|
2573
|
+
if (n > r + te)
|
|
2574
|
+
throw new Error(
|
|
2575
|
+
`Vault provider commission changed since quote: quoted ${r} bps, chain currently reports ${n} bps (allowed drift ${te} bps). Please refresh and try again.`
|
|
2576
|
+
);
|
|
2577
|
+
return ve(r);
|
|
2578
|
+
}
|
|
2579
|
+
return ve(n);
|
|
2580
|
+
}
|
|
2581
|
+
async sendAndWait(e, r, n, o) {
|
|
2582
|
+
let s = this.assertEthAccount(o), i;
|
|
2583
|
+
try {
|
|
2584
|
+
i = await this.config.publicClient.estimateGas({
|
|
2585
|
+
to: this.config.btcVaultRegistry,
|
|
2586
|
+
data: e,
|
|
2587
|
+
value: r,
|
|
2588
|
+
account: s
|
|
2589
|
+
});
|
|
2590
|
+
} catch (l) {
|
|
2591
|
+
Mt(l);
|
|
2592
|
+
}
|
|
2593
|
+
s = this.assertEthAccount(o);
|
|
2594
|
+
let a;
|
|
2595
|
+
try {
|
|
2596
|
+
a = await this.config.ethWallet.sendTransaction({
|
|
2597
|
+
to: this.config.btcVaultRegistry,
|
|
2598
|
+
data: e,
|
|
2599
|
+
value: r,
|
|
2600
|
+
gas: i,
|
|
2601
|
+
account: s,
|
|
2602
|
+
chain: this.config.ethChain
|
|
2603
|
+
});
|
|
2604
|
+
} catch (l) {
|
|
2605
|
+
Mt(l);
|
|
2606
|
+
}
|
|
2607
|
+
const u = await Hn({
|
|
2608
|
+
publicClient: this.config.publicClient,
|
|
2609
|
+
walletAddress: o,
|
|
2610
|
+
hash: a,
|
|
2611
|
+
timeout: this.config.receiptTimeoutMs ?? ro
|
|
2612
|
+
});
|
|
2613
|
+
return u.status === "reverted" && Mt(
|
|
2614
|
+
new Error(
|
|
2615
|
+
`${n ? "Batch transaction" : "Transaction"} reverted. Hash: ${u.transactionHash}. Check a block explorer for details.`
|
|
2616
|
+
)
|
|
2617
|
+
), u.transactionHash;
|
|
2618
|
+
}
|
|
2619
|
+
}
|
|
2620
|
+
export {
|
|
2621
|
+
te as C,
|
|
2622
|
+
Un as M,
|
|
2623
|
+
eo as N,
|
|
2624
|
+
mo as V,
|
|
2625
|
+
po as a,
|
|
2626
|
+
Qr as b,
|
|
2627
|
+
to as c,
|
|
2628
|
+
jr as d,
|
|
2629
|
+
ve as e,
|
|
2630
|
+
Yr as f,
|
|
2631
|
+
Wr as g,
|
|
2632
|
+
Xn as r,
|
|
2633
|
+
Gr as v
|
|
2634
|
+
};
|
|
2635
|
+
//# sourceMappingURL=pegin-registration-client-BdRiR8br.js.map
|