@strkfarm/sdk 1.0.57 → 1.0.59
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 +5951 -5863
- package/dist/index.browser.mjs +181 -2091
- package/dist/index.d.ts +8 -1
- package/dist/index.js +196 -2100
- package/dist/index.mjs +195 -2106
- package/package.json +3 -2
- package/src/interfaces/common.tsx +5 -3
- package/src/node/deployer.ts +2 -2
- package/src/strategies/universal-strategy.ts +162 -10
- package/src/utils/cacheClass.ts +28 -28
package/dist/index.browser.mjs
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __export = (target, all) => {
|
|
3
|
-
for (var name in all)
|
|
4
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
-
};
|
|
6
|
-
|
|
7
1
|
// src/modules/pricer.ts
|
|
8
2
|
import axios2 from "axios";
|
|
9
3
|
|
|
@@ -1938,2032 +1932,7 @@ import { processMultiProof, processProof } from "@ericnordelo/strk-merkle-tree/d
|
|
|
1938
1932
|
import { standardLeafHash } from "@ericnordelo/strk-merkle-tree/dist/hashes";
|
|
1939
1933
|
import { MerkleTreeImpl } from "@ericnordelo/strk-merkle-tree/dist/merkletree";
|
|
1940
1934
|
import { num as num2 } from "starknet";
|
|
1941
|
-
|
|
1942
|
-
// ../node_modules/@noble/hashes/esm/_assert.js
|
|
1943
|
-
function number(n) {
|
|
1944
|
-
if (!Number.isSafeInteger(n) || n < 0)
|
|
1945
|
-
throw new Error(`Wrong positive integer: ${n}`);
|
|
1946
|
-
}
|
|
1947
|
-
function bytes(b, ...lengths) {
|
|
1948
|
-
if (!(b instanceof Uint8Array))
|
|
1949
|
-
throw new Error("Expected Uint8Array");
|
|
1950
|
-
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
1951
|
-
throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
|
|
1952
|
-
}
|
|
1953
|
-
function hash(hash5) {
|
|
1954
|
-
if (typeof hash5 !== "function" || typeof hash5.create !== "function")
|
|
1955
|
-
throw new Error("Hash should be wrapped by utils.wrapConstructor");
|
|
1956
|
-
number(hash5.outputLen);
|
|
1957
|
-
number(hash5.blockLen);
|
|
1958
|
-
}
|
|
1959
|
-
function exists(instance, checkFinished = true) {
|
|
1960
|
-
if (instance.destroyed)
|
|
1961
|
-
throw new Error("Hash instance has been destroyed");
|
|
1962
|
-
if (checkFinished && instance.finished)
|
|
1963
|
-
throw new Error("Hash#digest() has already been called");
|
|
1964
|
-
}
|
|
1965
|
-
function output(out, instance) {
|
|
1966
|
-
bytes(out);
|
|
1967
|
-
const min = instance.outputLen;
|
|
1968
|
-
if (out.length < min) {
|
|
1969
|
-
throw new Error(`digestInto() expects output buffer of length at least ${min}`);
|
|
1970
|
-
}
|
|
1971
|
-
}
|
|
1972
|
-
|
|
1973
|
-
// ../node_modules/@noble/hashes/esm/crypto.js
|
|
1974
|
-
var crypto = typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
1975
|
-
|
|
1976
|
-
// ../node_modules/@noble/hashes/esm/utils.js
|
|
1977
|
-
var u8a = (a) => a instanceof Uint8Array;
|
|
1978
|
-
var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
1979
|
-
var rotr = (word, shift) => word << 32 - shift | word >>> shift;
|
|
1980
|
-
var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
|
|
1981
|
-
if (!isLE)
|
|
1982
|
-
throw new Error("Non little-endian hardware is not supported");
|
|
1983
|
-
function utf8ToBytes(str) {
|
|
1984
|
-
if (typeof str !== "string")
|
|
1985
|
-
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
|
1986
|
-
return new Uint8Array(new TextEncoder().encode(str));
|
|
1987
|
-
}
|
|
1988
|
-
function toBytes(data) {
|
|
1989
|
-
if (typeof data === "string")
|
|
1990
|
-
data = utf8ToBytes(data);
|
|
1991
|
-
if (!u8a(data))
|
|
1992
|
-
throw new Error(`expected Uint8Array, got ${typeof data}`);
|
|
1993
|
-
return data;
|
|
1994
|
-
}
|
|
1995
|
-
function concatBytes(...arrays) {
|
|
1996
|
-
const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0));
|
|
1997
|
-
let pad = 0;
|
|
1998
|
-
arrays.forEach((a) => {
|
|
1999
|
-
if (!u8a(a))
|
|
2000
|
-
throw new Error("Uint8Array expected");
|
|
2001
|
-
r.set(a, pad);
|
|
2002
|
-
pad += a.length;
|
|
2003
|
-
});
|
|
2004
|
-
return r;
|
|
2005
|
-
}
|
|
2006
|
-
var Hash = class {
|
|
2007
|
-
// Safe version that clones internal state
|
|
2008
|
-
clone() {
|
|
2009
|
-
return this._cloneInto();
|
|
2010
|
-
}
|
|
2011
|
-
};
|
|
2012
|
-
var toStr = {}.toString;
|
|
2013
|
-
function wrapConstructor(hashCons) {
|
|
2014
|
-
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
2015
|
-
const tmp = hashCons();
|
|
2016
|
-
hashC.outputLen = tmp.outputLen;
|
|
2017
|
-
hashC.blockLen = tmp.blockLen;
|
|
2018
|
-
hashC.create = () => hashCons();
|
|
2019
|
-
return hashC;
|
|
2020
|
-
}
|
|
2021
|
-
function randomBytes(bytesLength = 32) {
|
|
2022
|
-
if (crypto && typeof crypto.getRandomValues === "function") {
|
|
2023
|
-
return crypto.getRandomValues(new Uint8Array(bytesLength));
|
|
2024
|
-
}
|
|
2025
|
-
throw new Error("crypto.getRandomValues must be defined");
|
|
2026
|
-
}
|
|
2027
|
-
|
|
2028
|
-
// ../node_modules/@noble/hashes/esm/_sha2.js
|
|
2029
|
-
function setBigUint64(view, byteOffset, value, isLE2) {
|
|
2030
|
-
if (typeof view.setBigUint64 === "function")
|
|
2031
|
-
return view.setBigUint64(byteOffset, value, isLE2);
|
|
2032
|
-
const _32n = BigInt(32);
|
|
2033
|
-
const _u32_max = BigInt(4294967295);
|
|
2034
|
-
const wh = Number(value >> _32n & _u32_max);
|
|
2035
|
-
const wl = Number(value & _u32_max);
|
|
2036
|
-
const h = isLE2 ? 4 : 0;
|
|
2037
|
-
const l = isLE2 ? 0 : 4;
|
|
2038
|
-
view.setUint32(byteOffset + h, wh, isLE2);
|
|
2039
|
-
view.setUint32(byteOffset + l, wl, isLE2);
|
|
2040
|
-
}
|
|
2041
|
-
var SHA2 = class extends Hash {
|
|
2042
|
-
constructor(blockLen, outputLen, padOffset, isLE2) {
|
|
2043
|
-
super();
|
|
2044
|
-
this.blockLen = blockLen;
|
|
2045
|
-
this.outputLen = outputLen;
|
|
2046
|
-
this.padOffset = padOffset;
|
|
2047
|
-
this.isLE = isLE2;
|
|
2048
|
-
this.finished = false;
|
|
2049
|
-
this.length = 0;
|
|
2050
|
-
this.pos = 0;
|
|
2051
|
-
this.destroyed = false;
|
|
2052
|
-
this.buffer = new Uint8Array(blockLen);
|
|
2053
|
-
this.view = createView(this.buffer);
|
|
2054
|
-
}
|
|
2055
|
-
update(data) {
|
|
2056
|
-
exists(this);
|
|
2057
|
-
const { view, buffer, blockLen } = this;
|
|
2058
|
-
data = toBytes(data);
|
|
2059
|
-
const len = data.length;
|
|
2060
|
-
for (let pos = 0; pos < len; ) {
|
|
2061
|
-
const take = Math.min(blockLen - this.pos, len - pos);
|
|
2062
|
-
if (take === blockLen) {
|
|
2063
|
-
const dataView = createView(data);
|
|
2064
|
-
for (; blockLen <= len - pos; pos += blockLen)
|
|
2065
|
-
this.process(dataView, pos);
|
|
2066
|
-
continue;
|
|
2067
|
-
}
|
|
2068
|
-
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
2069
|
-
this.pos += take;
|
|
2070
|
-
pos += take;
|
|
2071
|
-
if (this.pos === blockLen) {
|
|
2072
|
-
this.process(view, 0);
|
|
2073
|
-
this.pos = 0;
|
|
2074
|
-
}
|
|
2075
|
-
}
|
|
2076
|
-
this.length += data.length;
|
|
2077
|
-
this.roundClean();
|
|
2078
|
-
return this;
|
|
2079
|
-
}
|
|
2080
|
-
digestInto(out) {
|
|
2081
|
-
exists(this);
|
|
2082
|
-
output(out, this);
|
|
2083
|
-
this.finished = true;
|
|
2084
|
-
const { buffer, view, blockLen, isLE: isLE2 } = this;
|
|
2085
|
-
let { pos } = this;
|
|
2086
|
-
buffer[pos++] = 128;
|
|
2087
|
-
this.buffer.subarray(pos).fill(0);
|
|
2088
|
-
if (this.padOffset > blockLen - pos) {
|
|
2089
|
-
this.process(view, 0);
|
|
2090
|
-
pos = 0;
|
|
2091
|
-
}
|
|
2092
|
-
for (let i = pos; i < blockLen; i++)
|
|
2093
|
-
buffer[i] = 0;
|
|
2094
|
-
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE2);
|
|
2095
|
-
this.process(view, 0);
|
|
2096
|
-
const oview = createView(out);
|
|
2097
|
-
const len = this.outputLen;
|
|
2098
|
-
if (len % 4)
|
|
2099
|
-
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
2100
|
-
const outLen = len / 4;
|
|
2101
|
-
const state = this.get();
|
|
2102
|
-
if (outLen > state.length)
|
|
2103
|
-
throw new Error("_sha2: outputLen bigger than state");
|
|
2104
|
-
for (let i = 0; i < outLen; i++)
|
|
2105
|
-
oview.setUint32(4 * i, state[i], isLE2);
|
|
2106
|
-
}
|
|
2107
|
-
digest() {
|
|
2108
|
-
const { buffer, outputLen } = this;
|
|
2109
|
-
this.digestInto(buffer);
|
|
2110
|
-
const res = buffer.slice(0, outputLen);
|
|
2111
|
-
this.destroy();
|
|
2112
|
-
return res;
|
|
2113
|
-
}
|
|
2114
|
-
_cloneInto(to) {
|
|
2115
|
-
to || (to = new this.constructor());
|
|
2116
|
-
to.set(...this.get());
|
|
2117
|
-
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
2118
|
-
to.length = length;
|
|
2119
|
-
to.pos = pos;
|
|
2120
|
-
to.finished = finished;
|
|
2121
|
-
to.destroyed = destroyed;
|
|
2122
|
-
if (length % blockLen)
|
|
2123
|
-
to.buffer.set(buffer);
|
|
2124
|
-
return to;
|
|
2125
|
-
}
|
|
2126
|
-
};
|
|
2127
|
-
|
|
2128
|
-
// ../node_modules/@noble/hashes/esm/sha256.js
|
|
2129
|
-
var Chi = (a, b, c) => a & b ^ ~a & c;
|
|
2130
|
-
var Maj = (a, b, c) => a & b ^ a & c ^ b & c;
|
|
2131
|
-
var SHA256_K = /* @__PURE__ */ new Uint32Array([
|
|
2132
|
-
1116352408,
|
|
2133
|
-
1899447441,
|
|
2134
|
-
3049323471,
|
|
2135
|
-
3921009573,
|
|
2136
|
-
961987163,
|
|
2137
|
-
1508970993,
|
|
2138
|
-
2453635748,
|
|
2139
|
-
2870763221,
|
|
2140
|
-
3624381080,
|
|
2141
|
-
310598401,
|
|
2142
|
-
607225278,
|
|
2143
|
-
1426881987,
|
|
2144
|
-
1925078388,
|
|
2145
|
-
2162078206,
|
|
2146
|
-
2614888103,
|
|
2147
|
-
3248222580,
|
|
2148
|
-
3835390401,
|
|
2149
|
-
4022224774,
|
|
2150
|
-
264347078,
|
|
2151
|
-
604807628,
|
|
2152
|
-
770255983,
|
|
2153
|
-
1249150122,
|
|
2154
|
-
1555081692,
|
|
2155
|
-
1996064986,
|
|
2156
|
-
2554220882,
|
|
2157
|
-
2821834349,
|
|
2158
|
-
2952996808,
|
|
2159
|
-
3210313671,
|
|
2160
|
-
3336571891,
|
|
2161
|
-
3584528711,
|
|
2162
|
-
113926993,
|
|
2163
|
-
338241895,
|
|
2164
|
-
666307205,
|
|
2165
|
-
773529912,
|
|
2166
|
-
1294757372,
|
|
2167
|
-
1396182291,
|
|
2168
|
-
1695183700,
|
|
2169
|
-
1986661051,
|
|
2170
|
-
2177026350,
|
|
2171
|
-
2456956037,
|
|
2172
|
-
2730485921,
|
|
2173
|
-
2820302411,
|
|
2174
|
-
3259730800,
|
|
2175
|
-
3345764771,
|
|
2176
|
-
3516065817,
|
|
2177
|
-
3600352804,
|
|
2178
|
-
4094571909,
|
|
2179
|
-
275423344,
|
|
2180
|
-
430227734,
|
|
2181
|
-
506948616,
|
|
2182
|
-
659060556,
|
|
2183
|
-
883997877,
|
|
2184
|
-
958139571,
|
|
2185
|
-
1322822218,
|
|
2186
|
-
1537002063,
|
|
2187
|
-
1747873779,
|
|
2188
|
-
1955562222,
|
|
2189
|
-
2024104815,
|
|
2190
|
-
2227730452,
|
|
2191
|
-
2361852424,
|
|
2192
|
-
2428436474,
|
|
2193
|
-
2756734187,
|
|
2194
|
-
3204031479,
|
|
2195
|
-
3329325298
|
|
2196
|
-
]);
|
|
2197
|
-
var IV = /* @__PURE__ */ new Uint32Array([
|
|
2198
|
-
1779033703,
|
|
2199
|
-
3144134277,
|
|
2200
|
-
1013904242,
|
|
2201
|
-
2773480762,
|
|
2202
|
-
1359893119,
|
|
2203
|
-
2600822924,
|
|
2204
|
-
528734635,
|
|
2205
|
-
1541459225
|
|
2206
|
-
]);
|
|
2207
|
-
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
2208
|
-
var SHA256 = class extends SHA2 {
|
|
2209
|
-
constructor() {
|
|
2210
|
-
super(64, 32, 8, false);
|
|
2211
|
-
this.A = IV[0] | 0;
|
|
2212
|
-
this.B = IV[1] | 0;
|
|
2213
|
-
this.C = IV[2] | 0;
|
|
2214
|
-
this.D = IV[3] | 0;
|
|
2215
|
-
this.E = IV[4] | 0;
|
|
2216
|
-
this.F = IV[5] | 0;
|
|
2217
|
-
this.G = IV[6] | 0;
|
|
2218
|
-
this.H = IV[7] | 0;
|
|
2219
|
-
}
|
|
2220
|
-
get() {
|
|
2221
|
-
const { A, B, C, D, E, F, G, H } = this;
|
|
2222
|
-
return [A, B, C, D, E, F, G, H];
|
|
2223
|
-
}
|
|
2224
|
-
// prettier-ignore
|
|
2225
|
-
set(A, B, C, D, E, F, G, H) {
|
|
2226
|
-
this.A = A | 0;
|
|
2227
|
-
this.B = B | 0;
|
|
2228
|
-
this.C = C | 0;
|
|
2229
|
-
this.D = D | 0;
|
|
2230
|
-
this.E = E | 0;
|
|
2231
|
-
this.F = F | 0;
|
|
2232
|
-
this.G = G | 0;
|
|
2233
|
-
this.H = H | 0;
|
|
2234
|
-
}
|
|
2235
|
-
process(view, offset) {
|
|
2236
|
-
for (let i = 0; i < 16; i++, offset += 4)
|
|
2237
|
-
SHA256_W[i] = view.getUint32(offset, false);
|
|
2238
|
-
for (let i = 16; i < 64; i++) {
|
|
2239
|
-
const W15 = SHA256_W[i - 15];
|
|
2240
|
-
const W2 = SHA256_W[i - 2];
|
|
2241
|
-
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
2242
|
-
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
|
|
2243
|
-
SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
2244
|
-
}
|
|
2245
|
-
let { A, B, C, D, E, F, G, H } = this;
|
|
2246
|
-
for (let i = 0; i < 64; i++) {
|
|
2247
|
-
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
2248
|
-
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
2249
|
-
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
2250
|
-
const T2 = sigma0 + Maj(A, B, C) | 0;
|
|
2251
|
-
H = G;
|
|
2252
|
-
G = F;
|
|
2253
|
-
F = E;
|
|
2254
|
-
E = D + T1 | 0;
|
|
2255
|
-
D = C;
|
|
2256
|
-
C = B;
|
|
2257
|
-
B = A;
|
|
2258
|
-
A = T1 + T2 | 0;
|
|
2259
|
-
}
|
|
2260
|
-
A = A + this.A | 0;
|
|
2261
|
-
B = B + this.B | 0;
|
|
2262
|
-
C = C + this.C | 0;
|
|
2263
|
-
D = D + this.D | 0;
|
|
2264
|
-
E = E + this.E | 0;
|
|
2265
|
-
F = F + this.F | 0;
|
|
2266
|
-
G = G + this.G | 0;
|
|
2267
|
-
H = H + this.H | 0;
|
|
2268
|
-
this.set(A, B, C, D, E, F, G, H);
|
|
2269
|
-
}
|
|
2270
|
-
roundClean() {
|
|
2271
|
-
SHA256_W.fill(0);
|
|
2272
|
-
}
|
|
2273
|
-
destroy() {
|
|
2274
|
-
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
2275
|
-
this.buffer.fill(0);
|
|
2276
|
-
}
|
|
2277
|
-
};
|
|
2278
|
-
var sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
|
|
2279
|
-
|
|
2280
|
-
// ../node_modules/@noble/curves/esm/abstract/utils.js
|
|
2281
|
-
var utils_exports = {};
|
|
2282
|
-
__export(utils_exports, {
|
|
2283
|
-
bitGet: () => bitGet,
|
|
2284
|
-
bitLen: () => bitLen,
|
|
2285
|
-
bitMask: () => bitMask,
|
|
2286
|
-
bitSet: () => bitSet,
|
|
2287
|
-
bytesToHex: () => bytesToHex,
|
|
2288
|
-
bytesToNumberBE: () => bytesToNumberBE,
|
|
2289
|
-
bytesToNumberLE: () => bytesToNumberLE,
|
|
2290
|
-
concatBytes: () => concatBytes2,
|
|
2291
|
-
createHmacDrbg: () => createHmacDrbg,
|
|
2292
|
-
ensureBytes: () => ensureBytes,
|
|
2293
|
-
equalBytes: () => equalBytes,
|
|
2294
|
-
hexToBytes: () => hexToBytes,
|
|
2295
|
-
hexToNumber: () => hexToNumber,
|
|
2296
|
-
numberToBytesBE: () => numberToBytesBE,
|
|
2297
|
-
numberToBytesLE: () => numberToBytesLE,
|
|
2298
|
-
numberToHexUnpadded: () => numberToHexUnpadded,
|
|
2299
|
-
numberToVarBytesBE: () => numberToVarBytesBE,
|
|
2300
|
-
utf8ToBytes: () => utf8ToBytes2,
|
|
2301
|
-
validateObject: () => validateObject
|
|
2302
|
-
});
|
|
2303
|
-
var _0n = BigInt(0);
|
|
2304
|
-
var _1n = BigInt(1);
|
|
2305
|
-
var _2n = BigInt(2);
|
|
2306
|
-
var u8a2 = (a) => a instanceof Uint8Array;
|
|
2307
|
-
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
2308
|
-
function bytesToHex(bytes2) {
|
|
2309
|
-
if (!u8a2(bytes2))
|
|
2310
|
-
throw new Error("Uint8Array expected");
|
|
2311
|
-
let hex = "";
|
|
2312
|
-
for (let i = 0; i < bytes2.length; i++) {
|
|
2313
|
-
hex += hexes[bytes2[i]];
|
|
2314
|
-
}
|
|
2315
|
-
return hex;
|
|
2316
|
-
}
|
|
2317
|
-
function numberToHexUnpadded(num10) {
|
|
2318
|
-
const hex = num10.toString(16);
|
|
2319
|
-
return hex.length & 1 ? `0${hex}` : hex;
|
|
2320
|
-
}
|
|
2321
|
-
function hexToNumber(hex) {
|
|
2322
|
-
if (typeof hex !== "string")
|
|
2323
|
-
throw new Error("hex string expected, got " + typeof hex);
|
|
2324
|
-
return BigInt(hex === "" ? "0" : `0x${hex}`);
|
|
2325
|
-
}
|
|
2326
|
-
function hexToBytes(hex) {
|
|
2327
|
-
if (typeof hex !== "string")
|
|
2328
|
-
throw new Error("hex string expected, got " + typeof hex);
|
|
2329
|
-
const len = hex.length;
|
|
2330
|
-
if (len % 2)
|
|
2331
|
-
throw new Error("padded hex string expected, got unpadded hex of length " + len);
|
|
2332
|
-
const array = new Uint8Array(len / 2);
|
|
2333
|
-
for (let i = 0; i < array.length; i++) {
|
|
2334
|
-
const j = i * 2;
|
|
2335
|
-
const hexByte = hex.slice(j, j + 2);
|
|
2336
|
-
const byte = Number.parseInt(hexByte, 16);
|
|
2337
|
-
if (Number.isNaN(byte) || byte < 0)
|
|
2338
|
-
throw new Error("Invalid byte sequence");
|
|
2339
|
-
array[i] = byte;
|
|
2340
|
-
}
|
|
2341
|
-
return array;
|
|
2342
|
-
}
|
|
2343
|
-
function bytesToNumberBE(bytes2) {
|
|
2344
|
-
return hexToNumber(bytesToHex(bytes2));
|
|
2345
|
-
}
|
|
2346
|
-
function bytesToNumberLE(bytes2) {
|
|
2347
|
-
if (!u8a2(bytes2))
|
|
2348
|
-
throw new Error("Uint8Array expected");
|
|
2349
|
-
return hexToNumber(bytesToHex(Uint8Array.from(bytes2).reverse()));
|
|
2350
|
-
}
|
|
2351
|
-
function numberToBytesBE(n, len) {
|
|
2352
|
-
return hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
2353
|
-
}
|
|
2354
|
-
function numberToBytesLE(n, len) {
|
|
2355
|
-
return numberToBytesBE(n, len).reverse();
|
|
2356
|
-
}
|
|
2357
|
-
function numberToVarBytesBE(n) {
|
|
2358
|
-
return hexToBytes(numberToHexUnpadded(n));
|
|
2359
|
-
}
|
|
2360
|
-
function ensureBytes(title, hex, expectedLength) {
|
|
2361
|
-
let res;
|
|
2362
|
-
if (typeof hex === "string") {
|
|
2363
|
-
try {
|
|
2364
|
-
res = hexToBytes(hex);
|
|
2365
|
-
} catch (e) {
|
|
2366
|
-
throw new Error(`${title} must be valid hex string, got "${hex}". Cause: ${e}`);
|
|
2367
|
-
}
|
|
2368
|
-
} else if (u8a2(hex)) {
|
|
2369
|
-
res = Uint8Array.from(hex);
|
|
2370
|
-
} else {
|
|
2371
|
-
throw new Error(`${title} must be hex string or Uint8Array`);
|
|
2372
|
-
}
|
|
2373
|
-
const len = res.length;
|
|
2374
|
-
if (typeof expectedLength === "number" && len !== expectedLength)
|
|
2375
|
-
throw new Error(`${title} expected ${expectedLength} bytes, got ${len}`);
|
|
2376
|
-
return res;
|
|
2377
|
-
}
|
|
2378
|
-
function concatBytes2(...arrays) {
|
|
2379
|
-
const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0));
|
|
2380
|
-
let pad = 0;
|
|
2381
|
-
arrays.forEach((a) => {
|
|
2382
|
-
if (!u8a2(a))
|
|
2383
|
-
throw new Error("Uint8Array expected");
|
|
2384
|
-
r.set(a, pad);
|
|
2385
|
-
pad += a.length;
|
|
2386
|
-
});
|
|
2387
|
-
return r;
|
|
2388
|
-
}
|
|
2389
|
-
function equalBytes(b1, b2) {
|
|
2390
|
-
if (b1.length !== b2.length)
|
|
2391
|
-
return false;
|
|
2392
|
-
for (let i = 0; i < b1.length; i++)
|
|
2393
|
-
if (b1[i] !== b2[i])
|
|
2394
|
-
return false;
|
|
2395
|
-
return true;
|
|
2396
|
-
}
|
|
2397
|
-
function utf8ToBytes2(str) {
|
|
2398
|
-
if (typeof str !== "string")
|
|
2399
|
-
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
|
2400
|
-
return new Uint8Array(new TextEncoder().encode(str));
|
|
2401
|
-
}
|
|
2402
|
-
function bitLen(n) {
|
|
2403
|
-
let len;
|
|
2404
|
-
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
2405
|
-
;
|
|
2406
|
-
return len;
|
|
2407
|
-
}
|
|
2408
|
-
function bitGet(n, pos) {
|
|
2409
|
-
return n >> BigInt(pos) & _1n;
|
|
2410
|
-
}
|
|
2411
|
-
var bitSet = (n, pos, value) => {
|
|
2412
|
-
return n | (value ? _1n : _0n) << BigInt(pos);
|
|
2413
|
-
};
|
|
2414
|
-
var bitMask = (n) => (_2n << BigInt(n - 1)) - _1n;
|
|
2415
|
-
var u8n = (data) => new Uint8Array(data);
|
|
2416
|
-
var u8fr = (arr) => Uint8Array.from(arr);
|
|
2417
|
-
function createHmacDrbg(hashLen, qByteLen, hmacFn) {
|
|
2418
|
-
if (typeof hashLen !== "number" || hashLen < 2)
|
|
2419
|
-
throw new Error("hashLen must be a number");
|
|
2420
|
-
if (typeof qByteLen !== "number" || qByteLen < 2)
|
|
2421
|
-
throw new Error("qByteLen must be a number");
|
|
2422
|
-
if (typeof hmacFn !== "function")
|
|
2423
|
-
throw new Error("hmacFn must be a function");
|
|
2424
|
-
let v = u8n(hashLen);
|
|
2425
|
-
let k = u8n(hashLen);
|
|
2426
|
-
let i = 0;
|
|
2427
|
-
const reset = () => {
|
|
2428
|
-
v.fill(1);
|
|
2429
|
-
k.fill(0);
|
|
2430
|
-
i = 0;
|
|
2431
|
-
};
|
|
2432
|
-
const h = (...b) => hmacFn(k, v, ...b);
|
|
2433
|
-
const reseed = (seed = u8n()) => {
|
|
2434
|
-
k = h(u8fr([0]), seed);
|
|
2435
|
-
v = h();
|
|
2436
|
-
if (seed.length === 0)
|
|
2437
|
-
return;
|
|
2438
|
-
k = h(u8fr([1]), seed);
|
|
2439
|
-
v = h();
|
|
2440
|
-
};
|
|
2441
|
-
const gen = () => {
|
|
2442
|
-
if (i++ >= 1e3)
|
|
2443
|
-
throw new Error("drbg: tried 1000 values");
|
|
2444
|
-
let len = 0;
|
|
2445
|
-
const out = [];
|
|
2446
|
-
while (len < qByteLen) {
|
|
2447
|
-
v = h();
|
|
2448
|
-
const sl = v.slice();
|
|
2449
|
-
out.push(sl);
|
|
2450
|
-
len += v.length;
|
|
2451
|
-
}
|
|
2452
|
-
return concatBytes2(...out);
|
|
2453
|
-
};
|
|
2454
|
-
const genUntil = (seed, pred) => {
|
|
2455
|
-
reset();
|
|
2456
|
-
reseed(seed);
|
|
2457
|
-
let res = void 0;
|
|
2458
|
-
while (!(res = pred(gen())))
|
|
2459
|
-
reseed();
|
|
2460
|
-
reset();
|
|
2461
|
-
return res;
|
|
2462
|
-
};
|
|
2463
|
-
return genUntil;
|
|
2464
|
-
}
|
|
2465
|
-
var validatorFns = {
|
|
2466
|
-
bigint: (val) => typeof val === "bigint",
|
|
2467
|
-
function: (val) => typeof val === "function",
|
|
2468
|
-
boolean: (val) => typeof val === "boolean",
|
|
2469
|
-
string: (val) => typeof val === "string",
|
|
2470
|
-
stringOrUint8Array: (val) => typeof val === "string" || val instanceof Uint8Array,
|
|
2471
|
-
isSafeInteger: (val) => Number.isSafeInteger(val),
|
|
2472
|
-
array: (val) => Array.isArray(val),
|
|
2473
|
-
field: (val, object) => object.Fp.isValid(val),
|
|
2474
|
-
hash: (val) => typeof val === "function" && Number.isSafeInteger(val.outputLen)
|
|
2475
|
-
};
|
|
2476
|
-
function validateObject(object, validators, optValidators = {}) {
|
|
2477
|
-
const checkField = (fieldName, type, isOptional) => {
|
|
2478
|
-
const checkVal = validatorFns[type];
|
|
2479
|
-
if (typeof checkVal !== "function")
|
|
2480
|
-
throw new Error(`Invalid validator "${type}", expected function`);
|
|
2481
|
-
const val = object[fieldName];
|
|
2482
|
-
if (isOptional && val === void 0)
|
|
2483
|
-
return;
|
|
2484
|
-
if (!checkVal(val, object)) {
|
|
2485
|
-
throw new Error(`Invalid param ${String(fieldName)}=${val} (${typeof val}), expected ${type}`);
|
|
2486
|
-
}
|
|
2487
|
-
};
|
|
2488
|
-
for (const [fieldName, type] of Object.entries(validators))
|
|
2489
|
-
checkField(fieldName, type, false);
|
|
2490
|
-
for (const [fieldName, type] of Object.entries(optValidators))
|
|
2491
|
-
checkField(fieldName, type, true);
|
|
2492
|
-
return object;
|
|
2493
|
-
}
|
|
2494
|
-
|
|
2495
|
-
// ../node_modules/@noble/curves/esm/abstract/modular.js
|
|
2496
|
-
var _0n2 = BigInt(0);
|
|
2497
|
-
var _1n2 = BigInt(1);
|
|
2498
|
-
var _2n2 = BigInt(2);
|
|
2499
|
-
var _3n = BigInt(3);
|
|
2500
|
-
var _4n = BigInt(4);
|
|
2501
|
-
var _5n = BigInt(5);
|
|
2502
|
-
var _8n = BigInt(8);
|
|
2503
|
-
var _9n = BigInt(9);
|
|
2504
|
-
var _16n = BigInt(16);
|
|
2505
|
-
function mod(a, b) {
|
|
2506
|
-
const result = a % b;
|
|
2507
|
-
return result >= _0n2 ? result : b + result;
|
|
2508
|
-
}
|
|
2509
|
-
function pow(num10, power, modulo) {
|
|
2510
|
-
if (modulo <= _0n2 || power < _0n2)
|
|
2511
|
-
throw new Error("Expected power/modulo > 0");
|
|
2512
|
-
if (modulo === _1n2)
|
|
2513
|
-
return _0n2;
|
|
2514
|
-
let res = _1n2;
|
|
2515
|
-
while (power > _0n2) {
|
|
2516
|
-
if (power & _1n2)
|
|
2517
|
-
res = res * num10 % modulo;
|
|
2518
|
-
num10 = num10 * num10 % modulo;
|
|
2519
|
-
power >>= _1n2;
|
|
2520
|
-
}
|
|
2521
|
-
return res;
|
|
2522
|
-
}
|
|
2523
|
-
function invert(number2, modulo) {
|
|
2524
|
-
if (number2 === _0n2 || modulo <= _0n2) {
|
|
2525
|
-
throw new Error(`invert: expected positive integers, got n=${number2} mod=${modulo}`);
|
|
2526
|
-
}
|
|
2527
|
-
let a = mod(number2, modulo);
|
|
2528
|
-
let b = modulo;
|
|
2529
|
-
let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
|
|
2530
|
-
while (a !== _0n2) {
|
|
2531
|
-
const q = b / a;
|
|
2532
|
-
const r = b % a;
|
|
2533
|
-
const m = x - u * q;
|
|
2534
|
-
const n = y - v * q;
|
|
2535
|
-
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
2536
|
-
}
|
|
2537
|
-
const gcd = b;
|
|
2538
|
-
if (gcd !== _1n2)
|
|
2539
|
-
throw new Error("invert: does not exist");
|
|
2540
|
-
return mod(x, modulo);
|
|
2541
|
-
}
|
|
2542
|
-
function tonelliShanks(P) {
|
|
2543
|
-
const legendreC = (P - _1n2) / _2n2;
|
|
2544
|
-
let Q, S, Z;
|
|
2545
|
-
for (Q = P - _1n2, S = 0; Q % _2n2 === _0n2; Q /= _2n2, S++)
|
|
2546
|
-
;
|
|
2547
|
-
for (Z = _2n2; Z < P && pow(Z, legendreC, P) !== P - _1n2; Z++)
|
|
2548
|
-
;
|
|
2549
|
-
if (S === 1) {
|
|
2550
|
-
const p1div4 = (P + _1n2) / _4n;
|
|
2551
|
-
return function tonelliFast(Fp, n) {
|
|
2552
|
-
const root = Fp.pow(n, p1div4);
|
|
2553
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
|
2554
|
-
throw new Error("Cannot find square root");
|
|
2555
|
-
return root;
|
|
2556
|
-
};
|
|
2557
|
-
}
|
|
2558
|
-
const Q1div2 = (Q + _1n2) / _2n2;
|
|
2559
|
-
return function tonelliSlow(Fp, n) {
|
|
2560
|
-
if (Fp.pow(n, legendreC) === Fp.neg(Fp.ONE))
|
|
2561
|
-
throw new Error("Cannot find square root");
|
|
2562
|
-
let r = S;
|
|
2563
|
-
let g = Fp.pow(Fp.mul(Fp.ONE, Z), Q);
|
|
2564
|
-
let x = Fp.pow(n, Q1div2);
|
|
2565
|
-
let b = Fp.pow(n, Q);
|
|
2566
|
-
while (!Fp.eql(b, Fp.ONE)) {
|
|
2567
|
-
if (Fp.eql(b, Fp.ZERO))
|
|
2568
|
-
return Fp.ZERO;
|
|
2569
|
-
let m = 1;
|
|
2570
|
-
for (let t2 = Fp.sqr(b); m < r; m++) {
|
|
2571
|
-
if (Fp.eql(t2, Fp.ONE))
|
|
2572
|
-
break;
|
|
2573
|
-
t2 = Fp.sqr(t2);
|
|
2574
|
-
}
|
|
2575
|
-
const ge = Fp.pow(g, _1n2 << BigInt(r - m - 1));
|
|
2576
|
-
g = Fp.sqr(ge);
|
|
2577
|
-
x = Fp.mul(x, ge);
|
|
2578
|
-
b = Fp.mul(b, g);
|
|
2579
|
-
r = m;
|
|
2580
|
-
}
|
|
2581
|
-
return x;
|
|
2582
|
-
};
|
|
2583
|
-
}
|
|
2584
|
-
function FpSqrt(P) {
|
|
2585
|
-
if (P % _4n === _3n) {
|
|
2586
|
-
const p1div4 = (P + _1n2) / _4n;
|
|
2587
|
-
return function sqrt3mod4(Fp, n) {
|
|
2588
|
-
const root = Fp.pow(n, p1div4);
|
|
2589
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
|
2590
|
-
throw new Error("Cannot find square root");
|
|
2591
|
-
return root;
|
|
2592
|
-
};
|
|
2593
|
-
}
|
|
2594
|
-
if (P % _8n === _5n) {
|
|
2595
|
-
const c1 = (P - _5n) / _8n;
|
|
2596
|
-
return function sqrt5mod8(Fp, n) {
|
|
2597
|
-
const n2 = Fp.mul(n, _2n2);
|
|
2598
|
-
const v = Fp.pow(n2, c1);
|
|
2599
|
-
const nv = Fp.mul(n, v);
|
|
2600
|
-
const i = Fp.mul(Fp.mul(nv, _2n2), v);
|
|
2601
|
-
const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
|
|
2602
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
|
2603
|
-
throw new Error("Cannot find square root");
|
|
2604
|
-
return root;
|
|
2605
|
-
};
|
|
2606
|
-
}
|
|
2607
|
-
if (P % _16n === _9n) {
|
|
2608
|
-
}
|
|
2609
|
-
return tonelliShanks(P);
|
|
2610
|
-
}
|
|
2611
|
-
var FIELD_FIELDS = [
|
|
2612
|
-
"create",
|
|
2613
|
-
"isValid",
|
|
2614
|
-
"is0",
|
|
2615
|
-
"neg",
|
|
2616
|
-
"inv",
|
|
2617
|
-
"sqrt",
|
|
2618
|
-
"sqr",
|
|
2619
|
-
"eql",
|
|
2620
|
-
"add",
|
|
2621
|
-
"sub",
|
|
2622
|
-
"mul",
|
|
2623
|
-
"pow",
|
|
2624
|
-
"div",
|
|
2625
|
-
"addN",
|
|
2626
|
-
"subN",
|
|
2627
|
-
"mulN",
|
|
2628
|
-
"sqrN"
|
|
2629
|
-
];
|
|
2630
|
-
function validateField(field) {
|
|
2631
|
-
const initial = {
|
|
2632
|
-
ORDER: "bigint",
|
|
2633
|
-
MASK: "bigint",
|
|
2634
|
-
BYTES: "isSafeInteger",
|
|
2635
|
-
BITS: "isSafeInteger"
|
|
2636
|
-
};
|
|
2637
|
-
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
2638
|
-
map[val] = "function";
|
|
2639
|
-
return map;
|
|
2640
|
-
}, initial);
|
|
2641
|
-
return validateObject(field, opts);
|
|
2642
|
-
}
|
|
2643
|
-
function FpPow(f, num10, power) {
|
|
2644
|
-
if (power < _0n2)
|
|
2645
|
-
throw new Error("Expected power > 0");
|
|
2646
|
-
if (power === _0n2)
|
|
2647
|
-
return f.ONE;
|
|
2648
|
-
if (power === _1n2)
|
|
2649
|
-
return num10;
|
|
2650
|
-
let p = f.ONE;
|
|
2651
|
-
let d = num10;
|
|
2652
|
-
while (power > _0n2) {
|
|
2653
|
-
if (power & _1n2)
|
|
2654
|
-
p = f.mul(p, d);
|
|
2655
|
-
d = f.sqr(d);
|
|
2656
|
-
power >>= _1n2;
|
|
2657
|
-
}
|
|
2658
|
-
return p;
|
|
2659
|
-
}
|
|
2660
|
-
function FpInvertBatch(f, nums) {
|
|
2661
|
-
const tmp = new Array(nums.length);
|
|
2662
|
-
const lastMultiplied = nums.reduce((acc, num10, i) => {
|
|
2663
|
-
if (f.is0(num10))
|
|
2664
|
-
return acc;
|
|
2665
|
-
tmp[i] = acc;
|
|
2666
|
-
return f.mul(acc, num10);
|
|
2667
|
-
}, f.ONE);
|
|
2668
|
-
const inverted = f.inv(lastMultiplied);
|
|
2669
|
-
nums.reduceRight((acc, num10, i) => {
|
|
2670
|
-
if (f.is0(num10))
|
|
2671
|
-
return acc;
|
|
2672
|
-
tmp[i] = f.mul(acc, tmp[i]);
|
|
2673
|
-
return f.mul(acc, num10);
|
|
2674
|
-
}, inverted);
|
|
2675
|
-
return tmp;
|
|
2676
|
-
}
|
|
2677
|
-
function nLength(n, nBitLength2) {
|
|
2678
|
-
const _nBitLength = nBitLength2 !== void 0 ? nBitLength2 : n.toString(2).length;
|
|
2679
|
-
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
2680
|
-
return { nBitLength: _nBitLength, nByteLength };
|
|
2681
|
-
}
|
|
2682
|
-
function Field(ORDER, bitLen2, isLE2 = false, redef = {}) {
|
|
2683
|
-
if (ORDER <= _0n2)
|
|
2684
|
-
throw new Error(`Expected Field ORDER > 0, got ${ORDER}`);
|
|
2685
|
-
const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, bitLen2);
|
|
2686
|
-
if (BYTES > 2048)
|
|
2687
|
-
throw new Error("Field lengths over 2048 bytes are not supported");
|
|
2688
|
-
const sqrtP = FpSqrt(ORDER);
|
|
2689
|
-
const f = Object.freeze({
|
|
2690
|
-
ORDER,
|
|
2691
|
-
BITS,
|
|
2692
|
-
BYTES,
|
|
2693
|
-
MASK: bitMask(BITS),
|
|
2694
|
-
ZERO: _0n2,
|
|
2695
|
-
ONE: _1n2,
|
|
2696
|
-
create: (num10) => mod(num10, ORDER),
|
|
2697
|
-
isValid: (num10) => {
|
|
2698
|
-
if (typeof num10 !== "bigint")
|
|
2699
|
-
throw new Error(`Invalid field element: expected bigint, got ${typeof num10}`);
|
|
2700
|
-
return _0n2 <= num10 && num10 < ORDER;
|
|
2701
|
-
},
|
|
2702
|
-
is0: (num10) => num10 === _0n2,
|
|
2703
|
-
isOdd: (num10) => (num10 & _1n2) === _1n2,
|
|
2704
|
-
neg: (num10) => mod(-num10, ORDER),
|
|
2705
|
-
eql: (lhs, rhs) => lhs === rhs,
|
|
2706
|
-
sqr: (num10) => mod(num10 * num10, ORDER),
|
|
2707
|
-
add: (lhs, rhs) => mod(lhs + rhs, ORDER),
|
|
2708
|
-
sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
|
|
2709
|
-
mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
|
|
2710
|
-
pow: (num10, power) => FpPow(f, num10, power),
|
|
2711
|
-
div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
|
|
2712
|
-
// Same as above, but doesn't normalize
|
|
2713
|
-
sqrN: (num10) => num10 * num10,
|
|
2714
|
-
addN: (lhs, rhs) => lhs + rhs,
|
|
2715
|
-
subN: (lhs, rhs) => lhs - rhs,
|
|
2716
|
-
mulN: (lhs, rhs) => lhs * rhs,
|
|
2717
|
-
inv: (num10) => invert(num10, ORDER),
|
|
2718
|
-
sqrt: redef.sqrt || ((n) => sqrtP(f, n)),
|
|
2719
|
-
invertBatch: (lst) => FpInvertBatch(f, lst),
|
|
2720
|
-
// TODO: do we really need constant cmov?
|
|
2721
|
-
// We don't have const-time bigints anyway, so probably will be not very useful
|
|
2722
|
-
cmov: (a, b, c) => c ? b : a,
|
|
2723
|
-
toBytes: (num10) => isLE2 ? numberToBytesLE(num10, BYTES) : numberToBytesBE(num10, BYTES),
|
|
2724
|
-
fromBytes: (bytes2) => {
|
|
2725
|
-
if (bytes2.length !== BYTES)
|
|
2726
|
-
throw new Error(`Fp.fromBytes: expected ${BYTES}, got ${bytes2.length}`);
|
|
2727
|
-
return isLE2 ? bytesToNumberLE(bytes2) : bytesToNumberBE(bytes2);
|
|
2728
|
-
}
|
|
2729
|
-
});
|
|
2730
|
-
return Object.freeze(f);
|
|
2731
|
-
}
|
|
2732
|
-
function getFieldBytesLength(fieldOrder) {
|
|
2733
|
-
if (typeof fieldOrder !== "bigint")
|
|
2734
|
-
throw new Error("field order must be bigint");
|
|
2735
|
-
const bitLength = fieldOrder.toString(2).length;
|
|
2736
|
-
return Math.ceil(bitLength / 8);
|
|
2737
|
-
}
|
|
2738
|
-
function getMinHashLength(fieldOrder) {
|
|
2739
|
-
const length = getFieldBytesLength(fieldOrder);
|
|
2740
|
-
return length + Math.ceil(length / 2);
|
|
2741
|
-
}
|
|
2742
|
-
function mapHashToField(key, fieldOrder, isLE2 = false) {
|
|
2743
|
-
const len = key.length;
|
|
2744
|
-
const fieldLen = getFieldBytesLength(fieldOrder);
|
|
2745
|
-
const minLen = getMinHashLength(fieldOrder);
|
|
2746
|
-
if (len < 16 || len < minLen || len > 1024)
|
|
2747
|
-
throw new Error(`expected ${minLen}-1024 bytes of input, got ${len}`);
|
|
2748
|
-
const num10 = isLE2 ? bytesToNumberBE(key) : bytesToNumberLE(key);
|
|
2749
|
-
const reduced = mod(num10, fieldOrder - _1n2) + _1n2;
|
|
2750
|
-
return isLE2 ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
2751
|
-
}
|
|
2752
|
-
|
|
2753
|
-
// ../node_modules/@noble/curves/esm/abstract/poseidon.js
|
|
2754
|
-
function validateOpts(opts) {
|
|
2755
|
-
const { Fp, mds, reversePartialPowIdx: rev, roundConstants: rc } = opts;
|
|
2756
|
-
const { roundsFull, roundsPartial, sboxPower, t } = opts;
|
|
2757
|
-
validateField(Fp);
|
|
2758
|
-
for (const i of ["t", "roundsFull", "roundsPartial"]) {
|
|
2759
|
-
if (typeof opts[i] !== "number" || !Number.isSafeInteger(opts[i]))
|
|
2760
|
-
throw new Error(`Poseidon: invalid param ${i}=${opts[i]} (${typeof opts[i]})`);
|
|
2761
|
-
}
|
|
2762
|
-
if (!Array.isArray(mds) || mds.length !== t)
|
|
2763
|
-
throw new Error("Poseidon: wrong MDS matrix");
|
|
2764
|
-
const _mds = mds.map((mdsRow) => {
|
|
2765
|
-
if (!Array.isArray(mdsRow) || mdsRow.length !== t)
|
|
2766
|
-
throw new Error(`Poseidon MDS matrix row: ${mdsRow}`);
|
|
2767
|
-
return mdsRow.map((i) => {
|
|
2768
|
-
if (typeof i !== "bigint")
|
|
2769
|
-
throw new Error(`Poseidon MDS matrix value=${i}`);
|
|
2770
|
-
return Fp.create(i);
|
|
2771
|
-
});
|
|
2772
|
-
});
|
|
2773
|
-
if (rev !== void 0 && typeof rev !== "boolean")
|
|
2774
|
-
throw new Error(`Poseidon: invalid param reversePartialPowIdx=${rev}`);
|
|
2775
|
-
if (roundsFull % 2 !== 0)
|
|
2776
|
-
throw new Error(`Poseidon roundsFull is not even: ${roundsFull}`);
|
|
2777
|
-
const rounds = roundsFull + roundsPartial;
|
|
2778
|
-
if (!Array.isArray(rc) || rc.length !== rounds)
|
|
2779
|
-
throw new Error("Poseidon: wrong round constants");
|
|
2780
|
-
const roundConstants = rc.map((rc2) => {
|
|
2781
|
-
if (!Array.isArray(rc2) || rc2.length !== t)
|
|
2782
|
-
throw new Error(`Poseidon wrong round constants: ${rc2}`);
|
|
2783
|
-
return rc2.map((i) => {
|
|
2784
|
-
if (typeof i !== "bigint" || !Fp.isValid(i))
|
|
2785
|
-
throw new Error(`Poseidon wrong round constant=${i}`);
|
|
2786
|
-
return Fp.create(i);
|
|
2787
|
-
});
|
|
2788
|
-
});
|
|
2789
|
-
if (!sboxPower || ![3, 5, 7].includes(sboxPower))
|
|
2790
|
-
throw new Error(`Poseidon wrong sboxPower=${sboxPower}`);
|
|
2791
|
-
const _sboxPower = BigInt(sboxPower);
|
|
2792
|
-
let sboxFn = (n) => FpPow(Fp, n, _sboxPower);
|
|
2793
|
-
if (sboxPower === 3)
|
|
2794
|
-
sboxFn = (n) => Fp.mul(Fp.sqrN(n), n);
|
|
2795
|
-
else if (sboxPower === 5)
|
|
2796
|
-
sboxFn = (n) => Fp.mul(Fp.sqrN(Fp.sqrN(n)), n);
|
|
2797
|
-
return Object.freeze({ ...opts, rounds, sboxFn, roundConstants, mds: _mds });
|
|
2798
|
-
}
|
|
2799
|
-
function poseidon(opts) {
|
|
2800
|
-
const _opts = validateOpts(opts);
|
|
2801
|
-
const { Fp, mds, roundConstants, rounds, roundsPartial, sboxFn, t } = _opts;
|
|
2802
|
-
const halfRoundsFull = _opts.roundsFull / 2;
|
|
2803
|
-
const partialIdx = _opts.reversePartialPowIdx ? t - 1 : 0;
|
|
2804
|
-
const poseidonRound = (values, isFull, idx) => {
|
|
2805
|
-
values = values.map((i, j) => Fp.add(i, roundConstants[idx][j]));
|
|
2806
|
-
if (isFull)
|
|
2807
|
-
values = values.map((i) => sboxFn(i));
|
|
2808
|
-
else
|
|
2809
|
-
values[partialIdx] = sboxFn(values[partialIdx]);
|
|
2810
|
-
values = mds.map((i) => i.reduce((acc, i2, j) => Fp.add(acc, Fp.mulN(i2, values[j])), Fp.ZERO));
|
|
2811
|
-
return values;
|
|
2812
|
-
};
|
|
2813
|
-
const poseidonHash = function poseidonHash2(values) {
|
|
2814
|
-
if (!Array.isArray(values) || values.length !== t)
|
|
2815
|
-
throw new Error(`Poseidon: wrong values (expected array of bigints with length ${t})`);
|
|
2816
|
-
values = values.map((i) => {
|
|
2817
|
-
if (typeof i !== "bigint")
|
|
2818
|
-
throw new Error(`Poseidon: wrong value=${i} (${typeof i})`);
|
|
2819
|
-
return Fp.create(i);
|
|
2820
|
-
});
|
|
2821
|
-
let round = 0;
|
|
2822
|
-
for (let i = 0; i < halfRoundsFull; i++)
|
|
2823
|
-
values = poseidonRound(values, true, round++);
|
|
2824
|
-
for (let i = 0; i < roundsPartial; i++)
|
|
2825
|
-
values = poseidonRound(values, false, round++);
|
|
2826
|
-
for (let i = 0; i < halfRoundsFull; i++)
|
|
2827
|
-
values = poseidonRound(values, true, round++);
|
|
2828
|
-
if (round !== rounds)
|
|
2829
|
-
throw new Error(`Poseidon: wrong number of rounds: last round=${round}, total=${rounds}`);
|
|
2830
|
-
return values;
|
|
2831
|
-
};
|
|
2832
|
-
poseidonHash.roundConstants = roundConstants;
|
|
2833
|
-
return poseidonHash;
|
|
2834
|
-
}
|
|
2835
|
-
|
|
2836
|
-
// ../node_modules/@noble/curves/esm/abstract/curve.js
|
|
2837
|
-
var _0n3 = BigInt(0);
|
|
2838
|
-
var _1n3 = BigInt(1);
|
|
2839
|
-
function wNAF(c, bits) {
|
|
2840
|
-
const constTimeNegate = (condition, item) => {
|
|
2841
|
-
const neg = item.negate();
|
|
2842
|
-
return condition ? neg : item;
|
|
2843
|
-
};
|
|
2844
|
-
const opts = (W) => {
|
|
2845
|
-
const windows = Math.ceil(bits / W) + 1;
|
|
2846
|
-
const windowSize = 2 ** (W - 1);
|
|
2847
|
-
return { windows, windowSize };
|
|
2848
|
-
};
|
|
2849
|
-
return {
|
|
2850
|
-
constTimeNegate,
|
|
2851
|
-
// non-const time multiplication ladder
|
|
2852
|
-
unsafeLadder(elm, n) {
|
|
2853
|
-
let p = c.ZERO;
|
|
2854
|
-
let d = elm;
|
|
2855
|
-
while (n > _0n3) {
|
|
2856
|
-
if (n & _1n3)
|
|
2857
|
-
p = p.add(d);
|
|
2858
|
-
d = d.double();
|
|
2859
|
-
n >>= _1n3;
|
|
2860
|
-
}
|
|
2861
|
-
return p;
|
|
2862
|
-
},
|
|
2863
|
-
/**
|
|
2864
|
-
* Creates a wNAF precomputation window. Used for caching.
|
|
2865
|
-
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
2866
|
-
* Number of precomputed points depends on the curve size:
|
|
2867
|
-
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
2868
|
-
* - 𝑊 is the window size
|
|
2869
|
-
* - 𝑛 is the bitlength of the curve order.
|
|
2870
|
-
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
2871
|
-
* @returns precomputed point tables flattened to a single array
|
|
2872
|
-
*/
|
|
2873
|
-
precomputeWindow(elm, W) {
|
|
2874
|
-
const { windows, windowSize } = opts(W);
|
|
2875
|
-
const points = [];
|
|
2876
|
-
let p = elm;
|
|
2877
|
-
let base = p;
|
|
2878
|
-
for (let window = 0; window < windows; window++) {
|
|
2879
|
-
base = p;
|
|
2880
|
-
points.push(base);
|
|
2881
|
-
for (let i = 1; i < windowSize; i++) {
|
|
2882
|
-
base = base.add(p);
|
|
2883
|
-
points.push(base);
|
|
2884
|
-
}
|
|
2885
|
-
p = base.double();
|
|
2886
|
-
}
|
|
2887
|
-
return points;
|
|
2888
|
-
},
|
|
2889
|
-
/**
|
|
2890
|
-
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
2891
|
-
* @param W window size
|
|
2892
|
-
* @param precomputes precomputed tables
|
|
2893
|
-
* @param n scalar (we don't check here, but should be less than curve order)
|
|
2894
|
-
* @returns real and fake (for const-time) points
|
|
2895
|
-
*/
|
|
2896
|
-
wNAF(W, precomputes, n) {
|
|
2897
|
-
const { windows, windowSize } = opts(W);
|
|
2898
|
-
let p = c.ZERO;
|
|
2899
|
-
let f = c.BASE;
|
|
2900
|
-
const mask = BigInt(2 ** W - 1);
|
|
2901
|
-
const maxNumber = 2 ** W;
|
|
2902
|
-
const shiftBy = BigInt(W);
|
|
2903
|
-
for (let window = 0; window < windows; window++) {
|
|
2904
|
-
const offset = window * windowSize;
|
|
2905
|
-
let wbits = Number(n & mask);
|
|
2906
|
-
n >>= shiftBy;
|
|
2907
|
-
if (wbits > windowSize) {
|
|
2908
|
-
wbits -= maxNumber;
|
|
2909
|
-
n += _1n3;
|
|
2910
|
-
}
|
|
2911
|
-
const offset1 = offset;
|
|
2912
|
-
const offset2 = offset + Math.abs(wbits) - 1;
|
|
2913
|
-
const cond1 = window % 2 !== 0;
|
|
2914
|
-
const cond2 = wbits < 0;
|
|
2915
|
-
if (wbits === 0) {
|
|
2916
|
-
f = f.add(constTimeNegate(cond1, precomputes[offset1]));
|
|
2917
|
-
} else {
|
|
2918
|
-
p = p.add(constTimeNegate(cond2, precomputes[offset2]));
|
|
2919
|
-
}
|
|
2920
|
-
}
|
|
2921
|
-
return { p, f };
|
|
2922
|
-
},
|
|
2923
|
-
wNAFCached(P, precomputesMap, n, transform) {
|
|
2924
|
-
const W = P._WINDOW_SIZE || 1;
|
|
2925
|
-
let comp = precomputesMap.get(P);
|
|
2926
|
-
if (!comp) {
|
|
2927
|
-
comp = this.precomputeWindow(P, W);
|
|
2928
|
-
if (W !== 1) {
|
|
2929
|
-
precomputesMap.set(P, transform(comp));
|
|
2930
|
-
}
|
|
2931
|
-
}
|
|
2932
|
-
return this.wNAF(W, comp, n);
|
|
2933
|
-
}
|
|
2934
|
-
};
|
|
2935
|
-
}
|
|
2936
|
-
function validateBasic(curve2) {
|
|
2937
|
-
validateField(curve2.Fp);
|
|
2938
|
-
validateObject(curve2, {
|
|
2939
|
-
n: "bigint",
|
|
2940
|
-
h: "bigint",
|
|
2941
|
-
Gx: "field",
|
|
2942
|
-
Gy: "field"
|
|
2943
|
-
}, {
|
|
2944
|
-
nBitLength: "isSafeInteger",
|
|
2945
|
-
nByteLength: "isSafeInteger"
|
|
2946
|
-
});
|
|
2947
|
-
return Object.freeze({
|
|
2948
|
-
...nLength(curve2.n, curve2.nBitLength),
|
|
2949
|
-
...curve2,
|
|
2950
|
-
...{ p: curve2.Fp.ORDER }
|
|
2951
|
-
});
|
|
2952
|
-
}
|
|
2953
|
-
|
|
2954
|
-
// ../node_modules/@noble/curves/esm/abstract/weierstrass.js
|
|
2955
|
-
function validatePointOpts(curve2) {
|
|
2956
|
-
const opts = validateBasic(curve2);
|
|
2957
|
-
validateObject(opts, {
|
|
2958
|
-
a: "field",
|
|
2959
|
-
b: "field"
|
|
2960
|
-
}, {
|
|
2961
|
-
allowedPrivateKeyLengths: "array",
|
|
2962
|
-
wrapPrivateKey: "boolean",
|
|
2963
|
-
isTorsionFree: "function",
|
|
2964
|
-
clearCofactor: "function",
|
|
2965
|
-
allowInfinityPoint: "boolean",
|
|
2966
|
-
fromBytes: "function",
|
|
2967
|
-
toBytes: "function"
|
|
2968
|
-
});
|
|
2969
|
-
const { endo, Fp, a } = opts;
|
|
2970
|
-
if (endo) {
|
|
2971
|
-
if (!Fp.eql(a, Fp.ZERO)) {
|
|
2972
|
-
throw new Error("Endomorphism can only be defined for Koblitz curves that have a=0");
|
|
2973
|
-
}
|
|
2974
|
-
if (typeof endo !== "object" || typeof endo.beta !== "bigint" || typeof endo.splitScalar !== "function") {
|
|
2975
|
-
throw new Error("Expected endomorphism with beta: bigint and splitScalar: function");
|
|
2976
|
-
}
|
|
2977
|
-
}
|
|
2978
|
-
return Object.freeze({ ...opts });
|
|
2979
|
-
}
|
|
2980
|
-
var { bytesToNumberBE: b2n, hexToBytes: h2b } = utils_exports;
|
|
2981
|
-
var DER = {
|
|
2982
|
-
// asn.1 DER encoding utils
|
|
2983
|
-
Err: class DERErr extends Error {
|
|
2984
|
-
constructor(m = "") {
|
|
2985
|
-
super(m);
|
|
2986
|
-
}
|
|
2987
|
-
},
|
|
2988
|
-
_parseInt(data) {
|
|
2989
|
-
const { Err: E } = DER;
|
|
2990
|
-
if (data.length < 2 || data[0] !== 2)
|
|
2991
|
-
throw new E("Invalid signature integer tag");
|
|
2992
|
-
const len = data[1];
|
|
2993
|
-
const res = data.subarray(2, len + 2);
|
|
2994
|
-
if (!len || res.length !== len)
|
|
2995
|
-
throw new E("Invalid signature integer: wrong length");
|
|
2996
|
-
if (res[0] & 128)
|
|
2997
|
-
throw new E("Invalid signature integer: negative");
|
|
2998
|
-
if (res[0] === 0 && !(res[1] & 128))
|
|
2999
|
-
throw new E("Invalid signature integer: unnecessary leading zero");
|
|
3000
|
-
return { d: b2n(res), l: data.subarray(len + 2) };
|
|
3001
|
-
},
|
|
3002
|
-
toSig(hex) {
|
|
3003
|
-
const { Err: E } = DER;
|
|
3004
|
-
const data = typeof hex === "string" ? h2b(hex) : hex;
|
|
3005
|
-
if (!(data instanceof Uint8Array))
|
|
3006
|
-
throw new Error("ui8a expected");
|
|
3007
|
-
let l = data.length;
|
|
3008
|
-
if (l < 2 || data[0] != 48)
|
|
3009
|
-
throw new E("Invalid signature tag");
|
|
3010
|
-
if (data[1] !== l - 2)
|
|
3011
|
-
throw new E("Invalid signature: incorrect length");
|
|
3012
|
-
const { d: r, l: sBytes } = DER._parseInt(data.subarray(2));
|
|
3013
|
-
const { d: s, l: rBytesLeft } = DER._parseInt(sBytes);
|
|
3014
|
-
if (rBytesLeft.length)
|
|
3015
|
-
throw new E("Invalid signature: left bytes after parsing");
|
|
3016
|
-
return { r, s };
|
|
3017
|
-
},
|
|
3018
|
-
hexFromSig(sig) {
|
|
3019
|
-
const slice = (s2) => Number.parseInt(s2[0], 16) & 8 ? "00" + s2 : s2;
|
|
3020
|
-
const h = (num10) => {
|
|
3021
|
-
const hex = num10.toString(16);
|
|
3022
|
-
return hex.length & 1 ? `0${hex}` : hex;
|
|
3023
|
-
};
|
|
3024
|
-
const s = slice(h(sig.s));
|
|
3025
|
-
const r = slice(h(sig.r));
|
|
3026
|
-
const shl = s.length / 2;
|
|
3027
|
-
const rhl = r.length / 2;
|
|
3028
|
-
const sl = h(shl);
|
|
3029
|
-
const rl = h(rhl);
|
|
3030
|
-
return `30${h(rhl + shl + 4)}02${rl}${r}02${sl}${s}`;
|
|
3031
|
-
}
|
|
3032
|
-
};
|
|
3033
|
-
var _0n4 = BigInt(0);
|
|
3034
|
-
var _1n4 = BigInt(1);
|
|
3035
|
-
var _2n3 = BigInt(2);
|
|
3036
|
-
var _3n2 = BigInt(3);
|
|
3037
|
-
var _4n2 = BigInt(4);
|
|
3038
|
-
function weierstrassPoints(opts) {
|
|
3039
|
-
const CURVE2 = validatePointOpts(opts);
|
|
3040
|
-
const { Fp } = CURVE2;
|
|
3041
|
-
const toBytes2 = CURVE2.toBytes || ((_c, point, _isCompressed) => {
|
|
3042
|
-
const a = point.toAffine();
|
|
3043
|
-
return concatBytes2(Uint8Array.from([4]), Fp.toBytes(a.x), Fp.toBytes(a.y));
|
|
3044
|
-
});
|
|
3045
|
-
const fromBytes = CURVE2.fromBytes || ((bytes2) => {
|
|
3046
|
-
const tail = bytes2.subarray(1);
|
|
3047
|
-
const x = Fp.fromBytes(tail.subarray(0, Fp.BYTES));
|
|
3048
|
-
const y = Fp.fromBytes(tail.subarray(Fp.BYTES, 2 * Fp.BYTES));
|
|
3049
|
-
return { x, y };
|
|
3050
|
-
});
|
|
3051
|
-
function weierstrassEquation(x) {
|
|
3052
|
-
const { a, b } = CURVE2;
|
|
3053
|
-
const x2 = Fp.sqr(x);
|
|
3054
|
-
const x3 = Fp.mul(x2, x);
|
|
3055
|
-
return Fp.add(Fp.add(x3, Fp.mul(x, a)), b);
|
|
3056
|
-
}
|
|
3057
|
-
if (!Fp.eql(Fp.sqr(CURVE2.Gy), weierstrassEquation(CURVE2.Gx)))
|
|
3058
|
-
throw new Error("bad generator point: equation left != right");
|
|
3059
|
-
function isWithinCurveOrder(num10) {
|
|
3060
|
-
return typeof num10 === "bigint" && _0n4 < num10 && num10 < CURVE2.n;
|
|
3061
|
-
}
|
|
3062
|
-
function assertGE(num10) {
|
|
3063
|
-
if (!isWithinCurveOrder(num10))
|
|
3064
|
-
throw new Error("Expected valid bigint: 0 < bigint < curve.n");
|
|
3065
|
-
}
|
|
3066
|
-
function normPrivateKeyToScalar(key) {
|
|
3067
|
-
const { allowedPrivateKeyLengths: lengths, nByteLength, wrapPrivateKey, n } = CURVE2;
|
|
3068
|
-
if (lengths && typeof key !== "bigint") {
|
|
3069
|
-
if (key instanceof Uint8Array)
|
|
3070
|
-
key = bytesToHex(key);
|
|
3071
|
-
if (typeof key !== "string" || !lengths.includes(key.length))
|
|
3072
|
-
throw new Error("Invalid key");
|
|
3073
|
-
key = key.padStart(nByteLength * 2, "0");
|
|
3074
|
-
}
|
|
3075
|
-
let num10;
|
|
3076
|
-
try {
|
|
3077
|
-
num10 = typeof key === "bigint" ? key : bytesToNumberBE(ensureBytes("private key", key, nByteLength));
|
|
3078
|
-
} catch (error) {
|
|
3079
|
-
throw new Error(`private key must be ${nByteLength} bytes, hex or bigint, not ${typeof key}`);
|
|
3080
|
-
}
|
|
3081
|
-
if (wrapPrivateKey)
|
|
3082
|
-
num10 = mod(num10, n);
|
|
3083
|
-
assertGE(num10);
|
|
3084
|
-
return num10;
|
|
3085
|
-
}
|
|
3086
|
-
const pointPrecomputes = /* @__PURE__ */ new Map();
|
|
3087
|
-
function assertPrjPoint(other) {
|
|
3088
|
-
if (!(other instanceof Point))
|
|
3089
|
-
throw new Error("ProjectivePoint expected");
|
|
3090
|
-
}
|
|
3091
|
-
class Point {
|
|
3092
|
-
constructor(px, py, pz) {
|
|
3093
|
-
this.px = px;
|
|
3094
|
-
this.py = py;
|
|
3095
|
-
this.pz = pz;
|
|
3096
|
-
if (px == null || !Fp.isValid(px))
|
|
3097
|
-
throw new Error("x required");
|
|
3098
|
-
if (py == null || !Fp.isValid(py))
|
|
3099
|
-
throw new Error("y required");
|
|
3100
|
-
if (pz == null || !Fp.isValid(pz))
|
|
3101
|
-
throw new Error("z required");
|
|
3102
|
-
}
|
|
3103
|
-
// Does not validate if the point is on-curve.
|
|
3104
|
-
// Use fromHex instead, or call assertValidity() later.
|
|
3105
|
-
static fromAffine(p) {
|
|
3106
|
-
const { x, y } = p || {};
|
|
3107
|
-
if (!p || !Fp.isValid(x) || !Fp.isValid(y))
|
|
3108
|
-
throw new Error("invalid affine point");
|
|
3109
|
-
if (p instanceof Point)
|
|
3110
|
-
throw new Error("projective point not allowed");
|
|
3111
|
-
const is0 = (i) => Fp.eql(i, Fp.ZERO);
|
|
3112
|
-
if (is0(x) && is0(y))
|
|
3113
|
-
return Point.ZERO;
|
|
3114
|
-
return new Point(x, y, Fp.ONE);
|
|
3115
|
-
}
|
|
3116
|
-
get x() {
|
|
3117
|
-
return this.toAffine().x;
|
|
3118
|
-
}
|
|
3119
|
-
get y() {
|
|
3120
|
-
return this.toAffine().y;
|
|
3121
|
-
}
|
|
3122
|
-
/**
|
|
3123
|
-
* Takes a bunch of Projective Points but executes only one
|
|
3124
|
-
* inversion on all of them. Inversion is very slow operation,
|
|
3125
|
-
* so this improves performance massively.
|
|
3126
|
-
* Optimization: converts a list of projective points to a list of identical points with Z=1.
|
|
3127
|
-
*/
|
|
3128
|
-
static normalizeZ(points) {
|
|
3129
|
-
const toInv = Fp.invertBatch(points.map((p) => p.pz));
|
|
3130
|
-
return points.map((p, i) => p.toAffine(toInv[i])).map(Point.fromAffine);
|
|
3131
|
-
}
|
|
3132
|
-
/**
|
|
3133
|
-
* Converts hash string or Uint8Array to Point.
|
|
3134
|
-
* @param hex short/long ECDSA hex
|
|
3135
|
-
*/
|
|
3136
|
-
static fromHex(hex) {
|
|
3137
|
-
const P = Point.fromAffine(fromBytes(ensureBytes("pointHex", hex)));
|
|
3138
|
-
P.assertValidity();
|
|
3139
|
-
return P;
|
|
3140
|
-
}
|
|
3141
|
-
// Multiplies generator point by privateKey.
|
|
3142
|
-
static fromPrivateKey(privateKey) {
|
|
3143
|
-
return Point.BASE.multiply(normPrivateKeyToScalar(privateKey));
|
|
3144
|
-
}
|
|
3145
|
-
// "Private method", don't use it directly
|
|
3146
|
-
_setWindowSize(windowSize) {
|
|
3147
|
-
this._WINDOW_SIZE = windowSize;
|
|
3148
|
-
pointPrecomputes.delete(this);
|
|
3149
|
-
}
|
|
3150
|
-
// A point on curve is valid if it conforms to equation.
|
|
3151
|
-
assertValidity() {
|
|
3152
|
-
if (this.is0()) {
|
|
3153
|
-
if (CURVE2.allowInfinityPoint && !Fp.is0(this.py))
|
|
3154
|
-
return;
|
|
3155
|
-
throw new Error("bad point: ZERO");
|
|
3156
|
-
}
|
|
3157
|
-
const { x, y } = this.toAffine();
|
|
3158
|
-
if (!Fp.isValid(x) || !Fp.isValid(y))
|
|
3159
|
-
throw new Error("bad point: x or y not FE");
|
|
3160
|
-
const left = Fp.sqr(y);
|
|
3161
|
-
const right = weierstrassEquation(x);
|
|
3162
|
-
if (!Fp.eql(left, right))
|
|
3163
|
-
throw new Error("bad point: equation left != right");
|
|
3164
|
-
if (!this.isTorsionFree())
|
|
3165
|
-
throw new Error("bad point: not in prime-order subgroup");
|
|
3166
|
-
}
|
|
3167
|
-
hasEvenY() {
|
|
3168
|
-
const { y } = this.toAffine();
|
|
3169
|
-
if (Fp.isOdd)
|
|
3170
|
-
return !Fp.isOdd(y);
|
|
3171
|
-
throw new Error("Field doesn't support isOdd");
|
|
3172
|
-
}
|
|
3173
|
-
/**
|
|
3174
|
-
* Compare one point to another.
|
|
3175
|
-
*/
|
|
3176
|
-
equals(other) {
|
|
3177
|
-
assertPrjPoint(other);
|
|
3178
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
3179
|
-
const { px: X2, py: Y2, pz: Z2 } = other;
|
|
3180
|
-
const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
|
|
3181
|
-
const U2 = Fp.eql(Fp.mul(Y1, Z2), Fp.mul(Y2, Z1));
|
|
3182
|
-
return U1 && U2;
|
|
3183
|
-
}
|
|
3184
|
-
/**
|
|
3185
|
-
* Flips point to one corresponding to (x, -y) in Affine coordinates.
|
|
3186
|
-
*/
|
|
3187
|
-
negate() {
|
|
3188
|
-
return new Point(this.px, Fp.neg(this.py), this.pz);
|
|
3189
|
-
}
|
|
3190
|
-
// Renes-Costello-Batina exception-free doubling formula.
|
|
3191
|
-
// There is 30% faster Jacobian formula, but it is not complete.
|
|
3192
|
-
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
3193
|
-
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
3194
|
-
double() {
|
|
3195
|
-
const { a, b } = CURVE2;
|
|
3196
|
-
const b3 = Fp.mul(b, _3n2);
|
|
3197
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
3198
|
-
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
3199
|
-
let t0 = Fp.mul(X1, X1);
|
|
3200
|
-
let t1 = Fp.mul(Y1, Y1);
|
|
3201
|
-
let t2 = Fp.mul(Z1, Z1);
|
|
3202
|
-
let t3 = Fp.mul(X1, Y1);
|
|
3203
|
-
t3 = Fp.add(t3, t3);
|
|
3204
|
-
Z3 = Fp.mul(X1, Z1);
|
|
3205
|
-
Z3 = Fp.add(Z3, Z3);
|
|
3206
|
-
X3 = Fp.mul(a, Z3);
|
|
3207
|
-
Y3 = Fp.mul(b3, t2);
|
|
3208
|
-
Y3 = Fp.add(X3, Y3);
|
|
3209
|
-
X3 = Fp.sub(t1, Y3);
|
|
3210
|
-
Y3 = Fp.add(t1, Y3);
|
|
3211
|
-
Y3 = Fp.mul(X3, Y3);
|
|
3212
|
-
X3 = Fp.mul(t3, X3);
|
|
3213
|
-
Z3 = Fp.mul(b3, Z3);
|
|
3214
|
-
t2 = Fp.mul(a, t2);
|
|
3215
|
-
t3 = Fp.sub(t0, t2);
|
|
3216
|
-
t3 = Fp.mul(a, t3);
|
|
3217
|
-
t3 = Fp.add(t3, Z3);
|
|
3218
|
-
Z3 = Fp.add(t0, t0);
|
|
3219
|
-
t0 = Fp.add(Z3, t0);
|
|
3220
|
-
t0 = Fp.add(t0, t2);
|
|
3221
|
-
t0 = Fp.mul(t0, t3);
|
|
3222
|
-
Y3 = Fp.add(Y3, t0);
|
|
3223
|
-
t2 = Fp.mul(Y1, Z1);
|
|
3224
|
-
t2 = Fp.add(t2, t2);
|
|
3225
|
-
t0 = Fp.mul(t2, t3);
|
|
3226
|
-
X3 = Fp.sub(X3, t0);
|
|
3227
|
-
Z3 = Fp.mul(t2, t1);
|
|
3228
|
-
Z3 = Fp.add(Z3, Z3);
|
|
3229
|
-
Z3 = Fp.add(Z3, Z3);
|
|
3230
|
-
return new Point(X3, Y3, Z3);
|
|
3231
|
-
}
|
|
3232
|
-
// Renes-Costello-Batina exception-free addition formula.
|
|
3233
|
-
// There is 30% faster Jacobian formula, but it is not complete.
|
|
3234
|
-
// https://eprint.iacr.org/2015/1060, algorithm 1
|
|
3235
|
-
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
3236
|
-
add(other) {
|
|
3237
|
-
assertPrjPoint(other);
|
|
3238
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
3239
|
-
const { px: X2, py: Y2, pz: Z2 } = other;
|
|
3240
|
-
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
3241
|
-
const a = CURVE2.a;
|
|
3242
|
-
const b3 = Fp.mul(CURVE2.b, _3n2);
|
|
3243
|
-
let t0 = Fp.mul(X1, X2);
|
|
3244
|
-
let t1 = Fp.mul(Y1, Y2);
|
|
3245
|
-
let t2 = Fp.mul(Z1, Z2);
|
|
3246
|
-
let t3 = Fp.add(X1, Y1);
|
|
3247
|
-
let t4 = Fp.add(X2, Y2);
|
|
3248
|
-
t3 = Fp.mul(t3, t4);
|
|
3249
|
-
t4 = Fp.add(t0, t1);
|
|
3250
|
-
t3 = Fp.sub(t3, t4);
|
|
3251
|
-
t4 = Fp.add(X1, Z1);
|
|
3252
|
-
let t5 = Fp.add(X2, Z2);
|
|
3253
|
-
t4 = Fp.mul(t4, t5);
|
|
3254
|
-
t5 = Fp.add(t0, t2);
|
|
3255
|
-
t4 = Fp.sub(t4, t5);
|
|
3256
|
-
t5 = Fp.add(Y1, Z1);
|
|
3257
|
-
X3 = Fp.add(Y2, Z2);
|
|
3258
|
-
t5 = Fp.mul(t5, X3);
|
|
3259
|
-
X3 = Fp.add(t1, t2);
|
|
3260
|
-
t5 = Fp.sub(t5, X3);
|
|
3261
|
-
Z3 = Fp.mul(a, t4);
|
|
3262
|
-
X3 = Fp.mul(b3, t2);
|
|
3263
|
-
Z3 = Fp.add(X3, Z3);
|
|
3264
|
-
X3 = Fp.sub(t1, Z3);
|
|
3265
|
-
Z3 = Fp.add(t1, Z3);
|
|
3266
|
-
Y3 = Fp.mul(X3, Z3);
|
|
3267
|
-
t1 = Fp.add(t0, t0);
|
|
3268
|
-
t1 = Fp.add(t1, t0);
|
|
3269
|
-
t2 = Fp.mul(a, t2);
|
|
3270
|
-
t4 = Fp.mul(b3, t4);
|
|
3271
|
-
t1 = Fp.add(t1, t2);
|
|
3272
|
-
t2 = Fp.sub(t0, t2);
|
|
3273
|
-
t2 = Fp.mul(a, t2);
|
|
3274
|
-
t4 = Fp.add(t4, t2);
|
|
3275
|
-
t0 = Fp.mul(t1, t4);
|
|
3276
|
-
Y3 = Fp.add(Y3, t0);
|
|
3277
|
-
t0 = Fp.mul(t5, t4);
|
|
3278
|
-
X3 = Fp.mul(t3, X3);
|
|
3279
|
-
X3 = Fp.sub(X3, t0);
|
|
3280
|
-
t0 = Fp.mul(t3, t1);
|
|
3281
|
-
Z3 = Fp.mul(t5, Z3);
|
|
3282
|
-
Z3 = Fp.add(Z3, t0);
|
|
3283
|
-
return new Point(X3, Y3, Z3);
|
|
3284
|
-
}
|
|
3285
|
-
subtract(other) {
|
|
3286
|
-
return this.add(other.negate());
|
|
3287
|
-
}
|
|
3288
|
-
is0() {
|
|
3289
|
-
return this.equals(Point.ZERO);
|
|
3290
|
-
}
|
|
3291
|
-
wNAF(n) {
|
|
3292
|
-
return wnaf.wNAFCached(this, pointPrecomputes, n, (comp) => {
|
|
3293
|
-
const toInv = Fp.invertBatch(comp.map((p) => p.pz));
|
|
3294
|
-
return comp.map((p, i) => p.toAffine(toInv[i])).map(Point.fromAffine);
|
|
3295
|
-
});
|
|
3296
|
-
}
|
|
3297
|
-
/**
|
|
3298
|
-
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
3299
|
-
* It's faster, but should only be used when you don't care about
|
|
3300
|
-
* an exposed private key e.g. sig verification, which works over *public* keys.
|
|
3301
|
-
*/
|
|
3302
|
-
multiplyUnsafe(n) {
|
|
3303
|
-
const I = Point.ZERO;
|
|
3304
|
-
if (n === _0n4)
|
|
3305
|
-
return I;
|
|
3306
|
-
assertGE(n);
|
|
3307
|
-
if (n === _1n4)
|
|
3308
|
-
return this;
|
|
3309
|
-
const { endo } = CURVE2;
|
|
3310
|
-
if (!endo)
|
|
3311
|
-
return wnaf.unsafeLadder(this, n);
|
|
3312
|
-
let { k1neg, k1, k2neg, k2 } = endo.splitScalar(n);
|
|
3313
|
-
let k1p = I;
|
|
3314
|
-
let k2p = I;
|
|
3315
|
-
let d = this;
|
|
3316
|
-
while (k1 > _0n4 || k2 > _0n4) {
|
|
3317
|
-
if (k1 & _1n4)
|
|
3318
|
-
k1p = k1p.add(d);
|
|
3319
|
-
if (k2 & _1n4)
|
|
3320
|
-
k2p = k2p.add(d);
|
|
3321
|
-
d = d.double();
|
|
3322
|
-
k1 >>= _1n4;
|
|
3323
|
-
k2 >>= _1n4;
|
|
3324
|
-
}
|
|
3325
|
-
if (k1neg)
|
|
3326
|
-
k1p = k1p.negate();
|
|
3327
|
-
if (k2neg)
|
|
3328
|
-
k2p = k2p.negate();
|
|
3329
|
-
k2p = new Point(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
|
|
3330
|
-
return k1p.add(k2p);
|
|
3331
|
-
}
|
|
3332
|
-
/**
|
|
3333
|
-
* Constant time multiplication.
|
|
3334
|
-
* Uses wNAF method. Windowed method may be 10% faster,
|
|
3335
|
-
* but takes 2x longer to generate and consumes 2x memory.
|
|
3336
|
-
* Uses precomputes when available.
|
|
3337
|
-
* Uses endomorphism for Koblitz curves.
|
|
3338
|
-
* @param scalar by which the point would be multiplied
|
|
3339
|
-
* @returns New point
|
|
3340
|
-
*/
|
|
3341
|
-
multiply(scalar) {
|
|
3342
|
-
assertGE(scalar);
|
|
3343
|
-
let n = scalar;
|
|
3344
|
-
let point, fake;
|
|
3345
|
-
const { endo } = CURVE2;
|
|
3346
|
-
if (endo) {
|
|
3347
|
-
const { k1neg, k1, k2neg, k2 } = endo.splitScalar(n);
|
|
3348
|
-
let { p: k1p, f: f1p } = this.wNAF(k1);
|
|
3349
|
-
let { p: k2p, f: f2p } = this.wNAF(k2);
|
|
3350
|
-
k1p = wnaf.constTimeNegate(k1neg, k1p);
|
|
3351
|
-
k2p = wnaf.constTimeNegate(k2neg, k2p);
|
|
3352
|
-
k2p = new Point(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
|
|
3353
|
-
point = k1p.add(k2p);
|
|
3354
|
-
fake = f1p.add(f2p);
|
|
3355
|
-
} else {
|
|
3356
|
-
const { p, f } = this.wNAF(n);
|
|
3357
|
-
point = p;
|
|
3358
|
-
fake = f;
|
|
3359
|
-
}
|
|
3360
|
-
return Point.normalizeZ([point, fake])[0];
|
|
3361
|
-
}
|
|
3362
|
-
/**
|
|
3363
|
-
* Efficiently calculate `aP + bQ`. Unsafe, can expose private key, if used incorrectly.
|
|
3364
|
-
* Not using Strauss-Shamir trick: precomputation tables are faster.
|
|
3365
|
-
* The trick could be useful if both P and Q are not G (not in our case).
|
|
3366
|
-
* @returns non-zero affine point
|
|
3367
|
-
*/
|
|
3368
|
-
multiplyAndAddUnsafe(Q, a, b) {
|
|
3369
|
-
const G = Point.BASE;
|
|
3370
|
-
const mul = (P, a2) => a2 === _0n4 || a2 === _1n4 || !P.equals(G) ? P.multiplyUnsafe(a2) : P.multiply(a2);
|
|
3371
|
-
const sum = mul(this, a).add(mul(Q, b));
|
|
3372
|
-
return sum.is0() ? void 0 : sum;
|
|
3373
|
-
}
|
|
3374
|
-
// Converts Projective point to affine (x, y) coordinates.
|
|
3375
|
-
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
3376
|
-
// (x, y, z) ∋ (x=x/z, y=y/z)
|
|
3377
|
-
toAffine(iz) {
|
|
3378
|
-
const { px: x, py: y, pz: z } = this;
|
|
3379
|
-
const is0 = this.is0();
|
|
3380
|
-
if (iz == null)
|
|
3381
|
-
iz = is0 ? Fp.ONE : Fp.inv(z);
|
|
3382
|
-
const ax = Fp.mul(x, iz);
|
|
3383
|
-
const ay = Fp.mul(y, iz);
|
|
3384
|
-
const zz = Fp.mul(z, iz);
|
|
3385
|
-
if (is0)
|
|
3386
|
-
return { x: Fp.ZERO, y: Fp.ZERO };
|
|
3387
|
-
if (!Fp.eql(zz, Fp.ONE))
|
|
3388
|
-
throw new Error("invZ was invalid");
|
|
3389
|
-
return { x: ax, y: ay };
|
|
3390
|
-
}
|
|
3391
|
-
isTorsionFree() {
|
|
3392
|
-
const { h: cofactor, isTorsionFree } = CURVE2;
|
|
3393
|
-
if (cofactor === _1n4)
|
|
3394
|
-
return true;
|
|
3395
|
-
if (isTorsionFree)
|
|
3396
|
-
return isTorsionFree(Point, this);
|
|
3397
|
-
throw new Error("isTorsionFree() has not been declared for the elliptic curve");
|
|
3398
|
-
}
|
|
3399
|
-
clearCofactor() {
|
|
3400
|
-
const { h: cofactor, clearCofactor } = CURVE2;
|
|
3401
|
-
if (cofactor === _1n4)
|
|
3402
|
-
return this;
|
|
3403
|
-
if (clearCofactor)
|
|
3404
|
-
return clearCofactor(Point, this);
|
|
3405
|
-
return this.multiplyUnsafe(CURVE2.h);
|
|
3406
|
-
}
|
|
3407
|
-
toRawBytes(isCompressed = true) {
|
|
3408
|
-
this.assertValidity();
|
|
3409
|
-
return toBytes2(Point, this, isCompressed);
|
|
3410
|
-
}
|
|
3411
|
-
toHex(isCompressed = true) {
|
|
3412
|
-
return bytesToHex(this.toRawBytes(isCompressed));
|
|
3413
|
-
}
|
|
3414
|
-
}
|
|
3415
|
-
Point.BASE = new Point(CURVE2.Gx, CURVE2.Gy, Fp.ONE);
|
|
3416
|
-
Point.ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO);
|
|
3417
|
-
const _bits = CURVE2.nBitLength;
|
|
3418
|
-
const wnaf = wNAF(Point, CURVE2.endo ? Math.ceil(_bits / 2) : _bits);
|
|
3419
|
-
return {
|
|
3420
|
-
CURVE: CURVE2,
|
|
3421
|
-
ProjectivePoint: Point,
|
|
3422
|
-
normPrivateKeyToScalar,
|
|
3423
|
-
weierstrassEquation,
|
|
3424
|
-
isWithinCurveOrder
|
|
3425
|
-
};
|
|
3426
|
-
}
|
|
3427
|
-
function validateOpts2(curve2) {
|
|
3428
|
-
const opts = validateBasic(curve2);
|
|
3429
|
-
validateObject(opts, {
|
|
3430
|
-
hash: "hash",
|
|
3431
|
-
hmac: "function",
|
|
3432
|
-
randomBytes: "function"
|
|
3433
|
-
}, {
|
|
3434
|
-
bits2int: "function",
|
|
3435
|
-
bits2int_modN: "function",
|
|
3436
|
-
lowS: "boolean"
|
|
3437
|
-
});
|
|
3438
|
-
return Object.freeze({ lowS: true, ...opts });
|
|
3439
|
-
}
|
|
3440
|
-
function weierstrass(curveDef) {
|
|
3441
|
-
const CURVE2 = validateOpts2(curveDef);
|
|
3442
|
-
const { Fp, n: CURVE_ORDER2 } = CURVE2;
|
|
3443
|
-
const compressedLen = Fp.BYTES + 1;
|
|
3444
|
-
const uncompressedLen = 2 * Fp.BYTES + 1;
|
|
3445
|
-
function isValidFieldElement(num10) {
|
|
3446
|
-
return _0n4 < num10 && num10 < Fp.ORDER;
|
|
3447
|
-
}
|
|
3448
|
-
function modN(a) {
|
|
3449
|
-
return mod(a, CURVE_ORDER2);
|
|
3450
|
-
}
|
|
3451
|
-
function invN(a) {
|
|
3452
|
-
return invert(a, CURVE_ORDER2);
|
|
3453
|
-
}
|
|
3454
|
-
const { ProjectivePoint: Point, normPrivateKeyToScalar, weierstrassEquation, isWithinCurveOrder } = weierstrassPoints({
|
|
3455
|
-
...CURVE2,
|
|
3456
|
-
toBytes(_c, point, isCompressed) {
|
|
3457
|
-
const a = point.toAffine();
|
|
3458
|
-
const x = Fp.toBytes(a.x);
|
|
3459
|
-
const cat = concatBytes2;
|
|
3460
|
-
if (isCompressed) {
|
|
3461
|
-
return cat(Uint8Array.from([point.hasEvenY() ? 2 : 3]), x);
|
|
3462
|
-
} else {
|
|
3463
|
-
return cat(Uint8Array.from([4]), x, Fp.toBytes(a.y));
|
|
3464
|
-
}
|
|
3465
|
-
},
|
|
3466
|
-
fromBytes(bytes2) {
|
|
3467
|
-
const len = bytes2.length;
|
|
3468
|
-
const head = bytes2[0];
|
|
3469
|
-
const tail = bytes2.subarray(1);
|
|
3470
|
-
if (len === compressedLen && (head === 2 || head === 3)) {
|
|
3471
|
-
const x = bytesToNumberBE(tail);
|
|
3472
|
-
if (!isValidFieldElement(x))
|
|
3473
|
-
throw new Error("Point is not on curve");
|
|
3474
|
-
const y2 = weierstrassEquation(x);
|
|
3475
|
-
let y = Fp.sqrt(y2);
|
|
3476
|
-
const isYOdd = (y & _1n4) === _1n4;
|
|
3477
|
-
const isHeadOdd = (head & 1) === 1;
|
|
3478
|
-
if (isHeadOdd !== isYOdd)
|
|
3479
|
-
y = Fp.neg(y);
|
|
3480
|
-
return { x, y };
|
|
3481
|
-
} else if (len === uncompressedLen && head === 4) {
|
|
3482
|
-
const x = Fp.fromBytes(tail.subarray(0, Fp.BYTES));
|
|
3483
|
-
const y = Fp.fromBytes(tail.subarray(Fp.BYTES, 2 * Fp.BYTES));
|
|
3484
|
-
return { x, y };
|
|
3485
|
-
} else {
|
|
3486
|
-
throw new Error(`Point of length ${len} was invalid. Expected ${compressedLen} compressed bytes or ${uncompressedLen} uncompressed bytes`);
|
|
3487
|
-
}
|
|
3488
|
-
}
|
|
3489
|
-
});
|
|
3490
|
-
const numToNByteStr = (num10) => bytesToHex(numberToBytesBE(num10, CURVE2.nByteLength));
|
|
3491
|
-
function isBiggerThanHalfOrder(number2) {
|
|
3492
|
-
const HALF = CURVE_ORDER2 >> _1n4;
|
|
3493
|
-
return number2 > HALF;
|
|
3494
|
-
}
|
|
3495
|
-
function normalizeS(s) {
|
|
3496
|
-
return isBiggerThanHalfOrder(s) ? modN(-s) : s;
|
|
3497
|
-
}
|
|
3498
|
-
const slcNum = (b, from, to) => bytesToNumberBE(b.slice(from, to));
|
|
3499
|
-
class Signature2 {
|
|
3500
|
-
constructor(r, s, recovery) {
|
|
3501
|
-
this.r = r;
|
|
3502
|
-
this.s = s;
|
|
3503
|
-
this.recovery = recovery;
|
|
3504
|
-
this.assertValidity();
|
|
3505
|
-
}
|
|
3506
|
-
// pair (bytes of r, bytes of s)
|
|
3507
|
-
static fromCompact(hex) {
|
|
3508
|
-
const l = CURVE2.nByteLength;
|
|
3509
|
-
hex = ensureBytes("compactSignature", hex, l * 2);
|
|
3510
|
-
return new Signature2(slcNum(hex, 0, l), slcNum(hex, l, 2 * l));
|
|
3511
|
-
}
|
|
3512
|
-
// DER encoded ECDSA signature
|
|
3513
|
-
// https://bitcoin.stackexchange.com/questions/57644/what-are-the-parts-of-a-bitcoin-transaction-input-script
|
|
3514
|
-
static fromDER(hex) {
|
|
3515
|
-
const { r, s } = DER.toSig(ensureBytes("DER", hex));
|
|
3516
|
-
return new Signature2(r, s);
|
|
3517
|
-
}
|
|
3518
|
-
assertValidity() {
|
|
3519
|
-
if (!isWithinCurveOrder(this.r))
|
|
3520
|
-
throw new Error("r must be 0 < r < CURVE.n");
|
|
3521
|
-
if (!isWithinCurveOrder(this.s))
|
|
3522
|
-
throw new Error("s must be 0 < s < CURVE.n");
|
|
3523
|
-
}
|
|
3524
|
-
addRecoveryBit(recovery) {
|
|
3525
|
-
return new Signature2(this.r, this.s, recovery);
|
|
3526
|
-
}
|
|
3527
|
-
recoverPublicKey(msgHash) {
|
|
3528
|
-
const { r, s, recovery: rec } = this;
|
|
3529
|
-
const h = bits2int_modN(ensureBytes("msgHash", msgHash));
|
|
3530
|
-
if (rec == null || ![0, 1, 2, 3].includes(rec))
|
|
3531
|
-
throw new Error("recovery id invalid");
|
|
3532
|
-
const radj = rec === 2 || rec === 3 ? r + CURVE2.n : r;
|
|
3533
|
-
if (radj >= Fp.ORDER)
|
|
3534
|
-
throw new Error("recovery id 2 or 3 invalid");
|
|
3535
|
-
const prefix = (rec & 1) === 0 ? "02" : "03";
|
|
3536
|
-
const R = Point.fromHex(prefix + numToNByteStr(radj));
|
|
3537
|
-
const ir = invN(radj);
|
|
3538
|
-
const u1 = modN(-h * ir);
|
|
3539
|
-
const u2 = modN(s * ir);
|
|
3540
|
-
const Q = Point.BASE.multiplyAndAddUnsafe(R, u1, u2);
|
|
3541
|
-
if (!Q)
|
|
3542
|
-
throw new Error("point at infinify");
|
|
3543
|
-
Q.assertValidity();
|
|
3544
|
-
return Q;
|
|
3545
|
-
}
|
|
3546
|
-
// Signatures should be low-s, to prevent malleability.
|
|
3547
|
-
hasHighS() {
|
|
3548
|
-
return isBiggerThanHalfOrder(this.s);
|
|
3549
|
-
}
|
|
3550
|
-
normalizeS() {
|
|
3551
|
-
return this.hasHighS() ? new Signature2(this.r, modN(-this.s), this.recovery) : this;
|
|
3552
|
-
}
|
|
3553
|
-
// DER-encoded
|
|
3554
|
-
toDERRawBytes() {
|
|
3555
|
-
return hexToBytes(this.toDERHex());
|
|
3556
|
-
}
|
|
3557
|
-
toDERHex() {
|
|
3558
|
-
return DER.hexFromSig({ r: this.r, s: this.s });
|
|
3559
|
-
}
|
|
3560
|
-
// padded bytes of r, then padded bytes of s
|
|
3561
|
-
toCompactRawBytes() {
|
|
3562
|
-
return hexToBytes(this.toCompactHex());
|
|
3563
|
-
}
|
|
3564
|
-
toCompactHex() {
|
|
3565
|
-
return numToNByteStr(this.r) + numToNByteStr(this.s);
|
|
3566
|
-
}
|
|
3567
|
-
}
|
|
3568
|
-
const utils2 = {
|
|
3569
|
-
isValidPrivateKey(privateKey) {
|
|
3570
|
-
try {
|
|
3571
|
-
normPrivateKeyToScalar(privateKey);
|
|
3572
|
-
return true;
|
|
3573
|
-
} catch (error) {
|
|
3574
|
-
return false;
|
|
3575
|
-
}
|
|
3576
|
-
},
|
|
3577
|
-
normPrivateKeyToScalar,
|
|
3578
|
-
/**
|
|
3579
|
-
* Produces cryptographically secure private key from random of size
|
|
3580
|
-
* (groupLen + ceil(groupLen / 2)) with modulo bias being negligible.
|
|
3581
|
-
*/
|
|
3582
|
-
randomPrivateKey: () => {
|
|
3583
|
-
const length = getMinHashLength(CURVE2.n);
|
|
3584
|
-
return mapHashToField(CURVE2.randomBytes(length), CURVE2.n);
|
|
3585
|
-
},
|
|
3586
|
-
/**
|
|
3587
|
-
* Creates precompute table for an arbitrary EC point. Makes point "cached".
|
|
3588
|
-
* Allows to massively speed-up `point.multiply(scalar)`.
|
|
3589
|
-
* @returns cached point
|
|
3590
|
-
* @example
|
|
3591
|
-
* const fast = utils.precompute(8, ProjectivePoint.fromHex(someonesPubKey));
|
|
3592
|
-
* fast.multiply(privKey); // much faster ECDH now
|
|
3593
|
-
*/
|
|
3594
|
-
precompute(windowSize = 8, point = Point.BASE) {
|
|
3595
|
-
point._setWindowSize(windowSize);
|
|
3596
|
-
point.multiply(BigInt(3));
|
|
3597
|
-
return point;
|
|
3598
|
-
}
|
|
3599
|
-
};
|
|
3600
|
-
function getPublicKey(privateKey, isCompressed = true) {
|
|
3601
|
-
return Point.fromPrivateKey(privateKey).toRawBytes(isCompressed);
|
|
3602
|
-
}
|
|
3603
|
-
function isProbPub(item) {
|
|
3604
|
-
const arr = item instanceof Uint8Array;
|
|
3605
|
-
const str = typeof item === "string";
|
|
3606
|
-
const len = (arr || str) && item.length;
|
|
3607
|
-
if (arr)
|
|
3608
|
-
return len === compressedLen || len === uncompressedLen;
|
|
3609
|
-
if (str)
|
|
3610
|
-
return len === 2 * compressedLen || len === 2 * uncompressedLen;
|
|
3611
|
-
if (item instanceof Point)
|
|
3612
|
-
return true;
|
|
3613
|
-
return false;
|
|
3614
|
-
}
|
|
3615
|
-
function getSharedSecret(privateA, publicB, isCompressed = true) {
|
|
3616
|
-
if (isProbPub(privateA))
|
|
3617
|
-
throw new Error("first arg must be private key");
|
|
3618
|
-
if (!isProbPub(publicB))
|
|
3619
|
-
throw new Error("second arg must be public key");
|
|
3620
|
-
const b = Point.fromHex(publicB);
|
|
3621
|
-
return b.multiply(normPrivateKeyToScalar(privateA)).toRawBytes(isCompressed);
|
|
3622
|
-
}
|
|
3623
|
-
const bits2int2 = CURVE2.bits2int || function(bytes2) {
|
|
3624
|
-
const num10 = bytesToNumberBE(bytes2);
|
|
3625
|
-
const delta = bytes2.length * 8 - CURVE2.nBitLength;
|
|
3626
|
-
return delta > 0 ? num10 >> BigInt(delta) : num10;
|
|
3627
|
-
};
|
|
3628
|
-
const bits2int_modN = CURVE2.bits2int_modN || function(bytes2) {
|
|
3629
|
-
return modN(bits2int2(bytes2));
|
|
3630
|
-
};
|
|
3631
|
-
const ORDER_MASK = bitMask(CURVE2.nBitLength);
|
|
3632
|
-
function int2octets(num10) {
|
|
3633
|
-
if (typeof num10 !== "bigint")
|
|
3634
|
-
throw new Error("bigint expected");
|
|
3635
|
-
if (!(_0n4 <= num10 && num10 < ORDER_MASK))
|
|
3636
|
-
throw new Error(`bigint expected < 2^${CURVE2.nBitLength}`);
|
|
3637
|
-
return numberToBytesBE(num10, CURVE2.nByteLength);
|
|
3638
|
-
}
|
|
3639
|
-
function prepSig(msgHash, privateKey, opts = defaultSigOpts) {
|
|
3640
|
-
if (["recovered", "canonical"].some((k) => k in opts))
|
|
3641
|
-
throw new Error("sign() legacy options not supported");
|
|
3642
|
-
const { hash: hash5, randomBytes: randomBytes2 } = CURVE2;
|
|
3643
|
-
let { lowS, prehash, extraEntropy: ent } = opts;
|
|
3644
|
-
if (lowS == null)
|
|
3645
|
-
lowS = true;
|
|
3646
|
-
msgHash = ensureBytes("msgHash", msgHash);
|
|
3647
|
-
if (prehash)
|
|
3648
|
-
msgHash = ensureBytes("prehashed msgHash", hash5(msgHash));
|
|
3649
|
-
const h1int = bits2int_modN(msgHash);
|
|
3650
|
-
const d = normPrivateKeyToScalar(privateKey);
|
|
3651
|
-
const seedArgs = [int2octets(d), int2octets(h1int)];
|
|
3652
|
-
if (ent != null) {
|
|
3653
|
-
const e = ent === true ? randomBytes2(Fp.BYTES) : ent;
|
|
3654
|
-
seedArgs.push(ensureBytes("extraEntropy", e));
|
|
3655
|
-
}
|
|
3656
|
-
const seed = concatBytes2(...seedArgs);
|
|
3657
|
-
const m = h1int;
|
|
3658
|
-
function k2sig(kBytes) {
|
|
3659
|
-
const k = bits2int2(kBytes);
|
|
3660
|
-
if (!isWithinCurveOrder(k))
|
|
3661
|
-
return;
|
|
3662
|
-
const ik = invN(k);
|
|
3663
|
-
const q = Point.BASE.multiply(k).toAffine();
|
|
3664
|
-
const r = modN(q.x);
|
|
3665
|
-
if (r === _0n4)
|
|
3666
|
-
return;
|
|
3667
|
-
const s = modN(ik * modN(m + r * d));
|
|
3668
|
-
if (s === _0n4)
|
|
3669
|
-
return;
|
|
3670
|
-
let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n4);
|
|
3671
|
-
let normS = s;
|
|
3672
|
-
if (lowS && isBiggerThanHalfOrder(s)) {
|
|
3673
|
-
normS = normalizeS(s);
|
|
3674
|
-
recovery ^= 1;
|
|
3675
|
-
}
|
|
3676
|
-
return new Signature2(r, normS, recovery);
|
|
3677
|
-
}
|
|
3678
|
-
return { seed, k2sig };
|
|
3679
|
-
}
|
|
3680
|
-
const defaultSigOpts = { lowS: CURVE2.lowS, prehash: false };
|
|
3681
|
-
const defaultVerOpts = { lowS: CURVE2.lowS, prehash: false };
|
|
3682
|
-
function sign(msgHash, privKey, opts = defaultSigOpts) {
|
|
3683
|
-
const { seed, k2sig } = prepSig(msgHash, privKey, opts);
|
|
3684
|
-
const C = CURVE2;
|
|
3685
|
-
const drbg = createHmacDrbg(C.hash.outputLen, C.nByteLength, C.hmac);
|
|
3686
|
-
return drbg(seed, k2sig);
|
|
3687
|
-
}
|
|
3688
|
-
Point.BASE._setWindowSize(8);
|
|
3689
|
-
function verify(signature, msgHash, publicKey, opts = defaultVerOpts) {
|
|
3690
|
-
const sg = signature;
|
|
3691
|
-
msgHash = ensureBytes("msgHash", msgHash);
|
|
3692
|
-
publicKey = ensureBytes("publicKey", publicKey);
|
|
3693
|
-
if ("strict" in opts)
|
|
3694
|
-
throw new Error("options.strict was renamed to lowS");
|
|
3695
|
-
const { lowS, prehash } = opts;
|
|
3696
|
-
let _sig = void 0;
|
|
3697
|
-
let P;
|
|
3698
|
-
try {
|
|
3699
|
-
if (typeof sg === "string" || sg instanceof Uint8Array) {
|
|
3700
|
-
try {
|
|
3701
|
-
_sig = Signature2.fromDER(sg);
|
|
3702
|
-
} catch (derError) {
|
|
3703
|
-
if (!(derError instanceof DER.Err))
|
|
3704
|
-
throw derError;
|
|
3705
|
-
_sig = Signature2.fromCompact(sg);
|
|
3706
|
-
}
|
|
3707
|
-
} else if (typeof sg === "object" && typeof sg.r === "bigint" && typeof sg.s === "bigint") {
|
|
3708
|
-
const { r: r2, s: s2 } = sg;
|
|
3709
|
-
_sig = new Signature2(r2, s2);
|
|
3710
|
-
} else {
|
|
3711
|
-
throw new Error("PARSE");
|
|
3712
|
-
}
|
|
3713
|
-
P = Point.fromHex(publicKey);
|
|
3714
|
-
} catch (error) {
|
|
3715
|
-
if (error.message === "PARSE")
|
|
3716
|
-
throw new Error(`signature must be Signature instance, Uint8Array or hex string`);
|
|
3717
|
-
return false;
|
|
3718
|
-
}
|
|
3719
|
-
if (lowS && _sig.hasHighS())
|
|
3720
|
-
return false;
|
|
3721
|
-
if (prehash)
|
|
3722
|
-
msgHash = CURVE2.hash(msgHash);
|
|
3723
|
-
const { r, s } = _sig;
|
|
3724
|
-
const h = bits2int_modN(msgHash);
|
|
3725
|
-
const is = invN(s);
|
|
3726
|
-
const u1 = modN(h * is);
|
|
3727
|
-
const u2 = modN(r * is);
|
|
3728
|
-
const R = Point.BASE.multiplyAndAddUnsafe(P, u1, u2)?.toAffine();
|
|
3729
|
-
if (!R)
|
|
3730
|
-
return false;
|
|
3731
|
-
const v = modN(R.x);
|
|
3732
|
-
return v === r;
|
|
3733
|
-
}
|
|
3734
|
-
return {
|
|
3735
|
-
CURVE: CURVE2,
|
|
3736
|
-
getPublicKey,
|
|
3737
|
-
getSharedSecret,
|
|
3738
|
-
sign,
|
|
3739
|
-
verify,
|
|
3740
|
-
ProjectivePoint: Point,
|
|
3741
|
-
Signature: Signature2,
|
|
3742
|
-
utils: utils2
|
|
3743
|
-
};
|
|
3744
|
-
}
|
|
3745
|
-
|
|
3746
|
-
// ../node_modules/@noble/hashes/esm/hmac.js
|
|
3747
|
-
var HMAC = class extends Hash {
|
|
3748
|
-
constructor(hash5, _key) {
|
|
3749
|
-
super();
|
|
3750
|
-
this.finished = false;
|
|
3751
|
-
this.destroyed = false;
|
|
3752
|
-
hash(hash5);
|
|
3753
|
-
const key = toBytes(_key);
|
|
3754
|
-
this.iHash = hash5.create();
|
|
3755
|
-
if (typeof this.iHash.update !== "function")
|
|
3756
|
-
throw new Error("Expected instance of class which extends utils.Hash");
|
|
3757
|
-
this.blockLen = this.iHash.blockLen;
|
|
3758
|
-
this.outputLen = this.iHash.outputLen;
|
|
3759
|
-
const blockLen = this.blockLen;
|
|
3760
|
-
const pad = new Uint8Array(blockLen);
|
|
3761
|
-
pad.set(key.length > blockLen ? hash5.create().update(key).digest() : key);
|
|
3762
|
-
for (let i = 0; i < pad.length; i++)
|
|
3763
|
-
pad[i] ^= 54;
|
|
3764
|
-
this.iHash.update(pad);
|
|
3765
|
-
this.oHash = hash5.create();
|
|
3766
|
-
for (let i = 0; i < pad.length; i++)
|
|
3767
|
-
pad[i] ^= 54 ^ 92;
|
|
3768
|
-
this.oHash.update(pad);
|
|
3769
|
-
pad.fill(0);
|
|
3770
|
-
}
|
|
3771
|
-
update(buf) {
|
|
3772
|
-
exists(this);
|
|
3773
|
-
this.iHash.update(buf);
|
|
3774
|
-
return this;
|
|
3775
|
-
}
|
|
3776
|
-
digestInto(out) {
|
|
3777
|
-
exists(this);
|
|
3778
|
-
bytes(out, this.outputLen);
|
|
3779
|
-
this.finished = true;
|
|
3780
|
-
this.iHash.digestInto(out);
|
|
3781
|
-
this.oHash.update(out);
|
|
3782
|
-
this.oHash.digestInto(out);
|
|
3783
|
-
this.destroy();
|
|
3784
|
-
}
|
|
3785
|
-
digest() {
|
|
3786
|
-
const out = new Uint8Array(this.oHash.outputLen);
|
|
3787
|
-
this.digestInto(out);
|
|
3788
|
-
return out;
|
|
3789
|
-
}
|
|
3790
|
-
_cloneInto(to) {
|
|
3791
|
-
to || (to = Object.create(Object.getPrototypeOf(this), {}));
|
|
3792
|
-
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
3793
|
-
to = to;
|
|
3794
|
-
to.finished = finished;
|
|
3795
|
-
to.destroyed = destroyed;
|
|
3796
|
-
to.blockLen = blockLen;
|
|
3797
|
-
to.outputLen = outputLen;
|
|
3798
|
-
to.oHash = oHash._cloneInto(to.oHash);
|
|
3799
|
-
to.iHash = iHash._cloneInto(to.iHash);
|
|
3800
|
-
return to;
|
|
3801
|
-
}
|
|
3802
|
-
destroy() {
|
|
3803
|
-
this.destroyed = true;
|
|
3804
|
-
this.oHash.destroy();
|
|
3805
|
-
this.iHash.destroy();
|
|
3806
|
-
}
|
|
3807
|
-
};
|
|
3808
|
-
var hmac = (hash5, key, message) => new HMAC(hash5, key).update(message).digest();
|
|
3809
|
-
hmac.create = (hash5, key) => new HMAC(hash5, key);
|
|
3810
|
-
|
|
3811
|
-
// ../node_modules/@noble/curves/esm/_shortw_utils.js
|
|
3812
|
-
function getHash(hash5) {
|
|
3813
|
-
return {
|
|
3814
|
-
hash: hash5,
|
|
3815
|
-
hmac: (key, ...msgs) => hmac(hash5, key, concatBytes(...msgs)),
|
|
3816
|
-
randomBytes
|
|
3817
|
-
};
|
|
3818
|
-
}
|
|
3819
|
-
|
|
3820
|
-
// ../node_modules/@scure/starknet/lib/esm/index.js
|
|
3821
|
-
var CURVE_ORDER = BigInt("3618502788666131213697322783095070105526743751716087489154079457884512865583");
|
|
3822
|
-
var MAX_VALUE = BigInt("0x800000000000000000000000000000000000000000000000000000000000000");
|
|
3823
|
-
var nBitLength = 252;
|
|
3824
|
-
function bits2int(bytes2) {
|
|
3825
|
-
while (bytes2[0] === 0)
|
|
3826
|
-
bytes2 = bytes2.subarray(1);
|
|
3827
|
-
const delta = bytes2.length * 8 - nBitLength;
|
|
3828
|
-
const num10 = bytesToNumberBE(bytes2);
|
|
3829
|
-
return delta > 0 ? num10 >> BigInt(delta) : num10;
|
|
3830
|
-
}
|
|
3831
|
-
function hex0xToBytes(hex) {
|
|
3832
|
-
if (typeof hex === "string") {
|
|
3833
|
-
hex = strip0x(hex);
|
|
3834
|
-
if (hex.length & 1)
|
|
3835
|
-
hex = "0" + hex;
|
|
3836
|
-
}
|
|
3837
|
-
return hexToBytes(hex);
|
|
3838
|
-
}
|
|
3839
|
-
var curve = weierstrass({
|
|
3840
|
-
a: BigInt(1),
|
|
3841
|
-
b: BigInt("3141592653589793238462643383279502884197169399375105820974944592307816406665"),
|
|
3842
|
-
Fp: Field(BigInt("0x800000000000011000000000000000000000000000000000000000000000001")),
|
|
3843
|
-
n: CURVE_ORDER,
|
|
3844
|
-
nBitLength,
|
|
3845
|
-
Gx: BigInt("874739451078007766457464989774322083649278607533249481151382481072868806602"),
|
|
3846
|
-
Gy: BigInt("152666792071518830868575557812948353041420400780739481342941381225525861407"),
|
|
3847
|
-
h: BigInt(1),
|
|
3848
|
-
lowS: false,
|
|
3849
|
-
...getHash(sha256),
|
|
3850
|
-
bits2int,
|
|
3851
|
-
bits2int_modN: (bytes2) => {
|
|
3852
|
-
const hex = bytesToNumberBE(bytes2).toString(16);
|
|
3853
|
-
if (hex.length === 63)
|
|
3854
|
-
bytes2 = hex0xToBytes(hex + "0");
|
|
3855
|
-
return mod(bits2int(bytes2), CURVE_ORDER);
|
|
3856
|
-
}
|
|
3857
|
-
});
|
|
3858
|
-
function ensureBytes2(hex) {
|
|
3859
|
-
return ensureBytes("", typeof hex === "string" ? hex0xToBytes(hex) : hex);
|
|
3860
|
-
}
|
|
3861
|
-
var { CURVE, ProjectivePoint, Signature, utils } = curve;
|
|
3862
|
-
function extractX(bytes2) {
|
|
3863
|
-
const hex = bytesToHex(bytes2.subarray(1));
|
|
3864
|
-
const stripped = hex.replace(/^0+/gm, "");
|
|
3865
|
-
return `0x${stripped}`;
|
|
3866
|
-
}
|
|
3867
|
-
function strip0x(hex) {
|
|
3868
|
-
return hex.replace(/^0x/i, "");
|
|
3869
|
-
}
|
|
3870
|
-
var MASK_31 = 2n ** 31n - 1n;
|
|
3871
|
-
var PEDERSEN_POINTS = [
|
|
3872
|
-
new ProjectivePoint(2089986280348253421170679821480865132823066470938446095505822317253594081284n, 1713931329540660377023406109199410414810705867260802078187082345529207694986n, 1n),
|
|
3873
|
-
new ProjectivePoint(996781205833008774514500082376783249102396023663454813447423147977397232763n, 1668503676786377725805489344771023921079126552019160156920634619255970485781n, 1n),
|
|
3874
|
-
new ProjectivePoint(2251563274489750535117886426533222435294046428347329203627021249169616184184n, 1798716007562728905295480679789526322175868328062420237419143593021674992973n, 1n),
|
|
3875
|
-
new ProjectivePoint(2138414695194151160943305727036575959195309218611738193261179310511854807447n, 113410276730064486255102093846540133784865286929052426931474106396135072156n, 1n),
|
|
3876
|
-
new ProjectivePoint(2379962749567351885752724891227938183011949129833673362440656643086021394946n, 776496453633298175483985398648758586525933812536653089401905292063708816422n, 1n)
|
|
3877
|
-
];
|
|
3878
|
-
function pedersenPrecompute(p1, p2) {
|
|
3879
|
-
const out = [];
|
|
3880
|
-
let p = p1;
|
|
3881
|
-
for (let i = 0; i < 248; i++) {
|
|
3882
|
-
out.push(p);
|
|
3883
|
-
p = p.double();
|
|
3884
|
-
}
|
|
3885
|
-
p = p2;
|
|
3886
|
-
for (let i = 0; i < 4; i++) {
|
|
3887
|
-
out.push(p);
|
|
3888
|
-
p = p.double();
|
|
3889
|
-
}
|
|
3890
|
-
return out;
|
|
3891
|
-
}
|
|
3892
|
-
var PEDERSEN_POINTS1 = pedersenPrecompute(PEDERSEN_POINTS[1], PEDERSEN_POINTS[2]);
|
|
3893
|
-
var PEDERSEN_POINTS2 = pedersenPrecompute(PEDERSEN_POINTS[3], PEDERSEN_POINTS[4]);
|
|
3894
|
-
function pedersenArg(arg) {
|
|
3895
|
-
let value;
|
|
3896
|
-
if (typeof arg === "bigint") {
|
|
3897
|
-
value = arg;
|
|
3898
|
-
} else if (typeof arg === "number") {
|
|
3899
|
-
if (!Number.isSafeInteger(arg))
|
|
3900
|
-
throw new Error(`Invalid pedersenArg: ${arg}`);
|
|
3901
|
-
value = BigInt(arg);
|
|
3902
|
-
} else {
|
|
3903
|
-
value = bytesToNumberBE(ensureBytes2(arg));
|
|
3904
|
-
}
|
|
3905
|
-
if (!(0n <= value && value < curve.CURVE.Fp.ORDER))
|
|
3906
|
-
throw new Error(`PedersenArg should be 0 <= value < CURVE.P: ${value}`);
|
|
3907
|
-
return value;
|
|
3908
|
-
}
|
|
3909
|
-
function pedersenSingle(point, value, constants) {
|
|
3910
|
-
let x = pedersenArg(value);
|
|
3911
|
-
for (let j = 0; j < 252; j++) {
|
|
3912
|
-
const pt = constants[j];
|
|
3913
|
-
if (pt.equals(point))
|
|
3914
|
-
throw new Error("Same point");
|
|
3915
|
-
if ((x & 1n) !== 0n)
|
|
3916
|
-
point = point.add(pt);
|
|
3917
|
-
x >>= 1n;
|
|
3918
|
-
}
|
|
3919
|
-
return point;
|
|
3920
|
-
}
|
|
3921
|
-
function pedersen(x, y) {
|
|
3922
|
-
let point = PEDERSEN_POINTS[0];
|
|
3923
|
-
point = pedersenSingle(point, x, PEDERSEN_POINTS1);
|
|
3924
|
-
point = pedersenSingle(point, y, PEDERSEN_POINTS2);
|
|
3925
|
-
return extractX(point.toRawBytes(true));
|
|
3926
|
-
}
|
|
3927
|
-
var MASK_250 = bitMask(250);
|
|
3928
|
-
var Fp251 = Field(BigInt("3618502788666131213697322783095070105623107215331596699973092056135872020481"));
|
|
3929
|
-
function poseidonRoundConstant(Fp, name, idx) {
|
|
3930
|
-
const val = Fp.fromBytes(sha256(utf8ToBytes(`${name}${idx}`)));
|
|
3931
|
-
return Fp.create(val);
|
|
3932
|
-
}
|
|
3933
|
-
var MDS_SMALL = [
|
|
3934
|
-
[3, 1, 1],
|
|
3935
|
-
[1, -1, 1],
|
|
3936
|
-
[1, 1, -2]
|
|
3937
|
-
].map((i) => i.map(BigInt));
|
|
3938
|
-
function poseidonBasic(opts, mds) {
|
|
3939
|
-
validateField(opts.Fp);
|
|
3940
|
-
if (!Number.isSafeInteger(opts.rate) || !Number.isSafeInteger(opts.capacity))
|
|
3941
|
-
throw new Error(`Wrong poseidon opts: ${opts}`);
|
|
3942
|
-
const m = opts.rate + opts.capacity;
|
|
3943
|
-
const rounds = opts.roundsFull + opts.roundsPartial;
|
|
3944
|
-
const roundConstants = [];
|
|
3945
|
-
for (let i = 0; i < rounds; i++) {
|
|
3946
|
-
const row = [];
|
|
3947
|
-
for (let j = 0; j < m; j++)
|
|
3948
|
-
row.push(poseidonRoundConstant(opts.Fp, "Hades", m * i + j));
|
|
3949
|
-
roundConstants.push(row);
|
|
3950
|
-
}
|
|
3951
|
-
const res = poseidon({
|
|
3952
|
-
...opts,
|
|
3953
|
-
t: m,
|
|
3954
|
-
sboxPower: 3,
|
|
3955
|
-
reversePartialPowIdx: true,
|
|
3956
|
-
mds,
|
|
3957
|
-
roundConstants
|
|
3958
|
-
});
|
|
3959
|
-
res.m = m;
|
|
3960
|
-
res.rate = opts.rate;
|
|
3961
|
-
res.capacity = opts.capacity;
|
|
3962
|
-
return res;
|
|
3963
|
-
}
|
|
3964
|
-
var poseidonSmall = poseidonBasic({ Fp: Fp251, rate: 2, capacity: 1, roundsFull: 8, roundsPartial: 83 }, MDS_SMALL);
|
|
3965
|
-
|
|
3966
|
-
// src/utils/oz-merkle.ts
|
|
1935
|
+
import * as starknet from "@scure/starknet";
|
|
3967
1936
|
function hash_leaf(leaf) {
|
|
3968
1937
|
if (leaf.data.length < 1) {
|
|
3969
1938
|
throw new Error("Invalid leaf data");
|
|
@@ -3976,7 +1945,7 @@ function hash_leaf(leaf) {
|
|
|
3976
1945
|
return `0x${num2.toHexString(value).replace(/^0x/, "").padStart(64, "0")}`;
|
|
3977
1946
|
}
|
|
3978
1947
|
function pedersen_hash(a, b) {
|
|
3979
|
-
return BigInt(pedersen(a, b).toString());
|
|
1948
|
+
return BigInt(starknet.pedersen(a, b).toString());
|
|
3980
1949
|
}
|
|
3981
1950
|
var StandardMerkleTree = class _StandardMerkleTree extends MerkleTreeImpl {
|
|
3982
1951
|
constructor(tree, values, leafEncoding) {
|
|
@@ -4133,6 +2102,7 @@ function getMainnetConfig(rpcUrl = "https://starknet-mainnet.public.blastapi.io"
|
|
|
4133
2102
|
provider: new RpcProvider2({
|
|
4134
2103
|
nodeUrl: rpcUrl,
|
|
4135
2104
|
blockIdentifier
|
|
2105
|
+
// specVersion
|
|
4136
2106
|
}),
|
|
4137
2107
|
stage: "production",
|
|
4138
2108
|
network: "mainnet" /* mainnet */
|
|
@@ -17888,7 +15858,7 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
|
|
|
17888
15858
|
quote.buyAmount.toString(),
|
|
17889
15859
|
tokenToBuyInfo.decimals
|
|
17890
15860
|
).multipliedBy(0.9999);
|
|
17891
|
-
const
|
|
15861
|
+
const output = await this.avnu.getSwapInfo(
|
|
17892
15862
|
quote,
|
|
17893
15863
|
this.address.address,
|
|
17894
15864
|
0,
|
|
@@ -17896,9 +15866,9 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
|
|
|
17896
15866
|
minAmountOut.toWei()
|
|
17897
15867
|
);
|
|
17898
15868
|
logger.verbose(
|
|
17899
|
-
`${_EkuboCLVault.name}: getSwapInfoToHandleUnused => swap info found: ${JSON.stringify(
|
|
15869
|
+
`${_EkuboCLVault.name}: getSwapInfoToHandleUnused => swap info found: ${JSON.stringify(output)}`
|
|
17900
15870
|
);
|
|
17901
|
-
return
|
|
15871
|
+
return output;
|
|
17902
15872
|
}
|
|
17903
15873
|
}
|
|
17904
15874
|
throw new Error("Failed to get swap info");
|
|
@@ -20453,7 +18423,7 @@ var SenseiStrategies = [
|
|
|
20453
18423
|
];
|
|
20454
18424
|
|
|
20455
18425
|
// src/strategies/universal-adapters/baseAdapter.ts
|
|
20456
|
-
import { hash
|
|
18426
|
+
import { hash, num as num6, shortString } from "starknet";
|
|
20457
18427
|
|
|
20458
18428
|
// src/strategies/universal-adapters/adapter-utils.ts
|
|
20459
18429
|
var SIMPLE_SANITIZER = ContractAddr.from("0x11b59e89b35dfceb3e48ec18c01f8ec569592026c275bcb58e22af9f4dedaac");
|
|
@@ -20479,7 +18449,7 @@ var BaseAdapter = class extends CacheClass {
|
|
|
20479
18449
|
// sanitizer address
|
|
20480
18450
|
target.toBigInt(),
|
|
20481
18451
|
// contract
|
|
20482
|
-
toBigInt(
|
|
18452
|
+
toBigInt(hash.getSelectorFromName(method)),
|
|
20483
18453
|
// method name
|
|
20484
18454
|
BigInt(packedArguments.length),
|
|
20485
18455
|
...packedArguments
|
|
@@ -20489,7 +18459,7 @@ var BaseAdapter = class extends CacheClass {
|
|
|
20489
18459
|
};
|
|
20490
18460
|
|
|
20491
18461
|
// src/strategies/universal-adapters/common-adapter.ts
|
|
20492
|
-
import { hash as
|
|
18462
|
+
import { hash as hash2, uint256 as uint2566 } from "starknet";
|
|
20493
18463
|
var CommonAdapter = class extends BaseAdapter {
|
|
20494
18464
|
constructor(config) {
|
|
20495
18465
|
super();
|
|
@@ -20519,7 +18489,7 @@ var CommonAdapter = class extends BaseAdapter {
|
|
|
20519
18489
|
sanitizer: SIMPLE_SANITIZER,
|
|
20520
18490
|
call: {
|
|
20521
18491
|
contractAddress: this.config.manager,
|
|
20522
|
-
selector:
|
|
18492
|
+
selector: hash2.getSelectorFromName("flash_loan"),
|
|
20523
18493
|
calldata: [
|
|
20524
18494
|
this.config.manager.toBigInt(),
|
|
20525
18495
|
// receiver
|
|
@@ -20558,7 +18528,7 @@ var CommonAdapter = class extends BaseAdapter {
|
|
|
20558
18528
|
sanitizer: SIMPLE_SANITIZER,
|
|
20559
18529
|
call: {
|
|
20560
18530
|
contractAddress: token,
|
|
20561
|
-
selector:
|
|
18531
|
+
selector: hash2.getSelectorFromName("approve"),
|
|
20562
18532
|
calldata: [
|
|
20563
18533
|
spender.toBigInt(),
|
|
20564
18534
|
// spender
|
|
@@ -20574,7 +18544,7 @@ var CommonAdapter = class extends BaseAdapter {
|
|
|
20574
18544
|
};
|
|
20575
18545
|
|
|
20576
18546
|
// src/strategies/universal-adapters/vesu-adapter.ts
|
|
20577
|
-
import { CairoCustomEnum as CairoCustomEnum2, Contract as Contract8, hash as
|
|
18547
|
+
import { CairoCustomEnum as CairoCustomEnum2, Contract as Contract8, hash as hash3, RpcProvider as RpcProvider4, uint256 as uint2567 } from "starknet";
|
|
20578
18548
|
|
|
20579
18549
|
// src/data/vesu-singleton.abi.json
|
|
20580
18550
|
var vesu_singleton_abi_default = [
|
|
@@ -22857,13 +20827,13 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
22857
20827
|
toBigInt(positionData.length),
|
|
22858
20828
|
...positionData
|
|
22859
20829
|
];
|
|
22860
|
-
const
|
|
20830
|
+
const output = this.constructSimpleLeafData({
|
|
22861
20831
|
id: this.config.id,
|
|
22862
20832
|
target: this.VESU_SINGLETON,
|
|
22863
20833
|
method: "modify_position",
|
|
22864
20834
|
packedArguments
|
|
22865
20835
|
});
|
|
22866
|
-
return { leaf:
|
|
20836
|
+
return { leaf: output, callConstructor: this.getModifyPositionCall.bind(this) };
|
|
22867
20837
|
};
|
|
22868
20838
|
this.getModifyPositionCall = (params) => {
|
|
22869
20839
|
const _collateral = {
|
|
@@ -22900,7 +20870,7 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
22900
20870
|
sanitizer: SIMPLE_SANITIZER,
|
|
22901
20871
|
call: {
|
|
22902
20872
|
contractAddress: this.VESU_SINGLETON,
|
|
22903
|
-
selector:
|
|
20873
|
+
selector: hash3.getSelectorFromName("modify_position"),
|
|
22904
20874
|
calldata: [
|
|
22905
20875
|
...call.calldata
|
|
22906
20876
|
]
|
|
@@ -22956,8 +20926,8 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
22956
20926
|
if (cacheData) {
|
|
22957
20927
|
return cacheData;
|
|
22958
20928
|
}
|
|
22959
|
-
const
|
|
22960
|
-
this.setCache(CACHE_KEY, Number(
|
|
20929
|
+
const output = await this.getVesuSingletonContract(config).call("ltv_config", [this.config.poolId.address, this.config.collateral.address.address, this.config.debt.address.address]);
|
|
20930
|
+
this.setCache(CACHE_KEY, Number(output.max_ltv) / 1e18, 3e5);
|
|
22961
20931
|
return this.getCache(CACHE_KEY);
|
|
22962
20932
|
}
|
|
22963
20933
|
async getPositions(config) {
|
|
@@ -22969,7 +20939,7 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
22969
20939
|
if (cacheData) {
|
|
22970
20940
|
return cacheData;
|
|
22971
20941
|
}
|
|
22972
|
-
const
|
|
20942
|
+
const output = await this.getVesuSingletonContract(config).call("position_unsafe", [
|
|
22973
20943
|
this.config.poolId.address,
|
|
22974
20944
|
this.config.collateral.address.address,
|
|
22975
20945
|
this.config.debt.address.address,
|
|
@@ -22977,8 +20947,8 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
22977
20947
|
]);
|
|
22978
20948
|
const token1Price = await this.pricer.getPrice(this.config.collateral.symbol);
|
|
22979
20949
|
const token2Price = await this.pricer.getPrice(this.config.debt.symbol);
|
|
22980
|
-
const collateralAmount = Web3Number.fromWei(
|
|
22981
|
-
const debtAmount = Web3Number.fromWei(
|
|
20950
|
+
const collateralAmount = Web3Number.fromWei(output["1"].toString(), this.config.collateral.decimals);
|
|
20951
|
+
const debtAmount = Web3Number.fromWei(output["2"].toString(), this.config.debt.decimals);
|
|
22982
20952
|
const value = [{
|
|
22983
20953
|
amount: collateralAmount,
|
|
22984
20954
|
token: this.config.collateral,
|
|
@@ -23002,14 +20972,14 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
23002
20972
|
if (cacheData) {
|
|
23003
20973
|
return cacheData;
|
|
23004
20974
|
}
|
|
23005
|
-
const
|
|
20975
|
+
const output = await this.getVesuSingletonContract(config).call("check_collateralization_unsafe", [
|
|
23006
20976
|
this.config.poolId.address,
|
|
23007
20977
|
this.config.collateral.address.address,
|
|
23008
20978
|
this.config.debt.address.address,
|
|
23009
20979
|
this.config.vaultAllocator.address
|
|
23010
20980
|
]);
|
|
23011
|
-
const collateralAmount = Web3Number.fromWei(
|
|
23012
|
-
const debtAmount = Web3Number.fromWei(
|
|
20981
|
+
const collateralAmount = Web3Number.fromWei(output["1"].toString(), 18);
|
|
20982
|
+
const debtAmount = Web3Number.fromWei(output["2"].toString(), 18);
|
|
23013
20983
|
const value = [{
|
|
23014
20984
|
token: this.config.collateral,
|
|
23015
20985
|
usdValue: collateralAmount.toNumber(),
|
|
@@ -23026,8 +20996,8 @@ var VesuAdapter = class _VesuAdapter extends BaseAdapter {
|
|
|
23026
20996
|
const collateralizationProm = this.getCollateralization(this.networkConfig);
|
|
23027
20997
|
const positionsProm = this.getPositions(this.networkConfig);
|
|
23028
20998
|
const ltvProm = this.getLTVConfig(this.networkConfig);
|
|
23029
|
-
const
|
|
23030
|
-
const [collateralization, positions, ltv] =
|
|
20999
|
+
const output = await Promise.all([collateralizationProm, positionsProm, ltvProm]);
|
|
21000
|
+
const [collateralization, positions, ltv] = output;
|
|
23031
21001
|
const collateralTokenAmount = positions[0].amount;
|
|
23032
21002
|
const collateralUSDAmount = collateralization[0].usdValue;
|
|
23033
21003
|
const collateralPrice = collateralUSDAmount / collateralTokenAmount.toNumber();
|
|
@@ -25295,6 +23265,11 @@ var vault_manager_abi_default = [
|
|
|
25295
23265
|
];
|
|
25296
23266
|
|
|
25297
23267
|
// src/strategies/universal-strategy.ts
|
|
23268
|
+
var AUMTypes = /* @__PURE__ */ ((AUMTypes2) => {
|
|
23269
|
+
AUMTypes2["FINALISED"] = "finalised";
|
|
23270
|
+
AUMTypes2["DEFISPRING"] = "defispring";
|
|
23271
|
+
return AUMTypes2;
|
|
23272
|
+
})(AUMTypes || {});
|
|
25298
23273
|
var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
25299
23274
|
constructor(config, pricer, metadata) {
|
|
25300
23275
|
super(config);
|
|
@@ -25379,6 +23354,19 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25379
23354
|
]);
|
|
25380
23355
|
return [call1, call2];
|
|
25381
23356
|
}
|
|
23357
|
+
async withdrawCall(amountInfo, receiver, owner) {
|
|
23358
|
+
assert(
|
|
23359
|
+
amountInfo.tokenInfo.address.eq(this.asset().address),
|
|
23360
|
+
"Withdraw token mismatch"
|
|
23361
|
+
);
|
|
23362
|
+
const shares = await this.contract.call("convert_to_shares", [uint2568.bnToUint256(amountInfo.amount.toWei())]);
|
|
23363
|
+
const call = this.contract.populate("request_redeem", [
|
|
23364
|
+
uint2568.bnToUint256(shares.toString()),
|
|
23365
|
+
receiver.address,
|
|
23366
|
+
owner.address
|
|
23367
|
+
]);
|
|
23368
|
+
return [call];
|
|
23369
|
+
}
|
|
25382
23370
|
/**
|
|
25383
23371
|
* Calculates the Total Value Locked (TVL) for a specific user.
|
|
25384
23372
|
* @param user - Address of the user
|
|
@@ -25425,14 +23413,24 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25425
23413
|
const collateral2APY = Number(collateralAsset2.supplyApy.value) / 1e18;
|
|
25426
23414
|
const debt2APY = Number(debtAsset2.borrowApr.value) / 1e18;
|
|
25427
23415
|
const positions = await this.getVaultPositions();
|
|
23416
|
+
logger.verbose(`${this.metadata.name}::netAPY: positions: ${JSON.stringify(positions)}`);
|
|
23417
|
+
if (positions.every((p) => p.amount.isZero())) {
|
|
23418
|
+
return { net: 0, splits: [{
|
|
23419
|
+
apy: 0,
|
|
23420
|
+
id: "base"
|
|
23421
|
+
}, {
|
|
23422
|
+
apy: 0,
|
|
23423
|
+
id: "defispring"
|
|
23424
|
+
}] };
|
|
23425
|
+
}
|
|
25428
23426
|
const weights = positions.map((p, index) => p.usdValue * (index % 2 == 0 ? 1 : -1));
|
|
25429
23427
|
const baseAPYs = [collateral1APY, debt1APY, collateral2APY, debt2APY];
|
|
25430
23428
|
assert(positions.length == baseAPYs.length, "Positions and APYs length mismatch");
|
|
25431
23429
|
const rewardAPYs = [Number(collateralAsset1.defiSpringSupplyApr.value) / 1e18, 0, Number(collateralAsset2.defiSpringSupplyApr.value) / 1e18, 0];
|
|
25432
23430
|
const baseAPY = this.computeAPY(baseAPYs, weights);
|
|
25433
23431
|
const rewardAPY = this.computeAPY(rewardAPYs, weights);
|
|
25434
|
-
const apys = [...baseAPYs, ...rewardAPYs];
|
|
25435
23432
|
const netAPY = baseAPY + rewardAPY;
|
|
23433
|
+
logger.verbose(`${this.metadata.name}::netAPY: net: ${netAPY}, baseAPY: ${baseAPY}, rewardAPY: ${rewardAPY}`);
|
|
25436
23434
|
return { net: netAPY, splits: [{
|
|
25437
23435
|
apy: baseAPY,
|
|
25438
23436
|
id: "base"
|
|
@@ -25467,6 +23465,16 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25467
23465
|
usdValue
|
|
25468
23466
|
};
|
|
25469
23467
|
}
|
|
23468
|
+
async getUnusedBalance() {
|
|
23469
|
+
const balance = await new ERC20(this.config).balanceOf(this.asset().address, this.metadata.additionalInfo.vaultAllocator, this.asset().decimals);
|
|
23470
|
+
const price = await this.pricer.getPrice(this.metadata.depositTokens[0].symbol);
|
|
23471
|
+
const usdValue = Number(balance.toFixed(6)) * price.price;
|
|
23472
|
+
return {
|
|
23473
|
+
tokenInfo: this.asset(),
|
|
23474
|
+
amount: balance,
|
|
23475
|
+
usdValue
|
|
23476
|
+
};
|
|
23477
|
+
}
|
|
25470
23478
|
async getAUM() {
|
|
25471
23479
|
const currentAUM = await this.contract.call("aum", []);
|
|
25472
23480
|
const lastReportTime = await this.contract.call("last_report_timestamp", []);
|
|
@@ -25474,14 +23482,32 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25474
23482
|
const [vesuAdapter1, vesuAdapter2] = this.getVesuAdapters();
|
|
25475
23483
|
const leg1AUM = await vesuAdapter1.getPositions(this.config);
|
|
25476
23484
|
const leg2AUM = await vesuAdapter2.getPositions(this.config);
|
|
25477
|
-
const
|
|
23485
|
+
const balance = await this.getUnusedBalance();
|
|
23486
|
+
logger.verbose(`${this.getTag()} unused balance: ${balance.amount.toNumber()}`);
|
|
23487
|
+
const vesuAum = leg1AUM[0].amount.plus(leg2AUM[0].usdValue / token1Price.price).minus(leg1AUM[1].usdValue / token1Price.price).minus(leg2AUM[1].amount);
|
|
23488
|
+
const zeroAmt = Web3Number.fromWei("0", this.asset().decimals);
|
|
23489
|
+
const net = {
|
|
23490
|
+
tokenInfo: this.asset(),
|
|
23491
|
+
amount: zeroAmt,
|
|
23492
|
+
usdValue: 0
|
|
23493
|
+
};
|
|
23494
|
+
const prevAum = Web3Number.fromWei(currentAUM.toString(), this.asset().decimals);
|
|
23495
|
+
if (vesuAum.isZero()) {
|
|
23496
|
+
return { net, splits: [{
|
|
23497
|
+
aum: zeroAmt,
|
|
23498
|
+
id: "finalised" /* FINALISED */
|
|
23499
|
+
}, {
|
|
23500
|
+
aum: zeroAmt,
|
|
23501
|
+
id: "defispring" /* DEFISPRING */
|
|
23502
|
+
}], prevAum };
|
|
23503
|
+
}
|
|
23504
|
+
const aumToken = vesuAum.plus(balance.amount);
|
|
25478
23505
|
logger.verbose(`${this.getTag()} Actual AUM: ${aumToken}`);
|
|
25479
23506
|
const netAPY = await this.netAPY();
|
|
25480
23507
|
const defispringAPY = netAPY.splits.find((s) => s.id === "defispring")?.apy || 0;
|
|
25481
23508
|
if (!defispringAPY) throw new Error("DefiSpring APY not found");
|
|
25482
23509
|
const timeDiff = Math.round(Date.now() / 1e3) - Number(lastReportTime);
|
|
25483
23510
|
const growthRate = timeDiff * defispringAPY / (365 * 24 * 60 * 60);
|
|
25484
|
-
const prevAum = Web3Number.fromWei(currentAUM.toString(), this.asset().decimals);
|
|
25485
23511
|
const rewardAssets = prevAum.multipliedBy(growthRate);
|
|
25486
23512
|
logger.verbose(`${this.getTag()} DefiSpring AUM time difference: ${timeDiff}`);
|
|
25487
23513
|
logger.verbose(`${this.getTag()} Current AUM: ${currentAUM}`);
|
|
@@ -25489,16 +23515,13 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25489
23515
|
logger.verbose(`${this.getTag()} rewards AUM: ${rewardAssets}`);
|
|
25490
23516
|
const newAUM = aumToken.plus(rewardAssets);
|
|
25491
23517
|
logger.verbose(`${this.getTag()} New AUM: ${newAUM}`);
|
|
25492
|
-
|
|
25493
|
-
|
|
25494
|
-
amount: newAUM,
|
|
25495
|
-
usdValue: newAUM.multipliedBy(token1Price.price).toNumber()
|
|
25496
|
-
};
|
|
23518
|
+
net.amount = newAUM;
|
|
23519
|
+
net.usdValue = newAUM.multipliedBy(token1Price.price).toNumber();
|
|
25497
23520
|
const splits = [{
|
|
25498
|
-
id: "finalised"
|
|
23521
|
+
id: "finalised" /* FINALISED */,
|
|
25499
23522
|
aum: aumToken
|
|
25500
23523
|
}, {
|
|
25501
|
-
id: "defispring"
|
|
23524
|
+
id: "defispring" /* DEFISPRING */,
|
|
25502
23525
|
aum: rewardAssets
|
|
25503
23526
|
}];
|
|
25504
23527
|
return { net, splits, prevAum };
|
|
@@ -25549,19 +23572,19 @@ var UniversalStrategy = class _UniversalStrategy extends BaseStrategy {
|
|
|
25549
23572
|
debtAmount: params.debtAmount,
|
|
25550
23573
|
isBorrow: params.isDeposit
|
|
25551
23574
|
}));
|
|
25552
|
-
const
|
|
23575
|
+
const output = [{
|
|
25553
23576
|
proofs: manage5Info.proofs,
|
|
25554
23577
|
manageCall: manageCall5,
|
|
25555
23578
|
step: STEP2_ID
|
|
25556
23579
|
}];
|
|
25557
23580
|
if (approveAmount.gt(0)) {
|
|
25558
|
-
|
|
23581
|
+
output.unshift({
|
|
25559
23582
|
proofs: manage4Info.proofs,
|
|
25560
23583
|
manageCall: manageCall4,
|
|
25561
23584
|
step: STEP1_ID
|
|
25562
23585
|
});
|
|
25563
23586
|
}
|
|
25564
|
-
return
|
|
23587
|
+
return output;
|
|
25565
23588
|
}
|
|
25566
23589
|
getTag() {
|
|
25567
23590
|
return `${_UniversalStrategy.name}:${this.metadata.name}`;
|
|
@@ -25796,6 +23819,7 @@ var usdcVaultSettings = {
|
|
|
25796
23819
|
manager: ContractAddr.from("0xf41a2b1f498a7f9629db0b8519259e66e964260a23d20003f3e42bb1997a07"),
|
|
25797
23820
|
vaultAllocator: ContractAddr.from("0x228cca1005d3f2b55cbaba27cb291dacf1b9a92d1d6b1638195fbd3d0c1e3ba"),
|
|
25798
23821
|
redeemRequestNFT: ContractAddr.from("0x906d03590010868cbf7590ad47043959d7af8e782089a605d9b22567b64fda"),
|
|
23822
|
+
aumOracle: ContractAddr.from("0x6faf45ed185dec13ef723c9ead4266cab98d06f2cb237e331b1fa5c2aa79afe"),
|
|
25799
23823
|
leafAdapters: [],
|
|
25800
23824
|
adapters: [],
|
|
25801
23825
|
targetHealthFactor: 1.3,
|
|
@@ -25805,6 +23829,37 @@ var wbtcVaultSettings = {
|
|
|
25805
23829
|
manager: ContractAddr.from("0xef8a664ffcfe46a6af550766d27c28937bf1b77fb4ab54d8553e92bca5ba34"),
|
|
25806
23830
|
vaultAllocator: ContractAddr.from("0x1e01c25f0d9494570226ad28a7fa856c0640505e809c366a9fab4903320e735"),
|
|
25807
23831
|
redeemRequestNFT: ContractAddr.from("0x4fec59a12f8424281c1e65a80b5de51b4e754625c60cddfcd00d46941ec37b2"),
|
|
23832
|
+
aumOracle: ContractAddr.from("0x2edf4edbed3f839e7f07dcd913e92299898ff4cf0ba532f8c572c66c5b331b2"),
|
|
23833
|
+
leafAdapters: [],
|
|
23834
|
+
adapters: [],
|
|
23835
|
+
targetHealthFactor: 1.3,
|
|
23836
|
+
minHealthFactor: 1.25
|
|
23837
|
+
};
|
|
23838
|
+
var ethVaultSettings = {
|
|
23839
|
+
manager: ContractAddr.from("0x494888b37206616bd09d759dcda61e5118470b9aa7f58fb84f21c778a7b8f97"),
|
|
23840
|
+
vaultAllocator: ContractAddr.from("0x4acc0ad6bea58cb578d60ff7c31f06f44369a7a9a7bbfffe4701f143e427bd"),
|
|
23841
|
+
redeemRequestNFT: ContractAddr.from("0x2e6cd71e5060a254d4db00655e420db7bf89da7755bb0d5f922e2f00c76ac49"),
|
|
23842
|
+
aumOracle: ContractAddr.from("0x4b747f2e75c057bed9aa2ce46fbdc2159dc684c15bd32d4f95983a6ecf39a05"),
|
|
23843
|
+
leafAdapters: [],
|
|
23844
|
+
adapters: [],
|
|
23845
|
+
targetHealthFactor: 1.3,
|
|
23846
|
+
minHealthFactor: 1.25
|
|
23847
|
+
};
|
|
23848
|
+
var strkVaultSettings = {
|
|
23849
|
+
manager: ContractAddr.from("0xcc6a5153ca56293405506eb20826a379d982cd738008ef7e808454d318fb81"),
|
|
23850
|
+
vaultAllocator: ContractAddr.from("0xf29d2f82e896c0ed74c9eff220af34ac148e8b99846d1ace9fbb02c9191d01"),
|
|
23851
|
+
redeemRequestNFT: ContractAddr.from("0x46902423bd632c428376b84fcee9cac5dbe016214e93a8103bcbde6e1de656b"),
|
|
23852
|
+
aumOracle: ContractAddr.from("0x6d7dbfad4bb51715da211468389a623da00c0625f8f6efbea822ee5ac5231f4"),
|
|
23853
|
+
leafAdapters: [],
|
|
23854
|
+
adapters: [],
|
|
23855
|
+
targetHealthFactor: 1.3,
|
|
23856
|
+
minHealthFactor: 1.25
|
|
23857
|
+
};
|
|
23858
|
+
var usdtVaultSettings = {
|
|
23859
|
+
manager: ContractAddr.from("0x39bb9843503799b552b7ed84b31c06e4ff10c0537edcddfbf01fe944b864029"),
|
|
23860
|
+
vaultAllocator: ContractAddr.from("0x56437d18c43727ac971f6c7086031cad7d9d6ccb340f4f3785a74cc791c931a"),
|
|
23861
|
+
redeemRequestNFT: ContractAddr.from("0x5af0c2a657eaa8e23ed78e855dac0c51e4f69e2cf91a18c472041a1f75bb41f"),
|
|
23862
|
+
aumOracle: ContractAddr.from("0x7018f8040c8066a4ab929e6760ae52dd43b6a3a289172f514750a61fcc565cc"),
|
|
25808
23863
|
leafAdapters: [],
|
|
25809
23864
|
adapters: [],
|
|
25810
23865
|
targetHealthFactor: 1.3,
|
|
@@ -25848,9 +23903,67 @@ var UniversalStrategies = [
|
|
|
25848
23903
|
contractDetails: [],
|
|
25849
23904
|
faqs: [],
|
|
25850
23905
|
investmentSteps: []
|
|
23906
|
+
},
|
|
23907
|
+
{
|
|
23908
|
+
name: "ETH Evergreen",
|
|
23909
|
+
description: "A universal strategy for managing ETH assets",
|
|
23910
|
+
address: ContractAddr.from("0x446c22d4d3f5cb52b4950ba832ba1df99464c6673a37c092b1d9622650dbd8"),
|
|
23911
|
+
launchBlock: 0,
|
|
23912
|
+
type: "ERC4626",
|
|
23913
|
+
depositTokens: [Global.getDefaultTokens().find((token) => token.symbol === "ETH")],
|
|
23914
|
+
additionalInfo: getLooperSettings("ETH", "WBTC", ethVaultSettings, VesuPools.Genesis, VesuPools.Genesis),
|
|
23915
|
+
risk: {
|
|
23916
|
+
riskFactor: _riskFactor4,
|
|
23917
|
+
netRisk: _riskFactor4.reduce((acc, curr) => acc + curr.value * curr.weight, 0) / _riskFactor4.reduce((acc, curr) => acc + curr.weight, 0),
|
|
23918
|
+
notARisks: getNoRiskTags(_riskFactor4)
|
|
23919
|
+
},
|
|
23920
|
+
protocols: [Protocols.VESU],
|
|
23921
|
+
maxTVL: Web3Number.fromWei(0, 18),
|
|
23922
|
+
contractDetails: [],
|
|
23923
|
+
faqs: [],
|
|
23924
|
+
investmentSteps: []
|
|
23925
|
+
},
|
|
23926
|
+
{
|
|
23927
|
+
name: "STRK Evergreen",
|
|
23928
|
+
description: "A universal strategy for managing STRK assets",
|
|
23929
|
+
address: ContractAddr.from("0x55d012f57e58c96e0a5c7ebbe55853989d01e6538b15a95e7178aca4af05c21"),
|
|
23930
|
+
launchBlock: 0,
|
|
23931
|
+
type: "ERC4626",
|
|
23932
|
+
depositTokens: [Global.getDefaultTokens().find((token) => token.symbol === "STRK")],
|
|
23933
|
+
additionalInfo: getLooperSettings("STRK", "ETH", strkVaultSettings, VesuPools.Genesis, VesuPools.Genesis),
|
|
23934
|
+
risk: {
|
|
23935
|
+
riskFactor: _riskFactor4,
|
|
23936
|
+
netRisk: _riskFactor4.reduce((acc, curr) => acc + curr.value * curr.weight, 0) / _riskFactor4.reduce((acc, curr) => acc + curr.weight, 0),
|
|
23937
|
+
notARisks: getNoRiskTags(_riskFactor4)
|
|
23938
|
+
},
|
|
23939
|
+
protocols: [Protocols.VESU],
|
|
23940
|
+
maxTVL: Web3Number.fromWei(0, 18),
|
|
23941
|
+
contractDetails: [],
|
|
23942
|
+
faqs: [],
|
|
23943
|
+
investmentSteps: []
|
|
23944
|
+
},
|
|
23945
|
+
{
|
|
23946
|
+
name: "USDT Evergreen",
|
|
23947
|
+
description: "A universal strategy for managing USDT assets",
|
|
23948
|
+
address: ContractAddr.from("0x1c4933d1880c6778585e597154eaca7b428579d72f3aae425ad2e4d26c6bb3"),
|
|
23949
|
+
launchBlock: 0,
|
|
23950
|
+
type: "ERC4626",
|
|
23951
|
+
depositTokens: [Global.getDefaultTokens().find((token) => token.symbol === "USDT")],
|
|
23952
|
+
additionalInfo: getLooperSettings("USDT", "ETH", usdtVaultSettings, VesuPools.Genesis, VesuPools.Genesis),
|
|
23953
|
+
risk: {
|
|
23954
|
+
riskFactor: _riskFactor4,
|
|
23955
|
+
netRisk: _riskFactor4.reduce((acc, curr) => acc + curr.value * curr.weight, 0) / _riskFactor4.reduce((acc, curr) => acc + curr.weight, 0),
|
|
23956
|
+
notARisks: getNoRiskTags(_riskFactor4)
|
|
23957
|
+
},
|
|
23958
|
+
protocols: [Protocols.VESU],
|
|
23959
|
+
maxTVL: Web3Number.fromWei(0, 6),
|
|
23960
|
+
contractDetails: [],
|
|
23961
|
+
faqs: [],
|
|
23962
|
+
investmentSteps: []
|
|
25851
23963
|
}
|
|
25852
23964
|
];
|
|
25853
23965
|
export {
|
|
23966
|
+
AUMTypes,
|
|
25854
23967
|
AutoCompounderSTRK,
|
|
25855
23968
|
AvnuWrapper,
|
|
25856
23969
|
BaseAdapter,
|
|
@@ -25892,26 +24005,3 @@ export {
|
|
|
25892
24005
|
getRiskExplaination,
|
|
25893
24006
|
highlightTextWithLinks
|
|
25894
24007
|
};
|
|
25895
|
-
/*! Bundled license information:
|
|
25896
|
-
|
|
25897
|
-
@noble/hashes/esm/utils.js:
|
|
25898
|
-
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
25899
|
-
|
|
25900
|
-
@noble/curves/esm/abstract/utils.js:
|
|
25901
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
25902
|
-
|
|
25903
|
-
@noble/curves/esm/abstract/modular.js:
|
|
25904
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
25905
|
-
|
|
25906
|
-
@noble/curves/esm/abstract/poseidon.js:
|
|
25907
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
25908
|
-
|
|
25909
|
-
@noble/curves/esm/abstract/curve.js:
|
|
25910
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
25911
|
-
|
|
25912
|
-
@noble/curves/esm/abstract/weierstrass.js:
|
|
25913
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
25914
|
-
|
|
25915
|
-
@noble/curves/esm/_shortw_utils.js:
|
|
25916
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
25917
|
-
*/
|