@peculiar/webcrypto
Version:
A WebCrypto Polyfill for NodeJS
39 lines (38 loc) • 1.39 kB
JavaScript
/**
* Copyright (c) 2019-2026, Peculiar Ventures
* SPDX-License-Identifier: MIT
*/
import { AsymmetricKey } from "../../keys/asymmetric.js";
import { getOidByNamedCurve } from "./helper.js";
import * as core from "webcrypto-core";
import { Buffer } from "node:buffer";
import { JsonParser, JsonSerializer } from "@peculiar/json-schema";
import { AsnParser, AsnSerializer } from "@peculiar/asn1-schema";
// src/mechs/ed/private_key.ts
var EdPrivateKey = class extends AsymmetricKey {
type = "private";
getKey() {
const keyInfo = AsnParser.parse(this.data, core.asn1.PrivateKeyInfo);
return AsnParser.parse(keyInfo.privateKey, core.asn1.CurvePrivateKey);
}
toJSON() {
const key = this.getKey();
const json = {
kty: "OKP",
crv: this.algorithm.namedCurve,
key_ops: this.usages,
ext: this.extractable
};
return Object.assign(json, JsonSerializer.toJSON(key));
}
fromJSON(json) {
if (!json.crv) throw new core.OperationError("Cannot get named curve from JWK. Property 'crv' is required");
const keyInfo = new core.asn1.PrivateKeyInfo();
keyInfo.privateKeyAlgorithm.algorithm = getOidByNamedCurve(json.crv);
const key = JsonParser.fromJSON(json, { targetSchema: core.asn1.CurvePrivateKey });
keyInfo.privateKey = AsnSerializer.serialize(key);
this.data = Buffer.from(AsnSerializer.serialize(keyInfo));
return this;
}
};
export { EdPrivateKey };