@solana/webcrypto-ed25519-polyfill 3.0.1 → 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.
@@ -9,26 +9,44 @@ var o__default = /*#__PURE__*/_interopDefault(o);
9
9
  // ../crypto-impl/dist/index.node.mjs
10
10
  var t = o__default.default;
11
11
 
12
- // ../../node_modules/.pnpm/@noble+ed25519@2.3.0/node_modules/@noble/ed25519/index.js
12
+ // ../../node_modules/.pnpm/@noble+ed25519@3.0.0/node_modules/@noble/ed25519/index.js
13
13
  var ed25519_CURVE = {
14
14
  p: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffedn,
15
15
  n: 0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3edn,
16
+ h: 8n,
16
17
  a: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffecn,
17
18
  d: 0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3n,
18
19
  Gx: 0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51an,
19
20
  Gy: 0x6666666666666666666666666666666666666666666666666666666666666658n
20
21
  };
21
- var { p: P, n: N, Gx, Gy, a: _a, d: _d } = ed25519_CURVE;
22
- var h = 8n;
22
+ var { p: P, n: N, Gx, Gy, a: _a, d: _d, h } = ed25519_CURVE;
23
23
  var L = 32;
24
24
  var L2 = 64;
25
- var err = (m = "") => {
26
- throw new Error(m);
25
+ var captureTrace = (...args) => {
26
+ if ("captureStackTrace" in Error && typeof Error.captureStackTrace === "function") {
27
+ Error.captureStackTrace(...args);
28
+ }
29
+ };
30
+ var err = (message = "") => {
31
+ const e = new Error(message);
32
+ captureTrace(e, err);
33
+ throw e;
27
34
  };
28
35
  var isBig = (n) => typeof n === "bigint";
29
36
  var isStr = (s) => typeof s === "string";
30
37
  var isBytes = (a) => a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
31
- var abytes = (a, l) => !isBytes(a) || typeof l === "number" && l > 0 && a.length !== l ? err("Uint8Array expected") : a;
38
+ var abytes = (value, length, title = "") => {
39
+ const bytes = isBytes(value);
40
+ const len = value?.length;
41
+ const needsLen = length !== void 0;
42
+ if (!bytes || needsLen && len !== length) {
43
+ const prefix = title && `"${title}" `;
44
+ const ofLen = needsLen ? ` of length ${length}` : "";
45
+ const got = bytes ? `length=${len}` : `type=${typeof value}`;
46
+ err(prefix + "expected Uint8Array" + ofLen + ", got " + got);
47
+ }
48
+ return value;
49
+ };
32
50
  var u8n = (len) => new Uint8Array(len);
33
51
  var u8fr = (buf) => Uint8Array.from(buf);
34
52
  var padh = (n, pad) => n.toString(16).padStart(pad, "0");
@@ -61,9 +79,8 @@ var hexToBytes = (hex) => {
61
79
  }
62
80
  return array;
63
81
  };
64
- var toU8 = (a, len) => abytes(isStr(a) ? hexToBytes(a) : u8fr(abytes(a)), len);
65
82
  var cr = () => globalThis?.crypto;
