@solana/webcrypto-ed25519-polyfill 2.0.0-experimental.58b1699 → 2.0.0-experimental.59e5893
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/README.md +4 -2
- package/dist/index.browser.cjs +453 -28
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +32 -27
- package/dist/index.browser.js.map +1 -1
- package/dist/index.native.js +32 -27
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +462 -25
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +37 -25
- package/dist/index.node.js.map +1 -1
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/secrets.d.ts +4 -4
- package/dist/types/secrets.d.ts.map +1 -0
- package/package.json +9 -8
- package/dist/index.development.js +0 -1744
- package/dist/index.development.js.map +0 -1
- package/dist/index.production.min.js +0 -26
package/README.md
CHANGED
|
@@ -16,9 +16,11 @@
|
|
|
16
16
|
|
|
17
17
|
This package contains a polyfill that enables Ed25519 key manipulation in environments where it is not yet implemented. It does so by proxying calls to `SubtleCrypto` instance methods to an Ed25519 implementation in userspace.
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
> [!WARNING]
|
|
20
|
+
> Because this package's implementation of Ed25519 key generation exists in userspace, it can't guarantee that the keys you generate with it are non-exportable. Untrusted code running in your JavaScript context may still be able to gain access to and/or exfiltrate secret key material.
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
> [!NOTE]
|
|
23
|
+
> Native `CryptoKeys` can be stored in IndexedDB but the keys created by this polyfill can not. This is because, unlike native `CryptoKeys`, our polyfilled key objects can not implement the [structured clone algorithm](https://www.w3.org/TR/WebCryptoAPI/#cryptokey-interface-clone).
|
|
22
24
|
|
|
23
25
|
## Usage
|
|
24
26
|
|
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",
|
|
@@ -14,7 +429,7 @@ var PROHIBITED_KEY_USAGES = /* @__PURE__ */ new Set([
|
|
|
14
429
|
]);
|
|
15
430
|
var ED25519_PKCS8_HEADER = (
|
|
16
431
|
// prettier-ignore
|
|
17
|
-
|
|
432
|
+
[
|
|
18
433
|
/**
|
|
19
434
|
* PKCS#8 header
|
|
20
435
|
*/
|
|
@@ -56,7 +471,7 @@ var ED25519_PKCS8_HEADER = (
|
|
|
56
471
|
// ASN.1 octet string tag
|
|
57
472
|
32
|
|
58
473
|
// String length (32 bytes)
|
|
59
|
-
]
|
|
474
|
+
]
|
|
60
475
|
);
|
|
61
476
|
function bufferSourceToUint8Array(data) {
|
|
62
477
|
return data instanceof Uint8Array ? data : new Uint8Array(ArrayBuffer.isView(data) ? data.buffer : data);
|
|
@@ -65,7 +480,7 @@ var storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT;
|
|
|
65
480
|
var publicKeyBytesStore;
|
|
66
481
|
function createKeyPairFromBytes(bytes, extractable, keyUsages) {
|
|
67
482
|
const keyPair = createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(extractable, keyUsages);
|
|
68
|
-
const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT
|
|
483
|
+
const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT ||= /* @__PURE__ */ new WeakMap();
|
|
69
484
|
cache.set(keyPair.privateKey, bytes);
|
|
70
485
|
cache.set(keyPair.publicKey, bytes);
|
|
71
486
|
return keyPair;
|
|
@@ -105,55 +520,62 @@ function getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key) {
|
|
|
105
520
|
}
|
|
106
521
|
return secretKeyBytes;
|
|
107
522
|
}
|
|
108
|
-
function getPublicKeyBytes(key) {
|
|
109
|
-
const publicKeyStore = publicKeyBytesStore
|
|
523
|
+
async function getPublicKeyBytes(key) {
|
|
524
|
+
const publicKeyStore = publicKeyBytesStore ||= /* @__PURE__ */ new WeakMap();
|
|
110
525
|
const fromPublicStore = publicKeyStore.get(key);
|
|
111
526
|
if (fromPublicStore)
|
|
112
527
|
return fromPublicStore;
|
|
113
|
-
const publicKeyBytes =
|
|
528
|
+
const publicKeyBytes = await getPublicKeyAsync(getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key));
|
|
114
529
|
publicKeyStore.set(key, publicKeyBytes);
|
|
115
530
|
return publicKeyBytes;
|
|
116
531
|
}
|
|
117
|
-
function exportKeyPolyfill(format, key) {
|
|
532
|
+
async function exportKeyPolyfill(format, key) {
|
|
533
|
+
if (key.extractable === false) {
|
|
534
|
+
throw new DOMException("key is not extractable", "InvalidAccessException");
|
|
535
|
+
}
|
|
118
536
|
switch (format) {
|
|
119
537
|
case "raw": {
|
|
120
|
-
if (key.extractable === false) {
|
|
121
|
-
throw new DOMException("key is not extractable", "InvalidAccessException");
|
|
122
|
-
}
|
|
123
538
|
if (key.type !== "public") {
|
|
124
539
|
throw new DOMException(`Unable to export a raw Ed25519 ${key.type} key`, "InvalidAccessError");
|
|
125
540
|
}
|
|
126
|
-
const publicKeyBytes = getPublicKeyBytes(key);
|
|
541
|
+
const publicKeyBytes = await getPublicKeyBytes(key);
|
|
127
542
|
return publicKeyBytes;
|
|
128
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
|
+
}
|
|
129
551
|
default:
|
|
130
552
|
throw new Error(`Exporting polyfilled Ed25519 keys in the "${format}" format is unimplemented`);
|
|
131
553
|
}
|
|
132
554
|
}
|
|
133
555
|
function generateKeyPolyfill(extractable, keyUsages) {
|
|
134
|
-
const privateKeyBytes =
|
|
556
|
+
const privateKeyBytes = utils.randomPrivateKey();
|
|
135
557
|
const keyPair = createKeyPairFromBytes(privateKeyBytes, extractable, keyUsages);
|
|
136
558
|
return keyPair;
|
|
137
559
|
}
|
|
138
560
|
function isPolyfilledKey(key) {
|
|
139
561
|
return !!storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT?.has(key) || !!publicKeyBytesStore?.has(key);
|
|
140
562
|
}
|
|
141
|
-
function signPolyfill(key, data) {
|
|
563
|
+
async function signPolyfill(key, data) {
|
|
142
564
|
if (key.type !== "private" || !key.usages.includes("sign")) {
|
|
143
565
|
throw new DOMException("Unable to use this key to sign", "InvalidAccessError");
|
|
144
566
|
}
|
|
145
567
|
const privateKeyBytes = getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key);
|
|
146
568
|
const payload = bufferSourceToUint8Array(data);
|
|
147
|
-
const signature =
|
|
569
|
+
const signature = await signAsync(payload, privateKeyBytes);
|
|
148
570
|
return signature;
|
|
149
571
|
}
|
|
150
|
-
function verifyPolyfill(key, signature, data) {
|
|
572
|
+
async function verifyPolyfill(key, signature, data) {
|
|
151
573
|
if (key.type !== "public" || !key.usages.includes("verify")) {
|
|
152
574
|
throw new DOMException("Unable to use this key to verify", "InvalidAccessError");
|
|
153
575
|
}
|
|
154
|
-
const publicKeyBytes = getPublicKeyBytes(key);
|
|
576
|
+
const publicKeyBytes = await getPublicKeyBytes(key);
|
|
155
577
|
try {
|
|
156
|
-
return
|
|
578
|
+
return await verifyAsync(bufferSourceToUint8Array(signature), bufferSourceToUint8Array(data), publicKeyBytes);
|
|
157
579
|
} catch {
|
|
158
580
|
return false;
|
|
159
581
|
}
|
|
@@ -174,7 +596,7 @@ function importKeyPolyfill(format, keyData, extractable, keyUsages) {
|
|
|
174
596
|
type: "public",
|
|
175
597
|
usages: Object.freeze(keyUsages.filter((usage) => usage === "verify"))
|
|
176
598
|
};
|
|
177
|
-
const cache = publicKeyBytesStore
|
|
599
|
+
const cache = publicKeyBytesStore ||= /* @__PURE__ */ new WeakMap();
|
|
178
600
|
cache.set(publicKey, bytes);
|
|
179
601
|
return publicKey;
|
|
180
602
|
}
|
|
@@ -197,22 +619,20 @@ function importKeyPolyfill(format, keyData, extractable, keyUsages) {
|
|
|
197
619
|
type: "private",
|
|
198
620
|
usages: Object.freeze(keyUsages.filter((usage) => usage === "sign"))
|
|
199
621
|
};
|
|
200
|
-
const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT
|
|
622
|
+
const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT ||= /* @__PURE__ */ new WeakMap();
|
|
201
623
|
cache.set(privateKey, secretKeyBytes);
|
|
202
624
|
return privateKey;
|
|
203
625
|
}
|
|
204
626
|
throw new Error(`Importing Ed25519 keys in the "${format}" format is unimplemented`);
|
|
205
627
|
}
|
|
206
|
-
|
|
207
|
-
// src/index.ts
|
|
208
628
|
if (globalThis.isSecureContext) {
|
|
209
|
-
const originalCryptoObject = globalThis.crypto
|
|
210
|
-
const originalSubtleCrypto = originalCryptoObject.subtle
|
|
629
|
+
const originalCryptoObject = globalThis.crypto ||= {};
|
|
630
|
+
const originalSubtleCrypto = originalCryptoObject.subtle ||= {};
|
|
211
631
|
const originalExportKey = originalSubtleCrypto.exportKey;
|
|
212
632
|
originalSubtleCrypto.exportKey = async (...args) => {
|
|
213
633
|
const [_, key] = args;
|
|
214
634
|
if (isPolyfilledKey(key)) {
|
|
215
|
-
return exportKeyPolyfill(...args);
|
|
635
|
+
return await exportKeyPolyfill(...args);
|
|
216
636
|
} else if (originalExportKey) {
|
|
217
637
|
return await originalExportKey.apply(originalSubtleCrypto, args);
|
|
218
638
|
} else {
|
|
@@ -271,7 +691,7 @@ if (globalThis.isSecureContext) {
|
|
|
271
691
|
const [_, key] = args;
|
|
272
692
|
if (isPolyfilledKey(key)) {
|
|
273
693
|
const [_2, ...rest] = args;
|
|
274
|
-
return signPolyfill(...rest);
|
|
694
|
+
return await signPolyfill(...rest);
|
|
275
695
|
} else if (originalSign) {
|
|
276
696
|
return await originalSign.apply(originalSubtleCrypto, args);
|
|
277
697
|
} else {
|
|
@@ -283,7 +703,7 @@ if (globalThis.isSecureContext) {
|
|
|
283
703
|
const [_, key] = args;
|
|
284
704
|
if (isPolyfilledKey(key)) {
|
|
285
705
|
const [_2, ...rest] = args;
|
|
286
|
-
return verifyPolyfill(...rest);
|
|
706
|
+
return await verifyPolyfill(...rest);
|
|
287
707
|
} else if (originalVerify) {
|
|
288
708
|
return await originalVerify.apply(originalSubtleCrypto, args);
|
|
289
709
|
} else {
|
|
@@ -338,5 +758,10 @@ if (globalThis.isSecureContext) {
|
|
|
338
758
|
}
|
|
339
759
|
};
|
|
340
760
|
}
|
|
761
|
+
/*! Bundled license information:
|
|
762
|
+
|
|
763
|
+
@noble/ed25519/index.js:
|
|
764
|
+
(*! noble-ed25519 - MIT License (c) 2019 Paul Miller (paulmillr.com) *)
|
|
765
|
+
*/
|
|
341
766
|
//# sourceMappingURL=out.js.map
|
|
342
767
|
//# sourceMappingURL=index.browser.cjs.map
|