@strkfarm/sdk 1.0.58 → 1.0.60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +0 -0
- package/dist/cli.mjs +0 -0
- package/dist/index.browser.global.js +6168 -5881
- package/dist/index.browser.mjs +399 -2110
- package/dist/index.d.ts +27 -2
- package/dist/index.js +413 -2118
- package/dist/index.mjs +412 -2124
- package/package.json +3 -2
- package/src/interfaces/common.tsx +18 -4
- package/src/node/deployer.ts +1 -1
- package/src/strategies/universal-adapters/adapter-utils.ts +1 -1
- package/src/strategies/universal-adapters/common-adapter.ts +31 -0
- package/src/strategies/{universal-strategy.ts → universal-strategy.tsx} +371 -28
- package/src/utils/cacheClass.ts +28 -28
package/dist/index.browser.mjs
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __export = (target, all) => {
|
|
3
|
-
for (var name in all)
|
|
4
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
-
};
|
|
6
|
-
|
|
7
1
|
// src/modules/pricer.ts
|
|
8
2
|
import axios2 from "axios";
|
|
9
3
|
|
|
@@ -1938,2032 +1932,7 @@ import { processMultiProof, processProof } from "@ericnordelo/strk-merkle-tree/d
|
|
|
1938
1932
|
import { standardLeafHash } from "@ericnordelo/strk-merkle-tree/dist/hashes";
|
|
1939
1933
|
import { MerkleTreeImpl } from "@ericnordelo/strk-merkle-tree/dist/merkletree";
|
|
1940
1934
|
import { num as num2 } from "starknet";
|
|
1941
|
-
|
|
1942
|
-
// ../node_modules/@noble/hashes/esm/_assert.js
|
|
1943
|
-
function number(n) {
|
|
1944
|
-
if (!Number.isSafeInteger(n) || n < 0)
|
|
1945
|
-
throw new Error(`Wrong positive integer: ${n}`);
|
|
1946
|
-
}
|
|
1947
|
-
function bytes(b, ...lengths) {
|
|
1948
|
-
if (!(b instanceof Uint8Array))
|
|
1949
|
-
throw new Error("Expected Uint8Array");
|
|
1950
|
-
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
1951
|
-
throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
|
|
1952
|
-
}
|
|
1953
|
-
function hash(hash5) {
|
|
1954
|
-
if (typeof hash5 !== "function" || typeof hash5.create !== "function")
|
|
1955
|
-
throw new Error("Hash should be wrapped by utils.wrapConstructor");
|
|
1956
|
-
number(hash5.outputLen);
|
|
1957
|
-
number(hash5.blockLen);
|
|
1958
|
-
}
|
|
1959
|
-
function exists(instance, checkFinished = true) {
|
|
1960
|
-
if (instance.destroyed)
|
|
1961
|
-
throw new Error("Hash instance has been destroyed");
|
|
1962
|
-
if (checkFinished && instance.finished)
|
|
1963
|
-
throw new Error("Hash#digest() has already been called");
|
|
1964
|
-
}
|
|
1965
|
-
function output(out, instance) {
|
|
1966
|
-
bytes(out);
|
|
1967
|
-
const min = instance.outputLen;
|
|
1968
|
-
if (out.length < min) {
|
|
1969
|
-
throw new Error(`digestInto() expects output buffer of length at least ${min}`);
|
|
1970
|
-
}
|
|
1971
|
-
}
|
|
1972
|
-
|
|
1973
|
-
// ../node_modules/@noble/hashes/esm/crypto.js
|
|
1974
|
-
var crypto = typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
1975
|
-
|
|
1976
|
-
// ../node_modules/@noble/hashes/esm/utils.js
|
|
1977
|
-
var u8a = (a) => a instanceof Uint8Array;
|
|
1978
|
-
var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
1979
|
-
var rotr = (word, shift) => word << 32 - shift | word >>> shift;
|
|
1980
|
-
var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
|
|
1981
|
-
if (!isLE)
|
|
1982
|
-
throw new Error("Non little-endian hardware is not supported");
|
|
1983
|
-
function utf8ToBytes(str) {
|
|
1984
|
-
if (typeof str !== "string")
|
|
1985
|
-
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
|
1986
|
-
return new Uint8Array(new TextEncoder().encode(str));
|
|
1987
|
-
}
|
|
1988
|
-
function toBytes(data) {
|
|
1989
|
-
if (typeof data === "string")
|
|
1990
|
-
data = utf8ToBytes(data);
|
|
1991
|
-
if (!u8a(data))
|
|
1992
|
-
throw new Error(`expected Uint8Array, got ${typeof data}`);
|
|
1993
|
-
return data;
|
|
1994
|
-
}
|
|
1995
|
-
function concatBytes(...arrays) {
|
|
1996
|
-
const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0));
|
|
1997
|
-
let pad = 0;
|
|
1998
|
-
arrays.forEach((a) => {
|
|
1999
|
-
if (!u8a(a))
|
|
2000
|
-
throw new Error("Uint8Array expected");
|
|
2001
|
-
r.set(a, pad);
|
|
2002
|
-
pad += a.length;
|
|
2003
|
-
});
|
|
2004
|
-
return r;
|
|
2005
|
-
}
|
|
2006
|
-
var Hash = class {
|
|
2007
|
-
// Safe version that clones internal state
|
|
2008
|
-
clone() {
|
|
2009
|
-
return this._cloneInto();
|
|
2010
|
-
}
|
|
2011
|
-
};
|
|
2012
|
-
var toStr = {}.toString;
|
|
2013
|
-
function wrapConstructor(hashCons) {
|
|
2014
|
-
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
2015
|
-
const tmp = hashCons();
|
|
2016
|
-
hashC.outputLen = tmp.outputLen;
|
|
2017
|
-
hashC.blockLen = tmp.blockLen;
|
|
2018
|
-
hashC.create = () => hashCons();
|
|
2019
|
-
return hashC;
|
|
2020
|
-
}
|
|
2021
|
-
function randomBytes(bytesLength = 32) {
|
|
2022
|
-
if (crypto && typeof crypto.getRandomValues === "function") {
|
|
2023
|
-
return crypto.getRandomValues(new Uint8Array(bytesLength));
|
|
2024
|
-
}
|
|
2025
|
-
throw new Error("crypto.getRandomValues must be defined");
|
|
2026
|
-
}
|
|
2027
|
-
|
|
2028
|
-
// ../node_modules/@noble/hashes/esm/_sha2.js
|
|
2029
|
-
function setBigUint64(view, byteOffset, value, isLE2) {
|
|
2030
|
-
if (typeof view.setBigUint64 === "function")
|
|
2031
|
-
return view.setBigUint64(byteOffset, value, isLE2);
|
|
2032
|
-
const _32n = BigInt(32);
|
|
2033
|
-
const _u32_max = BigInt(4294967295);
|
|
2034
|
-
const wh = Number(value >> _32n & _u32_max);
|
|
2035
|
-
const wl = Number(value & _u32_max);
|
|
2036
|
-
const h = isLE2 ? 4 : 0;
|
|
2037
|
-
const l = isLE2 ? 0 : 4;
|
|
2038
|
-
view.setUint32(byteOffset + h, wh, isLE2);
|
|
2039
|
-
view.setUint32(byteOffset + l, wl, isLE2);
|
|
2040
|
-
}
|
|
2041
|
-
var SHA2 = class extends Hash {
|
|
2042
|
-
constructor(blockLen, outputLen, padOffset, isLE2) {
|
|
2043
|
-
super();
|
|
2044
|
-
this.blockLen = blockLen;
|
|
2045
|
-
this.outputLen = outputLen;
|
|
2046
|
-
this.padOffset = padOffset;
|
|
2047
|
-
this.isLE = isLE2;
|
|
2048
|
-
this.finished = false;
|
|
2049
|
-
this.length = 0;
|
|
2050
|
-
this.pos = 0;
|
|
2051
|
-
this.destroyed = false;
|
|
2052
|
-
this.buffer = new Uint8Array(blockLen);
|
|
2053
|
-
this.view = createView(this.buffer);
|
|
2054
|
-
}
|
|
2055
|
-
update(data) {
|
|
2056
|
-
exists(this);
|
|
2057
|
-
const { view, buffer, blockLen } = this;
|
|
2058
|
-
data = toBytes(data);
|
|
2059
|
-
const len = data.length;
|
|
2060
|
-
for (let pos = 0; pos < len; ) {
|
|
2061
|
-
const take = Math.min(blockLen - this.pos, len - pos);
|
|
2062
|
-
if (take === blockLen) {
|
|
2063
|
-
const dataView = createView(data);
|
|
2064
|
-
for (; blockLen <= len - pos; pos += blockLen)
|
|
2065
|
-
this.process(dataView, pos);
|
|
2066
|
-
continue;
|
|
2067
|
-
}
|
|
2068
|
-
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
2069
|
-
this.pos += take;
|
|
2070
|
-
pos += take;
|
|
2071
|
-
if (this.pos === blockLen) {
|
|
2072
|
-
this.process(view, 0);
|
|
2073
|
-
this.pos = 0;
|
|
2074
|
-
}
|
|
2075
|
-
}
|
|
2076
|
-
this.length += data.length;
|
|
2077
|
-
this.roundClean();
|
|
2078
|
-
return this;
|
|
2079
|
-
}
|
|
2080
|
-
digestInto(out) {
|
|
2081
|
-
exists(this);
|
|
2082
|
-
output(out, this);
|
|
2083
|
-
this.finished = true;
|
|
2084
|
-
const { buffer, view, blockLen, isLE: isLE2 } = this;
|
|
2085
|
-
let { pos } = this;
|
|
2086
|
-
buffer[pos++] = 128;
|
|
2087
|
-
this.buffer.subarray(pos).fill(0);
|
|
2088
|
-
if (this.padOffset > blockLen - pos) {
|
|
2089
|
-
this.process(view, 0);
|
|
2090
|
-
pos = 0;
|
|
2091
|
-
}
|
|
2092
|
-
for (let i = pos; i < blockLen; i++)
|
|
2093
|
-
buffer[i] = 0;
|
|
2094
|
-
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE2);
|
|
2095
|
-
this.process(view, 0);
|
|
2096
|
-
const oview = createView(out);
|
|
2097
|
-
const len = this.outputLen;
|
|
2098
|
-
if (len % 4)
|
|
2099
|
-
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
2100
|
-
const outLen = len / 4;
|
|
2101
|
-
const state = this.get();
|
|
2102
|
-
if (outLen > state.length)
|
|
2103
|
-
throw new Error("_sha2: outputLen bigger than state");
|
|
2104
|
-
for (let i = 0; i < outLen; i++)
|
|
2105
|
-
oview.setUint32(4 * i, state[i], isLE2);
|
|
2106
|
-
}
|
|
2107
|
-
digest() {
|
|
2108
|
-
const { buffer, outputLen } = this;
|
|
2109
|
-
this.digestInto(buffer);
|
|
2110
|
-
const res = buffer.slice(0, outputLen);
|
|
2111
|
-
this.destroy();
|
|
2112
|
-
return res;
|
|
2113
|
-
}
|
|
2114
|
-
_cloneInto(to) {
|
|
2115
|
-
to || (to = new this.constructor());
|
|
2116
|
-
to.set(...this.get());
|
|
2117
|
-
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
2118
|
-
to.length = length;
|
|
2119
|
-
to.pos = pos;
|
|
2120
|
-
to.finished = finished;
|
|
2121
|
-
to.destroyed = destroyed;
|
|
2122
|
-
if (length % blockLen)
|
|
2123
|
-
to.buffer.set(buffer);
|
|
2124
|
-
return to;
|
|
2125
|
-
}
|
|
2126
|
-
};
|
|
2127
|
-
|
|
2128
|
-
// ../node_modules/@noble/hashes/esm/sha256.js
|
|
2129
|
-
var Chi = (a, b, c) => a & b ^ ~a & c;
|
|
2130
|
-
var Maj = (a, b, c) => a & b ^ a & c ^ b & c;
|
|
2131
|
-
var SHA256_K = /* @__PURE__ */ new Uint32Array([
|
|
2132
|
-
1116352408,
|
|
2133
|
-
1899447441,
|
|
2134
|
-
3049323471,
|
|
2135
|
-
3921009573,
|
|
2136
|
-
961987163,
|
|
2137
|
-
1508970993,
|
|
2138
|
-
2453635748,
|
|
2139
|
-
2870763221,
|
|
2140
|
-
3624381080,
|
|
2141
|
-
310598401,
|
|
2142
|
-
607225278,
|
|
2143
|
-
1426881987,
|
|
2144
|
-
1925078388,
|
|
2145
|
-
2162078206,
|
|
2146
|
-
2614888103,
|
|
2147
|
-
3248222580,
|
|
2148
|
-
3835390401,
|
|
2149
|
-
4022224774,
|
|
2150
|
-
264347078,
|
|
2151
|
-
604807628,
|
|
2152
|
-
770255983,
|
|
2153
|
-
1249150122,
|
|
2154
|
-
1555081692,
|
|
2155
|
-
1996064986,
|
|
2156
|
-
2554220882,
|
|
2157
|
-
2821834349,
|
|
2158
|
-
2952996808,
|
|
2159
|
-
3210313671,
|
|
2160
|
-
3336571891,
|
|
2161
|
-
3584528711,
|
|
2162
|
-
113926993,
|
|
2163
|
-
338241895,
|
|
2164
|
-
666307205,
|
|
2165
|
-
773529912,
|
|
2166
|
-
1294757372,
|
|
2167
|
-
1396182291,
|
|
2168
|
-
1695183700,
|
|
2169
|
-
1986661051,
|
|
2170
|
-
2177026350,
|
|
2171
|
-
2456956037,
|
|
2172
|
-
2730485921,
|
|
2173
|
-
2820302411,
|
|
2174
|
-
3259730800,
|
|
2175
|
-
3345764771,
|
|
2176
|
-
3516065817,
|
|
2177
|
-
3600352804,
|
|
2178
|
-
4094571909,
|
|
2179
|
-
275423344,
|
|
2180
|
-
430227734,
|
|
2181
|
-
506948616,
|
|
2182
|
-
659060556,
|
|
2183
|
-
883997877,
|
|
2184
|
-
958139571,
|
|
2185
|
-
1322822218,
|
|
2186
|
-
1537002063,
|
|
2187
|
-
1747873779,
|
|
2188
|
-
1955562222,
|
|
2189
|
-
2024104815,
|
|
2190
|
-
2227730452,
|
|
2191
|
-
2361852424,
|
|
2192
|
-
2428436474,
|
|
2193
|
-
2756734187,
|
|
2194
|
-
3204031479,
|
|
2195
|
-
3329325298
|
|
2196
|
-
]);
|
|
2197
|
-
var IV = /* @__PURE__ */ new Uint32Array([
|
|
2198
|
-
1779033703,
|
|
2199
|
-
3144134277,
|
|
2200
|
-
1013904242,
|
|
2201
|
-
2773480762,
|
|
2202
|
-
1359893119,
|
|
2203
|
-
2600822924,
|
|
2204
|
-
528734635,
|
|
2205
|
-
1541459225
|
|
2206
|
-
]);
|
|
2207
|
-
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
2208
|
-
var SHA256 = class extends SHA2 {
|
|
2209
|
-
constructor() {
|
|
2210
|
-
super(64, 32, 8, false);
|
|
2211
|
-
this.A = IV[0] | 0;
|
|
2212
|
-
this.B = IV[1] | 0;
|
|
2213
|
-
this.C = IV[2] | 0;
|
|
2214
|
-
this.D = IV[3] | 0;
|
|
2215
|
-
this.E = IV[4] | 0;
|
|
2216
|
-
this.F = IV[5] | 0;
|
|
2217
|
-
this.G = IV[6] | 0;
|
|
2218
|
-
this.H = IV[7] | 0;
|
|
2219
|
-
}
|
|
2220
|
-
get() {
|
|
2221
|
-
const { A, B, C, D, E, F, G, H } = this;
|
|
2222
|
-
return [A, B, C, D, E, F, G, H];
|
|
2223
|
-
}
|
|
2224
|
-
// prettier-ignore
|
|
2225
|
-
set(A, B, C, D, E, F, G, H) {
|
|
2226
|
-
this.A = A | 0;
|
|
2227
|
-
this.B = B | 0;
|
|
2228
|
-
this.C = C | 0;
|
|
2229
|
-
this.D = D | 0;
|
|
2230
|
-
this.E = E | 0;
|
|
2231
|
-
this.F = F | 0;
|
|
2232
|
-
this.G = G | 0;
|
|
2233
|
-
this.H = H | 0;
|
|
2234
|
-
}
|
|
2235
|
-
process(view, offset) {
|
|
2236
|
-
for (let i = 0; i < 16; i++, offset += 4)
|
|
2237
|
-
SHA256_W[i] = view.getUint32(offset, false);
|
|
2238
|
-
for (let i = 16; i < 64; i++) {
|
|
2239
|
-
const W15 = SHA256_W[i - 15];
|
|
2240
|
-
const W2 = SHA256_W[i - 2];
|
|
2241
|
-
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
2242
|
-
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
|
|
2243
|
-
SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
2244
|
-
}
|
|
2245
|
-
let { A, B, C, D, E, F, G, H } = this;
|
|
2246
|
-
for (let i = 0; i < 64; i++) {
|
|
2247
|
-
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
2248
|
-
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
2249
|
-
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
2250
|
-
const T2 = sigma0 + Maj(A, B, C) | 0;
|
|
2251
|
-
H = G;
|
|
2252
|
-
G = F;
|
|
2253
|
-
F = E;
|
|
2254
|
-
E = D + T1 | 0;
|
|
2255
|
-
D = C;
|
|
2256
|
-
C = B;
|
|
2257
|
-
B = A;
|
|
2258
|
-
A = T1 + T2 | 0;
|
|
2259
|
-
}
|
|
2260
|
-
A = A + this.A | 0;
|
|
2261
|
-
B = B + this.B | 0;
|
|
2262
|
-
C = C + this.C | 0;
|
|
2263
|
-
D = D + this.D | 0;
|
|
2264
|
-
E = E + this.E | 0;
|
|
2265
|
-
F = F + this.F | 0;
|
|
2266
|
-
G = G + this.G | 0;
|
|
2267
|
-
H = H + this.H | 0;
|
|
2268
|
-
this.set(A, B, C, D, E, F, G, H);
|
|
2269
|
-
}
|
|
2270
|
-
roundClean() {
|
|
2271
|
-
SHA256_W.fill(0);
|
|
2272
|
-
}
|
|
2273
|
-
destroy() {
|
|
2274
|
-
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
2275
|
-
this.buffer.fill(0);
|
|
2276
|
-
}
|
|
2277
|
-
};
|
|
2278
|
-
var sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
|
|
2279
|
-
|
|
2280
|
-
// ../node_modules/@noble/curves/esm/abstract/utils.js
|
|
2281
|
-
var utils_exports = {};
|
|
2282
|
-
__export(utils_exports, {
|
|
2283
|
-
bitGet: () => bitGet,
|
|
2284
|
-
bitLen: () => bitLen,
|
|
2285
|
-
bitMask: () => bitMask,
|
|
2286
|
-
bitSet: () => bitSet,
|
|
2287
|
-
bytesToHex: () => bytesToHex,
|
|
2288
|
-
bytesToNumberBE: () => bytesToNumberBE,
|
|
2289
|
-
bytesToNumberLE: () => bytesToNumberLE,
|
|
2290
|
-
concatBytes: () => concatBytes2,
|
|
2291
|
-
createHmacDrbg: () => createHmacDrbg,
|
|
2292
|
-
ensureBytes: () => ensureBytes,
|
|
2293
|
-
equalBytes: () => equalBytes,
|
|
2294
|
-
hexToBytes: () => hexToBytes,
|
|
2295
|
-
hexToNumber: () => hexToNumber,
|
|
2296
|
-
numberToBytesBE: () => numberToBytesBE,
|
|
2297
|
-
numberToBytesLE: () => numberToBytesLE,
|
|
2298
|
-
numberToHexUnpadded: () => numberToHexUnpadded,
|
|
2299
|
-
numberToVarBytesBE: () => numberToVarBytesBE,
|
|
2300
|
-
utf8ToBytes: () => utf8ToBytes2,
|
|
2301
|
-
validateObject: () => validateObject
|
|
2302
|
-
});
|
|
2303
|
-
var _0n = BigInt(0);
|
|
2304
|
-
var _1n = BigInt(1);
|
|
2305
|
-
var _2n = BigInt(2);
|
|
2306
|
-
var u8a2 = (a) => a instanceof Uint8Array;
|
|
2307
|
-
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
2308
|
-
function bytesToHex(bytes2) {
|
|
2309
|
-
if (!u8a2(bytes2))
|
|
2310
|
-
throw new Error("Uint8Array expected");
|
|
2311
|
-
let hex = "";
|
|
2312
|
-
for (let i = 0; i < bytes2.length; i++) {
|
|
2313
|
-
hex += hexes[bytes2[i]];
|
|
2314
|
-
}
|
|
2315
|
-
return hex;
|
|
2316
|
-
}
|
|
2317
|
-
function numberToHexUnpadded(num10) {
|
|
2318
|
-
const hex = num10.toString(16);
|
|
2319
|
-
return hex.length & 1 ? `0${hex}` : hex;
|
|
2320
|
-
}
|
|
2321
|
-
function hexToNumber(hex) {
|
|
2322
|
-
if (typeof hex !== "string")
|
|
2323
|
-
throw new Error("hex string expected, got " + typeof hex);
|
|
2324
|
-
return BigInt(hex === "" ? "0" : `0x${hex}`);
|
|
2325
|
-
}
|
|
2326
|
-
function hexToBytes(hex) {
|
|
2327
|
-
if (typeof hex !== "string")
|
|
2328
|
-
throw new Error("hex string expected, got " + typeof hex);
|
|
2329
|
-
const len = hex.length;
|
|
2330
|
-
if (len % 2)
|
|
2331
|
-
throw new Error("padded hex string expected, got unpadded hex of length " + len);
|
|
2332
|
-
const array = new Uint8Array(len / 2);
|
|
2333
|
-
for (let i = 0; i < array.length; i++) {
|
|
2334
|
-
const j = i * 2;
|
|
2335
|
-
const hexByte = hex.slice(j, j + 2);
|
|
2336
|
-
const byte = Number.parseInt(hexByte, 16);
|
|
2337
|
-
if (Number.isNaN(byte) || byte < 0)
|
|
2338
|
-
throw new Error("Invalid byte sequence");
|
|
2339
|
-
array[i] = byte;
|
|
2340
|
-
}
|
|
2341
|
-
return array;
|
|
2342
|
-
}
|
|
2343
|
-
function bytesToNumberBE(bytes2) {
|
|
2344
|
-
return hexToNumber(bytesToHex(bytes2));
|
|
2345
|
-
}
|
|
2346
|
-
function bytesToNumberLE(bytes2) {
|
|
2347
|
-
if (!u8a2(bytes2))
|
|
2348
|
-
throw new Error("Uint8Array expected");
|
|
2349
|
-
return hexToNumber(bytesToHex(Uint8Array.from(bytes2).reverse()));
|
|
2350
|
-
}
|
|
2351
|
-
function numberToBytesBE(n, len) {
|
|
2352
|
-
return hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
2353
|
-
}
|
|
2354
|
-
function numberToBytesLE(n, len) {
|
|
2355
|
-
return numberToBytesBE(n, len).reverse();
|
|
2356
|
-
}
|
|
2357
|
-
function numberToVarBytesBE(n) {
|
|
2358
|
-
return hexToBytes(numberToHexUnpadded(n));
|
|
2359
|
-
}
|
|
2360
|
-
function ensureBytes(title, hex, expectedLength) {
|
|
2361
|
-
let res;
|
|
2362
|
-
if (typeof hex === "string") {
|
|
2363
|
-
try {
|
|
2364
|
-
res = hexToBytes(hex);
|
|
2365
|
-
} catch (e) {
|
|
2366
|
-
throw new Error(`${title} must be valid hex string, got "${hex}". Cause: ${e}`);
|
|
2367
|
-
}
|
|
2368
|
-
} else if (u8a2(hex)) {
|
|
2369
|
-
res = Uint8Array.from(hex);
|
|
2370
|
-
} else {
|
|
2371
|
-
throw new Error(`${title} must be hex string or Uint8Array`);
|
|
2372
|
-
}
|
|
2373
|
-
const len = res.length;
|
|
2374
|
-
if (typeof expectedLength === "number" && len !== expectedLength)
|
|
2375
|
-
throw new Error(`${title} expected ${expectedLength} bytes, got ${len}`);
|
|
2376
|
-
return res;
|
|
2377
|
-
}
|
|
2378
|
-
function concatBytes2(...arrays) {
|
|
2379
|
-
const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0));
|
|
2380
|
-
let pad = 0;
|
|
2381
|
-
arrays.forEach((a) => {
|
|
2382
|
-
if (!u8a2(a))
|
|
2383
|
-
throw new Error("Uint8Array expected");
|
|
2384
|
-
r.set(a, pad);
|
|
2385
|
-
pad += a.length;
|
|
2386
|
-
});
|
|
2387
|
-
return r;
|
|
2388
|
-
}
|
|
2389
|
-
function equalBytes(b1, b2) {
|
|
2390
|
-
if (b1.length !== b2.length)
|
|
2391
|
-
return false;
|
|
2392
|
-
for (let i = 0; i < b1.length; i++)
|
|
2393
|
-
if (b1[i] !== b2[i])
|
|
2394
|
-
return false;
|
|
2395
|
-
return true;
|
|
2396
|
-
}
|
|
2397
|
-
function utf8ToBytes2(str) {
|
|
2398
|
-
if (typeof str !== "string")
|
|
2399
|
-
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
|
2400
|
-
return new Uint8Array(new TextEncoder().encode(str));
|
|
2401
|
-
}
|
|
2402
|
-
function bitLen(n) {
|
|
2403
|
-
let len;
|
|
2404
|
-
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
2405
|
-
;
|
|
2406
|
-
return len;
|
|
2407
|
-
}
|
|
2408
|
-
function bitGet(n, pos) {
|
|
2409
|
-
return n >> BigInt(pos) & _1n;
|
|
2410
|
-
}
|
|
2411
|
-
var bitSet = (n, pos, value) => {
|
|
2412
|
-
return n | (value ? _1n : _0n) << BigInt(pos);
|
|
2413
|
-
};
|
|
2414
|
-
var bitMask = (n) => (_2n << BigInt(n - 1)) - _1n;
|
|
2415
|
-
var u8n = (data) => new Uint8Array(data);
|
|
2416
|
-
var u8fr = (arr) => Uint8Array.from(arr);
|
|
2417
|
-
function createHmacDrbg(hashLen, qByteLen, hmacFn) {
|
|
2418
|
-
if (typeof hashLen !== "number" || hashLen < 2)
|
|
2419
|
-
throw new Error("hashLen must be a number");
|
|
2420
|
-
if (typeof qByteLen !== "number" || qByteLen < 2)
|
|
2421
|
-
throw new Error("qByteLen must be a number");
|
|
2422
|
-
if (typeof hmacFn !== "function")
|
|
2423
|
-
throw new Error("hmacFn must be a function");
|
|
2424
|
-
let v = u8n(hashLen);
|
|
2425
|
-
let k = u8n(hashLen);
|
|
2426
|
-
let i = 0;
|
|
2427
|
-
const reset = () => {
|
|
2428
|
-
v.fill(1);
|
|
2429
|
-
k.fill(0);
|
|
2430
|
-
i = 0;
|
|
2431
|
-
};
|
|
2432
|
-
const h = (...b) => hmacFn(k, v, ...b);
|
|
2433
|
-
const reseed = (seed = u8n()) => {
|
|
2434
|
-
k = h(u8fr([0]), seed);
|
|
2435
|
-
v = h();
|
|
2436
|
-
if (seed.length === 0)
|
|
2437
|
-
return;
|
|
2438
|
-
k = h(u8fr([1]), seed);
|
|
2439
|
-
v = h();
|
|
2440
|
-
};
|
|
2441
|
-
const gen = () => {
|
|
2442
|
-
if (i++ >= 1e3)
|
|
2443
|
-
throw new Error("drbg: tried 1000 values");
|
|
2444
|
-
let len = 0;
|
|
2445
|
-
const out = [];
|
|
2446
|
-
while (len < qByteLen) {
|
|
2447
|
-
v = h();
|
|
2448
|
-
const sl = v.slice();
|
|
2449
|
-
out.push(sl);
|
|
2450
|
-
len += v.length;
|
|
2451
|
-
}
|
|
2452
|
-
return concatBytes2(...out);
|
|
2453
|
-
};
|
|
2454
|
-
const genUntil = (seed, pred) => {
|
|
2455
|
-
reset();
|
|
2456
|
-
reseed(seed);
|
|
2457
|
-
let res = void 0;
|
|
2458
|
-
while (!(res = pred(gen())))
|
|
2459
|
-
reseed();
|
|
2460
|
-
reset();
|
|
2461
|
-
return res;
|
|
2462
|
-
};
|
|
2463
|
-
return genUntil;
|
|
2464
|
-
}
|
|
2465
|
-
var validatorFns = {
|
|
2466
|
-
bigint: (val) => typeof val === "bigint",
|
|
2467
|
-
function: (val) => typeof val === "function",
|
|
2468
|
-
boolean: (val) => typeof val === "boolean",
|
|
2469
|
-
string: (val) => typeof val === "string",
|
|
2470
|
-
stringOrUint8Array: (val) => typeof val === "string" || val instanceof Uint8Array,
|
|
2471
|
-
isSafeInteger: (val) => Number.isSafeInteger(val),
|
|
2472
|
-
array: (val) => Array.isArray(val),
|
|
2473
|
-
field: (val, object) => object.Fp.isValid(val),
|
|
2474
|
-
hash: (val) => typeof val === "function" && Number.isSafeInteger(val.outputLen)
|
|
2475
|
-
};
|
|
2476
|
-
function validateObject(object, validators, optValidators = {}) {
|
|
2477
|
-
const checkField = (fieldName, type, isOptional) => {
|
|
2478
|
-
const checkVal = validatorFns[type];
|
|
2479
|
-
if (typeof checkVal !== "function")
|
|
2480
|
-
throw new Error(`Invalid validator "${type}", expected function`);
|
|
2481
|
-
const val = object[fieldName];
|
|
2482
|
-
if (isOptional && val === void 0)
|
|
2483
|
-
return;
|
|
2484
|
-
if (!checkVal(val, object)) {
|
|
2485
|
-
throw new Error(`Invalid param ${String(fieldName)}=${val} (${typeof val}), expected ${type}`);
|
|
2486
|
-
}
|
|
2487
|
-
};
|
|
2488
|
-
for (const [fieldName, type] of Object.entries(validators))
|
|
2489
|
-
checkField(fieldName, type, false);
|
|
2490
|
-
for (const [fieldName, type] of Object.entries(optValidators))
|
|
2491
|
-
checkField(fieldName, type, true);
|
|
2492
|
-
return object;
|
|
2493
|
-
}
|
|
2494
|
-
|
|
2495
|
-
// ../node_modules/@noble/curves/esm/abstract/modular.js
|
|
2496
|
-
var _0n2 = BigInt(0);
|
|
2497
|
-
var _1n2 = BigInt(1);
|
|
2498
|
-
var _2n2 = BigInt(2);
|
|
2499
|
-
var _3n = BigInt(3);
|
|
2500
|
-
var _4n = BigInt(4);
|
|
2501
|
-
var _5n = BigInt(5);
|
|
2502
|
-
var _8n = BigInt(8);
|
|
2503
|
-
var _9n = BigInt(9);
|
|
2504
|
-
var _16n = BigInt(16);
|
|
2505
|
-
function mod(a, b) {
|
|
2506
|
-
const result = a % b;
|
|
2507
|
-
return result >= _0n2 ? result : b + result;
|
|
2508
|
-
}
|
|
2509
|
-
function pow(num10, power, modulo) {
|
|
2510
|
-
if (modulo <= _0n2 || power < _0n2)
|
|
2511
|
-
throw new Error("Expected power/modulo > 0");
|
|
2512
|
-
if (modulo === _1n2)
|
|
2513
|
-
return _0n2;
|
|
2514
|
-
let res = _1n2;
|
|
2515
|
-
while (power > _0n2) {
|
|
2516
|
-
if (power & _1n2)
|
|
2517
|
-
res = res * num10 % modulo;
|
|
2518
|
-
num10 = num10 * num10 % modulo;
|
|
2519
|
-
power >>= _1n2;
|
|
2520
|
-
}
|
|
2521
|
-
return res;
|
|
2522
|
-
}
|
|
2523
|
-
function invert(number2, modulo) {
|
|
2524
|
-
if (number2 === _0n2 || modulo <= _0n2) {
|
|
2525
|
-
throw new Error(`invert: expected positive integers, got n=${number2} mod=${modulo}`);
|
|
2526
|
-
}
|
|
2527
|
-
let a = mod(number2, modulo);
|
|
2528
|
-
let b = modulo;
|
|
2529
|
-
let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
|
|
2530
|
-
while (a !== _0n2) {
|
|
2531
|
-
const q = b / a;
|
|
2532
|
-
const r = b % a;
|
|
2533
|
-
const m = x - u * q;
|
|
2534
|
-
const n = y - v * q;
|
|
2535
|
-
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
2536
|
-
}
|
|
2537
|
-
const gcd = b;
|
|
2538
|
-
if (gcd !== _1n2)
|
|
2539
|
-
throw new Error("invert: does not exist");
|
|
2540
|
-
return mod(x, modulo);
|
|
2541
|
-
}
|
|
2542
|
-
function tonelliShanks(P) {
|
|
2543
|
-
const legendreC = (P - _1n2) / _2n2;
|
|
2544
|
-
let Q, S, Z;
|
|
2545
|
-
for (Q = P - _1n2, S = 0; Q % _2n2 === _0n2; Q /= _2n2, S++)
|
|
2546
|
-
;
|
|
2547
|
-
for (Z = _2n2; Z < P && pow(Z, legendreC, P) !== P - _1n2; Z++)
|
|
2548
|
-
;
|
|
2549
|
-
if (S === 1) {
|
|
2550
|
-
const p1div4 = (P + _1n2) / _4n;
|
|
2551
|
-
return function tonelliFast(Fp, n) {
|
|
2552
|
-
const root = Fp.pow(n, p1div4);
|
|
2553
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
|
2554
|
-
throw new Error("Cannot find square root");
|
|
2555
|
-
return root;
|
|
2556
|
-
};
|
|
2557
|
-
}
|
|
2558
|
-
const Q1div2 = (Q + _1n2) / _2n2;
|
|
2559
|
-
return function tonelliSlow(Fp, n) {
|
|
2560
|
-
if (Fp.pow(n, legendreC) === Fp.neg(Fp.ONE))
|
|
2561
|
-
throw new Error("Cannot find square root");
|
|
2562
|
-
let r = S;
|
|
2563
|
-
let g = Fp.pow(Fp.mul(Fp.ONE, Z), Q);
|
|
2564
|
-
let x = Fp.pow(n, Q1div2);
|
|
2565
|
-
let b = Fp.pow(n, Q);
|
|
2566
|
-
while (!Fp.eql(b, Fp.ONE)) {
|
|
2567
|
-
if (Fp.eql(b, Fp.ZERO))
|
|
2568
|
-
return Fp.ZERO;
|
|
2569
|
-
let m = 1;
|
|
2570
|
-
for (let t2 = Fp.sqr(b); m < r; m++) {
|
|
2571
|
-
if (Fp.eql(t2, Fp.ONE))
|
|
2572
|
-
break;
|
|
2573
|
-
t2 = Fp.sqr(t2);
|
|
2574
|
-
}
|
|
2575
|
-
const ge = Fp.pow(g, _1n2 << BigInt(r - m - 1));
|
|
2576
|
-
g = Fp.sqr(ge);
|
|
2577
|
-
x = Fp.mul(x, ge);
|
|
2578
|
-
b = Fp.mul(b, g);
|
|
2579
|
-
r = m;
|
|
2580
|
-
}
|
|
2581
|
-
return x;
|
|
2582
|
-
};
|
|
2583
|
-
}
|
|
2584
|
-
function FpSqrt(P) {
|
|
2585
|
-
if (P % _4n === _3n) {
|
|
2586
|
-
const p1div4 = (P + _1n2) / _4n;
|
|
2587
|
-
return function sqrt3mod4(Fp, n) {
|
|
2588
|
-
const root = Fp.pow(n, p1div4);
|
|
2589
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
|
2590
|
-
throw new Error("Cannot find square root");
|
|
2591
|
-
return root;
|
|
2592
|
-
};
|
|
2593
|
-
}
|
|
2594
|
-
if (P % _8n === _5n) {
|
|
2595
|
-
const c1 = (P - _5n) / _8n;
|
|
2596
|
-
return function sqrt5mod8(Fp, n) {
|
|
2597
|
-
const n2 = Fp.mul(n, _2n2);
|
|
2598
|
-
const v = Fp.pow(n2, c1);
|
|
2599
|
-
const nv = Fp.mul(n, v);
|
|
2600
|
-
const i = Fp.mul(Fp.mul(nv, _2n2), v);
|
|
2601
|
-
const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
|
|
2602
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
|
2603
|
-
throw new Error("Cannot find square root");
|
|
2604
|
-
return root;
|
|
2605
|
-
};
|
|
2606
|
-
}
|
|
2607
|
-
if (P % _16n === _9n) {
|
|
2608
|
-
}
|
|
2609
|
-
return tonelliShanks(P);
|
|
2610
|
-
}
|
|
2611
|
-
var FIELD_FIELDS = [
|
|
2612
|
-
"create",
|
|
2613
|
-
"isValid",
|
|
2614
|
-
"is0",
|
|
2615
|
-
"neg",
|
|
2616
|
-
"inv",
|
|
2617
|
-
"sqrt",
|
|
2618
|
-
"sqr",
|
|
2619
|
-
"eql",
|
|
2620
|
-
"add",
|
|
2621
|
-
"sub",
|
|
2622
|
-
"mul",
|
|
2623
|
-
"pow",
|
|
2624
|
-
"div",
|
|
2625
|
-
"addN",
|
|
2626
|
-
"subN",
|
|
2627
|
-
"mulN",
|
|
2628
|
-
"sqrN"
|
|
2629
|
-
];
|
|
2630
|
-
function validateField(field) {
|
|
2631
|
-
const initial = {
|
|
2632
|
-
ORDER: "bigint",
|
|
2633
|
-
MASK: "bigint",
|
|
2634
|
-
BYTES: "isSafeInteger",
|
|
2635
|
-
BITS: "isSafeInteger"
|
|
2636
|
-
};
|
|
2637
|
-
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
2638
|
-
map[val] = "function";
|
|
2639
|
-
return map;
|
|
2640
|
-
}, initial);
|
|
2641
|
-
return validateObject(field, opts);
|
|
2642
|
-
}
|
|
2643
|
-
function FpPow(f, num10, power) {
|
|
2644
|
-
if (power < _0n2)
|
|
2645
|
-
throw new Error("Expected power > 0");
|
|
2646
|
-
if (power === _0n2)
|
|
2647
|
-
return f.ONE;
|
|
2648
|
-
if (power === _1n2)
|
|
2649
|
-
return num10;
|
|
2650
|
-
let p = f.ONE;
|
|
2651
|
-
let d = num10;
|
|
2652
|
-
while (power > _0n2) {
|
|
2653
|
-
if (power & _1n2)
|
|
2654
|
-
p = f.mul(p, d);
|
|
2655
|
-
d = f.sqr(d);
|
|
2656
|
-
power >>= _1n2;
|
|
2657
|
-
}
|
|
2658
|
-
return p;
|
|
2659
|
-
}
|
|
2660
|
-
function FpInvertBatch(f, nums) {
|
|
2661
|
-
const tmp = new Array(nums.length);
|
|
2662
|
-
const lastMultiplied = nums.reduce((acc, num10, i) => {
|
|
2663
|
-
if (f.is0(num10))
|
|
2664
|
-
return acc;
|
|
2665
|
-
tmp[i] = acc;
|
|
2666
|
-
return f.mul(acc, num10);
|
|
2667
|
-
}, f.ONE);
|
|
2668
|
-
const inverted = f.inv(lastMultiplied);
|
|
2669
|
-
nums.reduceRight((acc, num10, i) => {
|
|
2670
|
-
if (f.is0(num10))
|
|
2671
|
-
return acc;
|
|
2672
|
-
tmp[i] = f.mul(acc, tmp[i]);
|
|
2673
|
-
return f.mul(acc, num10);
|
|
2674
|
-
}, inverted);
|
|
2675
|
-
return tmp;
|
|
2676
|
-
}
|
|
2677
|
-
function nLength(n, nBitLength2) {
|
|
2678
|
-
const _nBitLength = nBitLength2 !== void 0 ? nBitLength2 : n.toString(2).length;
|
|
2679
|
-
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
2680
|
-
return { nBitLength: _nBitLength, nByteLength };
|
|
2681
|
-
}
|
|
2682
|
-
function Field(ORDER, bitLen2, isLE2 = false, redef = {}) {
|
|
2683
|
-
if (ORDER <= _0n2)
|
|
2684
|
-
throw new Error(`Expected Field ORDER > 0, got ${ORDER}`);
|
|
2685
|
-
const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, bitLen2);
|
|
2686
|
-
if (BYTES > 2048)
|
|
2687
|
-
throw new Error("Field lengths over 2048 bytes are not supported");
|
|
2688
|
-
const sqrtP = FpSqrt(ORDER);
|
|
2689
|
-
const f = Object.freeze({
|
|
2690
|
-
ORDER,
|
|
2691
|
-
BITS,
|
|
2692
|
-
BYTES,
|
|
2693
|
-
MASK: bitMask(BITS),
|
|
2694
|
-
ZERO: _0n2,
|
|
2695
|
-
ONE: _1n2,
|
|
2696
|
-
create: (num10) => mod(num10, ORDER),
|
|
2697
|
-
isValid: (num10) => {
|
|
2698
|
-
if (typeof num10 !== "bigint")
|
|
2699
|
-
throw new Error(`Invalid field element: expected bigint, got ${typeof num10}`);
|
|
2700
|
-
return _0n2 <= num10 && num10 < ORDER;
|
|
2701
|
-
},
|
|
2702
|
-
is0: (num10) => num10 === _0n2,
|
|
2703
|
-
isOdd: (num10) => (num10 & _1n2) === _1n2,
|
|
2704
|
-
neg: (num10) => mod(-num10, ORDER),
|
|
2705
|
-
eql: (lhs, rhs) => lhs === rhs,
|
|
2706
|
-
sqr: (num10) => mod(num10 * num10, ORDER),
|
|
2707
|
-
add: (lhs, rhs) => mod(lhs + rhs, ORDER),
|
|
2708
|
-
sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
|
|
2709
|
-
mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
|
|
2710
|
-
pow: (num10, power) => FpPow(f, num10, power),
|
|
2711
|
-
div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
|
|
2712
|
-
// Same as above, but doesn't normalize
|
|
2713
|
-
sqrN: (num10) => num10 * num10,
|
|
2714
|
-
addN: (lhs, rhs) => lhs + rhs,
|
|
2715
|
-
subN: (lhs, rhs) => lhs - rhs,
|
|
2716
|
-
mulN: (lhs, rhs) => lhs * rhs,
|
|
2717
|
-
inv: (num10) => invert(num10, ORDER),
|
|
2718
|
-
sqrt: redef.sqrt || ((n) => sqrtP(f, n)),
|
|
2719
|
-
invertBatch: (lst) => FpInvertBatch(f, lst),
|
|
2720
|
-
// TODO: do we really need constant cmov?
|
|
2721
|
-
// We don't have const-time bigints anyway, so probably will be not very useful
|
|
2722
|
-
cmov: (a, b, c) => c ? b : a,
|
|
2723
|
-
toBytes: (num10) => isLE2 ? numberToBytesLE(num10, BYTES) : numberToBytesBE(num10, BYTES),
|
|
2724
|
-
fromBytes: (bytes2) => {
|
|
2725
|
-
if (bytes2.length !== BYTES)
|
|
2726
|
-
throw new Error(`Fp.fromBytes: expected ${BYTES}, got ${bytes2.length}`);
|
|
2727
|
-
return isLE2 ? bytesToNumberLE(bytes2) : bytesToNumberBE(bytes2);
|
|
2728
|
-
}
|
|
2729
|
-
});
|
|
2730
|
-
return Object.freeze(f);
|
|
2731
|
-
}
|
|
2732
|
-
function getFieldBytesLength(fieldOrder) {
|
|
2733
|
-
if (typeof fieldOrder !== "bigint")
|
|
2734
|
-
throw new Error("field order must be bigint");
|
|
2735
|
-
const bitLength = fieldOrder.toString(2).length;
|
|
2736
|
-
return Math.ceil(bitLength / 8);
|
|
2737
|
-
}
|
|
2738
|
-
function getMinHashLength(fieldOrder) {
|
|
2739
|
-
const length = getFieldBytesLength(fieldOrder);
|
|
2740
|
-
return length + Math.ceil(length / 2);
|
|
2741
|
-
}
|
|
2742
|
-
function mapHashToField(key, fieldOrder, isLE2 = false) {
|
|
2743
|
-
const len = key.length;
|
|
2744
|
-
const fieldLen = getFieldBytesLength(fieldOrder);
|
|
2745
|
-
const minLen = getMinHashLength(fieldOrder);
|
|
2746
|
-
if (len < 16 || len < minLen || len > 1024)
|
|
2747
|
-
throw new Error(`expected ${minLen}-1024 bytes of input, got ${len}`);
|
|
2748
|
-
const num10 = isLE2 ? bytesToNumberBE(key) : bytesToNumberLE(key);
|
|
2749
|
-
const reduced = mod(num10, fieldOrder - _1n2) + _1n2;
|
|
2750
|
-
return isLE2 ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
2751
|
-
}
|
|
2752
|
-
|
|
2753
|
-
// ../node_modules/@noble/curves/esm/abstract/poseidon.js
|
|
2754
|
-
function validateOpts(opts) {
|
|
2755
|
-
const { Fp, mds, reversePartialPowIdx: rev, roundConstants: rc } = opts;
|
|
2756
|
-
const { roundsFull, roundsPartial, sboxPower, t } = opts;
|
|
2757
|
-
validateField(Fp);
|
|
2758
|
-
for (const i of ["t", "roundsFull", "roundsPartial"]) {
|
|
2759
|
-
if (typeof opts[i] !== "number" || !Number.isSafeInteger(opts[i]))
|
|
2760
|
-
throw new Error(`Poseidon: invalid param ${i}=${opts[i]} (${typeof opts[i]})`);
|
|
2761
|
-
}
|
|
2762
|
-
if (!Array.isArray(mds) || mds.length !== t)
|
|
2763
|
-
throw new Error("Poseidon: wrong MDS matrix");
|
|
2764
|
-
const _mds = mds.map((mdsRow) => {
|
|
2765
|
-
if (!Array.isArray(mdsRow) || mdsRow.length !== t)
|
|
2766
|
-
throw new Error(`Poseidon MDS matrix row: ${mdsRow}`);
|
|
2767
|
-
return mdsRow.map((i) => {
|
|
2768
|
-
if (typeof i !== "bigint")
|
|
2769
|
-
throw new Error(`Poseidon MDS matrix value=${i}`);
|
|
2770
|
-
return Fp.create(i);
|
|
2771
|
-
});
|
|
2772
|
-
});
|
|
2773
|
-
if (rev !== void 0 && typeof rev !== "boolean")
|
|
2774
|
-
throw new Error(`Poseidon: invalid param reversePartialPowIdx=${rev}`);
|
|
2775
|
-
if (roundsFull % 2 !== 0)
|
|
2776
|
-
throw new Error(`Poseidon roundsFull is not even: ${roundsFull}`);
|
|
2777
|
-
const rounds = roundsFull + roundsPartial;
|
|
2778
|
-
if (!Array.isArray(rc) || rc.length !== rounds)
|
|
2779
|
-
throw new Error("Poseidon: wrong round constants");
|
|
2780
|
-
const roundConstants = rc.map((rc2) => {
|
|
2781
|
-
if (!Array.isArray(rc2) || rc2.length !== t)
|
|
2782
|
-
throw new Error(`Poseidon wrong round constants: ${rc2}`);
|
|
2783
|
-
return rc2.map((i) => {
|
|
2784
|
-
if (typeof i !== "bigint" || !Fp.isValid(i))
|
|
2785
|
-
throw new Error(`Poseidon wrong round constant=${i}`);
|
|
2786
|
-
return Fp.create(i);
|
|
2787
|
-
});
|
|
2788
|
-
});
|
|
2789
|
-
if (!sboxPower || ![3, 5, 7].includes(sboxPower))
|
|
2790
|
-
throw new Error(`Poseidon wrong sboxPower=${sboxPower}`);
|
|
2791
|
-
const _sboxPower = BigInt(sboxPower);
|
|
2792
|
-
let sboxFn = (n) => FpPow(Fp, n, _sboxPower);
|
|
2793
|
-
if (sboxPower === 3)
|
|
2794
|
-
sboxFn = (n) => Fp.mul(Fp.sqrN(n), n);
|
|
2795
|
-
else if (sboxPower === 5)
|
|
2796
|
-
sboxFn = (n) => Fp.mul(Fp.sqrN(Fp.sqrN(n)), n);
|
|
2797
|
-
return Object.freeze({ ...opts, rounds, sboxFn, roundConstants, mds: _mds });
|
|
2798
|
-
}
|
|
2799
|
-
function poseidon(opts) {
|
|
2800
|
-
const _opts = validateOpts(opts);
|
|
2801
|
-
const { Fp, mds, roundConstants, rounds, roundsPartial, sboxFn, t } = _opts;
|
|
2802
|
-
const halfRoundsFull = _opts.roundsFull / 2;
|
|
2803
|
-
const partialIdx = _opts.reversePartialPowIdx ? t - 1 : 0;
|
|
2804
|
-
const poseidonRound = (values, isFull, idx) => {
|
|
2805
|
-
values = values.map((i, j) => Fp.add(i, roundConstants[idx][j]));
|
|
2806
|
-
if (isFull)
|
|
2807
|
-
values = values.map((i) => sboxFn(i));
|
|
2808
|
-
else
|
|
2809
|
-
values[partialIdx] = sboxFn(values[partialIdx]);
|
|
2810
|
-
values = mds.map((i) => i.reduce((acc, i2, j) => Fp.add(acc, Fp.mulN(i2, values[j])), Fp.ZERO));
|
|
2811
|
-
return values;
|
|
2812
|
-
};
|
|
2813
|
-
const poseidonHash = function poseidonHash2(values) {
|
|
2814
|
-
if (!Array.isArray(values) || values.length !== t)
|
|
2815
|
-
throw new Error(`Poseidon: wrong values (expected array of bigints with length ${t})`);
|
|
2816
|
-
values = values.map((i) => {
|
|
2817
|
-
if (typeof i !== "bigint")
|
|
2818
|
-
throw new Error(`Poseidon: wrong value=${i} (${typeof i})`);
|
|
2819
|
-
return Fp.create(i);
|
|
2820
|
-
});
|
|
2821
|
-
let round = 0;
|
|
2822
|
-
for (let i = 0; i < halfRoundsFull; i++)
|
|
2823
|
-
values = poseidonRound(values, true, round++);
|
|
2824
|
-
for (let i = 0; i < roundsPartial; i++)
|
|
2825
|
-
values = poseidonRound(values, false, round++);
|
|
2826
|
-
for (let i = 0; i < halfRoundsFull; i++)
|
|
2827
|
-
values = poseidonRound(values, true, round++);
|
|
2828
|
-
if (round !== rounds)
|
|
2829
|
-
throw new Error(`Poseidon: wrong number of rounds: last round=${round}, total=${rounds}`);
|
|
2830
|
-
return values;
|
|
2831
|
-
};
|
|
2832
|
-
poseidonHash.roundConstants = roundConstants;
|
|
2833
|
-
return poseidonHash;
|
|
2834
|
-
}
|
|
2835
|
-
|
|
2836
|
-
// ../node_modules/@noble/curves/esm/abstract/curve.js
|
|
2837
|
-
var _0n3 = BigInt(0);
|
|
2838
|
-
var _1n3 = BigInt(1);
|
|
2839
|
-
function wNAF(c, bits) {
|
|
2840
|
-
const constTimeNegate = (condition, item) => {
|
|
2841
|
-
const neg = item.negate();
|
|
2842
|
-
return condition ? neg : item;
|
|
2843
|
-
};
|
|
2844
|
-
const opts = (W) => {
|
|
2845
|
-
const windows = Math.ceil(bits / W) + 1;
|
|
2846
|
-
const windowSize = 2 ** (W - 1);
|
|
2847
|
-
return { windows, windowSize };
|
|
2848
|
-
};
|
|
2849
|
-
return {
|
|
2850
|
-
constTimeNegate,
|
|
2851
|
-
// non-const time multiplication ladder
|
|
2852
|
-
unsafeLadder(elm, n) {
|
|
2853
|
-
let p = c.ZERO;
|
|
2854
|
-
let d = elm;
|
|
2855
|
-
while (n > _0n3) {
|
|
2856
|
-
if (n & _1n3)
|
|
2857
|
-
p = p.add(d);
|
|
2858
|
-
d = d.double();
|
|
2859
|
-
n >>= _1n3;
|
|
2860
|
-
}
|
|
2861
|
-
return p;
|
|
2862
|
-
},
|
|
2863
|
-
/**
|
|
2864
|
-
* Creates a wNAF precomputation window. Used for caching.
|
|
2865
|
-
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
2866
|
-
* Number of precomputed points depends on the curve size:
|
|
2867
|
-
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
2868
|
-
* - 𝑊 is the window size
|
|
2869
|
-
* - 𝑛 is the bitlength of the curve order.
|
|
2870
|
-
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
2871
|
-
* @returns precomputed point tables flattened to a single array
|
|
2872
|
-
*/
|
|
2873
|
-
precomputeWindow(elm, W) {
|
|
2874
|
-
const { windows, windowSize } = opts(W);
|
|
2875
|
-
const points = [];
|
|
2876
|
-
let p = elm;
|
|
2877
|
-
let base = p;
|
|
2878
|
-
for (let window = 0; window < windows; window++) {
|
|
2879
|
-
base = p;
|
|
2880
|
-
points.push(base);
|
|
2881
|
-
for (let i = 1; i < windowSize; i++) {
|
|
2882
|
-
base = base.add(p);
|
|
2883
|
-
points.push(base);
|
|
2884
|
-
}
|
|
2885
|
-
p = base.double();
|
|
2886
|
-
}
|
|
2887
|
-
return points;
|
|
2888
|
-
},
|
|
2889
|
-
/**
|
|
2890
|
-
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
2891
|
-
* @param W window size
|
|
2892
|
-
* @param precomputes precomputed tables
|
|
2893
|
-
* @param n scalar (we don't check here, but should be less than curve order)
|
|
2894
|
-
* @returns real and fake (for const-time) points
|
|
2895
|
-
*/
|
|
2896
|
-
wNAF(W, precomputes, n) {
|
|
2897
|
-
const { windows, windowSize } = opts(W);
|
|
2898
|
-
let p = c.ZERO;
|
|
2899
|
-
let f = c.BASE;
|
|
2900
|
-
const mask = BigInt(2 ** W - 1);
|
|
2901
|
-
const maxNumber = 2 ** W;
|
|
2902
|
-
const shiftBy = BigInt(W);
|
|
2903
|
-
for (let window = 0; window < windows; window++) {
|
|
2904
|
-
const offset = window * windowSize;
|
|
2905
|
-
let wbits = Number(n & mask);
|
|
2906
|
-
n >>= shiftBy;
|
|
2907
|
-
if (wbits > windowSize) {
|
|
2908
|
-
wbits -= maxNumber;
|
|
2909
|
-
n += _1n3;
|
|
2910
|
-
}
|
|
2911
|
-
const offset1 = offset;
|
|
2912
|
-
const offset2 = offset + Math.abs(wbits) - 1;
|
|
2913
|
-
const cond1 = window % 2 !== 0;
|
|
2914
|
-
const cond2 = wbits < 0;
|
|
2915
|
-
if (wbits === 0) {
|
|
2916
|
-
f = f.add(constTimeNegate(cond1, precomputes[offset1]));
|
|
2917
|
-
} else {
|
|
2918
|
-
p = p.add(constTimeNegate(cond2, precomputes[offset2]));
|
|
2919
|
-
}
|
|
2920
|
-
}
|
|
2921
|
-
return { p, f };
|
|
2922
|
-
},
|
|
2923
|
-
wNAFCached(P, precomputesMap, n, transform) {
|
|
2924
|
-
const W = P._WINDOW_SIZE || 1;
|
|
2925
|
-
let comp = precomputesMap.get(P);
|
|
2926
|
-
if (!comp) {
|
|
2927
|
-
comp = this.precomputeWindow(P, W);
|
|
2928
|
-
if (W !== 1) {
|
|
2929
|
-
precomputesMap.set(P, transform(comp));
|
|
2930
|
-
}
|
|
2931
|
-
}
|
|
2932
|
-
return this.wNAF(W, comp, n);
|
|
2933
|
-
}
|
|
2934
|
-
};
|
|
2935
|
-
}
|
|
2936
|
-
function validateBasic(curve2) {
|
|
2937
|
-
validateField(curve2.Fp);
|
|
2938
|
-
validateObject(curve2, {
|
|
2939
|
-
n: "bigint",
|
|
2940
|
-
h: "bigint",
|
|
2941
|
-
Gx: "field",
|
|
2942
|
-
Gy: "field"
|
|
2943
|
-
}, {
|
|
2944
|
-
nBitLength: "isSafeInteger",
|
|
2945
|
-
nByteLength: "isSafeInteger"
|
|
2946
|
-
});
|
|
2947
|
-
return Object.freeze({
|
|
2948
|
-
...nLength(curve2.n, curve2.nBitLength),
|
|
2949
|
-
...curve2,
|
|
2950
|
-
...{ p: curve2.Fp.ORDER }
|
|
2951
|
-
});
|
|
2952
|
-
}
|
|
2953
|
-
|
|
2954
|
-
// ../node_modules/@noble/curves/esm/abstract/weierstrass.js
|
|
2955
|
-
function validatePointOpts(curve2) {
|
|
2956
|
-
const opts = validateBasic(curve2);
|
|
2957
|
-
validateObject(opts, {
|
|
2958
|
-
a: "field",
|
|
2959
|
-
b: "field"
|
|
2960
|
-
}, {
|
|
2961
|
-
allowedPrivateKeyLengths: "array",
|
|
2962
|
-
wrapPrivateKey: "boolean",
|
|
2963
|
-
isTorsionFree: "function",
|
|
2964
|
-
clearCofactor: "function",
|
|
2965
|
-
allowInfinityPoint: "boolean",
|
|
2966
|
-
fromBytes: "function",
|
|
2967
|
-
toBytes: "function"
|
|
2968
|
-
});
|
|
2969
|
-
const { endo, Fp, a } = opts;
|
|
2970
|
-
if (endo) {
|
|
2971
|
-
if (!Fp.eql(a, Fp.ZERO)) {
|
|
2972
|
-
throw new Error("Endomorphism can only be defined for Koblitz curves that have a=0");
|
|
2973
|
-
}
|
|
2974
|
-
if (typeof endo !== "object" || typeof endo.beta !== "bigint" || typeof endo.splitScalar !== "function") {
|
|
2975
|
-
throw new Error("Expected endomorphism with beta: bigint and splitScalar: function");
|
|
2976
|
-
}
|
|
2977
|
-
}
|
|
2978
|
-
return Object.freeze({ ...opts });
|
|
2979
|
-
}
|
|
2980
|
-
var { bytesToNumberBE: b2n, hexToBytes: h2b } = utils_exports;
|
|
2981
|
-
var DER = {
|
|
2982
|
-
// asn.1 DER encoding utils
|
|
2983
|
-
Err: class DERErr extends Error {
|
|
2984
|
-
constructor(m = "") {
|
|
2985
|
-
super(m);
|
|
2986
|
-
}
|
|
2987
|
-
},
|
|
2988
|
-
_parseInt(data) {
|
|
2989
|
-
const { Err: E } = DER;
|
|
2990
|
-
if (data.length < 2 || data[0] !== 2)
|
|
2991
|
-
throw new E("Invalid signature integer tag");
|
|
2992
|
-
const len = data[1];
|
|
2993
|
-
const res = data.subarray(2, len + 2);
|
|
2994
|
-
if (!len || res.length !== len)
|
|
2995
|
-
throw new E("Invalid signature integer: wrong length");
|
|
2996
|
-
if (res[0] & 128)
|
|
2997
|
-
throw new E("Invalid signature integer: negative");
|
|
2998
|
-
if (res[0] === 0 && !(res[1] & 128))
|
|
2999
|
-
throw new E("Invalid signature integer: unnecessary leading zero");
|
|
3000
|
-
return { d: b2n(res), l: data.subarray(len + 2) };
|
|
3001
|
-
},
|
|
3002
|
-
toSig(hex) {
|
|
3003
|
-
const { Err: E } = DER;
|
|
3004
|
-
const data = typeof hex === "string" ? h2b(hex) : hex;
|
|
3005
|
-
if (!(data instanceof Uint8Array))
|
|
3006
|
-
throw new Error("ui8a expected");
|
|
3007
|
-
let l = data.length;
|
|
3008
|
-
if (l < 2 || data[0] != 48)
|
|
3009
|
-
throw new E("Invalid signature tag");
|
|
3010
|
-
if (data[1] !== l - 2)
|
|
3011
|
-
throw new E("Invalid signature: incorrect length");
|
|
3012
|
-
const { d: r, l: sBytes } = DER._parseInt(data.subarray(2));
|
|
3013
|
-
const { d: s, l: rBytesLeft } = DER._parseInt(sBytes);
|
|
3014
|
-
if (rBytesLeft.length)
|
|
3015
|
-
throw new E("Invalid signature: left bytes after parsing");
|
|
3016
|
-
return { r, s };
|
|
3017
|
-
},
|
|
3018
|
-
hexFromSig(sig) {
|
|
3019
|
-
const slice = (s2) => Number.parseInt(s2[0], 16) & 8 ? "00" + s2 : s2;
|
|
3020
|
-
const h = (num10) => {
|
|
3021
|
-
const hex = num10.toString(16);
|
|
3022
|
-
return hex.length & 1 ? `0${hex}` : hex;
|
|
3023
|
-
};
|
|
3024
|
-
const s = slice(h(sig.s));
|
|
3025
|
-
const r = slice(h(sig.r));
|
|
3026
|
-
const shl = s.length / 2;
|
|
3027
|
-
const rhl = r.length / 2;
|
|
3028
|
-
const sl = h(shl);
|
|
3029
|
-
const rl = h(rhl);
|
|
3030
|
-
return `30${h(rhl + shl + 4)}02${rl}${r}02${sl}${s}`;
|
|
3031
|
-
}
|
|
3032
|
-
};
|
|
3033
|
-
var _0n4 = BigInt(0);
|
|
3034
|
-
var _1n4 = BigInt(1);
|
|
3035
|
-
var _2n3 = BigInt(2);
|
|
3036
|
-
var _3n2 = BigInt(3);
|
|
3037
|
-
var _4n2 = BigInt(4);
|
|
3038
|
-
function weierstrassPoints(opts) {
|
|
3039
|
-
const CURVE2 = validatePointOpts(opts);
|
|
3040
|
-
const { Fp } = CURVE2;
|
|
3041
|
-
const toBytes2 = CURVE2.toBytes || ((_c, point, _isCompressed) => {
|
|
3042
|
-
const a = point.toAffine();
|
|
3043
|
-
return concatBytes2(Uint8Array.from([4]), Fp.toBytes(a.x), Fp.toBytes(a.y));
|
|
3044
|
-
});
|
|
3045
|
-
const fromBytes = CURVE2.fromBytes || ((bytes2) => {
|
|
3046
|
-
const tail = bytes2.subarray(1);
|
|
3047
|
-
const x = Fp.fromBytes(tail.subarray(0, Fp.BYTES));
|
|
3048
|
-
const y = Fp.fromBytes(tail.subarray(Fp.BYTES, 2 * Fp.BYTES));
|
|
3049
|
-
return { x, y };
|
|
3050
|
-
});
|
|
3051
|
-
function weierstrassEquation(x) {
|
|
3052
|
-
const { a, b } = CURVE2;
|
|
3053
|
-
const x2 = Fp.sqr(x);
|
|
3054
|
-
const x3 = Fp.mul(x2, x);
|
|
3055
|
-
return Fp.add(Fp.add(x3, Fp.mul(x, a)), b);
|
|
3056
|
-
}
|
|
3057
|
-
if (!Fp.eql(Fp.sqr(CURVE2.Gy), weierstrassEquation(CURVE2.Gx)))
|
|
3058
|
-
throw new Error("bad generator point: equation left != right");
|
|
3059
|
-
function isWithinCurveOrder(num10) {
|
|
3060
|
-
return typeof num10 === "bigint" && _0n4 < num10 && num10 < CURVE2.n;
|
|
3061
|
-
}
|
|
3062
|
-
function assertGE(num10) {
|
|
3063
|
-
if (!isWithinCurveOrder(num10))
|
|
3064
|
-
throw new Error("Expected valid bigint: 0 < bigint < curve.n");
|
|
3065
|
-
}
|
|
3066
|
-
function normPrivateKeyToScalar(key) {
|
|
3067
|
-
const { allowedPrivateKeyLengths: lengths, nByteLength, wrapPrivateKey, n } = CURVE2;
|
|
3068
|
-
if (lengths && typeof key !== "bigint") {
|
|
3069
|
-
if (key instanceof Uint8Array)
|
|
3070
|
-
key = bytesToHex(key);
|
|
3071
|
-
if (typeof key !== "string" || !lengths.includes(key.length))
|
|
3072
|
-
throw new Error("Invalid key");
|
|
3073
|
-
key = key.padStart(nByteLength * 2, "0");
|
|
3074
|
-
}
|
|
3075
|
-
let num10;
|
|
3076
|
-
try {
|
|
3077
|
-
num10 = typeof key === "bigint" ? key : bytesToNumberBE(ensureBytes("private key", key, nByteLength));
|
|
3078
|
-
} catch (error) {
|
|
3079
|
-
throw new Error(`private key must be ${nByteLength} bytes, hex or bigint, not ${typeof key}`);
|
|
3080
|
-
}
|
|
3081
|
-
if (wrapPrivateKey)
|
|
3082
|
-
num10 = mod(num10, n);
|
|
3083
|
-
assertGE(num10);
|
|
3084
|
-
return num10;
|
|
3085
|
-
}
|
|
3086
|
-
const pointPrecomputes = /* @__PURE__ */ new Map();
|
|
3087
|
-
function assertPrjPoint(other) {
|
|
3088
|
-
if (!(other instanceof Point))
|
|
3089
|
-
throw new Error("ProjectivePoint expected");
|
|
3090
|
-
}
|
|
3091
|
-
class Point {
|
|
3092
|
-
constructor(px, py, pz) {
|
|
3093
|
-
this.px = px;
|
|
3094
|
-
this.py = py;
|
|
3095
|
-
this.pz = pz;
|
|
3096
|
-
if (px == null || !Fp.isValid(px))
|
|
3097
|
-
throw new Error("x required");
|
|
3098
|
-
if (py == null || !Fp.isValid(py))
|
|
3099
|
-
throw new Error("y required");
|
|
3100
|
-
if (pz == null || !Fp.isValid(pz))
|
|
3101
|
-
throw new Error("z required");
|
|
3102
|
-
}
|
|
3103
|
-
// Does not validate if the point is on-curve.
|
|
3104
|
-
// Use fromHex instead, or call assertValidity() later.
|
|
3105
|
-
static fromAffine(p) {
|
|
3106
|
-
const { x, y } = p || {};
|
|
3107
|
-
if (!p || !Fp.isValid(x) || !Fp.isValid(y))
|
|
3108
|
-
throw new Error("invalid affine point");
|
|
3109
|
-
if (p instanceof Point)
|
|
3110
|
-
throw new Error("projective point not allowed");
|
|
3111
|
-
const is0 = (i) => Fp.eql(i, Fp.ZERO);
|
|
3112
|
-
if (is0(x) && is0(y))
|
|
3113
|
-
return Point.ZERO;
|
|
3114
|
-
return new Point(x, y, Fp.ONE);
|
|
3115
|
-
}
|
|
3116
|
-
get x() {
|
|
3117
|
-
return this.toAffine().x;
|
|
3118
|
-
}
|
|
3119
|
-
get y() {
|
|
3120
|
-
return this.toAffine().y;
|
|
3121
|
-
}
|
|
3122
|
-
/**
|
|
3123
|
-
* Takes a bunch of Projective Points but executes only one
|
|
3124
|
-
* inversion on all of them. Inversion is very slow operation,
|
|
3125
|
-
* so this improves performance massively.
|
|
3126
|
-
* Optimization: converts a list of projective points to a list of identical points with Z=1.
|
|
3127
|
-
*/
|
|
3128
|
-
static normalizeZ(points) {
|
|
3129
|
-
const toInv = Fp.invertBatch(points.map((p) => p.pz));
|
|
3130
|
-
return points.map((p, i) => p.toAffine(toInv[i])).map(Point.fromAffine);
|
|
3131
|
-
}
|
|
3132
|
-
/**
|
|
3133
|
-
* Converts hash string or Uint8Array to Point.
|
|
3134
|
-
* @param hex short/long ECDSA hex
|
|
3135
|
-
*/
|
|
3136
|
-
static fromHex(hex) {
|
|
3137
|
-
const P = Point.fromAffine(fromBytes(ensureBytes("pointHex", hex)));
|
|
3138
|
-
P.assertValidity();
|
|
3139
|
-
return P;
|
|
3140
|
-
}
|
|
3141
|
-
// Multiplies generator point by privateKey.
|
|
3142
|
-
static fromPrivateKey(privateKey) {
|
|
3143
|
-
return Point.BASE.multiply(normPrivateKeyToScalar(privateKey));
|
|
3144
|
-
}
|
|
3145
|
-
// "Private method", don't use it directly
|
|
3146
|
-
_setWindowSize(windowSize) {
|
|
3147
|
-
this._WINDOW_SIZE = windowSize;
|
|
3148
|
-
pointPrecomputes.delete(this);
|
|
3149
|
-
}
|
|
3150
|
-
// A point on curve is valid if it conforms to equation.
|
|
3151
|
-
assertValidity() {
|
|
3152
|
-
if (this.is0()) {
|
|
3153
|
-
if (CURVE2.allowInfinityPoint && !Fp.is0(this.py))
|
|
3154
|
-
return;
|
|
3155
|
-
throw new Error("bad point: ZERO");
|
|
3156
|
-
}
|
|
3157
|
-
const { x, y } = this.toAffine();
|
|
3158
|
-
if (!Fp.isValid(x) || !Fp.isValid(y))
|
|
3159
|
-
throw new Error("bad point: x or y not FE");
|
|
3160
|
-
const left = Fp.sqr(y);
|
|
3161
|
-
const right = weierstrassEquation(x);
|
|
3162
|
-
if (!Fp.eql(left, right))
|
|
3163
|
-
throw new Error("bad point: equation left != right");
|
|
3164
|
-
if (!this.isTorsionFree())
|
|
3165
|
-
throw new Error("bad point: not in prime-order subgroup");
|
|
3166
|
-
}
|
|
3167
|
-
hasEvenY() {
|
|
3168
|
-
const { y } = this.toAffine();
|
|
3169
|
-
if (Fp.isOdd)
|
|
3170
|
-
return !Fp.isOdd(y);
|
|
3171
|
-
throw new Error("Field doesn't support isOdd");
|
|
3172
|
-
}
|
|
3173
|
-
/**
|
|
3174
|
-
* Compare one point to another.
|
|
3175
|
-
*/
|
|
3176
|
-
equals(other) {
|
|
3177
|
-
assertPrjPoint(other);
|
|
3178
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
3179
|
-
const { px: X2, py: Y2, pz: Z2 } = other;
|
|
3180
|
-
const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
|
|
3181
|
-
const U2 = Fp.eql(Fp.mul(Y1, Z2), Fp.mul(Y2, Z1));
|
|
3182
|
-
return U1 && U2;
|
|
3183
|
-
}
|
|
3184
|
-
/**
|
|
3185
|
-
* Flips point to one corresponding to (x, -y) in Affine coordinates.
|
|
3186
|
-
*/
|
|
3187
|
-
negate() {
|
|
3188
|
-
return new Point(this.px, Fp.neg(this.py), this.pz);
|
|
3189
|
-
}
|
|
3190
|
-
// Renes-Costello-Batina exception-free doubling formula.
|
|
3191
|
-
// There is 30% faster Jacobian formula, but it is not complete.
|
|
3192
|
-
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
3193
|
-
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
3194
|
-
double() {
|
|
3195
|
-
const { a, b } = CURVE2;
|
|
3196
|
-
const b3 = Fp.mul(b, _3n2);
|
|
3197
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
3198
|
-
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
3199
|
-
let t0 = Fp.mul(X1, X1);
|
|
3200
|
-
let t1 = Fp.mul(Y1, Y1);
|
|
3201
|
-
let t2 = Fp.mul(Z1, Z1);
|
|
3202
|
-
let t3 = Fp.mul(X1, Y1);
|
|
3203
|
-
t3 = Fp.add(t3, t3);
|
|
3204
|
-
Z3 = Fp.mul(X1, Z1);
|
|
3205
|
-
Z3 = Fp.add(Z3, Z3);
|
|
3206
|
-
X3 = Fp.mul(a, Z3);
|
|
3207
|
-
Y3 = Fp.mul(b3, t2);
|
|
3208
|
-
Y3 = Fp.add(X3, Y3);
|
|
3209
|
-
X3 = Fp.sub(t1, Y3);
|
|
3210
|
-
Y3 = Fp.add(t1, Y3);
|
|
3211
|
-
Y3 = Fp.mul(X3, Y3);
|
|
3212
|
-
X3 = Fp.mul(t3, X3);
|
|
3213
|
-
Z3 = Fp.mul(b3, Z3);
|
|
3214
|
-
t2 = Fp.mul(a, t2);
|
|
3215
|
-
t3 = Fp.sub(t0, t2);
|
|
3216
|
-
t3 = Fp.mul(a, t3);
|
|
3217
|
-
t3 = Fp.add(t3, Z3);
|
|
3218
|
-
Z3 = Fp.add(t0, t0);
|
|
3219
|
-
t0 = Fp.add(Z3, t0);
|
|
3220
|
-
t0 = Fp.add(t0, t2);
|
|
3221
|
-
t0 = Fp.mul(t0, t3);
|
|
3222
|
-
Y3 = Fp.add(Y3, t0);
|
|
3223
|
-
t2 = Fp.mul(Y1, Z1);
|
|
3224
|
-
t2 = Fp.add(t2, t2);
|
|
3225
|
-
t0 = Fp.mul(t2, t3);
|
|
3226
|
-
X3 = Fp.sub(X3, t0);
|
|
3227
|
-
Z3 = Fp.mul(t2, t1);
|
|
3228
|
-
Z3 = Fp.add(Z3, Z3);
|
|
3229
|
-
Z3 = Fp.add(Z3, Z3);
|
|
3230
|
-
return new Point(X3, Y3, Z3);
|
|
3231
|
-
}
|
|
3232
|
-
// Renes-Costello-Batina exception-free addition formula.
|
|
3233
|
-
// There is 30% faster Jacobian formula, but it is not complete.
|
|
3234
|
-
// https://eprint.iacr.org/2015/1060, algorithm 1
|
|
3235
|
-
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
3236
|
-
add(other) {
|
|
3237
|
-
assertPrjPoint(other);
|
|
3238
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
3239
|
-
const { px: X2, py: Y2, pz: Z2 } = other;
|
|
3240
|
-
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
3241
|
-
const a = CURVE2.a;
|
|
3242
|
-
const b3 = Fp.mul(CURVE2.b, _3n2);
|
|
3243
|
-
let t0 = Fp.mul(X1, X2);
|
|
3244
|
-
let t1 = Fp.mul(Y1, Y2);
|
|
3245
|
-
let t2 = Fp.mul(Z1, Z2);
|
|
3246
|
-
let t3 = Fp.add(X1, Y1);
|
|
3247
|
-
let t4 = Fp.add(X2, Y2);
|
|
3248
|
-
t3 = Fp.mul(t3, t4);
|
|
3249
|
-
t4 = Fp.add(t0, t1);
|
|
3250
|
-
t3 = Fp.sub(t3, t4);
|
|
3251
|
-
t4 = Fp.add(X1, Z1);
|
|
3252
|
-
let t5 = Fp.add(X2, Z2);
|
|
3253
|
-
t4 = Fp.mul(t4, t5);
|
|
3254
|
-
t5 = Fp.add(t0, t2);
|
|
3255
|
-
t4 = Fp.sub(t4, t5);
|
|
3256
|
-
t5 = Fp.add(Y1, Z1);
|
|
3257
|
-
X3 = Fp.add(Y2, Z2);
|
|
3258
|
-
t5 = Fp.mul(t5, X3);
|
|
3259
|
-
X3 = Fp.add(t1, t2);
|
|
3260
|
-
t5 = Fp.sub(t5, X3);
|
|
3261
|
-
Z3 = Fp.mul(a, t4);
|
|
3262
|
-
X3 = Fp.mul(b3, t2);
|
|
3263
|
-
Z3 = Fp.add(X3, Z3);
|
|
3264
|
-
X3 = Fp.sub(t1, Z3);
|
|
3265
|
-
Z3 = Fp.add(t1, Z3);
|
|
3266
|
-
Y3 = Fp.mul(X3, Z3);
|
|
3267
|
-
t1 = Fp.add(t0, t0);
|
|
3268
|
-
t1 = Fp.add(t1, t0);
|
|
3269
|
-
t2 = Fp.mul(a, t2);
|
|
3270
|
-
t4 = Fp.mul(b3, t4);
|
|
3271
|
-
t1 = Fp.add(t1, t2);
|
|
3272
|
-
t2 = Fp.sub(t0, t2);
|
|
3273
|
-
t2 = Fp.mul(a, t2);
|
|
3274
|
-
t4 = Fp.add(t4, t2);
|
|
3275
|
-
t0 = Fp.mul(t1, t4);
|
|
3276
|
-
Y3 = Fp.add(Y3, t0);
|
|
3277
|
-
t0 = Fp.mul(t5, t4);
|
|
3278
|
-
X3 = Fp.mul(t3, X3);
|
|
3279
|
-
X3 = Fp.sub(X3, t0);
|
|
3280
|
-
t0 = Fp.mul(t3, t1);
|
|
3281
|
-
Z3 = Fp.mul(t5, Z3);
|
|
3282
|
-
Z3 = Fp.add(Z3, t0);
|
|
3283
|
-
return new Point(X3, Y3, Z3);
|
|
3284
|
-
}
|
|
3285
|
-
subtract(other) {
|
|
3286
|
-
return this.add(other.negate());
|
|
3287
|
-
}
|
|
3288
|
-
is0() {
|
|
3289
|
-
return this.equals(Point.ZERO);
|
|
3290
|
-
}
|
|
3291
|
-
wNAF(n) {
|
|
3292
|
-
return wnaf.wNAFCached(this, pointPrecomputes, n, (comp) => {
|
|
3293
|
-
const toInv = Fp.invertBatch(comp.map((p) => p.pz));
|
|
3294
|
-
return comp.map((p, i) => p.toAffine(toInv[i])).map(Point.fromAffine);
|
|
3295
|
-
});
|
|
3296
|
-
}
|
|
3297
|
-
/**
|
|
3298
|
-
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
3299
|
-
* It's faster, but should only be used when you don't care about
|
|
3300
|
-
* an exposed private key e.g. sig verification, which works over *public* keys.
|
|
3301
|
-
*/
|
|
3302
|
-
multiplyUnsafe(n) {
|
|
3303
|
-
const I = Point.ZERO;
|
|
3304
|
-
if (n === _0n4)
|
|
3305
|
-
return I;
|
|
3306
|
-
assertGE(n);
|
|
3307
|
-
if (n === _1n4)
|
|
3308
|
-
return this;
|
|
3309
|
-
const { endo } = CURVE2;
|
|
3310
|
-
if (!endo)
|
|
3311
|
-
return wnaf.unsafeLadder(this, n);
|
|
3312
|
-
let { k1neg, k1, k2neg, k2 } = endo.splitScalar(n);
|
|
3313
|
-
let k1p = I;
|
|
3314
|
-
let k2p = I;
|
|
3315
|
-
let d = this;
|
|
3316
|
-
while (k1 > _0n4 || k2 > _0n4) {
|
|
3317
|
-
if (k1 & _1n4)
|
|
3318
|
-
k1p = k1p.add(d);
|
|
3319
|
-
if (k2 & _1n4)
|
|
3320
|
-
k2p = k2p.add(d);
|
|
3321
|
-
d = d.double();
|
|
3322
|
-
k1 >>= _1n4;
|
|
3323
|
-
k2 >>= _1n4;
|
|
3324
|
-
}
|
|
3325
|
-
if (k1neg)
|
|
3326
|
-
k1p = k1p.negate();
|
|
3327
|
-
if (k2neg)
|
|
3328
|
-
k2p = k2p.negate();
|
|
3329
|
-
k2p = new Point(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
|
|
3330
|
-
return k1p.add(k2p);
|
|
3331
|
-
}
|
|
3332
|
-
/**
|
|
3333
|
-
* Constant time multiplication.
|
|
3334
|
-
* Uses wNAF method. Windowed method may be 10% faster,
|
|
3335
|
-
* but takes 2x longer to generate and consumes 2x memory.
|
|
3336
|
-
* Uses precomputes when available.
|
|
3337
|
-
* Uses endomorphism for Koblitz curves.
|
|
3338
|
-
* @param scalar by which the point would be multiplied
|
|
3339
|
-
* @returns New point
|
|
3340
|
-
*/
|
|
3341
|
-
multiply(scalar) {
|
|
3342
|
-
assertGE(scalar);
|
|
3343
|
-
let n = scalar;
|
|
3344
|
-
let point, fake;
|
|
3345
|
-
const { endo } = CURVE2;
|
|
3346
|
-
if (endo) {
|
|
3347
|
-
const { k1neg, k1, k2neg, k2 } = endo.splitScalar(n);
|
|
3348
|
-
let { p: k1p, f: f1p } = this.wNAF(k1);
|
|
3349
|
-
let { p: k2p, f: f2p } = this.wNAF(k2);
|
|
3350
|
-
k1p = wnaf.constTimeNegate(k1neg, k1p);
|
|
3351
|
-
k2p = wnaf.constTimeNegate(k2neg, k2p);
|
|
3352
|
-
k2p = new Point(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
|
|
3353
|
-
point = k1p.add(k2p);
|
|
3354
|
-
fake = f1p.add(f2p);
|
|
3355
|
-
} else {
|
|
3356
|
-
const { p, f } = this.wNAF(n);
|
|
3357
|
-
point = p;
|
|
3358
|
-
fake = f;
|
|
3359
|
-
}
|
|
3360
|
-
return Point.normalizeZ([point, fake])[0];
|
|
3361
|
-
}
|
|
3362
|
-
/**
|
|
3363
|
-
* Efficiently calculate `aP + bQ`. Unsafe, can expose private key, if used incorrectly.
|
|
3364
|
-
* Not using Strauss-Shamir trick: precomputation tables are faster.
|
|
3365
|
-
* The trick could be useful if both P and Q are not G (not in our case).
|
|
3366
|
-
* @returns non-zero affine point
|
|
3367
|
-
*/
|
|
3368
|
-
multiplyAndAddUnsafe(Q, a, b) {
|
|
3369
|
-
const G = Point.BASE;
|
|
3370
|
-
const mul = (P, a2) => a2 === _0n4 || a2 === _1n4 || !P.equals(G) ? P.multiplyUnsafe(a2) : P.multiply(a2);
|
|
3371
|
-
const sum = mul(this, a).add(mul(Q, b));
|
|
3372
|
-
return sum.is0() ? void 0 : sum;
|
|
3373
|
-
}
|
|
3374
|
-
// Converts Projective point to affine (x, y) coordinates.
|
|
3375
|
-
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
3376
|
-
// (x, y, z) ∋ (x=x/z, y=y/z)
|
|
3377
|
-
toAffine(iz) {
|
|
3378
|
-
const { px: x, py: y, pz: z } = this;
|
|
3379
|
-
const is0 = this.is0();
|
|
3380
|
-
if (iz == null)
|
|
3381
|
-
iz = is0 ? Fp.ONE : Fp.inv(z);
|
|
3382
|
-
const ax = Fp.mul(x, iz);
|
|
3383
|
-
const ay = Fp.mul(y, iz);
|
|
3384
|
-
const zz = Fp.mul(z, iz);
|
|
3385
|
-
if (is0)
|
|
3386
|
-
return { x: Fp.ZERO, y: Fp.ZERO };
|
|
3387
|
-
if (!Fp.eql(zz, Fp.ONE))
|
|
3388
|
-
throw new Error("invZ was invalid");
|
|
3389
|
-
return { x: ax, y: ay };
|
|
3390
|
-
}
|
|
3391
|
-
isTorsionFree() {
|
|
3392
|
-
const { h: cofactor, isTorsionFree } = CURVE2;
|
|
3393
|
-
if (cofactor === _1n4)
|
|
3394
|
-
return true;
|
|
3395
|
-
if (isTorsionFree)
|
|
3396
|
-
return isTorsionFree(Point, this);
|
|
3397
|
-
throw new Error("isTorsionFree() has not been declared for the elliptic curve");
|
|
3398
|
-
}
|
|
3399
|
-
clearCofactor() {
|
|
3400
|
-
const { h: cofactor, clearCofactor } = CURVE2;
|
|
3401
|
-
if (cofactor === _1n4)
|
|
3402
|
-
return this;
|
|
3403
|
-
if (clearCofactor)
|
|
3404
|
-
return clearCofactor(Point, this);
|
|
3405
|
-
return this.multiplyUnsafe(CURVE2.h);
|
|
3406
|
-
}
|
|
3407
|
-
toRawBytes(isCompressed = true) {
|
|
3408
|
-
this.assertValidity();
|
|
3409
|
-
return toBytes2(Point, this, isCompressed);
|
|
3410
|
-
}
|
|
3411
|
-
toHex(isCompressed = true) {
|
|
3412
|
-
return bytesToHex(this.toRawBytes(isCompressed));
|
|
3413
|
-
}
|
|
3414
|
-
}
|
|
3415
|
-
Point.BASE = new Point(CURVE2.Gx, CURVE2.Gy, Fp.ONE);
|
|
3416
|
-
Point.ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO);
|
|
3417
|
-
const _bits = CURVE2.nBitLength;
|
|
3418
|
-
const wnaf = wNAF(Point, CURVE2.endo ? Math.ceil(_bits / 2) : _bits);
|
|
3419
|
-
return {
|
|
3420
|
-
CURVE: CURVE2,
|
|
3421
|
-
ProjectivePoint: Point,
|
|
3422
|
-
normPrivateKeyToScalar,
|
|
3423
|
-
weierstrassEquation,
|
|
3424
|
-
isWithinCurveOrder
|
|
3425
|
-
};
|
|
3426
|
-
}
|
|
3427
|
-
function validateOpts2(curve2) {
|
|
3428
|
-
const opts = validateBasic(curve2);
|
|
3429
|
-
validateObject(opts, {
|
|
3430
|
-
hash: "hash",
|
|
3431
|
-
hmac: "function",
|
|
3432
|
-
randomBytes: "function"
|
|
3433
|
-
}, {
|
|
3434
|
-
bits2int: "function",
|
|
3435
|
-
bits2int_modN: "function",
|
|
3436
|
-
lowS: "boolean"
|
|
3437
|
-
});
|
|
3438
|
-
return Object.freeze({ lowS: true, ...opts });
|
|
3439
|
-
}
|
|
3440
|
-
function weierstrass(curveDef) {
|
|
3441
|
-
const CURVE2 = validateOpts2(curveDef);
|
|
3442
|
-
const { Fp, n: CURVE_ORDER2 } = CURVE2;
|
|
3443
|
-
const compressedLen = Fp.BYTES + 1;
|
|
3444
|
-
const uncompressedLen = 2 * Fp.BYTES + 1;
|
|
3445
|
-
function isValidFieldElement(num10) {
|
|
3446
|
-
return _0n4 < num10 && num10 < Fp.ORDER;
|
|
3447
|
-
}
|
|
3448
|
-
function modN(a) {
|
|
3449
|
-
return mod(a, CURVE_ORDER2);
|
|
3450
|
-
}
|
|
3451
|
-
function invN(a) {
|
|
3452
|
-
return invert(a, CURVE_ORDER2);
|
|
3453
|
-
}
|
|
3454
|
-
const { ProjectivePoint: Point, normPrivateKeyToScalar, weierstrassEquation, isWithinCurveOrder } = weierstrassPoints({
|
|
3455
|
-
...CURVE2,
|
|
3456
|
-
toBytes(_c, point, isCompressed) {
|
|
3457
|
-
const a = point.toAffine();
|
|
3458
|
-
const x = Fp.toBytes(a.x);
|
|
3459
|
-
const cat = concatBytes2;
|
|
3460
|
-
if (isCompressed) {
|
|
3461
|
-
return cat(Uint8Array.from([point.hasEvenY() ? 2 : 3]), x);
|
|
3462
|
-
} else {
|
|
3463
|
-
return cat(Uint8Array.from([4]), x, Fp.toBytes(a.y));
|
|
3464
|
-
}
|
|
3465
|
-
},
|
|
3466
|
-
fromBytes(bytes2) {
|
|
3467
|
-
const len = bytes2.length;
|
|
3468
|
-
const head = bytes2[0];
|
|
3469
|
-
const tail = bytes2.subarray(1);
|
|
3470
|
-
if (len === compressedLen && (head === 2 || head === 3)) {
|
|
3471
|
-
const x = bytesToNumberBE(tail);
|
|
3472
|
-
if (!isValidFieldElement(x))
|
|
3473
|
-
throw new Error("Point is not on curve");
|
|
3474
|
-
const y2 = weierstrassEquation(x);
|
|
3475
|
-
let y = Fp.sqrt(y2);
|
|
3476
|
-
const isYOdd = (y & _1n4) === _1n4;
|
|
3477
|
-
const isHeadOdd = (head & 1) === 1;
|
|
3478
|
-
if (isHeadOdd !== isYOdd)
|
|
3479
|
-
y = Fp.neg(y);
|
|
3480
|
-
return { x, y };
|
|
3481
|
-
} else if (len === uncompressedLen && head === 4) {
|
|
3482
|
-
const x = Fp.fromBytes(tail.subarray(0, Fp.BYTES));
|
|
3483
|
-
const y = Fp.fromBytes(tail.subarray(Fp.BYTES, 2 * Fp.BYTES));
|
|
3484
|
-
return { x, y };
|
|
3485
|
-
} else {
|
|
3486
|
-
throw new Error(`Point of length ${len} was invalid. Expected ${compressedLen} compressed bytes or ${uncompressedLen} uncompressed bytes`);
|
|
3487
|
-
}
|
|
3488
|
-
}
|
|
3489
|
-
});
|
|
3490
|
-
const numToNByteStr = (num10) => bytesToHex(numberToBytesBE(num10, CURVE2.nByteLength));
|
|
3491
|
-
function isBiggerThanHalfOrder(number2) {
|
|
3492
|
-
const HALF = CURVE_ORDER2 >> _1n4;
|
|
3493
|
-
return number2 > HALF;
|
|
3494
|
-
}
|
|
3495
|
-
function normalizeS(s) {
|
|
3496
|
-
return isBiggerThanHalfOrder(s) ? modN(-s) : s;
|
|
3497
|
-
}
|
|
3498
|
-
const slcNum = (b, from, to) => bytesToNumberBE(b.slice(from, to));
|
|
3499
|
-
class Signature2 {
|
|
3500
|
-
constructor(r, s, recovery) {
|
|
3501
|
-
this.r = r;
|
|
3502
|
-
this.s = s;
|
|
3503
|
-
this.recovery = recovery;
|
|
3504
|
-
this.assertValidity();
|
|
3505
|
-
}
|
|
3506
|
-
// pair (bytes of r, bytes of s)
|
|
3507
|
-
static fromCompact(hex) {
|
|
3508
|
-
const l = CURVE2.nByteLength;
|
|
3509
|
-
hex = ensureBytes("compactSignature", hex, l * 2);
|
|
3510
|
-
return new Signature2(slcNum(hex, 0, l), slcNum(hex, l, 2 * l));
|
|
3511
|
-
}
|
|
3512
|
-
// DER encoded ECDSA signature
|
|
3513
|
-
// https://bitcoin.stackexchange.com/questions/57644/what-are-the-parts-of-a-bitcoin-transaction-input-script
|
|
3514
|
-
static fromDER(hex) {
|
|
3515
|
-
const { r, s } = DER.toSig(ensureBytes("DER", hex));
|
|
3516
|
-
return new Signature2(r, s);
|
|
3517
|
-
}
|
|
3518
|
-
assertValidity() {
|
|
3519
|
-
if (!isWithinCurveOrder(this.r))
|
|
3520
|
-
throw new Error("r must be 0 < r < CURVE.n");
|
|
3521
|
-
if (!isWithinCurveOrder(this.s))
|
|
3522
|
-
throw new Error("s must be 0 < s < CURVE.n");
|
|
3523
|
-
}
|
|
3524
|
-
addRecoveryBit(recovery) {
|
|
3525
|
-
return new Signature2(this.r, this.s, recovery);
|
|
3526
|
-
}
|
|
3527
|
-
recoverPublicKey(msgHash) {
|
|
3528
|
-
const { r, s, recovery: rec } = this;
|
|
3529
|
-
const h = bits2int_modN(ensureBytes("msgHash", msgHash));
|
|
3530
|
-
if (rec == null || ![0, 1, 2, 3].includes(rec))
|
|
3531
|
-
throw new Error("recovery id invalid");
|
|
3532
|
-
const radj = rec === 2 || rec === 3 ? r + CURVE2.n : r;
|
|
3533
|
-
if (radj >= Fp.ORDER)
|
|
3534
|
-
throw new Error("recovery id 2 or 3 invalid");
|
|
3535
|
-
const prefix = (rec & 1) === 0 ? "02" : "03";
|
|
3536
|
-
const R = Point.fromHex(prefix + numToNByteStr(radj));
|
|
3537
|
-
const ir = invN(radj);
|
|
3538
|
-
const u1 = modN(-h * ir);
|
|
3539
|
-
const u2 = modN(s * ir);
|
|
3540
|
-
const Q = Point.BASE.multiplyAndAddUnsafe(R, u1, u2);
|
|
3541
|
-
if (!Q)
|
|
3542
|
-
throw new Error("point at infinify");
|
|
3543
|
-
Q.assertValidity();
|
|
3544
|
-
return Q;
|
|
3545
|
-
}
|
|
3546
|
-
// Signatures should be low-s, to prevent malleability.
|
|
3547
|
-
hasHighS() {
|
|
3548
|
-
return isBiggerThanHalfOrder(this.s);
|
|
3549
|
-
}
|
|
3550
|
-
normalizeS() {
|
|
3551
|
-
return this.hasHighS() ? new Signature2(this.r, modN(-this.s), this.recovery) : this;
|
|
3552
|
-
}
|
|
3553
|
-
// DER-encoded
|
|
3554
|
-
toDERRawBytes() {
|
|
3555
|
-
return hexToBytes(this.toDERHex());
|
|
3556
|
-
}
|
|
3557
|
-
toDERHex() {
|
|
3558
|
-
return DER.hexFromSig({ r: this.r, s: this.s });
|
|
3559
|
-
}
|
|
3560
|
-
// padded bytes of r, then padded bytes of s
|
|
3561
|
-
toCompactRawBytes() {
|
|
3562
|
-
return hexToBytes(this.toCompactHex());
|
|
3563
|
-
}
|
|
3564
|
-
toCompactHex() {
|
|
3565
|
-
return numToNByteStr(this.r) + numToNByteStr(this.s);
|
|
3566
|
-
}
|
|
3567
|
-
}
|
|
3568
|
-
const utils2 = {
|
|
3569
|
-
isValidPrivateKey(privateKey) {
|
|
3570
|
-
try {
|
|
3571
|
-
normPrivateKeyToScalar(privateKey);
|
|
3572
|
-
return true;
|
|
3573
|
-
} catch (error) {
|
|
3574
|
-
return false;
|
|
3575
|
-
}
|
|
3576
|
-
},
|
|
3577
|
-
normPrivateKeyToScalar,
|
|
3578
|
-
/**
|
|
3579
|
-
* Produces cryptographically secure private key from random of size
|
|
3580
|
-
* (groupLen + ceil(groupLen / 2)) with modulo bias being negligible.
|
|
3581
|
-
*/
|
|
3582
|
-
randomPrivateKey: () => {
|
|
3583
|
-
const length = getMinHashLength(CURVE2.n);
|
|
3584
|
-
return mapHashToField(CURVE2.randomBytes(length), CURVE2.n);
|
|
3585
|
-
},
|
|
3586
|
-
/**
|
|
3587
|
-
* Creates precompute table for an arbitrary EC point. Makes point "cached".
|
|
3588
|
-
* Allows to massively speed-up `point.multiply(scalar)`.
|
|
3589
|
-
* @returns cached point
|
|
3590
|
-
* @example
|
|
3591
|
-
* const fast = utils.precompute(8, ProjectivePoint.fromHex(someonesPubKey));
|
|
3592
|
-
* fast.multiply(privKey); // much faster ECDH now
|
|
3593
|
-
*/
|
|
3594
|
-
precompute(windowSize = 8, point = Point.BASE) {
|
|
3595
|
-
point._setWindowSize(windowSize);
|
|
3596
|
-
point.multiply(BigInt(3));
|
|
3597
|
-
return point;
|
|
3598
|
-
}
|
|
3599
|
-
};
|
|
3600
|
-
function getPublicKey(privateKey, isCompressed = true) {
|
|
3601
|
-
return Point.fromPrivateKey(privateKey).toRawBytes(isCompressed);
|
|
3602
|
-
}
|
|
3603
|
-
function isProbPub(item) {
|
|
3604
|
-
const arr = item instanceof Uint8Array;
|
|
3605
|
-
const str = typeof item === "string";
|
|
3606
|
-
const len = (arr || str) && item.length;
|
|
3607
|
-
if (arr)
|
|
3608
|
-
return len === compressedLen || len === uncompressedLen;
|
|
3609
|
-
if (str)
|
|
3610
|
-
return len === 2 * compressedLen || len === 2 * uncompressedLen;
|
|
3611
|
-
if (item instanceof Point)
|
|
3612
|
-
return true;
|
|
3613
|
-
return false;
|
|
3614
|
-
}
|
|
3615
|
-
function getSharedSecret(privateA, publicB, isCompressed = true) {
|
|
3616
|
-
if (isProbPub(privateA))
|
|
3617
|
-
throw new Error("first arg must be private key");
|
|
3618
|
-
if (!isProbPub(publicB))
|
|
3619
|
-
throw new Error("second arg must be public key");
|
|
3620
|
-
const b = Point.fromHex(publicB);
|
|
3621
|
-
return b.multiply(normPrivateKeyToScalar(privateA)).toRawBytes(isCompressed);
|
|
3622
|
-
}
|
|
3623
|
-
const bits2int2 = CURVE2.bits2int || function(bytes2) {
|
|
3624
|
-
const num10 = bytesToNumberBE(bytes2);
|
|
3625
|
-
const delta = bytes2.length * 8 - CURVE2.nBitLength;
|
|
3626
|
-
return delta > 0 ? num10 >> BigInt(delta) : num10;
|
|
3627
|
-
};
|
|
3628
|
-
const bits2int_modN = CURVE2.bits2int_modN || function(bytes2) {
|
|
3629
|
-
return modN(bits2int2(bytes2));
|
|
3630
|
-
};
|
|
3631
|
-
const ORDER_MASK = bitMask(CURVE2.nBitLength);
|
|
3632
|
-
function int2octets(num10) {
|
|
3633
|
-
if (typeof num10 !== "bigint")
|
|
3634
|
-
throw new Error("bigint expected");
|
|
3635
|
-
if (!(_0n4 <= num10 && num10 < ORDER_MASK))
|
|
3636
|
-
throw new Error(`bigint expected < 2^${CURVE2.nBitLength}`);
|
|
3637
|
-
return numberToBytesBE(num10, CURVE2.nByteLength);
|
|
3638
|
-
}
|
|
3639
|
-
function prepSig(msgHash, privateKey, opts = defaultSigOpts) {
|
|
3640
|
-
if (["recovered", "canonical"].some((k) => k in opts))
|
|
3641
|
-
throw new Error("sign() legacy options not supported");
|
|
3642
|
-
const { hash: hash5, randomBytes: randomBytes2 } = CURVE2;
|
|
3643
|
-
let { lowS, prehash, extraEntropy: ent } = opts;
|
|
3644
|
-
if (lowS == null)
|
|
3645
|
-
lowS = true;
|
|
3646
|
-
msgHash = ensureBytes("msgHash", msgHash);
|
|
3647
|
-
if (prehash)
|
|
3648
|
-
msgHash = ensureBytes("prehashed msgHash", hash5(msgHash));
|
|
3649
|
-
const h1int = bits2int_modN(msgHash);
|
|
3650
|
-
const d = normPrivateKeyToScalar(privateKey);
|
|
3651
|
-
const seedArgs = [int2octets(d), int2octets(h1int)];
|
|
3652
|
-
if (ent != null) {
|
|
3653
|
-
const e = ent === true ? randomBytes2(Fp.BYTES) : ent;
|
|
3654
|
-
seedArgs.push(ensureBytes("extraEntropy", e));
|
|
3655
|
-
}
|
|
3656
|
-
const seed = concatBytes2(...seedArgs);
|
|
3657
|
-
const m = h1int;
|
|
3658
|
-
function k2sig(kBytes) {
|
|
3659
|
-
const k = bits2int2(kBytes);
|
|
3660
|
-
if (!isWithinCurveOrder(k))
|
|
3661
|
-
return;
|
|
3662
|
-
const ik = invN(k);
|
|
3663
|
-
const q = Point.BASE.multiply(k).toAffine();
|
|
3664
|
-
const r = modN(q.x);
|
|
3665
|
-
if (r === _0n4)
|
|
3666
|
-
return;
|
|
3667
|
-
const s = modN(ik * modN(m + r * d));
|
|
3668
|
-
if (s === _0n4)
|
|
3669
|
-
return;
|
|
3670
|
-
let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n4);
|
|
3671
|
-
let normS = s;
|
|
3672
|
-
if (lowS && isBiggerThanHalfOrder(s)) {
|
|
3673
|
-
normS = normalizeS(s);
|
|
3674
|
-
recovery ^= 1;
|
|
3675
|
-
}
|
|
3676
|
-
return new Signature2(r, normS, recovery);
|
|
3677
|
-
}
|
|
3678
|
-
return { seed, k2sig };
|
|
3679
|
-
}
|
|
3680
|
-
const defaultSigOpts = { lowS: CURVE2.lowS, prehash: false };
|
|
3681
|
-
const defaultVerOpts = { lowS: CURVE2.lowS, prehash: false };
|
|
3682
|
-
function sign(msgHash, privKey, opts = defaultSigOpts) {
|
|
3683
|
-
const { seed, k2sig } = prepSig(msgHash, privKey, opts);
|
|
3684
|
-
const C = CURVE2;
|
|
3685
|
-
const drbg = createHmacDrbg(C.hash.outputLen, C.nByteLength, C.hmac);
|
|
3686
|
-
return drbg(seed, k2sig);
|
|
3687
|
-
}
|
|
3688
|
-
Point.BASE._setWindowSize(8);
|
|
3689
|
-
function verify(signature, msgHash, publicKey, opts = defaultVerOpts) {
|
|
3690
|
-
const sg = signature;
|
|
3691
|
-
msgHash = ensureBytes("msgHash", msgHash);
|
|
3692
|
-
publicKey = ensureBytes("publicKey", publicKey);
|
|
3693
|
-
if ("strict" in opts)
|
|
3694
|
-
throw new Error("options.strict was renamed to lowS");
|
|
3695
|
-
const { lowS, prehash } = opts;
|
|
3696
|
-
let _sig = void 0;
|
|
3697
|
-
let P;
|
|
3698
|
-
try {
|
|
3699
|
-
if (typeof sg === "string" || sg instanceof Uint8Array) {
|
|
3700
|
-
try {
|
|
3701
|
-
_sig = Signature2.fromDER(sg);
|
|
3702
|
-
} catch (derError) {
|
|
3703
|
-
if (!(derError instanceof DER.Err))
|
|
3704
|
-
throw derError;
|
|
3705
|
-
_sig = Signature2.fromCompact(sg);
|
|
3706
|
-
}
|
|
3707
|
-
} else if (typeof sg === "object" && typeof sg.r === "bigint" && typeof sg.s === "bigint") {
|
|
3708
|
-
const { r: r2, s: s2 } = sg;
|
|
3709
|
-
_sig = new Signature2(r2, s2);
|
|
3710
|
-
} else {
|
|
3711
|
-
throw new Error("PARSE");
|
|
3712
|
-
}
|
|
3713
|
-
P = Point.fromHex(publicKey);
|
|
3714
|
-
} catch (error) {
|
|
3715
|
-
if (error.message === "PARSE")
|
|
3716
|
-
throw new Error(`signature must be Signature instance, Uint8Array or hex string`);
|
|
3717
|
-
return false;
|
|
3718
|
-
}
|
|
3719
|
-
if (lowS && _sig.hasHighS())
|
|
3720
|
-
return false;
|
|
3721
|
-
if (prehash)
|
|
3722
|
-
msgHash = CURVE2.hash(msgHash);
|
|
3723
|
-
const { r, s } = _sig;
|
|
3724
|
-
const h = bits2int_modN(msgHash);
|
|
3725
|
-
const is = invN(s);
|
|
3726
|
-
const u1 = modN(h * is);
|
|
3727
|
-
const u2 = modN(r * is);
|
|
3728
|
-
const R = Point.BASE.multiplyAndAddUnsafe(P, u1, u2)?.toAffine();
|
|
3729
|
-
if (!R)
|
|
3730
|
-
return false;
|
|
3731
|
-
const v = modN(R.x);
|
|
3732
|
-
return v === r;
|
|
3733
|
-
}
|
|
3734
|
-
return {
|
|
3735
|
-
CURVE: CURVE2,
|
|
3736
|
-
getPublicKey,
|
|
3737
|
-
getSharedSecret,
|
|
3738
|
-
sign,
|
|
3739
|
-
verify,
|
|
3740
|
-
ProjectivePoint: Point,
|
|
3741
|
-
Signature: Signature2,
|
|
3742
|
-
utils: utils2
|
|
3743
|
-
};
|
|
3744
|
-
}
|
|
3745
|
-
|
|
3746
|
-
// ../node_modules/@noble/hashes/esm/hmac.js
|
|
3747
|
-
var HMAC = class extends Hash {
|
|
3748
|
-
constructor(hash5, _key) {
|
|
3749
|
-
super();
|
|
3750
|
-
this.finished = false;
|
|
3751
|
-
this.destroyed = false;
|
|
3752
|
-
hash(hash5);
|
|
3753
|
-
const key = toBytes(_key);
|
|
3754
|
-
this.iHash = hash5.create();
|
|
3755
|
-
if (typeof this.iHash.update !== "function")
|
|
3756
|
-
throw new Error("Expected instance of class which extends utils.Hash");
|
|
3757
|
-
this.blockLen = this.iHash.blockLen;
|
|
3758
|
-
this.outputLen = this.iHash.outputLen;
|
|
3759
|
-
const blockLen = this.blockLen;
|
|
3760
|
-
const pad = new Uint8Array(blockLen);
|
|
3761
|
-
pad.set(key.length > blockLen ? hash5.create().update(key).digest() : key);
|
|
3762
|
-
for (let i = 0; i < pad.length; i++)
|
|
3763
|
-
pad[i] ^= 54;
|
|
3764
|
-
this.iHash.update(pad);
|
|
3765
|
-
this.oHash = hash5.create();
|
|
3766
|
-
for (let i = 0; i < pad.length; i++)
|
|
3767
|
-
pad[i] ^= 54 ^ 92;
|
|
3768
|
-
this.oHash.update(pad);
|
|
3769
|
-
pad.fill(0);
|
|
3770
|
-
}
|
|
3771
|
-
update(buf) {
|
|
3772
|
-
exists(this);
|
|
3773
|
-
this.iHash.update(buf);
|
|
3774
|
-
return this;
|
|
3775
|
-
}
|
|
3776
|
-
digestInto(out) {
|
|
3777
|
-
exists(this);
|
|
3778
|
-
bytes(out, this.outputLen);
|
|
3779
|
-
this.finished = true;
|
|
3780
|
-
this.iHash.digestInto(out);
|
|
3781
|
-
this.oHash.update(out);
|
|
3782
|
-
this.oHash.digestInto(out);
|
|
3783
|
-
this.destroy();
|
|
3784
|
-
}
|
|
3785
|
-
digest() {
|
|
3786
|
-
const out = new Uint8Array(this.oHash.outputLen);
|
|
3787
|
-
this.digestInto(out);
|
|
3788
|
-
return out;
|
|
3789
|
-
}
|
|
3790
|
-
_cloneInto(to) {
|
|
3791
|
-
to || (to = Object.create(Object.getPrototypeOf(this), {}));
|
|
3792
|
-
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
3793
|
-
to = to;
|
|
3794
|
-
to.finished = finished;
|
|
3795
|
-
to.destroyed = destroyed;
|
|
3796
|
-
to.blockLen = blockLen;
|
|
3797
|
-
to.outputLen = outputLen;
|
|
3798
|
-
to.oHash = oHash._cloneInto(to.oHash);
|
|
3799
|
-
to.iHash = iHash._cloneInto(to.iHash);
|
|
3800
|
-
return to;
|
|
3801
|
-
}
|
|
3802
|
-
destroy() {
|
|
3803
|
-
this.destroyed = true;
|
|
3804
|
-
this.oHash.destroy();
|
|
3805
|
-
this.iHash.destroy();
|
|
3806
|
-
}
|
|
3807
|
-
};
|
|
3808
|
-
var hmac = (hash5, key, message) => new HMAC(hash5, key).update(message).digest();
|
|
3809
|
-
hmac.create = (hash5, key) => new HMAC(hash5, key);
|
|
3810
|
-
|
|
3811
|
-
// ../node_modules/@noble/curves/esm/_shortw_utils.js
|
|
3812
|
-
function getHash(hash5) {
|
|
3813
|
-
return {
|
|
3814
|
-
hash: hash5,
|
|
3815
|
-
hmac: (key, ...msgs) => hmac(hash5, key, concatBytes(...msgs)),
|
|
3816
|
-
randomBytes
|
|
3817
|
-
};
|
|
3818
|
-
}
|
|
3819
|
-
|
|
3820
|
-
// ../node_modules/@scure/starknet/lib/esm/index.js
|
|
3821
|
-
var CURVE_ORDER = BigInt("3618502788666131213697322783095070105526743751716087489154079457884512865583");
|
|
3822
|
-
var MAX_VALUE = BigInt("0x800000000000000000000000000000000000000000000000000000000000000");
|
|
3823
|
-
var nBitLength = 252;
|
|
3824
|
-
function bits2int(bytes2) {
|
|
3825
|
-
while (bytes2[0] === 0)
|
|
3826
|
-
bytes2 = bytes2.subarray(1);
|
|
3827
|
-
const delta = bytes2.length * 8 - nBitLength;
|
|
3828
|
-
const num10 = bytesToNumberBE(bytes2);
|
|
3829
|
-
return delta > 0 ? num10 >> BigInt(delta) : num10;
|
|
3830
|
-
}
|
|
3831
|
-
function hex0xToBytes(hex) {
|
|
3832
|
-
if (typeof hex === "string") {
|
|
3833
|
-
hex = strip0x(hex);
|
|
3834
|
-
if (hex.length & 1)
|
|
3835
|
-
hex = "0" + hex;
|
|
3836
|
-
}
|
|
3837
|
-
return hexToBytes(hex);
|
|
3838
|
-
}
|
|
3839
|
-
var curve = weierstrass({
|
|
3840
|
-
a: BigInt(1),
|
|
3841
|
-
b: BigInt("3141592653589793238462643383279502884197169399375105820974944592307816406665"),
|
|
3842
|
-
Fp: Field(BigInt("0x800000000000011000000000000000000000000000000000000000000000001")),
|
|
3843
|
-
n: CURVE_ORDER,
|
|
3844
|
-
nBitLength,
|
|
3845
|
-
Gx: BigInt("874739451078007766457464989774322083649278607533249481151382481072868806602"),
|
|
3846
|
-
Gy: BigInt("152666792071518830868575557812948353041420400780739481342941381225525861407"),
|
|
3847
|
-
h: BigInt(1),
|
|
3848
|
-
lowS: false,
|
|
3849
|
-
...getHash(sha256),
|
|
3850
|
-
bits2int,
|
|
3851
|
-
bits2int_modN: (bytes2) => {
|
|
3852
|
-
const hex = bytesToNumberBE(bytes2).toString(16);
|
|
3853
|
-
if (hex.length === 63)
|
|
3854
|
-
bytes2 = hex0xToBytes(hex + "0");
|
|
3855
|
-
return mod(bits2int(bytes2), CURVE_ORDER);
|
|
3856
|
-
}
|
|
3857
|
-
});
|
|
3858
|
-
function ensureBytes2(hex) {
|
|
3859
|
-
return ensureBytes("", typeof hex === "string" ? hex0xToBytes(hex) : hex);
|
|
3860
|
-
}
|
|
3861
|
-
var { CURVE, ProjectivePoint, Signature, utils } = curve;
|
|
3862
|
-
function extractX(bytes2) {
|
|
3863
|
-
const hex = bytesToHex(bytes2.subarray(1));
|
|
3864
|
-
const stripped = hex.replace(/^0+/gm, "");
|
|
3865
|
-
return `0x${stripped}`;
|
|
3866
|
-
}
|
|
3867
|
-
function strip0x(hex) {
|
|
3868
|
-
return hex.replace(/^0x/i, "");
|
|
3869
|
-
}
|
|
3870
|
-
var MASK_31 = 2n ** 31n - 1n;
|
|
3871
|
-
var PEDERSEN_POINTS = [
|
|
3872
|
-
new ProjectivePoint(2089986280348253421170679821480865132823066470938446095505822317253594081284n, 1713931329540660377023406109199410414810705867260802078187082345529207694986n, 1n),
|
|
3873
|
-
new ProjectivePoint(996781205833008774514500082376783249102396023663454813447423147977397232763n, 1668503676786377725805489344771023921079126552019160156920634619255970485781n, 1n),
|
|
3874
|
-
new ProjectivePoint(2251563274489750535117886426533222435294046428347329203627021249169616184184n, 1798716007562728905295480679789526322175868328062420237419143593021674992973n, 1n),
|
|
3875
|
-
new ProjectivePoint(2138414695194151160943305727036575959195309218611738193261179310511854807447n, 113410276730064486255102093846540133784865286929052426931474106396135072156n, 1n),
|
|
3876
|
-
new ProjectivePoint(2379962749567351885752724891227938183011949129833673362440656643086021394946n, 776496453633298175483985398648758586525933812536653089401905292063708816422n, 1n)
|
|
3877
|
-
];
|
|
3878
|
-
function pedersenPrecompute(p1, p2) {
|
|
3879
|
-
const out = [];
|
|
3880
|
-
let p = p1;
|
|
3881
|
-
for (let i = 0; i < 248; i++) {
|
|
3882
|
-
out.push(p);
|
|
3883
|
-
p = p.double();
|
|
3884
|
-
}
|
|
3885
|
-
p = p2;
|
|
3886
|
-
for (let i = 0; i < 4; i++) {
|
|
3887
|
-
out.push(p);
|
|
3888
|
-
p = p.double();
|
|
3889
|
-
}
|
|
3890
|
-
return out;
|
|
3891
|
-
}
|
|
3892
|
-
var PEDERSEN_POINTS1 = pedersenPrecompute(PEDERSEN_POINTS[1], PEDERSEN_POINTS[2]);
|
|
3893
|
-
var PEDERSEN_POINTS2 = pedersenPrecompute(PEDERSEN_POINTS[3], PEDERSEN_POINTS[4]);
|
|
3894
|
-
function pedersenArg(arg) {
|
|
3895
|
-
let value;
|
|
3896
|
-
if (typeof arg === "bigint") {
|
|
3897
|
-
value = arg;
|
|
3898
|
-
} else if (typeof arg === "number") {
|
|
3899
|
-
if (!Number.isSafeInteger(arg))
|
|
3900
|
-
throw new Error(`Invalid pedersenArg: ${arg}`);
|
|
3901
|
-
value = BigInt(arg);
|
|
3902
|
-
} else {
|
|
3903
|
-
value = bytesToNumberBE(ensureBytes2(arg));
|
|
3904
|
-
}
|
|
3905
|
-
if (!(0n <= value && value < curve.CURVE.Fp.ORDER))
|
|
3906
|
-
throw new Error(`PedersenArg should be 0 <= value < CURVE.P: ${value}`);
|
|
3907
|
-
return value;
|
|
3908
|
-
}
|
|
3909
|
-
function pedersenSingle(point, value, constants) {
|
|
3910
|
-
let x = pedersenArg(value);
|
|
3911
|
-
for (let j = 0; j < 252; j++) {
|
|
3912
|
-
const pt = constants[j];
|
|
3913
|
-
if (pt.equals(point))
|
|
3914
|
-
throw new Error("Same point");
|
|
3915
|
-
if ((x & 1n) !== 0n)
|
|
3916
|
-
point = point.add(pt);
|
|
3917
|
-
x >>= 1n;
|
|
3918
|
-
}
|
|
3919
|
-
return point;
|
|
3920
|
-
}
|
|
3921
|
-
function pedersen(x, y) {
|
|
3922
|
-
let point = PEDERSEN_POINTS[0];
|
|
3923
|
-
point = pedersenSingle(point, x, PEDERSEN_POINTS1);
|
|
3924
|
-
point = pedersenSingle(point, y, PEDERSEN_POINTS2);
|
|
3925
|
-
return extractX(point.toRawBytes(true));
|
|
3926
|
-
}
|
|
3927
|
-
var MASK_250 = bitMask(250);
|
|
3928
|
-
var Fp251 = Field(BigInt("3618502788666131213697322783095070105623107215331596699973092056135872020481"));
|
|
3929
|
-
function poseidonRoundConstant(Fp, name, idx) {
|
|
3930
|
-
const val = Fp.fromBytes(sha256(utf8ToBytes(`${name}${idx}`)));
|
|
3931
|
-
return Fp.create(val);
|
|
3932
|
-
}
|
|
3933
|
-
var MDS_SMALL = [
|
|
3934
|
-
[3, 1, 1],
|
|
3935
|
-
[1, -1, 1],
|
|
3936
|
-
[1, 1, -2]
|
|
3937
|
-
].map((i) => i.map(BigInt));
|
|
3938
|
-
function poseidonBasic(opts, mds) {
|
|
3939
|
-
validateField(opts.Fp);
|
|
3940
|
-
if (!Number.isSafeInteger(opts.rate) || !Number.isSafeInteger(opts.capacity))
|
|
3941
|
-
throw new Error(`Wrong poseidon opts: ${opts}`);
|
|
3942
|
-
const m = opts.rate + opts.capacity;
|
|
3943
|
-
const rounds = opts.roundsFull + opts.roundsPartial;
|
|
3944
|
-
const roundConstants = [];
|
|
3945
|
-
for (let i = 0; i < rounds; i++) {
|
|
3946
|
-
const row = [];
|
|
3947
|
-
for (let j = 0; j < m; j++)
|
|
3948
|
-
row.push(poseidonRoundConstant(opts.Fp, "Hades", m * i + j));
|
|
3949
|
-
roundConstants.push(row);
|
|
3950
|
-
}
|
|
3951
|
-
const res = poseidon({
|
|
3952
|
-
...opts,
|
|
3953
|
-
t: m,
|
|
3954
|
-
sboxPower: 3,
|
|
3955
|
-
reversePartialPowIdx: true,
|
|
3956
|
-
mds,
|
|
3957
|
-
roundConstants
|
|
3958
|
-
});
|
|
3959
|
-
res.m = m;
|
|
3960
|
-
res.rate = opts.rate;
|
|
3961
|
-
res.capacity = opts.capacity;
|
|
3962
|
-
return res;
|
|
3963
|
-
}
|
|
3964
|
-
var poseidonSmall = poseidonBasic({ Fp: Fp251, rate: 2, capacity: 1, roundsFull: 8, roundsPartial: 83 }, MDS_SMALL);
|
|
3965
|
-
|
|
3966
|
-
// src/utils/oz-merkle.ts
|
|
1935
|
+
import * as starknet from "@scure/starknet";
|
|
3967
1936
|
function hash_leaf(leaf) {
|
|
3968
1937
|
if (leaf.data.length < 1) {
|
|
3969
1938
|
throw new Error("Invalid leaf data");
|
|
@@ -3976,7 +1945,7 @@ function hash_leaf(leaf) {
|
|
|
3976
1945
|
return `0x${num2.toHexString(value).replace(/^0x/, "").padStart(64, "0")}`;
|
|
3977
1946
|
}
|
|
3978
1947
|
function pedersen_hash(a, b) {
|
|
3979
|
-
return BigInt(pedersen(a, b).toString());
|
|
1948
|
+
return BigInt(starknet.pedersen(a, b).toString());
|
|
3980
1949
|
}
|
|
3981
1950
|
var StandardMerkleTree = class _StandardMerkleTree extends MerkleTreeImpl {
|
|
3982
1951
|
constructor(tree, values, leafEncoding) {
|
|
@@ -4133,6 +2102,7 @@ function getMainnetConfig(rpcUrl = "https://starknet-mainnet.public.blastapi.io"
|
|
|
4133
2102
|
provider: new RpcProvider2({
|
|
4134
2103
|
nodeUrl: rpcUrl,
|
|
4135
2104
|
blockIdentifier
|
|
2105
|
+
// specVersion
|
|
4136
2106
|
}),
|
|
4137
2107
|
stage: "production",
|
|
4138
2108
|
network: "mainnet" /* mainnet */
|
|
@@ -4162,7 +2132,7 @@ var getRiskExplaination = (riskType) => {
|
|
|
4162
2132
|
};
|
|
4163
2133
|
var getRiskColor = (risk) => {
|
|
4164
2134
|
const value = risk.value;
|
|
4165
|
-
if (value <=
|
|
2135
|
+
if (value <= 2) return "light_green_2";
|
|
4166
2136
|
if (value < 3) return "yellow";
|
|
4167
2137
|
return "red";
|
|
4168
2138
|
};
|
|
@@ -4190,8 +2160,18 @@ var VesuProtocol = {
|
|
|
4190
2160
|
name: "Vesu",
|
|
4191
2161
|
logo: "https://static-assets-8zct.onrender.com/integrations/vesu/logo.png"
|
|
4192
2162
|
};
|
|
2163
|
+
var EndurProtocol = {
|
|
2164
|
+
name: "Endur",
|
|
2165
|
+
logo: "http://endur.fi/logo.png"
|
|
2166
|
+
};
|
|
2167
|
+
var ExtendedProtocol = {
|
|
2168
|
+
name: "Extended",
|
|
2169
|
+
logo: "https://static-assets-8zct.onrender.com/integrations/extended/extended.svg"
|
|
2170
|
+
};
|
|
4193
2171
|
var Protocols = {
|
|
4194
|
-
VESU: VesuProtocol
|
|
2172
|
+
VESU: VesuProtocol,
|
|
2173
|
+
ENDUR: EndurProtocol,
|
|
2174
|
+
EXTENDED: ExtendedProtocol
|
|
4195
2175
|
};
|
|
4196
2176
|
|
|
4197
2177
|
// src/interfaces/initializable.ts
|
|
@@ -17888,7 +15868,7 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
|
|
|
17888
15868
|
quote.buyAmount.toString(),
|
|
17889
15869
|
tokenToBuyInfo.decimals
|
|
17890
15870
|
).multipliedBy(0.9999);
|
|
17891
|
-
const
|
|
15871
|
+
const output = await this.avnu.getSwapInfo(
|
|
17892
15872
|
quote,
|
|
17893
15873
|
this.address.address,
|
|
17894
15874
|
0,
|
|
@@ -17896,9 +15876,9 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
|
|
|
17896
15876
|
minAmountOut.toWei()
|
|
17897
15877
|
);
|
|
17898
15878
|
logger.verbose(
|
|
17899
|
-
`${_EkuboCLVault.name}: getSwapInfoToHandleUnused => swap info found: ${JSON.stringify(
|
|
15879
|
+
`${_EkuboCLVault.name}: getSwapInfoToHandleUnused => swap info found: ${JSON.stringify(output)}`
|
|
17900
15880
|
);
|
|
17901
|
-
return
|
|
15881
|
+
return output;
|
|
17902
15882
|
}
|
|
17903
15883
|
}
|
|
17904
15884
|
throw new Error("Failed to get swap info");
|
|
@@ -20453,10 +18433,10 @@ var SenseiStrategies = [
|
|
|
20453
18433
|
];
|
|
20454
18434
|
|
|
20455
18435
|
// src/strategies/universal-adapters/baseAdapter.ts
|
|
20456
|
-
import { hash
|
|
18436
|
+
import { hash, num as num6, shortString } from "starknet";
|
|
20457
18437
|
|
|
20458
18438
|
// src/strategies/universal-adapters/adapter-utils.ts
|
|
20459
|
-
var SIMPLE_SANITIZER = ContractAddr.from("
|
|
18439
|
+
var SIMPLE_SANITIZER = ContractAddr.from("0x3798dc4f83fdfad199e5236e3656cf2fb79bc50c00504d0dd41522e0f042072");
|
|
20460
18440
|
function toBigInt(value) {
|
|
20461
18441
|
if (typeof value === "string") {
|
|
20462
18442
|
return BigInt(value);
|
|
@@ -20479,7 +18459,7 @@ var BaseAdapter = class extends CacheClass {
|
|
|
20479
18459
|
// sanitizer address
|
|
20480
18460
|
target.toBigInt(),
|
|
20481
18461
|
// contract
|
|
20482
|
-
toBigInt(
|
|
18462
|
+
toBigInt(hash.getSelectorFromName(method)),
|
|
20483
18463
|
// method name
|
|
20484
18464
|
BigInt(packedArguments.length),
|
|
20485
18465
|
...packedArguments
|
|
@@ -20489,7 +18469,7 @@ var BaseAdapter = class extends CacheClass {
|
|
|
20489
18469
|
};
|
|
20490
18470
|
|
|
20491
18471
|
// src/strategies/universal-adapters/common-adapter.ts
|
|
20492
|
-
import { hash as
|
|
18472
|
+
import { hash as hash2, uint256 as uint2566 } from "starknet";
|
|
20493
18473
|
var CommonAdapter = class extends BaseAdapter {
|
|
20494
18474
|
constructor(config) {
|
|
20495
18475
|
super();
|
|
@@ -20519,7 +18499,7 @@ var CommonAdapter = class extends BaseAdapter {
|
|
|
20519
18499
|
sanitizer: SIMPLE_SANITIZER,
|
|
20520
18500
|
call: {
|
|
20521
18501
|
contractAddress: this.config.manager,
|
|
20522
|
-
selector:
|
|
18502
|
+
selector: hash2.getSelectorFromName("flash_loan"),
|
|
20523
18503
|
calldata: [
|
|
20524
18504
|
this.config.manager.toBigInt(),
|
|
20525
18505
|
// receiver
|
|
@@ -20558,7 +18538,7 @@ var CommonAdapter = class extends BaseAdapter {
|
|
|
20558
18538
|
sanitizer: SIMPLE_SANITIZER,
|
|
20559
18539
|
call: {
|
|
20560
18540
|
contractAddress: token,
|
|
20561
|
-
selector:
|
|
18541
|
+
selector: hash2.getSelectorFromName("approve"),
|
|
20562
18542
|
calldata: [
|
|
20563
18543
|
spender.toBigInt(),
|
|
20564
18544
|
// spender
|
|
@@ -20571,10 +18551,39 @@ var CommonAdapter = class extends BaseAdapter {
|
|
|
20571
18551
|
};
|
|
20572
18552
|
};
|
|
20573
18553
|
}
|
|
18554
|
+
getBringLiquidityAdapter(id) {
|
|
18555
|
+
return () => ({
|
|
18556
|
+
leaf: this.constructSimpleLeafData({
|
|
18557
|
+
id,
|
|
18558
|
+
target: this.config.vaultAddress,
|
|
18559
|
+
method: "bring_liquidity",
|
|
18560
|
+
packedArguments: []
|
|
18561
|
+
}),
|
|
18562
|
+
callConstructor: this.getBringLiquidityCall().bind(this)
|
|
18563
|
+
});
|
|
18564
|
+
}
|
|
18565
|
+
getBringLiquidityCall() {
|
|
18566
|
+
return (params) => {
|
|
18567
|
+
const uint256Amount = uint2566.bnToUint256(params.amount.toWei());
|
|
18568
|
+
return {
|
|
18569
|
+
sanitizer: SIMPLE_SANITIZER,
|
|
18570
|
+
call: {
|
|
18571
|
+
contractAddress: this.config.vaultAddress,
|
|
18572
|
+
selector: hash2.getSelectorFromName("bring_liquidity"),
|
|
18573
|
+
calldata: [
|
|
18574
|
+
toBigInt(uint256Amount.low.toString()),
|
|
18575
|
+
// amount low
|
|
18576
|
+
toBigInt(uint256Amount.high.toString())
|
|
18577
|
+
// amount high
|
|
18578
|
+
]
|
|
18579
|
+
}
|
|
18580
|
+
};
|
|
18581
|
+
};
|
|
18582
|
+
}
|
|
20574
18583
|
};
|
|
20575
18584
|
|
|
20576
18585
|
// src/strategies/universal-adapters/vesu-adapter.ts
|
|
20577
|
-
import { CairoCustomEnum as CairoCustomEnum2, Contract as Contract8, hash as
|
|
18586
|
+
import { CairoCustomEnum as CairoCustomEnum2, Contract as Contract8, hash as hash3, RpcProvider as RpcProvider4, uint256 as uint2567 } from "starknet";
|
|
20578
18587
|
|
|
20579
18588
|
// src/data/vesu-singleton.abi.json
|
|
20580
18589
|
var vesu_singleton_abi_default = [
|
|
@@ -22857,13 +20866,13 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
22857
20866
|
toBigInt(positionData.length),
|
|
22858
20867
|
...positionData
|
|
22859
20868
|
];
|
|
22860
|
-
const
|
|
20869
|
+
const output = this.constructSimpleLeafData({
|
|
22861
20870
|
id: this.config.id,
|
|
22862
20871
|
target: this.VESU_SINGLETON,
|
|
22863
20872
|
method: "modify_position",
|
|
22864
20873
|
packedArguments
|
|
22865
20874
|
});
|
|
22866
|
-
return { leaf:
|
|
20875
|
+
return { leaf: output, callConstructor: this.getModifyPositionCall.bind(this) };
|
|
22867
20876
|
};
|
|
22868
20877
|
this.getModifyPositionCall = (params) => {
|
|
22869
20878
|
const _collateral = {
|
|
@@ -22900,7 +20909,7 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
22900
20909
|
sanitizer: SIMPLE_SANITIZER,
|
|
22901
20910
|
call: {
|
|
22902
20911
|
contractAddress: this.VESU_SINGLETON,
|
|
22903
|
-
selector:
|
|
20912
|
+
selector: hash3.getSelectorFromName("modify_position"),
|
|
22904
20913
|
calldata: [
|
|
22905
20914
|
...call.calldata
|
|
22906
20915
|
]
|
|
@@ -22956,8 +20965,8 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
22956
20965
|
if (cacheData) {
|
|
22957
20966
|
return cacheData;
|
|
22958
20967
|
}
|
|
22959
|
-
const
|
|
22960
|
-
this.setCache(CACHE_KEY, Number(
|
|
20968
|
+
const output = await this.getVesuSingletonContract(config).call("ltv_config", [this.config.poolId.address, this.config.collateral.address.address, this.config.debt.address.address]);
|
|
20969
|
+
this.setCache(CACHE_KEY, Number(output.max_ltv) / 1e18, 3e5);
|
|
22961
20970
|
return this.getCache(CACHE_KEY);
|
|
22962
20971
|
}
|
|
22963
20972
|
async getPositions(config) {
|
|
@@ -22969,7 +20978,7 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
22969
20978
|
if (cacheData) {
|
|
22970
20979
|
return cacheData;
|
|
22971
20980
|
}
|
|
22972
|
-
const
|
|
20981
|
+
const output = await this.getVesuSingletonContract(config).call("position_unsafe", [
|
|
22973
20982
|
this.config.poolId.address,
|
|
22974
20983
|
this.config.collateral.address.address,
|
|
22975
20984
|
this.config.debt.address.address,
|
|
@@ -22977,8 +20986,8 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
22977
20986
|
]);
|
|
22978
20987
|
const token1Price = await this.pricer.getPrice(this.config.collateral.symbol);
|
|
22979
20988
|
const token2Price = await this.pricer.getPrice(this.config.debt.symbol);
|
|
22980
|
-
const collateralAmount = Web3Number.fromWei(
|
|
22981
|
-
const debtAmount = Web3Number.fromWei(
|
|
20989
|
+
const collateralAmount = Web3Number.fromWei(output["1"].toString(), this.config.collateral.decimals);
|
|
20990
|
+
const debtAmount = Web3Number.fromWei(output["2"].toString(), this.config.debt.decimals);
|
|
22982
20991
|
const value = [{
|
|
22983
20992
|
amount: collateralAmount,
|
|
22984
20993
|
token: this.config.collateral,
|
|
@@ -23002,14 +21011,14 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
23002
21011
|
if (cacheData) {
|
|
23003
21012
|
return cacheData;
|
|
23004
21013
|
}
|
|
23005
|
-
const
|
|
21014
|
+
const output = await this.getVesuSingletonContract(config).call("check_collateralization_unsafe", [
|
|
23006
21015
|
this.config.poolId.address,
|
|
23007
21016
|
this.config.collateral.address.address,
|
|
23008
21017
|
this.config.debt.address.address,
|
|
23009
21018
|
this.config.vaultAllocator.address
|
|
23010
21019
|
]);
|
|
23011
|
-
const collateralAmount = Web3Number.fromWei(
|
|
23012
|
-
const debtAmount = Web3Number.fromWei(
|
|
21020
|
+
const collateralAmount = Web3Number.fromWei(output["1"].toString(), 18);
|
|
21021
|
+
const debtAmount = Web3Number.fromWei(output["2"].toString(), 18);
|
|
23013
21022
|
const value = [{
|
|
23014
21023
|
token: this.config.collateral,
|
|
23015
21024
|
usdValue: collateralAmount.toNumber(),
|
|
@@ -23026,8 +21035,8 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
23026
21035
|
const collateralizationProm = this.getCollateralization(this.networkConfig);
|
|
23027
21036
|
const positionsProm = this.getPositions(this.networkConfig);
|
|
23028
21037
|
const ltvProm = this.getLTVConfig(this.networkConfig);
|
|
23029
|
-
const
|
|
23030
|
-
const [collateralization, positions, ltv] =
|
|
21038
|
+
const output = await Promise.all([collateralizationProm, positionsProm, ltvProm]);
|
|
21039
|
+
const [collateralization, positions, ltv] = output;
|
|
23031
21040
|
const collateralTokenAmount = positions[0].amount;
|
|
23032
21041
|
const collateralUSDAmount = collateralization[0].usdValue;
|
|
23033
21042
|
const collateralPrice = collateralUSDAmount / collateralTokenAmount.toNumber();
|
|
@@ -23088,7 +21097,7 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
23088
21097
|
}
|
|
23089
21098
|
};
|
|
23090
21099
|
|
|
23091
|
-
// src/strategies/universal-strategy.
|
|
21100
|
+
// src/strategies/universal-strategy.tsx
|
|
23092
21101
|
import { CallData, Contract as Contract9, num as num9, uint256 as uint2568 } from "starknet";
|
|
23093
21102
|
|
|
23094
21103
|
// src/data/universal-vault.abi.json
|
|
@@ -25294,7 +23303,13 @@ var vault_manager_abi_default = [
|
|
|
25294
23303
|
}
|
|
25295
23304
|
];
|
|
25296
23305
|
|
|
25297
|
-
// src/strategies/universal-strategy.
|
|
23306
|
+
// src/strategies/universal-strategy.tsx
|
|
23307
|
+
import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
23308
|
+
var AUMTypes = /* @__PURE__ */ ((AUMTypes2) => {
|
|
23309
|
+
AUMTypes2["FINALISED"] = "finalised";
|
|
23310
|
+
AUMTypes2["DEFISPRING"] = "defispring";
|
|
23311
|
+
return AUMTypes2;
|
|
23312
|
+
})(AUMTypes || {});
|
|
25298
23313
|
var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
25299
23314
|
constructor(config, pricer, metadata) {
|
|
25300
23315
|
super(config);
|
|
@@ -25379,6 +23394,19 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25379
23394
|
]);
|
|
25380
23395
|
return [call1, call2];
|
|
25381
23396
|
}
|
|
23397
|
+
async withdrawCall(amountInfo, receiver, owner) {
|
|
23398
|
+
assert(
|
|
23399
|
+
amountInfo.tokenInfo.address.eq(this.asset().address),
|
|
23400
|
+
"Withdraw token mismatch"
|
|
23401
|
+
);
|
|
23402
|
+
const shares = await this.contract.call("convert_to_shares", [uint2568.bnToUint256(amountInfo.amount.toWei())]);
|
|
23403
|
+
const call = this.contract.populate("request_redeem", [
|
|
23404
|
+
uint2568.bnToUint256(shares.toString()),
|
|
23405
|
+
receiver.address,
|
|
23406
|
+
owner.address
|
|
23407
|
+
]);
|
|
23408
|
+
return [call];
|
|
23409
|
+
}
|
|
25382
23410
|
/**
|
|
25383
23411
|
* Calculates the Total Value Locked (TVL) for a specific user.
|
|
25384
23412
|
* @param user - Address of the user
|
|
@@ -25425,14 +23453,24 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25425
23453
|
const collateral2APY = Number(collateralAsset2.supplyApy.value) / 1e18;
|
|
25426
23454
|
const debt2APY = Number(debtAsset2.borrowApr.value) / 1e18;
|
|
25427
23455
|
const positions = await this.getVaultPositions();
|
|
23456
|
+
logger.verbose(`${this.metadata.name}::netAPY: positions: ${JSON.stringify(positions)}`);
|
|
23457
|
+
if (positions.every((p) => p.amount.isZero())) {
|
|
23458
|
+
return { net: 0, splits: [{
|
|
23459
|
+
apy: 0,
|
|
23460
|
+
id: "base"
|
|
23461
|
+
}, {
|
|
23462
|
+
apy: 0,
|
|
23463
|
+
id: "defispring"
|
|
23464
|
+
}] };
|
|
23465
|
+
}
|
|
25428
23466
|
const weights = positions.map((p, index) => p.usdValue * (index % 2 == 0 ? 1 : -1));
|
|
25429
23467
|
const baseAPYs = [collateral1APY, debt1APY, collateral2APY, debt2APY];
|
|
25430
23468
|
assert(positions.length == baseAPYs.length, "Positions and APYs length mismatch");
|
|
25431
23469
|
const rewardAPYs = [Number(collateralAsset1.defiSpringSupplyApr.value) / 1e18, 0, Number(collateralAsset2.defiSpringSupplyApr.value) / 1e18, 0];
|
|
25432
23470
|
const baseAPY = this.computeAPY(baseAPYs, weights);
|
|
25433
23471
|
const rewardAPY = this.computeAPY(rewardAPYs, weights);
|
|
25434
|
-
const apys = [...baseAPYs, ...rewardAPYs];
|
|
25435
23472
|
const netAPY = baseAPY + rewardAPY;
|
|
23473
|
+
logger.verbose(`${this.metadata.name}::netAPY: net: ${netAPY}, baseAPY: ${baseAPY}, rewardAPY: ${rewardAPY}`);
|
|
25436
23474
|
return { net: netAPY, splits: [{
|
|
25437
23475
|
apy: baseAPY,
|
|
25438
23476
|
id: "base"
|
|
@@ -25467,6 +23505,16 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25467
23505
|
usdValue
|
|
25468
23506
|
};
|
|
25469
23507
|
}
|
|
23508
|
+
async getUnusedBalance() {
|
|
23509
|
+
const balance = await new ERC20(this.config).balanceOf(this.asset().address, this.metadata.additionalInfo.vaultAllocator, this.asset().decimals);
|
|
23510
|
+
const price = await this.pricer.getPrice(this.metadata.depositTokens[0].symbol);
|
|
23511
|
+
const usdValue = Number(balance.toFixed(6)) * price.price;
|
|
23512
|
+
return {
|
|
23513
|
+
tokenInfo: this.asset(),
|
|
23514
|
+
amount: balance,
|
|
23515
|
+
usdValue
|
|
23516
|
+
};
|
|
23517
|
+
}
|
|
25470
23518
|
async getAUM() {
|
|
25471
23519
|
const currentAUM = await this.contract.call("aum", []);
|
|
25472
23520
|
const lastReportTime = await this.contract.call("last_report_timestamp", []);
|
|
@@ -25474,16 +23522,33 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25474
23522
|
const [vesuAdapter1, vesuAdapter2] = this.getVesuAdapters();
|
|
25475
23523
|
const leg1AUM = await vesuAdapter1.getPositions(this.config);
|
|
25476
23524
|
const leg2AUM = await vesuAdapter2.getPositions(this.config);
|
|
25477
|
-
const balance = await
|
|
25478
|
-
logger.verbose(`${this.getTag()} unused balance: ${balance}`);
|
|
25479
|
-
const
|
|
23525
|
+
const balance = await this.getUnusedBalance();
|
|
23526
|
+
logger.verbose(`${this.getTag()} unused balance: ${balance.amount.toNumber()}`);
|
|
23527
|
+
const vesuAum = leg1AUM[0].amount.plus(leg2AUM[0].usdValue / token1Price.price).minus(leg1AUM[1].usdValue / token1Price.price).minus(leg2AUM[1].amount);
|
|
23528
|
+
logger.verbose(`${this.getTag()} Vesu AUM: leg1: ${leg1AUM[0].amount.toNumber()}, ${leg1AUM[1].amount.toNumber()}, leg2: ${leg2AUM[0].amount.toNumber()}, ${leg2AUM[1].amount.toNumber()}`);
|
|
23529
|
+
const zeroAmt = Web3Number.fromWei("0", this.asset().decimals);
|
|
23530
|
+
const net = {
|
|
23531
|
+
tokenInfo: this.asset(),
|
|
23532
|
+
amount: zeroAmt,
|
|
23533
|
+
usdValue: 0
|
|
23534
|
+
};
|
|
23535
|
+
const prevAum = Web3Number.fromWei(currentAUM.toString(), this.asset().decimals);
|
|
23536
|
+
if (vesuAum.isZero()) {
|
|
23537
|
+
return { net, splits: [{
|
|
23538
|
+
aum: zeroAmt,
|
|
23539
|
+
id: "finalised" /* FINALISED */
|
|
23540
|
+
}, {
|
|
23541
|
+
aum: zeroAmt,
|
|
23542
|
+
id: "defispring" /* DEFISPRING */
|
|
23543
|
+
}], prevAum };
|
|
23544
|
+
}
|
|
23545
|
+
const aumToken = vesuAum.plus(balance.amount);
|
|
25480
23546
|
logger.verbose(`${this.getTag()} Actual AUM: ${aumToken}`);
|
|
25481
23547
|
const netAPY = await this.netAPY();
|
|
25482
23548
|
const defispringAPY = netAPY.splits.find((s) => s.id === "defispring")?.apy || 0;
|
|
25483
23549
|
if (!defispringAPY) throw new Error("DefiSpring APY not found");
|
|
25484
23550
|
const timeDiff = Math.round(Date.now() / 1e3) - Number(lastReportTime);
|
|
25485
23551
|
const growthRate = timeDiff * defispringAPY / (365 * 24 * 60 * 60);
|
|
25486
|
-
const prevAum = Web3Number.fromWei(currentAUM.toString(), this.asset().decimals);
|
|
25487
23552
|
const rewardAssets = prevAum.multipliedBy(growthRate);
|
|
25488
23553
|
logger.verbose(`${this.getTag()} DefiSpring AUM time difference: ${timeDiff}`);
|
|
25489
23554
|
logger.verbose(`${this.getTag()} Current AUM: ${currentAUM}`);
|
|
@@ -25491,16 +23556,13 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25491
23556
|
logger.verbose(`${this.getTag()} rewards AUM: ${rewardAssets}`);
|
|
25492
23557
|
const newAUM = aumToken.plus(rewardAssets);
|
|
25493
23558
|
logger.verbose(`${this.getTag()} New AUM: ${newAUM}`);
|
|
25494
|
-
|
|
25495
|
-
|
|
25496
|
-
amount: newAUM,
|
|
25497
|
-
usdValue: newAUM.multipliedBy(token1Price.price).toNumber()
|
|
25498
|
-
};
|
|
23559
|
+
net.amount = newAUM;
|
|
23560
|
+
net.usdValue = newAUM.multipliedBy(token1Price.price).toNumber();
|
|
25499
23561
|
const splits = [{
|
|
25500
|
-
id: "finalised"
|
|
23562
|
+
id: "finalised" /* FINALISED */,
|
|
25501
23563
|
aum: aumToken
|
|
25502
23564
|
}, {
|
|
25503
|
-
id: "defispring"
|
|
23565
|
+
id: "defispring" /* DEFISPRING */,
|
|
25504
23566
|
aum: rewardAssets
|
|
25505
23567
|
}];
|
|
25506
23568
|
return { net, splits, prevAum };
|
|
@@ -25551,19 +23613,19 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25551
23613
|
debtAmount: params.debtAmount,
|
|
25552
23614
|
isBorrow: params.isDeposit
|
|
25553
23615
|
}));
|
|
25554
|
-
const
|
|
23616
|
+
const output = [{
|
|
25555
23617
|
proofs: manage5Info.proofs,
|
|
25556
23618
|
manageCall: manageCall5,
|
|
25557
23619
|
step: STEP2_ID
|
|
25558
23620
|
}];
|
|
25559
23621
|
if (approveAmount.gt(0)) {
|
|
25560
|
-
|
|
23622
|
+
output.unshift({
|
|
25561
23623
|
proofs: manage4Info.proofs,
|
|
25562
23624
|
manageCall: manageCall4,
|
|
25563
23625
|
step: STEP1_ID
|
|
25564
23626
|
});
|
|
25565
23627
|
}
|
|
25566
|
-
return
|
|
23628
|
+
return output;
|
|
25567
23629
|
}
|
|
25568
23630
|
getTag() {
|
|
25569
23631
|
return `${_UniversalStrategy.name}:${this.metadata.name}`;
|
|
@@ -25680,18 +23742,23 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25680
23742
|
logger.verbose(`${this.getTag()}:: leg1DepositAmount: ${params.leg1DepositAmount.toString()} ${vesuAdapter1.config.collateral.symbol}`);
|
|
25681
23743
|
logger.verbose(`${this.getTag()}:: borrow1Amount: ${borrow1Amount.toString()} ${vesuAdapter1.config.debt.symbol}`);
|
|
25682
23744
|
logger.verbose(`${this.getTag()}:: borrow2Amount: ${borrow2Amount.toString()} ${vesuAdapter2.config.debt.symbol}`);
|
|
25683
|
-
|
|
23745
|
+
let callSet1 = this.getVesuModifyPositionCalls({
|
|
25684
23746
|
isLeg1: true,
|
|
25685
23747
|
isDeposit: params.isDeposit,
|
|
25686
23748
|
depositAmount: params.leg1DepositAmount.plus(borrow2Amount),
|
|
25687
23749
|
debtAmount: borrow1Amount
|
|
25688
23750
|
});
|
|
25689
|
-
|
|
23751
|
+
let callSet2 = this.getVesuModifyPositionCalls({
|
|
25690
23752
|
isLeg1: false,
|
|
25691
23753
|
isDeposit: params.isDeposit,
|
|
25692
23754
|
depositAmount: borrow1Amount,
|
|
25693
23755
|
debtAmount: borrow2Amount
|
|
25694
23756
|
});
|
|
23757
|
+
if (!params.isDeposit) {
|
|
23758
|
+
let temp = callSet2;
|
|
23759
|
+
callSet2 = [...callSet1];
|
|
23760
|
+
callSet1 = [...temp];
|
|
23761
|
+
}
|
|
25695
23762
|
const allActions = [...callSet1.map((i) => i.manageCall), ...callSet2.map((i) => i.manageCall)];
|
|
25696
23763
|
const flashloanCalldata = CallData.compile([
|
|
25697
23764
|
[...callSet1.map((i) => i.proofs), ...callSet2.map((i) => i.proofs)],
|
|
@@ -25709,6 +23776,18 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25709
23776
|
const manageCall = this.getManageCall(["flash_loan_init" /* FLASH_LOAN */], [manageCall1]);
|
|
25710
23777
|
return manageCall;
|
|
25711
23778
|
}
|
|
23779
|
+
async getBringLiquidityCall(params) {
|
|
23780
|
+
const manage1Info = this.getProofs("approve_bring_liquidity" /* APPROVE_BRING_LIQUIDITY */);
|
|
23781
|
+
const manageCall1 = manage1Info.callConstructor({
|
|
23782
|
+
amount: params.amount
|
|
23783
|
+
});
|
|
23784
|
+
const manage2Info = this.getProofs("bring_liquidity" /* BRING_LIQUIDITY */);
|
|
23785
|
+
const manageCall2 = manage2Info.callConstructor({
|
|
23786
|
+
amount: params.amount
|
|
23787
|
+
});
|
|
23788
|
+
const manageCall = this.getManageCall(["approve_bring_liquidity" /* APPROVE_BRING_LIQUIDITY */, "bring_liquidity" /* BRING_LIQUIDITY */], [manageCall1, manageCall2]);
|
|
23789
|
+
return manageCall;
|
|
23790
|
+
}
|
|
25712
23791
|
async getRebalanceCall(params) {
|
|
25713
23792
|
let callSet1 = this.getVesuModifyPositionCalls({
|
|
25714
23793
|
isLeg1: true,
|
|
@@ -25743,6 +23822,8 @@ var UNIVERSAL_MANAGE_IDS = /* @__PURE__ */ ((UNIVERSAL_MANAGE_IDS2) => {
|
|
|
25743
23822
|
UNIVERSAL_MANAGE_IDS2["VESU_LEG2"] = "vesu_leg2";
|
|
25744
23823
|
UNIVERSAL_MANAGE_IDS2["APPROVE_TOKEN1"] = "approve_token1";
|
|
25745
23824
|
UNIVERSAL_MANAGE_IDS2["APPROVE_TOKEN2"] = "approve_token2";
|
|
23825
|
+
UNIVERSAL_MANAGE_IDS2["APPROVE_BRING_LIQUIDITY"] = "approve_bring_liquidity";
|
|
23826
|
+
UNIVERSAL_MANAGE_IDS2["BRING_LIQUIDITY"] = "bring_liquidity";
|
|
25746
23827
|
return UNIVERSAL_MANAGE_IDS2;
|
|
25747
23828
|
})(UNIVERSAL_MANAGE_IDS || {});
|
|
25748
23829
|
var UNIVERSAL_ADAPTERS = /* @__PURE__ */ ((UNIVERSAL_ADAPTERS2) => {
|
|
@@ -25757,7 +23838,9 @@ function getLooperSettings(token1Symbol, token2Symbol, vaultSettings, pool1, poo
|
|
|
25757
23838
|
const commonAdapter = new CommonAdapter({
|
|
25758
23839
|
manager: vaultSettings.manager,
|
|
25759
23840
|
asset: USDCToken.address,
|
|
25760
|
-
id: "flash_loan_init" /* FLASH_LOAN
|
|
23841
|
+
id: "flash_loan_init" /* FLASH_LOAN */,
|
|
23842
|
+
vaultAddress: vaultSettings.vaultAddress,
|
|
23843
|
+
vaultAllocator: vaultSettings.vaultAllocator
|
|
25761
23844
|
});
|
|
25762
23845
|
const vesuAdapterUSDCETH = new VesuAdapter({
|
|
25763
23846
|
poolId: pool1,
|
|
@@ -25788,13 +23871,17 @@ function getLooperSettings(token1Symbol, token2Symbol, vaultSettings, pool1, poo
|
|
|
25788
23871
|
vaultSettings.leafAdapters.push(vesuAdapterETHUSDC.getModifyPosition.bind(vesuAdapterETHUSDC));
|
|
25789
23872
|
vaultSettings.leafAdapters.push(commonAdapter.getApproveAdapter(USDCToken.address, vesuAdapterUSDCETH.VESU_SINGLETON, "approve_token1" /* APPROVE_TOKEN1 */).bind(commonAdapter));
|
|
25790
23873
|
vaultSettings.leafAdapters.push(commonAdapter.getApproveAdapter(ETHToken.address, vesuAdapterETHUSDC.VESU_SINGLETON, "approve_token2" /* APPROVE_TOKEN2 */).bind(commonAdapter));
|
|
23874
|
+
vaultSettings.leafAdapters.push(commonAdapter.getApproveAdapter(USDCToken.address, vaultSettings.vaultAddress, "approve_bring_liquidity" /* APPROVE_BRING_LIQUIDITY */).bind(commonAdapter));
|
|
23875
|
+
vaultSettings.leafAdapters.push(commonAdapter.getBringLiquidityAdapter("bring_liquidity" /* BRING_LIQUIDITY */).bind(commonAdapter));
|
|
25791
23876
|
return vaultSettings;
|
|
25792
23877
|
}
|
|
25793
23878
|
var _riskFactor4 = [
|
|
25794
23879
|
{ type: "Smart Contract Risk" /* SMART_CONTRACT_RISK */, value: 0.5, weight: 25, reason: "Audited by Zellic" },
|
|
25795
|
-
{ type: "Liquidation Risk" /* LIQUIDATION_RISK */, value: 1, weight: 50, reason: "Liquidation risk is mitigated
|
|
23880
|
+
{ type: "Liquidation Risk" /* LIQUIDATION_RISK */, value: 1.5, weight: 50, reason: "Liquidation risk is mitigated by stable price feed on Starknet" },
|
|
23881
|
+
{ type: "Technical Risk" /* TECHNICAL_RISK */, value: 1, weight: 50, reason: "Technical failures like risk monitoring failures" }
|
|
25796
23882
|
];
|
|
25797
23883
|
var usdcVaultSettings = {
|
|
23884
|
+
vaultAddress: ContractAddr.from("0x7e6498cf6a1bfc7e6fc89f1831865e2dacb9756def4ec4b031a9138788a3b5e"),
|
|
25798
23885
|
manager: ContractAddr.from("0xf41a2b1f498a7f9629db0b8519259e66e964260a23d20003f3e42bb1997a07"),
|
|
25799
23886
|
vaultAllocator: ContractAddr.from("0x228cca1005d3f2b55cbaba27cb291dacf1b9a92d1d6b1638195fbd3d0c1e3ba"),
|
|
25800
23887
|
redeemRequestNFT: ContractAddr.from("0x906d03590010868cbf7590ad47043959d7af8e782089a605d9b22567b64fda"),
|
|
@@ -25805,6 +23892,7 @@ var usdcVaultSettings = {
|
|
|
25805
23892
|
minHealthFactor: 1.25
|
|
25806
23893
|
};
|
|
25807
23894
|
var wbtcVaultSettings = {
|
|
23895
|
+
vaultAddress: ContractAddr.from("0x5a4c1651b913aa2ea7afd9024911603152a19058624c3e425405370d62bf80c"),
|
|
25808
23896
|
manager: ContractAddr.from("0xef8a664ffcfe46a6af550766d27c28937bf1b77fb4ab54d8553e92bca5ba34"),
|
|
25809
23897
|
vaultAllocator: ContractAddr.from("0x1e01c25f0d9494570226ad28a7fa856c0640505e809c366a9fab4903320e735"),
|
|
25810
23898
|
redeemRequestNFT: ContractAddr.from("0x4fec59a12f8424281c1e65a80b5de51b4e754625c60cddfcd00d46941ec37b2"),
|
|
@@ -25814,10 +23902,176 @@ var wbtcVaultSettings = {
|
|
|
25814
23902
|
targetHealthFactor: 1.3,
|
|
25815
23903
|
minHealthFactor: 1.25
|
|
25816
23904
|
};
|
|
23905
|
+
var ethVaultSettings = {
|
|
23906
|
+
vaultAddress: ContractAddr.from("0x446c22d4d3f5cb52b4950ba832ba1df99464c6673a37c092b1d9622650dbd8"),
|
|
23907
|
+
manager: ContractAddr.from("0x494888b37206616bd09d759dcda61e5118470b9aa7f58fb84f21c778a7b8f97"),
|
|
23908
|
+
vaultAllocator: ContractAddr.from("0x4acc0ad6bea58cb578d60ff7c31f06f44369a7a9a7bbfffe4701f143e427bd"),
|
|
23909
|
+
redeemRequestNFT: ContractAddr.from("0x2e6cd71e5060a254d4db00655e420db7bf89da7755bb0d5f922e2f00c76ac49"),
|
|
23910
|
+
aumOracle: ContractAddr.from("0x4b747f2e75c057bed9aa2ce46fbdc2159dc684c15bd32d4f95983a6ecf39a05"),
|
|
23911
|
+
leafAdapters: [],
|
|
23912
|
+
adapters: [],
|
|
23913
|
+
targetHealthFactor: 1.3,
|
|
23914
|
+
minHealthFactor: 1.25
|
|
23915
|
+
};
|
|
23916
|
+
var strkVaultSettings = {
|
|
23917
|
+
vaultAddress: ContractAddr.from("0x55d012f57e58c96e0a5c7ebbe55853989d01e6538b15a95e7178aca4af05c21"),
|
|
23918
|
+
manager: ContractAddr.from("0xcc6a5153ca56293405506eb20826a379d982cd738008ef7e808454d318fb81"),
|
|
23919
|
+
vaultAllocator: ContractAddr.from("0xf29d2f82e896c0ed74c9eff220af34ac148e8b99846d1ace9fbb02c9191d01"),
|
|
23920
|
+
redeemRequestNFT: ContractAddr.from("0x46902423bd632c428376b84fcee9cac5dbe016214e93a8103bcbde6e1de656b"),
|
|
23921
|
+
aumOracle: ContractAddr.from("0x6d7dbfad4bb51715da211468389a623da00c0625f8f6efbea822ee5ac5231f4"),
|
|
23922
|
+
leafAdapters: [],
|
|
23923
|
+
adapters: [],
|
|
23924
|
+
targetHealthFactor: 1.3,
|
|
23925
|
+
minHealthFactor: 1.25
|
|
23926
|
+
};
|
|
23927
|
+
var usdtVaultSettings = {
|
|
23928
|
+
vaultAddress: ContractAddr.from("0x1c4933d1880c6778585e597154eaca7b428579d72f3aae425ad2e4d26c6bb3"),
|
|
23929
|
+
manager: ContractAddr.from("0x39bb9843503799b552b7ed84b31c06e4ff10c0537edcddfbf01fe944b864029"),
|
|
23930
|
+
vaultAllocator: ContractAddr.from("0x56437d18c43727ac971f6c7086031cad7d9d6ccb340f4f3785a74cc791c931a"),
|
|
23931
|
+
redeemRequestNFT: ContractAddr.from("0x5af0c2a657eaa8e23ed78e855dac0c51e4f69e2cf91a18c472041a1f75bb41f"),
|
|
23932
|
+
aumOracle: ContractAddr.from("0x7018f8040c8066a4ab929e6760ae52dd43b6a3a289172f514750a61fcc565cc"),
|
|
23933
|
+
leafAdapters: [],
|
|
23934
|
+
adapters: [],
|
|
23935
|
+
targetHealthFactor: 1.3,
|
|
23936
|
+
minHealthFactor: 1.25
|
|
23937
|
+
};
|
|
23938
|
+
function MetaVaultDescription() {
|
|
23939
|
+
const logos = {
|
|
23940
|
+
vesu: Protocols.VESU.logo,
|
|
23941
|
+
endur: Protocols.ENDUR.logo,
|
|
23942
|
+
extended: Protocols.EXTENDED.logo,
|
|
23943
|
+
ekubo: "https://dummyimage.com/64x64/ffffff/000000&text=K"
|
|
23944
|
+
};
|
|
23945
|
+
const sources = [
|
|
23946
|
+
{ key: "vesu", name: "Vesu", status: "Live", description: "Integrated liquidity venue used for automated routing to capture the best available yield." },
|
|
23947
|
+
{ key: "endur", name: "Endur", status: "Coming soon", description: "Planned integration to tap into STRK staking\u2013backed yields via our LST pipeline." },
|
|
23948
|
+
{ key: "extended", name: "Extended", status: "Coming soon", description: "Expanding coverage to additional money markets and vaults across the ecosystem." }
|
|
23949
|
+
// { key: "ekubo", name: "Ekubo", status: "Coming soon", description: "Concentrated liquidity strategies targeted for optimized fee APR harvesting." },
|
|
23950
|
+
];
|
|
23951
|
+
const containerStyle = {
|
|
23952
|
+
maxWidth: "800px",
|
|
23953
|
+
margin: "0 auto",
|
|
23954
|
+
backgroundColor: "#111",
|
|
23955
|
+
color: "#eee",
|
|
23956
|
+
fontFamily: "Arial, sans-serif",
|
|
23957
|
+
borderRadius: "12px"
|
|
23958
|
+
};
|
|
23959
|
+
const cardStyle = {
|
|
23960
|
+
border: "1px solid #333",
|
|
23961
|
+
borderRadius: "10px",
|
|
23962
|
+
padding: "12px",
|
|
23963
|
+
marginBottom: "12px",
|
|
23964
|
+
backgroundColor: "#1a1a1a"
|
|
23965
|
+
};
|
|
23966
|
+
const logoStyle = {
|
|
23967
|
+
width: "24px",
|
|
23968
|
+
height: "24px",
|
|
23969
|
+
borderRadius: "8px",
|
|
23970
|
+
border: "1px solid #444",
|
|
23971
|
+
backgroundColor: "#222"
|
|
23972
|
+
};
|
|
23973
|
+
return /* @__PURE__ */ jsxs3("div", { style: containerStyle, children: [
|
|
23974
|
+
/* @__PURE__ */ jsx4("h1", { style: { fontSize: "18px", marginBottom: "10px" }, children: "Meta Vault \u2014 Automated Yield Router" }),
|
|
23975
|
+
/* @__PURE__ */ jsx4("p", { style: { fontSize: "14px", lineHeight: "1.5", marginBottom: "16px" }, children: "This Evergreen vault is a tokenized Meta Vault, auto-compounding strategy that continuously allocates your deposited asset to the best available yield source in the ecosystem. Depositors receive vault shares that represent a proportional claim on the underlying assets and accrued yield. Allocation shifts are handled programmatically based on on-chain signals and risk filters, minimizing idle capital and maximizing net APY." }),
|
|
23976
|
+
/* @__PURE__ */ jsx4("div", { style: { backgroundColor: "#222", padding: "10px", borderRadius: "8px", marginBottom: "20px", border: "1px solid #444" }, children: /* @__PURE__ */ jsxs3("p", { style: { fontSize: "13px", color: "#ccc" }, children: [
|
|
23977
|
+
/* @__PURE__ */ jsx4("strong", { children: "Withdrawals:" }),
|
|
23978
|
+
" Requests can take up to ",
|
|
23979
|
+
/* @__PURE__ */ jsx4("strong", { children: "1-2 hours" }),
|
|
23980
|
+
" to process as the vault unwinds and settles routing."
|
|
23981
|
+
] }) }),
|
|
23982
|
+
/* @__PURE__ */ jsx4("h2", { style: { fontSize: "18px", marginBottom: "10px" }, children: "Supported Yield Sources" }),
|
|
23983
|
+
sources.map((s) => /* @__PURE__ */ jsx4("div", { style: cardStyle, children: /* @__PURE__ */ jsxs3("div", { style: { display: "flex", gap: "10px", alignItems: "flex-start" }, children: [
|
|
23984
|
+
/* @__PURE__ */ jsx4("img", { src: logos[s.key], alt: `${s.name} logo`, style: logoStyle }),
|
|
23985
|
+
/* @__PURE__ */ jsx4("div", { style: { flex: 1 }, children: /* @__PURE__ */ jsxs3("div", { style: { display: "flex", justifyContent: "space-between", alignItems: "center" }, children: [
|
|
23986
|
+
/* @__PURE__ */ jsx4("h3", { style: { fontSize: "15px", margin: 0 }, children: s.name }),
|
|
23987
|
+
/* @__PURE__ */ jsx4(StatusBadge, { status: s.status })
|
|
23988
|
+
] }) })
|
|
23989
|
+
] }) }, s.key))
|
|
23990
|
+
] });
|
|
23991
|
+
}
|
|
23992
|
+
function StatusBadge({ status }) {
|
|
23993
|
+
const isSoon = String(status).toLowerCase() !== "live";
|
|
23994
|
+
const badgeStyle = {
|
|
23995
|
+
fontSize: "12px",
|
|
23996
|
+
padding: "2px 6px",
|
|
23997
|
+
borderRadius: "12px",
|
|
23998
|
+
border: "1px solid",
|
|
23999
|
+
color: isSoon ? "rgb(196 196 195)" : "#6aff7d",
|
|
24000
|
+
borderColor: isSoon ? "#484848" : "#6aff7d",
|
|
24001
|
+
backgroundColor: isSoon ? "#424242" : "#002b1a"
|
|
24002
|
+
};
|
|
24003
|
+
return /* @__PURE__ */ jsx4("span", { style: badgeStyle, children: status });
|
|
24004
|
+
}
|
|
24005
|
+
function getDescription(tokenSymbol) {
|
|
24006
|
+
return MetaVaultDescription();
|
|
24007
|
+
}
|
|
24008
|
+
function getFAQs() {
|
|
24009
|
+
return [
|
|
24010
|
+
{
|
|
24011
|
+
question: "What is the Meta Vault?",
|
|
24012
|
+
answer: "The Meta Vault is a tokenized strategy that automatically allocates your deposited assets to the best available yield source in the ecosystem. It optimizes returns while minimizing idle capital."
|
|
24013
|
+
},
|
|
24014
|
+
{
|
|
24015
|
+
question: "How does yield allocation work?",
|
|
24016
|
+
answer: "The vault continuously monitors supported protocols and routes liquidity to the source offering the highest net yield after accounting for fees, slippage, and gas costs. Reallocations are performed automatically."
|
|
24017
|
+
},
|
|
24018
|
+
{
|
|
24019
|
+
question: "Which yield sources are supported?",
|
|
24020
|
+
answer: /* @__PURE__ */ jsxs3("span", { children: [
|
|
24021
|
+
"Currently, ",
|
|
24022
|
+
/* @__PURE__ */ jsx4("strong", { children: "Vesu" }),
|
|
24023
|
+
" is live. Future integrations include",
|
|
24024
|
+
" ",
|
|
24025
|
+
/* @__PURE__ */ jsx4("strong", { children: "Endur" }),
|
|
24026
|
+
", ",
|
|
24027
|
+
/* @__PURE__ */ jsx4("strong", { children: "Extended" }),
|
|
24028
|
+
", and",
|
|
24029
|
+
" ",
|
|
24030
|
+
/* @__PURE__ */ jsx4("strong", { children: "Ekubo" }),
|
|
24031
|
+
" (all coming soon)."
|
|
24032
|
+
] })
|
|
24033
|
+
},
|
|
24034
|
+
{
|
|
24035
|
+
question: "What do I receive when I deposit?",
|
|
24036
|
+
answer: "Depositors receive vault tokens representing their proportional share of the vault. These tokens entitle holders to both the principal and accrued yield."
|
|
24037
|
+
},
|
|
24038
|
+
{
|
|
24039
|
+
question: "How long do withdrawals take?",
|
|
24040
|
+
answer: "Withdrawals may take up to 1-2 hours to process, as the vault unwinds and settles liquidity routing across integrated protocols."
|
|
24041
|
+
},
|
|
24042
|
+
{
|
|
24043
|
+
question: "Is the Meta Vault non-custodial?",
|
|
24044
|
+
answer: "Yes. The Meta Vault operates entirely on-chain. Users always maintain control of their vault tokens, and the strategy is fully transparent."
|
|
24045
|
+
},
|
|
24046
|
+
{
|
|
24047
|
+
question: "How is risk managed?",
|
|
24048
|
+
answer: "Integrations are supported with active risk monitoring like liquidation risk. Only vetted protocols are included to balance yield with safety. However, usual Defi risks like smart contract risk, extreme volatile market risk, etc. are still present."
|
|
24049
|
+
},
|
|
24050
|
+
{
|
|
24051
|
+
question: "Are there any fees?",
|
|
24052
|
+
answer: "Troves charges a performance of 10% on the yield generated. The APY shown is net of this fee. This fee is only applied to the profits earned, ensuring that users retain their initial capital."
|
|
24053
|
+
}
|
|
24054
|
+
];
|
|
24055
|
+
}
|
|
24056
|
+
function getContractDetails(settings) {
|
|
24057
|
+
return [
|
|
24058
|
+
{ address: settings.vaultAddress, name: "Vault" },
|
|
24059
|
+
{ address: settings.manager, name: "Vault Manager" },
|
|
24060
|
+
{ address: settings.vaultAllocator, name: "Vault Allocator" },
|
|
24061
|
+
{ address: settings.redeemRequestNFT, name: "Redeem Request NFT" },
|
|
24062
|
+
{ address: settings.aumOracle, name: "AUM Oracle" }
|
|
24063
|
+
];
|
|
24064
|
+
}
|
|
24065
|
+
var investmentSteps = [
|
|
24066
|
+
"Deposit funds into the vault",
|
|
24067
|
+
"Vault manager allocates funds to optimal yield sources",
|
|
24068
|
+
"Vault manager reports asset under management (AUM) regularly to the vault",
|
|
24069
|
+
"Request withdrawal and vault manager processes it in 1-2 hours"
|
|
24070
|
+
];
|
|
25817
24071
|
var UniversalStrategies = [
|
|
25818
24072
|
{
|
|
25819
24073
|
name: "USDC Evergreen",
|
|
25820
|
-
description: "
|
|
24074
|
+
description: getDescription("USDC"),
|
|
25821
24075
|
address: ContractAddr.from("0x7e6498cf6a1bfc7e6fc89f1831865e2dacb9756def4ec4b031a9138788a3b5e"),
|
|
25822
24076
|
launchBlock: 0,
|
|
25823
24077
|
type: "ERC4626",
|
|
@@ -25830,13 +24084,13 @@ var UniversalStrategies = [
|
|
|
25830
24084
|
},
|
|
25831
24085
|
protocols: [Protocols.VESU],
|
|
25832
24086
|
maxTVL: Web3Number.fromWei(0, 6),
|
|
25833
|
-
contractDetails:
|
|
25834
|
-
faqs:
|
|
25835
|
-
investmentSteps
|
|
24087
|
+
contractDetails: getContractDetails(usdcVaultSettings),
|
|
24088
|
+
faqs: getFAQs(),
|
|
24089
|
+
investmentSteps
|
|
25836
24090
|
},
|
|
25837
24091
|
{
|
|
25838
24092
|
name: "WBTC Evergreen",
|
|
25839
|
-
description: "
|
|
24093
|
+
description: getDescription("WBTC"),
|
|
25840
24094
|
address: ContractAddr.from("0x5a4c1651b913aa2ea7afd9024911603152a19058624c3e425405370d62bf80c"),
|
|
25841
24095
|
launchBlock: 0,
|
|
25842
24096
|
type: "ERC4626",
|
|
@@ -25849,12 +24103,70 @@ var UniversalStrategies = [
|
|
|
25849
24103
|
},
|
|
25850
24104
|
protocols: [Protocols.VESU],
|
|
25851
24105
|
maxTVL: Web3Number.fromWei(0, 8),
|
|
25852
|
-
contractDetails:
|
|
25853
|
-
faqs:
|
|
25854
|
-
investmentSteps
|
|
24106
|
+
contractDetails: getContractDetails(wbtcVaultSettings),
|
|
24107
|
+
faqs: getFAQs(),
|
|
24108
|
+
investmentSteps
|
|
24109
|
+
},
|
|
24110
|
+
{
|
|
24111
|
+
name: "ETH Evergreen",
|
|
24112
|
+
description: getDescription("ETH"),
|
|
24113
|
+
address: ContractAddr.from("0x446c22d4d3f5cb52b4950ba832ba1df99464c6673a37c092b1d9622650dbd8"),
|
|
24114
|
+
launchBlock: 0,
|
|
24115
|
+
type: "ERC4626",
|
|
24116
|
+
depositTokens: [Global.getDefaultTokens().find((token) => token.symbol === "ETH")],
|
|
24117
|
+
additionalInfo: getLooperSettings("ETH", "WBTC", ethVaultSettings, VesuPools.Genesis, VesuPools.Genesis),
|
|
24118
|
+
risk: {
|
|
24119
|
+
riskFactor: _riskFactor4,
|
|
24120
|
+
netRisk: _riskFactor4.reduce((acc, curr) => acc + curr.value * curr.weight, 0) / _riskFactor4.reduce((acc, curr) => acc + curr.weight, 0),
|
|
24121
|
+
notARisks: getNoRiskTags(_riskFactor4)
|
|
24122
|
+
},
|
|
24123
|
+
protocols: [Protocols.VESU],
|
|
24124
|
+
maxTVL: Web3Number.fromWei(0, 18),
|
|
24125
|
+
contractDetails: getContractDetails(ethVaultSettings),
|
|
24126
|
+
faqs: getFAQs(),
|
|
24127
|
+
investmentSteps
|
|
24128
|
+
},
|
|
24129
|
+
{
|
|
24130
|
+
name: "STRK Evergreen",
|
|
24131
|
+
description: getDescription("STRK"),
|
|
24132
|
+
address: ContractAddr.from("0x55d012f57e58c96e0a5c7ebbe55853989d01e6538b15a95e7178aca4af05c21"),
|
|
24133
|
+
launchBlock: 0,
|
|
24134
|
+
type: "ERC4626",
|
|
24135
|
+
depositTokens: [Global.getDefaultTokens().find((token) => token.symbol === "STRK")],
|
|
24136
|
+
additionalInfo: getLooperSettings("STRK", "ETH", strkVaultSettings, VesuPools.Genesis, VesuPools.Genesis),
|
|
24137
|
+
risk: {
|
|
24138
|
+
riskFactor: _riskFactor4,
|
|
24139
|
+
netRisk: _riskFactor4.reduce((acc, curr) => acc + curr.value * curr.weight, 0) / _riskFactor4.reduce((acc, curr) => acc + curr.weight, 0),
|
|
24140
|
+
notARisks: getNoRiskTags(_riskFactor4)
|
|
24141
|
+
},
|
|
24142
|
+
protocols: [Protocols.VESU],
|
|
24143
|
+
maxTVL: Web3Number.fromWei(0, 18),
|
|
24144
|
+
contractDetails: getContractDetails(strkVaultSettings),
|
|
24145
|
+
faqs: getFAQs(),
|
|
24146
|
+
investmentSteps
|
|
24147
|
+
},
|
|
24148
|
+
{
|
|
24149
|
+
name: "USDT Evergreen",
|
|
24150
|
+
description: getDescription("USDT"),
|
|
24151
|
+
address: ContractAddr.from("0x1c4933d1880c6778585e597154eaca7b428579d72f3aae425ad2e4d26c6bb3"),
|
|
24152
|
+
launchBlock: 0,
|
|
24153
|
+
type: "ERC4626",
|
|
24154
|
+
depositTokens: [Global.getDefaultTokens().find((token) => token.symbol === "USDT")],
|
|
24155
|
+
additionalInfo: getLooperSettings("USDT", "ETH", usdtVaultSettings, VesuPools.Genesis, VesuPools.Genesis),
|
|
24156
|
+
risk: {
|
|
24157
|
+
riskFactor: _riskFactor4,
|
|
24158
|
+
netRisk: _riskFactor4.reduce((acc, curr) => acc + curr.value * curr.weight, 0) / _riskFactor4.reduce((acc, curr) => acc + curr.weight, 0),
|
|
24159
|
+
notARisks: getNoRiskTags(_riskFactor4)
|
|
24160
|
+
},
|
|
24161
|
+
protocols: [Protocols.VESU],
|
|
24162
|
+
maxTVL: Web3Number.fromWei(0, 6),
|
|
24163
|
+
contractDetails: getContractDetails(usdtVaultSettings),
|
|
24164
|
+
faqs: getFAQs(),
|
|
24165
|
+
investmentSteps
|
|
25855
24166
|
}
|
|
25856
24167
|
];
|
|
25857
24168
|
export {
|
|
24169
|
+
AUMTypes,
|
|
25858
24170
|
AutoCompounderSTRK,
|
|
25859
24171
|
AvnuWrapper,
|
|
25860
24172
|
BaseAdapter,
|
|
@@ -25896,26 +24208,3 @@ export {
|
|
|
25896
24208
|
getRiskExplaination,
|
|
25897
24209
|
highlightTextWithLinks
|
|
25898
24210
|
};
|
|
25899
|
-
/*! Bundled license information:
|
|
25900
|
-
|
|
25901
|
-
@noble/hashes/esm/utils.js:
|
|
25902
|
-
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
25903
|
-
|
|
25904
|
-
@noble/curves/esm/abstract/utils.js:
|
|
25905
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
25906
|
-
|
|
25907
|
-
@noble/curves/esm/abstract/modular.js:
|
|
25908
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
25909
|
-
|
|
25910
|
-
@noble/curves/esm/abstract/poseidon.js:
|
|
25911
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
25912
|
-
|
|
25913
|
-
@noble/curves/esm/abstract/curve.js:
|
|
25914
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
25915
|
-
|
|
25916
|
-
@noble/curves/esm/abstract/weierstrass.js:
|
|
25917
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
25918
|
-
|
|
25919
|
-
@noble/curves/esm/_shortw_utils.js:
|
|
25920
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
25921
|
-
*/
|