'use strict' ; import * as vscode from 'vscode' ; import * as sinon from 'sinon' ; import * as path from 'path' ; import * as chai from 'chai' ; import * as sinonChai from 'sinon-chai' ; import * as chaiAsPromised from 'chai-as-promised' ; import { UserInputUtil } from '../../extension/commands/UserInputUtil' ; import { UserInputUtilHelper } from './userInputUtilHelper' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { IFabricWallet , IFabricWalletGenerator , FabricIdentity , FabricWalletRegistry , FabricWalletRegistryEntry , FabricWalletGeneratorFactory , FabricGatewayRegistryEntry , FabricGatewayRegistry } from 'ibm-blockchain-platform-common' ; chai . use ( sinonChai ) ; chai . use ( chaiAsPromised ) ; export class WalletAndIdentityHelper { certPath : string ; keyPath : string ; jsonFilePath : string ; connectionProfilePath : string ; mySandBox : sinon . SinonSandbox ; userInputUtilHelper : UserInputUtilHelper ; constructor ( sandbox , userInputUtilHelper ) { this . mySandBox = sandbox ; this . userInputUtilHelper = userInputUtilHelper ; this . certPath = path . join ( this . userInputUtilHelper . cucumberDir , 'hlfv1' , 'crypto-config' , 'peerOrganizations' , 'org1.example.com' , 'users' , 'Admin@org1.example.com' , 'msp' , 'signcerts' , 'Admin@org1.example.com-cert.pem' ) ; this . keyPath = path . join ( this . userInputUtilHelper . cucumberDir , 'hlfv1' , 'crypto-config' , 'peerOrganizations' , 'org1.example.com' , 'users' , 'Admin@org1.example.com' , 'msp' , 'keystore' , 'priv_sk' ) ; this . jsonFilePath = path . join ( this . userInputUtilHelper . cucumberDir , 'hlfv1' , 'crypto-config' , 'peerOrganizations' , 'org1.example.com' , 'users' , 'Admin@org1.example.com' , 'Org1Admin.json' ) ; this . connectionProfilePath = path . join ( this . userInputUtilHelper . cucumberDir , 'hlfv1' , 'connection.json' ) ; } public async createCAIdentity ( walletName , identityName , environmentName , attributes = '[]' ) { const walletEntry = await FabricWalletRegistry . instance ( ) . get ( walletName , environmentName ) ; const fabricWalletGenerator = FabricWalletGeneratorFactory . getFabricWalletGenerator ( ) ; const wallet = await fabricWalletGenerator . getWallet ( walletEntry ) ; const identityExists = await wallet . exists ( identityName ) ; let ca ; if ( process . env . OTHER_FABRIC ) { ca = 'ca.example.com' ; } else { ca = 'Org1 CA' ; } this . userInputUtilHelper . showCertificateAuthorityQuickPickStub . withArgs ( 'Choose certificate authority to create a new identity with' ) . resolves ( ca ) ; if ( ! identityExists ) { this . userInputUtilHelper . inputBoxStub . withArgs ( 'Provide a name for the identity' ) . resolves ( identityName ) ; if ( attributes !== '[]' ) { this . userInputUtilHelper . showYesNoQuickPick . withArgs ( 'Do you want to add attributes to the identity?' ) . resolves ( UserInputUtil . YES ) ; this . userInputUtilHelper . inputBoxStub . withArgs ( `What are the attributes for the identity? e.g. [{ "name":"hello", "value":"world", "ecert":true }]` , '[]' ) . resolves ( attributes ) ; } else { this . userInputUtilHelper . showYesNoQuickPick . withArgs ( 'Do you want to add attributes to the identity?' ) . resolves ( UserInputUtil . NO ) ; } await vscode . commands . executeCommand ( ExtensionCommands . CREATE_NEW_IDENTITY ) ; } } public async deleteCAIdentity ( identityName , walletName , environmentName ) { const walletEntry = await FabricWalletRegistry . instance ( ) . get ( walletName , environmentName ) ; const fabricWalletGenerator = FabricWalletGeneratorFactory . getFabricWalletGenerator ( ) ; const wallet = await fabricWalletGenerator . getWallet ( walletEntry ) ; const identityExists = await wallet . exists ( identityName ) ; if ( identityExists ) { this . userInputUtilHelper . showWalletsQuickPickStub . withArgs ( 'Choose the wallet containing the identity that you want to delete' ) . resolves ( { label : walletEntry . displayName , data : walletEntry } ) ; this . userInputUtilHelper . showIdentitiesQuickPickStub . withArgs ( 'Choose the identities to delete' ) . resolves ( [ identityName ] ) ; this . userInputUtilHelper . showConfirmationWarningMessageStub . withArgs ( `This will delete ${ identityName } from your file system. Do you want to continue?` ) . resolves ( true ) ; await vscode . commands . executeCommand ( ExtensionCommands . DELETE_IDENTITY ) ; } } public async createWallet ( name , identityName , mspid , method ) { const exists = await FabricWalletRegistry . instance ( ) . exists ( name ) ; if ( ! exists ) { this . userInputUtilHelper . showAddWalletOptionsQuickPickStub . resolves ( UserInputUtil . WALLET_NEW_ID ) ; this . userInputUtilHelper . inputBoxStub . withArgs ( 'Enter a name for the wallet' ) . resolves ( name ) ; await this . setIdentityStubs ( method , identityName , mspid ) ; await vscode . commands . executeCommand ( ExtensionCommands . ADD_WALLET ) ; } else { const walletEntry = await FabricWalletRegistry . instance ( ) . get ( name ) ; const wallet = await FabricWalletGeneratorFactory . getFabricWalletGenerator ( ) . getWallet ( walletEntry ) ; const identities = await wallet . getIdentities ( ) ; const identity = identities . find ( ( _identity ) => { return _identity . name === identityName ; } ) ; if ( ! identity ) { await this . createIdentity ( name , identityName , mspid , method ) ; } } } public async createIdentity ( walletName , identityName , mspid , method ) { await this . setIdentityStubs ( method , identityName , mspid ) ; const wallet = await FabricWalletRegistry . instance ( ) . get ( walletName ) ; await vscode . commands . executeCommand ( ExtensionCommands . ADD_WALLET_IDENTITY , wallet ) ; } private async setIdentityStubs ( method , identityName , mspid ) { this . userInputUtilHelper . inputBoxStub . withArgs ( 'Enter MSPID' ) . resolves ( mspid ) ; if ( method === 'certs' ) { this . userInputUtilHelper . showAddIdentityMethodStub . resolves ( UserInputUtil . ADD_CERT_KEY_OPTION ) ; this . userInputUtilHelper . inputBoxStub . withArgs ( 'Provide a name for the identity' ) . resolves ( identityName ) ; this . userInputUtilHelper . showGetCertKeyStub . resolves ( { certificatePath : this . certPath , privateKeyPath : this . keyPath } ) ; } else if ( method === 'JSON file' ) { const jsonPath = process . env . OPSTOOLS_FABRIC ? path . join ( process . env . JSON_DIR , `${ identityName }.json` ) : this . jsonFilePath ; this . userInputUtilHelper . showAddIdentityMethodStub . resolves ( UserInputUtil . ADD_JSON_ID_OPTION ) ; this . userInputUtilHelper . browseStub . resolves ( vscode . Uri . file ( jsonPath ) ) ; } else { this . userInputUtilHelper . showAddIdentityMethodStub . resolves ( UserInputUtil . ADD_ID_SECRET_OPTION ) ; this . userInputUtilHelper . inputBoxStub . withArgs ( 'Provide a name for the identity' ) . resolves ( identityName ) ; const gatewayRegistryEntry = await FabricGatewayRegistry . instance ( ) . get ( 'myGateway' ) ; this . userInputUtilHelper . showGatewayQuickPickStub . resolves ( { data : gatewayRegistryEntry } ) ; this . userInputUtilHelper . getEnrollIdSecretStub . resolves ( { enrollmentID : 'admin' , enrollmentSecret : 'adminpw' } ) ; } } }