{"version":3,"file":"MdocService.mjs","names":[],"sources":["../../../src/modules/mdoc/MdocService.ts"],"sourcesContent":["import { AgentContext } from '../../agent'\nimport { injectable } from '../../plugins'\nimport type { Query, QueryOptions } from '../../storage/StorageService'\nimport { KeyManagementApi } from '../kms'\nimport { Mdoc } from './Mdoc'\nimport { MdocDeviceResponse } from './MdocDeviceResponse'\nimport type {\n  MdocDeleteVerificationSessionOptions,\n  MdocDeviceResponseDcqlQueryOptions,\n  MdocDeviceResponseOptions,\n  MdocDeviceResponsePresentationDefinitionOptions,\n  MdocDeviceResponseVerifyOptions,\n  MdocSignOptions,\n  MdocStoreOptions,\n  MdocVerifyOptions,\n} from './MdocOptions'\nimport {\n  MdocRecord,\n  MdocRepository,\n  type MdocVerificationSessionRecord,\n  MdocVerificationSessionRepository,\n} from './repository'\n\n/**\n * @internal\n */\n@injectable()\nexport class MdocService {\n  private mdocRepository: MdocRepository\n  private mdocVerificationSessionRepository: MdocVerificationSessionRepository\n\n  public constructor(\n    mdocRepository: MdocRepository,\n    mdocVerificationSessionRepository: MdocVerificationSessionRepository\n  ) {\n    this.mdocRepository = mdocRepository\n    this.mdocVerificationSessionRepository = mdocVerificationSessionRepository\n  }\n\n  public mdocFromBase64Url(hexEncodedMdoc: string) {\n    return Mdoc.fromBase64Url(hexEncodedMdoc)\n  }\n\n  public signMdoc(agentContext: AgentContext, options: MdocSignOptions) {\n    return Mdoc.sign(agentContext, options)\n  }\n\n  public async verifyMdoc(agentContext: AgentContext, mdoc: Mdoc, options: MdocVerifyOptions) {\n    return await mdoc.verify(agentContext, options)\n  }\n\n  public async createDeviceResponse(agentContext: AgentContext, options: MdocDeviceResponseOptions) {\n    return MdocDeviceResponse.createDeviceResponse(agentContext, options)\n  }\n\n  public async createPresentationDefinitionDeviceResponse(\n    agentContext: AgentContext,\n    options: MdocDeviceResponsePresentationDefinitionOptions\n  ) {\n    return MdocDeviceResponse.createDeviceResponseWithPresentationDefinition(agentContext, options)\n  }\n\n  /**\n   * @deprecated Credo does not use this method itself, and it only supports part of DCQL. Every claim\n   * of every `mso_mdoc` credential query is requested: `credential_sets`, `claim_sets` and `values`\n   * are ignored, credential queries with the same doctype are merged, nested claim paths are\n   * truncated, and a claim missing from the mdoc throws instead of being treated as optional. Use\n   * `DcqlService.createPresentation` to present mdocs for a DCQL query, or\n   * {@link MdocService.createDeviceResponse} with explicit `documentRequests`.\n   */\n  public async createDcqlQueryDeviceResponse(agentContext: AgentContext, options: MdocDeviceResponseDcqlQueryOptions) {\n    return MdocDeviceResponse.createDeviceResponseWithDcqlQuery(agentContext, options)\n  }\n\n  public async verifyDeviceResponse(agentContext: AgentContext, options: MdocDeviceResponseVerifyOptions) {\n    const deviceResponse = MdocDeviceResponse.fromBase64Url(options.deviceResponse)\n    return deviceResponse.verify(agentContext, options)\n  }\n\n  public async store(agentContext: AgentContext, options: MdocStoreOptions) {\n    await this.mdocRepository.save(agentContext, options.record)\n\n    return options.record\n  }\n\n  public async getById(agentContext: AgentContext, id: string): Promise<MdocRecord> {\n    return await this.mdocRepository.getById(agentContext, id)\n  }\n\n  public async getAll(agentContext: AgentContext): Promise<Array<MdocRecord>> {\n    return await this.mdocRepository.getAll(agentContext)\n  }\n\n  public async findByQuery(\n    agentContext: AgentContext,\n    query: Query<MdocRecord>,\n    queryOptions?: QueryOptions\n  ): Promise<Array<MdocRecord>> {\n    return await this.mdocRepository.findByQuery(agentContext, query, queryOptions)\n  }\n\n  public async deleteById(agentContext: AgentContext, id: string) {\n    await this.mdocRepository.deleteById(agentContext, id)\n  }\n\n  public async update(agentContext: AgentContext, mdocRecord: MdocRecord) {\n    await this.mdocRepository.update(agentContext, mdocRecord)\n  }\n\n  public async getVerificationSessionById(\n    agentContext: AgentContext,\n    verificationSessionId: string\n  ): Promise<MdocVerificationSessionRecord> {\n    return await this.mdocVerificationSessionRepository.getById(agentContext, verificationSessionId)\n  }\n\n  public async findVerificationSessionsByQuery(\n    agentContext: AgentContext,\n    query: Query<MdocVerificationSessionRecord>,\n    queryOptions?: QueryOptions\n  ): Promise<Array<MdocVerificationSessionRecord>> {\n    return await this.mdocVerificationSessionRepository.findByQuery(agentContext, query, queryOptions)\n  }\n\n  /**\n   * Delete a verification session, and by default the ephemeral session key it holds. The key is\n   * not deleted at any other point in the session lifecycle.\n   */\n  public async deleteVerificationSessionById(\n    agentContext: AgentContext,\n    verificationSessionId: string,\n    options?: MdocDeleteVerificationSessionOptions\n  ) {\n    const verificationSession = await this.mdocVerificationSessionRepository.getById(\n      agentContext,\n      verificationSessionId\n    )\n\n    const deleteAssociatedKey = options?.deleteAssociatedKey ?? true\n    if (deleteAssociatedKey) {\n      const kms = agentContext.resolve(KeyManagementApi)\n\n      // Returns false if the key was already gone, which is the state we want it in\n      await kms.deleteKey({ keyId: verificationSession.sessionKeyId })\n    }\n\n    await this.mdocVerificationSessionRepository.delete(agentContext, verificationSession)\n  }\n}\n"],"mappings":";;;;;;;;;;;;AA2BO,IAAM,cAAN,MAAM,YAAY;CAIvB,YACE,gBACA,mCACA;EACA,KAAK,iBAAiB;EACtB,KAAK,oCAAoC;CAC3C;CAEA,kBAAyB,gBAAwB;EAC/C,OAAO,KAAK,cAAc,cAAc;CAC1C;CAEA,SAAgB,cAA4B,SAA0B;EACpE,OAAO,KAAK,KAAK,cAAc,OAAO;CACxC;CAEA,MAAa,WAAW,cAA4B,MAAY,SAA4B;EAC1F,OAAO,MAAM,KAAK,OAAO,cAAc,OAAO;CAChD;CAEA,MAAa,qBAAqB,cAA4B,SAAoC;EAChG,OAAO,mBAAmB,qBAAqB,cAAc,OAAO;CACtE;CAEA,MAAa,2CACX,cACA,SACA;EACA,OAAO,mBAAmB,+CAA+C,cAAc,OAAO;CAChG;;;;;;;;;CAUA,MAAa,8BAA8B,cAA4B,SAA6C;EAClH,OAAO,mBAAmB,kCAAkC,cAAc,OAAO;CACnF;CAEA,MAAa,qBAAqB,cAA4B,SAA0C;EAEtG,OADuB,mBAAmB,cAAc,QAAQ,cAC5C,CAAC,CAAC,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