@solana/webcrypto-ed25519-polyfill 3.0.0 → 4.0.0-canary-20250903000115

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.
@@ -1,25 +1,43 @@
1
1
  'use strict';
2
2
 
3
- // ../../node_modules/.pnpm/@noble+ed25519@2.3.0/node_modules/@noble/ed25519/index.js
3
+ // ../../node_modules/.pnpm/@noble+ed25519@3.0.0/node_modules/@noble/ed25519/index.js
4
4
  var ed25519_CURVE = {
5
5
  p: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffedn,
6
6
  n: 0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3edn,
7
+ h: 8n,
7
8
  a: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffecn,
8
9
  d: 0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3n,
9
10
  Gx: 0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51an,
10
11
  Gy: 0x6666666666666666666666666666666666666666666666666666666666666658n
11
12
  };
12
- var { p: P, n: N, Gx, Gy, a: _a, d: _d } = ed25519_CURVE;
13
- var h = 8n;
13
+ var { p: P, n: N, Gx, Gy, a: _a, d: _d, h } = ed25519_CURVE;
14
14
  var L = 32;
15
15
  var L2 = 64;
16
- var err = (m = "") => {
17
- throw new Error(m);
16
+ var captureTrace = (...args) => {
17
+ if ("captureStackTrace" in Error && typeof Error.captureStackTrace === "function") {
18
+ Error.captureStackTrace(...args);
19
+ }
20
+ };
21
+ var err = (message = "") => {
22
+ const e = new Error(message);
23
+ captureTrace(e, err);
24
+ throw e;
18
25
  };
19
26
  var isBig = (n) => typeof n === "bigint";
20
27
  var isStr = (s) => typeof s === "string";
21
28
  var isBytes = (a) => a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
22
- var abytes = (a, l) => !isBytes(a) || typeof l === "number" && l > 0 && a.length !== l ? err("Uint8Array expected") : a;
29
+ var abytes = (value, length, title = "") => {
30
+ const bytes = isBytes(value);
31
+ const len = value?.length;
32
+ const needsLen = length !== void 0;
33
+ if (!bytes || needsLen && len !== length) {
34
+ const prefix = title && `"${title}" `;
35
+ const ofLen = needsLen ? ` of length ${length}` : "";
36
+ const got = bytes ? `length=${len}` : `type=${typeof value}`;
37
+ err(prefix + "expected Uint8Array" + ofLen + ", got " + got);
38
+ }
39
+ return value;
40
+ };
23
41
  var u8n = (len) => new Uint8Array(len);
24
42
  var u8fr = (buf) => Uint8Array.from(buf);
25
43
  var padh = (n, pad) => n.toString(16).padStart(pad, "0");
@@ -52,9 +70,8 @@ var hexToBytes = (hex) => {
52
70
  }
53
71
  return array;
54
72
  };
55
- var toU8 = (a, len) => abytes(isStr(a) ? hexToBytes(a) : u8fr(abytes(a)), len);
56
73
  var cr = () => globalThis?.crypto;
