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

1 lines • 6.36 kB
{"version":3,"file":"W3cCredential.mjs","names":[],"sources":["../../../../../src/modules/vc/models/credential/W3cCredential.ts"],"sourcesContent":["import { Expose, Type } from 'class-transformer'\nimport { IsInstance, IsOptional, IsRFC3339, ValidateNested } from 'class-validator'\nimport type { JsonObject, SingleOrArray } from '../../../../types'\nimport { asArray, JsonTransformer, mapSingleOrArray } from '../../../../utils'\nimport { IsInstanceOrArrayOfInstances, IsUri } from '../../../../utils/validators'\nimport { CREDENTIALS_CONTEXT_V1_URL } from '../../constants'\nimport { IsCredentialJsonLdContext, IsCredentialType } from '../../validators'\nimport { W3cCredentialSchema } from './W3cCredentialSchema'\nimport { W3cCredentialStatus } from './W3cCredentialStatus'\nimport type { W3cCredentialSubjectOptions } from './W3cCredentialSubject'\nimport { IsW3cCredentialSubject, W3cCredentialSubject, W3cCredentialSubjectTransformer } from './W3cCredentialSubject'\nimport type { W3cIssuerOptions } from './W3cIssuer'\nimport { IsW3cIssuer, W3cIssuer, W3cIssuerTransformer } from './W3cIssuer'\n\nexport interface W3cCredentialOptions {\n context?: Array<string | JsonObject>\n id?: string\n type: Array<string>\n issuer: string | W3cIssuerOptions\n issuanceDate: string\n expirationDate?: string\n credentialSubject: SingleOrArray<W3cCredentialSubjectOptions>\n credentialStatus?: W3cCredentialStatus\n credentialSchema?: SingleOrArray<W3cCredentialSchema>\n}\n\nexport class W3cCredential {\n public constructor(options: W3cCredentialOptions) {\n if (options) {\n this.context = options.context ?? [CREDENTIALS_CONTEXT_V1_URL]\n this.id = options.id\n this.type = options.type || ['VerifiableCredential']\n this.issuer =\n typeof options.issuer === 'string' || options.issuer instanceof W3cIssuer\n ? options.issuer\n : new W3cIssuer(options.issuer)\n this.issuanceDate = options.issuanceDate\n this.expirationDate = options.expirationDate\n this.credentialSubject = mapSingleOrArray(options.credentialSubject, (subject) =>\n subject instanceof W3cCredentialSubject ? subject : new W3cCredentialSubject(subject)\n )\n\n if (options.credentialStatus) {\n this.credentialStatus =\n options.credentialStatus instanceof W3cCredentialStatus\n ? options.credentialStatus\n : new W3cCredentialStatus(options.credentialStatus)\n }\n\n if (options.credentialSchema) {\n this.credentialSchema = mapSingleOrArray(options.credentialSchema, (schema) =>\n schema instanceof W3cCredentialSchema ? schema : new W3cCredentialSchema(schema)\n )\n }\n }\n }\n\n @Expose({ name: '@context' })\n @IsCredentialJsonLdContext()\n public context!: Array<string | JsonObject>\n\n @IsOptional()\n @IsUri()\n public id?: string\n\n @IsCredentialType()\n public type!: Array<string>\n\n @W3cIssuerTransformer()\n @IsW3cIssuer()\n public issuer!: string | W3cIssuer\n\n @IsRFC3339()\n public issuanceDate!: string\n\n @IsRFC3339()\n @IsOptional()\n public expirationDate?: string\n\n @IsW3cCredentialSubject({ each: true })\n @W3cCredentialSubjectTransformer()\n public credentialSubject!: SingleOrArray<W3cCredentialSubject>\n\n @IsOptional()\n @Type(() => W3cCredentialSchema)\n @ValidateNested({ each: true })\n @IsInstanceOrArrayOfInstances({ classType: W3cCredentialSchema, allowEmptyArray: true })\n public credentialSchema?: SingleOrArray<W3cCredentialSchema>\n\n @IsOptional()\n @Type(() => W3cCredentialStatus)\n @ValidateNested({ each: true })\n @IsInstance(W3cCredentialStatus)\n public credentialStatus?: W3cCredentialStatus\n\n public get issuerId(): string {\n return this.issuer instanceof W3cIssuer ? this.issuer.id : this.issuer\n }\n\n public get credentialSchemaIds(): string[] {\n if (!this.credentialSchema) return []\n\n if (Array.isArray(this.credentialSchema)) {\n return this.credentialSchema.map((credentialSchema) => credentialSchema.id)\n }\n\n return [this.credentialSchema.id]\n }\n\n public get credentialSubjectIds(): string[] {\n if (Array.isArray(this.credentialSubject)) {\n return this.credentialSubject\n .map((credentialSubject) => credentialSubject.id)\n .filter((v): v is string => v !== undefined)\n }\n\n return this.credentialSubject.id ? [this.credentialSubject.id] : []\n }\n\n public get contexts(): Array<string | JsonObject> {\n return asArray(this.context)\n }\n\n public static fromJson(json: Record<string, unknown>) {\n return JsonTransformer.fromJSON(json, W3cCredential)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AA0BA,IAAa,gBAAb,MAAa,cAAc;CACzB,YAAmB,SAA+B;EAChD,IAAI,SAAS;GACX,KAAK,UAAU,QAAQ,WAAW,CAAA,wCAA2B;GAC7D,KAAK,KAAK,QAAQ;GAClB,KAAK,OAAO,QAAQ,QAAQ,CAAC,sBAAsB;GACnD,KAAK,SACH,OAAO,QAAQ,WAAW,YAAY,QAAQ,kBAAkB,YAC5D,QAAQ,SACR,IAAI,UAAU,QAAQ,MAAM;GAClC,KAAK,eAAe,QAAQ;GAC5B,KAAK,iBAAiB,QAAQ;GAC9B,KAAK,oBAAoB,iBAAiB,QAAQ,oBAAoB,YACpE,mBAAmB,uBAAuB,UAAU,IAAI,qBAAqB,OAAO,CACtF;GAEA,IAAI,QAAQ,kBACV,KAAK,mBACH,QAAQ,4BAA4B,sBAChC,QAAQ,mBACR,IAAI,oBAAoB,QAAQ,gBAAgB;GAGxD,IAAI,QAAQ,kBACV,KAAK,mBAAmB,iBAAiB,QAAQ,mBAAmB,WAClE,kBAAkB,sBAAsB,SAAS,IAAI,oBAAoB,MAAM,CACjF;EAEJ;CACF;CAwCA,IAAW,WAAmB;EAC5B,OAAO,KAAK,kBAAkB,YAAY,KAAK,OAAO,KAAK,KAAK;CAClE;CAEA,IAAW,sBAAgC;EACzC,IAAI,CAAC,KAAK,kBAAkB,OAAO,CAAC;EAEpC,IAAI,MAAM,QAAQ,KAAK,gBAAgB,GACrC,OAAO,KAAK,iBAAiB,KAAK,qBAAqB,iBAAiB,EAAE;EAG5E,OAAO,CAAC,KAAK,iBAAiB,EAAE;CAClC;CAEA,IAAW,uBAAiC;EAC1C,IAAI,MAAM,QAAQ,KAA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