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

58 lines (57 loc) • 2.22 kB
import { CredoError } from "../../../error/CredoError.mjs"; import "../../../error/index.mjs"; import { MessageValidator } from "../../../utils/MessageValidator.mjs"; import { JsonTransformer } from "../../../utils/JsonTransformer.mjs"; import "../../../utils/index.mjs"; import { Jwt } from "../../../crypto/jose/jwt/Jwt.mjs"; import "../models/ClaimFormat.mjs"; import { W3cV2Credential } from "../models/credential/W3cV2Credential.mjs"; //#region src/modules/vc/jwt-vc/W3cV2JwtVerifiableCredential.ts /** * Represents a Verifiable Credential encoded as a JWT. * * @see https://www.w3.org/TR/vc-jose-cose/#securing-with-jose */ var W3cV2JwtVerifiableCredential = class W3cV2JwtVerifiableCredential { constructor(options) { this.jwt = options.jwt; this.resolvedCredential = JsonTransformer.fromJSON(options.jwt.payload.additionalClaims, W3cV2Credential, { validate: false }); this.validate(); } static fromCompact(compact) { const jwt = Jwt.fromSerializedJwt(compact); return new W3cV2JwtVerifiableCredential({ jwt }); } /** * The encoded version of this credential. */ get encoded() { return this.jwt.serializedJwt; } /** * The {@link ClaimFormat} of the credential. * * For W3C VC JWT credentials this is always `vc+jwt`. */ get claimFormat() { return "vc+jwt"; } /** * Validates the JWT and the resolved credential contained. */ validate() { MessageValidator.validateSync(this.resolvedCredential); const jwt = this.jwt; const header = jwt.header; const payload = jwt.payload; if (header.alg === "none") throw new CredoError(`The provided W3C VC JWT uses 'alg'='none', which is not allowed.`); if ("typ" in header && header.typ !== "vc+jwt") throw new CredoError(`The provided W3C VC JWT does not have the correct 'typ' header.`); if ("cty" in header && header.cty !== "vc") throw new CredoError(`The provided W3C VC JWT does not have the correct 'cty' header.`); if (payload.iss) { if (this.resolvedCredential.issuerId !== payload.iss) throw new CredoError(`The provided W3C VC JWT has both 'iss' and 'issuer' claims, but they differ.`); } } }; //#endregion export { W3cV2JwtVerifiableCredential }; //# sourceMappingURL=W3cV2JwtVerifiableCredential.mjs.map