'use strict' ; import * as vscode from 'vscode' ; import { Reporter } from '../util/Reporter' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { CertificateAuthorityTreeItem } from '../explorer/runtimeOps/connectedTree/CertificateAuthorityTreeItem' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { UserInputUtil } from './UserInputUtil' ; import { Attribute , FabricNode , IFabricEnvironmentConnection , IFabricWallet , LogType } from 'ibm-blockchain-platform-common' ; import { FabricEnvironmentManager } from '../fabric/environments/FabricEnvironmentManager' ; export async function createNewIdentity ( certificateAuthorityTreeItem ? ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , 'createNewIdentity' ) ; let certificateAuthorityName ; let connection = FabricEnvironmentManager . instance ( ) . getConnection ( ) ; if ( ! connection ) { await vscode . commands . executeCommand ( ExtensionCommands . CONNECT_TO_ENVIRONMENT ) ; connection = FabricEnvironmentManager . instance ( ) . getConnection ( ) ; if ( ! connection ) { return ; } } if ( ! certificateAuthorityTreeItem ) { certificateAuthorityName = await UserInputUtil . showCertificateAuthorityQuickPickBox ( 'Choose certificate authority to create a new identity with' ) ; if ( ! certificateAuthorityName ) { return ; } } else { certificateAuthorityName = certificateAuthorityTreeItem . name ; } const identityName = await UserInputUtil . showInputBox ( 'Provide a name for the identity' ) ; if ( ! identityName ) { return ; } try { const wallet = await connection . getWallet ( certificateAuthorityName ) ; const identityExists = await wallet . exists ( identityName ) ; if ( identityExists ) { outputAdapter . log ( LogType . ERROR , `An identity called ${ identityName } already exists` ) ; return ; } const caNode = connection . getNode ( certificateAuthorityName ) ; let mspid = caNode . msp_id ; if ( ! mspid ) { const chosenMspid = await UserInputUtil . showInputBox ( 'Enter MSPID' ) ; if ( ! chosenMspid ) { return ; } mspid = chosenMspid ; } const affiliation = '' ; const addAttributes = await UserInputUtil . showQuickPickYesNo ( 'Do you want to add attributes to the identity?' ) ; let attributes ; let attributesString ; if ( ! addAttributes ) { return ; } else if ( addAttributes === UserInputUtil . YES ) { attributesString = await UserInputUtil . showInputBox ( `What are the attributes for the identity? e.g. [{ "name":"hello", "value":"world", "ecert":true }]` , '[]' ) ; if ( attributesString === undefined ) { return ; } else { attributes = JSON . parse ( attributesString ) ; } } const secret = await connection . register ( certificateAuthorityName , identityName , affiliation , attributes ) ; const details = await connection . enroll ( certificateAuthorityName , identityName , secret ) ; await wallet . importIdentity ( details . certificate , details . privateKey , identityName , mspid ) ; await vscode . commands . executeCommand ( ExtensionCommands . REFRESH_WALLETS ) ; let message ; if ( attributes ) { message = `Successfully created identity '${ identityName }' with the attributes: ${ attributesString }` ; } else { message = `Successfully created identity '${ identityName }'` ; } outputAdapter . log ( LogType . SUCCESS , message ) ; Reporter . instance ( ) . sendTelemetryEvent ( 'createNewIdentityCommand' ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Issue creating new identity: ${ error . message }` , `Issue creating new identity: ${ error . toString ( ) }` ) ; } }