'use strict' ; import { FabricConnection } from './FabricConnection' ; import { FabricWallet } from 'ibm-blockchain-platform-wallet' ; import { IFabricGatewayConnection , OutputAdapter , LogType , ConnectionProfileUtil } from 'ibm-blockchain-platform-common' ; import { Network , Contract , Transaction , ContractListener } from 'fabric-network' ; import { Endorser } from 'fabric-common' ; import { EvaluateQueryHandler } from 'ibm-blockchain-platform-fabric-admin' ; export class FabricGatewayConnection extends FabricConnection implements IFabricGatewayConnection { private description : boolean ; constructor ( connectionProfilePath , outputAdapter ? ) { super ( connectionProfilePath , outputAdapter ) ; } async connect ( wallet , identityName , timeout ) { const connectionProfile = await ConnectionProfileUtil . readConnectionProfile ( this . connectionProfilePath ) ; if ( connectionProfile [ 'description' ] ) { this . description = ( connectionProfile [ 'description' ] . includes ( 'Network on IBP' ) ? true : false ) ; } else { this . description = false ; } await this . connectInner ( connectionProfile , wallet . getWallet ( ) , identityName , timeout ) ; } public isIBPConnection ( ) { return this . description ; } public async getMetadata ( instantiatedChaincodeName , channel ) { const network = await this . gateway . getNetwork ( channel ) ; const smartContract = network . getContract ( instantiatedChaincodeName ) ; let metadataBuffer ; try { metadataBuffer = await smartContract . evaluateTransaction ( 'org.hyperledger.fabric:GetMetadata' ) ; } catch ( error ) { throw new Error ( `Transaction function "org.hyperledger.fabric:GetMetadata" returned an error: ${ error . message }` ) ; } const metadataString = metadataBuffer . toString ( ) ; if ( ! metadataString ) { throw new Error ( `Transaction function "org.hyperledger.fabric:GetMetadata" did not return any metadata` ) ; } try { const metadataObject = JSON . parse ( metadataBuffer . toString ( ) ) ; return metadataObject ; } catch ( error ) { throw new Error ( `Transaction function "org.hyperledger.fabric:GetMetadata" did not return valid JSON metadata: ${ error . message }` ) ; } } public async submitTransaction ( chaincodeName , transactionName , channelName , args , namespace , transientData , evaluate ? , peerTargetNames = [ ] ) { const network = await this . gateway . getNetwork ( channelName ) ; const smartContract = network . getContract ( chaincodeName , namespace ) ; const transaction = smartContract . createTransaction ( transactionName ) ; if ( transientData ) { transaction . setTransient ( transientData ) ; } let peerTargets ; if ( peerTargetNames && peerTargetNames . length > 0 ) { peerTargets = await this . getChannelPeers ( channelName , peerTargetNames ) ; transaction . setEndorsingPeers ( peerTargets ) ; } let response ; if ( evaluate ) { const allAvailablePeers = EvaluateQueryHandler . getPeers ( ) ; EvaluateQueryHandler . setPeers ( peerTargets ) ; response = await transaction . evaluate ( ... args ) ; EvaluateQueryHandler . setPeers ( allAvailablePeers ) ; } else { response = await transaction . submit ( ... args ) ; } if ( response . buffer . byteLength === 0 ) { return undefined ; } else { const result = response . toString ( 'utf8' ) ; return result ; } } public async addContractListener ( channelName , contractName , eventName , outputAdapter ) { const network = await this . gateway . getNetwork ( channelName ) ; const contract = network . getContract ( contractName ) ; const listener = async ( event ) => { if ( event . eventName . match ( eventName ) ) { const eventString = `chaincodeId: ${ event . chaincodeId }, eventName: "${ event . eventName }", payload: ${ event . payload . toString ( ) }` ; outputAdapter . log ( LogType . INFO , undefined , `Event emitted: ${ eventString }` ) ; } } ; await contract . addContractListener ( listener ) ; } }