import ErrorHelpers from '../error_handling/ErrorHelpers' ; import { IValidationResponse , IValidationOptions , TokenType , ValidatorBuilder , DidValidation , IExpectedStatusReceipt , ClaimToken } from '../index' ; const errorCode = ( error ) => ErrorHelpers . errorCode ( 'VCSDKVPSR' , error ) ; export interface IVerifiablePresentationStatus { id : string ; status : string ; reason : string ; passed : boolean ; token : ClaimToken ; } /**
 * Class to validate status receipts
 */ export default class VerifiablePresentationStatusReceipt { constructor ( public receipts , private validationBuilder , private options , private expected ) { this . _didValidation = new DidValidation ( this . options , this . expected ) ; } private _verifiablePresentationStatus : { [ jti ] : IVerifiablePresentationStatus ; } | undefined ; private _didValidation : DidValidation ; /**
     * Gets the result of the status
     */ public get verifiablePresentationStatus ( ) : { [ jti ] : IVerifiablePresentationStatus ; } | undefined { return this . _verifiablePresentationStatus ; } /**
     * Gets the DID validator
     */ public get didValidation ( ) : DidValidation { return this . _didValidation ; } /**
     * Sets the DID validator
     */ public set didValidation ( validator ) { this . _didValidation = validator ; } /**
     * The status validator
     */ public async validate ( ) { if ( ! this . receipts ?. receipt ) { return { result : false , status : this . options . validatorOptions . invalidTokenError , code : errorCode ( 1 ) , detailedError : 'The status receipt is missing receipt' } ; } this . _verifiablePresentationStatus = { } ; for ( let jti in this . receipts . receipt ) { const receipt = this . receipts . receipt [ jti ] ; const receiptResponse = await this . didValidation . validate ( receipt ) ; if ( ! receiptResponse . result ) { return receiptResponse ; } if ( receiptResponse . payloadObject . aud !== this . expected . didAudience ) { return { result : false , status : this . options . validatorOptions . invalidTokenError , detailedError : `The status receipt aud '${ receiptResponse . payloadObject . aud }' is wrong. Expected '${ this . expected . didAudience }'` } ; } if ( receiptResponse . issuer !== this . expected . didIssuer ) { return { result : false , status : this . options . validatorOptions . invalidTokenError , code : errorCode ( 2 ) , detailedError : `The status receipt iss '${ receiptResponse . issuer }' is wrong. Expected '${ this . expected . didIssuer }'` } ; } this . _verifiablePresentationStatus [ jti ] = < IVerifiablePresentationStatus > { id : jti , reason : receiptResponse . payloadObject . credentialStatus ?. reason , status : receiptResponse . payloadObject . credentialStatus ?. status , passed : receiptResponse . payloadObject . credentialStatus ?. status ?. toLowerCase ( ) === 'valid' , token : new ClaimToken ( TokenType . verifiablePresentationStatus , receipt ) } ; if ( ! this . verifiablePresentationStatus ! [ jti ] . passed ) { return { result : false , status : this . options . validatorOptions . invalidTokenError , code : errorCode ( 3 ) , detailedError : `The status receipt for jti '${ jti }' failed with status ${ this . verifiablePresentationStatus ! [ jti ] . status }.` } ; } } return { result : true , status : 200 , validationResult : { verifiablePresentationStatus : this . verifiablePresentationStatus } } ; } }