@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.26 kB
JavaScript
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 "../models/ClaimFormat.mjs";
import { W3cV2Credential } from "../models/credential/W3cV2Credential.mjs";
import { decodeSdJwt } from "./W3cV2SdJwt.mjs";
//#region src/modules/vc/sd-jwt-vc/W3cV2SdJwtVerifiableCredential.ts
/**
* Represents a Verifiable Credential encoded as a SD-JWT.
*
* @see https://www.w3.org/TR/vc-jose-cose/#securing-with-sd-jwt
*/
var W3cV2SdJwtVerifiableCredential = class W3cV2SdJwtVerifiableCredential {
constructor(options) {
this.sdJwt = options.sdJwt;
this.resolvedCredential = JsonTransformer.fromJSON(options.sdJwt.prettyClaims, W3cV2Credential, { validate: false });
this.validate();
}
static fromCompact(compact) {
const sdJwt = decodeSdJwt(compact, "vc+sd-jwt");
return new W3cV2SdJwtVerifiableCredential({ sdJwt });
}
/**
* The encoded version of this credential.
*/
get encoded() {
return this.sdJwt.compact;
}
/**
* The {@link ClaimFormat} of the credential.
*
* For W3C VC SD-JWT credentials this is always `vc+sd-jwt`.
*/
get claimFormat() {
return "vc+sd-jwt";
}
/**
* Validates the SD-JWT and the resolved credential.
*/
validate() {
MessageValidator.validateSync(this.resolvedCredential);
const sdJwt = this.sdJwt;
const header = sdJwt.header;
const payload = sdJwt.prettyClaims;
if (header.alg === "none") throw new CredoError(`The provided W3C VC SD-JWT uses 'alg'='none', which is not allowed.`);
if ("typ" in header && header.typ !== "vc+sd-jwt") throw new CredoError(`The provided W3C VC SD-JWT does not have the correct 'typ' header.`);
if ("cty" in header && header.cty !== "vc") throw new CredoError(`The provided W3C VC SD-JWT does not have the correct 'cty' header.`);
if (payload.iss) {
if (this.resolvedCredential.issuerId !== payload.iss) throw new CredoError(`The provided W3C VC SD-JWT has both 'iss' and 'issuer' claims, but they differ.`);
}
}
};
//#endregion
export { W3cV2SdJwtVerifiableCredential };
//# sourceMappingURL=W3cV2SdJwtVerifiableCredential.mjs.map