@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.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
|
|
|
@@ -1987,2033 +1981,7 @@ import { processMultiProof, processProof } from "@ericnordelo/strk-merkle-tree/d
|
|
|
1987
1981
|
import { standardLeafHash } from "@ericnordelo/strk-merkle-tree/dist/hashes";
|
|
1988
1982
|
import { MerkleTreeImpl } from "@ericnordelo/strk-merkle-tree/dist/merkletree";
|
|
1989
1983
|
import { num as num2 } from "starknet";
|
|
1990
|
-
|
|
1991
|
-
// ../node_modules/@noble/hashes/esm/_assert.js
|
|
1992
|
-
function number(n) {
|
|
1993
|
-
if (!Number.isSafeInteger(n) || n < 0)
|
|
1994
|
-
throw new Error(`Wrong positive integer: ${n}`);
|
|
1995
|
-
}
|
|
1996
|
-
function bytes(b, ...lengths) {
|
|
1997
|
-
if (!(b instanceof Uint8Array))
|
|
1998
|
-
throw new Error("Expected Uint8Array");
|
|
1999
|
-
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
2000
|
-
throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
|
|
2001
|
-
}
|
|
2002
|
-
function hash(hash6) {
|
|
2003
|
-
if (typeof hash6 !== "function" || typeof hash6.create !== "function")
|
|
2004
|
-
throw new Error("Hash should be wrapped by utils.wrapConstructor");
|
|
2005
|
-
number(hash6.outputLen);
|
|
2006
|
-
number(hash6.blockLen);
|
|
2007
|
-
}
|
|
2008
|
-
function exists(instance, checkFinished = true) {
|
|
2009
|
-
if (instance.destroyed)
|
|
2010
|
-
throw new Error("Hash instance has been destroyed");
|
|
2011
|
-
if (checkFinished && instance.finished)
|
|
2012
|
-
throw new Error("Hash#digest() has already been called");
|
|
2013
|
-
}
|
|
2014
|
-
function output(out, instance) {
|
|
2015
|
-
bytes(out);
|
|
2016
|
-
const min = instance.outputLen;
|
|
2017
|
-
if (out.length < min) {
|
|
2018
|
-
throw new Error(`digestInto() expects output buffer of length at least ${min}`);
|
|
2019
|
-
}
|
|
2020
|
-
}
|
|
2021
|
-
|
|
2022
|
-
// ../node_modules/@noble/hashes/esm/cryptoNode.js
|
|
2023
|
-
import * as nc from "crypto";
|
|
2024
|
-
var crypto = nc && typeof nc === "object" && "webcrypto" in nc ? nc.webcrypto : void 0;
|
|
2025
|
-
|
|
2026
|
-
// ../node_modules/@noble/hashes/esm/utils.js
|
|
2027
|
-
var u8a = (a) => a instanceof Uint8Array;
|
|
2028
|
-
var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
2029
|
-
var rotr = (word, shift) => word << 32 - shift | word >>> shift;
|
|
2030
|
-
var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
|
|
2031
|
-
if (!isLE)
|
|
2032
|
-
throw new Error("Non little-endian hardware is not supported");
|
|
2033
|
-
function utf8ToBytes(str) {
|
|
2034
|
-
if (typeof str !== "string")
|
|
2035
|
-
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
|
2036
|
-
return new Uint8Array(new TextEncoder().encode(str));
|
|
2037
|
-
}
|
|
2038
|
-
function toBytes(data) {
|
|
2039
|
-
if (typeof data === "string")
|
|
2040
|
-
data = utf8ToBytes(data);
|
|
2041
|
-
if (!u8a(data))
|
|
2042
|
-
throw new Error(`expected Uint8Array, got ${typeof data}`);
|
|
2043
|
-
return data;
|
|
2044
|
-
}
|
|
2045
|
-
function concatBytes(...arrays) {
|
|
2046
|
-
const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0));
|
|
2047
|
-
let pad = 0;
|
|
2048
|
-
arrays.forEach((a) => {
|
|
2049
|
-
if (!u8a(a))
|
|
2050
|
-
throw new Error("Uint8Array expected");
|
|
2051
|
-
r.set(a, pad);
|
|
2052
|
-
pad += a.length;
|
|
2053
|
-
});
|
|
2054
|
-
return r;
|
|
2055
|
-
}
|
|
2056
|
-
var Hash = class {
|
|
2057
|
-
// Safe version that clones internal state
|
|
2058
|
-
clone() {
|
|
2059
|
-
return this._cloneInto();
|
|
2060
|
-
}
|
|
2061
|
-
};
|
|
2062
|
-
var toStr = {}.toString;
|
|
2063
|
-
function wrapConstructor(hashCons) {
|
|
2064
|
-
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
2065
|
-
const tmp = hashCons();
|
|
2066
|
-
hashC.outputLen = tmp.outputLen;
|
|
2067
|
-
hashC.blockLen = tmp.blockLen;
|
|
2068
|
-
hashC.create = () => hashCons();
|
|
2069
|
-
return hashC;
|
|
2070
|
-
}
|
|
2071
|
-
function randomBytes(bytesLength = 32) {
|
|
2072
|
-
if (crypto && typeof crypto.getRandomValues === "function") {
|
|
2073
|
-
return crypto.getRandomValues(new Uint8Array(bytesLength));
|
|
2074
|
-
}
|
|
2075
|
-
throw new Error("crypto.getRandomValues must be defined");
|
|
2076
|
-
}
|
|
2077
|
-
|
|
2078
|
-
// ../node_modules/@noble/hashes/esm/_sha2.js
|
|
2079
|
-
function setBigUint64(view, byteOffset, value, isLE2) {
|
|
2080
|
-
if (typeof view.setBigUint64 === "function")
|
|
2081
|
-
return view.setBigUint64(byteOffset, value, isLE2);
|
|
2082
|
-
const _32n = BigInt(32);
|
|
2083
|
-
const _u32_max = BigInt(4294967295);
|
|
2084
|
-
const wh = Number(value >> _32n & _u32_max);
|
|
2085
|
-
const wl = Number(value & _u32_max);
|
|
2086
|
-
const h = isLE2 ? 4 : 0;
|
|
2087
|
-
const l = isLE2 ? 0 : 4;
|
|
2088
|
-
view.setUint32(byteOffset + h, wh, isLE2);
|
|
2089
|
-
view.setUint32(byteOffset + l, wl, isLE2);
|
|
2090
|
-
}
|
|
2091
|
-
var SHA2 = class extends Hash {
|
|
2092
|
-
constructor(blockLen, outputLen, padOffset, isLE2) {
|
|
2093
|
-
super();
|
|
2094
|
-
this.blockLen = blockLen;
|
|
2095
|
-
this.outputLen = outputLen;
|
|
2096
|
-
this.padOffset = padOffset;
|
|
2097
|
-
this.isLE = isLE2;
|
|
2098
|
-
this.finished = false;
|
|
2099
|
-
this.length = 0;
|
|
2100
|
-
this.pos = 0;
|
|
2101
|
-
this.destroyed = false;
|
|
2102
|
-
this.buffer = new Uint8Array(blockLen);
|
|
2103
|
-
this.view = createView(this.buffer);
|
|
2104
|
-
}
|
|
2105
|
-
update(data) {
|
|
2106
|
-
exists(this);
|
|
2107
|
-
const { view, buffer, blockLen } = this;
|
|
2108
|
-
data = toBytes(data);
|
|
2109
|
-
const len = data.length;
|
|
2110
|
-
for (let pos = 0; pos < len; ) {
|
|
2111
|
-
const take = Math.min(blockLen - this.pos, len - pos);
|
|
2112
|
-
if (take === blockLen) {
|
|
2113
|
-
const dataView = createView(data);
|
|
2114
|
-
for (; blockLen <= len - pos; pos += blockLen)
|
|
2115
|
-
this.process(dataView, pos);
|
|
2116
|
-
continue;
|
|
2117
|
-
}
|
|
2118
|
-
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
2119
|
-
this.pos += take;
|
|
2120
|
-
pos += take;
|
|
2121
|
-
if (this.pos === blockLen) {
|
|
2122
|
-
this.process(view, 0);
|
|
2123
|
-
this.pos = 0;
|
|
2124
|
-
}
|
|
2125
|
-
}
|
|
2126
|
-
this.length += data.length;
|
|
2127
|
-
this.roundClean();
|
|
2128
|
-
return this;
|
|
2129
|
-
}
|
|
2130
|
-
digestInto(out) {
|
|
2131
|
-
exists(this);
|
|
2132
|
-
output(out, this);
|
|
2133
|
-
this.finished = true;
|
|
2134
|
-
const { buffer, view, blockLen, isLE: isLE2 } = this;
|
|
2135
|
-
let { pos } = this;
|
|
2136
|
-
buffer[pos++] = 128;
|
|
2137
|
-
this.buffer.subarray(pos).fill(0);
|
|
2138
|
-
if (this.padOffset > blockLen - pos) {
|
|
2139
|
-
this.process(view, 0);
|
|
2140
|
-
pos = 0;
|
|
2141
|
-
}
|
|
2142
|
-
for (let i = pos; i < blockLen; i++)
|
|
2143
|
-
buffer[i] = 0;
|
|
2144
|
-
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE2);
|
|
2145
|
-
this.process(view, 0);
|
|
2146
|
-
const oview = createView(out);
|
|
2147
|
-
const len = this.outputLen;
|
|
2148
|
-
if (len % 4)
|
|
2149
|
-
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
2150
|
-
const outLen = len / 4;
|
|
2151
|
-
const state = this.get();
|
|
2152
|
-
if (outLen > state.length)
|
|
2153
|
-
throw new Error("_sha2: outputLen bigger than state");
|
|
2154
|
-
for (let i = 0; i < outLen; i++)
|
|
2155
|
-
oview.setUint32(4 * i, state[i], isLE2);
|
|
2156
|
-
}
|
|
2157
|
-
digest() {
|
|
2158
|
-
const { buffer, outputLen } = this;
|
|
2159
|
-
this.digestInto(buffer);
|
|
2160
|
-
const res = buffer.slice(0, outputLen);
|
|
2161
|
-
this.destroy();
|
|
2162
|
-
return res;
|
|
2163
|
-
}
|
|
2164
|
-
_cloneInto(to) {
|
|
2165
|
-
to || (to = new this.constructor());
|
|
2166
|
-
to.set(...this.get());
|
|
2167
|
-
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
2168
|
-
to.length = length;
|
|
2169
|
-
to.pos = pos;
|
|
2170
|
-
to.finished = finished;
|
|
2171
|
-
to.destroyed = destroyed;
|
|
2172
|
-
if (length % blockLen)
|
|
2173
|
-
to.buffer.set(buffer);
|
|
2174
|
-
return to;
|
|
2175
|
-
}
|
|
2176
|
-
};
|
|
2177
|
-
|
|
2178
|
-
// ../node_modules/@noble/hashes/esm/sha256.js
|
|
2179
|
-
var Chi = (a, b, c) => a & b ^ ~a & c;
|
|
2180
|
-
var Maj = (a, b, c) => a & b ^ a & c ^ b & c;
|
|
2181
|
-
var SHA256_K = /* @__PURE__ */ new Uint32Array([
|
|
2182
|
-
1116352408,
|
|
2183
|
-
1899447441,
|
|
2184
|
-
3049323471,
|
|
2185
|
-
3921009573,
|
|
2186
|
-
961987163,
|
|
2187
|
-
1508970993,
|
|
2188
|
-
2453635748,
|
|
2189
|
-
2870763221,
|
|
2190
|
-
3624381080,
|
|
2191
|
-
310598401,
|
|
2192
|
-
607225278,
|
|
2193
|
-
1426881987,
|
|
2194
|
-
1925078388,
|
|
2195
|
-
2162078206,
|
|
2196
|
-
2614888103,
|
|
2197
|
-
3248222580,
|
|
2198
|
-
3835390401,
|
|
2199
|
-
4022224774,
|
|
2200
|
-
264347078,
|
|
2201
|
-
604807628,
|
|
2202
|
-
770255983,
|
|
2203
|
-
1249150122,
|
|
2204
|
-
1555081692,
|
|
2205
|
-
1996064986,
|
|
2206
|
-
2554220882,
|
|
2207
|
-
2821834349,
|
|
2208
|
-
2952996808,
|
|
2209
|
-
3210313671,
|
|
2210
|
-
3336571891,
|
|
2211
|
-
3584528711,
|
|
2212
|
-
113926993,
|
|
2213
|
-
338241895,
|
|
2214
|
-
666307205,
|
|
2215
|
-
773529912,
|
|
2216
|
-
1294757372,
|
|
2217
|
-
1396182291,
|
|
2218
|
-
1695183700,
|
|
2219
|
-
1986661051,
|
|
2220
|
-
2177026350,
|
|
2221
|
-
2456956037,
|
|
2222
|
-
2730485921,
|
|
2223
|
-
2820302411,
|
|
2224
|
-
3259730800,
|
|
2225
|
-
3345764771,
|
|
2226
|
-
3516065817,
|
|
2227
|
-
3600352804,
|
|
2228
|
-
4094571909,
|
|
2229
|
-
275423344,
|
|
2230
|
-
430227734,
|
|
2231
|
-
506948616,
|
|
2232
|
-
659060556,
|
|
2233
|
-
883997877,
|
|
2234
|
-
958139571,
|
|
2235
|
-
1322822218,
|
|
2236
|
-
1537002063,
|
|
2237
|
-
1747873779,
|
|
2238
|
-
1955562222,
|
|
2239
|
-
2024104815,
|
|
2240
|
-
2227730452,
|
|
2241
|
-
2361852424,
|
|
2242
|
-
2428436474,
|
|
2243
|
-
2756734187,
|
|
2244
|
-
3204031479,
|
|
2245
|
-
3329325298
|
|
2246
|
-
]);
|
|
2247
|
-
var IV = /* @__PURE__ */ new Uint32Array([
|
|
2248
|
-
1779033703,
|
|
2249
|
-
3144134277,
|
|
2250
|
-
1013904242,
|
|
2251
|
-
2773480762,
|
|
2252
|
-
1359893119,
|
|
2253
|
-
2600822924,
|
|
2254
|
-
528734635,
|
|
2255
|
-
1541459225
|
|
2256
|
-
]);
|
|
2257
|
-
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
2258
|
-
var SHA256 = class extends SHA2 {
|
|
2259
|
-
constructor() {
|
|
2260
|
-
super(64, 32, 8, false);
|
|
2261
|
-
this.A = IV[0] | 0;
|
|
2262
|
-
this.B = IV[1] | 0;
|
|
2263
|
-
this.C = IV[2] | 0;
|
|
2264
|
-
this.D = IV[3] | 0;
|
|
2265
|
-
this.E = IV[4] | 0;
|
|
2266
|
-
this.F = IV[5] | 0;
|
|
2267
|
-
this.G = IV[6] | 0;
|
|
2268
|
-
this.H = IV[7] | 0;
|
|
2269
|
-
}
|
|
2270
|
-
get() {
|
|
2271
|
-
const { A, B, C, D, E, F, G, H } = this;
|
|
2272
|
-
return [A, B, C, D, E, F, G, H];
|
|
2273
|
-
}
|
|
2274
|
-
// prettier-ignore
|
|
2275
|
-
set(A, B, C, D, E, F, G, H) {
|
|
2276
|
-
this.A = A | 0;
|
|
2277
|
-
this.B = B | 0;
|
|
2278
|
-
this.C = C | 0;
|
|
2279
|
-
this.D = D | 0;
|
|
2280
|
-
this.E = E | 0;
|
|
2281
|
-
this.F = F | 0;
|
|
2282
|
-
this.G = G | 0;
|
|
2283
|
-
this.H = H | 0;
|
|
2284
|
-
}
|
|
2285
|
-
process(view, offset) {
|
|
2286
|
-
for (let i = 0; i < 16; i++, offset += 4)
|
|
2287
|
-
SHA256_W[i] = view.getUint32(offset, false);
|
|
2288
|
-
for (let i = 16; i < 64; i++) {
|
|
2289
|
-
const W15 = SHA256_W[i - 15];
|
|
2290
|
-
const W2 = SHA256_W[i - 2];
|
|
2291
|
-
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
2292
|
-
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
|
|
2293
|
-
SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
2294
|
-
}
|
|
2295
|
-
let { A, B, C, D, E, F, G, H } = this;
|
|
2296
|
-
for (let i = 0; i < 64; i++) {
|
|
2297
|
-
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
2298
|
-
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
2299
|
-
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
2300
|
-
const T2 = sigma0 + Maj(A, B, C) | 0;
|
|
2301
|
-
H = G;
|
|
2302
|
-
G = F;
|
|
2303
|
-
F = E;
|
|
2304
|
-
E = D + T1 | 0;
|
|
2305
|
-
D = C;
|
|
2306
|
-
C = B;
|
|
2307
|
-
B = A;
|
|
2308
|
-
A = T1 + T2 | 0;
|
|
2309
|
-
}
|
|
2310
|
-
A = A + this.A | 0;
|
|
2311
|
-
B = B + this.B | 0;
|
|
2312
|
-
C = C + this.C | 0;
|
|
2313
|
-
D = D + this.D | 0;
|
|
2314
|
-
E = E + this.E | 0;
|
|
2315
|
-
F = F + this.F | 0;
|
|
2316
|
-
G = G + this.G | 0;
|
|
2317
|
-
H = H + this.H | 0;
|
|
2318
|
-
this.set(A, B, C, D, E, F, G, H);
|
|
2319
|
-
}
|
|
2320
|
-
roundClean() {
|
|
2321
|
-
SHA256_W.fill(0);
|
|
2322
|
-
}
|
|
2323
|
-
destroy() {
|
|
2324
|
-
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
2325
|
-
this.buffer.fill(0);
|
|
2326
|
-
}
|
|
2327
|
-
};
|
|
2328
|
-
var sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
|
|
2329
|
-
|
|
2330
|
-
// ../node_modules/@noble/curves/esm/abstract/utils.js
|
|
2331
|
-
var utils_exports = {};
|
|
2332
|
-
__export(utils_exports, {
|
|
2333
|
-
bitGet: () => bitGet,
|
|
2334
|
-
bitLen: () => bitLen,
|
|
2335
|
-
bitMask: () => bitMask,
|
|
2336
|
-
bitSet: () => bitSet,
|
|
2337
|
-
bytesToHex: () => bytesToHex,
|
|
2338
|
-
bytesToNumberBE: () => bytesToNumberBE,
|
|
2339
|
-
bytesToNumberLE: () => bytesToNumberLE,
|
|
2340
|
-
concatBytes: () => concatBytes2,
|
|
2341
|
-
createHmacDrbg: () => createHmacDrbg,
|
|
2342
|
-
ensureBytes: () => ensureBytes,
|
|
2343
|
-
equalBytes: () => equalBytes,
|
|
2344
|
-
hexToBytes: () => hexToBytes,
|
|
2345
|
-
hexToNumber: () => hexToNumber,
|
|
2346
|
-
numberToBytesBE: () => numberToBytesBE,
|
|
2347
|
-
numberToBytesLE: () => numberToBytesLE,
|
|
2348
|
-
numberToHexUnpadded: () => numberToHexUnpadded,
|
|
2349
|
-
numberToVarBytesBE: () => numberToVarBytesBE,
|
|
2350
|
-
utf8ToBytes: () => utf8ToBytes2,
|
|
2351
|
-
validateObject: () => validateObject
|
|
2352
|
-
});
|
|
2353
|
-
var _0n = BigInt(0);
|
|
2354
|
-
var _1n = BigInt(1);
|
|
2355
|
-
var _2n = BigInt(2);
|
|
2356
|
-
var u8a2 = (a) => a instanceof Uint8Array;
|
|
2357
|
-
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
2358
|
-
function bytesToHex(bytes2) {
|
|
2359
|
-
if (!u8a2(bytes2))
|
|
2360
|
-
throw new Error("Uint8Array expected");
|
|
2361
|
-
let hex = "";
|
|
2362
|
-
for (let i = 0; i < bytes2.length; i++) {
|
|
2363
|
-
hex += hexes[bytes2[i]];
|
|
2364
|
-
}
|
|
2365
|
-
return hex;
|
|
2366
|
-
}
|
|
2367
|
-
function numberToHexUnpadded(num11) {
|
|
2368
|
-
const hex = num11.toString(16);
|
|
2369
|
-
return hex.length & 1 ? `0${hex}` : hex;
|
|
2370
|
-
}
|
|
2371
|
-
function hexToNumber(hex) {
|
|
2372
|
-
if (typeof hex !== "string")
|
|
2373
|
-
throw new Error("hex string expected, got " + typeof hex);
|
|
2374
|
-
return BigInt(hex === "" ? "0" : `0x${hex}`);
|
|
2375
|
-
}
|
|
2376
|
-
function hexToBytes(hex) {
|
|
2377
|
-
if (typeof hex !== "string")
|
|
2378
|
-
throw new Error("hex string expected, got " + typeof hex);
|
|
2379
|
-
const len = hex.length;
|
|
2380
|
-
if (len % 2)
|
|
2381
|
-
throw new Error("padded hex string expected, got unpadded hex of length " + len);
|
|
2382
|
-
const array = new Uint8Array(len / 2);
|
|
2383
|
-
for (let i = 0; i < array.length; i++) {
|
|
2384
|
-
const j = i * 2;
|
|
2385
|
-
const hexByte = hex.slice(j, j + 2);
|
|
2386
|
-
const byte = Number.parseInt(hexByte, 16);
|
|
2387
|
-
if (Number.isNaN(byte) || byte < 0)
|
|
2388
|
-
throw new Error("Invalid byte sequence");
|
|
2389
|
-
array[i] = byte;
|
|
2390
|
-
}
|
|
2391
|
-
return array;
|
|
2392
|
-
}
|
|
2393
|
-
function bytesToNumberBE(bytes2) {
|
|
2394
|
-
return hexToNumber(bytesToHex(bytes2));
|
|
2395
|
-
}
|
|
2396
|
-
function bytesToNumberLE(bytes2) {
|
|
2397
|
-
if (!u8a2(bytes2))
|
|
2398
|
-
throw new Error("Uint8Array expected");
|
|
2399
|
-
return hexToNumber(bytesToHex(Uint8Array.from(bytes2).reverse()));
|
|
2400
|
-
}
|
|
2401
|
-
function numberToBytesBE(n, len) {
|
|
2402
|
-
return hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
2403
|
-
}
|
|
2404
|
-
function numberToBytesLE(n, len) {
|
|
2405
|
-
return numberToBytesBE(n, len).reverse();
|
|
2406
|
-
}
|
|
2407
|
-
function numberToVarBytesBE(n) {
|
|
2408
|
-
return hexToBytes(numberToHexUnpadded(n));
|
|
2409
|
-
}
|
|
2410
|
-
function ensureBytes(title, hex, expectedLength) {
|
|
2411
|
-
let res;
|
|
2412
|
-
if (typeof hex === "string") {
|
|
2413
|
-
try {
|
|
2414
|
-
res = hexToBytes(hex);
|
|
2415
|
-
} catch (e) {
|
|
2416
|
-
throw new Error(`${title} must be valid hex string, got "${hex}". Cause: ${e}`);
|
|
2417
|
-
}
|
|
2418
|
-
} else if (u8a2(hex)) {
|
|
2419
|
-
res = Uint8Array.from(hex);
|
|
2420
|
-
} else {
|
|
2421
|
-
throw new Error(`${title} must be hex string or Uint8Array`);
|
|
2422
|
-
}
|
|
2423
|
-
const len = res.length;
|
|
2424
|
-
if (typeof expectedLength === "number" && len !== expectedLength)
|
|
2425
|
-
throw new Error(`${title} expected ${expectedLength} bytes, got ${len}`);
|
|
2426
|
-
return res;
|
|
2427
|
-
}
|
|
2428
|
-
function concatBytes2(...arrays) {
|
|
2429
|
-
const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0));
|
|
2430
|
-
let pad = 0;
|
|
2431
|
-
arrays.forEach((a) => {
|
|
2432
|
-
if (!u8a2(a))
|
|
2433
|
-
throw new Error("Uint8Array expected");
|
|
2434
|
-
r.set(a, pad);
|
|
2435
|
-
pad += a.length;
|
|
2436
|
-
});
|
|
2437
|
-
return r;
|
|
2438
|
-
}
|
|
2439
|
-
function equalBytes(b1, b2) {
|
|
2440
|
-
if (b1.length !== b2.length)
|
|
2441
|
-
return false;
|
|
2442
|
-
for (let i = 0; i < b1.length; i++)
|
|
2443
|
-
if (b1[i] !== b2[i])
|
|
2444
|
-
return false;
|
|
2445
|
-
return true;
|
|
2446
|
-
}
|
|
2447
|
-
function utf8ToBytes2(str) {
|
|
2448
|
-
if (typeof str !== "string")
|
|
2449
|
-
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
|
2450
|
-
return new Uint8Array(new TextEncoder().encode(str));
|
|
2451
|
-
}
|
|
2452
|
-
function bitLen(n) {
|
|
2453
|
-
let len;
|
|
2454
|
-
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
2455
|
-
;
|
|
2456
|
-
return len;
|
|
2457
|
-
}
|
|
2458
|
-
function bitGet(n, pos) {
|
|
2459
|
-
return n >> BigInt(pos) & _1n;
|
|
2460
|
-
}
|
|
2461
|
-
var bitSet = (n, pos, value) => {
|
|
2462
|
-
return n | (value ? _1n : _0n) << BigInt(pos);
|
|
2463
|
-
};
|
|
2464
|
-
var bitMask = (n) => (_2n << BigInt(n - 1)) - _1n;
|
|
2465
|
-
var u8n = (data) => new Uint8Array(data);
|
|
2466
|
-
var u8fr = (arr) => Uint8Array.from(arr);
|
|
2467
|
-
function createHmacDrbg(hashLen, qByteLen, hmacFn) {
|
|
2468
|
-
if (typeof hashLen !== "number" || hashLen < 2)
|
|
2469
|
-
throw new Error("hashLen must be a number");
|
|
2470
|
-
if (typeof qByteLen !== "number" || qByteLen < 2)
|
|
2471
|
-
throw new Error("qByteLen must be a number");
|
|
2472
|
-
if (typeof hmacFn !== "function")
|
|
2473
|
-
throw new Error("hmacFn must be a function");
|
|
2474
|
-
let v = u8n(hashLen);
|
|
2475
|
-
let k = u8n(hashLen);
|
|
2476
|
-
let i = 0;
|
|
2477
|
-
const reset = () => {
|
|
2478
|
-
v.fill(1);
|
|
2479
|
-
k.fill(0);
|
|
2480
|
-
i = 0;
|
|
2481
|
-
};
|
|
2482
|
-
const h = (...b) => hmacFn(k, v, ...b);
|
|
2483
|
-
const reseed = (seed = u8n()) => {
|
|
2484
|
-
k = h(u8fr([0]), seed);
|
|
2485
|
-
v = h();
|
|
2486
|
-
if (seed.length === 0)
|
|
2487
|
-
return;
|
|
2488
|
-
k = h(u8fr([1]), seed);
|
|
2489
|
-
v = h();
|
|
2490
|
-
};
|
|
2491
|
-
const gen = () => {
|
|
2492
|
-
if (i++ >= 1e3)
|
|
2493
|
-
throw new Error("drbg: tried 1000 values");
|
|
2494
|
-
let len = 0;
|
|
2495
|
-
const out = [];
|
|
2496
|
-
while (len < qByteLen) {
|
|
2497
|
-
v = h();
|
|
2498
|
-
const sl = v.slice();
|
|
2499
|
-
out.push(sl);
|
|
2500
|
-
len += v.length;
|
|
2501
|
-
}
|
|
2502
|
-
return concatBytes2(...out);
|
|
2503
|
-
};
|
|
2504
|
-
const genUntil = (seed, pred) => {
|
|
2505
|
-
reset();
|
|
2506
|
-
reseed(seed);
|
|
2507
|
-
let res = void 0;
|
|
2508
|
-
while (!(res = pred(gen())))
|
|
2509
|
-
reseed();
|
|
2510
|
-
reset();
|
|
2511
|
-
return res;
|
|
2512
|
-
};
|
|
2513
|
-
return genUntil;
|
|
2514
|
-
}
|
|
2515
|
-
var validatorFns = {
|
|
2516
|
-
bigint: (val) => typeof val === "bigint",
|
|
2517
|
-
function: (val) => typeof val === "function",
|
|
2518
|
-
boolean: (val) => typeof val === "boolean",
|
|
2519
|
-
string: (val) => typeof val === "string",
|
|
2520
|
-
stringOrUint8Array: (val) => typeof val === "string" || val instanceof Uint8Array,
|
|
2521
|
-
isSafeInteger: (val) => Number.isSafeInteger(val),
|
|
2522
|
-
array: (val) => Array.isArray(val),
|
|
2523
|
-
field: (val, object) => object.Fp.isValid(val),
|
|
2524
|
-
hash: (val) => typeof val === "function" && Number.isSafeInteger(val.outputLen)
|
|
2525
|
-
};
|
|
2526
|
-
function validateObject(object, validators, optValidators = {}) {
|
|
2527
|
-
const checkField = (fieldName, type, isOptional) => {
|
|
2528
|
-
const checkVal = validatorFns[type];
|
|
2529
|
-
if (typeof checkVal !== "function")
|
|
2530
|
-
throw new Error(`Invalid validator "${type}", expected function`);
|
|
2531
|
-
const val = object[fieldName];
|
|
2532
|
-
if (isOptional && val === void 0)
|
|
2533
|
-
return;
|
|
2534
|
-
if (!checkVal(val, object)) {
|
|
2535
|
-
throw new Error(`Invalid param ${String(fieldName)}=${val} (${typeof val}), expected ${type}`);
|
|
2536
|
-
}
|
|
2537
|
-
};
|
|
2538
|
-
for (const [fieldName, type] of Object.entries(validators))
|
|
2539
|
-
checkField(fieldName, type, false);
|
|
2540
|
-
for (const [fieldName, type] of Object.entries(optValidators))
|
|
2541
|
-
checkField(fieldName, type, true);
|
|
2542
|
-
return object;
|
|
2543
|
-
}
|
|
2544
|
-
|
|
2545
|
-
// ../node_modules/@noble/curves/esm/abstract/modular.js
|
|
2546
|
-
var _0n2 = BigInt(0);
|
|
2547
|
-
var _1n2 = BigInt(1);
|
|
2548
|
-
var _2n2 = BigInt(2);
|
|
2549
|
-
var _3n = BigInt(3);
|
|
2550
|
-
var _4n = BigInt(4);
|
|
2551
|
-
var _5n = BigInt(5);
|
|
2552
|
-
var _8n = BigInt(8);
|
|
2553
|
-
var _9n = BigInt(9);
|
|
2554
|
-
var _16n = BigInt(16);
|
|
2555
|
-
function mod(a, b) {
|
|
2556
|
-
const result = a % b;
|
|
2557
|
-
return result >= _0n2 ? result : b + result;
|
|
2558
|
-
}
|
|
2559
|
-
function pow(num11, power, modulo) {
|
|
2560
|
-
if (modulo <= _0n2 || power < _0n2)
|
|
2561
|
-
throw new Error("Expected power/modulo > 0");
|
|
2562
|
-
if (modulo === _1n2)
|
|
2563
|
-
return _0n2;
|
|
2564
|
-
let res = _1n2;
|
|
2565
|
-
while (power > _0n2) {
|
|
2566
|
-
if (power & _1n2)
|
|
2567
|
-
res = res * num11 % modulo;
|
|
2568
|
-
num11 = num11 * num11 % modulo;
|
|
2569
|
-
power >>= _1n2;
|
|
2570
|
-
}
|
|
2571
|
-
return res;
|
|
2572
|
-
}
|
|
2573
|
-
function invert(number2, modulo) {
|
|
2574
|
-
if (number2 === _0n2 || modulo <= _0n2) {
|
|
2575
|
-
throw new Error(`invert: expected positive integers, got n=${number2} mod=${modulo}`);
|
|
2576
|
-
}
|
|
2577
|
-
let a = mod(number2, modulo);
|
|
2578
|
-
let b = modulo;
|
|
2579
|
-
let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
|
|
2580
|
-
while (a !== _0n2) {
|
|
2581
|
-
const q = b / a;
|
|
2582
|
-
const r = b % a;
|
|
2583
|
-
const m = x - u * q;
|
|
2584
|
-
const n = y - v * q;
|
|
2585
|
-
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
2586
|
-
}
|
|
2587
|
-
const gcd = b;
|
|
2588
|
-
if (gcd !== _1n2)
|
|
2589
|
-
throw new Error("invert: does not exist");
|
|
2590
|
-
return mod(x, modulo);
|
|
2591
|
-
}
|
|
2592
|
-
function tonelliShanks(P) {
|
|
2593
|
-
const legendreC = (P - _1n2) / _2n2;
|
|
2594
|
-
let Q, S, Z;
|
|
2595
|
-
for (Q = P - _1n2, S = 0; Q % _2n2 === _0n2; Q /= _2n2, S++)
|
|
2596
|
-
;
|
|
2597
|
-
for (Z = _2n2; Z < P && pow(Z, legendreC, P) !== P - _1n2; Z++)
|
|
2598
|
-
;
|
|
2599
|
-
if (S === 1) {
|
|
2600
|
-
const p1div4 = (P + _1n2) / _4n;
|
|
2601
|
-
return function tonelliFast(Fp, n) {
|
|
2602
|
-
const root = Fp.pow(n, p1div4);
|
|
2603
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
|
2604
|
-
throw new Error("Cannot find square root");
|
|
2605
|
-
return root;
|
|
2606
|
-
};
|
|
2607
|
-
}
|
|
2608
|
-
const Q1div2 = (Q + _1n2) / _2n2;
|
|
2609
|
-
return function tonelliSlow(Fp, n) {
|
|
2610
|
-
if (Fp.pow(n, legendreC) === Fp.neg(Fp.ONE))
|
|
2611
|
-
throw new Error("Cannot find square root");
|
|
2612
|
-
let r = S;
|
|
2613
|
-
let g = Fp.pow(Fp.mul(Fp.ONE, Z), Q);
|
|
2614
|
-
let x = Fp.pow(n, Q1div2);
|
|
2615
|
-
let b = Fp.pow(n, Q);
|
|
2616
|
-
while (!Fp.eql(b, Fp.ONE)) {
|
|
2617
|
-
if (Fp.eql(b, Fp.ZERO))
|
|
2618
|
-
return Fp.ZERO;
|
|
2619
|
-
let m = 1;
|
|
2620
|
-
for (let t2 = Fp.sqr(b); m < r; m++) {
|
|
2621
|
-
if (Fp.eql(t2, Fp.ONE))
|
|
2622
|
-
break;
|
|
2623
|
-
t2 = Fp.sqr(t2);
|
|
2624
|
-
}
|
|
2625
|
-
const ge = Fp.pow(g, _1n2 << BigInt(r - m - 1));
|
|
2626
|
-
g = Fp.sqr(ge);
|
|
2627
|
-
x = Fp.mul(x, ge);
|
|
2628
|
-
b = Fp.mul(b, g);
|
|
2629
|
-
r = m;
|
|
2630
|
-
}
|
|
2631
|
-
return x;
|
|
2632
|
-
};
|
|
2633
|
-
}
|
|
2634
|
-
function FpSqrt(P) {
|
|
2635
|
-
if (P % _4n === _3n) {
|
|
2636
|
-
const p1div4 = (P + _1n2) / _4n;
|
|
2637
|
-
return function sqrt3mod4(Fp, n) {
|
|
2638
|
-
const root = Fp.pow(n, p1div4);
|
|
2639
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
|
2640
|
-
throw new Error("Cannot find square root");
|
|
2641
|
-
return root;
|
|
2642
|
-
};
|
|
2643
|
-
}
|
|
2644
|
-
if (P % _8n === _5n) {
|
|
2645
|
-
const c1 = (P - _5n) / _8n;
|
|
2646
|
-
return function sqrt5mod8(Fp, n) {
|
|
2647
|
-
const n2 = Fp.mul(n, _2n2);
|
|
2648
|
-
const v = Fp.pow(n2, c1);
|
|
2649
|
-
const nv = Fp.mul(n, v);
|
|
2650
|
-
const i = Fp.mul(Fp.mul(nv, _2n2), v);
|
|
2651
|
-
const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
|
|
2652
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
|
2653
|
-
throw new Error("Cannot find square root");
|
|
2654
|
-
return root;
|
|
2655
|
-
};
|
|
2656
|
-
}
|
|
2657
|
-
if (P % _16n === _9n) {
|
|
2658
|
-
}
|
|
2659
|
-
return tonelliShanks(P);
|
|
2660
|
-
}
|
|
2661
|
-
var FIELD_FIELDS = [
|
|
2662
|
-
"create",
|
|
2663
|
-
"isValid",
|
|
2664
|
-
"is0",
|
|
2665
|
-
"neg",
|
|
2666
|
-
"inv",
|
|
2667
|
-
"sqrt",
|
|
2668
|
-
"sqr",
|
|
2669
|
-
"eql",
|
|
2670
|
-
"add",
|
|
2671
|
-
"sub",
|
|
2672
|
-
"mul",
|
|
2673
|
-
"pow",
|
|
2674
|
-
"div",
|
|
2675
|
-
"addN",
|
|
2676
|
-
"subN",
|
|
2677
|
-
"mulN",
|
|
2678
|
-
"sqrN"
|
|
2679
|
-
];
|
|
2680
|
-
function validateField(field) {
|
|
2681
|
-
const initial = {
|
|
2682
|
-
ORDER: "bigint",
|
|
2683
|
-
MASK: "bigint",
|
|
2684
|
-
BYTES: "isSafeInteger",
|
|
2685
|
-
BITS: "isSafeInteger"
|
|
2686
|
-
};
|
|
2687
|
-
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
2688
|
-
map[val] = "function";
|
|
2689
|
-
return map;
|
|
2690
|
-
}, initial);
|
|
2691
|
-
return validateObject(field, opts);
|
|
2692
|
-
}
|
|
2693
|
-
function FpPow(f, num11, power) {
|
|
2694
|
-
if (power < _0n2)
|
|
2695
|
-
throw new Error("Expected power > 0");
|
|
2696
|
-
if (power === _0n2)
|
|
2697
|
-
return f.ONE;
|
|
2698
|
-
if (power === _1n2)
|
|
2699
|
-
return num11;
|
|
2700
|
-
let p = f.ONE;
|
|
2701
|
-
let d = num11;
|
|
2702
|
-
while (power > _0n2) {
|
|
2703
|
-
if (power & _1n2)
|
|
2704
|
-
p = f.mul(p, d);
|
|
2705
|
-
d = f.sqr(d);
|
|
2706
|
-
power >>= _1n2;
|
|
2707
|
-
}
|
|
2708
|
-
return p;
|
|
2709
|
-
}
|
|
2710
|
-
function FpInvertBatch(f, nums) {
|
|
2711
|
-
const tmp = new Array(nums.length);
|
|
2712
|
-
const lastMultiplied = nums.reduce((acc, num11, i) => {
|
|
2713
|
-
if (f.is0(num11))
|
|
2714
|
-
return acc;
|
|
2715
|
-
tmp[i] = acc;
|
|
2716
|
-
return f.mul(acc, num11);
|
|
2717
|
-
}, f.ONE);
|
|
2718
|
-
const inverted = f.inv(lastMultiplied);
|
|
2719
|
-
nums.reduceRight((acc, num11, i) => {
|
|
2720
|
-
if (f.is0(num11))
|
|
2721
|
-
return acc;
|
|
2722
|
-
tmp[i] = f.mul(acc, tmp[i]);
|
|
2723
|
-
return f.mul(acc, num11);
|
|
2724
|
-
}, inverted);
|
|
2725
|
-
return tmp;
|
|
2726
|
-
}
|
|
2727
|
-
function nLength(n, nBitLength2) {
|
|
2728
|
-
const _nBitLength = nBitLength2 !== void 0 ? nBitLength2 : n.toString(2).length;
|
|
2729
|
-
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
2730
|
-
return { nBitLength: _nBitLength, nByteLength };
|
|
2731
|
-
}
|
|
2732
|
-
function Field(ORDER, bitLen2, isLE2 = false, redef = {}) {
|
|
2733
|
-
if (ORDER <= _0n2)
|
|
2734
|
-
throw new Error(`Expected Field ORDER > 0, got ${ORDER}`);
|
|
2735
|
-
const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, bitLen2);
|
|
2736
|
-
if (BYTES > 2048)
|
|
2737
|
-
throw new Error("Field lengths over 2048 bytes are not supported");
|
|
2738
|
-
const sqrtP = FpSqrt(ORDER);
|
|
2739
|
-
const f = Object.freeze({
|
|
2740
|
-
ORDER,
|
|
2741
|
-
BITS,
|
|
2742
|
-
BYTES,
|
|
2743
|
-
MASK: bitMask(BITS),
|
|
2744
|
-
ZERO: _0n2,
|
|
2745
|
-
ONE: _1n2,
|
|
2746
|
-
create: (num11) => mod(num11, ORDER),
|
|
2747
|
-
isValid: (num11) => {
|
|
2748
|
-
if (typeof num11 !== "bigint")
|
|
2749
|
-
throw new Error(`Invalid field element: expected bigint, got ${typeof num11}`);
|
|
2750
|
-
return _0n2 <= num11 && num11 < ORDER;
|
|
2751
|
-
},
|
|
2752
|
-
is0: (num11) => num11 === _0n2,
|
|
2753
|
-
isOdd: (num11) => (num11 & _1n2) === _1n2,
|
|
2754
|
-
neg: (num11) => mod(-num11, ORDER),
|
|
2755
|
-
eql: (lhs, rhs) => lhs === rhs,
|
|
2756
|
-
sqr: (num11) => mod(num11 * num11, ORDER),
|
|
2757
|
-
add: (lhs, rhs) => mod(lhs + rhs, ORDER),
|
|
2758
|
-
sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
|
|
2759
|
-
mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
|
|
2760
|
-
pow: (num11, power) => FpPow(f, num11, power),
|
|
2761
|
-
div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
|
|
2762
|
-
// Same as above, but doesn't normalize
|
|
2763
|
-
sqrN: (num11) => num11 * num11,
|
|
2764
|
-
addN: (lhs, rhs) => lhs + rhs,
|
|
2765
|
-
subN: (lhs, rhs) => lhs - rhs,
|
|
2766
|
-
mulN: (lhs, rhs) => lhs * rhs,
|
|
2767
|
-
inv: (num11) => invert(num11, ORDER),
|
|
2768
|
-
sqrt: redef.sqrt || ((n) => sqrtP(f, n)),
|
|
2769
|
-
invertBatch: (lst) => FpInvertBatch(f, lst),
|
|
2770
|
-
// TODO: do we really need constant cmov?
|
|
2771
|
-
// We don't have const-time bigints anyway, so probably will be not very useful
|
|
2772
|
-
cmov: (a, b, c) => c ? b : a,
|
|
2773
|
-
toBytes: (num11) => isLE2 ? numberToBytesLE(num11, BYTES) : numberToBytesBE(num11, BYTES),
|
|
2774
|
-
fromBytes: (bytes2) => {
|
|
2775
|
-
if (bytes2.length !== BYTES)
|
|
2776
|
-
throw new Error(`Fp.fromBytes: expected ${BYTES}, got ${bytes2.length}`);
|
|
2777
|
-
return isLE2 ? bytesToNumberLE(bytes2) : bytesToNumberBE(bytes2);
|
|
2778
|
-
}
|
|
2779
|
-
});
|
|
2780
|
-
return Object.freeze(f);
|
|
2781
|
-
}
|
|
2782
|
-
function getFieldBytesLength(fieldOrder) {
|
|
2783
|
-
if (typeof fieldOrder !== "bigint")
|
|
2784
|
-
throw new Error("field order must be bigint");
|
|
2785
|
-
const bitLength = fieldOrder.toString(2).length;
|
|
2786
|
-
return Math.ceil(bitLength / 8);
|
|
2787
|
-
}
|
|
2788
|
-
function getMinHashLength(fieldOrder) {
|
|
2789
|
-
const length = getFieldBytesLength(fieldOrder);
|
|
2790
|
-
return length + Math.ceil(length / 2);
|
|
2791
|
-
}
|
|
2792
|
-
function mapHashToField(key, fieldOrder, isLE2 = false) {
|
|
2793
|
-
const len = key.length;
|
|
2794
|
-
const fieldLen = getFieldBytesLength(fieldOrder);
|
|
2795
|
-
const minLen = getMinHashLength(fieldOrder);
|
|
2796
|
-
if (len < 16 || len < minLen || len > 1024)
|
|
2797
|
-
throw new Error(`expected ${minLen}-1024 bytes of input, got ${len}`);
|
|
2798
|
-
const num11 = isLE2 ? bytesToNumberBE(key) : bytesToNumberLE(key);
|
|
2799
|
-
const reduced = mod(num11, fieldOrder - _1n2) + _1n2;
|
|
2800
|
-
return isLE2 ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
2801
|
-
}
|
|
2802
|
-
|
|
2803
|
-
// ../node_modules/@noble/curves/esm/abstract/poseidon.js
|
|
2804
|
-
function validateOpts(opts) {
|
|
2805
|
-
const { Fp, mds, reversePartialPowIdx: rev, roundConstants: rc } = opts;
|
|
2806
|
-
const { roundsFull, roundsPartial, sboxPower, t } = opts;
|
|
2807
|
-
validateField(Fp);
|
|
2808
|
-
for (const i of ["t", "roundsFull", "roundsPartial"]) {
|
|
2809
|
-
if (typeof opts[i] !== "number" || !Number.isSafeInteger(opts[i]))
|
|
2810
|
-
throw new Error(`Poseidon: invalid param ${i}=${opts[i]} (${typeof opts[i]})`);
|
|
2811
|
-
}
|
|
2812
|
-
if (!Array.isArray(mds) || mds.length !== t)
|
|
2813
|
-
throw new Error("Poseidon: wrong MDS matrix");
|
|
2814
|
-
const _mds = mds.map((mdsRow) => {
|
|
2815
|
-
if (!Array.isArray(mdsRow) || mdsRow.length !== t)
|
|
2816
|
-
throw new Error(`Poseidon MDS matrix row: ${mdsRow}`);
|
|
2817
|
-
return mdsRow.map((i) => {
|
|
2818
|
-
if (typeof i !== "bigint")
|
|
2819
|
-
throw new Error(`Poseidon MDS matrix value=${i}`);
|
|
2820
|
-
return Fp.create(i);
|
|
2821
|
-
});
|
|
2822
|
-
});
|
|
2823
|
-
if (rev !== void 0 && typeof rev !== "boolean")
|
|
2824
|
-
throw new Error(`Poseidon: invalid param reversePartialPowIdx=${rev}`);
|
|
2825
|
-
if (roundsFull % 2 !== 0)
|
|
2826
|
-
throw new Error(`Poseidon roundsFull is not even: ${roundsFull}`);
|
|
2827
|
-
const rounds = roundsFull + roundsPartial;
|
|
2828
|
-
if (!Array.isArray(rc) || rc.length !== rounds)
|
|
2829
|
-
throw new Error("Poseidon: wrong round constants");
|
|
2830
|
-
const roundConstants = rc.map((rc2) => {
|
|
2831
|
-
if (!Array.isArray(rc2) || rc2.length !== t)
|
|
2832
|
-
throw new Error(`Poseidon wrong round constants: ${rc2}`);
|
|
2833
|
-
return rc2.map((i) => {
|
|
2834
|
-
if (typeof i !== "bigint" || !Fp.isValid(i))
|
|
2835
|
-
throw new Error(`Poseidon wrong round constant=${i}`);
|
|
2836
|
-
return Fp.create(i);
|
|
2837
|
-
});
|
|
2838
|
-
});
|
|
2839
|
-
if (!sboxPower || ![3, 5, 7].includes(sboxPower))
|
|
2840
|
-
throw new Error(`Poseidon wrong sboxPower=${sboxPower}`);
|
|
2841
|
-
const _sboxPower = BigInt(sboxPower);
|
|
2842
|
-
let sboxFn = (n) => FpPow(Fp, n, _sboxPower);
|
|
2843
|
-
if (sboxPower === 3)
|
|
2844
|
-
sboxFn = (n) => Fp.mul(Fp.sqrN(n), n);
|
|
2845
|
-
else if (sboxPower === 5)
|
|
2846
|
-
sboxFn = (n) => Fp.mul(Fp.sqrN(Fp.sqrN(n)), n);
|
|
2847
|
-
return Object.freeze({ ...opts, rounds, sboxFn, roundConstants, mds: _mds });
|
|
2848
|
-
}
|
|
2849
|
-
function poseidon(opts) {
|
|
2850
|
-
const _opts = validateOpts(opts);
|
|
2851
|
-
const { Fp, mds, roundConstants, rounds, roundsPartial, sboxFn, t } = _opts;
|
|
2852
|
-
const halfRoundsFull = _opts.roundsFull / 2;
|
|
2853
|
-
const partialIdx = _opts.reversePartialPowIdx ? t - 1 : 0;
|
|
2854
|
-
const poseidonRound = (values, isFull, idx) => {
|
|
2855
|
-
values = values.map((i, j) => Fp.add(i, roundConstants[idx][j]));
|
|
2856
|
-
if (isFull)
|
|
2857
|
-
values = values.map((i) => sboxFn(i));
|
|
2858
|
-
else
|
|
2859
|
-
values[partialIdx] = sboxFn(values[partialIdx]);
|
|
2860
|
-
values = mds.map((i) => i.reduce((acc, i2, j) => Fp.add(acc, Fp.mulN(i2, values[j])), Fp.ZERO));
|
|
2861
|
-
return values;
|
|
2862
|
-
};
|
|
2863
|
-
const poseidonHash = function poseidonHash2(values) {
|
|
2864
|
-
if (!Array.isArray(values) || values.length !== t)
|
|
2865
|
-
throw new Error(`Poseidon: wrong values (expected array of bigints with length ${t})`);
|
|
2866
|
-
values = values.map((i) => {
|
|
2867
|
-
if (typeof i !== "bigint")
|
|
2868
|
-
throw new Error(`Poseidon: wrong value=${i} (${typeof i})`);
|
|
2869
|
-
return Fp.create(i);
|
|
2870
|
-
});
|
|
2871
|
-
let round = 0;
|
|
2872
|
-
for (let i = 0; i < halfRoundsFull; i++)
|
|
2873
|
-
values = poseidonRound(values, true, round++);
|
|
2874
|
-
for (let i = 0; i < roundsPartial; i++)
|
|
2875
|
-
values = poseidonRound(values, false, round++);
|
|
2876
|
-
for (let i = 0; i < halfRoundsFull; i++)
|
|
2877
|
-
values = poseidonRound(values, true, round++);
|
|
2878
|
-
if (round !== rounds)
|
|
2879
|
-
throw new Error(`Poseidon: wrong number of rounds: last round=${round}, total=${rounds}`);
|
|
2880
|
-
return values;
|
|
2881
|
-
};
|
|
2882
|
-
poseidonHash.roundConstants = roundConstants;
|
|
2883
|
-
return poseidonHash;
|
|
2884
|
-
}
|
|
2885
|
-
|
|
2886
|
-
// ../node_modules/@noble/curves/esm/abstract/curve.js
|
|
2887
|
-
var _0n3 = BigInt(0);
|
|
2888
|
-
var _1n3 = BigInt(1);
|
|
2889
|
-
function wNAF(c, bits) {
|
|
2890
|
-
const constTimeNegate = (condition, item) => {
|
|
2891
|
-
const neg = item.negate();
|
|
2892
|
-
return condition ? neg : item;
|
|
2893
|
-
};
|
|
2894
|
-
const opts = (W) => {
|
|
2895
|
-
const windows = Math.ceil(bits / W) + 1;
|
|
2896
|
-
const windowSize = 2 ** (W - 1);
|
|
2897
|
-
return { windows, windowSize };
|
|
2898
|
-
};
|
|
2899
|
-
return {
|
|
2900
|
-
constTimeNegate,
|
|
2901
|
-
// non-const time multiplication ladder
|
|
2902
|
-
unsafeLadder(elm, n) {
|
|
2903
|
-
let p = c.ZERO;
|
|
2904
|
-
let d = elm;
|
|
2905
|
-
while (n > _0n3) {
|
|
2906
|
-
if (n & _1n3)
|
|
2907
|
-
p = p.add(d);
|
|
2908
|
-
d = d.double();
|
|
2909
|
-
n >>= _1n3;
|
|
2910
|
-
}
|
|
2911
|
-
return p;
|
|
2912
|
-
},
|
|
2913
|
-
/**
|
|
2914
|
-
* Creates a wNAF precomputation window. Used for caching.
|
|
2915
|
-
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
2916
|
-
* Number of precomputed points depends on the curve size:
|
|
2917
|
-
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
2918
|
-
* - 𝑊 is the window size
|
|
2919
|
-
* - 𝑛 is the bitlength of the curve order.
|
|
2920
|
-
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
2921
|
-
* @returns precomputed point tables flattened to a single array
|
|
2922
|
-
*/
|
|
2923
|
-
precomputeWindow(elm, W) {
|
|
2924
|
-
const { windows, windowSize } = opts(W);
|
|
2925
|
-
const points = [];
|
|
2926
|
-
let p = elm;
|
|
2927
|
-
let base = p;
|
|
2928
|
-
for (let window = 0; window < windows; window++) {
|
|
2929
|
-
base = p;
|
|
2930
|
-
points.push(base);
|
|
2931
|
-
for (let i = 1; i < windowSize; i++) {
|
|
2932
|
-
base = base.add(p);
|
|
2933
|
-
points.push(base);
|
|
2934
|
-
}
|
|
2935
|
-
p = base.double();
|
|
2936
|
-
}
|
|
2937
|
-
return points;
|
|
2938
|
-
},
|
|
2939
|
-
/**
|
|
2940
|
-
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
2941
|
-
* @param W window size
|
|
2942
|
-
* @param precomputes precomputed tables
|
|
2943
|
-
* @param n scalar (we don't check here, but should be less than curve order)
|
|
2944
|
-
* @returns real and fake (for const-time) points
|
|
2945
|
-
*/
|
|
2946
|
-
wNAF(W, precomputes, n) {
|
|
2947
|
-
const { windows, windowSize } = opts(W);
|
|
2948
|
-
let p = c.ZERO;
|
|
2949
|
-
let f = c.BASE;
|
|
2950
|
-
const mask = BigInt(2 ** W - 1);
|
|
2951
|
-
const maxNumber = 2 ** W;
|
|
2952
|
-
const shiftBy = BigInt(W);
|
|
2953
|
-
for (let window = 0; window < windows; window++) {
|
|
2954
|
-
const offset = window * windowSize;
|
|
2955
|
-
let wbits = Number(n & mask);
|
|
2956
|
-
n >>= shiftBy;
|
|
2957
|
-
if (wbits > windowSize) {
|
|
2958
|
-
wbits -= maxNumber;
|
|
2959
|
-
n += _1n3;
|
|
2960
|
-
}
|
|
2961
|
-
const offset1 = offset;
|
|
2962
|
-
const offset2 = offset + Math.abs(wbits) - 1;
|
|
2963
|
-
const cond1 = window % 2 !== 0;
|
|
2964
|
-
const cond2 = wbits < 0;
|
|
2965
|
-
if (wbits === 0) {
|
|
2966
|
-
f = f.add(constTimeNegate(cond1, precomputes[offset1]));
|
|
2967
|
-
} else {
|
|
2968
|
-
p = p.add(constTimeNegate(cond2, precomputes[offset2]));
|
|
2969
|
-
}
|
|
2970
|
-
}
|
|
2971
|
-
return { p, f };
|
|
2972
|
-
},
|
|
2973
|
-
wNAFCached(P, precomputesMap, n, transform) {
|
|
2974
|
-
const W = P._WINDOW_SIZE || 1;
|
|
2975
|
-
let comp = precomputesMap.get(P);
|
|
2976
|
-
if (!comp) {
|
|
2977
|
-
comp = this.precomputeWindow(P, W);
|
|
2978
|
-
if (W !== 1) {
|
|
2979
|
-
precomputesMap.set(P, transform(comp));
|
|
2980
|
-
}
|
|
2981
|
-
}
|
|
2982
|
-
return this.wNAF(W, comp, n);
|
|
2983
|
-
}
|
|
2984
|
-
};
|
|
2985
|
-
}
|
|
2986
|
-
function validateBasic(curve2) {
|
|
2987
|
-
validateField(curve2.Fp);
|
|
2988
|
-
validateObject(curve2, {
|
|
2989
|
-
n: "bigint",
|
|
2990
|
-
h: "bigint",
|
|
2991
|
-
Gx: "field",
|
|
2992
|
-
Gy: "field"
|
|
2993
|
-
}, {
|
|
2994
|
-
nBitLength: "isSafeInteger",
|
|
2995
|
-
nByteLength: "isSafeInteger"
|
|
2996
|
-
});
|
|
2997
|
-
return Object.freeze({
|
|
2998
|
-
...nLength(curve2.n, curve2.nBitLength),
|
|
2999
|
-
...curve2,
|
|
3000
|
-
...{ p: curve2.Fp.ORDER }
|
|
3001
|
-
});
|
|
3002
|
-
}
|
|
3003
|
-
|
|
3004
|
-
// ../node_modules/@noble/curves/esm/abstract/weierstrass.js
|
|
3005
|
-
function validatePointOpts(curve2) {
|
|
3006
|
-
const opts = validateBasic(curve2);
|
|
3007
|
-
validateObject(opts, {
|
|
3008
|
-
a: "field",
|
|
3009
|
-
b: "field"
|
|
3010
|
-
}, {
|
|
3011
|
-
allowedPrivateKeyLengths: "array",
|
|
3012
|
-
wrapPrivateKey: "boolean",
|
|
3013
|
-
isTorsionFree: "function",
|
|
3014
|
-
clearCofactor: "function",
|
|
3015
|
-
allowInfinityPoint: "boolean",
|
|
3016
|
-
fromBytes: "function",
|
|
3017
|
-
toBytes: "function"
|
|
3018
|
-
});
|
|
3019
|
-
const { endo, Fp, a } = opts;
|
|
3020
|
-
if (endo) {
|
|
3021
|
-
if (!Fp.eql(a, Fp.ZERO)) {
|
|
3022
|
-
throw new Error("Endomorphism can only be defined for Koblitz curves that have a=0");
|
|
3023
|
-
}
|
|
3024
|
-
if (typeof endo !== "object" || typeof endo.beta !== "bigint" || typeof endo.splitScalar !== "function") {
|
|
3025
|
-
throw new Error("Expected endomorphism with beta: bigint and splitScalar: function");
|
|
3026
|
-
}
|
|
3027
|
-
}
|
|
3028
|
-
return Object.freeze({ ...opts });
|
|
3029
|
-
}
|
|
3030
|
-
var { bytesToNumberBE: b2n, hexToBytes: h2b } = utils_exports;
|
|
3031
|
-
var DER = {
|
|
3032
|
-
// asn.1 DER encoding utils
|
|
3033
|
-
Err: class DERErr extends Error {
|
|
3034
|
-
constructor(m = "") {
|
|
3035
|
-
super(m);
|
|
3036
|
-
}
|
|
3037
|
-
},
|
|
3038
|
-
_parseInt(data) {
|
|
3039
|
-
const { Err: E } = DER;
|
|
3040
|
-
if (data.length < 2 || data[0] !== 2)
|
|
3041
|
-
throw new E("Invalid signature integer tag");
|
|
3042
|
-
const len = data[1];
|
|
3043
|
-
const res = data.subarray(2, len + 2);
|
|
3044
|
-
if (!len || res.length !== len)
|
|
3045
|
-
throw new E("Invalid signature integer: wrong length");
|
|
3046
|
-
if (res[0] & 128)
|
|
3047
|
-
throw new E("Invalid signature integer: negative");
|
|
3048
|
-
if (res[0] === 0 && !(res[1] & 128))
|
|
3049
|
-
throw new E("Invalid signature integer: unnecessary leading zero");
|
|
3050
|
-
return { d: b2n(res), l: data.subarray(len + 2) };
|
|
3051
|
-
},
|
|
3052
|
-
toSig(hex) {
|
|
3053
|
-
const { Err: E } = DER;
|
|
3054
|
-
const data = typeof hex === "string" ? h2b(hex) : hex;
|
|
3055
|
-
if (!(data instanceof Uint8Array))
|
|
3056
|
-
throw new Error("ui8a expected");
|
|
3057
|
-
let l = data.length;
|
|
3058
|
-
if (l < 2 || data[0] != 48)
|
|
3059
|
-
throw new E("Invalid signature tag");
|
|
3060
|
-
if (data[1] !== l - 2)
|
|
3061
|
-
throw new E("Invalid signature: incorrect length");
|
|
3062
|
-
const { d: r, l: sBytes } = DER._parseInt(data.subarray(2));
|
|
3063
|
-
const { d: s, l: rBytesLeft } = DER._parseInt(sBytes);
|
|
3064
|
-
if (rBytesLeft.length)
|
|
3065
|
-
throw new E("Invalid signature: left bytes after parsing");
|
|
3066
|
-
return { r, s };
|
|
3067
|
-
},
|
|
3068
|
-
hexFromSig(sig) {
|
|
3069
|
-
const slice = (s2) => Number.parseInt(s2[0], 16) & 8 ? "00" + s2 : s2;
|
|
3070
|
-
const h = (num11) => {
|
|
3071
|
-
const hex = num11.toString(16);
|
|
3072
|
-
return hex.length & 1 ? `0${hex}` : hex;
|
|
3073
|
-
};
|
|
3074
|
-
const s = slice(h(sig.s));
|
|
3075
|
-
const r = slice(h(sig.r));
|
|
3076
|
-
const shl = s.length / 2;
|
|
3077
|
-
const rhl = r.length / 2;
|
|
3078
|
-
const sl = h(shl);
|
|
3079
|
-
const rl = h(rhl);
|
|
3080
|
-
return `30${h(rhl + shl + 4)}02${rl}${r}02${sl}${s}`;
|
|
3081
|
-
}
|
|
3082
|
-
};
|
|
3083
|
-
var _0n4 = BigInt(0);
|
|
3084
|
-
var _1n4 = BigInt(1);
|
|
3085
|
-
var _2n3 = BigInt(2);
|
|
3086
|
-
var _3n2 = BigInt(3);
|
|
3087
|
-
var _4n2 = BigInt(4);
|
|
3088
|
-
function weierstrassPoints(opts) {
|
|
3089
|
-
const CURVE2 = validatePointOpts(opts);
|
|
3090
|
-
const { Fp } = CURVE2;
|
|
3091
|
-
const toBytes2 = CURVE2.toBytes || ((_c, point, _isCompressed) => {
|
|
3092
|
-
const a = point.toAffine();
|
|
3093
|
-
return concatBytes2(Uint8Array.from([4]), Fp.toBytes(a.x), Fp.toBytes(a.y));
|
|
3094
|
-
});
|
|
3095
|
-
const fromBytes = CURVE2.fromBytes || ((bytes2) => {
|
|
3096
|
-
const tail = bytes2.subarray(1);
|
|
3097
|
-
const x = Fp.fromBytes(tail.subarray(0, Fp.BYTES));
|
|
3098
|
-
const y = Fp.fromBytes(tail.subarray(Fp.BYTES, 2 * Fp.BYTES));
|
|
3099
|
-
return { x, y };
|
|
3100
|
-
});
|
|
3101
|
-
function weierstrassEquation(x) {
|
|
3102
|
-
const { a, b } = CURVE2;
|
|
3103
|
-
const x2 = Fp.sqr(x);
|
|
3104
|
-
const x3 = Fp.mul(x2, x);
|
|
3105
|
-
return Fp.add(Fp.add(x3, Fp.mul(x, a)), b);
|
|
3106
|
-
}
|
|
3107
|
-
if (!Fp.eql(Fp.sqr(CURVE2.Gy), weierstrassEquation(CURVE2.Gx)))
|
|
3108
|
-
throw new Error("bad generator point: equation left != right");
|
|
3109
|
-
function isWithinCurveOrder(num11) {
|
|
3110
|
-
return typeof num11 === "bigint" && _0n4 < num11 && num11 < CURVE2.n;
|
|
3111
|
-
}
|
|
3112
|
-
function assertGE(num11) {
|
|
3113
|
-
if (!isWithinCurveOrder(num11))
|
|
3114
|
-
throw new Error("Expected valid bigint: 0 < bigint < curve.n");
|
|
3115
|
-
}
|
|
3116
|
-
function normPrivateKeyToScalar(key) {
|
|
3117
|
-
const { allowedPrivateKeyLengths: lengths, nByteLength, wrapPrivateKey, n } = CURVE2;
|
|
3118
|
-
if (lengths && typeof key !== "bigint") {
|
|
3119
|
-
if (key instanceof Uint8Array)
|
|
3120
|
-
key = bytesToHex(key);
|
|
3121
|
-
if (typeof key !== "string" || !lengths.includes(key.length))
|
|
3122
|
-
throw new Error("Invalid key");
|
|
3123
|
-
key = key.padStart(nByteLength * 2, "0");
|
|
3124
|
-
}
|
|
3125
|
-
let num11;
|
|
3126
|
-
try {
|
|
3127
|
-
num11 = typeof key === "bigint" ? key : bytesToNumberBE(ensureBytes("private key", key, nByteLength));
|
|
3128
|
-
} catch (error) {
|
|
3129
|
-
throw new Error(`private key must be ${nByteLength} bytes, hex or bigint, not ${typeof key}`);
|
|
3130
|
-
}
|
|
3131
|
-
if (wrapPrivateKey)
|
|
3132
|
-
num11 = mod(num11, n);
|
|
3133
|
-
assertGE(num11);
|
|
3134
|
-
return num11;
|
|
3135
|
-
}
|
|
3136
|
-
const pointPrecomputes = /* @__PURE__ */ new Map();
|
|
3137
|
-
function assertPrjPoint(other) {
|
|
3138
|
-
if (!(other instanceof Point))
|
|
3139
|
-
throw new Error("ProjectivePoint expected");
|
|
3140
|
-
}
|
|
3141
|
-
class Point {
|
|
3142
|
-
constructor(px, py, pz) {
|
|
3143
|
-
this.px = px;
|
|
3144
|
-
this.py = py;
|
|
3145
|
-
this.pz = pz;
|
|
3146
|
-
if (px == null || !Fp.isValid(px))
|
|
3147
|
-
throw new Error("x required");
|
|
3148
|
-
if (py == null || !Fp.isValid(py))
|
|
3149
|
-
throw new Error("y required");
|
|
3150
|
-
if (pz == null || !Fp.isValid(pz))
|
|
3151
|
-
throw new Error("z required");
|
|
3152
|
-
}
|
|
3153
|
-
// Does not validate if the point is on-curve.
|
|
3154
|
-
// Use fromHex instead, or call assertValidity() later.
|
|
3155
|
-
static fromAffine(p) {
|
|
3156
|
-
const { x, y } = p || {};
|
|
3157
|
-
if (!p || !Fp.isValid(x) || !Fp.isValid(y))
|
|
3158
|
-
throw new Error("invalid affine point");
|
|
3159
|
-
if (p instanceof Point)
|
|
3160
|
-
throw new Error("projective point not allowed");
|
|
3161
|
-
const is0 = (i) => Fp.eql(i, Fp.ZERO);
|
|
3162
|
-
if (is0(x) && is0(y))
|
|
3163
|
-
return Point.ZERO;
|
|
3164
|
-
return new Point(x, y, Fp.ONE);
|
|
3165
|
-
}
|
|
3166
|
-
get x() {
|
|
3167
|
-
return this.toAffine().x;
|
|
3168
|
-
}
|
|
3169
|
-
get y() {
|
|
3170
|
-
return this.toAffine().y;
|
|
3171
|
-
}
|
|
3172
|
-
/**
|
|
3173
|
-
* Takes a bunch of Projective Points but executes only one
|
|
3174
|
-
* inversion on all of them. Inversion is very slow operation,
|
|
3175
|
-
* so this improves performance massively.
|
|
3176
|
-
* Optimization: converts a list of projective points to a list of identical points with Z=1.
|
|
3177
|
-
*/
|
|
3178
|
-
static normalizeZ(points) {
|
|
3179
|
-
const toInv = Fp.invertBatch(points.map((p) => p.pz));
|
|
3180
|
-
return points.map((p, i) => p.toAffine(toInv[i])).map(Point.fromAffine);
|
|
3181
|
-
}
|
|
3182
|
-
/**
|
|
3183
|
-
* Converts hash string or Uint8Array to Point.
|
|
3184
|
-
* @param hex short/long ECDSA hex
|
|
3185
|
-
*/
|
|
3186
|
-
static fromHex(hex) {
|
|
3187
|
-
const P = Point.fromAffine(fromBytes(ensureBytes("pointHex", hex)));
|
|
3188
|
-
P.assertValidity();
|
|
3189
|
-
return P;
|
|
3190
|
-
}
|
|
3191
|
-
// Multiplies generator point by privateKey.
|
|
3192
|
-
static fromPrivateKey(privateKey) {
|
|
3193
|
-
return Point.BASE.multiply(normPrivateKeyToScalar(privateKey));
|
|
3194
|
-
}
|
|
3195
|
-
// "Private method", don't use it directly
|
|
3196
|
-
_setWindowSize(windowSize) {
|
|
3197
|
-
this._WINDOW_SIZE = windowSize;
|
|
3198
|
-
pointPrecomputes.delete(this);
|
|
3199
|
-
}
|
|
3200
|
-
// A point on curve is valid if it conforms to equation.
|
|
3201
|
-
assertValidity() {
|
|
3202
|
-
if (this.is0()) {
|
|
3203
|
-
if (CURVE2.allowInfinityPoint && !Fp.is0(this.py))
|
|
3204
|
-
return;
|
|
3205
|
-
throw new Error("bad point: ZERO");
|
|
3206
|
-
}
|
|
3207
|
-
const { x, y } = this.toAffine();
|
|
3208
|
-
if (!Fp.isValid(x) || !Fp.isValid(y))
|
|
3209
|
-
throw new Error("bad point: x or y not FE");
|
|
3210
|
-
const left = Fp.sqr(y);
|
|
3211
|
-
const right = weierstrassEquation(x);
|
|
3212
|
-
if (!Fp.eql(left, right))
|
|
3213
|
-
throw new Error("bad point: equation left != right");
|
|
3214
|
-
if (!this.isTorsionFree())
|
|
3215
|
-
throw new Error("bad point: not in prime-order subgroup");
|
|
3216
|
-
}
|
|
3217
|
-
hasEvenY() {
|
|
3218
|
-
const { y } = this.toAffine();
|
|
3219
|
-
if (Fp.isOdd)
|
|
3220
|
-
return !Fp.isOdd(y);
|
|
3221
|
-
throw new Error("Field doesn't support isOdd");
|
|
3222
|
-
}
|
|
3223
|
-
/**
|
|
3224
|
-
* Compare one point to another.
|
|
3225
|
-
*/
|
|
3226
|
-
equals(other) {
|
|
3227
|
-
assertPrjPoint(other);
|
|
3228
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
3229
|
-
const { px: X2, py: Y2, pz: Z2 } = other;
|
|
3230
|
-
const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
|
|
3231
|
-
const U2 = Fp.eql(Fp.mul(Y1, Z2), Fp.mul(Y2, Z1));
|
|
3232
|
-
return U1 && U2;
|
|
3233
|
-
}
|
|
3234
|
-
/**
|
|
3235
|
-
* Flips point to one corresponding to (x, -y) in Affine coordinates.
|
|
3236
|
-
*/
|
|
3237
|
-
negate() {
|
|
3238
|
-
return new Point(this.px, Fp.neg(this.py), this.pz);
|
|
3239
|
-
}
|
|
3240
|
-
// Renes-Costello-Batina exception-free doubling formula.
|
|
3241
|
-
// There is 30% faster Jacobian formula, but it is not complete.
|
|
3242
|
-
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
3243
|
-
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
3244
|
-
double() {
|
|
3245
|
-
const { a, b } = CURVE2;
|
|
3246
|
-
const b3 = Fp.mul(b, _3n2);
|
|
3247
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
3248
|
-
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
3249
|
-
let t0 = Fp.mul(X1, X1);
|
|
3250
|
-
let t1 = Fp.mul(Y1, Y1);
|
|
3251
|
-
let t2 = Fp.mul(Z1, Z1);
|
|
3252
|
-
let t3 = Fp.mul(X1, Y1);
|
|
3253
|
-
t3 = Fp.add(t3, t3);
|
|
3254
|
-
Z3 = Fp.mul(X1, Z1);
|
|
3255
|
-
Z3 = Fp.add(Z3, Z3);
|
|
3256
|
-
X3 = Fp.mul(a, Z3);
|
|
3257
|
-
Y3 = Fp.mul(b3, t2);
|
|
3258
|
-
Y3 = Fp.add(X3, Y3);
|
|
3259
|
-
X3 = Fp.sub(t1, Y3);
|
|
3260
|
-
Y3 = Fp.add(t1, Y3);
|
|
3261
|
-
Y3 = Fp.mul(X3, Y3);
|
|
3262
|
-
X3 = Fp.mul(t3, X3);
|
|
3263
|
-
Z3 = Fp.mul(b3, Z3);
|
|
3264
|
-
t2 = Fp.mul(a, t2);
|
|
3265
|
-
t3 = Fp.sub(t0, t2);
|
|
3266
|
-
t3 = Fp.mul(a, t3);
|
|
3267
|
-
t3 = Fp.add(t3, Z3);
|
|
3268
|
-
Z3 = Fp.add(t0, t0);
|
|
3269
|
-
t0 = Fp.add(Z3, t0);
|
|
3270
|
-
t0 = Fp.add(t0, t2);
|
|
3271
|
-
t0 = Fp.mul(t0, t3);
|
|
3272
|
-
Y3 = Fp.add(Y3, t0);
|
|
3273
|
-
t2 = Fp.mul(Y1, Z1);
|
|
3274
|
-
t2 = Fp.add(t2, t2);
|
|
3275
|
-
t0 = Fp.mul(t2, t3);
|
|
3276
|
-
X3 = Fp.sub(X3, t0);
|
|
3277
|
-
Z3 = Fp.mul(t2, t1);
|
|
3278
|
-
Z3 = Fp.add(Z3, Z3);
|
|
3279
|
-
Z3 = Fp.add(Z3, Z3);
|
|
3280
|
-
return new Point(X3, Y3, Z3);
|
|
3281
|
-
}
|
|
3282
|
-
// Renes-Costello-Batina exception-free addition formula.
|
|
3283
|
-
// There is 30% faster Jacobian formula, but it is not complete.
|
|
3284
|
-
// https://eprint.iacr.org/2015/1060, algorithm 1
|
|
3285
|
-
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
3286
|
-
add(other) {
|
|
3287
|
-
assertPrjPoint(other);
|
|
3288
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
3289
|
-
const { px: X2, py: Y2, pz: Z2 } = other;
|
|
3290
|
-
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
3291
|
-
const a = CURVE2.a;
|
|
3292
|
-
const b3 = Fp.mul(CURVE2.b, _3n2);
|
|
3293
|
-
let t0 = Fp.mul(X1, X2);
|
|
3294
|
-
let t1 = Fp.mul(Y1, Y2);
|
|
3295
|
-
let t2 = Fp.mul(Z1, Z2);
|
|
3296
|
-
let t3 = Fp.add(X1, Y1);
|
|
3297
|
-
let t4 = Fp.add(X2, Y2);
|
|
3298
|
-
t3 = Fp.mul(t3, t4);
|
|
3299
|
-
t4 = Fp.add(t0, t1);
|
|
3300
|
-
t3 = Fp.sub(t3, t4);
|
|
3301
|
-
t4 = Fp.add(X1, Z1);
|
|
3302
|
-
let t5 = Fp.add(X2, Z2);
|
|
3303
|
-
t4 = Fp.mul(t4, t5);
|
|
3304
|
-
t5 = Fp.add(t0, t2);
|
|
3305
|
-
t4 = Fp.sub(t4, t5);
|
|
3306
|
-
t5 = Fp.add(Y1, Z1);
|
|
3307
|
-
X3 = Fp.add(Y2, Z2);
|
|
3308
|
-
t5 = Fp.mul(t5, X3);
|
|
3309
|
-
X3 = Fp.add(t1, t2);
|
|
3310
|
-
t5 = Fp.sub(t5, X3);
|
|
3311
|
-
Z3 = Fp.mul(a, t4);
|
|
3312
|
-
X3 = Fp.mul(b3, t2);
|
|
3313
|
-
Z3 = Fp.add(X3, Z3);
|
|
3314
|
-
X3 = Fp.sub(t1, Z3);
|
|
3315
|
-
Z3 = Fp.add(t1, Z3);
|
|
3316
|
-
Y3 = Fp.mul(X3, Z3);
|
|
3317
|
-
t1 = Fp.add(t0, t0);
|
|
3318
|
-
t1 = Fp.add(t1, t0);
|
|
3319
|
-
t2 = Fp.mul(a, t2);
|
|
3320
|
-
t4 = Fp.mul(b3, t4);
|
|
3321
|
-
t1 = Fp.add(t1, t2);
|
|
3322
|
-
t2 = Fp.sub(t0, t2);
|
|
3323
|
-
t2 = Fp.mul(a, t2);
|
|
3324
|
-
t4 = Fp.add(t4, t2);
|
|
3325
|
-
t0 = Fp.mul(t1, t4);
|
|
3326
|
-
Y3 = Fp.add(Y3, t0);
|
|
3327
|
-
t0 = Fp.mul(t5, t4);
|
|
3328
|
-
X3 = Fp.mul(t3, X3);
|
|
3329
|
-
X3 = Fp.sub(X3, t0);
|
|
3330
|
-
t0 = Fp.mul(t3, t1);
|
|
3331
|
-
Z3 = Fp.mul(t5, Z3);
|
|
3332
|
-
Z3 = Fp.add(Z3, t0);
|
|
3333
|
-
return new Point(X3, Y3, Z3);
|
|
3334
|
-
}
|
|
3335
|
-
subtract(other) {
|
|
3336
|
-
return this.add(other.negate());
|
|
3337
|
-
}
|
|
3338
|
-
is0() {
|
|
3339
|
-
return this.equals(Point.ZERO);
|
|
3340
|
-
}
|
|
3341
|
-
wNAF(n) {
|
|
3342
|
-
return wnaf.wNAFCached(this, pointPrecomputes, n, (comp) => {
|
|
3343
|
-
const toInv = Fp.invertBatch(comp.map((p) => p.pz));
|
|
3344
|
-
return comp.map((p, i) => p.toAffine(toInv[i])).map(Point.fromAffine);
|
|
3345
|
-
});
|
|
3346
|
-
}
|
|
3347
|
-
/**
|
|
3348
|
-
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
3349
|
-
* It's faster, but should only be used when you don't care about
|
|
3350
|
-
* an exposed private key e.g. sig verification, which works over *public* keys.
|
|
3351
|
-
*/
|
|
3352
|
-
multiplyUnsafe(n) {
|
|
3353
|
-
const I = Point.ZERO;
|
|
3354
|
-
if (n === _0n4)
|
|
3355
|
-
return I;
|
|
3356
|
-
assertGE(n);
|
|
3357
|
-
if (n === _1n4)
|
|
3358
|
-
return this;
|
|
3359
|
-
const { endo } = CURVE2;
|
|
3360
|
-
if (!endo)
|
|
3361
|
-
return wnaf.unsafeLadder(this, n);
|
|
3362
|
-
let { k1neg, k1, k2neg, k2 } = endo.splitScalar(n);
|
|
3363
|
-
let k1p = I;
|
|
3364
|
-
let k2p = I;
|
|
3365
|
-
let d = this;
|
|
3366
|
-
while (k1 > _0n4 || k2 > _0n4) {
|
|
3367
|
-
if (k1 & _1n4)
|
|
3368
|
-
k1p = k1p.add(d);
|
|
3369
|
-
if (k2 & _1n4)
|
|
3370
|
-
k2p = k2p.add(d);
|
|
3371
|
-
d = d.double();
|
|
3372
|
-
k1 >>= _1n4;
|
|
3373
|
-
k2 >>= _1n4;
|
|
3374
|
-
}
|
|
3375
|
-
if (k1neg)
|
|
3376
|
-
k1p = k1p.negate();
|
|
3377
|
-
if (k2neg)
|
|
3378
|
-
k2p = k2p.negate();
|
|
3379
|
-
k2p = new Point(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
|
|
3380
|
-
return k1p.add(k2p);
|
|
3381
|
-
}
|
|
3382
|
-
/**
|
|
3383
|
-
* Constant time multiplication.
|
|
3384
|
-
* Uses wNAF method. Windowed method may be 10% faster,
|
|
3385
|
-
* but takes 2x longer to generate and consumes 2x memory.
|
|
3386
|
-
* Uses precomputes when available.
|
|
3387
|
-
* Uses endomorphism for Koblitz curves.
|
|
3388
|
-
* @param scalar by which the point would be multiplied
|
|
3389
|
-
* @returns New point
|
|
3390
|
-
*/
|
|
3391
|
-
multiply(scalar) {
|
|
3392
|
-
assertGE(scalar);
|
|
3393
|
-
let n = scalar;
|
|
3394
|
-
let point, fake;
|
|
3395
|
-
const { endo } = CURVE2;
|
|
3396
|
-
if (endo) {
|
|
3397
|
-
const { k1neg, k1, k2neg, k2 } = endo.splitScalar(n);
|
|
3398
|
-
let { p: k1p, f: f1p } = this.wNAF(k1);
|
|
3399
|
-
let { p: k2p, f: f2p } = this.wNAF(k2);
|
|
3400
|
-
k1p = wnaf.constTimeNegate(k1neg, k1p);
|
|
3401
|
-
k2p = wnaf.constTimeNegate(k2neg, k2p);
|
|
3402
|
-
k2p = new Point(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
|
|
3403
|
-
point = k1p.add(k2p);
|
|
3404
|
-
fake = f1p.add(f2p);
|
|
3405
|
-
} else {
|
|
3406
|
-
const { p, f } = this.wNAF(n);
|
|
3407
|
-
point = p;
|
|
3408
|
-
fake = f;
|
|
3409
|
-
}
|
|
3410
|
-
return Point.normalizeZ([point, fake])[0];
|
|
3411
|
-
}
|
|
3412
|
-
/**
|
|
3413
|
-
* Efficiently calculate `aP + bQ`. Unsafe, can expose private key, if used incorrectly.
|
|
3414
|
-
* Not using Strauss-Shamir trick: precomputation tables are faster.
|
|
3415
|
-
* The trick could be useful if both P and Q are not G (not in our case).
|
|
3416
|
-
* @returns non-zero affine point
|
|
3417
|
-
*/
|
|
3418
|
-
multiplyAndAddUnsafe(Q, a, b) {
|
|
3419
|
-
const G = Point.BASE;
|
|
3420
|
-
const mul = (P, a2) => a2 === _0n4 || a2 === _1n4 || !P.equals(G) ? P.multiplyUnsafe(a2) : P.multiply(a2);
|
|
3421
|
-
const sum = mul(this, a).add(mul(Q, b));
|
|
3422
|
-
return sum.is0() ? void 0 : sum;
|
|
3423
|
-
}
|
|
3424
|
-
// Converts Projective point to affine (x, y) coordinates.
|
|
3425
|
-
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
3426
|
-
// (x, y, z) ∋ (x=x/z, y=y/z)
|
|
3427
|
-
toAffine(iz) {
|
|
3428
|
-
const { px: x, py: y, pz: z } = this;
|
|
3429
|
-
const is0 = this.is0();
|
|
3430
|
-
if (iz == null)
|
|
3431
|
-
iz = is0 ? Fp.ONE : Fp.inv(z);
|
|
3432
|
-
const ax = Fp.mul(x, iz);
|
|
3433
|
-
const ay = Fp.mul(y, iz);
|
|
3434
|
-
const zz = Fp.mul(z, iz);
|
|
3435
|
-
if (is0)
|
|
3436
|
-
return { x: Fp.ZERO, y: Fp.ZERO };
|
|
3437
|
-
if (!Fp.eql(zz, Fp.ONE))
|
|
3438
|
-
throw new Error("invZ was invalid");
|
|
3439
|
-
return { x: ax, y: ay };
|
|
3440
|
-
}
|
|
3441
|
-
isTorsionFree() {
|
|
3442
|
-
const { h: cofactor, isTorsionFree } = CURVE2;
|
|
3443
|
-
if (cofactor === _1n4)
|
|
3444
|
-
return true;
|
|
3445
|
-
if (isTorsionFree)
|
|
3446
|
-
return isTorsionFree(Point, this);
|
|
3447
|
-
throw new Error("isTorsionFree() has not been declared for the elliptic curve");
|
|
3448
|
-
}
|
|
3449
|
-
clearCofactor() {
|
|
3450
|
-
const { h: cofactor, clearCofactor } = CURVE2;
|
|
3451
|
-
if (cofactor === _1n4)
|
|
3452
|
-
return this;
|
|
3453
|
-
if (clearCofactor)
|
|
3454
|
-
return clearCofactor(Point, this);
|
|
3455
|
-
return this.multiplyUnsafe(CURVE2.h);
|
|
3456
|
-
}
|
|
3457
|
-
toRawBytes(isCompressed = true) {
|
|
3458
|
-
this.assertValidity();
|
|
3459
|
-
return toBytes2(Point, this, isCompressed);
|
|
3460
|
-
}
|
|
3461
|
-
toHex(isCompressed = true) {
|
|
3462
|
-
return bytesToHex(this.toRawBytes(isCompressed));
|
|
3463
|
-
}
|
|
3464
|
-
}
|
|
3465
|
-
Point.BASE = new Point(CURVE2.Gx, CURVE2.Gy, Fp.ONE);
|
|
3466
|
-
Point.ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO);
|
|
3467
|
-
const _bits = CURVE2.nBitLength;
|
|
3468
|
-
const wnaf = wNAF(Point, CURVE2.endo ? Math.ceil(_bits / 2) : _bits);
|
|
3469
|
-
return {
|
|
3470
|
-
CURVE: CURVE2,
|
|
3471
|
-
ProjectivePoint: Point,
|
|
3472
|
-
normPrivateKeyToScalar,
|
|
3473
|
-
weierstrassEquation,
|
|
3474
|
-
isWithinCurveOrder
|
|
3475
|
-
};
|
|
3476
|
-
}
|
|
3477
|
-
function validateOpts2(curve2) {
|
|
3478
|
-
const opts = validateBasic(curve2);
|
|
3479
|
-
validateObject(opts, {
|
|
3480
|
-
hash: "hash",
|
|
3481
|
-
hmac: "function",
|
|
3482
|
-
randomBytes: "function"
|
|
3483
|
-
}, {
|
|
3484
|
-
bits2int: "function",
|
|
3485
|
-
bits2int_modN: "function",
|
|
3486
|
-
lowS: "boolean"
|
|
3487
|
-
});
|
|
3488
|
-
return Object.freeze({ lowS: true, ...opts });
|
|
3489
|
-
}
|
|
3490
|
-
function weierstrass(curveDef) {
|
|
3491
|
-
const CURVE2 = validateOpts2(curveDef);
|
|
3492
|
-
const { Fp, n: CURVE_ORDER2 } = CURVE2;
|
|
3493
|
-
const compressedLen = Fp.BYTES + 1;
|
|
3494
|
-
const uncompressedLen = 2 * Fp.BYTES + 1;
|
|
3495
|
-
function isValidFieldElement(num11) {
|
|
3496
|
-
return _0n4 < num11 && num11 < Fp.ORDER;
|
|
3497
|
-
}
|
|
3498
|
-
function modN(a) {
|
|
3499
|
-
return mod(a, CURVE_ORDER2);
|
|
3500
|
-
}
|
|
3501
|
-
function invN(a) {
|
|
3502
|
-
return invert(a, CURVE_ORDER2);
|
|
3503
|
-
}
|
|
3504
|
-
const { ProjectivePoint: Point, normPrivateKeyToScalar, weierstrassEquation, isWithinCurveOrder } = weierstrassPoints({
|
|
3505
|
-
...CURVE2,
|
|
3506
|
-
toBytes(_c, point, isCompressed) {
|
|
3507
|
-
const a = point.toAffine();
|
|
3508
|
-
const x = Fp.toBytes(a.x);
|
|
3509
|
-
const cat = concatBytes2;
|
|
3510
|
-
if (isCompressed) {
|
|
3511
|
-
return cat(Uint8Array.from([point.hasEvenY() ? 2 : 3]), x);
|
|
3512
|
-
} else {
|
|
3513
|
-
return cat(Uint8Array.from([4]), x, Fp.toBytes(a.y));
|
|
3514
|
-
}
|
|
3515
|
-
},
|
|
3516
|
-
fromBytes(bytes2) {
|
|
3517
|
-
const len = bytes2.length;
|
|
3518
|
-
const head = bytes2[0];
|
|
3519
|
-
const tail = bytes2.subarray(1);
|
|
3520
|
-
if (len === compressedLen && (head === 2 || head === 3)) {
|
|
3521
|
-
const x = bytesToNumberBE(tail);
|
|
3522
|
-
if (!isValidFieldElement(x))
|
|
3523
|
-
throw new Error("Point is not on curve");
|
|
3524
|
-
const y2 = weierstrassEquation(x);
|
|
3525
|
-
let y = Fp.sqrt(y2);
|
|
3526
|
-
const isYOdd = (y & _1n4) === _1n4;
|
|
3527
|
-
const isHeadOdd = (head & 1) === 1;
|
|
3528
|
-
if (isHeadOdd !== isYOdd)
|
|
3529
|
-
y = Fp.neg(y);
|
|
3530
|
-
return { x, y };
|
|
3531
|
-
} else if (len === uncompressedLen && head === 4) {
|
|
3532
|
-
const x = Fp.fromBytes(tail.subarray(0, Fp.BYTES));
|
|
3533
|
-
const y = Fp.fromBytes(tail.subarray(Fp.BYTES, 2 * Fp.BYTES));
|
|
3534
|
-
return { x, y };
|
|
3535
|
-
} else {
|
|
3536
|
-
throw new Error(`Point of length ${len} was invalid. Expected ${compressedLen} compressed bytes or ${uncompressedLen} uncompressed bytes`);
|
|
3537
|
-
}
|
|
3538
|
-
}
|
|
3539
|
-
});
|
|
3540
|
-
const numToNByteStr = (num11) => bytesToHex(numberToBytesBE(num11, CURVE2.nByteLength));
|
|
3541
|
-
function isBiggerThanHalfOrder(number2) {
|
|
3542
|
-
const HALF = CURVE_ORDER2 >> _1n4;
|
|
3543
|
-
return number2 > HALF;
|
|
3544
|
-
}
|
|
3545
|
-
function normalizeS(s) {
|
|
3546
|
-
return isBiggerThanHalfOrder(s) ? modN(-s) : s;
|
|
3547
|
-
}
|
|
3548
|
-
const slcNum = (b, from, to) => bytesToNumberBE(b.slice(from, to));
|
|
3549
|
-
class Signature2 {
|
|
3550
|
-
constructor(r, s, recovery) {
|
|
3551
|
-
this.r = r;
|
|
3552
|
-
this.s = s;
|
|
3553
|
-
this.recovery = recovery;
|
|
3554
|
-
this.assertValidity();
|
|
3555
|
-
}
|
|
3556
|
-
// pair (bytes of r, bytes of s)
|
|
3557
|
-
static fromCompact(hex) {
|
|
3558
|
-
const l = CURVE2.nByteLength;
|
|
3559
|
-
hex = ensureBytes("compactSignature", hex, l * 2);
|
|
3560
|
-
return new Signature2(slcNum(hex, 0, l), slcNum(hex, l, 2 * l));
|
|
3561
|
-
}
|
|
3562
|
-
// DER encoded ECDSA signature
|
|
3563
|
-
// https://bitcoin.stackexchange.com/questions/57644/what-are-the-parts-of-a-bitcoin-transaction-input-script
|
|
3564
|
-
static fromDER(hex) {
|
|
3565
|
-
const { r, s } = DER.toSig(ensureBytes("DER", hex));
|
|
3566
|
-
return new Signature2(r, s);
|
|
3567
|
-
}
|
|
3568
|
-
assertValidity() {
|
|
3569
|
-
if (!isWithinCurveOrder(this.r))
|
|
3570
|
-
throw new Error("r must be 0 < r < CURVE.n");
|
|
3571
|
-
if (!isWithinCurveOrder(this.s))
|
|
3572
|
-
throw new Error("s must be 0 < s < CURVE.n");
|
|
3573
|
-
}
|
|
3574
|
-
addRecoveryBit(recovery) {
|
|
3575
|
-
return new Signature2(this.r, this.s, recovery);
|
|
3576
|
-
}
|
|
3577
|
-
recoverPublicKey(msgHash) {
|
|
3578
|
-
const { r, s, recovery: rec } = this;
|
|
3579
|
-
const h = bits2int_modN(ensureBytes("msgHash", msgHash));
|
|
3580
|
-
if (rec == null || ![0, 1, 2, 3].includes(rec))
|
|
3581
|
-
throw new Error("recovery id invalid");
|
|
3582
|
-
const radj = rec === 2 || rec === 3 ? r + CURVE2.n : r;
|
|
3583
|
-
if (radj >= Fp.ORDER)
|
|
3584
|
-
throw new Error("recovery id 2 or 3 invalid");
|
|
3585
|
-
const prefix = (rec & 1) === 0 ? "02" : "03";
|
|
3586
|
-
const R = Point.fromHex(prefix + numToNByteStr(radj));
|
|
3587
|
-
const ir = invN(radj);
|
|
3588
|
-
const u1 = modN(-h * ir);
|
|
3589
|
-
const u2 = modN(s * ir);
|
|
3590
|
-
const Q = Point.BASE.multiplyAndAddUnsafe(R, u1, u2);
|
|
3591
|
-
if (!Q)
|
|
3592
|
-
throw new Error("point at infinify");
|
|
3593
|
-
Q.assertValidity();
|
|
3594
|
-
return Q;
|
|
3595
|
-
}
|
|
3596
|
-
// Signatures should be low-s, to prevent malleability.
|
|
3597
|
-
hasHighS() {
|
|
3598
|
-
return isBiggerThanHalfOrder(this.s);
|
|
3599
|
-
}
|
|
3600
|
-
normalizeS() {
|
|
3601
|
-
return this.hasHighS() ? new Signature2(this.r, modN(-this.s), this.recovery) : this;
|
|
3602
|
-
}
|
|
3603
|
-
// DER-encoded
|
|
3604
|
-
toDERRawBytes() {
|
|
3605
|
-
return hexToBytes(this.toDERHex());
|
|
3606
|
-
}
|
|
3607
|
-
toDERHex() {
|
|
3608
|
-
return DER.hexFromSig({ r: this.r, s: this.s });
|
|
3609
|
-
}
|
|
3610
|
-
// padded bytes of r, then padded bytes of s
|
|
3611
|
-
toCompactRawBytes() {
|
|
3612
|
-
return hexToBytes(this.toCompactHex());
|
|
3613
|
-
}
|
|
3614
|
-
toCompactHex() {
|
|
3615
|
-
return numToNByteStr(this.r) + numToNByteStr(this.s);
|
|
3616
|
-
}
|
|
3617
|
-
}
|
|
3618
|
-
const utils2 = {
|
|
3619
|
-
isValidPrivateKey(privateKey) {
|
|
3620
|
-
try {
|
|
3621
|
-
normPrivateKeyToScalar(privateKey);
|
|
3622
|
-
return true;
|
|
3623
|
-
} catch (error) {
|
|
3624
|
-
return false;
|
|
3625
|
-
}
|
|
3626
|
-
},
|
|
3627
|
-
normPrivateKeyToScalar,
|
|
3628
|
-
/**
|
|
3629
|
-
* Produces cryptographically secure private key from random of size
|
|
3630
|
-
* (groupLen + ceil(groupLen / 2)) with modulo bias being negligible.
|
|
3631
|
-
*/
|
|
3632
|
-
randomPrivateKey: () => {
|
|
3633
|
-
const length = getMinHashLength(CURVE2.n);
|
|
3634
|
-
return mapHashToField(CURVE2.randomBytes(length), CURVE2.n);
|
|
3635
|
-
},
|
|
3636
|
-
/**
|
|
3637
|
-
* Creates precompute table for an arbitrary EC point. Makes point "cached".
|
|
3638
|
-
* Allows to massively speed-up `point.multiply(scalar)`.
|
|
3639
|
-
* @returns cached point
|
|
3640
|
-
* @example
|
|
3641
|
-
* const fast = utils.precompute(8, ProjectivePoint.fromHex(someonesPubKey));
|
|
3642
|
-
* fast.multiply(privKey); // much faster ECDH now
|
|
3643
|
-
*/
|
|
3644
|
-
precompute(windowSize = 8, point = Point.BASE) {
|
|
3645
|
-
point._setWindowSize(windowSize);
|
|
3646
|
-
point.multiply(BigInt(3));
|
|
3647
|
-
return point;
|
|
3648
|
-
}
|
|
3649
|
-
};
|
|
3650
|
-
function getPublicKey(privateKey, isCompressed = true) {
|
|
3651
|
-
return Point.fromPrivateKey(privateKey).toRawBytes(isCompressed);
|
|
3652
|
-
}
|
|
3653
|
-
function isProbPub(item) {
|
|
3654
|
-
const arr = item instanceof Uint8Array;
|
|
3655
|
-
const str = typeof item === "string";
|
|
3656
|
-
const len = (arr || str) && item.length;
|
|
3657
|
-
if (arr)
|
|
3658
|
-
return len === compressedLen || len === uncompressedLen;
|
|
3659
|
-
if (str)
|
|
3660
|
-
return len === 2 * compressedLen || len === 2 * uncompressedLen;
|
|
3661
|
-
if (item instanceof Point)
|
|
3662
|
-
return true;
|
|
3663
|
-
return false;
|
|
3664
|
-
}
|
|
3665
|
-
function getSharedSecret(privateA, publicB, isCompressed = true) {
|
|
3666
|
-
if (isProbPub(privateA))
|
|
3667
|
-
throw new Error("first arg must be private key");
|
|
3668
|
-
if (!isProbPub(publicB))
|
|
3669
|
-
throw new Error("second arg must be public key");
|
|
3670
|
-
const b = Point.fromHex(publicB);
|
|
3671
|
-
return b.multiply(normPrivateKeyToScalar(privateA)).toRawBytes(isCompressed);
|
|
3672
|
-
}
|
|
3673
|
-
const bits2int2 = CURVE2.bits2int || function(bytes2) {
|
|
3674
|
-
const num11 = bytesToNumberBE(bytes2);
|
|
3675
|
-
const delta = bytes2.length * 8 - CURVE2.nBitLength;
|
|
3676
|
-
return delta > 0 ? num11 >> BigInt(delta) : num11;
|
|
3677
|
-
};
|
|
3678
|
-
const bits2int_modN = CURVE2.bits2int_modN || function(bytes2) {
|
|
3679
|
-
return modN(bits2int2(bytes2));
|
|
3680
|
-
};
|
|
3681
|
-
const ORDER_MASK = bitMask(CURVE2.nBitLength);
|
|
3682
|
-
function int2octets(num11) {
|
|
3683
|
-
if (typeof num11 !== "bigint")
|
|
3684
|
-
throw new Error("bigint expected");
|
|
3685
|
-
if (!(_0n4 <= num11 && num11 < ORDER_MASK))
|
|
3686
|
-
throw new Error(`bigint expected < 2^${CURVE2.nBitLength}`);
|
|
3687
|
-
return numberToBytesBE(num11, CURVE2.nByteLength);
|
|
3688
|
-
}
|
|
3689
|
-
function prepSig(msgHash, privateKey, opts = defaultSigOpts) {
|
|
3690
|
-
if (["recovered", "canonical"].some((k) => k in opts))
|
|
3691
|
-
throw new Error("sign() legacy options not supported");
|
|
3692
|
-
const { hash: hash6, randomBytes: randomBytes4 } = CURVE2;
|
|
3693
|
-
let { lowS, prehash, extraEntropy: ent } = opts;
|
|
3694
|
-
if (lowS == null)
|
|
3695
|
-
lowS = true;
|
|
3696
|
-
msgHash = ensureBytes("msgHash", msgHash);
|
|
3697
|
-
if (prehash)
|
|
3698
|
-
msgHash = ensureBytes("prehashed msgHash", hash6(msgHash));
|
|
3699
|
-
const h1int = bits2int_modN(msgHash);
|
|
3700
|
-
const d = normPrivateKeyToScalar(privateKey);
|
|
3701
|
-
const seedArgs = [int2octets(d), int2octets(h1int)];
|
|
3702
|
-
if (ent != null) {
|
|
3703
|
-
const e = ent === true ? randomBytes4(Fp.BYTES) : ent;
|
|
3704
|
-
seedArgs.push(ensureBytes("extraEntropy", e));
|
|
3705
|
-
}
|
|
3706
|
-
const seed = concatBytes2(...seedArgs);
|
|
3707
|
-
const m = h1int;
|
|
3708
|
-
function k2sig(kBytes) {
|
|
3709
|
-
const k = bits2int2(kBytes);
|
|
3710
|
-
if (!isWithinCurveOrder(k))
|
|
3711
|
-
return;
|
|
3712
|
-
const ik = invN(k);
|
|
3713
|
-
const q = Point.BASE.multiply(k).toAffine();
|
|
3714
|
-
const r = modN(q.x);
|
|
3715
|
-
if (r === _0n4)
|
|
3716
|
-
return;
|
|
3717
|
-
const s = modN(ik * modN(m + r * d));
|
|
3718
|
-
if (s === _0n4)
|
|
3719
|
-
return;
|
|
3720
|
-
let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n4);
|
|
3721
|
-
let normS = s;
|
|
3722
|
-
if (lowS && isBiggerThanHalfOrder(s)) {
|
|
3723
|
-
normS = normalizeS(s);
|
|
3724
|
-
recovery ^= 1;
|
|
3725
|
-
}
|
|
3726
|
-
return new Signature2(r, normS, recovery);
|
|
3727
|
-
}
|
|
3728
|
-
return { seed, k2sig };
|
|
3729
|
-
}
|
|
3730
|
-
const defaultSigOpts = { lowS: CURVE2.lowS, prehash: false };
|
|
3731
|
-
const defaultVerOpts = { lowS: CURVE2.lowS, prehash: false };
|
|
3732
|
-
function sign(msgHash, privKey, opts = defaultSigOpts) {
|
|
3733
|
-
const { seed, k2sig } = prepSig(msgHash, privKey, opts);
|
|
3734
|
-
const C = CURVE2;
|
|
3735
|
-
const drbg = createHmacDrbg(C.hash.outputLen, C.nByteLength, C.hmac);
|
|
3736
|
-
return drbg(seed, k2sig);
|
|
3737
|
-
}
|
|
3738
|
-
Point.BASE._setWindowSize(8);
|
|
3739
|
-
function verify(signature, msgHash, publicKey, opts = defaultVerOpts) {
|
|
3740
|
-
const sg = signature;
|
|
3741
|
-
msgHash = ensureBytes("msgHash", msgHash);
|
|
3742
|
-
publicKey = ensureBytes("publicKey", publicKey);
|
|
3743
|
-
if ("strict" in opts)
|
|
3744
|
-
throw new Error("options.strict was renamed to lowS");
|
|
3745
|
-
const { lowS, prehash } = opts;
|
|
3746
|
-
let _sig = void 0;
|
|
3747
|
-
let P;
|
|
3748
|
-
try {
|
|
3749
|
-
if (typeof sg === "string" || sg instanceof Uint8Array) {
|
|
3750
|
-
try {
|
|
3751
|
-
_sig = Signature2.fromDER(sg);
|
|
3752
|
-
} catch (derError) {
|
|
3753
|
-
if (!(derError instanceof DER.Err))
|
|
3754
|
-
throw derError;
|
|
3755
|
-
_sig = Signature2.fromCompact(sg);
|
|
3756
|
-
}
|
|
3757
|
-
} else if (typeof sg === "object" && typeof sg.r === "bigint" && typeof sg.s === "bigint") {
|
|
3758
|
-
const { r: r2, s: s2 } = sg;
|
|
3759
|
-
_sig = new Signature2(r2, s2);
|
|
3760
|
-
} else {
|
|
3761
|
-
throw new Error("PARSE");
|
|
3762
|
-
}
|
|
3763
|
-
P = Point.fromHex(publicKey);
|
|
3764
|
-
} catch (error) {
|
|
3765
|
-
if (error.message === "PARSE")
|
|
3766
|
-
throw new Error(`signature must be Signature instance, Uint8Array or hex string`);
|
|
3767
|
-
return false;
|
|
3768
|
-
}
|
|
3769
|
-
if (lowS && _sig.hasHighS())
|
|
3770
|
-
return false;
|
|
3771
|
-
if (prehash)
|
|
3772
|
-
msgHash = CURVE2.hash(msgHash);
|
|
3773
|
-
const { r, s } = _sig;
|
|
3774
|
-
const h = bits2int_modN(msgHash);
|
|
3775
|
-
const is = invN(s);
|
|
3776
|
-
const u1 = modN(h * is);
|
|
3777
|
-
const u2 = modN(r * is);
|
|
3778
|
-
const R = Point.BASE.multiplyAndAddUnsafe(P, u1, u2)?.toAffine();
|
|
3779
|
-
if (!R)
|
|
3780
|
-
return false;
|
|
3781
|
-
const v = modN(R.x);
|
|
3782
|
-
return v === r;
|
|
3783
|
-
}
|
|
3784
|
-
return {
|
|
3785
|
-
CURVE: CURVE2,
|
|
3786
|
-
getPublicKey,
|
|
3787
|
-
getSharedSecret,
|
|
3788
|
-
sign,
|
|
3789
|
-
verify,
|
|
3790
|
-
ProjectivePoint: Point,
|
|
3791
|
-
Signature: Signature2,
|
|
3792
|
-
utils: utils2
|
|
3793
|
-
};
|
|
3794
|
-
}
|
|
3795
|
-
|
|
3796
|
-
// ../node_modules/@noble/hashes/esm/hmac.js
|
|
3797
|
-
var HMAC = class extends Hash {
|
|
3798
|
-
constructor(hash6, _key) {
|
|
3799
|
-
super();
|
|
3800
|
-
this.finished = false;
|
|
3801
|
-
this.destroyed = false;
|
|
3802
|
-
hash(hash6);
|
|
3803
|
-
const key = toBytes(_key);
|
|
3804
|
-
this.iHash = hash6.create();
|
|
3805
|
-
if (typeof this.iHash.update !== "function")
|
|
3806
|
-
throw new Error("Expected instance of class which extends utils.Hash");
|
|
3807
|
-
this.blockLen = this.iHash.blockLen;
|
|
3808
|
-
this.outputLen = this.iHash.outputLen;
|
|
3809
|
-
const blockLen = this.blockLen;
|
|
3810
|
-
const pad = new Uint8Array(blockLen);
|
|
3811
|
-
pad.set(key.length > blockLen ? hash6.create().update(key).digest() : key);
|
|
3812
|
-
for (let i = 0; i < pad.length; i++)
|
|
3813
|
-
pad[i] ^= 54;
|
|
3814
|
-
this.iHash.update(pad);
|
|
3815
|
-
this.oHash = hash6.create();
|
|
3816
|
-
for (let i = 0; i < pad.length; i++)
|
|
3817
|
-
pad[i] ^= 54 ^ 92;
|
|
3818
|
-
this.oHash.update(pad);
|
|
3819
|
-
pad.fill(0);
|
|
3820
|
-
}
|
|
3821
|
-
update(buf) {
|
|
3822
|
-
exists(this);
|
|
3823
|
-
this.iHash.update(buf);
|
|
3824
|
-
return this;
|
|
3825
|
-
}
|
|
3826
|
-
digestInto(out) {
|
|
3827
|
-
exists(this);
|
|
3828
|
-
bytes(out, this.outputLen);
|
|
3829
|
-
this.finished = true;
|
|
3830
|
-
this.iHash.digestInto(out);
|
|
3831
|
-
this.oHash.update(out);
|
|
3832
|
-
this.oHash.digestInto(out);
|
|
3833
|
-
this.destroy();
|
|
3834
|
-
}
|
|
3835
|
-
digest() {
|
|
3836
|
-
const out = new Uint8Array(this.oHash.outputLen);
|
|
3837
|
-
this.digestInto(out);
|
|
3838
|
-
return out;
|
|
3839
|
-
}
|
|
3840
|
-
_cloneInto(to) {
|
|
3841
|
-
to || (to = Object.create(Object.getPrototypeOf(this), {}));
|
|
3842
|
-
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
3843
|
-
to = to;
|
|
3844
|
-
to.finished = finished;
|
|
3845
|
-
to.destroyed = destroyed;
|
|
3846
|
-
to.blockLen = blockLen;
|
|
3847
|
-
to.outputLen = outputLen;
|
|
3848
|
-
to.oHash = oHash._cloneInto(to.oHash);
|
|
3849
|
-
to.iHash = iHash._cloneInto(to.iHash);
|
|
3850
|
-
return to;
|
|
3851
|
-
}
|
|
3852
|
-
destroy() {
|
|
3853
|
-
this.destroyed = true;
|
|
3854
|
-
this.oHash.destroy();
|
|
3855
|
-
this.iHash.destroy();
|
|
3856
|
-
}
|
|
3857
|
-
};
|
|
3858
|
-
var hmac = (hash6, key, message) => new HMAC(hash6, key).update(message).digest();
|
|
3859
|
-
hmac.create = (hash6, key) => new HMAC(hash6, key);
|
|
3860
|
-
|
|
3861
|
-
// ../node_modules/@noble/curves/esm/_shortw_utils.js
|
|
3862
|
-
function getHash(hash6) {
|
|
3863
|
-
return {
|
|
3864
|
-
hash: hash6,
|
|
3865
|
-
hmac: (key, ...msgs) => hmac(hash6, key, concatBytes(...msgs)),
|
|
3866
|
-
randomBytes
|
|
3867
|
-
};
|
|
3868
|
-
}
|
|
3869
|
-
|
|
3870
|
-
// ../node_modules/@scure/starknet/lib/esm/index.js
|
|
3871
|
-
var CURVE_ORDER = BigInt("3618502788666131213697322783095070105526743751716087489154079457884512865583");
|
|
3872
|
-
var MAX_VALUE = BigInt("0x800000000000000000000000000000000000000000000000000000000000000");
|
|
3873
|
-
var nBitLength = 252;
|
|
3874
|
-
function bits2int(bytes2) {
|
|
3875
|
-
while (bytes2[0] === 0)
|
|
3876
|
-
bytes2 = bytes2.subarray(1);
|
|
3877
|
-
const delta = bytes2.length * 8 - nBitLength;
|
|
3878
|
-
const num11 = bytesToNumberBE(bytes2);
|
|
3879
|
-
return delta > 0 ? num11 >> BigInt(delta) : num11;
|
|
3880
|
-
}
|
|
3881
|
-
function hex0xToBytes(hex) {
|
|
3882
|
-
if (typeof hex === "string") {
|
|
3883
|
-
hex = strip0x(hex);
|
|
3884
|
-
if (hex.length & 1)
|
|
3885
|
-
hex = "0" + hex;
|
|
3886
|
-
}
|
|
3887
|
-
return hexToBytes(hex);
|
|
3888
|
-
}
|
|
3889
|
-
var curve = weierstrass({
|
|
3890
|
-
a: BigInt(1),
|
|
3891
|
-
b: BigInt("3141592653589793238462643383279502884197169399375105820974944592307816406665"),
|
|
3892
|
-
Fp: Field(BigInt("0x800000000000011000000000000000000000000000000000000000000000001")),
|
|
3893
|
-
n: CURVE_ORDER,
|
|
3894
|
-
nBitLength,
|
|
3895
|
-
Gx: BigInt("874739451078007766457464989774322083649278607533249481151382481072868806602"),
|
|
3896
|
-
Gy: BigInt("152666792071518830868575557812948353041420400780739481342941381225525861407"),
|
|
3897
|
-
h: BigInt(1),
|
|
3898
|
-
lowS: false,
|
|
3899
|
-
...getHash(sha256),
|
|
3900
|
-
bits2int,
|
|
3901
|
-
bits2int_modN: (bytes2) => {
|
|
3902
|
-
const hex = bytesToNumberBE(bytes2).toString(16);
|
|
3903
|
-
if (hex.length === 63)
|
|
3904
|
-
bytes2 = hex0xToBytes(hex + "0");
|
|
3905
|
-
return mod(bits2int(bytes2), CURVE_ORDER);
|
|
3906
|
-
}
|
|
3907
|
-
});
|
|
3908
|
-
function ensureBytes2(hex) {
|
|
3909
|
-
return ensureBytes("", typeof hex === "string" ? hex0xToBytes(hex) : hex);
|
|
3910
|
-
}
|
|
3911
|
-
var { CURVE, ProjectivePoint, Signature, utils } = curve;
|
|
3912
|
-
function extractX(bytes2) {
|
|
3913
|
-
const hex = bytesToHex(bytes2.subarray(1));
|
|
3914
|
-
const stripped = hex.replace(/^0+/gm, "");
|
|
3915
|
-
return `0x${stripped}`;
|
|
3916
|
-
}
|
|
3917
|
-
function strip0x(hex) {
|
|
3918
|
-
return hex.replace(/^0x/i, "");
|
|
3919
|
-
}
|
|
3920
|
-
var MASK_31 = 2n ** 31n - 1n;
|
|
3921
|
-
var PEDERSEN_POINTS = [
|
|
3922
|
-
new ProjectivePoint(2089986280348253421170679821480865132823066470938446095505822317253594081284n, 1713931329540660377023406109199410414810705867260802078187082345529207694986n, 1n),
|
|
3923
|
-
new ProjectivePoint(996781205833008774514500082376783249102396023663454813447423147977397232763n, 1668503676786377725805489344771023921079126552019160156920634619255970485781n, 1n),
|
|
3924
|
-
new ProjectivePoint(2251563274489750535117886426533222435294046428347329203627021249169616184184n, 1798716007562728905295480679789526322175868328062420237419143593021674992973n, 1n),
|
|
3925
|
-
new ProjectivePoint(2138414695194151160943305727036575959195309218611738193261179310511854807447n, 113410276730064486255102093846540133784865286929052426931474106396135072156n, 1n),
|
|
3926
|
-
new ProjectivePoint(2379962749567351885752724891227938183011949129833673362440656643086021394946n, 776496453633298175483985398648758586525933812536653089401905292063708816422n, 1n)
|
|
3927
|
-
];
|
|
3928
|
-
function pedersenPrecompute(p1, p2) {
|
|
3929
|
-
const out = [];
|
|
3930
|
-
let p = p1;
|
|
3931
|
-
for (let i = 0; i < 248; i++) {
|
|
3932
|
-
out.push(p);
|
|
3933
|
-
p = p.double();
|
|
3934
|
-
}
|
|
3935
|
-
p = p2;
|
|
3936
|
-
for (let i = 0; i < 4; i++) {
|
|
3937
|
-
out.push(p);
|
|
3938
|
-
p = p.double();
|
|
3939
|
-
}
|
|
3940
|
-
return out;
|
|
3941
|
-
}
|
|
3942
|
-
var PEDERSEN_POINTS1 = pedersenPrecompute(PEDERSEN_POINTS[1], PEDERSEN_POINTS[2]);
|
|
3943
|
-
var PEDERSEN_POINTS2 = pedersenPrecompute(PEDERSEN_POINTS[3], PEDERSEN_POINTS[4]);
|
|
3944
|
-
function pedersenArg(arg) {
|
|
3945
|
-
let value;
|
|
3946
|
-
if (typeof arg === "bigint") {
|
|
3947
|
-
value = arg;
|
|
3948
|
-
} else if (typeof arg === "number") {
|
|
3949
|
-
if (!Number.isSafeInteger(arg))
|
|
3950
|
-
throw new Error(`Invalid pedersenArg: ${arg}`);
|
|
3951
|
-
value = BigInt(arg);
|
|
3952
|
-
} else {
|
|
3953
|
-
value = bytesToNumberBE(ensureBytes2(arg));
|
|
3954
|
-
}
|
|
3955
|
-
if (!(0n <= value && value < curve.CURVE.Fp.ORDER))
|
|
3956
|
-
throw new Error(`PedersenArg should be 0 <= value < CURVE.P: ${value}`);
|
|
3957
|
-
return value;
|
|
3958
|
-
}
|
|
3959
|
-
function pedersenSingle(point, value, constants3) {
|
|
3960
|
-
let x = pedersenArg(value);
|
|
3961
|
-
for (let j = 0; j < 252; j++) {
|
|
3962
|
-
const pt = constants3[j];
|
|
3963
|
-
if (pt.equals(point))
|
|
3964
|
-
throw new Error("Same point");
|
|
3965
|
-
if ((x & 1n) !== 0n)
|
|
3966
|
-
point = point.add(pt);
|
|
3967
|
-
x >>= 1n;
|
|
3968
|
-
}
|
|
3969
|
-
return point;
|
|
3970
|
-
}
|
|
3971
|
-
function pedersen(x, y) {
|
|
3972
|
-
let point = PEDERSEN_POINTS[0];
|
|
3973
|
-
point = pedersenSingle(point, x, PEDERSEN_POINTS1);
|
|
3974
|
-
point = pedersenSingle(point, y, PEDERSEN_POINTS2);
|
|
3975
|
-
return extractX(point.toRawBytes(true));
|
|
3976
|
-
}
|
|
3977
|
-
var MASK_250 = bitMask(250);
|
|
3978
|
-
var Fp251 = Field(BigInt("3618502788666131213697322783095070105623107215331596699973092056135872020481"));
|
|
3979
|
-
function poseidonRoundConstant(Fp, name, idx) {
|
|
3980
|
-
const val = Fp.fromBytes(sha256(utf8ToBytes(`${name}${idx}`)));
|
|
3981
|
-
return Fp.create(val);
|
|
3982
|
-
}
|
|
3983
|
-
var MDS_SMALL = [
|
|
3984
|
-
[3, 1, 1],
|
|
3985
|
-
[1, -1, 1],
|
|
3986
|
-
[1, 1, -2]
|
|
3987
|
-
].map((i) => i.map(BigInt));
|
|
3988
|
-
function poseidonBasic(opts, mds) {
|
|
3989
|
-
validateField(opts.Fp);
|
|
3990
|
-
if (!Number.isSafeInteger(opts.rate) || !Number.isSafeInteger(opts.capacity))
|
|
3991
|
-
throw new Error(`Wrong poseidon opts: ${opts}`);
|
|
3992
|
-
const m = opts.rate + opts.capacity;
|
|
3993
|
-
const rounds = opts.roundsFull + opts.roundsPartial;
|
|
3994
|
-
const roundConstants = [];
|
|
3995
|
-
for (let i = 0; i < rounds; i++) {
|
|
3996
|
-
const row = [];
|
|
3997
|
-
for (let j = 0; j < m; j++)
|
|
3998
|
-
row.push(poseidonRoundConstant(opts.Fp, "Hades", m * i + j));
|
|
3999
|
-
roundConstants.push(row);
|
|
4000
|
-
}
|
|
4001
|
-
const res = poseidon({
|
|
4002
|
-
...opts,
|
|
4003
|
-
t: m,
|
|
4004
|
-
sboxPower: 3,
|
|
4005
|
-
reversePartialPowIdx: true,
|
|
4006
|
-
mds,
|
|
4007
|
-
roundConstants
|
|
4008
|
-
});
|
|
4009
|
-
res.m = m;
|
|
4010
|
-
res.rate = opts.rate;
|
|
4011
|
-
res.capacity = opts.capacity;
|
|
4012
|
-
return res;
|
|
4013
|
-
}
|
|
4014
|
-
var poseidonSmall = poseidonBasic({ Fp: Fp251, rate: 2, capacity: 1, roundsFull: 8, roundsPartial: 83 }, MDS_SMALL);
|
|
4015
|
-
|
|
4016
|
-
// src/utils/oz-merkle.ts
|
|
1984
|
+
import * as starknet from "@scure/starknet";
|
|
4017
1985
|
function hash_leaf(leaf) {
|
|
4018
1986
|
if (leaf.data.length < 1) {
|
|
4019
1987
|
throw new Error("Invalid leaf data");
|
|
@@ -4026,7 +1994,7 @@ function hash_leaf(leaf) {
|
|
|
4026
1994
|
return `0x${num2.toHexString(value).replace(/^0x/, "").padStart(64, "0")}`;
|
|
4027
1995
|
}
|
|
4028
1996
|
function pedersen_hash(a, b) {
|
|
4029
|
-
return BigInt(pedersen(a, b).toString());
|
|
1997
|
+
return BigInt(starknet.pedersen(a, b).toString());
|
|
4030
1998
|
}
|
|
4031
1999
|
var StandardMerkleTree = class _StandardMerkleTree extends MerkleTreeImpl {
|
|
4032
2000
|
constructor(tree, values, leafEncoding) {
|
|
@@ -4183,6 +2151,7 @@ function getMainnetConfig(rpcUrl = "https://starknet-mainnet.public.blastapi.io"
|
|
|
4183
2151
|
provider: new RpcProvider2({
|
|
4184
2152
|
nodeUrl: rpcUrl,
|
|
4185
2153
|
blockIdentifier
|
|
2154
|
+
// specVersion
|
|
4186
2155
|
}),
|
|
4187
2156
|
stage: "production",
|
|
4188
2157
|
network: "mainnet" /* mainnet */
|
|
@@ -4212,7 +2181,7 @@ var getRiskExplaination = (riskType) => {
|
|
|
4212
2181
|
};
|
|
4213
2182
|
var getRiskColor = (risk) => {
|
|
4214
2183
|
const value = risk.value;
|
|
4215
|
-
if (value <=
|
|
2184
|
+
if (value <= 2) return "light_green_2";
|
|
4216
2185
|
if (value < 3) return "yellow";
|
|
4217
2186
|
return "red";
|
|
4218
2187
|
};
|
|
@@ -4240,8 +2209,18 @@ var VesuProtocol = {
|
|
|
4240
2209
|
name: "Vesu",
|
|
4241
2210
|
logo: "https://static-assets-8zct.onrender.com/integrations/vesu/logo.png"
|
|
4242
2211
|
};
|
|
2212
|
+
var EndurProtocol = {
|
|
2213
|
+
name: "Endur",
|
|
2214
|
+
logo: "http://endur.fi/logo.png"
|
|
2215
|
+
};
|
|
2216
|
+
var ExtendedProtocol = {
|
|
2217
|
+
name: "Extended",
|
|
2218
|
+
logo: "https://static-assets-8zct.onrender.com/integrations/extended/extended.svg"
|
|
2219
|
+
};
|
|
4243
2220
|
var Protocols = {
|
|
4244
|
-
VESU: VesuProtocol
|
|
2221
|
+
VESU: VesuProtocol,
|
|
2222
|
+
ENDUR: EndurProtocol,
|
|
2223
|
+
EXTENDED: ExtendedProtocol
|
|
4245
2224
|
};
|
|
4246
2225
|
|
|
4247
2226
|
// src/interfaces/initializable.ts
|
|
@@ -17938,7 +15917,7 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
|
|
|
17938
15917
|
quote.buyAmount.toString(),
|
|
17939
15918
|
tokenToBuyInfo.decimals
|
|
17940
15919
|
).multipliedBy(0.9999);
|
|
17941
|
-
const
|
|
15920
|
+
const output = await this.avnu.getSwapInfo(
|
|
17942
15921
|
quote,
|
|
17943
15922
|
this.address.address,
|
|
17944
15923
|
0,
|
|
@@ -17946,9 +15925,9 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
|
|
|
17946
15925
|
minAmountOut.toWei()
|
|
17947
15926
|
);
|
|
17948
15927
|
logger.verbose(
|
|
17949
|
-
`${_EkuboCLVault.name}: getSwapInfoToHandleUnused => swap info found: ${JSON.stringify(
|
|
15928
|
+
`${_EkuboCLVault.name}: getSwapInfoToHandleUnused => swap info found: ${JSON.stringify(output)}`
|
|
17950
15929
|
);
|
|
17951
|
-
return
|
|
15930
|
+
return output;
|
|
17952
15931
|
}
|
|
17953
15932
|
}
|
|
17954
15933
|
throw new Error("Failed to get swap info");
|
|
@@ -20503,10 +18482,10 @@ var SenseiStrategies = [
|
|
|
20503
18482
|
];
|
|
20504
18483
|
|
|
20505
18484
|
// src/strategies/universal-adapters/baseAdapter.ts
|
|
20506
|
-
import { hash
|
|
18485
|
+
import { hash, num as num6, shortString } from "starknet";
|
|
20507
18486
|
|
|
20508
18487
|
// src/strategies/universal-adapters/adapter-utils.ts
|
|
20509
|
-
var SIMPLE_SANITIZER = ContractAddr.from("
|
|
18488
|
+
var SIMPLE_SANITIZER = ContractAddr.from("0x3798dc4f83fdfad199e5236e3656cf2fb79bc50c00504d0dd41522e0f042072");
|
|
20510
18489
|
function toBigInt(value) {
|
|
20511
18490
|
if (typeof value === "string") {
|
|
20512
18491
|
return BigInt(value);
|
|
@@ -20529,7 +18508,7 @@ var BaseAdapter = class extends CacheClass {
|
|
|
20529
18508
|
// sanitizer address
|
|
20530
18509
|
target.toBigInt(),
|
|
20531
18510
|
// contract
|
|
20532
|
-
toBigInt(
|
|
18511
|
+
toBigInt(hash.getSelectorFromName(method)),
|
|
20533
18512
|
// method name
|
|
20534
18513
|
BigInt(packedArguments.length),
|
|
20535
18514
|
...packedArguments
|
|
@@ -20539,7 +18518,7 @@ var BaseAdapter = class extends CacheClass {
|
|
|
20539
18518
|
};
|
|
20540
18519
|
|
|
20541
18520
|
// src/strategies/universal-adapters/common-adapter.ts
|
|
20542
|
-
import { hash as
|
|
18521
|
+
import { hash as hash2, uint256 as uint2566 } from "starknet";
|
|
20543
18522
|
var CommonAdapter = class extends BaseAdapter {
|
|
20544
18523
|
constructor(config) {
|
|
20545
18524
|
super();
|
|
@@ -20569,7 +18548,7 @@ var CommonAdapter = class extends BaseAdapter {
|
|
|
20569
18548
|
sanitizer: SIMPLE_SANITIZER,
|
|
20570
18549
|
call: {
|
|
20571
18550
|
contractAddress: this.config.manager,
|
|
20572
|
-
selector:
|
|
18551
|
+
selector: hash2.getSelectorFromName("flash_loan"),
|
|
20573
18552
|
calldata: [
|
|
20574
18553
|
this.config.manager.toBigInt(),
|
|
20575
18554
|
// receiver
|
|
@@ -20608,7 +18587,7 @@ var CommonAdapter = class extends BaseAdapter {
|
|
|
20608
18587
|
sanitizer: SIMPLE_SANITIZER,
|
|
20609
18588
|
call: {
|
|
20610
18589
|
contractAddress: token,
|
|
20611
|
-
selector:
|
|
18590
|
+
selector: hash2.getSelectorFromName("approve"),
|
|
20612
18591
|
calldata: [
|
|
20613
18592
|
spender.toBigInt(),
|
|
20614
18593
|
// spender
|
|
@@ -20621,10 +18600,39 @@ var CommonAdapter = class extends BaseAdapter {
|
|
|
20621
18600
|
};
|
|
20622
18601
|
};
|
|
20623
18602
|
}
|
|
18603
|
+
getBringLiquidityAdapter(id) {
|
|
18604
|
+
return () => ({
|
|
18605
|
+
leaf: this.constructSimpleLeafData({
|
|
18606
|
+
id,
|
|
18607
|
+
target: this.config.vaultAddress,
|
|
18608
|
+
method: "bring_liquidity",
|
|
18609
|
+
packedArguments: []
|
|
18610
|
+
}),
|
|
18611
|
+
callConstructor: this.getBringLiquidityCall().bind(this)
|
|
18612
|
+
});
|
|
18613
|
+
}
|
|
18614
|
+
getBringLiquidityCall() {
|
|
18615
|
+
return (params) => {
|
|
18616
|
+
const uint256Amount = uint2566.bnToUint256(params.amount.toWei());
|
|
18617
|
+
return {
|
|
18618
|
+
sanitizer: SIMPLE_SANITIZER,
|
|
18619
|
+
call: {
|
|
18620
|
+
contractAddress: this.config.vaultAddress,
|
|
18621
|
+
selector: hash2.getSelectorFromName("bring_liquidity"),
|
|
18622
|
+
calldata: [
|
|
18623
|
+
toBigInt(uint256Amount.low.toString()),
|
|
18624
|
+
// amount low
|
|
18625
|
+
toBigInt(uint256Amount.high.toString())
|
|
18626
|
+
// amount high
|
|
18627
|
+
]
|
|
18628
|
+
}
|
|
18629
|
+
};
|
|
18630
|
+
};
|
|
18631
|
+
}
|
|
20624
18632
|
};
|
|
20625
18633
|
|
|
20626
18634
|
// src/strategies/universal-adapters/vesu-adapter.ts
|
|
20627
|
-
import { CairoCustomEnum as CairoCustomEnum2, Contract as Contract8, hash as
|
|
18635
|
+
import { CairoCustomEnum as CairoCustomEnum2, Contract as Contract8, hash as hash3, RpcProvider as RpcProvider4, uint256 as uint2567 } from "starknet";
|
|
20628
18636
|
|
|
20629
18637
|
// src/data/vesu-singleton.abi.json
|
|
20630
18638
|
var vesu_singleton_abi_default = [
|
|
@@ -22907,13 +20915,13 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
22907
20915
|
toBigInt(positionData.length),
|
|
22908
20916
|
...positionData
|
|
22909
20917
|
];
|
|
22910
|
-
const
|
|
20918
|
+
const output = this.constructSimpleLeafData({
|
|
22911
20919
|
id: this.config.id,
|
|
22912
20920
|
target: this.VESU_SINGLETON,
|
|
22913
20921
|
method: "modify_position",
|
|
22914
20922
|
packedArguments
|
|
22915
20923
|
});
|
|
22916
|
-
return { leaf:
|
|
20924
|
+
return { leaf: output, callConstructor: this.getModifyPositionCall.bind(this) };
|
|
22917
20925
|
};
|
|
22918
20926
|
this.getModifyPositionCall = (params) => {
|
|
22919
20927
|
const _collateral = {
|
|
@@ -22950,7 +20958,7 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
22950
20958
|
sanitizer: SIMPLE_SANITIZER,
|
|
22951
20959
|
call: {
|
|
22952
20960
|
contractAddress: this.VESU_SINGLETON,
|
|
22953
|
-
selector:
|
|
20961
|
+
selector: hash3.getSelectorFromName("modify_position"),
|
|
22954
20962
|
calldata: [
|
|
22955
20963
|
...call.calldata
|
|
22956
20964
|
]
|
|
@@ -23006,8 +21014,8 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
23006
21014
|
if (cacheData) {
|
|
23007
21015
|
return cacheData;
|
|
23008
21016
|
}
|
|
23009
|
-
const
|
|
23010
|
-
this.setCache(CACHE_KEY, Number(
|
|
21017
|
+
const output = await this.getVesuSingletonContract(config).call("ltv_config", [this.config.poolId.address, this.config.collateral.address.address, this.config.debt.address.address]);
|
|
21018
|
+
this.setCache(CACHE_KEY, Number(output.max_ltv) / 1e18, 3e5);
|
|
23011
21019
|
return this.getCache(CACHE_KEY);
|
|
23012
21020
|
}
|
|
23013
21021
|
async getPositions(config) {
|
|
@@ -23019,7 +21027,7 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
23019
21027
|
if (cacheData) {
|
|
23020
21028
|
return cacheData;
|
|
23021
21029
|
}
|
|
23022
|
-
const
|
|
21030
|
+
const output = await this.getVesuSingletonContract(config).call("position_unsafe", [
|
|
23023
21031
|
this.config.poolId.address,
|
|
23024
21032
|
this.config.collateral.address.address,
|
|
23025
21033
|
this.config.debt.address.address,
|
|
@@ -23027,8 +21035,8 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
23027
21035
|
]);
|
|
23028
21036
|
const token1Price = await this.pricer.getPrice(this.config.collateral.symbol);
|
|
23029
21037
|
const token2Price = await this.pricer.getPrice(this.config.debt.symbol);
|
|
23030
|
-
const collateralAmount = Web3Number.fromWei(
|
|
23031
|
-
const debtAmount = Web3Number.fromWei(
|
|
21038
|
+
const collateralAmount = Web3Number.fromWei(output["1"].toString(), this.config.collateral.decimals);
|
|
21039
|
+
const debtAmount = Web3Number.fromWei(output["2"].toString(), this.config.debt.decimals);
|
|
23032
21040
|
const value = [{
|
|
23033
21041
|
amount: collateralAmount,
|
|
23034
21042
|
token: this.config.collateral,
|
|
@@ -23052,14 +21060,14 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
23052
21060
|
if (cacheData) {
|
|
23053
21061
|
return cacheData;
|
|
23054
21062
|
}
|
|
23055
|
-
const
|
|
21063
|
+
const output = await this.getVesuSingletonContract(config).call("check_collateralization_unsafe", [
|
|
23056
21064
|
this.config.poolId.address,
|
|
23057
21065
|
this.config.collateral.address.address,
|
|
23058
21066
|
this.config.debt.address.address,
|
|
23059
21067
|
this.config.vaultAllocator.address
|
|
23060
21068
|
]);
|
|
23061
|
-
const collateralAmount = Web3Number.fromWei(
|
|
23062
|
-
const debtAmount = Web3Number.fromWei(
|
|
21069
|
+
const collateralAmount = Web3Number.fromWei(output["1"].toString(), 18);
|
|
21070
|
+
const debtAmount = Web3Number.fromWei(output["2"].toString(), 18);
|
|
23063
21071
|
const value = [{
|
|
23064
21072
|
token: this.config.collateral,
|
|
23065
21073
|
usdValue: collateralAmount.toNumber(),
|
|
@@ -23076,8 +21084,8 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
23076
21084
|
const collateralizationProm = this.getCollateralization(this.networkConfig);
|
|
23077
21085
|
const positionsProm = this.getPositions(this.networkConfig);
|
|
23078
21086
|
const ltvProm = this.getLTVConfig(this.networkConfig);
|
|
23079
|
-
const
|
|
23080
|
-
const [collateralization, positions, ltv] =
|
|
21087
|
+
const output = await Promise.all([collateralizationProm, positionsProm, ltvProm]);
|
|
21088
|
+
const [collateralization, positions, ltv] = output;
|
|
23081
21089
|
const collateralTokenAmount = positions[0].amount;
|
|
23082
21090
|
const collateralUSDAmount = collateralization[0].usdValue;
|
|
23083
21091
|
const collateralPrice = collateralUSDAmount / collateralTokenAmount.toNumber();
|
|
@@ -23138,7 +21146,7 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
23138
21146
|
}
|
|
23139
21147
|
};
|
|
23140
21148
|
|
|
23141
|
-
// src/strategies/universal-strategy.
|
|
21149
|
+
// src/strategies/universal-strategy.tsx
|
|
23142
21150
|
import { CallData, Contract as Contract9, num as num9, uint256 as uint2568 } from "starknet";
|
|
23143
21151
|
|
|
23144
21152
|
// src/data/universal-vault.abi.json
|
|
@@ -25344,7 +23352,13 @@ var vault_manager_abi_default = [
|
|
|
25344
23352
|
}
|
|
25345
23353
|
];
|
|
25346
23354
|
|
|
25347
|
-
// src/strategies/universal-strategy.
|
|
23355
|
+
// src/strategies/universal-strategy.tsx
|
|
23356
|
+
import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
23357
|
+
var AUMTypes = /* @__PURE__ */ ((AUMTypes2) => {
|
|
23358
|
+
AUMTypes2["FINALISED"] = "finalised";
|
|
23359
|
+
AUMTypes2["DEFISPRING"] = "defispring";
|
|
23360
|
+
return AUMTypes2;
|
|
23361
|
+
})(AUMTypes || {});
|
|
25348
23362
|
var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
25349
23363
|
constructor(config, pricer, metadata) {
|
|
25350
23364
|
super(config);
|
|
@@ -25429,6 +23443,19 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25429
23443
|
]);
|
|
25430
23444
|
return [call1, call2];
|
|
25431
23445
|
}
|
|
23446
|
+
async withdrawCall(amountInfo, receiver, owner) {
|
|
23447
|
+
assert(
|
|
23448
|
+
amountInfo.tokenInfo.address.eq(this.asset().address),
|
|
23449
|
+
"Withdraw token mismatch"
|
|
23450
|
+
);
|
|
23451
|
+
const shares = await this.contract.call("convert_to_shares", [uint2568.bnToUint256(amountInfo.amount.toWei())]);
|
|
23452
|
+
const call = this.contract.populate("request_redeem", [
|
|
23453
|
+
uint2568.bnToUint256(shares.toString()),
|
|
23454
|
+
receiver.address,
|
|
23455
|
+
owner.address
|
|
23456
|
+
]);
|
|
23457
|
+
return [call];
|
|
23458
|
+
}
|
|
25432
23459
|
/**
|
|
25433
23460
|
* Calculates the Total Value Locked (TVL) for a specific user.
|
|
25434
23461
|
* @param user - Address of the user
|
|
@@ -25475,14 +23502,24 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25475
23502
|
const collateral2APY = Number(collateralAsset2.supplyApy.value) / 1e18;
|
|
25476
23503
|
const debt2APY = Number(debtAsset2.borrowApr.value) / 1e18;
|
|
25477
23504
|
const positions = await this.getVaultPositions();
|
|
23505
|
+
logger.verbose(`${this.metadata.name}::netAPY: positions: ${JSON.stringify(positions)}`);
|
|
23506
|
+
if (positions.every((p) => p.amount.isZero())) {
|
|
23507
|
+
return { net: 0, splits: [{
|
|
23508
|
+
apy: 0,
|
|
23509
|
+
id: "base"
|
|
23510
|
+
}, {
|
|
23511
|
+
apy: 0,
|
|
23512
|
+
id: "defispring"
|
|
23513
|
+
}] };
|
|
23514
|
+
}
|
|
25478
23515
|
const weights = positions.map((p, index) => p.usdValue * (index % 2 == 0 ? 1 : -1));
|
|
25479
23516
|
const baseAPYs = [collateral1APY, debt1APY, collateral2APY, debt2APY];
|
|
25480
23517
|
assert(positions.length == baseAPYs.length, "Positions and APYs length mismatch");
|
|
25481
23518
|
const rewardAPYs = [Number(collateralAsset1.defiSpringSupplyApr.value) / 1e18, 0, Number(collateralAsset2.defiSpringSupplyApr.value) / 1e18, 0];
|
|
25482
23519
|
const baseAPY = this.computeAPY(baseAPYs, weights);
|
|
25483
23520
|
const rewardAPY = this.computeAPY(rewardAPYs, weights);
|
|
25484
|
-
const apys = [...baseAPYs, ...rewardAPYs];
|
|
25485
23521
|
const netAPY = baseAPY + rewardAPY;
|
|
23522
|
+
logger.verbose(`${this.metadata.name}::netAPY: net: ${netAPY}, baseAPY: ${baseAPY}, rewardAPY: ${rewardAPY}`);
|
|
25486
23523
|
return { net: netAPY, splits: [{
|
|
25487
23524
|
apy: baseAPY,
|
|
25488
23525
|
id: "base"
|
|
@@ -25517,6 +23554,16 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25517
23554
|
usdValue
|
|
25518
23555
|
};
|
|
25519
23556
|
}
|
|
23557
|
+
async getUnusedBalance() {
|
|
23558
|
+
const balance = await new ERC20(this.config).balanceOf(this.asset().address, this.metadata.additionalInfo.vaultAllocator, this.asset().decimals);
|
|
23559
|
+
const price = await this.pricer.getPrice(this.metadata.depositTokens[0].symbol);
|
|
23560
|
+
const usdValue = Number(balance.toFixed(6)) * price.price;
|
|
23561
|
+
return {
|
|
23562
|
+
tokenInfo: this.asset(),
|
|
23563
|
+
amount: balance,
|
|
23564
|
+
usdValue
|
|
23565
|
+
};
|
|
23566
|
+
}
|
|
25520
23567
|
async getAUM() {
|
|
25521
23568
|
const currentAUM = await this.contract.call("aum", []);
|
|
25522
23569
|
const lastReportTime = await this.contract.call("last_report_timestamp", []);
|
|
@@ -25524,16 +23571,33 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25524
23571
|
const [vesuAdapter1, vesuAdapter2] = this.getVesuAdapters();
|
|
25525
23572
|
const leg1AUM = await vesuAdapter1.getPositions(this.config);
|
|
25526
23573
|
const leg2AUM = await vesuAdapter2.getPositions(this.config);
|
|
25527
|
-
const balance = await
|
|
25528
|
-
logger.verbose(`${this.getTag()} unused balance: ${balance}`);
|
|
25529
|
-
const
|
|
23574
|
+
const balance = await this.getUnusedBalance();
|
|
23575
|
+
logger.verbose(`${this.getTag()} unused balance: ${balance.amount.toNumber()}`);
|
|
23576
|
+
const vesuAum = leg1AUM[0].amount.plus(leg2AUM[0].usdValue / token1Price.price).minus(leg1AUM[1].usdValue / token1Price.price).minus(leg2AUM[1].amount);
|
|
23577
|
+
logger.verbose(`${this.getTag()} Vesu AUM: leg1: ${leg1AUM[0].amount.toNumber()}, ${leg1AUM[1].amount.toNumber()}, leg2: ${leg2AUM[0].amount.toNumber()}, ${leg2AUM[1].amount.toNumber()}`);
|
|
23578
|
+
const zeroAmt = Web3Number.fromWei("0", this.asset().decimals);
|
|
23579
|
+
const net = {
|
|
23580
|
+
tokenInfo: this.asset(),
|
|
23581
|
+
amount: zeroAmt,
|
|
23582
|
+
usdValue: 0
|
|
23583
|
+
};
|
|
23584
|
+
const prevAum = Web3Number.fromWei(currentAUM.toString(), this.asset().decimals);
|
|
23585
|
+
if (vesuAum.isZero()) {
|
|
23586
|
+
return { net, splits: [{
|
|
23587
|
+
aum: zeroAmt,
|
|
23588
|
+
id: "finalised" /* FINALISED */
|
|
23589
|
+
}, {
|
|
23590
|
+
aum: zeroAmt,
|
|
23591
|
+
id: "defispring" /* DEFISPRING */
|
|
23592
|
+
}], prevAum };
|
|
23593
|
+
}
|
|
23594
|
+
const aumToken = vesuAum.plus(balance.amount);
|
|
25530
23595
|
logger.verbose(`${this.getTag()} Actual AUM: ${aumToken}`);
|
|
25531
23596
|
const netAPY = await this.netAPY();
|
|
25532
23597
|
const defispringAPY = netAPY.splits.find((s) => s.id === "defispring")?.apy || 0;
|
|
25533
23598
|
if (!defispringAPY) throw new Error("DefiSpring APY not found");
|
|
25534
23599
|
const timeDiff = Math.round(Date.now() / 1e3) - Number(lastReportTime);
|
|
25535
23600
|
const growthRate = timeDiff * defispringAPY / (365 * 24 * 60 * 60);
|
|
25536
|
-
const prevAum = Web3Number.fromWei(currentAUM.toString(), this.asset().decimals);
|
|
25537
23601
|
const rewardAssets = prevAum.multipliedBy(growthRate);
|
|
25538
23602
|
logger.verbose(`${this.getTag()} DefiSpring AUM time difference: ${timeDiff}`);
|
|
25539
23603
|
logger.verbose(`${this.getTag()} Current AUM: ${currentAUM}`);
|
|
@@ -25541,16 +23605,13 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25541
23605
|
logger.verbose(`${this.getTag()} rewards AUM: ${rewardAssets}`);
|
|
25542
23606
|
const newAUM = aumToken.plus(rewardAssets);
|
|
25543
23607
|
logger.verbose(`${this.getTag()} New AUM: ${newAUM}`);
|
|
25544
|
-
|
|
25545
|
-
|
|
25546
|
-
amount: newAUM,
|
|
25547
|
-
usdValue: newAUM.multipliedBy(token1Price.price).toNumber()
|
|
25548
|
-
};
|
|
23608
|
+
net.amount = newAUM;
|
|
23609
|
+
net.usdValue = newAUM.multipliedBy(token1Price.price).toNumber();
|
|
25549
23610
|
const splits = [{
|
|
25550
|
-
id: "finalised"
|
|
23611
|
+
id: "finalised" /* FINALISED */,
|
|
25551
23612
|
aum: aumToken
|
|
25552
23613
|
}, {
|
|
25553
|
-
id: "defispring"
|
|
23614
|
+
id: "defispring" /* DEFISPRING */,
|
|
25554
23615
|
aum: rewardAssets
|
|
25555
23616
|
}];
|
|
25556
23617
|
return { net, splits, prevAum };
|
|
@@ -25601,19 +23662,19 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25601
23662
|
debtAmount: params.debtAmount,
|
|
25602
23663
|
isBorrow: params.isDeposit
|
|
25603
23664
|
}));
|
|
25604
|
-
const
|
|
23665
|
+
const output = [{
|
|
25605
23666
|
proofs: manage5Info.proofs,
|
|
25606
23667
|
manageCall: manageCall5,
|
|
25607
23668
|
step: STEP2_ID
|
|
25608
23669
|
}];
|
|
25609
23670
|
if (approveAmount.gt(0)) {
|
|
25610
|
-
|
|
23671
|
+
output.unshift({
|
|
25611
23672
|
proofs: manage4Info.proofs,
|
|
25612
23673
|
manageCall: manageCall4,
|
|
25613
23674
|
step: STEP1_ID
|
|
25614
23675
|
});
|
|
25615
23676
|
}
|
|
25616
|
-
return
|
|
23677
|
+
return output;
|
|
25617
23678
|
}
|
|
25618
23679
|
getTag() {
|
|
25619
23680
|
return `${_UniversalStrategy.name}:${this.metadata.name}`;
|
|
@@ -25730,18 +23791,23 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25730
23791
|
logger.verbose(`${this.getTag()}:: leg1DepositAmount: ${params.leg1DepositAmount.toString()} ${vesuAdapter1.config.collateral.symbol}`);
|
|
25731
23792
|
logger.verbose(`${this.getTag()}:: borrow1Amount: ${borrow1Amount.toString()} ${vesuAdapter1.config.debt.symbol}`);
|
|
25732
23793
|
logger.verbose(`${this.getTag()}:: borrow2Amount: ${borrow2Amount.toString()} ${vesuAdapter2.config.debt.symbol}`);
|
|
25733
|
-
|
|
23794
|
+
let callSet1 = this.getVesuModifyPositionCalls({
|
|
25734
23795
|
isLeg1: true,
|
|
25735
23796
|
isDeposit: params.isDeposit,
|
|
25736
23797
|
depositAmount: params.leg1DepositAmount.plus(borrow2Amount),
|
|
25737
23798
|
debtAmount: borrow1Amount
|
|
25738
23799
|
});
|
|
25739
|
-
|
|
23800
|
+
let callSet2 = this.getVesuModifyPositionCalls({
|
|
25740
23801
|
isLeg1: false,
|
|
25741
23802
|
isDeposit: params.isDeposit,
|
|
25742
23803
|
depositAmount: borrow1Amount,
|
|
25743
23804
|
debtAmount: borrow2Amount
|
|
25744
23805
|
});
|
|
23806
|
+
if (!params.isDeposit) {
|
|
23807
|
+
let temp = callSet2;
|
|
23808
|
+
callSet2 = [...callSet1];
|
|
23809
|
+
callSet1 = [...temp];
|
|
23810
|
+
}
|
|
25745
23811
|
const allActions = [...callSet1.map((i) => i.manageCall), ...callSet2.map((i) => i.manageCall)];
|
|
25746
23812
|
const flashloanCalldata = CallData.compile([
|
|
25747
23813
|
[...callSet1.map((i) => i.proofs), ...callSet2.map((i) => i.proofs)],
|
|
@@ -25759,6 +23825,18 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25759
23825
|
const manageCall = this.getManageCall(["flash_loan_init" /* FLASH_LOAN */], [manageCall1]);
|
|
25760
23826
|
return manageCall;
|
|
25761
23827
|
}
|
|
23828
|
+
async getBringLiquidityCall(params) {
|
|
23829
|
+
const manage1Info = this.getProofs("approve_bring_liquidity" /* APPROVE_BRING_LIQUIDITY */);
|
|
23830
|
+
const manageCall1 = manage1Info.callConstructor({
|
|
23831
|
+
amount: params.amount
|
|
23832
|
+
});
|
|
23833
|
+
const manage2Info = this.getProofs("bring_liquidity" /* BRING_LIQUIDITY */);
|
|
23834
|
+
const manageCall2 = manage2Info.callConstructor({
|
|
23835
|
+
amount: params.amount
|
|
23836
|
+
});
|
|
23837
|
+
const manageCall = this.getManageCall(["approve_bring_liquidity" /* APPROVE_BRING_LIQUIDITY */, "bring_liquidity" /* BRING_LIQUIDITY */], [manageCall1, manageCall2]);
|
|
23838
|
+
return manageCall;
|
|
23839
|
+
}
|
|
25762
23840
|
async getRebalanceCall(params) {
|
|
25763
23841
|
let callSet1 = this.getVesuModifyPositionCalls({
|
|
25764
23842
|
isLeg1: true,
|
|
@@ -25793,6 +23871,8 @@ var UNIVERSAL_MANAGE_IDS = /* @__PURE__ */ ((UNIVERSAL_MANAGE_IDS2) => {
|
|
|
25793
23871
|
UNIVERSAL_MANAGE_IDS2["VESU_LEG2"] = "vesu_leg2";
|
|
25794
23872
|
UNIVERSAL_MANAGE_IDS2["APPROVE_TOKEN1"] = "approve_token1";
|
|
25795
23873
|
UNIVERSAL_MANAGE_IDS2["APPROVE_TOKEN2"] = "approve_token2";
|
|
23874
|
+
UNIVERSAL_MANAGE_IDS2["APPROVE_BRING_LIQUIDITY"] = "approve_bring_liquidity";
|
|
23875
|
+
UNIVERSAL_MANAGE_IDS2["BRING_LIQUIDITY"] = "bring_liquidity";
|
|
25796
23876
|
return UNIVERSAL_MANAGE_IDS2;
|
|
25797
23877
|
})(UNIVERSAL_MANAGE_IDS || {});
|
|
25798
23878
|
var UNIVERSAL_ADAPTERS = /* @__PURE__ */ ((UNIVERSAL_ADAPTERS2) => {
|
|
@@ -25807,7 +23887,9 @@ function getLooperSettings(token1Symbol, token2Symbol, vaultSettings, pool1, poo
|
|
|
25807
23887
|
const commonAdapter = new CommonAdapter({
|
|
25808
23888
|
manager: vaultSettings.manager,
|
|
25809
23889
|
asset: USDCToken.address,
|
|
25810
|
-
id: "flash_loan_init" /* FLASH_LOAN
|
|
23890
|
+
id: "flash_loan_init" /* FLASH_LOAN */,
|
|
23891
|
+
vaultAddress: vaultSettings.vaultAddress,
|
|
23892
|
+
vaultAllocator: vaultSettings.vaultAllocator
|
|
25811
23893
|
});
|
|
25812
23894
|
const vesuAdapterUSDCETH = new VesuAdapter({
|
|
25813
23895
|
poolId: pool1,
|
|
@@ -25838,13 +23920,17 @@ function getLooperSettings(token1Symbol, token2Symbol, vaultSettings, pool1, poo
|
|
|
25838
23920
|
vaultSettings.leafAdapters.push(vesuAdapterETHUSDC.getModifyPosition.bind(vesuAdapterETHUSDC));
|
|
25839
23921
|
vaultSettings.leafAdapters.push(commonAdapter.getApproveAdapter(USDCToken.address, vesuAdapterUSDCETH.VESU_SINGLETON, "approve_token1" /* APPROVE_TOKEN1 */).bind(commonAdapter));
|
|
25840
23922
|
vaultSettings.leafAdapters.push(commonAdapter.getApproveAdapter(ETHToken.address, vesuAdapterETHUSDC.VESU_SINGLETON, "approve_token2" /* APPROVE_TOKEN2 */).bind(commonAdapter));
|
|
23923
|
+
vaultSettings.leafAdapters.push(commonAdapter.getApproveAdapter(USDCToken.address, vaultSettings.vaultAddress, "approve_bring_liquidity" /* APPROVE_BRING_LIQUIDITY */).bind(commonAdapter));
|
|
23924
|
+
vaultSettings.leafAdapters.push(commonAdapter.getBringLiquidityAdapter("bring_liquidity" /* BRING_LIQUIDITY */).bind(commonAdapter));
|
|
25841
23925
|
return vaultSettings;
|
|
25842
23926
|
}
|
|
25843
23927
|
var _riskFactor4 = [
|
|
25844
23928
|
{ type: "Smart Contract Risk" /* SMART_CONTRACT_RISK */, value: 0.5, weight: 25, reason: "Audited by Zellic" },
|
|
25845
|
-
{ type: "Liquidation Risk" /* LIQUIDATION_RISK */, value: 1, weight: 50, reason: "Liquidation risk is mitigated
|
|
23929
|
+
{ type: "Liquidation Risk" /* LIQUIDATION_RISK */, value: 1.5, weight: 50, reason: "Liquidation risk is mitigated by stable price feed on Starknet" },
|
|
23930
|
+
{ type: "Technical Risk" /* TECHNICAL_RISK */, value: 1, weight: 50, reason: "Technical failures like risk monitoring failures" }
|
|
25846
23931
|
];
|
|
25847
23932
|
var usdcVaultSettings = {
|
|
23933
|
+
vaultAddress: ContractAddr.from("0x7e6498cf6a1bfc7e6fc89f1831865e2dacb9756def4ec4b031a9138788a3b5e"),
|
|
25848
23934
|
manager: ContractAddr.from("0xf41a2b1f498a7f9629db0b8519259e66e964260a23d20003f3e42bb1997a07"),
|
|
25849
23935
|
vaultAllocator: ContractAddr.from("0x228cca1005d3f2b55cbaba27cb291dacf1b9a92d1d6b1638195fbd3d0c1e3ba"),
|
|
25850
23936
|
redeemRequestNFT: ContractAddr.from("0x906d03590010868cbf7590ad47043959d7af8e782089a605d9b22567b64fda"),
|
|
@@ -25855,6 +23941,7 @@ var usdcVaultSettings = {
|
|
|
25855
23941
|
minHealthFactor: 1.25
|
|
25856
23942
|
};
|
|
25857
23943
|
var wbtcVaultSettings = {
|
|
23944
|
+
vaultAddress: ContractAddr.from("0x5a4c1651b913aa2ea7afd9024911603152a19058624c3e425405370d62bf80c"),
|
|
25858
23945
|
manager: ContractAddr.from("0xef8a664ffcfe46a6af550766d27c28937bf1b77fb4ab54d8553e92bca5ba34"),
|
|
25859
23946
|
vaultAllocator: ContractAddr.from("0x1e01c25f0d9494570226ad28a7fa856c0640505e809c366a9fab4903320e735"),
|
|
25860
23947
|
redeemRequestNFT: ContractAddr.from("0x4fec59a12f8424281c1e65a80b5de51b4e754625c60cddfcd00d46941ec37b2"),
|
|
@@ -25864,10 +23951,176 @@ var wbtcVaultSettings = {
|
|
|
25864
23951
|
targetHealthFactor: 1.3,
|
|
25865
23952
|
minHealthFactor: 1.25
|
|
25866
23953
|
};
|
|
23954
|
+
var ethVaultSettings = {
|
|
23955
|
+
vaultAddress: ContractAddr.from("0x446c22d4d3f5cb52b4950ba832ba1df99464c6673a37c092b1d9622650dbd8"),
|
|
23956
|
+
manager: ContractAddr.from("0x494888b37206616bd09d759dcda61e5118470b9aa7f58fb84f21c778a7b8f97"),
|
|
23957
|
+
vaultAllocator: ContractAddr.from("0x4acc0ad6bea58cb578d60ff7c31f06f44369a7a9a7bbfffe4701f143e427bd"),
|
|
23958
|
+
redeemRequestNFT: ContractAddr.from("0x2e6cd71e5060a254d4db00655e420db7bf89da7755bb0d5f922e2f00c76ac49"),
|
|
23959
|
+
aumOracle: ContractAddr.from("0x4b747f2e75c057bed9aa2ce46fbdc2159dc684c15bd32d4f95983a6ecf39a05"),
|
|
23960
|
+
leafAdapters: [],
|
|
23961
|
+
adapters: [],
|
|
23962
|
+
targetHealthFactor: 1.3,
|
|
23963
|
+
minHealthFactor: 1.25
|
|
23964
|
+
};
|
|
23965
|
+
var strkVaultSettings = {
|
|
23966
|
+
vaultAddress: ContractAddr.from("0x55d012f57e58c96e0a5c7ebbe55853989d01e6538b15a95e7178aca4af05c21"),
|
|
23967
|
+
manager: ContractAddr.from("0xcc6a5153ca56293405506eb20826a379d982cd738008ef7e808454d318fb81"),
|
|
23968
|
+
vaultAllocator: ContractAddr.from("0xf29d2f82e896c0ed74c9eff220af34ac148e8b99846d1ace9fbb02c9191d01"),
|
|
23969
|
+
redeemRequestNFT: ContractAddr.from("0x46902423bd632c428376b84fcee9cac5dbe016214e93a8103bcbde6e1de656b"),
|
|
23970
|
+
aumOracle: ContractAddr.from("0x6d7dbfad4bb51715da211468389a623da00c0625f8f6efbea822ee5ac5231f4"),
|
|
23971
|
+
leafAdapters: [],
|
|
23972
|
+
adapters: [],
|
|
23973
|
+
targetHealthFactor: 1.3,
|
|
23974
|
+
minHealthFactor: 1.25
|
|
23975
|
+
};
|
|
23976
|
+
var usdtVaultSettings = {
|
|
23977
|
+
vaultAddress: ContractAddr.from("0x1c4933d1880c6778585e597154eaca7b428579d72f3aae425ad2e4d26c6bb3"),
|
|
23978
|
+
manager: ContractAddr.from("0x39bb9843503799b552b7ed84b31c06e4ff10c0537edcddfbf01fe944b864029"),
|
|
23979
|
+
vaultAllocator: ContractAddr.from("0x56437d18c43727ac971f6c7086031cad7d9d6ccb340f4f3785a74cc791c931a"),
|
|
23980
|
+
redeemRequestNFT: ContractAddr.from("0x5af0c2a657eaa8e23ed78e855dac0c51e4f69e2cf91a18c472041a1f75bb41f"),
|
|
23981
|
+
aumOracle: ContractAddr.from("0x7018f8040c8066a4ab929e6760ae52dd43b6a3a289172f514750a61fcc565cc"),
|
|
23982
|
+
leafAdapters: [],
|
|
23983
|
+
adapters: [],
|
|
23984
|
+
targetHealthFactor: 1.3,
|
|
23985
|
+
minHealthFactor: 1.25
|
|
23986
|
+
};
|
|
23987
|
+
function MetaVaultDescription() {
|
|
23988
|
+
const logos = {
|
|
23989
|
+
vesu: Protocols.VESU.logo,
|
|
23990
|
+
endur: Protocols.ENDUR.logo,
|
|
23991
|
+
extended: Protocols.EXTENDED.logo,
|
|
23992
|
+
ekubo: "https://dummyimage.com/64x64/ffffff/000000&text=K"
|
|
23993
|
+
};
|
|
23994
|
+
const sources = [
|
|
23995
|
+
{ key: "vesu", name: "Vesu", status: "Live", description: "Integrated liquidity venue used for automated routing to capture the best available yield." },
|
|
23996
|
+
{ key: "endur", name: "Endur", status: "Coming soon", description: "Planned integration to tap into STRK staking\u2013backed yields via our LST pipeline." },
|
|
23997
|
+
{ key: "extended", name: "Extended", status: "Coming soon", description: "Expanding coverage to additional money markets and vaults across the ecosystem." }
|
|
23998
|
+
// { key: "ekubo", name: "Ekubo", status: "Coming soon", description: "Concentrated liquidity strategies targeted for optimized fee APR harvesting." },
|
|
23999
|
+
];
|
|
24000
|
+
const containerStyle = {
|
|
24001
|
+
maxWidth: "800px",
|
|
24002
|
+
margin: "0 auto",
|
|
24003
|
+
backgroundColor: "#111",
|
|
24004
|
+
color: "#eee",
|
|
24005
|
+
fontFamily: "Arial, sans-serif",
|
|
24006
|
+
borderRadius: "12px"
|
|
24007
|
+
};
|
|
24008
|
+
const cardStyle = {
|
|
24009
|
+
border: "1px solid #333",
|
|
24010
|
+
borderRadius: "10px",
|
|
24011
|
+
padding: "12px",
|
|
24012
|
+
marginBottom: "12px",
|
|
24013
|
+
backgroundColor: "#1a1a1a"
|
|
24014
|
+
};
|
|
24015
|
+
const logoStyle = {
|
|
24016
|
+
width: "24px",
|
|
24017
|
+
height: "24px",
|
|
24018
|
+
borderRadius: "8px",
|
|
24019
|
+
border: "1px solid #444",
|
|
24020
|
+
backgroundColor: "#222"
|
|
24021
|
+
};
|
|
24022
|
+
return /* @__PURE__ */ jsxs3("div", { style: containerStyle, children: [
|
|
24023
|
+
/* @__PURE__ */ jsx4("h1", { style: { fontSize: "18px", marginBottom: "10px" }, children: "Meta Vault \u2014 Automated Yield Router" }),
|
|
24024
|
+
/* @__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." }),
|
|
24025
|
+
/* @__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: [
|
|
24026
|
+
/* @__PURE__ */ jsx4("strong", { children: "Withdrawals:" }),
|
|
24027
|
+
" Requests can take up to ",
|
|
24028
|
+
/* @__PURE__ */ jsx4("strong", { children: "1-2 hours" }),
|
|
24029
|
+
" to process as the vault unwinds and settles routing."
|
|
24030
|
+
] }) }),
|
|
24031
|
+
/* @__PURE__ */ jsx4("h2", { style: { fontSize: "18px", marginBottom: "10px" }, children: "Supported Yield Sources" }),
|
|
24032
|
+
sources.map((s) => /* @__PURE__ */ jsx4("div", { style: cardStyle, children: /* @__PURE__ */ jsxs3("div", { style: { display: "flex", gap: "10px", alignItems: "flex-start" }, children: [
|
|
24033
|
+
/* @__PURE__ */ jsx4("img", { src: logos[s.key], alt: `${s.name} logo`, style: logoStyle }),
|
|
24034
|
+
/* @__PURE__ */ jsx4("div", { style: { flex: 1 }, children: /* @__PURE__ */ jsxs3("div", { style: { display: "flex", justifyContent: "space-between", alignItems: "center" }, children: [
|
|
24035
|
+
/* @__PURE__ */ jsx4("h3", { style: { fontSize: "15px", margin: 0 }, children: s.name }),
|
|
24036
|
+
/* @__PURE__ */ jsx4(StatusBadge, { status: s.status })
|
|
24037
|
+
] }) })
|
|
24038
|
+
] }) }, s.key))
|
|
24039
|
+
] });
|
|
24040
|
+
}
|
|
24041
|
+
function StatusBadge({ status }) {
|
|
24042
|
+
const isSoon = String(status).toLowerCase() !== "live";
|
|
24043
|
+
const badgeStyle = {
|
|
24044
|
+
fontSize: "12px",
|
|
24045
|
+
padding: "2px 6px",
|
|
24046
|
+
borderRadius: "12px",
|
|
24047
|
+
border: "1px solid",
|
|
24048
|
+
color: isSoon ? "rgb(196 196 195)" : "#6aff7d",
|
|
24049
|
+
borderColor: isSoon ? "#484848" : "#6aff7d",
|
|
24050
|
+
backgroundColor: isSoon ? "#424242" : "#002b1a"
|
|
24051
|
+
};
|
|
24052
|
+
return /* @__PURE__ */ jsx4("span", { style: badgeStyle, children: status });
|
|
24053
|
+
}
|
|
24054
|
+
function getDescription(tokenSymbol) {
|
|
24055
|
+
return MetaVaultDescription();
|
|
24056
|
+
}
|
|
24057
|
+
function getFAQs() {
|
|
24058
|
+
return [
|
|
24059
|
+
{
|
|
24060
|
+
question: "What is the Meta Vault?",
|
|
24061
|
+
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."
|
|
24062
|
+
},
|
|
24063
|
+
{
|
|
24064
|
+
question: "How does yield allocation work?",
|
|
24065
|
+
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."
|
|
24066
|
+
},
|
|
24067
|
+
{
|
|
24068
|
+
question: "Which yield sources are supported?",
|
|
24069
|
+
answer: /* @__PURE__ */ jsxs3("span", { children: [
|
|
24070
|
+
"Currently, ",
|
|
24071
|
+
/* @__PURE__ */ jsx4("strong", { children: "Vesu" }),
|
|
24072
|
+
" is live. Future integrations include",
|
|
24073
|
+
" ",
|
|
24074
|
+
/* @__PURE__ */ jsx4("strong", { children: "Endur" }),
|
|
24075
|
+
", ",
|
|
24076
|
+
/* @__PURE__ */ jsx4("strong", { children: "Extended" }),
|
|
24077
|
+
", and",
|
|
24078
|
+
" ",
|
|
24079
|
+
/* @__PURE__ */ jsx4("strong", { children: "Ekubo" }),
|
|
24080
|
+
" (all coming soon)."
|
|
24081
|
+
] })
|
|
24082
|
+
},
|
|
24083
|
+
{
|
|
24084
|
+
question: "What do I receive when I deposit?",
|
|
24085
|
+
answer: "Depositors receive vault tokens representing their proportional share of the vault. These tokens entitle holders to both the principal and accrued yield."
|
|
24086
|
+
},
|
|
24087
|
+
{
|
|
24088
|
+
question: "How long do withdrawals take?",
|
|
24089
|
+
answer: "Withdrawals may take up to 1-2 hours to process, as the vault unwinds and settles liquidity routing across integrated protocols."
|
|
24090
|
+
},
|
|
24091
|
+
{
|
|
24092
|
+
question: "Is the Meta Vault non-custodial?",
|
|
24093
|
+
answer: "Yes. The Meta Vault operates entirely on-chain. Users always maintain control of their vault tokens, and the strategy is fully transparent."
|
|
24094
|
+
},
|
|
24095
|
+
{
|
|
24096
|
+
question: "How is risk managed?",
|
|
24097
|
+
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."
|
|
24098
|
+
},
|
|
24099
|
+
{
|
|
24100
|
+
question: "Are there any fees?",
|
|
24101
|
+
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."
|
|
24102
|
+
}
|
|
24103
|
+
];
|
|
24104
|
+
}
|
|
24105
|
+
function getContractDetails(settings) {
|
|
24106
|
+
return [
|
|
24107
|
+
{ address: settings.vaultAddress, name: "Vault" },
|
|
24108
|
+
{ address: settings.manager, name: "Vault Manager" },
|
|
24109
|
+
{ address: settings.vaultAllocator, name: "Vault Allocator" },
|
|
24110
|
+
{ address: settings.redeemRequestNFT, name: "Redeem Request NFT" },
|
|
24111
|
+
{ address: settings.aumOracle, name: "AUM Oracle" }
|
|
24112
|
+
];
|
|
24113
|
+
}
|
|
24114
|
+
var investmentSteps = [
|
|
24115
|
+
"Deposit funds into the vault",
|
|
24116
|
+
"Vault manager allocates funds to optimal yield sources",
|
|
24117
|
+
"Vault manager reports asset under management (AUM) regularly to the vault",
|
|
24118
|
+
"Request withdrawal and vault manager processes it in 1-2 hours"
|
|
24119
|
+
];
|
|
25867
24120
|
var UniversalStrategies = [
|
|
25868
24121
|
{
|
|
25869
24122
|
name: "USDC Evergreen",
|
|
25870
|
-
description: "
|
|
24123
|
+
description: getDescription("USDC"),
|
|
25871
24124
|
address: ContractAddr.from("0x7e6498cf6a1bfc7e6fc89f1831865e2dacb9756def4ec4b031a9138788a3b5e"),
|
|
25872
24125
|
launchBlock: 0,
|
|
25873
24126
|
type: "ERC4626",
|
|
@@ -25880,13 +24133,13 @@ var UniversalStrategies = [
|
|
|
25880
24133
|
},
|
|
25881
24134
|
protocols: [Protocols.VESU],
|
|
25882
24135
|
maxTVL: Web3Number.fromWei(0, 6),
|
|
25883
|
-
contractDetails:
|
|
25884
|
-
faqs:
|
|
25885
|
-
investmentSteps
|
|
24136
|
+
contractDetails: getContractDetails(usdcVaultSettings),
|
|
24137
|
+
faqs: getFAQs(),
|
|
24138
|
+
investmentSteps
|
|
25886
24139
|
},
|
|
25887
24140
|
{
|
|
25888
24141
|
name: "WBTC Evergreen",
|
|
25889
|
-
description: "
|
|
24142
|
+
description: getDescription("WBTC"),
|
|
25890
24143
|
address: ContractAddr.from("0x5a4c1651b913aa2ea7afd9024911603152a19058624c3e425405370d62bf80c"),
|
|
25891
24144
|
launchBlock: 0,
|
|
25892
24145
|
type: "ERC4626",
|
|
@@ -25899,9 +24152,66 @@ var UniversalStrategies = [
|
|
|
25899
24152
|
},
|
|
25900
24153
|
protocols: [Protocols.VESU],
|
|
25901
24154
|
maxTVL: Web3Number.fromWei(0, 8),
|
|
25902
|
-
contractDetails:
|
|
25903
|
-
faqs:
|
|
25904
|
-
investmentSteps
|
|
24155
|
+
contractDetails: getContractDetails(wbtcVaultSettings),
|
|
24156
|
+
faqs: getFAQs(),
|
|
24157
|
+
investmentSteps
|
|
24158
|
+
},
|
|
24159
|
+
{
|
|
24160
|
+
name: "ETH Evergreen",
|
|
24161
|
+
description: getDescription("ETH"),
|
|
24162
|
+
address: ContractAddr.from("0x446c22d4d3f5cb52b4950ba832ba1df99464c6673a37c092b1d9622650dbd8"),
|
|
24163
|
+
launchBlock: 0,
|
|
24164
|
+
type: "ERC4626",
|
|
24165
|
+
depositTokens: [Global.getDefaultTokens().find((token) => token.symbol === "ETH")],
|
|
24166
|
+
additionalInfo: getLooperSettings("ETH", "WBTC", ethVaultSettings, VesuPools.Genesis, VesuPools.Genesis),
|
|
24167
|
+
risk: {
|
|
24168
|
+
riskFactor: _riskFactor4,
|
|
24169
|
+
netRisk: _riskFactor4.reduce((acc, curr) => acc + curr.value * curr.weight, 0) / _riskFactor4.reduce((acc, curr) => acc + curr.weight, 0),
|
|
24170
|
+
notARisks: getNoRiskTags(_riskFactor4)
|
|
24171
|
+
},
|
|
24172
|
+
protocols: [Protocols.VESU],
|
|
24173
|
+
maxTVL: Web3Number.fromWei(0, 18),
|
|
24174
|
+
contractDetails: getContractDetails(ethVaultSettings),
|
|
24175
|
+
faqs: getFAQs(),
|
|
24176
|
+
investmentSteps
|
|
24177
|
+
},
|
|
24178
|
+
{
|
|
24179
|
+
name: "STRK Evergreen",
|
|
24180
|
+
description: getDescription("STRK"),
|
|
24181
|
+
address: ContractAddr.from("0x55d012f57e58c96e0a5c7ebbe55853989d01e6538b15a95e7178aca4af05c21"),
|
|
24182
|
+
launchBlock: 0,
|
|
24183
|
+
type: "ERC4626",
|
|
24184
|
+
depositTokens: [Global.getDefaultTokens().find((token) => token.symbol === "STRK")],
|
|
24185
|
+
additionalInfo: getLooperSettings("STRK", "ETH", strkVaultSettings, VesuPools.Genesis, VesuPools.Genesis),
|
|
24186
|
+
risk: {
|
|
24187
|
+
riskFactor: _riskFactor4,
|
|
24188
|
+
netRisk: _riskFactor4.reduce((acc, curr) => acc + curr.value * curr.weight, 0) / _riskFactor4.reduce((acc, curr) => acc + curr.weight, 0),
|
|
24189
|
+
notARisks: getNoRiskTags(_riskFactor4)
|
|
24190
|
+
},
|
|
24191
|
+
protocols: [Protocols.VESU],
|
|
24192
|
+
maxTVL: Web3Number.fromWei(0, 18),
|
|
24193
|
+
contractDetails: getContractDetails(strkVaultSettings),
|
|
24194
|
+
faqs: getFAQs(),
|
|
24195
|
+
investmentSteps
|
|
24196
|
+
},
|
|
24197
|
+
{
|
|
24198
|
+
name: "USDT Evergreen",
|
|
24199
|
+
description: getDescription("USDT"),
|
|
24200
|
+
address: ContractAddr.from("0x1c4933d1880c6778585e597154eaca7b428579d72f3aae425ad2e4d26c6bb3"),
|
|
24201
|
+
launchBlock: 0,
|
|
24202
|
+
type: "ERC4626",
|
|
24203
|
+
depositTokens: [Global.getDefaultTokens().find((token) => token.symbol === "USDT")],
|
|
24204
|
+
additionalInfo: getLooperSettings("USDT", "ETH", usdtVaultSettings, VesuPools.Genesis, VesuPools.Genesis),
|
|
24205
|
+
risk: {
|
|
24206
|
+
riskFactor: _riskFactor4,
|
|
24207
|
+
netRisk: _riskFactor4.reduce((acc, curr) => acc + curr.value * curr.weight, 0) / _riskFactor4.reduce((acc, curr) => acc + curr.weight, 0),
|
|
24208
|
+
notARisks: getNoRiskTags(_riskFactor4)
|
|
24209
|
+
},
|
|
24210
|
+
protocols: [Protocols.VESU],
|
|
24211
|
+
maxTVL: Web3Number.fromWei(0, 6),
|
|
24212
|
+
contractDetails: getContractDetails(usdtVaultSettings),
|
|
24213
|
+
faqs: getFAQs(),
|
|
24214
|
+
investmentSteps
|
|
25905
24215
|
}
|
|
25906
24216
|
];
|
|
25907
24217
|
|
|
@@ -26008,16 +24318,16 @@ var PricerRedis = class extends Pricer {
|
|
|
26008
24318
|
|
|
26009
24319
|
// src/node/deployer.ts
|
|
26010
24320
|
import assert2 from "assert";
|
|
26011
|
-
import { TransactionExecutionStatus, constants as
|
|
24321
|
+
import { TransactionExecutionStatus, constants as constants3, extractContractHashes, json, num as num10, transaction } from "starknet";
|
|
26012
24322
|
import { readFileSync as readFileSync2, existsSync, writeFileSync as writeFileSync2 } from "fs";
|
|
26013
24323
|
|
|
26014
24324
|
// src/utils/store.ts
|
|
26015
24325
|
import fs, { readFileSync, writeFileSync } from "fs";
|
|
26016
|
-
import { Account as Account3, constants } from "starknet";
|
|
26017
|
-
import * as
|
|
24326
|
+
import { Account as Account3, constants as constants2 } from "starknet";
|
|
24327
|
+
import * as crypto2 from "crypto";
|
|
26018
24328
|
|
|
26019
24329
|
// src/utils/encrypt.ts
|
|
26020
|
-
import * as
|
|
24330
|
+
import * as crypto from "crypto";
|
|
26021
24331
|
var PasswordJsonCryptoUtil = class {
|
|
26022
24332
|
constructor() {
|
|
26023
24333
|
this.algorithm = "aes-256-gcm";
|
|
@@ -26033,14 +24343,14 @@ var PasswordJsonCryptoUtil = class {
|
|
|
26033
24343
|
}
|
|
26034
24344
|
// Number of iterations for PBKDF2
|
|
26035
24345
|
deriveKey(password, salt) {
|
|
26036
|
-
return
|
|
24346
|
+
return crypto.pbkdf2Sync(password, salt, this.pbkdf2Iterations, this.keyLength, "sha256");
|
|
26037
24347
|
}
|
|
26038
24348
|
encrypt(data, password) {
|
|
26039
24349
|
const jsonString = JSON.stringify(data);
|
|
26040
|
-
const salt =
|
|
26041
|
-
const iv =
|
|
24350
|
+
const salt = crypto.randomBytes(this.saltLength);
|
|
24351
|
+
const iv = crypto.randomBytes(this.ivLength);
|
|
26042
24352
|
const key = this.deriveKey(password, salt);
|
|
26043
|
-
const cipher =
|
|
24353
|
+
const cipher = crypto.createCipheriv(this.algorithm, key, iv, { authTagLength: this.tagLength });
|
|
26044
24354
|
let encrypted = cipher.update(jsonString, "utf8", "hex");
|
|
26045
24355
|
encrypted += cipher.final("hex");
|
|
26046
24356
|
const tag = cipher.getAuthTag();
|
|
@@ -26053,7 +24363,7 @@ var PasswordJsonCryptoUtil = class {
|
|
|
26053
24363
|
const tag = data.subarray(this.saltLength + this.ivLength, this.saltLength + this.ivLength + this.tagLength);
|
|
26054
24364
|
const encrypted = data.subarray(this.saltLength + this.ivLength + this.tagLength);
|
|
26055
24365
|
const key = this.deriveKey(password, salt);
|
|
26056
|
-
const decipher =
|
|
24366
|
+
const decipher = crypto.createDecipheriv(this.algorithm, key, iv, { authTagLength: this.tagLength });
|
|
26057
24367
|
decipher.setAuthTag(tag);
|
|
26058
24368
|
try {
|
|
26059
24369
|
let decrypted = decipher.update(encrypted.toString("hex"), "hex", "utf8");
|
|
@@ -26074,7 +24384,7 @@ function getDefaultStoreConfig(network) {
|
|
|
26074
24384
|
SECRET_FILE_FOLDER: `${process.env.HOME}/.starknet-store`,
|
|
26075
24385
|
NETWORK: network,
|
|
26076
24386
|
ACCOUNTS_FILE_NAME: "accounts.json",
|
|
26077
|
-
PASSWORD:
|
|
24387
|
+
PASSWORD: crypto2.randomBytes(16).toString("hex")
|
|
26078
24388
|
};
|
|
26079
24389
|
}
|
|
26080
24390
|
var Store = class _Store {
|
|
@@ -26098,7 +24408,7 @@ var Store = class _Store {
|
|
|
26098
24408
|
logger.warn(`This not stored anywhere, please you backup this password for future use`);
|
|
26099
24409
|
logger.warn(`\u26A0\uFE0F=========================================\u26A0\uFE0F`);
|
|
26100
24410
|
}
|
|
26101
|
-
getAccount(accountKey, txVersion =
|
|
24411
|
+
getAccount(accountKey, txVersion = constants2.TRANSACTION_VERSION.V2) {
|
|
26102
24412
|
const accounts = this.loadAccounts();
|
|
26103
24413
|
logger.verbose(`nAccounts loaded for network: ${Object.keys(accounts).length}`);
|
|
26104
24414
|
const data = accounts[accountKey];
|
|
@@ -26242,7 +24552,7 @@ async function deployContract(contract_name, classHash, constructorData, config,
|
|
|
26242
24552
|
classHash,
|
|
26243
24553
|
constructorCalldata: constructorData
|
|
26244
24554
|
});
|
|
26245
|
-
console.log("Deploy fee", contract_name, Number(fee.
|
|
24555
|
+
console.log("Deploy fee", contract_name, Number(fee.overall_fee) / 10 ** 18, "ETH");
|
|
26246
24556
|
const tx = await acc.deployContract({
|
|
26247
24557
|
classHash,
|
|
26248
24558
|
constructorCalldata: constructorData
|
|
@@ -26284,7 +24594,7 @@ async function prepareMultiDeployContracts(contracts, config, acc) {
|
|
|
26284
24594
|
}
|
|
26285
24595
|
async function executeDeployCalls(contractsInfo, acc, provider2) {
|
|
26286
24596
|
for (let contractInfo of contractsInfo) {
|
|
26287
|
-
assert2(num10.toHexString(contractInfo.call.contractAddress) == num10.toHexString(
|
|
24597
|
+
assert2(num10.toHexString(contractInfo.call.contractAddress) == num10.toHexString(constants3.UDC.ADDRESS), "Must be pointed at UDC address");
|
|
26288
24598
|
}
|
|
26289
24599
|
const allCalls = contractsInfo.map((info) => info.call);
|
|
26290
24600
|
await executeTransactions(allCalls, acc, provider2, `Deploying contracts: ${contractsInfo.map((info) => info.contract_name).join(", ")}`);
|
|
@@ -26323,6 +24633,7 @@ var Deployer = {
|
|
|
26323
24633
|
};
|
|
26324
24634
|
var deployer_default = Deployer;
|
|
26325
24635
|
export {
|
|
24636
|
+
AUMTypes,
|
|
26326
24637
|
AutoCompounderSTRK,
|
|
26327
24638
|
AvnuWrapper,
|
|
26328
24639
|
BaseAdapter,
|
|
@@ -26375,26 +24686,3 @@ export {
|
|
|
26375
24686
|
highlightTextWithLinks,
|
|
26376
24687
|
logger
|
|
26377
24688
|
};
|
|
26378
|
-
/*! Bundled license information:
|
|
26379
|
-
|
|
26380
|
-
@noble/hashes/esm/utils.js:
|
|
26381
|
-
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
26382
|
-
|
|
26383
|
-
@noble/curves/esm/abstract/utils.js:
|
|
26384
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
26385
|
-
|
|
26386
|
-
@noble/curves/esm/abstract/modular.js:
|
|
26387
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
26388
|
-
|
|
26389
|
-
@noble/curves/esm/abstract/poseidon.js:
|
|
26390
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
26391
|
-
|
|
26392
|
-
@noble/curves/esm/abstract/curve.js:
|
|
26393
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
26394
|
-
|
|
26395
|
-
@noble/curves/esm/abstract/weierstrass.js:
|
|
26396
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
26397
|
-
|
|
26398
|
-
@noble/curves/esm/_shortw_utils.js:
|
|
26399
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
26400
|
-
*/
|