'use strict' ; import * as vscode from 'vscode' ; import { UserInputUtil , IBlockchainQuickPickItem } from './UserInputUtil' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { IdentityTreeItem } from '../explorer/model/IdentityTreeItem' ; import { FabricWalletRegistryEntry , IFabricWallet , IFabricWalletGenerator , LogType , FabricWalletGeneratorFactory , FabricRuntimeUtil } from 'ibm-blockchain-platform-common' ; import { ExtensionCommands } from '../../ExtensionCommands' ; export async function deleteIdentity ( treeItem ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , `deleteIdentity` ) ; let wallet ; let identitiesToDelete ; const walletGenerator = FabricWalletGeneratorFactory . getFabricWalletGenerator ( ) ; if ( ! treeItem ) { const chosenWallet = await UserInputUtil . showWalletsQuickPickBox ( 'Choose the wallet containing the identity that you want to delete' , false , true ) as IBlockchainQuickPickItem < FabricWalletRegistryEntry > ; if ( ! chosenWallet ) { return ; } wallet = await walletGenerator . getWallet ( chosenWallet . data ) ; let identityNames = await wallet . getIdentityNames ( ) ; const walletName = ( chosenWallet . data . displayName ) ? chosenWallet . data . displayName : chosenWallet . data . name ; if ( walletName . includes ( `${ FabricRuntimeUtil . LOCAL_FABRIC } - ` ) ) { identityNames = identityNames . filter ( ( identity ) => { return identity !== FabricRuntimeUtil . ADMIN_USER ; } ) ; } if ( walletName . includes ( `${ FabricRuntimeUtil . LOCAL_FABRIC } - ` ) && identityNames . length === 0 ) { outputAdapter . log ( LogType . ERROR , `No identities to delete in wallet: ${ walletName }. The ${ FabricRuntimeUtil . ADMIN_USER } identity cannot be deleted.` ) ; return ; } else if ( identityNames . length === 0 ) { outputAdapter . log ( LogType . ERROR , `No identities in wallet: ${ wallet . getWalletPath ( ) }` ) ; return ; } identitiesToDelete = await UserInputUtil . showIdentitiesQuickPickBox ( 'Choose the identities to delete' , true , identityNames ) as string [ ] ; if ( ! identitiesToDelete || identitiesToDelete . length === 0 ) { return ; } } else { const registryEntry = treeItem . registryEntry ; wallet = await walletGenerator . getWallet ( registryEntry ) ; identitiesToDelete = [ treeItem . label ] ; } let areYouSure ; if ( identitiesToDelete . length > 1 ) { areYouSure = await UserInputUtil . showConfirmationWarningMessage ( `This will delete the selected identities from your file system. Do you want to continue?` ) ; } else { areYouSure = await UserInputUtil . showConfirmationWarningMessage ( `This will delete ${ identitiesToDelete [ 0 ] } from your file system. Do you want to continue?` ) ; } if ( ! areYouSure ) { return ; } for ( const _identity of identitiesToDelete ) { await wallet . removeIdentity ( _identity ) ; await vscode . commands . executeCommand ( ExtensionCommands . REFRESH_WALLETS ) ; } if ( identitiesToDelete . length > 1 ) { outputAdapter . log ( LogType . SUCCESS , `Successfully deleted selected identities.` ) ; } else { outputAdapter . log ( LogType . SUCCESS , `Successfully deleted identity: ${ identitiesToDelete [ 0 ] }` ) ; } return ; }