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

104 lines (100 loc) • 5.64 kB
import { CredoError } from "../../error/CredoError.mjs"; import "../../error/index.mjs"; import { injectable } from "../../plugins/index.mjs"; import { __decorateMetadata } from "../../_virtual/_@oxc-project_runtime@0.112.0/helpers/decorateMetadata.mjs"; import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.112.0/helpers/decorate.mjs"; import { ClaimFormat } from "./models/ClaimFormat.mjs"; import { W3cV2JwtVerifiableCredential } from "./jwt-vc/W3cV2JwtVerifiableCredential.mjs"; import { W3cV2SdJwtVerifiableCredential } from "./sd-jwt-vc/W3cV2SdJwtVerifiableCredential.mjs"; import { W3cV2SdJwtVerifiablePresentation } from "./sd-jwt-vc/W3cV2SdJwtVerifiablePresentation.mjs"; import { W3cV2SdJwtCredentialService } from "./sd-jwt-vc/W3cV2SdJwtCredentialService.mjs"; import "./sd-jwt-vc/index.mjs"; import { W3cV2JwtVerifiablePresentation } from "./jwt-vc/W3cV2JwtVerifiablePresentation.mjs"; import { W3cV2JwtCredentialService } from "./jwt-vc/W3cV2JwtCredentialService.mjs"; import "./jwt-vc/index.mjs"; import "./models/index.mjs"; import { W3cV2CredentialRepository } from "./repository/W3cV2CredentialRepository.mjs"; import "./repository/index.mjs"; //#region src/modules/vc/W3cV2CredentialService.ts var _ref, _ref2, _ref3; let W3cV2CredentialService = class W3cV2CredentialService { constructor(w3cV2CredentialRepository, w3cV2SdJwtCredentialService, w3cV2JwtCredentialService) { this.w3cV2CredentialRepository = w3cV2CredentialRepository; this.w3cV2SdJwtCredentialService = w3cV2SdJwtCredentialService; this.w3cV2JwtCredentialService = w3cV2JwtCredentialService; } /** * Signs a credential * * @param credential the credential to be signed * @returns the signed credential */ async signCredential(agentContext, options) { if (options.format === ClaimFormat.JwtW3cVc) return await this.w3cV2JwtCredentialService.signCredential(agentContext, options); if (options.format === ClaimFormat.SdJwtW3cVc) return await this.w3cV2SdJwtCredentialService.signCredential(agentContext, options); throw new CredoError(`Unsupported format in options. Format must be either 'vc+jwt' or 'vc+sd-jwt'`); } /** * Verifies the signature(s) of a credential */ async verifyCredential(agentContext, options) { if (options.credential instanceof W3cV2JwtVerifiableCredential) return this.w3cV2JwtCredentialService.verifyCredential(agentContext, options); if (options.credential instanceof W3cV2SdJwtVerifiableCredential) return this.w3cV2SdJwtCredentialService.verifyCredential(agentContext, options); throw new CredoError("Unsupported credential type in options. Credential must be either a W3cV2JwtVerifiablePresentation or a W3cV2SdJwtVerifiablePresentation"); } /** * Signs a presentation including the credentials it includes * * @param presentation the presentation to be signed * @returns the signed presentation */ async signPresentation(agentContext, options) { if (options.format === ClaimFormat.JwtW3cVp) return await this.w3cV2JwtCredentialService.signPresentation(agentContext, options); if (options.format === ClaimFormat.SdJwtW3cVp) return await this.w3cV2SdJwtCredentialService.signPresentation(agentContext, options); throw new CredoError(`Unsupported format in options. Format must be either 'vp+jwt' or 'vp+sd-jwt'`); } /** * Verifies a presentation including the credentials it includes * * @param presentation the presentation to be verified * @returns the verification result */ async verifyPresentation(agentContext, options) { if (options.presentation instanceof W3cV2JwtVerifiablePresentation) return this.w3cV2JwtCredentialService.verifyPresentation(agentContext, options); if (options.presentation instanceof W3cV2SdJwtVerifiablePresentation) return this.w3cV2SdJwtCredentialService.verifyPresentation(agentContext, options); throw new CredoError("Unsupported credential type in options. Presentation must be either a W3cV2JwtVerifiablePresentation or a W3cV2SdJwtVerifiablePresentation"); } /** * Writes a credential to storage * * @param record the credential to be stored * @returns the credential record that was written to storage */ async storeCredential(agentContext, options) { await this.w3cV2CredentialRepository.save(agentContext, options.record); return options.record; } async removeCredentialRecord(agentContext, id) { await this.w3cV2CredentialRepository.deleteById(agentContext, id); } async getAllCredentialRecords(agentContext) { return await this.w3cV2CredentialRepository.getAll(agentContext); } async getCredentialRecordById(agentContext, id) { return await this.w3cV2CredentialRepository.getById(agentContext, id); } async findCredentialsByQuery(agentContext, query, queryOptions) { return (await this.w3cV2CredentialRepository.findByQuery(agentContext, query, queryOptions)).map((record) => record.firstCredential); } async findCredentialRecordByQuery(agentContext, query) { return (await this.w3cV2CredentialRepository.findSingleByQuery(agentContext, query))?.firstCredential; } }; W3cV2CredentialService = __decorate([injectable(), __decorateMetadata("design:paramtypes", [ typeof (_ref = typeof W3cV2CredentialRepository !== "undefined" && W3cV2CredentialRepository) === "function" ? _ref : Object, typeof (_ref2 = typeof W3cV2SdJwtCredentialService !== "undefined" && W3cV2SdJwtCredentialService) === "function" ? _ref2 : Object, typeof (_ref3 = typeof W3cV2JwtCredentialService !== "undefined" && W3cV2JwtCredentialService) === "function" ? _ref3 : Object ])], W3cV2CredentialService); //#endregion export { W3cV2CredentialService }; //# sourceMappingURL=W3cV2CredentialService.mjs.map