import { StatusListCredentialFetcher, StatusListCredentialResponse, } from '../../resolvers/statusListCredentialFetcher.js'; import { DecodedVerifiableCredentialDto } from '../../shared/dto/decodedVerifiableCredential.dto.js'; import { InvalidStatusListCredentialException } from '../../statusList/exceptions/invalidStatusListCredentialException.js'; import { CredentialValidationResult, CredentialValidator, } from './credentialValidator.interface.js'; import { CredentialValidationTypes } from '../presentation/verifiablePresentationValidationReport.js'; import { StatusListReference } from '../../statusList/statusListReference.js'; import { StatusListDecoderFactory } from '../../statusList/statusListDecoderFactory.js'; export class RevocationValidator implements CredentialValidator { private result: CredentialValidationResult; constructor( private statusListCredentialFetcher: StatusListCredentialFetcher, ) { this.result = { valid: true, }; } async validate( credential: DecodedVerifiableCredentialDto, ): Promise { if (!credential.vc.credentialStatus && !credential.vc.status?.status_list) { return this.result; } const statusReference = StatusListReference.fromVc(credential.vc); const statusListResponse = await this.loadStatusListCredential(statusReference); const source = StatusListDecoderFactory.getDecoder( statusReference.getType(), ); const encodedList = source.getEncodedList(statusListResponse); try { if (encodedList.isRevoked(statusReference.getIndex())) { this.result = { valid: false, message: `${credential.vc.id} is revoked`, }; } return this.result; } catch (error) { return { valid: false, message: error.message, }; } } getValidationType(): CredentialValidationTypes { return CredentialValidationTypes.Revocation; } private async loadStatusListCredential( statusReference: StatusListReference, ): Promise { const statusListResponseData = await this.statusListCredentialFetcher.getStatusListCredential( statusReference.getUri(), ); if (!statusListResponseData) { throw new InvalidStatusListCredentialException(statusReference.getUri()); } return statusListResponseData; } }