@solana/webcrypto-ed25519-polyfill 2.0.0-experimental.e0b865d → 2.0.0-experimental.e4483d3
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/LICENSE +1 -1
- package/dist/index.browser.cjs +595 -22
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +174 -21
- package/dist/index.browser.js.map +1 -1
- package/dist/index.development.js +563 -1392
- package/dist/index.development.js.map +1 -1
- package/dist/index.native.js +174 -21
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +604 -19
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +179 -19
- package/dist/index.node.js.map +1 -1
- package/dist/index.production.min.js +3 -18
- package/dist/types/secrets.d.ts +5 -4
- package/dist/types/secrets.d.ts.map +1 -1
- package/package.json +10 -9
package/LICENSE
CHANGED
package/dist/index.browser.cjs
CHANGED
|
@@ -1,9 +1,424 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var ed25519 = require('@noble/curves/ed25519');
|
|
4
|
-
|
|
5
3
|
// ../build-scripts/env-shim.ts
|
|
6
4
|
var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")();
|
|
5
|
+
|
|
6
|
+
// ../../node_modules/.pnpm/@noble+ed25519@2.0.0/node_modules/@noble/ed25519/index.js
|
|
7
|
+
var P = 2n ** 255n - 19n;
|
|
8
|
+
var N = 2n ** 252n + 27742317777372353535851937790883648493n;
|
|
9
|
+
var Gx = 0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51an;
|
|
10
|
+
var Gy = 0x6666666666666666666666666666666666666666666666666666666666666658n;
|
|
11
|
+
var CURVE = {
|
|
12
|
+
a: -1n,
|
|
13
|
+
d: 37095705934669439343138083508754565189542113879843219016388785533085940283555n,
|
|
14
|
+
p: P,
|
|
15
|
+
n: N,
|
|
16
|
+
h: 8,
|
|
17
|
+
Gx,
|
|
18
|
+
Gy
|
|
19
|
+
// field prime, curve (group) order, cofactor
|
|
20
|
+
};
|
|
21
|
+
var err = (m = "") => {
|
|
22
|
+
throw new Error(m);
|
|
23
|
+
};
|
|
24
|
+
var str = (s) => typeof s === "string";
|
|
25
|
+
var au8 = (a, l) => (
|
|
26
|
+
// is Uint8Array (of specific length)
|
|
27
|
+
!(a instanceof Uint8Array) || typeof l === "number" && l > 0 && a.length !== l ? err("Uint8Array expected") : a
|
|
28
|
+
);
|
|
29
|
+
var u8n = (data) => new Uint8Array(data);
|
|
30
|
+
var toU8 = (a, len) => au8(str(a) ? h2b(a) : u8n(a), len);
|
|
31
|
+
var mod = (a, b = P) => {
|
|
32
|
+
let r = a % b;
|
|
33
|
+
return r >= 0n ? r : b + r;
|
|
34
|
+
};
|
|
35
|
+
var isPoint = (p) => p instanceof Point ? p : err("Point expected");
|
|
36
|
+
var Gpows = void 0;
|
|
37
|
+
var Point = class _Point {
|
|
38
|
+
constructor(ex, ey, ez, et) {
|
|
39
|
+
this.ex = ex;
|
|
40
|
+
this.ey = ey;
|
|
41
|
+
this.ez = ez;
|
|
42
|
+
this.et = et;
|
|
43
|
+
}
|
|
44
|
+
static fromAffine(p) {
|
|
45
|
+
return new _Point(p.x, p.y, 1n, mod(p.x * p.y));
|
|
46
|
+
}
|
|
47
|
+
static fromHex(hex, strict = true) {
|
|
48
|
+
const { d } = CURVE;
|
|
49
|
+
hex = toU8(hex, 32);
|
|
50
|
+
const normed = hex.slice();
|
|
51
|
+
normed[31] = hex[31] & ~128;
|
|
52
|
+
const y = b2n_LE(normed);
|
|
53
|
+
if (y === 0n) ; else {
|
|
54
|
+
if (strict && !(0n < y && y < P))
|
|
55
|
+
err("bad y coord 1");
|
|
56
|
+
if (!strict && !(0n < y && y < 2n ** 256n))
|
|
57
|
+
err("bad y coord 2");
|
|
58
|
+
}
|
|
59
|
+
const y2 = mod(y * y);
|
|
60
|
+
const u = mod(y2 - 1n);
|
|
61
|
+
const v = mod(d * y2 + 1n);
|
|
62
|
+
let { isValid, value: x } = uvRatio(u, v);
|
|
63
|
+
if (!isValid)
|
|
64
|
+
err("bad y coordinate 3");
|
|
65
|
+
const isXOdd = (x & 1n) === 1n;
|
|
66
|
+
const isHeadOdd = (hex[31] & 128) !== 0;
|
|
67
|
+
if (isHeadOdd !== isXOdd)
|
|
68
|
+
x = mod(-x);
|
|
69
|
+
return new _Point(x, y, 1n, mod(x * y));
|
|
70
|
+
}
|
|
71
|
+
get x() {
|
|
72
|
+
return this.toAffine().x;
|
|
73
|
+
}
|
|
74
|
+
// .x, .y will call expensive toAffine.
|
|
75
|
+
get y() {
|
|
76
|
+
return this.toAffine().y;
|
|
77
|
+
}
|
|
78
|
+
// Should be used with care.
|
|
79
|
+
equals(other) {
|
|
80
|
+
const { ex: X1, ey: Y1, ez: Z1 } = this;
|
|
81
|
+
const { ex: X2, ey: Y2, ez: Z2 } = isPoint(other);
|
|
82
|
+
const X1Z2 = mod(X1 * Z2), X2Z1 = mod(X2 * Z1);
|
|
83
|
+
const Y1Z2 = mod(Y1 * Z2), Y2Z1 = mod(Y2 * Z1);
|
|
84
|
+
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
85
|
+
}
|
|
86
|
+
is0() {
|
|
87
|
+
return this.equals(I);
|
|
88
|
+
}
|
|
89
|
+
negate() {
|
|
90
|
+
return new _Point(mod(-this.ex), this.ey, this.ez, mod(-this.et));
|
|
91
|
+
}
|
|
92
|
+
double() {
|
|
93
|
+
const { ex: X1, ey: Y1, ez: Z1 } = this;
|
|
94
|
+
const { a } = CURVE;
|
|
95
|
+
const A = mod(X1 * X1);
|
|
96
|
+
const B = mod(Y1 * Y1);
|
|
97
|
+
const C = mod(2n * mod(Z1 * Z1));
|
|
98
|
+
const D = mod(a * A);
|
|
99
|
+
const x1y1 = X1 + Y1;
|
|
100
|
+
const E = mod(mod(x1y1 * x1y1) - A - B);
|
|
101
|
+
const G2 = D + B;
|
|
102
|
+
const F = G2 - C;
|
|
103
|
+
const H = D - B;
|
|
104
|
+
const X3 = mod(E * F);
|
|
105
|
+
const Y3 = mod(G2 * H);
|
|
106
|
+
const T3 = mod(E * H);
|
|
107
|
+
const Z3 = mod(F * G2);
|
|
108
|
+
return new _Point(X3, Y3, Z3, T3);
|
|
109
|
+
}
|
|
110
|
+
add(other) {
|
|
111
|
+
const { ex: X1, ey: Y1, ez: Z1, et: T1 } = this;
|
|
112
|
+
const { ex: X2, ey: Y2, ez: Z2, et: T2 } = isPoint(other);
|
|
113
|
+
const { a, d } = CURVE;
|
|
114
|
+
const A = mod(X1 * X2);
|
|
115
|
+
const B = mod(Y1 * Y2);
|
|
116
|
+
const C = mod(T1 * d * T2);
|
|
117
|
+
const D = mod(Z1 * Z2);
|
|
118
|
+
const E = mod((X1 + Y1) * (X2 + Y2) - A - B);
|
|
119
|
+
const F = mod(D - C);
|
|
120
|
+
const G2 = mod(D + C);
|
|
121
|
+
const H = mod(B - a * A);
|
|
122
|
+
const X3 = mod(E * F);
|
|
123
|
+
const Y3 = mod(G2 * H);
|
|
124
|
+
const T3 = mod(E * H);
|
|
125
|
+
const Z3 = mod(F * G2);
|
|
126
|
+
return new _Point(X3, Y3, Z3, T3);
|
|
127
|
+
}
|
|
128
|
+
mul(n, safe = true) {
|
|
129
|
+
if (n === 0n)
|
|
130
|
+
return safe === true ? err("cannot multiply by 0") : I;
|
|
131
|
+
if (!(typeof n === "bigint" && 0n < n && n < N))
|
|
132
|
+
err("invalid scalar, must be < L");
|
|
133
|
+
if (!safe && this.is0() || n === 1n)
|
|
134
|
+
return this;
|
|
135
|
+
if (this.equals(G))
|
|
136
|
+
return wNAF(n).p;
|
|
137
|
+
let p = I, f = G;
|
|
138
|
+
for (let d = this; n > 0n; d = d.double(), n >>= 1n) {
|
|
139
|
+
if (n & 1n)
|
|
140
|
+
p = p.add(d);
|
|
141
|
+
else if (safe)
|
|
142
|
+
f = f.add(d);
|
|
143
|
+
}
|
|
144
|
+
return p;
|
|
145
|
+
}
|
|
146
|
+
multiply(scalar) {
|
|
147
|
+
return this.mul(scalar);
|
|
148
|
+
}
|
|
149
|
+
// Aliases for compatibilty
|
|
150
|
+
clearCofactor() {
|
|
151
|
+
return this.mul(BigInt(CURVE.h), false);
|
|
152
|
+
}
|
|
153
|
+
// multiply by cofactor
|
|
154
|
+
isSmallOrder() {
|
|
155
|
+
return this.clearCofactor().is0();
|
|
156
|
+
}
|
|
157
|
+
// check if P is small order
|
|
158
|
+
isTorsionFree() {
|
|
159
|
+
let p = this.mul(N / 2n, false).double();
|
|
160
|
+
if (N % 2n)
|
|
161
|
+
p = p.add(this);
|
|
162
|
+
return p.is0();
|
|
163
|
+
}
|
|
164
|
+
toAffine() {
|
|
165
|
+
const { ex: x, ey: y, ez: z } = this;
|
|
166
|
+
if (this.is0())
|
|
167
|
+
return { x: 0n, y: 0n };
|
|
168
|
+
const iz = invert(z);
|
|
169
|
+
if (mod(z * iz) !== 1n)
|
|
170
|
+
err("invalid inverse");
|
|
171
|
+
return { x: mod(x * iz), y: mod(y * iz) };
|
|
172
|
+
}
|
|
173
|
+
toRawBytes() {
|
|
174
|
+
const { x, y } = this.toAffine();
|
|
175
|
+
const b = n2b_32LE(y);
|
|
176
|
+
b[31] |= x & 1n ? 128 : 0;
|
|
177
|
+
return b;
|
|
178
|
+
}
|
|
179
|
+
toHex() {
|
|
180
|
+
return b2h(this.toRawBytes());
|
|
181
|
+
}
|
|
182
|
+
// encode to hex string
|
|
183
|
+
};
|
|
184
|
+
Point.BASE = new Point(Gx, Gy, 1n, mod(Gx * Gy));
|
|
185
|
+
Point.ZERO = new Point(0n, 1n, 1n, 0n);
|
|
186
|
+
var { BASE: G, ZERO: I } = Point;
|
|
187
|
+
var padh = (num, pad) => num.toString(16).padStart(pad, "0");
|
|
188
|
+
var b2h = (b) => Array.from(b).map((e) => padh(e, 2)).join("");
|
|
189
|
+
var h2b = (hex) => {
|
|
190
|
+
const l = hex.length;
|
|
191
|
+
if (!str(hex) || l % 2)
|
|
192
|
+
err("hex invalid 1");
|
|
193
|
+
const arr = u8n(l / 2);
|
|
194
|
+
for (let i = 0; i < arr.length; i++) {
|
|
195
|
+
const j = i * 2;
|
|
196
|
+
const h = hex.slice(j, j + 2);
|
|
197
|
+
const b = Number.parseInt(h, 16);
|
|
198
|
+
if (Number.isNaN(b) || b < 0)
|
|
199
|
+
err("hex invalid 2");
|
|
200
|
+
arr[i] = b;
|
|
201
|
+
}
|
|
202
|
+
return arr;
|
|
203
|
+
};
|
|
204
|
+
var n2b_32LE = (num) => h2b(padh(num, 32 * 2)).reverse();
|
|
205
|
+
var b2n_LE = (b) => BigInt("0x" + b2h(u8n(au8(b)).reverse()));
|
|
206
|
+
var concatB = (...arrs) => {
|
|
207
|
+
const r = u8n(arrs.reduce((sum, a) => sum + au8(a).length, 0));
|
|
208
|
+
let pad = 0;
|
|
209
|
+
arrs.forEach((a) => {
|
|
210
|
+
r.set(a, pad);
|
|
211
|
+
pad += a.length;
|
|
212
|
+
});
|
|
213
|
+
return r;
|
|
214
|
+
};
|
|
215
|
+
var invert = (num, md = P) => {
|
|
216
|
+
if (num === 0n || md <= 0n)
|
|
217
|
+
err("no inverse n=" + num + " mod=" + md);
|
|
218
|
+
let a = mod(num, md), b = md, x = 0n, u = 1n;
|
|
219
|
+
while (a !== 0n) {
|
|
220
|
+
const q = b / a, r = b % a;
|
|
221
|
+
const m = x - u * q;
|
|
222
|
+
b = a, a = r, x = u, u = m;
|
|
223
|
+
}
|
|
224
|
+
return b === 1n ? mod(x, md) : err("no inverse");
|
|
225
|
+
};
|
|
226
|
+
var pow2 = (x, power) => {
|
|
227
|
+
let r = x;
|
|
228
|
+
while (power-- > 0n) {
|
|
229
|
+
r *= r;
|
|
230
|
+
r %= P;
|
|
231
|
+
}
|
|
232
|
+
return r;
|
|
233
|
+
};
|
|
234
|
+
var pow_2_252_3 = (x) => {
|
|
235
|
+
const x2 = x * x % P;
|
|
236
|
+
const b2 = x2 * x % P;
|
|
237
|
+
const b4 = pow2(b2, 2n) * b2 % P;
|
|
238
|
+
const b5 = pow2(b4, 1n) * x % P;
|
|
239
|
+
const b10 = pow2(b5, 5n) * b5 % P;
|
|
240
|
+
const b20 = pow2(b10, 10n) * b10 % P;
|
|
241
|
+
const b40 = pow2(b20, 20n) * b20 % P;
|
|
242
|
+
const b80 = pow2(b40, 40n) * b40 % P;
|
|
243
|
+
const b160 = pow2(b80, 80n) * b80 % P;
|
|
244
|
+
const b240 = pow2(b160, 80n) * b80 % P;
|
|
245
|
+
const b250 = pow2(b240, 10n) * b10 % P;
|
|
246
|
+
const pow_p_5_8 = pow2(b250, 2n) * x % P;
|
|
247
|
+
return { pow_p_5_8, b2 };
|
|
248
|
+
};
|
|
249
|
+
var RM1 = 19681161376707505956807079304988542015446066515923890162744021073123829784752n;
|
|
250
|
+
var uvRatio = (u, v) => {
|
|
251
|
+
const v3 = mod(v * v * v);
|
|
252
|
+
const v7 = mod(v3 * v3 * v);
|
|
253
|
+
const pow = pow_2_252_3(u * v7).pow_p_5_8;
|
|
254
|
+
let x = mod(u * v3 * pow);
|
|
255
|
+
const vx2 = mod(v * x * x);
|
|
256
|
+
const root1 = x;
|
|
257
|
+
const root2 = mod(x * RM1);
|
|
258
|
+
const useRoot1 = vx2 === u;
|
|
259
|
+
const useRoot2 = vx2 === mod(-u);
|
|
260
|
+
const noRoot = vx2 === mod(-u * RM1);
|
|
261
|
+
if (useRoot1)
|
|
262
|
+
x = root1;
|
|
263
|
+
if (useRoot2 || noRoot)
|
|
264
|
+
x = root2;
|
|
265
|
+
if ((mod(x) & 1n) === 1n)
|
|
266
|
+
x = mod(-x);
|
|
267
|
+
return { isValid: useRoot1 || useRoot2, value: x };
|
|
268
|
+
};
|
|
269
|
+
var modL_LE = (hash) => mod(b2n_LE(hash), N);
|
|
270
|
+
var _shaS;
|
|
271
|
+
var sha512a = (...m) => etc.sha512Async(...m);
|
|
272
|
+
var sha512s = (...m) => (
|
|
273
|
+
// Sync SHA512, not set by default
|
|
274
|
+
typeof _shaS === "function" ? _shaS(...m) : err("etc.sha512Sync not set")
|
|
275
|
+
);
|
|
276
|
+
var hash2extK = (hashed) => {
|
|
277
|
+
const head = hashed.slice(0, 32);
|
|
278
|
+
head[0] &= 248;
|
|
279
|
+
head[31] &= 127;
|
|
280
|
+
head[31] |= 64;
|
|
281
|
+
const prefix = hashed.slice(32, 64);
|
|
282
|
+
const scalar = modL_LE(head);
|
|
283
|
+
const point = G.mul(scalar);
|
|
284
|
+
const pointBytes = point.toRawBytes();
|
|
285
|
+
return { head, prefix, scalar, point, pointBytes };
|
|
286
|
+
};
|
|
287
|
+
var getExtendedPublicKeyAsync = (priv) => sha512a(toU8(priv, 32)).then(hash2extK);
|
|
288
|
+
var getExtendedPublicKey = (priv) => hash2extK(sha512s(toU8(priv, 32)));
|
|
289
|
+
var getPublicKeyAsync = (priv) => getExtendedPublicKeyAsync(priv).then((p) => p.pointBytes);
|
|
290
|
+
function hashFinish(asynchronous, res) {
|
|
291
|
+
if (asynchronous)
|
|
292
|
+
return sha512a(res.hashable).then(res.finish);
|
|
293
|
+
return res.finish(sha512s(res.hashable));
|
|
294
|
+
}
|
|
295
|
+
var _sign = (e, rBytes, msg) => {
|
|
296
|
+
const { pointBytes: P2, scalar: s } = e;
|
|
297
|
+
const r = modL_LE(rBytes);
|
|
298
|
+
const R = G.mul(r).toRawBytes();
|
|
299
|
+
const hashable = concatB(R, P2, msg);
|
|
300
|
+
const finish = (hashed) => {
|
|
301
|
+
const S = mod(r + modL_LE(hashed) * s, N);
|
|
302
|
+
return au8(concatB(R, n2b_32LE(S)), 64);
|
|
303
|
+
};
|
|
304
|
+
return { hashable, finish };
|
|
305
|
+
};
|
|
306
|
+
var signAsync = async (msg, privKey) => {
|
|
307
|
+
const m = toU8(msg);
|
|
308
|
+
const e = await getExtendedPublicKeyAsync(privKey);
|
|
309
|
+
const rBytes = await sha512a(e.prefix, m);
|
|
310
|
+
return hashFinish(true, _sign(e, rBytes, m));
|
|
311
|
+
};
|
|
312
|
+
var _verify = (sig, msg, pub) => {
|
|
313
|
+
msg = toU8(msg);
|
|
314
|
+
sig = toU8(sig, 64);
|
|
315
|
+
const A = Point.fromHex(pub, false);
|
|
316
|
+
const R = Point.fromHex(sig.slice(0, 32), false);
|
|
317
|
+
const s = b2n_LE(sig.slice(32, 64));
|
|
318
|
+
const SB = G.mul(s, false);
|
|
319
|
+
const hashable = concatB(R.toRawBytes(), A.toRawBytes(), msg);
|
|
320
|
+
const finish = (hashed) => {
|
|
321
|
+
const k = modL_LE(hashed);
|
|
322
|
+
const RkA = R.add(A.mul(k, false));
|
|
323
|
+
return RkA.add(SB.negate()).clearCofactor().is0();
|
|
324
|
+
};
|
|
325
|
+
return { hashable, finish };
|
|
326
|
+
};
|
|
327
|
+
var verifyAsync = async (s, m, p) => hashFinish(true, _verify(s, m, p));
|
|
328
|
+
var cr = () => (
|
|
329
|
+
// We support: 1) browsers 2) node.js 19+
|
|
330
|
+
typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : void 0
|
|
331
|
+
);
|
|
332
|
+
var etc = {
|
|
333
|
+
bytesToHex: b2h,
|
|
334
|
+
hexToBytes: h2b,
|
|
335
|
+
concatBytes: concatB,
|
|
336
|
+
mod,
|
|
337
|
+
invert,
|
|
338
|
+
randomBytes: (len) => {
|
|
339
|
+
const crypto = cr();
|
|
340
|
+
if (!crypto)
|
|
341
|
+
err("crypto.getRandomValues must be defined");
|
|
342
|
+
return crypto.getRandomValues(u8n(len));
|
|
343
|
+
},
|
|
344
|
+
sha512Async: async (...messages) => {
|
|
345
|
+
const crypto = cr();
|
|
346
|
+
if (!crypto)
|
|
347
|
+
err("crypto.subtle or etc.sha512Async must be defined");
|
|
348
|
+
const m = concatB(...messages);
|
|
349
|
+
return u8n(await crypto.subtle.digest("SHA-512", m.buffer));
|
|
350
|
+
},
|
|
351
|
+
sha512Sync: void 0
|
|
352
|
+
// Actual logic below
|
|
353
|
+
};
|
|
354
|
+
Object.defineProperties(etc, { sha512Sync: {
|
|
355
|
+
configurable: false,
|
|
356
|
+
get() {
|
|
357
|
+
return _shaS;
|
|
358
|
+
},
|
|
359
|
+
set(f) {
|
|
360
|
+
if (!_shaS)
|
|
361
|
+
_shaS = f;
|
|
362
|
+
}
|
|
363
|
+
} });
|
|
364
|
+
var utils = {
|
|
365
|
+
getExtendedPublicKeyAsync,
|
|
366
|
+
getExtendedPublicKey,
|
|
367
|
+
randomPrivateKey: () => etc.randomBytes(32),
|
|
368
|
+
precompute(w = 8, p = G) {
|
|
369
|
+
p.multiply(3n);
|
|
370
|
+
return p;
|
|
371
|
+
}
|
|
372
|
+
// no-op
|
|
373
|
+
};
|
|
374
|
+
var W = 8;
|
|
375
|
+
var precompute = () => {
|
|
376
|
+
const points = [];
|
|
377
|
+
const windows = 256 / W + 1;
|
|
378
|
+
let p = G, b = p;
|
|
379
|
+
for (let w = 0; w < windows; w++) {
|
|
380
|
+
b = p;
|
|
381
|
+
points.push(b);
|
|
382
|
+
for (let i = 1; i < 2 ** (W - 1); i++) {
|
|
383
|
+
b = b.add(p);
|
|
384
|
+
points.push(b);
|
|
385
|
+
}
|
|
386
|
+
p = b.double();
|
|
387
|
+
}
|
|
388
|
+
return points;
|
|
389
|
+
};
|
|
390
|
+
var wNAF = (n) => {
|
|
391
|
+
const comp = Gpows || (Gpows = precompute());
|
|
392
|
+
const neg = (cnd, p2) => {
|
|
393
|
+
let n2 = p2.negate();
|
|
394
|
+
return cnd ? n2 : p2;
|
|
395
|
+
};
|
|
396
|
+
let p = I, f = G;
|
|
397
|
+
const windows = 1 + 256 / W;
|
|
398
|
+
const wsize = 2 ** (W - 1);
|
|
399
|
+
const mask = BigInt(2 ** W - 1);
|
|
400
|
+
const maxNum = 2 ** W;
|
|
401
|
+
const shiftBy = BigInt(W);
|
|
402
|
+
for (let w = 0; w < windows; w++) {
|
|
403
|
+
const off = w * wsize;
|
|
404
|
+
let wbits = Number(n & mask);
|
|
405
|
+
n >>= shiftBy;
|
|
406
|
+
if (wbits > wsize) {
|
|
407
|
+
wbits -= maxNum;
|
|
408
|
+
n += 1n;
|
|
409
|
+
}
|
|
410
|
+
const off1 = off, off2 = off + Math.abs(wbits) - 1;
|
|
411
|
+
const cnd1 = w % 2 !== 0, cnd2 = wbits < 0;
|
|
412
|
+
if (wbits === 0) {
|
|
413
|
+
f = f.add(neg(cnd1, comp[off1]));
|
|
414
|
+
} else {
|
|
415
|
+
p = p.add(neg(cnd2, comp[off2]));
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return { p, f };
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
// src/secrets.ts
|
|
7
422
|
var PROHIBITED_KEY_USAGES = /* @__PURE__ */ new Set([
|
|
8
423
|
"decrypt",
|
|
9
424
|
"deriveBits",
|
|
@@ -12,13 +427,60 @@ var PROHIBITED_KEY_USAGES = /* @__PURE__ */ new Set([
|
|
|
12
427
|
"unwrapKey",
|
|
13
428
|
"wrapKey"
|
|
14
429
|
]);
|
|
430
|
+
var ED25519_PKCS8_HEADER = (
|
|
431
|
+
// prettier-ignore
|
|
432
|
+
[
|
|
433
|
+
/**
|
|
434
|
+
* PKCS#8 header
|
|
435
|
+
*/
|
|
436
|
+
48,
|
|
437
|
+
// ASN.1 sequence tag
|
|
438
|
+
46,
|
|
439
|
+
// Length of sequence (46 more bytes)
|
|
440
|
+
2,
|
|
441
|
+
// ASN.1 integer tag
|
|
442
|
+
1,
|
|
443
|
+
// Length of integer
|
|
444
|
+
0,
|
|
445
|
+
// Version number
|
|
446
|
+
48,
|
|
447
|
+
// ASN.1 sequence tag
|
|
448
|
+
5,
|
|
449
|
+
// Length of sequence
|
|
450
|
+
6,
|
|
451
|
+
// ASN.1 object identifier tag
|
|
452
|
+
3,
|
|
453
|
+
// Length of object identifier
|
|
454
|
+
// Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
|
|
455
|
+
43,
|
|
456
|
+
// iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)
|
|
457
|
+
101,
|
|
458
|
+
// thawte(101)
|
|
459
|
+
// Ed25519 identifier
|
|
460
|
+
112,
|
|
461
|
+
// id-Ed25519(112)
|
|
462
|
+
/**
|
|
463
|
+
* Private key payload
|
|
464
|
+
*/
|
|
465
|
+
4,
|
|
466
|
+
// ASN.1 octet string tag
|
|
467
|
+
34,
|
|
468
|
+
// String length (34 more bytes)
|
|
469
|
+
// Private key bytes as octet string
|
|
470
|
+
4,
|
|
471
|
+
// ASN.1 octet string tag
|
|
472
|
+
32
|
|
473
|
+
// String length (32 bytes)
|
|
474
|
+
]
|
|
475
|
+
);
|
|
15
476
|
function bufferSourceToUint8Array(data) {
|
|
16
477
|
return data instanceof Uint8Array ? data : new Uint8Array(ArrayBuffer.isView(data) ? data.buffer : data);
|
|
17
478
|
}
|
|
18
479
|
var storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT;
|
|
480
|
+
var publicKeyBytesStore;
|
|
19
481
|
function createKeyPairFromBytes(bytes, extractable, keyUsages) {
|
|
20
482
|
const keyPair = createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(extractable, keyUsages);
|
|
21
|
-
const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT
|
|
483
|
+
const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT ||= /* @__PURE__ */ new WeakMap();
|
|
22
484
|
cache.set(keyPair.privateKey, bytes);
|
|
23
485
|
cache.set(keyPair.publicKey, bytes);
|
|
24
486
|
return keyPair;
|
|
@@ -58,60 +520,119 @@ function getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key) {
|
|
|
58
520
|
}
|
|
59
521
|
return secretKeyBytes;
|
|
60
522
|
}
|
|
61
|
-
function
|
|
523
|
+
async function getPublicKeyBytes(key) {
|
|
524
|
+
const publicKeyStore = publicKeyBytesStore ||= /* @__PURE__ */ new WeakMap();
|
|
525
|
+
const fromPublicStore = publicKeyStore.get(key);
|
|
526
|
+
if (fromPublicStore)
|
|
527
|
+
return fromPublicStore;
|
|
528
|
+
const publicKeyBytes = await getPublicKeyAsync(getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key));
|
|
529
|
+
publicKeyStore.set(key, publicKeyBytes);
|
|
530
|
+
return publicKeyBytes;
|
|
531
|
+
}
|
|
532
|
+
async function exportKeyPolyfill(format, key) {
|
|
533
|
+
if (key.extractable === false) {
|
|
534
|
+
throw new DOMException("key is not extractable", "InvalidAccessException");
|
|
535
|
+
}
|
|
62
536
|
switch (format) {
|
|
63
537
|
case "raw": {
|
|
64
|
-
if (key.extractable === false) {
|
|
65
|
-
throw new DOMException("key is not extractable", "InvalidAccessException");
|
|
66
|
-
}
|
|
67
538
|
if (key.type !== "public") {
|
|
68
539
|
throw new DOMException(`Unable to export a raw Ed25519 ${key.type} key`, "InvalidAccessError");
|
|
69
540
|
}
|
|
70
|
-
const publicKeyBytes =
|
|
541
|
+
const publicKeyBytes = await getPublicKeyBytes(key);
|
|
71
542
|
return publicKeyBytes;
|
|
72
543
|
}
|
|
544
|
+
case "pkcs8": {
|
|
545
|
+
if (key.type !== "private") {
|
|
546
|
+
throw new DOMException(`Unable to export a pkcs8 Ed25519 ${key.type} key`, "InvalidAccessError");
|
|
547
|
+
}
|
|
548
|
+
const secretKeyBytes = getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key);
|
|
549
|
+
return new Uint8Array([...ED25519_PKCS8_HEADER, ...secretKeyBytes]);
|
|
550
|
+
}
|
|
73
551
|
default:
|
|
74
552
|
throw new Error(`Exporting polyfilled Ed25519 keys in the "${format}" format is unimplemented`);
|
|
75
553
|
}
|
|
76
554
|
}
|
|
77
555
|
function generateKeyPolyfill(extractable, keyUsages) {
|
|
78
|
-
const privateKeyBytes =
|
|
556
|
+
const privateKeyBytes = utils.randomPrivateKey();
|
|
79
557
|
const keyPair = createKeyPairFromBytes(privateKeyBytes, extractable, keyUsages);
|
|
80
558
|
return keyPair;
|
|
81
559
|
}
|
|
82
560
|
function isPolyfilledKey(key) {
|
|
83
|
-
return !!storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT?.has(key);
|
|
561
|
+
return !!storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT?.has(key) || !!publicKeyBytesStore?.has(key);
|
|
84
562
|
}
|
|
85
|
-
function signPolyfill(key, data) {
|
|
563
|
+
async function signPolyfill(key, data) {
|
|
86
564
|
if (key.type !== "private" || !key.usages.includes("sign")) {
|
|
87
565
|
throw new DOMException("Unable to use this key to sign", "InvalidAccessError");
|
|
88
566
|
}
|
|
89
567
|
const privateKeyBytes = getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key);
|
|
90
568
|
const payload = bufferSourceToUint8Array(data);
|
|
91
|
-
const signature =
|
|
569
|
+
const signature = await signAsync(payload, privateKeyBytes);
|
|
92
570
|
return signature;
|
|
93
571
|
}
|
|
94
|
-
function verifyPolyfill(key, signature, data) {
|
|
572
|
+
async function verifyPolyfill(key, signature, data) {
|
|
95
573
|
if (key.type !== "public" || !key.usages.includes("verify")) {
|
|
96
574
|
throw new DOMException("Unable to use this key to verify", "InvalidAccessError");
|
|
97
575
|
}
|
|
98
|
-
const publicKeyBytes =
|
|
576
|
+
const publicKeyBytes = await getPublicKeyBytes(key);
|
|
99
577
|
try {
|
|
100
|
-
return
|
|
578
|
+
return await verifyAsync(bufferSourceToUint8Array(signature), bufferSourceToUint8Array(data), publicKeyBytes);
|
|
101
579
|
} catch {
|
|
102
580
|
return false;
|
|
103
581
|
}
|
|
104
582
|
}
|
|
105
|
-
|
|
106
|
-
|
|
583
|
+
function importKeyPolyfill(format, keyData, extractable, keyUsages) {
|
|
584
|
+
const bytes = bufferSourceToUint8Array(keyData);
|
|
585
|
+
if (format === "raw") {
|
|
586
|
+
if (keyUsages.some((usage) => usage === "sign" || PROHIBITED_KEY_USAGES.has(usage))) {
|
|
587
|
+
throw new DOMException("Unsupported key usage for a Ed25519 key", "SyntaxError");
|
|
588
|
+
}
|
|
589
|
+
if (bytes.length !== 32) {
|
|
590
|
+
throw new DOMException("Ed25519 raw keys must be exactly 32-bytes", "DataError");
|
|
591
|
+
}
|
|
592
|
+
const publicKey = {
|
|
593
|
+
[Symbol.toStringTag]: "CryptoKey",
|
|
594
|
+
algorithm: Object.freeze({ name: "Ed25519" }),
|
|
595
|
+
extractable,
|
|
596
|
+
type: "public",
|
|
597
|
+
usages: Object.freeze(keyUsages.filter((usage) => usage === "verify"))
|
|
598
|
+
};
|
|
599
|
+
const cache = publicKeyBytesStore ||= /* @__PURE__ */ new WeakMap();
|
|
600
|
+
cache.set(publicKey, bytes);
|
|
601
|
+
return publicKey;
|
|
602
|
+
}
|
|
603
|
+
if (format === "pkcs8") {
|
|
604
|
+
if (keyUsages.some((usage) => usage === "verify" || PROHIBITED_KEY_USAGES.has(usage))) {
|
|
605
|
+
throw new DOMException("Unsupported key usage for a Ed25519 key", "SyntaxError");
|
|
606
|
+
}
|
|
607
|
+
if (bytes.length !== 48) {
|
|
608
|
+
throw new DOMException("Invalid keyData", "DataError");
|
|
609
|
+
}
|
|
610
|
+
const header = bytes.slice(0, 16);
|
|
611
|
+
if (!header.every((val, i) => val === ED25519_PKCS8_HEADER[i])) {
|
|
612
|
+
throw new DOMException("Invalid keyData", "DataError");
|
|
613
|
+
}
|
|
614
|
+
const secretKeyBytes = bytes.slice(16);
|
|
615
|
+
const privateKey = {
|
|
616
|
+
[Symbol.toStringTag]: "CryptoKey",
|
|
617
|
+
algorithm: Object.freeze({ name: "Ed25519" }),
|
|
618
|
+
extractable,
|
|
619
|
+
type: "private",
|
|
620
|
+
usages: Object.freeze(keyUsages.filter((usage) => usage === "sign"))
|
|
621
|
+
};
|
|
622
|
+
const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT ||= /* @__PURE__ */ new WeakMap();
|
|
623
|
+
cache.set(privateKey, secretKeyBytes);
|
|
624
|
+
return privateKey;
|
|
625
|
+
}
|
|
626
|
+
throw new Error(`Importing Ed25519 keys in the "${format}" format is unimplemented`);
|
|
627
|
+
}
|
|
107
628
|
if (globalThis.isSecureContext) {
|
|
108
|
-
const originalCryptoObject = globalThis.crypto
|
|
109
|
-
const originalSubtleCrypto = originalCryptoObject.subtle
|
|
629
|
+
const originalCryptoObject = globalThis.crypto ||= {};
|
|
630
|
+
const originalSubtleCrypto = originalCryptoObject.subtle ||= {};
|
|
110
631
|
const originalExportKey = originalSubtleCrypto.exportKey;
|
|
111
632
|
originalSubtleCrypto.exportKey = async (...args) => {
|
|
112
633
|
const [_, key] = args;
|
|
113
634
|
if (isPolyfilledKey(key)) {
|
|
114
|
-
return exportKeyPolyfill(...args);
|
|
635
|
+
return await exportKeyPolyfill(...args);
|
|
115
636
|
} else if (originalExportKey) {
|
|
116
637
|
return await originalExportKey.apply(originalSubtleCrypto, args);
|
|
117
638
|
} else {
|
|
@@ -170,7 +691,7 @@ if (globalThis.isSecureContext) {
|
|
|
170
691
|
const [_, key] = args;
|
|
171
692
|
if (isPolyfilledKey(key)) {
|
|
172
693
|
const [_2, ...rest] = args;
|
|
173
|
-
return signPolyfill(...rest);
|
|
694
|
+
return await signPolyfill(...rest);
|
|
174
695
|
} else if (originalSign) {
|
|
175
696
|
return await originalSign.apply(originalSubtleCrypto, args);
|
|
176
697
|
} else {
|
|
@@ -182,13 +703,65 @@ if (globalThis.isSecureContext) {
|
|
|
182
703
|
const [_, key] = args;
|
|
183
704
|
if (isPolyfilledKey(key)) {
|
|
184
705
|
const [_2, ...rest] = args;
|
|
185
|
-
return verifyPolyfill(...rest);
|
|
706
|
+
return await verifyPolyfill(...rest);
|
|
186
707
|
} else if (originalVerify) {
|
|
187
708
|
return await originalVerify.apply(originalSubtleCrypto, args);
|
|
188
709
|
} else {
|
|
189
710
|
throw new TypeError("No native `verify` function exists to handle this call");
|
|
190
711
|
}
|
|
191
712
|
};
|
|
713
|
+
const originalImportKey = originalSubtleCrypto.importKey;
|
|
714
|
+
let originalImportKeySupportsEd25519;
|
|
715
|
+
originalSubtleCrypto.importKey = async (...args) => {
|
|
716
|
+
const [format, keyData, algorithm] = args;
|
|
717
|
+
if (algorithm !== "Ed25519") {
|
|
718
|
+
if (originalImportKey) {
|
|
719
|
+
return await originalImportKey.apply(originalSubtleCrypto, args);
|
|
720
|
+
} else {
|
|
721
|
+
throw new TypeError("No native `importKey` function exists to handle this call");
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
let optimisticallyImportedKey;
|
|
725
|
+
if (originalImportKeySupportsEd25519 === void 0) {
|
|
726
|
+
originalImportKeySupportsEd25519 = new Promise((resolve) => {
|
|
727
|
+
if (!originalImportKey) {
|
|
728
|
+
resolve(originalImportKeySupportsEd25519 = false);
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
originalImportKey.apply(originalSubtleCrypto, args).then((key) => {
|
|
732
|
+
if (__DEV__) {
|
|
733
|
+
console.warn(
|
|
734
|
+
"`@solana/webcrypto-ed25519-polyfill` was included in an environment that supports Ed25519 key manipulation natively. Falling back to the native implementation. Consider including this polyfill only in environments where Ed25519 is not supported."
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
if (originalSubtleCrypto.importKey !== originalImportKey) {
|
|
738
|
+
originalSubtleCrypto.importKey = originalImportKey;
|
|
739
|
+
}
|
|
740
|
+
optimisticallyImportedKey = key;
|
|
741
|
+
resolve(originalImportKeySupportsEd25519 = true);
|
|
742
|
+
}).catch(() => {
|
|
743
|
+
resolve(originalImportKeySupportsEd25519 = false);
|
|
744
|
+
});
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
if (typeof originalImportKey === "boolean" ? originalImportKeySupportsEd25519 : await originalImportKeySupportsEd25519) {
|
|
748
|
+
if (optimisticallyImportedKey) {
|
|
749
|
+
return optimisticallyImportedKey;
|
|
750
|
+
} else if (originalImportKey) {
|
|
751
|
+
return await originalImportKey.apply(originalSubtleCrypto, args);
|
|
752
|
+
} else {
|
|
753
|
+
throw new TypeError("No native `importKey` function exists to handle this call");
|
|
754
|
+
}
|
|
755
|
+
} else {
|
|
756
|
+
const [_format, _keyData, _algorithm, extractable, keyUsages] = args;
|
|
757
|
+
return importKeyPolyfill(format, keyData, extractable, keyUsages);
|
|
758
|
+
}
|
|
759
|
+
};
|
|
192
760
|
}
|
|
761
|
+
/*! Bundled license information:
|
|
762
|
+
|
|
763
|
+
@noble/ed25519/index.js:
|
|
764
|
+
(*! noble-ed25519 - MIT License (c) 2019 Paul Miller (paulmillr.com) *)
|
|
765
|
+
*/
|
|
193
766
|
//# sourceMappingURL=out.js.map
|
|
194
767
|
//# sourceMappingURL=index.browser.cjs.map
|