import { CryptoBuilder , KeyReference , LongFormDid , KeyUse , TokenType , ClaimToken , JoseBuilder } from '../lib/index' ; import RequestorHelper from './RequestorHelper' ; import TokenGenerator from './TokenGenerator' ; import ITestModel from './models/ITestModel' ; import { v4 as uuidv4 } from 'uuid' ; const jp = require ( 'jsonpath' ) ; const clone = require ( 'clone' ) ; export default class ResponderHelper { constructor ( public requestor , public responseDefinition ) { } public crypto = new CryptoBuilder ( ) . useSigningKeyReference ( new KeyReference ( 'signingResponder' ) ) . useRecoveryKeyReference ( new KeyReference ( 'recoveryKey' ) ) . useUpdateKeyReference ( new KeyReference ( 'updateKey' ) ) . build ( ) ; public vcPayload = { givenName : 'Jules' , familyName : 'Winnfield' } ; public generator = new TokenGenerator ( this ) ; public async setup ( signingProtocol = 'ES256K' ) { this . crypto . builder . useSigningAlgorithm ( signingProtocol ) ; this . crypto . builder . useRecoveryAlgorithm ( signingProtocol ) ; this . crypto . builder . useUpdateAlgorithm ( signingProtocol ) ; this . crypto = await this . crypto . generateKey ( KeyUse . Signature , 'signing' ) ; this . crypto = await this . crypto . generateKey ( KeyUse . Signature , 'recovery' ) ; this . crypto = await this . crypto . generateKey ( KeyUse . Signature , 'update' ) ; let did = signingProtocol === 'ES256K' ? await ( new LongFormDid ( this . crypto ) ) . serialize ( ) : 'did:test:responder' ; this . crypto . builder . useDid ( did ) ; TokenGenerator . mockResolver ( this . crypto ) ; await this . generator . setup ( signingProtocol ) ; } public async createResponse ( vcProtocol = JoseBuilder . JOSE ) { const payload = this . responseDefinition . response ; const preSignatureResponseOperations = this . responseDefinition . preSignatureResponseOperations ; if ( preSignatureResponseOperations ) { for ( let inx in preSignatureResponseOperations ) { jp . apply ( payload , preSignatureResponseOperations ! [ inx ] . path , preSignatureResponseOperations ! [ inx ] . operation ) ; } } await this . generator . setVcsInPresentations ( vcProtocol ) ; const presentations = ( < ITestModel > this . responseDefinition ) . getPresentationsFromModel ( ) ; if ( presentations ) { for ( let presentation in presentations ) { const jti = uuidv4 ( ) ; const vcs = presentations [ presentation ] ; const statusReceipts = { receipt : { } } ; this . responseDefinition . responseStatus [ presentation ] . credentialStatus . id = jti ; this . responseDefinition . responseStatus [ presentation ] . aud = this . requestor . crypto . builder . did ; this . responseDefinition . responseStatus [ presentation ] . iss = this . generator . crypto . builder . did ; await this . generator . crypto . signingProtocol ( JoseBuilder . JWT ) . sign ( this . responseDefinition . responseStatus [ presentation ] ) ; statusReceipts . receipt [ jti ] = await this . generator . crypto . signingProtocol ( JoseBuilder . JWT ) . serialize ( ) ; const statusUrl = vcs . decodedToken . vc ?. credentialStatus ?. id || vcs . decodedToken . verifiableCredential ?. credentialStatus ?. id ; TokenGenerator . fetchMock . post ( statusUrl , statusReceipts , { overwriteRoutes : true } ) ; console . log ( `Set mock for ${ statusUrl }` ) ; let vpPayload ; if ( vcProtocol === JoseBuilder . JSONLDProofs ) { const vc = clone ( presentations [ presentation ] . decodedToken ) ; presentations [ presentation ] = { verifiableCredential : vc } ; } else { vpPayload = { jti , vp : { '\@context' : [ 'https://www.w3.org/2018/credentials/v1' , 'https://www.w3.org/2018/credentials/examples/v1' ] , type : [ 'VerifiablePresentation' ] , verifiableCredential : [ vcs . rawToken ] , } , sub : `${ this . requestor . crypto . builder . did }` , iss : `${ this . crypto . builder . did }` , aud : `${ this . requestor . crypto . builder . did }` } ; await this . crypto . signingProtocol ( JoseBuilder . JWT ) . sign ( vpPayload ) ; presentations [ presentation ] = await this . crypto . signingProtocol ( JoseBuilder . JWT ) . serialize ( ) ; } } } await this . generator . setIdTokens ( ) ; if ( this . responseDefinition . preSiopResponseOperations ) { for ( let inx in this . responseDefinition . preSiopResponseOperations ) { jp . apply ( payload , this . responseDefinition ! . preSiopResponseOperations ! [ inx ] . path , this . responseDefinition ! . preSiopResponseOperations ! [ inx ] . operation ) ; } } const token = await ( await this . crypto . signingProtocol ( JoseBuilder . JOSE ) . sign ( payload ) ) . serialize ( ) ; return new ClaimToken ( TokenType . siopPresentationAttestation , token ) ; } }