'use strict' ; import * as vscode from 'vscode' ; import { UserInputUtil , IBlockchainQuickPickItem } from './UserInputUtil' ; import { FabricConnectionFactory } from '../fabric/FabricConnectionFactory' ; import { FabricGatewayConnectionManager } from '../fabric/FabricGatewayConnectionManager' ; import { Reporter } from '../util/Reporter' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { FabricWalletRegistry , FabricWalletRegistryEntry , IFabricWalletGenerator , FabricGatewayRegistryEntry , FabricWalletGeneratorFactory , IFabricGatewayConnection , IFabricWallet , LogType , FabricEnvironmentRegistry , FabricEnvironmentRegistryEntry , FabricIdentity , EnvironmentType } from 'ibm-blockchain-platform-common' ; import { ExtensionUtil } from '../util/ExtensionUtil' ; import { SettingConfigurations } from '../configurations' ; import { FabricGatewayHelper } from '../fabric/FabricGatewayHelper' ; import { EnvironmentFactory } from '../fabric/environments/EnvironmentFactory' ; import { LocalMicroEnvironment } from '../fabric/environments/LocalMicroEnvironment' ; import { FabricWalletHelper } from '../fabric/FabricWalletHelper' ; export async function gatewayConnect ( gatewayRegistryEntry , identityName ? ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , `connect` ) ; let runtimeData ; if ( ! gatewayRegistryEntry ) { const chosenEntry = await UserInputUtil . showGatewayQuickPickBox ( 'Choose a gateway to connect with' , false , true ) as IBlockchainQuickPickItem < FabricGatewayRegistryEntry > ; if ( ! chosenEntry ) { return ; } gatewayRegistryEntry = chosenEntry . data ; } const gatewayName = gatewayRegistryEntry . name ; const environmentName = gatewayRegistryEntry . fromEnvironment ; let environmentEntry ; if ( environmentName ) { environmentEntry = await FabricEnvironmentRegistry . instance ( ) . get ( environmentName ) ; } if ( environmentEntry && environmentEntry . managedRuntime ) { const environment = EnvironmentFactory . getEnvironment ( environmentEntry ) as LocalMicroEnvironment ; const running = await environment . isRunning ( ) ; if ( ! running ) { outputAdapter . log ( LogType . ERROR , `${ environmentName } has not been started, please start it before connecting.` ) ; return ; } runtimeData = 'managed runtime' ; } let walletName ; let walletRegistryEntry ; if ( ! gatewayRegistryEntry . associatedWallet ) { const wallets = await FabricWalletRegistry . instance ( ) . getAll ( false ) ; if ( wallets . length === 0 ) { outputAdapter . log ( LogType . ERROR , `You must first add a wallet with identities to connect to this gateway` ) ; return ; } const chosenWallet = await UserInputUtil . showWalletsQuickPickBox ( 'Choose a wallet to connect with' , false , false ) as IBlockchainQuickPickItem < FabricWalletRegistryEntry > ; if ( ! chosenWallet ) { return ; } walletName = chosenWallet . data . name ; walletRegistryEntry = chosenWallet . data ; } else { walletName = gatewayRegistryEntry . associatedWallet ; const fabricWalletRegistry = FabricWalletRegistry . instance ( ) ; walletRegistryEntry = await fabricWalletRegistry . get ( walletName , gatewayRegistryEntry . fromEnvironment ) ; } const FabricWalletGenerator = FabricWalletGeneratorFactory . getFabricWalletGenerator ( ) ; const wallet = await FabricWalletGenerator . getWallet ( walletRegistryEntry ) ; let identityNames ; if ( environmentEntry && environmentEntry . environmentType === EnvironmentType . LOCAL_MICROFAB_ENVIRONMENT ) { const identities = await FabricWalletHelper . getVisibleIdentities ( environmentEntry , walletRegistryEntry ) ; identityNames = identities . map ( ( identity ) => identity . name ) ; } else { identityNames = await wallet . getIdentityNames ( ) ; } if ( identityNames . length > 1 ) { identityName = await UserInputUtil . showIdentitiesQuickPickBox ( 'Choose an identity to connect with' , false , identityNames ) as string ; if ( ! identityName ) { return ; } } else if ( identityNames . length === 0 ) { outputAdapter . log ( LogType . ERROR , 'No identities found in wallet: ' + walletName ) ; return ; } else { identityName = identityNames [ 0 ] ; } const connectionProfilePath = await FabricGatewayHelper . getConnectionProfilePath ( gatewayRegistryEntry ) ; const connection = FabricConnectionFactory . createFabricGatewayConnection ( connectionProfilePath ) ; try { const timeout = vscode . workspace . getConfiguration ( ) . get ( SettingConfigurations . FABRIC_CLIENT_TIMEOUT ) ; await connection . connect ( wallet , identityName , timeout ) ; connection . identityName = identityName ; FabricGatewayConnectionManager . instance ( ) . connect ( connection , gatewayRegistryEntry , walletRegistryEntry ) ; outputAdapter . log ( LogType . SUCCESS , `Connecting to ${ gatewayName }` ) ; if ( ! runtimeData ) { const isIBP = connection . isIBPConnection ( ) ; runtimeData = ( isIBP ? 'IBP instance' : 'user runtime' ) ; } const isIBMer = ExtensionUtil . checkIfIBMer ( ) ; Reporter . instance ( ) . sendTelemetryEvent ( 'connectCommand' , { runtimeData : runtimeData , connectIBM : isIBMer + '' } ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , error . message , error . toString ( ) ) ; return ; } }