UNPKG

@peculiar/webcrypto

Version:
38 lines (37 loc) • 1.35 kB
/** * 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 { convert, toArrayBuffer } from "@peculiar/utils"; import { AsnParser, AsnSerializer } from "@peculiar/asn1-schema"; // src/mechs/ed/public_key.ts var EdPublicKey = class extends AsymmetricKey { type = "public"; getKey() { return AsnParser.parse(this.data, core.asn1.PublicKeyInfo).publicKey; } toJSON() { const key = this.getKey(); const json = { kty: "OKP", crv: this.algorithm.namedCurve, key_ops: this.usages, ext: this.extractable }; return Object.assign(json, { x: convert.encode("base64url", key) }); } fromJSON(json) { if (!json.crv) throw new core.OperationError("Cannot get named curve from JWK. Property 'crv' is required"); if (!json.x) throw new core.OperationError("Cannot get property from JWK. Property 'x' is required"); const keyInfo = new core.asn1.PublicKeyInfo(); keyInfo.publicKeyAlgorithm.algorithm = getOidByNamedCurve(json.crv); keyInfo.publicKey = toArrayBuffer(convert.decode("base64url", json.x)); this.data = Buffer.from(AsnSerializer.serialize(keyInfo)); return this; } }; export { EdPublicKey };