import { RequestorBuilder , IRequestorAttestation , IRequestorPresentationExchange , IssuerMap , IssuanceAttestationsModel , IdTokenAttestationModel , VerifiablePresentationAttestationModel , ValidationError } from '../index' ; import { PresentationProtocol } from './RequestorBuilder' ; import { IRequestorResult } from './IRequestorResult' ; import { JoseBuilder } from 'verifiablecredentials-crypto-sdk-typescript' ; import ErrorHelpers from '../error_handling/ErrorHelpers' ; const errorCode = ( error ) => ErrorHelpers . errorCode ( 'VCSDKRequ' , error ) ; /**
 * Class to model the OIDC requestor
 */ export default class Requestor { private _payload : any = { } ; param _builder constructor ( private _builder ) { } /**
     * Gets the builder for the request
     */ public get builder ( ) : RequestorBuilder { return this . _builder ; } /**
     * Gets the payload for the request
     */ public get payload ( ) : any { return this . _payload ; } param state param nonce public async create ( state ? , nonce ? ) { const crypto = this . builder . crypto . builder ; this . _payload = { response_type : 'id_token' , response_mode : 'form_post' , client_id : this . builder . redirectUri , redirect_uri : this . builder . redirectUri , scope : 'openid did_authn' , state : state || this . builder . state , nonce : nonce || this . builder . nonce , iss : crypto . did , registration : { client_name : this . builder . clientName , client_purpose : this . builder . clientPurpose , tos_uri : this . builder . tosUri } } ; const current = Math . trunc ( Date . now ( ) / 1000 ) ; const iat = current ; let expiry = iat + this . builder . OidcRequestExpiry ; this . _payload . iat = iat ; this . _payload . exp = expiry ; const issuance = this . builder . issuance ; if ( issuance ) { this . _payload . prompt = 'create' ; } if ( this . builder . logoUri ) { this . _payload . registration . logo_uri = this . builder . logoUri ; } this . _payload = this . builder . presentationProtocol === PresentationProtocol . attestation ? this . createAttestationPresentationRequest ( this . _payload ) : this . createPresentationExchangeRequest ( this . _payload ) ; const key = crypto . signingKeyReference ; const signature = await crypto . build ( ) . signingProtocol ( JoseBuilder . JWT ) . sign ( this . _payload ) ; const token = await signature . serialize ( ) ; const response = { result : true , status : 200 , request : token } ; return response ; } /**
     * Create the presentation exchange request
     */ public createPresentationExchangeRequest ( payload ) { payload . presentation_definition = ( < IRequestorPresentationExchange > this . builder . requestor ) . presentationDefinition ; return payload ; } /**
     * Create the attestation presentation request
     */ public createAttestationPresentationRequest ( payload ) { payload . attestations = ( < IRequestorAttestation > this . builder . requestor ) . attestations ; return payload ; } /**
     * Get the audience url from the requestor
     */ public audienceUrl ( ) { return this . builder . redirectUri || this . builder . clientId ; } /**
     * Get the trusted issuer's configuration urls from the requestor
     */ public trustedIssuerConfigurationsForIdTokens ( ) { const attestations = ( < IRequestorAttestation > this . builder . requestor ) . attestations ; if ( attestations ) { if ( ! attestations . idTokens ) { return [ ] ; } else { const configurations = attestations . idTokens . map ( ( idToken ) => { return idToken . configuration ; } ) ; return < string [ ] > configurations . filter ( ( config ) => config ) ; } } else { throw new ValidationError ( `Id Tokens only supported in Attestation Requestor model.` , errorCode ( 1 ) ) ; } } /**
     * Get the trusted issuer's configuration urls from the requestor
     */ public trustedIssuersForVerifiableCredentials ( ) { const issuers = { } ; if ( this . isPresentationExchange ( ) ) { throw new ValidationError ( 'trustedIssuersForVerifiableCredentials not supported for presentation exchange. Requires constraints.' , errorCode ( 2 ) ) ; } else { const attestations = ( < IRequestorAttestation > this . builder . requestor ) . attestations ; if ( ! attestations . presentations ) { return { undefined : [ ] } ; } else { attestations . presentations . forEach ( ( presentation ) => { if ( ! presentation . credentialType ) { throw new ValidationError ( 'Missing credentialType for presentation.' , errorCode ( 3 ) ) ; } if ( ! issuers [ presentation . credentialType ] ) { issuers [ presentation . credentialType ] = [ ] ; } presentation . issuers ?. forEach ( ( issuer ) => issuers [ presentation . credentialType ! ] . push ( < string > issuer . iss ) ) ; } ) ; return issuers ; } } } public isPresentationExchange ( ) { return ( < IRequestorAttestation > this . builder . requestor ) . attestations === undefined ; } }