57
- var subtle = () => cr()?.subtle ?? err("crypto.subtle must be defined");
74
+ var subtle = () => cr()?.subtle ?? err("crypto.subtle must be defined, consider polyfill");
58
75
  var concatBytes = (...arrs) => {
59
76
  const r = u8n(arrs.reduce((sum, a) => sum + abytes(a).length, 0));
60
77
  let pad = 0;
@@ -69,7 +86,7 @@ var randomBytes = (len = L) => {
69
86
  return c.getRandomValues(u8n(len));
70
87
  };
71
88
  var big = BigInt;
72
- var arange = (n, min, max, msg = "bad number: out of range") => isBig(n) && min <= n && n < max ? n : err(msg);
89
+ var assertRange = (n, min, max, msg = "bad number: out of range") => isBig(n) && min <= n && n < max ? n : err(msg);
73
90
  var M = (a, b = P) => {
74
91
  const r = a % b;
75
92
  return r >= 0n ? r : b + r;
@@ -87,7 +104,7 @@ var invert = (num, md) => {
87
104
  return b === 1n ? M(x, md) : err("no inverse");
88
105
  };
89
106
  var callHash = (name) => {
90
- const fn = etc[name];
107
+ const fn = hashes[name];
91
108
  err("hashes." + name + " not set");
92
109
  return fn;
93
110
  };
@@ -96,18 +113,21 @@ var B256 = 2n ** 256n;
96
113
  var Point = class _Point {
97
114
  static BASE;
98
115
  static ZERO;
99
- ex;
100
- ey;
101
- ez;
102
- et;
103
- constructor(ex, ey, ez, et) {
116
+ X;
117
+ Y;
118
+ Z;
119
+ T;
120
+ constructor(X, Y, Z, T) {
104
121
  const max = B256;
105
- this.ex = arange(ex, 0n, max);
106
- this.ey = arange(ey, 0n, max);
107
- this.ez = arange(ez, 1n, max);
108
- this.et = arange(et, 0n, max);
122
+ this.X = assertRange(X, 0n, max);
123
+ this.Y = assertRange(Y, 0n, max);
124
+ this.Z = assertRange(Z, 1n, max);
125
+ this.T = assertRange(T, 0n, max);
109
126
  Object.freeze(this);
110
127
  }
128
+ static CURVE() {
129
+ return ed25519_CURVE;
130
+ }
111
131
  static fromAffine(p) {
112
132
  return new _Point(p.x, p.y, 1n, M(p.x * p.y));
113
133
  }
@@ -119,7 +139,7 @@ var Point = class _Point {
119
139
  normed[31] = lastByte & -129;
120
140
  const y = bytesToNumLE(normed);
121
141
  const max = zip215 ? B256 : P;
122
- arange(y, 0n, max);
142
+ assertRange(y, 0n, max);
123
143
  const y2 = M(y * y);
124
144
  const u = M(y2 - 1n);
125
145
  const v = M(d * y2 + 1n);
@@ -134,14 +154,23 @@ var Point = class _Point {
134
154
  x = M(-x);
135
155
  return new _Point(x, y, 1n, M(x * y));
136
156
  }
157
+ static fromHex(hex, zip215) {
158
+ return _Point.fromBytes(hexToBytes(hex), zip215);
159
+ }
160
+ get x() {
161
+ return this.toAffine().x;
162
+ }
163
+ get y() {
164
+ return this.toAffine().y;
165
+ }
137
166
  /** Checks if the point is valid and on-curve. */
138
167
  assertValidity() {
139
168
  const a = _a;
140
169
  const d = _d;
141
170
  const p = this;
142
171
  if (p.is0())
143
- throw new Error("bad point: ZERO");
144
- const { ex: X, ey: Y, ez: Z, et: T } = p;
172
+ return err("bad point: ZERO");
173
+ const { X, Y, Z, T } = p;
145
174
  const X2 = M(X * X);
146
175
  const Y2 = M(Y * Y);
147
176
  const Z2 = M(Z * Z);
@@ -150,17 +179,17 @@ var Point = class _Point {
150
179
  const left = M(Z2 * M(aX2 + Y2));
151
180
  const right = M(Z4 + M(d * M(X2 * Y2)));
152
181
  if (left !== right)
153
- throw new Error("bad point: equation left != right (1)");
182
+ return err("bad point: equation left != right (1)");
154
183
  const XY = M(X * Y);
155
184
  const ZT = M(Z * T);
156
185
  if (XY !== ZT)
157
- throw new Error("bad point: equation left != right (2)");
186
+ return err("bad point: equation left != right (2)");
158
187
  return this;
159
188
  }
160
189
  /** Equality check: compare points P&Q. */
161
190
  equals(other) {
162
- const { ex: X1, ey: Y1, ez: Z1 } = this;
163
- const { ex: X2, ey: Y2, ez: Z2 } = apoint(other);
191
+ const { X: X1, Y: Y1, Z: Z1 } = this;
192
+ const { X: X2, Y: Y2, Z: Z2 } = apoint(other);
164
193
  const X1Z2 = M(X1 * Z2);
165
194
  const X2Z1 = M(X2 * Z1);
166
195
  const Y1Z2 = M(Y1 * Z2);
@@ -172,11 +201,11 @@ var Point = class _Point {
172
201
  }
173
202
  /** Flip point over y coordinate. */
174
203
  negate() {
175
- return new _Point(M(-this.ex), this.ey, this.ez, M(-this.et));
204
+ return new _Point(M(-this.X), this.Y, this.Z, M(-this.T));
176
205
  }
177
206
  /** Point doubling. Complete formula. Cost: `4M + 4S + 1*a + 6add + 1*2`. */
178
207
  double() {
179
- const { ex: X1, ey: Y1, ez: Z1 } = this;
208
+ const { X: X1, Y: Y1, Z: Z1 } = this;
180
209
  const a = _a;
181
210
  const A = M(X1 * X1);
182
211
  const B = M(Y1 * Y1);
@@ -195,8 +224,8 @@ var Point = class _Point {
195
224
  }
196
225
  /** Point addition. Complete formula. Cost: `8M + 1*k + 8add + 1*2`. */
197
226
  add(other) {
198
- const { ex: X1, ey: Y1, ez: Z1, et: T1 } = this;
199
- const { ex: X2, ey: Y2, ez: Z2, et: T2 } = apoint(other);
227
+ const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
228
+ const { X: X2, Y: Y2, Z: Z2, T: T2 } = apoint(other);
200
229
  const a = _a;
201
230
  const d = _d;
202
231
  const A = M(X1 * X2);
@@ -213,6 +242,9 @@ var Point = class _Point {
213
242
  const Z3 = M(F * G2);
214
243
  return new _Point(X3, Y3, Z3, T3);
215
244
  }
245
+ subtract(other) {
246
+ return this.add(apoint(other).negate());
247
+ }
216
248
  /**
217
249
  * Point-by-scalar multiplication. Scalar must be in range 1 <= n < CURVE.n.
218
250
  * Uses {@link wNAF} for base point.
@@ -223,7 +255,7 @@ var Point = class _Point {
223
255
  multiply(n, safe = true) {
224
256
  if (!safe && (n === 0n || this.is0()))
225
257
  return I;
226
- arange(n, 1n, N);
258
+ assertRange(n, 1n, N);
227
259
  if (n === 1n)
228
260
  return this;
229
261
  if (this.equals(G))
@@ -238,15 +270,20 @@ var Point = class _Point {
238
270
  }
239
271
  return p;
240
272
  }
273
+ multiplyUnsafe(scalar) {
274
+ return this.multiply(scalar, false);
275
+ }
241
276
  /** Convert point to 2d xy affine point. (X, Y, Z) ∋ (x=X/Z, y=Y/Z) */
242
277
  toAffine() {
243
- const { ex: x, ey: y, ez: z } = this;
278
+ const { X, Y, Z } = this;
244
279
  if (this.equals(I))
245
280
  return { x: 0n, y: 1n };
246
- const iz = invert(z, P);
247
- if (M(z * iz) !== 1n)
281
+ const iz = invert(Z, P);
282
+ if (M(Z * iz) !== 1n)
248
283
  err("invalid inverse");
249
- return { x: M(x * iz), y: M(y * iz) };
284
+ const x = M(X * iz);
285
+ const y = M(Y * iz);
286
+ return { x, y };
250
287
  }
251
288
  toBytes() {
252
289
  const { x, y } = this.assertValidity().toAffine();
@@ -257,7 +294,6 @@ var Point = class _Point {
257
294
  toHex() {
258
295
  return bytesToHex(this.toBytes());
259
296
  }
260
- // encode to hex string
261
297
  clearCofactor() {
262
298
  return this.multiply(big(h), false);
263
299
  }
@@ -270,24 +306,12 @@ var Point = class _Point {
270
306
  p = p.add(this);
271
307
  return p.is0();
272
308
  }
273
- static fromHex(hex, zip215) {
274
- return _Point.fromBytes(toU8(hex), zip215);
275
- }
276
- get x() {
277
- return this.toAffine().x;
278
- }
279
- get y() {
280
- return this.toAffine().y;
281
- }
282
- toRawBytes() {
283
- return this.toBytes();
284
- }
285
309
  };
286
310
  var G = new Point(Gx, Gy, 1n, M(Gx * Gy));
287
311
  var I = new Point(0n, 1n, 1n, 0n);
288
312
  Point.BASE = G;
289
313
  Point.ZERO = I;
290
- var numTo32bLE = (num) => hexToBytes(padh(arange(num, 0n, B256), L2)).reverse();
314
+ var numTo32bLE = (num) => hexToBytes(padh(assertRange(num, 0n, B256), L2)).reverse();
291
315
  var bytesToNumLE = (b) => big("0x" + bytesToHex(u8fr(abytes(b)).reverse()));
292
316
  var pow2 = (x, power) => {
293
317
  let r = x;
@@ -333,8 +357,8 @@ var uvRatio = (u, v) => {
333
357
  return { isValid: useRoot1 || useRoot2, value: x };
334
358
  };
335
359
  var modL_LE = (hash) => modN(bytesToNumLE(hash));
336
- var sha512a = (...m) => etc.sha512Async(...m);
337
- var sha512s = (...m) => callHash("sha512Sync")(...m);
360
+ var sha512a = (...m) => hashes.sha512Async(concatBytes(...m));
361
+ var sha512s = (...m) => callHash("sha512")(concatBytes(...m));
338
362
  var hash2extK = (hashed) => {
339
363
  const head = hashed.slice(0, L);
340
364
  head[0] &= 248;
@@ -346,9 +370,9 @@ var hash2extK = (hashed) => {
346
370
  const pointBytes = point.toBytes();
347
371
  return { head, prefix, scalar, point, pointBytes };
348
372
  };
349
- var getExtendedPublicKeyAsync = (priv) => sha512a(toU8(priv, L)).then(hash2extK);
350
- var getExtendedPublicKey = (priv) => hash2extK(sha512s(toU8(priv, L)));
351
- var getPublicKeyAsync = (priv) => getExtendedPublicKeyAsync(priv).then((p) => p.pointBytes);
373
+ var getExtendedPublicKeyAsync = (secretKey) => sha512a(abytes(secretKey, L)).then(hash2extK);
374
+ var getExtendedPublicKey = (secretKey) => hash2extK(sha512s(abytes(secretKey, L)));
375
+ var getPublicKeyAsync = (secretKey) => getExtendedPublicKeyAsync(secretKey).then((p) => p.pointBytes);
352
376
  var hashFinishA = (res) => sha512a(res.hashable).then(res.finish);
353
377
  var _sign = (e, rBytes, msg) => {
354
378
  const { pointBytes: P2, scalar: s } = e;
@@ -361,17 +385,17 @@ var _sign = (e, rBytes, msg) => {
361
385
  };
362
386
  return { hashable, finish };
363
387
  };
364
- var signAsync = async (msg, privKey) => {
365
- const m = toU8(msg);
366
- const e = await getExtendedPublicKeyAsync(privKey);
388
+ var signAsync = async (message, secretKey) => {
389
+ const m = abytes(message);
390
+ const e = await getExtendedPublicKeyAsync(secretKey);
367
391
  const rBytes = await sha512a(e.prefix, m);
368
392
  return hashFinishA(_sign(e, rBytes, m));
369
393
  };
370
- var veriOpts = { zip215: true };
371
- var _verify = (sig, msg, pub, opts = veriOpts) => {
372
- sig = toU8(sig, L2);
373
- msg = toU8(msg);
374
- pub = toU8(pub, L);
394
+ var defaultVerifyOpts = { zip215: true };
395
+ var _verify = (sig, msg, pub, opts = defaultVerifyOpts) => {
396
+ sig = abytes(sig, L2);
397
+ msg = abytes(msg);
398
+ pub = abytes(pub, L);
375
399
  const { zip215 } = opts;
376
400
  let A;
377
401
  let R;
@@ -379,8 +403,8 @@ var _verify = (sig, msg, pub, opts = veriOpts) => {
379
403
  let SB;
380
404
  let hashable = Uint8Array.of();
381
405
  try {
382
- A = Point.fromHex(pub, zip215);
383
- R = Point.fromHex(sig.slice(0, L), zip215);
406
+ A = Point.fromBytes(pub, zip215);
407
+ R = Point.fromBytes(sig.slice(0, L), zip215);
384
408
  s = bytesToNumLE(sig.slice(L, L2));
385
409
  SB = G.multiply(s, false);
386
410
  hashable = concatBytes(R.toBytes(), A.toBytes(), msg);
@@ -397,30 +421,20 @@ var _verify = (sig, msg, pub, opts = veriOpts) => {
397
421
  };
398
422
  return { hashable, finish };
399
423
  };
400
- var verifyAsync = async (s, m, p, opts = veriOpts) => hashFinishA(_verify(s, m, p, opts));
401
- var etc = {
402
- sha512Async: async (...messages) => {
424
+ var verifyAsync = async (signature, message, publicKey, opts = defaultVerifyOpts) => hashFinishA(_verify(signature, message, publicKey, opts));
425
+ var hashes = {
426
+ sha512Async: async (message) => {
403
427
  const s = subtle();
404
- const m = concatBytes(...messages);
428
+ const m = concatBytes(message);
405
429
  return u8n(await s.digest("SHA-512", m.buffer));
406
430
  },
407
- sha512Sync: void 0,
408
- bytesToHex,
409
- hexToBytes,
410
- concatBytes,
411
- mod: M,
412
- invert,
413
- randomBytes
431
+ sha512: void 0
414
432
  };
433
+ var randomSecretKey = (seed = randomBytes(L)) => seed;
415
434
  var utils = {
416
435
  getExtendedPublicKeyAsync,
417
436
  getExtendedPublicKey,
418
- randomPrivateKey: () => randomBytes(L),
419
- precompute: (w = 8, p = G) => {
420
- p.multiply(3n);
421
- return p;
422
- }
423
- // no-op
437
+ randomSecretKey
424
438
  };
425
439
  var W = 8;
426
440
  var scalarBits = 256;
@@ -472,6 +486,8 @@ var wNAF = (n) => {
472
486
  p = p.add(ctneg(isNeg, comp[offP]));
473
487
  }
474
488
  }
489
+ if (n !== 0n)
490
+ err("invalid wnaf");
475
491
  return { p, f };
476
492
  };
477
493
 
@@ -603,14 +619,17 @@ async function exportKeyPolyfill(format, key) {
603
619
  throw new DOMException(`Unable to export a raw Ed25519 ${key.type} key`, "InvalidAccessError");
604
620
  }
605
621
  const publicKeyBytes = await getPublicKeyBytes(key);
606
- return publicKeyBytes;
622
+ return publicKeyBytes.buffer;
607
623
  }
608
624
  case "pkcs8": {
609
625
  if (key.type !== "private") {
610
626
  throw new DOMException(`Unable to export a pkcs8 Ed25519 ${key.type} key`, "InvalidAccessError");
611
627
  }
612
628
  const secretKeyBytes = getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key);
613
- return new Uint8Array([...ED25519_PKCS8_HEADER, ...secretKeyBytes]);
629
+ const result = new Uint8Array(ED25519_PKCS8_HEADER.length + secretKeyBytes.length);
630
+ result.set(ED25519_PKCS8_HEADER, 0);
631
+ result.set(secretKeyBytes, ED25519_PKCS8_HEADER.length);
632
+ return result.buffer;
614
633
  }
615
634
  case "jwk": {
616
635
  const publicKeyBytes = await getPublicKeyBytes(key);
@@ -635,7 +654,7 @@ async function exportKeyPolyfill(format, key) {
635
654
  }
636
655
  }
637
656
  function generateKeyPolyfill(extractable, keyUsages) {
638
- const privateKeyBytes = utils.randomPrivateKey();
657
+ const privateKeyBytes = utils.randomSecretKey();
639
658
  const keyPair = createKeyPairFromBytes(privateKeyBytes, extractable, keyUsages);
640
659
  return keyPair;
641
660
  }
@@ -649,7 +668,7 @@ async function signPolyfill(key, data) {
649
668
  const privateKeyBytes = getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key);
650
669
  const payload = bufferSourceToUint8Array(data);
651
670
  const signature = await signAsync(payload, privateKeyBytes);
652
- return signature;
671
+ return signature.buffer;
653
672
  }
654
673
  async function verifyPolyfill(key, signature, data) {
655
674
  if (key.type !== "public" || !key.usages.includes("verify")) {