UNPKG

did-jwt

Version:

Library for Signing and Verifying JWTs that use DIDs as issuers and JWEs that use DIDs as recipients

60 lines • 2.16 kB
import { fromJose, toJose } from './util.js'; // eslint-disable-next-line @typescript-eslint/no-explicit-any function instanceOfEcdsaSignature(object) { return typeof object === 'object' && 'r' in object && 's' in object; } function ES256SignerAlg() { return async function sign(payload, signer) { const signature = await signer(payload); if (instanceOfEcdsaSignature(signature)) { return toJose(signature); } else { return signature; } }; } function ES256KSignerAlg(recoverable) { return async function sign(payload, signer) { const signature = await signer(payload); if (instanceOfEcdsaSignature(signature)) { return toJose(signature, recoverable); } else { if (recoverable && typeof fromJose(signature).recoveryParam === 'undefined') { throw new Error(`not_supported: ES256K-R not supported when signer doesn't provide a recovery param`); } return signature; } }; } function Ed25519SignerAlg() { return async function sign(payload, signer) { const signature = await signer(payload); if (!instanceOfEcdsaSignature(signature)) { return signature; } else { throw new Error('invalid_config: expected a signer function that returns a string instead of signature object'); } }; } const algorithms = { ES256: ES256SignerAlg(), ES256K: ES256KSignerAlg(), // This is a non-standard algorithm but retained for backwards compatibility // see https://github.com/decentralized-identity/did-jwt/issues/146 'ES256K-R': ES256KSignerAlg(true), // This is actually incorrect but retained for backwards compatibility // see https://github.com/decentralized-identity/did-jwt/issues/130 Ed25519: Ed25519SignerAlg(), EdDSA: Ed25519SignerAlg(), }; function SignerAlg(alg) { const impl = algorithms[alg]; if (!impl) throw new Error(`not_supported: Unsupported algorithm ${alg}`); return impl; } export default SignerAlg; //# sourceMappingURL=SignerAlgorithm.js.map