{"version":3,"file":"W3cV2CredentialService.mjs","names":[],"sources":["../../../src/modules/vc/W3cV2CredentialService.ts"],"sourcesContent":["import type { AgentContext } from '../../agent/context'\nimport { CredoError } from '../../error'\nimport { injectable } from '../../plugins'\nimport type { Query, QueryOptions } from '../../storage/StorageService'\nimport { W3cV2JwtVerifiableCredential, W3cV2JwtVerifiablePresentation } from './jwt-vc'\nimport { W3cV2JwtCredentialService } from './jwt-vc/W3cV2JwtCredentialService'\nimport type { W3cV2VerifiableCredential, W3cV2VerifyCredentialResult, W3cV2VerifyPresentationResult } from './models'\nimport { ClaimFormat } from './models'\nimport type { W3cV2VerifiablePresentation } from './models/presentation/W3cV2VerifiablePresentation'\nimport { W3cV2CredentialRecord, W3cV2CredentialRepository } from './repository'\nimport {\n  W3cV2SdJwtCredentialService,\n  W3cV2SdJwtVerifiableCredential,\n  W3cV2SdJwtVerifiablePresentation,\n} from './sd-jwt-vc'\nimport type {\n  W3cV2JwtVerifyCredentialOptions,\n  W3cV2JwtVerifyPresentationOptions,\n  W3cV2SdJwtVerifyCredentialOptions,\n  W3cV2SdJwtVerifyPresentationOptions,\n  W3cV2SignCredentialOptions,\n  W3cV2SignPresentationOptions,\n  W3cV2StoreCredentialOptions,\n  W3cV2VerifyCredentialOptions,\n  W3cV2VerifyPresentationOptions,\n} from './W3cV2CredentialServiceOptions'\n\n@injectable()\nexport class W3cV2CredentialService {\n  private w3cV2CredentialRepository: W3cV2CredentialRepository\n  private w3cV2SdJwtCredentialService: W3cV2SdJwtCredentialService\n  private w3cV2JwtCredentialService: W3cV2JwtCredentialService\n\n  public constructor(\n    w3cV2CredentialRepository: W3cV2CredentialRepository,\n    w3cV2SdJwtCredentialService: W3cV2SdJwtCredentialService,\n    w3cV2JwtCredentialService: W3cV2JwtCredentialService\n  ) {\n    this.w3cV2CredentialRepository = w3cV2CredentialRepository\n    this.w3cV2SdJwtCredentialService = w3cV2SdJwtCredentialService\n    this.w3cV2JwtCredentialService = w3cV2JwtCredentialService\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.JwtW3cVc | ClaimFormat.SdJwtW3cVc>(\n    agentContext: AgentContext,\n    options: W3cV2SignCredentialOptions<Format>\n  ): Promise<W3cV2VerifiableCredential<Format>> {\n    if (options.format === ClaimFormat.JwtW3cVc) {\n      const signed = await this.w3cV2JwtCredentialService.signCredential(agentContext, options)\n      return signed as W3cV2VerifiableCredential<Format>\n    }\n    if (options.format === ClaimFormat.SdJwtW3cVc) {\n      const signed = await this.w3cV2SdJwtCredentialService.signCredential(agentContext, options)\n      return signed as W3cV2VerifiableCredential<Format>\n    }\n    throw new CredoError(`Unsupported format in options. Format must be either 'vc+jwt' or 'vc+sd-jwt'`)\n  }\n\n  /**\n   * Verifies the signature(s) of a credential\n   */\n  public async verifyCredential(\n    agentContext: AgentContext,\n    options: W3cV2VerifyCredentialOptions\n  ): Promise<W3cV2VerifyCredentialResult> {\n    if (options.credential instanceof W3cV2JwtVerifiableCredential) {\n      return this.w3cV2JwtCredentialService.verifyCredential(agentContext, options as W3cV2JwtVerifyCredentialOptions)\n    }\n    if (options.credential instanceof W3cV2SdJwtVerifiableCredential) {\n      return this.w3cV2SdJwtCredentialService.verifyCredential(\n        agentContext,\n        options as W3cV2SdJwtVerifyCredentialOptions\n      )\n    }\n    throw new CredoError(\n      'Unsupported credential type in options. Credential must be either a W3cV2JwtVerifiablePresentation or a W3cV2SdJwtVerifiablePresentation'\n    )\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.JwtW3cVp | ClaimFormat.SdJwtW3cVp>(\n    agentContext: AgentContext,\n    options: W3cV2SignPresentationOptions<Format>\n  ): Promise<W3cV2VerifiablePresentation<Format>> {\n    if (options.format === ClaimFormat.JwtW3cVp) {\n      const signed = await this.w3cV2JwtCredentialService.signPresentation(agentContext, options)\n      return signed as W3cV2VerifiablePresentation<Format>\n    }\n    if (options.format === ClaimFormat.SdJwtW3cVp) {\n      const signed = await this.w3cV2SdJwtCredentialService.signPresentation(agentContext, options)\n      return signed as W3cV2VerifiablePresentation<Format>\n    }\n    throw new CredoError(`Unsupported format in options. Format must be either 'vp+jwt' or 'vp+sd-jwt'`)\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: W3cV2VerifyPresentationOptions\n  ): Promise<W3cV2VerifyPresentationResult> {\n    if (options.presentation instanceof W3cV2JwtVerifiablePresentation) {\n      return this.w3cV2JwtCredentialService.verifyPresentation(\n        agentContext,\n        options as W3cV2JwtVerifyPresentationOptions\n      )\n    }\n    if (options.presentation instanceof W3cV2SdJwtVerifiablePresentation) {\n      return this.w3cV2SdJwtCredentialService.verifyPresentation(\n        agentContext,\n        options as W3cV2SdJwtVerifyPresentationOptions\n      )\n    }\n    throw new CredoError(\n      'Unsupported credential type in options. Presentation must be either a W3cV2JwtVerifiablePresentation or a W3cV2SdJwtVerifiablePresentation'\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: W3cV2StoreCredentialOptions\n  ): Promise<W3cV2CredentialRecord> {\n    // Store the w3cV2 credential record\n    await this.w3cV2CredentialRepository.save(agentContext, options.record)\n\n    return options.record\n  }\n\n  public async removeCredentialRecord(agentContext: AgentContext, id: string) {\n    await this.w3cV2CredentialRepository.deleteById(agentContext, id)\n  }\n\n  public async getAllCredentialRecords(agentContext: AgentContext): Promise<W3cV2CredentialRecord[]> {\n    return await this.w3cV2CredentialRepository.getAll(agentContext)\n  }\n\n  public async getCredentialRecordById(agentContext: AgentContext, id: string): Promise<W3cV2CredentialRecord> {\n    return await this.w3cV2CredentialRepository.getById(agentContext, id)\n  }\n\n  public async findCredentialsByQuery(\n    agentContext: AgentContext,\n    query: Query<W3cV2CredentialRecord>,\n    queryOptions?: QueryOptions\n  ): Promise<W3cV2VerifiableCredential[]> {\n    const result = await this.w3cV2CredentialRepository.findByQuery(agentContext, query, queryOptions)\n    return result.map((record) => record.firstCredential)\n  }\n\n  public async findCredentialRecordByQuery(\n    agentContext: AgentContext,\n    query: Query<W3cV2CredentialRecord>\n  ): Promise<W3cV2VerifiableCredential | undefined> {\n    const result = await this.w3cV2CredentialRepository.findSingleByQuery(agentContext, query)\n    return result?.firstCredential\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AA4BO,mCAAM,uBAAuB;CAKlC,AAAO,YACL,2BACA,6BACA,2BACA;AACA,OAAK,4BAA4B;AACjC,OAAK,8BAA8B;AACnC,OAAK,4BAA4B;;;;;;;;CASnC,MAAa,eACX,cACA,SAC4C;AAC5C,MAAI,QAAQ,WAAW,YAAY,SAEjC,QADe,MAAM,KAAK,0BAA0B,eAAe,cAAc,QAAQ;AAG3F,MAAI,QAAQ,WAAW,YAAY,WAEjC,QADe,MAAM,KAAK,4BAA4B,eAAe,cAAc,QAAQ;AAG7F,QAAM,IAAI,WAAW,+EAA+E;;;;;CAMtG,MAAa,iBACX,cACA,SACsC;AACtC,MAAI,QAAQ,sBAAsB,6BAChC,QAAO,KAAK,0BAA0B,iBAAiB,cAAc,QAA2C;AAElH,MAAI,QAAQ,sBAAsB,+BAChC,QAAO,KAAK,4BAA4B,iBACtC,cACA,QACD;AAEH,QAAM,IAAI,WACR,2IACD;;;;;;;;CASH,MAAa,iBACX,cACA,SAC8C;AAC9C,MAAI,QAAQ,WAAW,YAAY,SAEjC,QADe,MAAM,KAAK,0BAA0B,iBAAiB,cAAc,QAAQ;AAG7F,MAAI,QAAQ,WAAW,YAAY,WAEjC,QADe,MAAM,KAAK,4BAA4B,iBAAiB,cAAc,QAAQ;AAG/F,QAAM,IAAI,WAAW,+EAA+E;;;;;;;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