'use strict' ; import * as path from 'path' ; import * as fs from 'fs-extra' ; import * as yaml from 'js-yaml' ; import { SettingConfigurations } from '../../extension/configurations' ; import { FabricNode , FileConfigurations , FileSystemUtil , FabricGatewayRegistryEntry } from 'ibm-blockchain-platform-common' ; export class FabricGatewayHelper { public static async getConnectionProfilePath ( gatewayRegistryEntry ) { return gatewayRegistryEntry . connectionProfilePath ; } public static async generateConnectionProfile ( gatewayName , peerNode , caNode ) { const connectionProfile = { name : gatewayName , version : '1.0.0' , wallet : peerNode . wallet , client : { organization : peerNode . msp_id , connection : { timeout : { peer : { endorser : '300' } , orderer : '300' } } } , organizations : { } , peers : { } , } ; connectionProfile . organizations [ peerNode . msp_id ] = { mspid : peerNode . msp_id , peers : [ peerNode . name ] } ; connectionProfile . peers [ peerNode . name ] = { url : peerNode . api_url } ; if ( peerNode . pem ) { connectionProfile . peers [ peerNode . name ] . tlsCACerts = { pem : Buffer . from ( peerNode . pem , 'base64' ) . toString ( ) } ; } if ( peerNode . ssl_target_name_override ) { connectionProfile . peers [ peerNode . name ] . grpcOptions = { 'ssl-target-name-override' : peerNode . ssl_target_name_override } ; } if ( caNode ) { connectionProfile . certificateAuthorities = { } ; connectionProfile . organizations [ peerNode . msp_id ] . certificateAuthorities = [ caNode . name ] ; connectionProfile . certificateAuthorities [ caNode . name ] = { url : caNode . api_url , caName : caNode . ca_name } ; if ( caNode . pem ) { connectionProfile . certificateAuthorities [ caNode . name ] . tlsCACerts = { pem : Buffer . from ( caNode . pem , 'base64' ) . toString ( ) } ; } } const extDir = SettingConfigurations . getExtensionDir ( ) ; const homeExtDir = FileSystemUtil . getDirPath ( extDir ) ; const profileDirPath = path . join ( homeExtDir , FileConfigurations . FABRIC_GATEWAYS , gatewayName ) ; await fs . ensureDir ( profileDirPath ) ; const profileFilePath = path . join ( profileDirPath , `${ this . profileName }.json` ) ; const connectionProfileString = JSON . stringify ( connectionProfile , null , 4 ) ; await fs . writeFile ( profileFilePath , connectionProfileString ) ; return profileFilePath ; } public static async copyConnectionProfile ( gatewayName , connectionProfilePath ) { try { const extDir = SettingConfigurations . getExtensionDir ( ) ; const homeExtDir = FileSystemUtil . getDirPath ( extDir ) ; const profileDirPath = path . join ( homeExtDir , FileConfigurations . FABRIC_GATEWAYS , gatewayName ) ; await fs . ensureDir ( profileDirPath ) ; const connectionProfileFile = await fs . readFile ( connectionProfilePath , 'utf8' ) ; let connectionProfile ; let profilePath ; if ( connectionProfilePath . endsWith ( '.json' ) ) { connectionProfile = JSON . parse ( connectionProfileFile ) ; profilePath = path . join ( profileDirPath , `${ this . profileName }.json` ) ; } else { connectionProfile = yaml . safeLoad ( connectionProfileFile ) ; profilePath = path . join ( profileDirPath , `${ this . profileName }.yml` ) ; } const properties = [ 'peers' , 'orderers' , 'certificateAuthorities' ] ; for ( const property of properties ) { if ( connectionProfile [ property ] ) { for ( const item of Object . keys ( connectionProfile [ property ] ) ) { if ( connectionProfile [ property ] [ item ] . tlsCACerts && connectionProfile [ property ] [ item ] . tlsCACerts . path !== undefined ) { let tlsPath ; if ( path . isAbsolute ( connectionProfile [ property ] [ item ] . tlsCACerts . path ) ) { tlsPath = connectionProfile [ property ] [ item ] . tlsCACerts . path ; } else { tlsPath = path . resolve ( path . dirname ( connectionProfilePath ) , connectionProfile [ property ] [ item ] . tlsCACerts . path ) ; } const tlsCertData = await fs . readFile ( tlsPath , 'utf8' ) ; connectionProfile [ property ] [ item ] . tlsCACerts . pem = tlsCertData ; delete connectionProfile [ property ] [ item ] . tlsCACerts . path ; } } } } let connectionProfileData ; if ( connectionProfilePath . endsWith ( '.json' ) ) { connectionProfileData = JSON . stringify ( connectionProfile , null , 4 ) ; } else { connectionProfileData = yaml . dump ( connectionProfile ) ; } await fs . writeFile ( profilePath , connectionProfileData ) ; return profilePath ; } catch ( error ) { const newError = new Error ( `Unable to copy connection profile: ${ error . message }` ) ; throw newError ; } } private static profileName : string = 'connection' ; }