@yoursunny/webcrypto-ed25519
Advanced tools
Comparing version 0.0.20230624 to 0.0.20250208
@@ -6,5 +6,3 @@ import * as ed from "@noble/ed25519"; | ||
import { C, isEd25519Algorithm } from "./common.js"; | ||
export const Ed25519Algorithm = { | ||
name: C.wicgAlgorithm, | ||
}; | ||
export { Ed25519Algorithm } from "./common.js"; | ||
function asUint8Array(b) { | ||
@@ -26,4 +24,4 @@ if (b instanceof Uint8Array) { | ||
const slot = "8d9df0f7-1363-4d2c-8152-ce4ed78f27d8"; | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging | ||
class Ponyfill { | ||
super_; | ||
constructor(super_) { | ||
@@ -41,2 +39,3 @@ this.super_ = super_; | ||
} | ||
orig_; | ||
async generateKey(algorithm, extractable, keyUsages) { | ||
@@ -147,6 +146,35 @@ if (isEd25519Algorithm(algorithm)) { | ||
} | ||
export function ponyfillEd25519(subtle = crypto.subtle) { | ||
return new Ponyfill(subtle); | ||
async function checkNativeSupport() { | ||
try { | ||
// https://datatracker.ietf.org/doc/html/rfc8037#appendix-A | ||
const jwk = { | ||
kty: "OKP", | ||
crv: "Ed25519", | ||
d: "nWGxne_9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A", | ||
x: "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo", | ||
}; | ||
const pvt = await crypto.subtle.importKey("jwk", jwk, "Ed25519", false, ["sign"]); | ||
delete jwk.d; | ||
const pub = await crypto.subtle.importKey("jwk", jwk, "Ed25519", true, ["verify"]); | ||
const data = new TextEncoder().encode("eyJhbGciOiJFZERTQSJ9.RXhhbXBsZSBvZiBFZDI1NTE5IHNpZ25pbmc"); | ||
const sig = await crypto.subtle.sign("Ed25519", pvt, data); | ||
const verified = await crypto.subtle.verify("Ed25519", pub, sig, data); | ||
return verified && ed.etc.bytesToHex(new Uint8Array(sig)) === | ||
"860c98d2297f3060a33f42739672d61b53cf3adefed3d3c672f320dc021b411e9d59b8628dc351e248b88b29468e0e41855b0fb7d83bb15be902bfccb8cd0a02"; | ||
} | ||
catch { | ||
return false; | ||
} | ||
} | ||
export const hasNativeSupport = await checkNativeSupport(); | ||
export function ponyfillEd25519() { | ||
if (hasNativeSupport) { | ||
return crypto.subtle; | ||
} | ||
return new Ponyfill(crypto.subtle); | ||
} | ||
export function polyfillEd25519() { | ||
if (hasNativeSupport) { | ||
return; | ||
} | ||
Object.defineProperty(globalThis.crypto, "subtle", { | ||
@@ -153,0 +181,0 @@ value: ponyfillEd25519(), |
@@ -8,5 +8,3 @@ import * as ed from "@noble/ed25519"; | ||
export const Ed25519Algorithm: KeyAlgorithm = { | ||
name: C.wicgAlgorithm, | ||
}; | ||
export { Ed25519Algorithm } from "./common.js"; | ||
@@ -25,5 +23,5 @@ function asUint8Array(b: BufferSource): Uint8Array { | ||
if (b.byteLength === b.buffer.byteLength) { | ||
return b.buffer; | ||
return b.buffer as ArrayBuffer; | ||
} | ||
return b.buffer.slice(b.byteOffset, b.byteLength); | ||
return (b.buffer as ArrayBuffer).slice(b.byteOffset, b.byteLength); | ||
} | ||
@@ -37,3 +35,2 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging | ||
class Ponyfill implements Record<keyof SubtleCrypto, Function> { | ||
@@ -163,10 +160,39 @@ constructor(private readonly super_: SubtleCrypto) { | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging | ||
interface Ponyfill extends Record<keyof SubtleCrypto, Function> {} | ||
export function ponyfillEd25519(subtle = crypto.subtle): SubtleCrypto { | ||
return new Ponyfill(subtle) as unknown as SubtleCrypto; | ||
async function checkNativeSupport(): Promise<boolean> { | ||
try { | ||
// https://datatracker.ietf.org/doc/html/rfc8037#appendix-A | ||
const jwk: JsonWebKey = { | ||
kty: "OKP", | ||
crv: "Ed25519", | ||
d: "nWGxne_9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A", | ||
x: "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo", | ||
}; | ||
const pvt = await crypto.subtle.importKey("jwk", jwk, "Ed25519", false, ["sign"]); | ||
delete jwk.d; | ||
const pub = await crypto.subtle.importKey("jwk", jwk, "Ed25519", true, ["verify"]); | ||
const data = new TextEncoder().encode("eyJhbGciOiJFZERTQSJ9.RXhhbXBsZSBvZiBFZDI1NTE5IHNpZ25pbmc"); | ||
const sig = await crypto.subtle.sign("Ed25519", pvt, data); | ||
const verified = await crypto.subtle.verify("Ed25519", pub, sig, data); | ||
return verified && ed.etc.bytesToHex(new Uint8Array(sig)) === | ||
"860c98d2297f3060a33f42739672d61b53cf3adefed3d3c672f320dc021b411e9d59b8628dc351e248b88b29468e0e41855b0fb7d83bb15be902bfccb8cd0a02"; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
export const hasNativeSupport = await checkNativeSupport(); | ||
export function ponyfillEd25519(): SubtleCrypto { | ||
if (hasNativeSupport) { | ||
return crypto.subtle; | ||
} | ||
return new Ponyfill(crypto.subtle) as unknown as SubtleCrypto; | ||
} | ||
export function polyfillEd25519(): void { | ||
if (hasNativeSupport) { | ||
return; | ||
} | ||
Object.defineProperty(globalThis.crypto, "subtle", { | ||
@@ -173,0 +199,0 @@ value: ponyfillEd25519(), |
@@ -0,5 +1,5 @@ | ||
export const Ed25519Algorithm = { | ||
name: "Ed25519", | ||
}; | ||
export const C = { | ||
wicgAlgorithm: "Ed25519", | ||
nodeAlgorithm: "NODE-ED25519", | ||
nodeNamedCurve: "NODE-ED25519", | ||
kty: "OKP", | ||
@@ -10,6 +10,4 @@ crv: "Ed25519", | ||
export function isEd25519Algorithm(a) { | ||
return a === C.wicgAlgorithm || a === C.nodeAlgorithm || | ||
a.name === C.wicgAlgorithm || | ||
(a.name === C.nodeAlgorithm && a.namedCurve === C.nodeNamedCurve); | ||
return a === Ed25519Algorithm.name || a.name === Ed25519Algorithm.name; | ||
} | ||
//# sourceMappingURL=common.js.map |
@@ -0,5 +1,6 @@ | ||
export const Ed25519Algorithm: Algorithm = { | ||
name: "Ed25519", | ||
}; | ||
export const C = { | ||
wicgAlgorithm: "Ed25519", | ||
nodeAlgorithm: "NODE-ED25519", | ||
nodeNamedCurve: "NODE-ED25519", | ||
kty: "OKP", | ||
@@ -10,6 +11,4 @@ crv: "Ed25519", | ||
export function isEd25519Algorithm(a: AlgorithmIdentifier | KeyAlgorithm): boolean { | ||
return a === C.wicgAlgorithm || a === C.nodeAlgorithm || | ||
(a as KeyAlgorithm).name === C.wicgAlgorithm || | ||
((a as EcKeyAlgorithm).name === C.nodeAlgorithm && (a as EcKeyAlgorithm).namedCurve === C.nodeNamedCurve); | ||
export function isEd25519Algorithm(a: AlgorithmIdentifier): boolean { | ||
return a === Ed25519Algorithm.name || (a as Algorithm).name === Ed25519Algorithm.name; | ||
} |
17
index.js
@@ -1,14 +0,5 @@ | ||
import { satisfies } from "compare-versions"; | ||
import { C } from "./common.js"; | ||
const wicgKeyAlgorithm = { | ||
name: C.wicgAlgorithm, | ||
}; | ||
const nodeKeyAlgorithm = { | ||
name: C.nodeAlgorithm, | ||
namedCurve: C.nodeNamedCurve, | ||
}; | ||
export const Ed25519Algorithm = satisfies(process.version, "<18.4.0") && !satisfies(process.version, "^16.17.0") ? | ||
nodeKeyAlgorithm : wicgKeyAlgorithm; | ||
export function ponyfillEd25519(subtle = crypto.subtle) { | ||
return subtle; | ||
export { Ed25519Algorithm } from "./common.js"; | ||
export const hasNativeSupport = true; | ||
export function ponyfillEd25519() { | ||
return crypto.subtle; | ||
} | ||
@@ -15,0 +6,0 @@ export function polyfillEd25519() { |
21
index.ts
@@ -1,20 +0,7 @@ | ||
import { satisfies } from "compare-versions"; | ||
export { Ed25519Algorithm } from "./common.js"; | ||
import { C } from "./common.js"; | ||
export const hasNativeSupport = true; | ||
const wicgKeyAlgorithm: KeyAlgorithm = { | ||
name: C.wicgAlgorithm, | ||
}; | ||
const nodeKeyAlgorithm: EcKeyAlgorithm = { | ||
name: C.nodeAlgorithm, | ||
namedCurve: C.nodeNamedCurve, | ||
}; | ||
export const Ed25519Algorithm: KeyAlgorithm = | ||
satisfies(process.version, "<18.4.0") && !satisfies(process.version, "^16.17.0") ? | ||
nodeKeyAlgorithm : wicgKeyAlgorithm; | ||
export function ponyfillEd25519(subtle = crypto.subtle): SubtleCrypto { | ||
return subtle; | ||
export function ponyfillEd25519(): SubtleCrypto { | ||
return crypto.subtle; | ||
} | ||
@@ -21,0 +8,0 @@ |
{ | ||
"name": "@yoursunny/webcrypto-ed25519", | ||
"version": "0.0.20230624", | ||
"version": "0.0.20250208", | ||
"description": "Ed25519 Ponyfill & Polyfill for WebCrypto", | ||
@@ -26,18 +26,19 @@ "keywords": [ | ||
"types": "index.d.ts", | ||
"packageManager": "pnpm@8.6.3", | ||
"dependencies": { | ||
"@noble/ed25519": "^2.0.0", | ||
"@noble/ed25519": "^2.2.3", | ||
"@yoursunny/asn1": "0.0.20200718", | ||
"b64u-lite": "^1.1.0", | ||
"compare-versions": "6.0.0-rc.1" | ||
"b64u-lite": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/minimalistic-assert": "^1.0.1", | ||
"@types/node": "^20.3.1", | ||
"@yoursunny/xo-config": "^0.54.0", | ||
"@types/minimalistic-assert": "^1.0.3", | ||
"@types/node": "^22.13.1", | ||
"@yoursunny/xo-config": "^0.60.0", | ||
"minimalistic-assert": "^1.0.1", | ||
"typescript": "^5.1.3", | ||
"webpack": "^5.88.0", | ||
"webpack-cli": "^5.1.4" | ||
"typescript": "^5.7.3", | ||
"webpack": "^5.97.1", | ||
"webpack-cli": "^6.0.1" | ||
}, | ||
"engines": { | ||
"node": ">=18.4.0" | ||
}, | ||
"scripts": { | ||
@@ -44,0 +45,0 @@ "build": "tsc", |
@@ -27,9 +27,6 @@ # Ed25519 Ponyfill & Polyfill for WebCrypto | ||
The ponyfill and polyfill for browser recognize two forms of algorithm identifier: | ||
The ponyfill and polyfill for browser recognize the algorithm identifier `{ name: "Ed25519" }`, as specified in [Secure Curves in the Web Cryptography API](https://wicg.github.io/webcrypto-secure-curves/) draft. | ||
* `{ name: "Ed25519" }`, as specified in [Secure Curves in the Web Cryptography API](https://wicg.github.io/webcrypto-secure-curves/) draft and implemented in Node.js 18.4.0 and later. | ||
* `{ name: "NODE-ED25519", namedCurve: "NODE-ED25519" }`, as implemented in Node.js 18.3.0 and earlier. | ||
The same algorithm identifier is supported in Node.js since v18.4.0. | ||
This package does not provide any ponyfill or polyfill for Node.js. | ||
The exported `Ed25519Algorithm` variable gives a suitable algorithm identifier for the current Node.js version, which helps you write code to support Node.js before and after 18.4.0. | ||
@@ -36,0 +33,0 @@ ## Features |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
25688
3
397
43
- Removedcompare-versions@6.0.0-rc.1
- Removedcompare-versions@6.0.0-rc.1(transitive)
Updated@noble/ed25519@^2.2.3