'use strict' ; import * as path from 'path' ; import * as vscode from 'vscode' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { UserInputUtil } from './UserInputUtil' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { FabricWalletRegistry , FabricWalletRegistryEntry , IFabricWallet , IFabricWalletGenerator , LogType , FabricWalletGeneratorFactory } from 'ibm-blockchain-platform-common' ; import { FabricWalletHelper } from '../fabric/FabricWalletHelper' ; export async function addWallet ( createIdentity = true , environmentGroup ? ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , 'addWallet' ) ; let walletName ; let walletPath ; let walletUri ; let wallet ; let identities ; const fabricWalletRegistry = FabricWalletRegistry . instance ( ) ; const fabricWalletRegistryEntry = new FabricWalletRegistryEntry ( ) ; try { const walletMethod = await UserInputUtil . showAddWalletOptionsQuickPick ( 'Choose a method to add a wallet:' , createIdentity ) ; if ( ! walletMethod ) { return ; } if ( walletMethod === UserInputUtil . IMPORT_WALLET ) { const openDialogOptions = { canSelectFiles : false , canSelectFolders : true , canSelectMany : false , openLabel : 'Select' , } ; walletUri = await UserInputUtil . browse ( 'Enter a file path to a wallet directory' , UserInputUtil . BROWSE_LABEL , openDialogOptions , true ) as vscode . Uri ; if ( ! walletUri ) { return ; } walletPath = walletUri . fsPath ; walletName = path . basename ( walletPath ) ; const exists = await fabricWalletRegistry . exists ( walletName ) ; if ( exists ) { outputAdapter . log ( LogType . ERROR , `A wallet with this name already exists.` ) ; return ; } fabricWalletRegistryEntry . name = walletName ; fabricWalletRegistryEntry . walletPath = walletPath ; if ( environmentGroup ) { fabricWalletRegistryEntry . environmentGroups = [ environmentGroup ] ; } await fabricWalletRegistry . add ( fabricWalletRegistryEntry ) ; const fabricWalletGenerator = FabricWalletGeneratorFactory . getFabricWalletGenerator ( ) ; wallet = await fabricWalletGenerator . getWallet ( fabricWalletRegistryEntry ) ; identities = await wallet . getIdentityNames ( ) ; if ( identities . length === 0 ) { await wallet . migrateToV2Wallet ( ) ; identities = await wallet . getIdentityNames ( ) ; if ( identities . length === 0 ) { await fabricWalletRegistry . delete ( walletName ) ; throw new Error ( `No identities found in wallet: ${ walletPath }` ) ; } } } else { walletName = await UserInputUtil . showInputBox ( 'Enter a name for the wallet' ) ; if ( ! walletName ) { return ; } const exists = await fabricWalletRegistry . exists ( walletName ) ; if ( exists ) { outputAdapter . log ( LogType . ERROR , `A wallet with this name already exists.` ) ; return ; } fabricWalletRegistryEntry . name = walletName ; fabricWalletRegistryEntry . walletPath = FabricWalletHelper . getWalletPath ( walletName ) ; if ( environmentGroup ) { fabricWalletRegistryEntry . environmentGroups = [ environmentGroup ] ; } await fabricWalletRegistry . add ( fabricWalletRegistryEntry ) ; if ( createIdentity ) { await vscode . commands . executeCommand ( ExtensionCommands . ADD_WALLET_IDENTITY , fabricWalletRegistryEntry ) ; const fabricWalletGenerator = FabricWalletGeneratorFactory . getFabricWalletGenerator ( ) ; wallet = await fabricWalletGenerator . getWallet ( fabricWalletRegistryEntry ) ; identities = await wallet . getIdentityNames ( ) ; if ( identities . length === 0 ) { await FabricWalletRegistry . instance ( ) . delete ( walletName ) ; return ; } } } outputAdapter . log ( LogType . SUCCESS , 'Successfully added a new wallet' ) ; return fabricWalletRegistryEntry ; } catch ( error ) { await FabricWalletRegistry . instance ( ) . delete ( walletName , true ) ; outputAdapter . log ( LogType . ERROR , `Failed to add a new wallet: ${ error . message }` , `Failed to add a new wallet: ${ error . message }` ) ; } }