import type { JsonWebKey } from "did-resolver"; import * as jwkJcsPubCodec from "./codecs/jwk_jcs-pub.ts"; import { KEY_DID_METHOD_PREFIX } from "./constants.ts"; import { encodePublicKey, resolveDidDoc } from "./internals.ts"; /** * Generic function to create a `did:key:` DID based on the provided public key JWK. * @param publicKey - The raw public key, in the JWK format. * @returns The `did:key` DID corresponding to the JWK. */ export function createDid(publicKey: JsonWebKey): string { try { return `${KEY_DID_METHOD_PREFIX}${encodePublicKey( jwkJcsPubCodec.encode(publicKey), jwkJcsPubCodec.code, )}`; } catch (error) { throw new Error(error instanceof Error ? error.message : "Unknown error"); } } /** * Asserts if the DID can be resolved. * @param did - The DID to verify */ export function validateDid(did: string) { resolveDidDoc(did); } export const { validateJwk } = jwkJcsPubCodec;