import { VerifiablePresentationValidationResponse , IVerifiablePresentationValidation } from './VerifiablePresentationValidationResponse' ; import { IValidationOptions } from '../options/IValidationOptions' ; import { DidValidation } from './DidValidation' ; import VerifiableCredentialConstants from '../verifiable_credential/VerifiableCredentialConstants' ; import { IExpectedVerifiablePresentation } from '../index' ; import ErrorHelpers from '../error_handling/ErrorHelpers' ; const errorCode = ( error ) => ErrorHelpers . errorCode ( 'VCSDKVPVa' , error ) ; require ( 'es6-promise' ) . polyfill ( ) ; require ( 'isomorphic-fetch' ) ; /**
 * Class for verifiable presentation validation
 */ export class VerifiablePresentationValidation implements IVerifiablePresentationValidation { param options param expected param siopDid constructor ( private options , private expected , private siopDid , private id ) { } param verifiablePresentationToken param siopDid returns public async validate ( verifiablePresentationToken ) { let validationResponse = { result : true , detailedError : '' , status : 200 } ; const didValidation = new DidValidation ( this . options , this . expected ) ; validationResponse = await didValidation . validate ( verifiablePresentationToken ) ; if ( ! validationResponse . result ) { return validationResponse ; } validationResponse = await this . options . checkScopeValidityOnVpTokenDelegate ( validationResponse , this . expected , this . siopDid ) ; if ( ! validationResponse . result ) { return validationResponse ; } if ( this . siopDid && validationResponse . did !== this . siopDid ) { return { result : false , code : errorCode ( 1 ) , detailedError : `The DID used for the SIOP ${ this . siopDid } is not equal to the DID used for the verifiable presentation ${ validationResponse . did }` , status : this . options . validatorOptions . invalidTokenError } ; } if ( ! validationResponse . payloadObject . vp ) { return { result : false , status : this . options . validatorOptions . invalidTokenError , code : errorCode ( 2 ) , detailedError : `Missing vp in presentation` } ; } if ( ! validationResponse . payloadObject . vp [ '@context' ] ) { return { result : false , status : this . options . validatorOptions . invalidTokenError , code : errorCode ( 3 ) , detailedError : `Missing @context in presentation` } ; } if ( ! validationResponse . payloadObject . vp . type || validationResponse . payloadObject . vp . type [ 0 ] !== VerifiableCredentialConstants . DEFAULT_VERIFIABLEPRESENTATION_TYPE ) { return { result : false , status : this . options . validatorOptions . invalidTokenError , code : errorCode ( 3 ) , detailedError : `Missing or wrong default type in vp of presentation. Should be ${ VerifiableCredentialConstants . DEFAULT_VERIFIABLEPRESENTATION_TYPE }` } ; } if ( ! validationResponse . payloadObject . vp [ 'verifiableCredential' ] ) { return { result : false , status : this . options . validatorOptions . invalidTokenError , code : errorCode ( 4 ) , detailedError : `Missing verifiableCredential in presentation` } ; } return validationResponse ; } }