@fangorn-network/sdk 2026.7.14 → 2026.7.24
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/lib/crypto/aes.js +1 -1
- package/lib/crypto/encryption.d.ts +2 -2
- package/lib/crypto/encryption.js +3 -3
- package/lib/engine/index.js +1 -1
- package/lib/fangorn.d.ts +1 -1
- package/lib/fangorn.js +2 -6
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -1
- package/lib/providers/storage/car.js +1 -1
- package/lib/providers/storage/pinata.js +22 -3
- package/package.json +3 -1
- package/lib/node_modules/@noble/ciphers/esm/_polyval.js +0 -218
- package/lib/node_modules/@noble/ciphers/esm/aes.js +0 -261
- package/lib/node_modules/@noble/ciphers/esm/utils.js +0 -156
- package/lib/node_modules/@noble/curves/esm/abstract/curve.js +0 -299
- package/lib/node_modules/@noble/curves/esm/abstract/edwards.js +0 -532
- package/lib/node_modules/@noble/curves/esm/abstract/modular.js +0 -344
- package/lib/node_modules/@noble/curves/esm/abstract/montgomery.js +0 -137
- package/lib/node_modules/@noble/curves/esm/ed25519.js +0 -262
- package/lib/node_modules/@noble/curves/esm/utils.js +0 -150
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
import { bytesToNumberLE, ensureBytes, equalBytes } from "./utils.js";
|
|
2
|
-
import { Field, isNegativeLE, mod, pow2 } from "./abstract/modular.js";
|
|
3
|
-
import { pippenger } from "./abstract/curve.js";
|
|
4
|
-
import { PrimeEdwardsPoint, twistedEdwards } from "./abstract/edwards.js";
|
|
5
|
-
import { montgomery } from "./abstract/montgomery.js";
|
|
6
|
-
import { sha512 } from "@noble/hashes/sha2.js";
|
|
7
|
-
import { abytes } from "@noble/hashes/utils.js";
|
|
8
|
-
|
|
9
|
-
//#region node_modules/@noble/curves/esm/ed25519.js
|
|
10
|
-
/**
|
|
11
|
-
* ed25519 Twisted Edwards curve with following addons:
|
|
12
|
-
* - X25519 ECDH
|
|
13
|
-
* - Ristretto cofactor elimination
|
|
14
|
-
* - Elligator hash-to-group / point indistinguishability
|
|
15
|
-
* @module
|
|
16
|
-
*/
|
|
17
|
-
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
18
|
-
const _0n = /* @__PURE__ */ BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
|
|
19
|
-
const _5n = BigInt(5), _8n = BigInt(8);
|
|
20
|
-
const ed25519_CURVE_p = BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
|
|
21
|
-
const ed25519_CURVE = /* @__PURE__ */ (() => ({
|
|
22
|
-
p: ed25519_CURVE_p,
|
|
23
|
-
n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
|
|
24
|
-
h: _8n,
|
|
25
|
-
a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
|
|
26
|
-
d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
|
|
27
|
-
Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
|
|
28
|
-
Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
|
|
29
|
-
}))();
|
|
30
|
-
function ed25519_pow_2_252_3(x) {
|
|
31
|
-
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
32
|
-
const P = ed25519_CURVE_p;
|
|
33
|
-
const b2 = x * x % P * x % P;
|
|
34
|
-
const b5 = pow2(pow2(b2, _2n, P) * b2 % P, _1n, P) * x % P;
|
|
35
|
-
const b10 = pow2(b5, _5n, P) * b5 % P;
|
|
36
|
-
const b20 = pow2(b10, _10n, P) * b10 % P;
|
|
37
|
-
const b40 = pow2(b20, _20n, P) * b20 % P;
|
|
38
|
-
const b80 = pow2(b40, _40n, P) * b40 % P;
|
|
39
|
-
return {
|
|
40
|
-
pow_p_5_8: pow2(pow2(pow2(pow2(b80, _80n, P) * b80 % P, _80n, P) * b80 % P, _10n, P) * b10 % P, _2n, P) * x % P,
|
|
41
|
-
b2
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
function adjustScalarBytes(bytes) {
|
|
45
|
-
bytes[0] &= 248;
|
|
46
|
-
bytes[31] &= 127;
|
|
47
|
-
bytes[31] |= 64;
|
|
48
|
-
return bytes;
|
|
49
|
-
}
|
|
50
|
-
const ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
|
|
51
|
-
function uvRatio(u, v) {
|
|
52
|
-
const P = ed25519_CURVE_p;
|
|
53
|
-
const v3 = mod(v * v * v, P);
|
|
54
|
-
const pow = ed25519_pow_2_252_3(u * mod(v3 * v3 * v, P)).pow_p_5_8;
|
|
55
|
-
let x = mod(u * v3 * pow, P);
|
|
56
|
-
const vx2 = mod(v * x * x, P);
|
|
57
|
-
const root1 = x;
|
|
58
|
-
const root2 = mod(x * ED25519_SQRT_M1, P);
|
|
59
|
-
const useRoot1 = vx2 === u;
|
|
60
|
-
const useRoot2 = vx2 === mod(-u, P);
|
|
61
|
-
const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
|
|
62
|
-
if (useRoot1) x = root1;
|
|
63
|
-
if (useRoot2 || noRoot) x = root2;
|
|
64
|
-
if (isNegativeLE(x, P)) x = mod(-x, P);
|
|
65
|
-
return {
|
|
66
|
-
isValid: useRoot1 || useRoot2,
|
|
67
|
-
value: x
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
const Fp = /* @__PURE__ */ (() => Field(ed25519_CURVE.p, { isLE: true }))();
|
|
71
|
-
const Fn = /* @__PURE__ */ (() => Field(ed25519_CURVE.n, { isLE: true }))();
|
|
72
|
-
const ed25519Defaults = /* @__PURE__ */ (() => ({
|
|
73
|
-
...ed25519_CURVE,
|
|
74
|
-
Fp,
|
|
75
|
-
hash: sha512,
|
|
76
|
-
adjustScalarBytes,
|
|
77
|
-
uvRatio
|
|
78
|
-
}))();
|
|
79
|
-
/**
|
|
80
|
-
* ed25519 curve with EdDSA signatures.
|
|
81
|
-
* @example
|
|
82
|
-
* import { ed25519 } from '@noble/curves/ed25519';
|
|
83
|
-
* const { secretKey, publicKey } = ed25519.keygen();
|
|
84
|
-
* const msg = new TextEncoder().encode('hello');
|
|
85
|
-
* const sig = ed25519.sign(msg, priv);
|
|
86
|
-
* ed25519.verify(sig, msg, pub); // Default mode: follows ZIP215
|
|
87
|
-
* ed25519.verify(sig, msg, pub, { zip215: false }); // RFC8032 / FIPS 186-5
|
|
88
|
-
*/
|
|
89
|
-
const ed25519 = /* @__PURE__ */ (() => twistedEdwards(ed25519Defaults))();
|
|
90
|
-
/**
|
|
91
|
-
* ECDH using curve25519 aka x25519.
|
|
92
|
-
* @example
|
|
93
|
-
* import { x25519 } from '@noble/curves/ed25519';
|
|
94
|
-
* const priv = 'a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4';
|
|
95
|
-
* const pub = 'e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c';
|
|
96
|
-
* x25519.getSharedSecret(priv, pub) === x25519.scalarMult(priv, pub); // aliases
|
|
97
|
-
* x25519.getPublicKey(priv) === x25519.scalarMultBase(priv);
|
|
98
|
-
* x25519.getPublicKey(x25519.utils.randomSecretKey());
|
|
99
|
-
*/
|
|
100
|
-
const x25519 = /* @__PURE__ */ (() => {
|
|
101
|
-
const P = Fp.ORDER;
|
|
102
|
-
return montgomery({
|
|
103
|
-
P,
|
|
104
|
-
type: "x25519",
|
|
105
|
-
powPminus2: (x) => {
|
|
106
|
-
const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
|
|
107
|
-
return mod(pow2(pow_p_5_8, _3n, P) * b2, P);
|
|
108
|
-
},
|
|
109
|
-
adjustScalarBytes
|
|
110
|
-
});
|
|
111
|
-
})();
|
|
112
|
-
const SQRT_M1 = ED25519_SQRT_M1;
|
|
113
|
-
const SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
|
|
114
|
-
const INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
|
|
115
|
-
const ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
|
|
116
|
-
const D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
|
|
117
|
-
const invertSqrt = (number) => uvRatio(_1n, number);
|
|
118
|
-
const MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
119
|
-
const bytes255ToNumberLE = (bytes) => ed25519.Point.Fp.create(bytesToNumberLE(bytes) & MAX_255B);
|
|
120
|
-
/**
|
|
121
|
-
* Computes Elligator map for Ristretto255.
|
|
122
|
-
* Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-B) and on
|
|
123
|
-
* the [website](https://ristretto.group/formulas/elligator.html).
|
|
124
|
-
*/
|
|
125
|
-
function calcElligatorRistrettoMap(r0) {
|
|
126
|
-
const { d } = ed25519_CURVE;
|
|
127
|
-
const P = ed25519_CURVE_p;
|
|
128
|
-
const mod$1 = (n) => Fp.create(n);
|
|
129
|
-
const r = mod$1(SQRT_M1 * r0 * r0);
|
|
130
|
-
const Ns = mod$1((r + _1n) * ONE_MINUS_D_SQ);
|
|
131
|
-
let c = BigInt(-1);
|
|
132
|
-
const D = mod$1((c - d * r) * mod$1(r + d));
|
|
133
|
-
let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
|
|
134
|
-
let s_ = mod$1(s * r0);
|
|
135
|
-
if (!isNegativeLE(s_, P)) s_ = mod$1(-s_);
|
|
136
|
-
if (!Ns_D_is_sq) s = s_;
|
|
137
|
-
if (!Ns_D_is_sq) c = r;
|
|
138
|
-
const Nt = mod$1(c * (r - _1n) * D_MINUS_ONE_SQ - D);
|
|
139
|
-
const s2 = s * s;
|
|
140
|
-
const W0 = mod$1((s + s) * D);
|
|
141
|
-
const W1 = mod$1(Nt * SQRT_AD_MINUS_ONE);
|
|
142
|
-
const W2 = mod$1(_1n - s2);
|
|
143
|
-
const W3 = mod$1(_1n + s2);
|
|
144
|
-
return new ed25519.Point(mod$1(W0 * W3), mod$1(W2 * W1), mod$1(W1 * W3), mod$1(W0 * W2));
|
|
145
|
-
}
|
|
146
|
-
function ristretto255_map(bytes) {
|
|
147
|
-
abytes(bytes, 64);
|
|
148
|
-
const R1 = calcElligatorRistrettoMap(bytes255ToNumberLE(bytes.subarray(0, 32)));
|
|
149
|
-
const R2 = calcElligatorRistrettoMap(bytes255ToNumberLE(bytes.subarray(32, 64)));
|
|
150
|
-
return new _RistrettoPoint(R1.add(R2));
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Wrapper over Edwards Point for ristretto255.
|
|
154
|
-
*
|
|
155
|
-
* Each ed25519/ExtendedPoint has 8 different equivalent points. This can be
|
|
156
|
-
* a source of bugs for protocols like ring signatures. Ristretto was created to solve this.
|
|
157
|
-
* Ristretto point operates in X:Y:Z:T extended coordinates like ExtendedPoint,
|
|
158
|
-
* but it should work in its own namespace: do not combine those two.
|
|
159
|
-
* See [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
|
|
160
|
-
*/
|
|
161
|
-
var _RistrettoPoint = class _RistrettoPoint extends PrimeEdwardsPoint {
|
|
162
|
-
constructor(ep) {
|
|
163
|
-
super(ep);
|
|
164
|
-
}
|
|
165
|
-
static fromAffine(ap) {
|
|
166
|
-
return new _RistrettoPoint(ed25519.Point.fromAffine(ap));
|
|
167
|
-
}
|
|
168
|
-
assertSame(other) {
|
|
169
|
-
if (!(other instanceof _RistrettoPoint)) throw new Error("RistrettoPoint expected");
|
|
170
|
-
}
|
|
171
|
-
init(ep) {
|
|
172
|
-
return new _RistrettoPoint(ep);
|
|
173
|
-
}
|
|
174
|
-
/** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */
|
|
175
|
-
static hashToCurve(hex) {
|
|
176
|
-
return ristretto255_map(ensureBytes("ristrettoHash", hex, 64));
|
|
177
|
-
}
|
|
178
|
-
static fromBytes(bytes) {
|
|
179
|
-
abytes(bytes, 32);
|
|
180
|
-
const { a, d } = ed25519_CURVE;
|
|
181
|
-
const P = ed25519_CURVE_p;
|
|
182
|
-
const mod$1 = (n) => Fp.create(n);
|
|
183
|
-
const s = bytes255ToNumberLE(bytes);
|
|
184
|
-
if (!equalBytes(Fp.toBytes(s), bytes) || isNegativeLE(s, P)) throw new Error("invalid ristretto255 encoding 1");
|
|
185
|
-
const s2 = mod$1(s * s);
|
|
186
|
-
const u1 = mod$1(_1n + a * s2);
|
|
187
|
-
const u2 = mod$1(_1n - a * s2);
|
|
188
|
-
const u1_2 = mod$1(u1 * u1);
|
|
189
|
-
const u2_2 = mod$1(u2 * u2);
|
|
190
|
-
const v = mod$1(a * d * u1_2 - u2_2);
|
|
191
|
-
const { isValid, value: I } = invertSqrt(mod$1(v * u2_2));
|
|
192
|
-
const Dx = mod$1(I * u2);
|
|
193
|
-
const Dy = mod$1(I * Dx * v);
|
|
194
|
-
let x = mod$1((s + s) * Dx);
|
|
195
|
-
if (isNegativeLE(x, P)) x = mod$1(-x);
|
|
196
|
-
const y = mod$1(u1 * Dy);
|
|
197
|
-
const t = mod$1(x * y);
|
|
198
|
-
if (!isValid || isNegativeLE(t, P) || y === _0n) throw new Error("invalid ristretto255 encoding 2");
|
|
199
|
-
return new _RistrettoPoint(new ed25519.Point(x, y, _1n, t));
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Converts ristretto-encoded string to ristretto point.
|
|
203
|
-
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
204
|
-
* @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
205
|
-
*/
|
|
206
|
-
static fromHex(hex) {
|
|
207
|
-
return _RistrettoPoint.fromBytes(ensureBytes("ristrettoHex", hex, 32));
|
|
208
|
-
}
|
|
209
|
-
static msm(points, scalars) {
|
|
210
|
-
return pippenger(_RistrettoPoint, ed25519.Point.Fn, points, scalars);
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Encodes ristretto point to Uint8Array.
|
|
214
|
-
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
|
|
215
|
-
*/
|
|
216
|
-
toBytes() {
|
|
217
|
-
let { X, Y, Z, T } = this.ep;
|
|
218
|
-
const P = ed25519_CURVE_p;
|
|
219
|
-
const mod$1 = (n) => Fp.create(n);
|
|
220
|
-
const u1 = mod$1(mod$1(Z + Y) * mod$1(Z - Y));
|
|
221
|
-
const u2 = mod$1(X * Y);
|
|
222
|
-
const { value: invsqrt } = invertSqrt(mod$1(u1 * mod$1(u2 * u2)));
|
|
223
|
-
const D1 = mod$1(invsqrt * u1);
|
|
224
|
-
const D2 = mod$1(invsqrt * u2);
|
|
225
|
-
const zInv = mod$1(D1 * D2 * T);
|
|
226
|
-
let D;
|
|
227
|
-
if (isNegativeLE(T * zInv, P)) {
|
|
228
|
-
let _x = mod$1(Y * SQRT_M1);
|
|
229
|
-
let _y = mod$1(X * SQRT_M1);
|
|
230
|
-
X = _x;
|
|
231
|
-
Y = _y;
|
|
232
|
-
D = mod$1(D1 * INVSQRT_A_MINUS_D);
|
|
233
|
-
} else D = D2;
|
|
234
|
-
if (isNegativeLE(X * zInv, P)) Y = mod$1(-Y);
|
|
235
|
-
let s = mod$1((Z - Y) * D);
|
|
236
|
-
if (isNegativeLE(s, P)) s = mod$1(-s);
|
|
237
|
-
return Fp.toBytes(s);
|
|
238
|
-
}
|
|
239
|
-
/**
|
|
240
|
-
* Compares two Ristretto points.
|
|
241
|
-
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
|
|
242
|
-
*/
|
|
243
|
-
equals(other) {
|
|
244
|
-
this.assertSame(other);
|
|
245
|
-
const { X: X1, Y: Y1 } = this.ep;
|
|
246
|
-
const { X: X2, Y: Y2 } = other.ep;
|
|
247
|
-
const mod$1 = (n) => Fp.create(n);
|
|
248
|
-
const one = mod$1(X1 * Y2) === mod$1(Y1 * X2);
|
|
249
|
-
const two = mod$1(Y1 * Y2) === mod$1(X1 * X2);
|
|
250
|
-
return one || two;
|
|
251
|
-
}
|
|
252
|
-
is0() {
|
|
253
|
-
return this.equals(_RistrettoPoint.ZERO);
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
_RistrettoPoint.BASE = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.BASE))();
|
|
257
|
-
_RistrettoPoint.ZERO = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.ZERO))();
|
|
258
|
-
_RistrettoPoint.Fp = /* @__PURE__ */ (() => Fp)();
|
|
259
|
-
_RistrettoPoint.Fn = /* @__PURE__ */ (() => Fn)();
|
|
260
|
-
|
|
261
|
-
//#endregion
|
|
262
|
-
export { x25519 };
|
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import { abytes, abytes as abytes$1, anumber, bytesToHex, bytesToHex as bytesToHex$1, concatBytes as concatBytes$1, hexToBytes, isBytes, isBytes as isBytes$1, randomBytes } from "@noble/hashes/utils.js";
|
|
2
|
-
|
|
3
|
-
//#region node_modules/@noble/curves/esm/utils.js
|
|
4
|
-
/**
|
|
5
|
-
* Hex, bytes and number utilities.
|
|
6
|
-
* @module
|
|
7
|
-
*/
|
|
8
|
-
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
9
|
-
const _0n = /* @__PURE__ */ BigInt(0);
|
|
10
|
-
const _1n = /* @__PURE__ */ BigInt(1);
|
|
11
|
-
function _abool2(value, title = "") {
|
|
12
|
-
if (typeof value !== "boolean") {
|
|
13
|
-
const prefix = title && `"${title}"`;
|
|
14
|
-
throw new Error(prefix + "expected boolean, got type=" + typeof value);
|
|
15
|
-
}
|
|
16
|
-
return value;
|
|
17
|
-
}
|
|
18
|
-
/** Asserts something is Uint8Array. */
|
|
19
|
-
function _abytes2(value, length, title = "") {
|
|
20
|
-
const bytes = isBytes(value);
|
|
21
|
-
const len = value?.length;
|
|
22
|
-
const needsLen = length !== void 0;
|
|
23
|
-
if (!bytes || needsLen && len !== length) {
|
|
24
|
-
const prefix = title && `"${title}" `;
|
|
25
|
-
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
26
|
-
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
27
|
-
throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
|
|
28
|
-
}
|
|
29
|
-
return value;
|
|
30
|
-
}
|
|
31
|
-
function hexToNumber(hex) {
|
|
32
|
-
if (typeof hex !== "string") throw new Error("hex string expected, got " + typeof hex);
|
|
33
|
-
return hex === "" ? _0n : BigInt("0x" + hex);
|
|
34
|
-
}
|
|
35
|
-
function bytesToNumberBE(bytes) {
|
|
36
|
-
return hexToNumber(bytesToHex(bytes));
|
|
37
|
-
}
|
|
38
|
-
function bytesToNumberLE(bytes) {
|
|
39
|
-
abytes(bytes);
|
|
40
|
-
return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
|
|
41
|
-
}
|
|
42
|
-
function numberToBytesBE(n, len) {
|
|
43
|
-
return hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
44
|
-
}
|
|
45
|
-
function numberToBytesLE(n, len) {
|
|
46
|
-
return numberToBytesBE(n, len).reverse();
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Takes hex string or Uint8Array, converts to Uint8Array.
|
|
50
|
-
* Validates output length.
|
|
51
|
-
* Will throw error for other types.
|
|
52
|
-
* @param title descriptive title for an error e.g. 'secret key'
|
|
53
|
-
* @param hex hex string or Uint8Array
|
|
54
|
-
* @param expectedLength optional, will compare to result array's length
|
|
55
|
-
* @returns
|
|
56
|
-
*/
|
|
57
|
-
function ensureBytes(title, hex, expectedLength) {
|
|
58
|
-
let res;
|
|
59
|
-
if (typeof hex === "string") try {
|
|
60
|
-
res = hexToBytes(hex);
|
|
61
|
-
} catch (e) {
|
|
62
|
-
throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
|
|
63
|
-
}
|
|
64
|
-
else if (isBytes(hex)) res = Uint8Array.from(hex);
|
|
65
|
-
else throw new Error(title + " must be hex string or Uint8Array");
|
|
66
|
-
const len = res.length;
|
|
67
|
-
if (typeof expectedLength === "number" && len !== expectedLength) throw new Error(title + " of length " + expectedLength + " expected, got " + len);
|
|
68
|
-
return res;
|
|
69
|
-
}
|
|
70
|
-
function equalBytes(a, b) {
|
|
71
|
-
if (a.length !== b.length) return false;
|
|
72
|
-
let diff = 0;
|
|
73
|
-
for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];
|
|
74
|
-
return diff === 0;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Copies Uint8Array. We can't use u8a.slice(), because u8a can be Buffer,
|
|
78
|
-
* and Buffer#slice creates mutable copy. Never use Buffers!
|
|
79
|
-
*/
|
|
80
|
-
function copyBytes(bytes) {
|
|
81
|
-
return Uint8Array.from(bytes);
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
|
85
|
-
*/
|
|
86
|
-
/**
|
|
87
|
-
* Converts bytes to string using UTF8 encoding.
|
|
88
|
-
* @example bytesToUtf8(Uint8Array.from([97, 98, 99])) // 'abc'
|
|
89
|
-
*/
|
|
90
|
-
const isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
91
|
-
function inRange(n, min, max) {
|
|
92
|
-
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Asserts min <= n < max. NOTE: It's < max and not <= max.
|
|
96
|
-
* @example
|
|
97
|
-
* aInRange('x', x, 1n, 256n); // would assume x is in (1n..255n)
|
|
98
|
-
*/
|
|
99
|
-
function aInRange(title, n, min, max) {
|
|
100
|
-
if (!inRange(n, min, max)) throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Calculates amount of bits in a bigint.
|
|
104
|
-
* Same as `n.toString(2).length`
|
|
105
|
-
* TODO: merge with nLength in modular
|
|
106
|
-
*/
|
|
107
|
-
function bitLen(n) {
|
|
108
|
-
let len;
|
|
109
|
-
for (len = 0; n > _0n; n >>= _1n, len += 1);
|
|
110
|
-
return len;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Calculate mask for N bits. Not using ** operator with bigints because of old engines.
|
|
114
|
-
* Same as BigInt(`0b${Array(i).fill('1').join('')}`)
|
|
115
|
-
*/
|
|
116
|
-
const bitMask = (n) => (_1n << BigInt(n)) - _1n;
|
|
117
|
-
function _validateObject(object, fields, optFields = {}) {
|
|
118
|
-
if (!object || typeof object !== "object") throw new Error("expected valid options object");
|
|
119
|
-
function checkField(fieldName, expectedType, isOpt) {
|
|
120
|
-
const val = object[fieldName];
|
|
121
|
-
if (isOpt && val === void 0) return;
|
|
122
|
-
const current = typeof val;
|
|
123
|
-
if (current !== expectedType || val === null) throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
|
|
124
|
-
}
|
|
125
|
-
Object.entries(fields).forEach(([k, v]) => checkField(k, v, false));
|
|
126
|
-
Object.entries(optFields).forEach(([k, v]) => checkField(k, v, true));
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* throws not implemented error
|
|
130
|
-
*/
|
|
131
|
-
const notImplemented = () => {
|
|
132
|
-
throw new Error("not implemented");
|
|
133
|
-
};
|
|
134
|
-
/**
|
|
135
|
-
* Memoizes (caches) computation result.
|
|
136
|
-
* Uses WeakMap: the value is going auto-cleaned by GC after last reference is removed.
|
|
137
|
-
*/
|
|
138
|
-
function memoized(fn) {
|
|
139
|
-
const map = /* @__PURE__ */ new WeakMap();
|
|
140
|
-
return (arg, ...args) => {
|
|
141
|
-
const val = map.get(arg);
|
|
142
|
-
if (val !== void 0) return val;
|
|
143
|
-
const computed = fn(arg, ...args);
|
|
144
|
-
map.set(arg, computed);
|
|
145
|
-
return computed;
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
//#endregion
|
|
150
|
-
export { _abool2, _abytes2, _validateObject, aInRange, abytes$1 as abytes, anumber, bitLen, bitMask, bytesToHex$1 as bytesToHex, bytesToNumberBE, bytesToNumberLE, concatBytes$1 as concatBytes, copyBytes, ensureBytes, equalBytes, isBytes$1 as isBytes, memoized, notImplemented, numberToBytesBE, numberToBytesLE, randomBytes };
|