66
- var subtle = () => cr()?.subtle ?? err("crypto.subtle must be defined");
83
+ var subtle = () => cr()?.subtle ?? err("crypto.subtle must be defined, consider polyfill");
67
84
  var concatBytes = (...arrs) => {
68
85
  const r = u8n(arrs.reduce((sum, a) => sum + abytes(a).length, 0));
69
86
  let pad = 0;
@@ -78,7 +95,7 @@ var randomBytes = (len = L) => {
78
95
  return c.getRandomValues(u8n(len));
79
96
  };
80
97
  var big = BigInt;
81
- var arange = (n, min, max, msg = "bad number: out of range") => isBig(n) && min <= n && n < max ? n : err(msg);
98
+ var assertRange = (n, min, max, msg = "bad number: out of range") => isBig(n) && min <= n && n < max ? n : err(msg);
82
99
  var M = (a, b = P) => {
83
100
  const r = a % b;
84
101
  return r >= 0n ? r : b + r;
@@ -96,7 +113,7 @@ var invert = (num, md) => {
96
113
  return b === 1n ? M(x, md) : err("no inverse");
97
114
  };
98
115
  var callHash = (name) => {
99
- const fn = etc[name];
116
+ const fn = hashes[name];
100
117
  err("hashes." + name + " not set");
101
118
  return fn;
102
119
  };
@@ -105,18 +122,21 @@ var B256 = 2n ** 256n;
105
122
  var Point = class _Point {
106
123
  static BASE;
107
124
  static ZERO;
108
- ex;
109
- ey;
110
- ez;
111
- et;
112
- constructor(ex, ey, ez, et) {
125
+ X;
126
+ Y;
127
+ Z;
128
+ T;
129
+ constructor(X, Y, Z, T) {
113
130
  const max = B256;
114
- this.ex = arange(ex, 0n, max);
115
- this.ey = arange(ey, 0n, max);
116
- this.ez = arange(ez, 1n, max);
117
- this.et = arange(et, 0n, max);
131
+ this.X = assertRange(X, 0n, max);
132
+ this.Y = assertRange(Y, 0n, max);
133
+ this.Z = assertRange(Z, 1n, max);
134
+ this.T = assertRange(T, 0n, max);
118
135
  Object.freeze(this);
119
136
  }
137
+ static CURVE() {
138
+ return ed25519_CURVE;
139
+ }
120
140
  static fromAffine(p) {
121
141
  return new _Point(p.x, p.y, 1n, M(p.x * p.y));
122
142
  }
@@ -128,7 +148,7 @@ var Point = class _Point {
128
148
  normed[31] = lastByte & -129;
129
149
  const y = bytesToNumLE(normed);
130
150
  const max = zip215 ? B256 : P;
131
- arange(y, 0n, max);
151
+ assertRange(y, 0n, max);
132
152
  const y2 = M(y * y);
133
153
  const u = M(y2 - 1n);
134
154
  const v = M(d * y2 + 1n);
@@ -143,14 +163,23 @@ var Point = class _Point {
143
163
  x = M(-x);
144
164
  return new _Point(x, y, 1n, M(x * y));
145
165
  }
166
+ static fromHex(hex, zip215) {
167
+ return _Point.fromBytes(hexToBytes(hex), zip215);
168
+ }
169
+ get x() {
170
+ return this.toAffine().x;
171
+ }
172
+ get y() {
173
+ return this.toAffine().y;
174
+ }
146
175
  /** Checks if the point is valid and on-curve. */
147
176
  assertValidity() {
148
177
  const a = _a;
149
178
  const d = _d;
150
179
  const p = this;
151
180
  if (p.is0())
152
- throw new Error("bad point: ZERO");
153
- const { ex: X, ey: Y, ez: Z, et: T } = p;
181
+ return err("bad point: ZERO");
182
+ const { X, Y, Z, T } = p;
154
183
  const X2 = M(X * X);
155
184
  const Y2 = M(Y * Y);
156
185
  const Z2 = M(Z * Z);
@@ -159,17 +188,17 @@ var Point = class _Point {
159
188
  const left = M(Z2 * M(aX2 + Y2));
160
189
  const right = M(Z4 + M(d * M(X2 * Y2)));
161
190
  if (left !== right)
162
- throw new Error("bad point: equation left != right (1)");
191
+ return err("bad point: equation left != right (1)");
163
192
  const XY = M(X * Y);
164
193
  const ZT = M(Z * T);
165
194
  if (XY !== ZT)
166
- throw new Error("bad point: equation left != right (2)");
195
+ return err("bad point: equation left != right (2)");
167
196
  return this;
168
197
  }
169
198
  /** Equality check: compare points P&Q. */
170
199
  equals(other) {
171
- const { ex: X1, ey: Y1, ez: Z1 } = this;
172
- const { ex: X2, ey: Y2, ez: Z2 } = apoint(other);
200
+ const { X: X1, Y: Y1, Z: Z1 } = this;
201
+ const { X: X2, Y: Y2, Z: Z2 } = apoint(other);
173
202
  const X1Z2 = M(X1 * Z2);
174
203
  const X2Z1 = M(X2 * Z1);
175
204
  const Y1Z2 = M(Y1 * Z2);
@@ -181,11 +210,11 @@ var Point = class _Point {
181
210
  }
182
211
  /** Flip point over y coordinate. */
183
212
  negate() {
184
- return new _Point(M(-this.ex), this.ey, this.ez, M(-this.et));
213
+ return new _Point(M(-this.X), this.Y, this.Z, M(-this.T));
185
214
  }
186
215
  /** Point doubling. Complete formula. Cost: `4M + 4S + 1*a + 6add + 1*2`. */
187
216
  double() {
188
- const { ex: X1, ey: Y1, ez: Z1 } = this;
217
+ const { X: X1, Y: Y1, Z: Z1 } = this;
189
218
  const a = _a;
190
219
  const A = M(X1 * X1);
191
220
  const B = M(Y1 * Y1);
@@ -204,8 +233,8 @@ var Point = class _Point {
204
233
  }
205
234
  /** Point addition. Complete formula. Cost: `8M + 1*k + 8add + 1*2`. */
206
235
  add(other) {
207
- const { ex: X1, ey: Y1, ez: Z1, et: T1 } = this;
208
- const { ex: X2, ey: Y2, ez: Z2, et: T2 } = apoint(other);
236
+ const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
237
+ const { X: X2, Y: Y2, Z: Z2, T: T2 } = apoint(other);
209
238
  const a = _a;
210
239
  const d = _d;
211
240
  const A = M(X1 * X2);
@@ -222,6 +251,9 @@ var Point = class _Point {
222
251
  const Z3 = M(F * G2);
223
252
  return new _Point(X3, Y3, Z3, T3);
224
253
  }
254
+ subtract(other) {
255
+ return this.add(apoint(other).negate());
256
+ }
225
257
  /**
226
258
  * Point-by-scalar multiplication. Scalar must be in range 1 <= n < CURVE.n.
227
259
  * Uses {@link wNAF} for base point.
@@ -232,7 +264,7 @@ var Point = class _Point {
232
264
  multiply(n, safe = true) {
233
265
  if (!safe && (n === 0n || this.is0()))
234
266
  return I;
235
- arange(n, 1n, N);
267
+ assertRange(n, 1n, N);
236
268
  if (n === 1n)
237
269
  return this;
238
270
  if (this.equals(G))
@@ -247,15 +279,20 @@ var Point = class _Point {
247
279
  }
248
280
  return p;
249
281
  }
282
+ multiplyUnsafe(scalar) {
283
+ return this.multiply(scalar, false);
284
+ }
250
285
  /** Convert point to 2d xy affine point. (X, Y, Z) ∋ (x=X/Z, y=Y/Z) */
251
286
  toAffine() {
252
- const { ex: x, ey: y, ez: z } = this;
287
+ const { X, Y, Z } = this;
253
288
  if (this.equals(I))
254
289
  return { x: 0n, y: 1n };
255
- const iz = invert(z, P);
256
- if (M(z * iz) !== 1n)
290
+ const iz = invert(Z, P);
291
+ if (M(Z * iz) !== 1n)
257
292
  err("invalid inverse");
258
- return { x: M(x * iz), y: M(y * iz) };
293
+ const x = M(X * iz);
294
+ const y = M(Y * iz);
295
+ return { x, y };
259
296
  }
260
297
  toBytes() {
261
298
  const { x, y } = this.assertValidity().toAffine();
@@ -266,7 +303,6 @@ var Point = class _Point {
266
303
  toHex() {
267
304
  return bytesToHex(this.toBytes());
268
305
  }
269
- // encode to hex string
270
306
  clearCofactor() {
271
307
  return this.multiply(big(h), false);
272
308
  }
@@ -279,24 +315,12 @@ var Point = class _Point {
279
315
  p = p.add(this);
280
316
  return p.is0();
281
317
  }
282
- static fromHex(hex, zip215) {
283
- return _Point.fromBytes(toU8(hex), zip215);
284
- }
285
- get x() {
286
- return this.toAffine().x;
287
- }
288
- get y() {
289
- return this.toAffine().y;
290
- }
291
- toRawBytes() {
292
- return this.toBytes();
293
- }
294
318
  };
295
319
  var G = new Point(Gx, Gy, 1n, M(Gx * Gy));
296
320
  var I = new Point(0n, 1n, 1n, 0n);
297
321
  Point.BASE = G;
298
322
  Point.ZERO = I;
299
- var numTo32bLE = (num) => hexToBytes(padh(arange(num, 0n, B256), L2)).reverse();
323
+ var numTo32bLE = (num) => hexToBytes(padh(assertRange(num, 0n, B256), L2)).reverse();
300
324
  var bytesToNumLE = (b) => big("0x" + bytesToHex(u8fr(abytes(b)).reverse()));
301
325
  var pow2 = (x, power) => {
302
326
  let r = x;
@@ -342,8 +366,8 @@ var uvRatio = (u, v) => {
342
366
  return { isValid: useRoot1 || useRoot2, value: x };
343
367
  };
344
368
  var modL_LE = (hash) => modN(bytesToNumLE(hash));
345
- var sha512a = (...m) => etc.sha512Async(...m);
346
- var sha512s = (...m) => callHash("sha512Sync")(...m);
369
+ var sha512a = (...m) => hashes.sha512Async(concatBytes(...m));
370
+ var sha512s = (...m) => callHash("sha512")(concatBytes(...m));
347
371
  var hash2extK = (hashed) => {
348
372
  const head = hashed.slice(0, L);
349
373
  head[0] &= 248;
@@ -355,9 +379,9 @@ var hash2extK = (hashed) => {
355
379
  const pointBytes = point.toBytes();
356
380
  return { head, prefix, scalar, point, pointBytes };
357
381
  };
358
- var getExtendedPublicKeyAsync = (priv) => sha512a(toU8(priv, L)).then(hash2extK);
359
- var getExtendedPublicKey = (priv) => hash2extK(sha512s(toU8(priv, L)));
360
- var getPublicKeyAsync = (priv) => getExtendedPublicKeyAsync(priv).then((p) => p.pointBytes);
382
+ var getExtendedPublicKeyAsync = (secretKey) => sha512a(abytes(secretKey, L)).then(hash2extK);
383
+ var getExtendedPublicKey = (secretKey) => hash2extK(sha512s(abytes(secretKey, L)));
384
+ var getPublicKeyAsync = (secretKey) => getExtendedPublicKeyAsync(secretKey).then((p) => p.pointBytes);
361
385
  var hashFinishA = (res) => sha512a(res.hashable).then(res.finish);
362
386
  var _sign = (e, rBytes, msg) => {
363
387
  const { pointBytes: P2, scalar: s } = e;
@@ -370,17 +394,17 @@ var _sign = (e, rBytes, msg) => {
370
394
  };
371
395
  return { hashable, finish };
372
396
  };
373
- var signAsync = async (msg, privKey) => {
374
- const m = toU8(msg);
375
- const e = await getExtendedPublicKeyAsync(privKey);
397
+ var signAsync = async (message, secretKey) => {
398
+ const m = abytes(message);
399
+ const e = await getExtendedPublicKeyAsync(secretKey);
376
400
  const rBytes = await sha512a(e.prefix, m);
377
401
  return hashFinishA(_sign(e, rBytes, m));
378
402
  };
379
- var veriOpts = { zip215: true };
380
- var _verify = (sig, msg, pub, opts = veriOpts) => {
381
- sig = toU8(sig, L2);
382
- msg = toU8(msg);
383
- pub = toU8(pub, L);
403
+ var defaultVerifyOpts = { zip215: true };
404
+ var _verify = (sig, msg, pub, opts = defaultVerifyOpts) => {
405
+ sig = abytes(sig, L2);
406
+ msg = abytes(msg);
407
+ pub = abytes(pub, L);
384
408
  const { zip215 } = opts;
385
409
  let A;
386
410
  let R;
@@ -388,8 +412,8 @@ var _verify = (sig, msg, pub, opts = veriOpts) => {
388
412
  let SB;
389
413
  let hashable = Uint8Array.of();
390
414
  try {
391
- A = Point.fromHex(pub, zip215);
392
- R = Point.fromHex(sig.slice(0, L), zip215);
415
+ A = Point.fromBytes(pub, zip215);
416
+ R = Point.fromBytes(sig.slice(0, L), zip215);
393
417
  s = bytesToNumLE(sig.slice(L, L2));
394
418
  SB = G.multiply(s, false);
395
419
  hashable = concatBytes(R.toBytes(), A.toBytes(), msg);
@@ -406,30 +430,20 @@ var _verify = (sig, msg, pub, opts = veriOpts) => {
406
430
  };
407
431
  return { hashable, finish };
408
432
  };
409
- var verifyAsync = async (s, m, p, opts = veriOpts) => hashFinishA(_verify(s, m, p, opts));
410
- var etc = {
411
- sha512Async: async (...messages) => {
433
+ var verifyAsync = async (signature, message, publicKey, opts = defaultVerifyOpts) => hashFinishA(_verify(signature, message, publicKey, opts));
434
+ var hashes = {
435
+ sha512Async: async (message) => {
412
436
  const s = subtle();
413
- const m = concatBytes(...messages);
437
+ const m = concatBytes(message);
414
438
  return u8n(await s.digest("SHA-512", m.buffer));
415
439
  },
416
- sha512Sync: void 0,
417
- bytesToHex,
418
- hexToBytes,
419
- concatBytes,
420
- mod: M,
421
- invert,
422
- randomBytes
440
+ sha512: void 0
423
441
  };
442
+ var randomSecretKey = (seed = randomBytes(L)) => seed;
424
443
  var utils = {
425
444
  getExtendedPublicKeyAsync,
426
445
  getExtendedPublicKey,
427
- randomPrivateKey: () => randomBytes(L),
428
- precompute: (w = 8, p = G) => {
429
- p.multiply(3n);
430
- return p;
431
- }
432
- // no-op
446
+ randomSecretKey
433
447
  };
434
448
  var W = 8;
435
449
  var scalarBits = 256;
@@ -481,6 +495,8 @@ var wNAF = (n) => {
481
495
  p = p.add(ctneg(isNeg, comp[offP]));
482
496
  }
483
497
  }
498
+ if (n !== 0n)
499
+ err("invalid wnaf");
484
500
  return { p, f };
485
501
  };
486
502
 
@@ -612,14 +628,17 @@ async function exportKeyPolyfill(format, key) {
612
628
  throw new DOMException(`Unable to export a raw Ed25519 ${key.type} key`, "InvalidAccessError");
613
629
  }
614
630
  const publicKeyBytes = await getPublicKeyBytes(key);
615
- return publicKeyBytes;
631
+ return publicKeyBytes.buffer;
616
632
  }
617
633
  case "pkcs8": {
618
634
  if (key.type !== "private") {
619
635
  throw new DOMException(`Unable to export a pkcs8 Ed25519 ${key.type} key`, "InvalidAccessError");
620
636
  }
621
637
  const secretKeyBytes = getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key);
622
- return new Uint8Array([...ED25519_PKCS8_HEADER, ...secretKeyBytes]);
638
+ const result = new Uint8Array(ED25519_PKCS8_HEADER.length + secretKeyBytes.length);
639
+ result.set(ED25519_PKCS8_HEADER, 0);
640
+ result.set(secretKeyBytes, ED25519_PKCS8_HEADER.length);
641
+ return result.buffer;
623
642
  }
624
643
  case "jwk": {
625
644
  const publicKeyBytes = await getPublicKeyBytes(key);
@@ -644,7 +663,7 @@ async function exportKeyPolyfill(format, key) {
644
663
  }
645
664
  }
646
665
  function generateKeyPolyfill(extractable, keyUsages) {
647
- const privateKeyBytes = utils.randomPrivateKey();
666
+ const privateKeyBytes = utils.randomSecretKey();
648
667
  const keyPair = createKeyPairFromBytes(privateKeyBytes, extractable, keyUsages);
649
668
  return keyPair;
650
669
  }
@@ -658,7 +677,7 @@ async function signPolyfill(key, data) {
658
677
  const privateKeyBytes = getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key);
659
678
  const payload = bufferSourceToUint8Array(data);
660
679
  const signature = await signAsync(payload, privateKeyBytes);
661
- return signature;
680
+ return signature.buffer;
662
681
  }
663
682
  async function verifyPolyfill(key, signature, data) {
664
683
  if (key.type !== "public" || !key.usages.includes("verify")) {