UNPKG

ox

Version:

Ethereum Standard Library

93 lines 3.49 kB
import * as Bytes from './Bytes.js'; import * as Cbor from './Cbor.js'; import * as Errors from './Errors.js'; import type * as Hex from './Hex.js'; import * as PublicKey from './PublicKey.js'; /** * Converts a P256 {@link ox#PublicKey.PublicKey} to a CBOR-encoded COSE_Key. * * The COSE_Key uses integer map keys per [RFC 9053](https://datatracker.ietf.org/doc/html/rfc9053): * - `1` (kty): `2` (EC2) * - `3` (alg): `-7` (ES256) * - `-1` (crv): `1` (P-256) * - `-2` (x): x coordinate bytes * - `-3` (y): y coordinate bytes * * @example * ```ts twoslash * import { CoseKey, P256 } from 'ox' * * const { publicKey } = P256.createKeyPair() * * const coseKey = CoseKey.fromPublicKey(publicKey) * ``` * * @param publicKey - The P256 public key to convert. * @returns The CBOR-encoded COSE_Key as a Hex string. */ export declare function fromPublicKey(publicKey: PublicKey.PublicKey): Hex.Hex; export declare namespace fromPublicKey { type ErrorType = PublicKey.toBytes.ErrorType | Cbor.encode.ErrorType | Errors.GlobalErrorType; } /** * Converts a CBOR-encoded COSE_Key to a P256 {@link ox#PublicKey.PublicKey}. * * Accepts the COSE key as either a hex string or raw bytes. When the * `returnByteLength` or `returnDecoded` option is set, the function returns an * object containing the public key plus the consumed byte length and/or the * decoded CBOR map. This is useful for parsing CBOR streams (such as WebAuthn * `authenticatorData`) where the COSE key is followed by trailing data. * * @example * ```ts twoslash * import { CoseKey, P256 } from 'ox' * * const { publicKey } = P256.createKeyPair() * const coseKey = CoseKey.fromPublicKey(publicKey) * * const publicKey2 = CoseKey.toPublicKey(coseKey) * ``` * * @example * ### With Byte Length * * ```ts twoslash * import { CoseKey, P256 } from 'ox' * * const { publicKey } = P256.createKeyPair() * const coseKey = CoseKey.fromPublicKey(publicKey) * * const { publicKey: pk, byteLength } = CoseKey.toPublicKey( * coseKey, * { * returnByteLength: true * } * ) * ``` * * @param coseKey - The CBOR-encoded COSE_Key as hex or bytes. * @param options - Decoding options. * @returns The P256 public key, optionally with byte length and decoded CBOR. */ export declare function toPublicKey<options extends toPublicKey.Options = {}>(coseKey: Hex.Hex | Uint8Array, options?: options | toPublicKey.Options): toPublicKey.ReturnType<options>; export declare namespace toPublicKey { type Options = { /** Include the consumed byte length of the COSE key in the result. */ returnByteLength?: boolean | undefined; /** Include the decoded CBOR map in the result. */ returnDecoded?: boolean | undefined; }; type ResultObject = { publicKey: PublicKey.PublicKey; byteLength?: number; decoded?: Record<string, unknown>; }; type ReturnType<options extends Options = {}> = options['returnByteLength'] extends true ? ResultObject : options['returnDecoded'] extends true ? ResultObject : PublicKey.PublicKey; type ErrorType = Bytes.fromHex.ErrorType | Cbor.decode.ErrorType | PublicKey.from.ErrorType | InvalidCoseKeyError | Errors.GlobalErrorType; } /** Thrown when a COSE_Key does not contain valid P256 public key coordinates. */ export declare class InvalidCoseKeyError extends Errors.BaseError { readonly name = "CoseKey.InvalidCoseKeyError"; constructor(); } //# sourceMappingURL=CoseKey.d.ts.map