UNPKG

@credo-ts/core

Version:

<p align="center"> <br /> <img alt="Credo Logo" src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png" height="250px" /> </p> <h1 align="center"><b>Credo - Core</b></h1

41 lines (40 loc) • 1.89 kB
import { CredoError } from "../../error/CredoError.mjs"; import "../../error/index.mjs"; import { knownJwaFromCoseSignatureAlgorithm } from "../../modules/kms/cose/algorithm.mjs"; import { KeyManagementApi } from "../../modules/kms/KeyManagementApi.mjs"; import "../../modules/kms/index.mjs"; import "@owf/cose"; //#region src/crypto/contexts/mac0Context.ts const getMac0Context = (agentContext) => { const kms = agentContext.resolve(KeyManagementApi); return { authenticate: async (input) => { if (input.key instanceof Uint8Array) throw new CredoError("For mdoc authentication with mac0 a CoseKey is required, not a Uint8Array"); if (!input.key.keyId) throw new CredoError("Missing required keyId on CoseKey for signing mdoc"); const algorithm = input.algorithm ?? input.key.algorithm; if (algorithm === void 0) throw new CredoError("Unable to authenticate COSE Mac0 structure. No algorithm provided or defined on the COSE key."); const { signature } = await kms.sign({ data: input.toBeAuthenticated, algorithm: knownJwaFromCoseSignatureAlgorithm(algorithm), keyId: input.key.keyId }); return signature; }, verify: async (input) => { const { tag, toBeAuthenticated, key } = input; if (key instanceof Uint8Array) throw new CredoError("For mdoc authentication verification with mac0 a CoseKey is required, not a Uint8Array"); const algorithm = input.algorithm ?? key.algorithm; if (algorithm === void 0) throw new CredoError("Unable to verify COSE Mac0 structure. No algorithm defined in the protected header or on the COSE key."); const { verified } = await kms.verify({ key: { publicJwk: key.jwk }, data: toBeAuthenticated, algorithm: knownJwaFromCoseSignatureAlgorithm(algorithm), signature: tag }); return verified; } }; }; //#endregion export { getMac0Context }; //# sourceMappingURL=mac0Context.mjs.map