@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
97 lines (93 loc) • 4.8 kB
JavaScript
import { CredoError } from "../../../../error/CredoError.mjs";
import "../../../../error/index.mjs";
import { __decorateMetadata } from "../../../../_virtual/_@oxc-project_runtime@0.112.0/helpers/decorateMetadata.mjs";
import { __decorate } from "../../../../_virtual/_@oxc-project_runtime@0.112.0/helpers/decorate.mjs";
import { CREDENTIALS_CONTEXT_V2_URL, ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE, ENVELOPED_VERIFIABLE_PRESENTATION_TYPE } from "../../constants.mjs";
import { IsCredentialJsonLdContext } from "../../validators.mjs";
import "./W3cV2Presentation.mjs";
import { W3cV2SdJwtVerifiablePresentation } from "../../sd-jwt-vc/W3cV2SdJwtVerifiablePresentation.mjs";
import "../../sd-jwt-vc/index.mjs";
import { W3cV2JwtVerifiablePresentation } from "../../jwt-vc/W3cV2JwtVerifiablePresentation.mjs";
import "../../jwt-vc/index.mjs";
import { Exclude, Expose } from "class-transformer";
import { IsDataURI, ValidateBy, buildMessage } from "class-validator";
//#region src/modules/vc/models/presentation/W3cV2EnvelopedVerifiablePresentation.ts
var W3cV2EnvelopedVerifiablePresentation = class W3cV2EnvelopedVerifiablePresentation {
constructor(options) {
if (options) {
this.context = options.context ?? CREDENTIALS_CONTEXT_V2_URL;
this.id = options.id;
this.type = options.type ?? ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE;
this._envelopedPresentation = presentationFromDataUri(options.id);
}
}
static fromVerifiablePresentation(presentation) {
return new W3cV2EnvelopedVerifiablePresentation({
id: presentationToDataUri(presentation),
context: CREDENTIALS_CONTEXT_V2_URL,
type: ENVELOPED_VERIFIABLE_PRESENTATION_TYPE
});
}
/**
* Gets the enveloped presentation.
*/
get envelopedPresentation() {
if (!this._envelopedPresentation) this._envelopedPresentation = presentationFromDataUri(this.id);
return this._envelopedPresentation;
}
/**
* Resolved presentation is the fully resolved {@link W3cV2Presentation} instance.
*/
get resolvedPresentation() {
return this.envelopedPresentation.resolvedPresentation;
}
/**
* The {@link ClaimFormat} of the enveloped presentation.
*/
get claimFormat() {
return this.envelopedPresentation.claimFormat;
}
};
__decorate([Exclude(), __decorateMetadata("design:type", Object)], W3cV2EnvelopedVerifiablePresentation.prototype, "_envelopedPresentation", void 0);
__decorate([
Expose({ name: "@context" }),
IsCredentialJsonLdContext({
allowString: true,
credentialContext: CREDENTIALS_CONTEXT_V2_URL
}),
__decorateMetadata("design:type", Object)
], W3cV2EnvelopedVerifiablePresentation.prototype, "context", void 0);
__decorate([IsDataURI(), __decorateMetadata("design:type", String)], W3cV2EnvelopedVerifiablePresentation.prototype, "id", void 0);
__decorate([IsEnvelopedVerifiablePresentationType(), __decorateMetadata("design:type", Object)], W3cV2EnvelopedVerifiablePresentation.prototype, "type", void 0);
function IsEnvelopedVerifiablePresentationType(validationOptions) {
return ValidateBy({
name: "IsEnvelopedVerifiablePresentationType",
validator: {
validate: (value) => {
return Array.isArray(value) ? value.includes(ENVELOPED_VERIFIABLE_PRESENTATION_TYPE) : value === ENVELOPED_VERIFIABLE_PRESENTATION_TYPE;
},
defaultMessage: buildMessage((eachPrefix) => `${eachPrefix}$property must be a string that equals, or an array of strings which includes "${ENVELOPED_VERIFIABLE_PRESENTATION_TYPE}"`, validationOptions)
}
}, validationOptions);
}
function presentationFromDataUri(uri) {
if (!uri.startsWith("data:")) throw new CredoError("Invalid Enveloped Verifiable Presentation: \"id\" is not a valid data URI");
const mimetypeData = uri.slice(5);
const commaIndex = mimetypeData.indexOf(",");
if (commaIndex === -1) throw new CredoError("Invalid Enveloped Verifiable Presentation: \"id\" data URI is missing comma separator");
const mimetype = mimetypeData.slice(0, commaIndex);
const data = mimetypeData.slice(commaIndex + 1);
switch (mimetype) {
case "application/vp+sd-jwt": return W3cV2SdJwtVerifiablePresentation.fromCompact(data);
case "application/vp+jwt": return W3cV2JwtVerifiablePresentation.fromCompact(data);
default: throw new CredoError(`Unsupported Enveloped Verifiable Presentation: ${mimetype} not recognized`);
}
}
function presentationToDataUri(presentation) {
if (presentation instanceof W3cV2SdJwtVerifiablePresentation) return `data:application/vp+sd-jwt,${presentation.encoded}`;
if (presentation instanceof W3cV2JwtVerifiablePresentation) return `data:application/vp+jwt,${presentation.encoded}`;
throw new CredoError("Unsupported Verifiable Presentation instance");
}
//#endregion
export { IsEnvelopedVerifiablePresentationType, W3cV2EnvelopedVerifiablePresentation };
//# sourceMappingURL=W3cV2EnvelopedVerifiablePresentation.mjs.map