{"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 { W3cJsonLdVerifiableCredential } from './data-integrity'\nimport { W3cJsonLdVerifiablePresentation } from './data-integrity/models/W3cJsonLdVerifiablePresentation'\nimport { W3cJsonLdCredentialService } from './data-integrity/W3cJsonLdCredentialService'\nimport { W3cJwtVerifiableCredential, W3cJwtVerifiablePresentation } from './jwt-vc'\nimport { W3cJwtCredentialService } from './jwt-vc/W3cJwtCredentialService'\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":";;;;;;;;;;;;;;;;;;;;;;;;AAiCO,iCAAM,qBAAqB;CAKhC,AAAO,YACL,yBACA,4BACA,yBACA;AACA,OAAK,0BAA0B;AAC/B,OAAK,6BAA6B;AAClC,OAAK,0BAA0B;;;;;;;;CASjC,MAAa,eACX,cACA,SAC0C;AAC1C,MAAI,QAAQ,WAAW,YAAY,MAEjC,QADe,MAAM,KAAK,wBAAwB,eAAe,cAAc,QAAQ;AAGzF,MAAI,QAAQ,WAAW,YAAY,MAEjC,QADe,MAAM,KAAK,2BAA2B,eAAe,cAAc,QAAQ;AAG5F,QAAM,IAAI,WAAW,4EAA4E;;;;;CAMnG,MAAa,iBACX,cACA,SACoC;AACpC,MAAI,QAAQ,sBAAsB,8BAChC,QA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