Skip to content

Commit

Permalink
keychain: polyfillEd25519 => ponyfillEd25519
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Jan 18, 2024
1 parent ccd2cb4 commit eb1562a
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions packages/keychain/src/algo/ed.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { type LLSign, type LLVerify, SigType, Verifier } from "@ndn/packet";
import { crypto } from "@ndn/util";
import type * as asn1 from "@yoursunny/asn1";
import { Ed25519Algorithm, polyfillEd25519 } from "@yoursunny/webcrypto-ed25519";
import { Ed25519Algorithm, ponyfillEd25519 } from "@yoursunny/webcrypto-ed25519";

import type { CryptoAlgorithm, SigningAlgorithm } from "../key/mod";
import { assertSpkiAlgorithm } from "./impl-spki";

polyfillEd25519();
const subtle = ponyfillEd25519();

class EdAlgo implements SigningAlgorithm<{}, true, {}> {
constructor(
Expand All @@ -27,15 +26,15 @@ class EdAlgo implements SigningAlgorithm<{}, true, {}> {
if (importPkcs8) {
const [pkcs8, spki] = importPkcs8;
[privateKey, publicKey] = await Promise.all([
crypto.subtle.importKey("pkcs8", pkcs8, this.algo, extractable, this.keyUsages.private),
crypto.subtle.importKey("spki", spki, this.algo, true, this.keyUsages.public),
subtle.importKey("pkcs8", pkcs8, this.algo, extractable, this.keyUsages.private),
subtle.importKey("spki", spki, this.algo, true, this.keyUsages.public),
]);
} else {
({ privateKey, publicKey } = await crypto.subtle.generateKey(this.algo, extractable,
({ privateKey, publicKey } = await subtle.generateKey(this.algo, extractable,
[...this.keyUsages.private, ...this.keyUsages.public]) as CryptoKeyPair);
}

const spki = new Uint8Array(await crypto.subtle.exportKey("spki", publicKey));
const spki = new Uint8Array(await subtle.exportKey("spki", publicKey));
return {
privateKey,
publicKey,
Expand All @@ -47,7 +46,7 @@ class EdAlgo implements SigningAlgorithm<{}, true, {}> {

public async importSpki(spki: Uint8Array, der: asn1.ElementBuffer) {
assertSpkiAlgorithm(der, this.algo.name, this.oid);
const key = await crypto.subtle.importKey(
const key = await subtle.importKey(
"spki", spki, this.algo, true, this.keyUsages.public);
return {
publicKey: key,
Expand All @@ -58,14 +57,14 @@ class EdAlgo implements SigningAlgorithm<{}, true, {}> {

public makeLLSign({ privateKey }: CryptoAlgorithm.PrivateKey<{}>): LLSign {
return async (input) => {
const raw = await crypto.subtle.sign(this.algo, privateKey, input);
const raw = await subtle.sign(this.algo, privateKey, input);
return new Uint8Array(raw);
};
}

public makeLLVerify({ publicKey }: CryptoAlgorithm.PublicKey<{}>): LLVerify {
return async (input, sig) => {
const ok = await crypto.subtle.verify(this.algo, publicKey, sig, input);
const ok = await subtle.verify(this.algo, publicKey, sig, input);
Verifier.throwOnBadSig(ok);
};
}
Expand Down

0 comments on commit eb1562a

Please sign in to comment.