'use strict' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { GatewayDissociatedTreeItem } from '../explorer/model/GatewayDissociatedTreeItem' ; import { IBlockchainQuickPickItem , UserInputUtil } from './UserInputUtil' ; import { FabricWalletRegistry , FabricWalletRegistryEntry , LogType , FabricGatewayRegistry , FabricGatewayRegistryEntry } from 'ibm-blockchain-platform-common' ; export async function associateWallet ( gatewayTreeItem ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , 'associateWallet' ) ; let gateway ; if ( ! gatewayTreeItem ) { let gateways = [ ] ; gateways = await FabricGatewayRegistry . instance ( ) . getAll ( false ) ; if ( gateways . length === 0 ) { outputAdapter . log ( LogType . ERROR , `Add a gateway to associate a wallet.` ) ; return ; } const chosenGateway = await UserInputUtil . showGatewayQuickPickBox ( 'Pick a gateway to associate a wallet with' , false , false , false ) as IBlockchainQuickPickItem < FabricGatewayRegistryEntry > ; if ( ! chosenGateway ) { return ; } gateway = chosenGateway . data ; } else { gateway = gatewayTreeItem . gateway ; } let wallets = [ ] ; wallets = await FabricWalletRegistry . instance ( ) . getAll ( false ) ; if ( wallets . length === 0 ) { outputAdapter . log ( LogType . ERROR , `You must first add a wallet, to then associate with this gateway` ) ; return ; } const chosenWallet = await UserInputUtil . showWalletsQuickPickBox ( 'Choose a wallet to associate with this gateway' , false , false ) as IBlockchainQuickPickItem < FabricWalletRegistryEntry > ; if ( ! chosenWallet ) { return ; } const associatedWallet = chosenWallet . label ; try { const fabricGatewayRegistry = FabricGatewayRegistry . instance ( ) ; gateway . associatedWallet = associatedWallet ; await fabricGatewayRegistry . update ( gateway ) ; const gatewayName = gateway . name ; outputAdapter . log ( LogType . SUCCESS , `Successfully associated "${ associatedWallet }" wallet with "${ gatewayName }" gateway` ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Unable to associate wallet: ${ error . message }` , `Unable to associate wallet: ${ error . toString ( ) }` ) ; } }