@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 • 10.3 kB
Source Map (JSON)
{"version":3,"file":"W3cCredentialService.mjs","names":[],"sources":["../../../src/modules/vc/W3cCredentialService.ts"],"sourcesContent":["import type { AgentContext } from '../../agent/context'\nimport { CredoError } from '../../error'\nimport { injectable } from '../../plugins'\nimport type { Query, QueryOptions } from '../../storage/StorageService'\nimport { CREDENTIALS_CONTEXT_V1_URL } from './constants'\nimport { W3cJwtVerifiableCredential, W3cJwtVerifiablePresentation } from './jwt-vc'\nimport { W3cJwtCredentialService } from './jwt-vc/W3cJwtCredentialService'\nimport {\n W3cJsonLdCredentialService,\n W3cJsonLdVerifiableCredential,\n W3cJsonLdVerifiablePresentation,\n} from './linked-data-proofs'\nimport type {\n W3cVerifiableCredential,\n W3cVerifiablePresentation,\n W3cVerifyCredentialResult,\n W3cVerifyPresentationResult,\n} from './models'\nimport { ClaimFormat } from './models'\nimport { W3cPresentation } from './models/presentation/W3cPresentation'\nimport { W3cCredentialRecord, W3cCredentialRepository } from './repository'\nimport type {\n W3cCreatePresentationOptions,\n W3cJsonLdVerifyCredentialOptions,\n W3cJsonLdVerifyPresentationOptions,\n W3cJwtVerifyCredentialOptions,\n W3cJwtVerifyPresentationOptions,\n W3cSignCredentialOptions,\n W3cSignPresentationOptions,\n W3cStoreCredentialOptions,\n W3cVerifyCredentialOptions,\n W3cVerifyPresentationOptions,\n} from './W3cCredentialServiceOptions'\n\n@injectable()\nexport class W3cCredentialService {\n private w3cCredentialRepository: W3cCredentialRepository\n private w3cJsonLdCredentialService: W3cJsonLdCredentialService\n private w3cJwtCredentialService: W3cJwtCredentialService\n\n public constructor(\n w3cCredentialRepository: W3cCredentialRepository,\n w3cJsonLdCredentialService: W3cJsonLdCredentialService,\n w3cJwtCredentialService: W3cJwtCredentialService\n ) {\n this.w3cCredentialRepository = w3cCredentialRepository\n this.w3cJsonLdCredentialService = w3cJsonLdCredentialService\n this.w3cJwtCredentialService = w3cJwtCredentialService\n }\n\n /**\n * Signs a credential\n *\n * @param credential the credential to be signed\n * @returns the signed credential\n */\n public async signCredential<Format extends ClaimFormat.JwtVc | ClaimFormat.LdpVc>(\n agentContext: AgentContext,\n options: W3cSignCredentialOptions<Format>\n ): Promise<W3cVerifiableCredential<Format>> {\n if (options.format === ClaimFormat.JwtVc) {\n const signed = await this.w3cJwtCredentialService.signCredential(agentContext, options)\n return signed as W3cVerifiableCredential<Format>\n }\n if (options.format === ClaimFormat.LdpVc) {\n const signed = await this.w3cJsonLdCredentialService.signCredential(agentContext, options)\n return signed as W3cVerifiableCredential<Format>\n }\n throw new CredoError(`Unsupported format in options. Format must be either 'jwt_vc' or 'ldp_vc'`)\n }\n\n /**\n * Verifies the signature(s) of a credential\n */\n public async verifyCredential(\n agentContext: AgentContext,\n options: W3cVerifyCredentialOptions\n ): Promise<W3cVerifyCredentialResult> {\n if (options.credential instanceof W3cJsonLdVerifiableCredential) {\n return this.w3cJsonLdCredentialService.verifyCredential(agentContext, options as W3cJsonLdVerifyCredentialOptions)\n }\n if (options.credential instanceof W3cJwtVerifiableCredential || typeof options.credential === 'string') {\n return this.w3cJwtCredentialService.verifyCredential(agentContext, options as W3cJwtVerifyCredentialOptions)\n }\n throw new CredoError(\n 'Unsupported credential type in options. Credential must be either a W3cJsonLdVerifiableCredential or a W3cJwtVerifiableCredential'\n )\n }\n\n /**\n * Utility method that creates a {@link W3cPresentation} from one or more {@link W3cJsonLdVerifiableCredential}s.\n *\n * **NOTE: the presentation that is returned is unsigned.**\n *\n * @returns An instance of {@link W3cPresentation}\n */\n public async createPresentation(options: W3cCreatePresentationOptions): Promise<W3cPresentation> {\n const presentation = new W3cPresentation({\n context: [CREDENTIALS_CONTEXT_V1_URL],\n type: ['VerifiablePresentation'],\n verifiableCredential: options.credentials,\n holder: options.holder,\n id: options.id,\n })\n\n return presentation\n }\n\n /**\n * Signs a presentation including the credentials it includes\n *\n * @param presentation the presentation to be signed\n * @returns the signed presentation\n */\n public async signPresentation<Format extends ClaimFormat.JwtVp | ClaimFormat.LdpVp>(\n agentContext: AgentContext,\n options: W3cSignPresentationOptions<Format>\n ): Promise<W3cVerifiablePresentation<Format>> {\n if (options.format === ClaimFormat.JwtVp) {\n const signed = await this.w3cJwtCredentialService.signPresentation(agentContext, options)\n return signed as W3cVerifiablePresentation<Format>\n }\n if (options.format === ClaimFormat.LdpVp) {\n const signed = await this.w3cJsonLdCredentialService.signPresentation(agentContext, options)\n return signed as W3cVerifiablePresentation<Format>\n }\n throw new CredoError(`Unsupported format in options. Format must be either 'jwt_vp' or 'ldp_vp'`)\n }\n\n /**\n * Verifies a presentation including the credentials it includes\n *\n * @param presentation the presentation to be verified\n * @returns the verification result\n */\n public async verifyPresentation(\n agentContext: AgentContext,\n options: W3cVerifyPresentationOptions\n ): Promise<W3cVerifyPresentationResult> {\n if (options.presentation instanceof W3cJsonLdVerifiablePresentation) {\n return this.w3cJsonLdCredentialService.verifyPresentation(\n agentContext,\n options as W3cJsonLdVerifyPresentationOptions\n )\n }\n if (options.presentation instanceof W3cJwtVerifiablePresentation || typeof options.presentation === 'string') {\n return this.w3cJwtCredentialService.verifyPresentation(agentContext, options as W3cJwtVerifyPresentationOptions)\n }\n throw new CredoError(\n 'Unsupported credential type in options. Presentation must be either a W3cJsonLdVerifiablePresentation or a W3cJwtVerifiablePresentation'\n )\n }\n\n /**\n * Writes a credential to storage\n *\n * @param record the credential to be stored\n * @returns the credential record that was written to storage\n */\n public async storeCredential(\n agentContext: AgentContext,\n options: W3cStoreCredentialOptions\n ): Promise<W3cCredentialRecord> {\n const credential = options.record.firstCredential\n\n // JsonLd credentials need expanded types to be stored.\n if (credential instanceof W3cJsonLdVerifiableCredential && !options.record.getTag('expandedTypes')) {\n options.record.setTag(\n 'expandedTypes',\n await this.w3cJsonLdCredentialService.getExpandedTypesForCredential(agentContext, credential)\n )\n }\n\n // Store the w3c credential record\n await this.w3cCredentialRepository.save(agentContext, options.record)\n\n return options.record\n }\n\n public async removeCredentialRecord(agentContext: AgentContext, id: string) {\n await this.w3cCredentialRepository.deleteById(agentContext, id)\n }\n\n public async getAllCredentialRecords(agentContext: AgentContext): Promise<W3cCredentialRecord[]> {\n return await this.w3cCredentialRepository.getAll(agentContext)\n }\n\n public async getCredentialRecordById(agentContext: AgentContext, id: string): Promise<W3cCredentialRecord> {\n return await this.w3cCredentialRepository.getById(agentContext, id)\n }\n\n public async findCredentialsByQuery(\n agentContext: AgentContext,\n query: Query<W3cCredentialRecord>,\n queryOptions?: QueryOptions\n ): Promise<W3cVerifiableCredential[]> {\n const result = await this.w3cCredentialRepository.findByQuery(agentContext, query, queryOptions)\n return result.map((record) => record.firstCredential)\n }\n\n public async findCredentialRecordByQuery(\n agentContext: AgentContext,\n query: Query<W3cCredentialRecord>\n ): Promise<W3cVerifiableCredential | undefined> {\n const result = await this.w3cCredentialRepository.findSingleByQuery(agentContext, query)\n return result?.firstCredential\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAmCO,IAAM,uBAAN,MAAM,qBAAqB;CAKhC,YACE,yBACA,4BACA,yBACA;EACA,KAAK,0BAA0B;EAC/B,KAAK,6BAA6B;EAClC,KAAK,0BAA0B;CACjC;;;;;;;CAQA,MAAa,eACX,cACA,SAC0C;EAC1C,IAAI,QAAQ,WAAA,UAEV,OAAO,MADc,KAAK,wBAAwB,eAAe,cAAc,OAAO;EAGxF,IAAI,QAAQ,WAAA,UAEV,OAAO,MADc,KAAK,2BAA2B,eAAe,cAAc,OAAO;EAG3F,MAAM,IAAI,WAAW,2EAA2E;CAClG;;;;CAKA,MAAa,iBACX,cACA,SACoC;EACpC,IAAI,QAAQ,sBAAsB,+BAChC,OAAO,KAAK,2BAA2B,iBAAiB,cAAc,OAA2C;EAEnH,IAAI,QAAQ,sBAAsB,8BAA8B,O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