diff --git a/src/api/api.ts b/src/api/api.ts index 296242c..56dd4e6 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -7,7 +7,7 @@ import { getSelectedNetConf, getSelectedProvider } from '../utils/networks' -import { type ContractABI, type CompiledJSONOutput, type NetworkConfig } from '../types' +import { type ContractABI, type CompiledJSONOutput, type NetworkConfig, type IAccountCreated } from '../types' import { createConstructorInput, createDeployed, @@ -24,11 +24,13 @@ const event: { account: vscode.EventEmitter contracts: vscode.EventEmitter updateAccountList: vscode.EventEmitter + accountCreated: vscode.EventEmitter } = { network: new vscode.EventEmitter(), account: new vscode.EventEmitter(), contracts: new vscode.EventEmitter(), - updateAccountList: new vscode.EventEmitter() + updateAccountList: new vscode.EventEmitter(), + accountCreated: new vscode.EventEmitter() } // PROVIDER diff --git a/src/api/events.ts b/src/api/events.ts index 4d02de2..53a1aff 100644 --- a/src/api/events.ts +++ b/src/api/events.ts @@ -1,5 +1,6 @@ import type * as vscode from 'vscode' import { event } from './api' +import { type IAccountCreated } from '../types' /** * Represents an interface for event emitters of network and account changes. @@ -36,6 +37,14 @@ export interface EventsInterface { * @type {vscode.EventEmitter} */ updateAccountList: vscode.EventEmitter + + /** + * An event emitter for account creation. + * + * @event + * @type {vscode.EventEmitter} + */ + accountCreated: vscode.EventEmitter } /** @@ -49,11 +58,13 @@ export function events (): EventsInterface { const account = event.account const contracts = event.contracts const updateAccountList = event.updateAccountList + const accountCreated = event.accountCreated return { network, account, contracts, - updateAccountList + updateAccountList, + accountCreated } } diff --git a/src/extension.ts b/src/extension.ts index 0711aac..fce35f4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -220,6 +220,13 @@ export async function activate (context: ExtensionContext): Promise { + if (info.success) logger.success(info.successMsg) + else { + logger.error(info.error) + } + }) + const path_ = workspace.workspaceFolders if (path_ === undefined) { await window.showErrorMessage('No folder selected please open one.') diff --git a/src/types/types.ts b/src/types/types.ts index 60460ef..9135830 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -116,3 +116,11 @@ export function isConstructorInputValue ( ): obj is ConstructorInputValue { return obj.value !== undefined } + +export type IAccountCreated = { + successMsg: string + success: true +} | { + error: any + success: false +} diff --git a/src/utils/wallet.ts b/src/utils/wallet.ts index 7ba0584..bc28f7f 100644 --- a/src/utils/wallet.ts +++ b/src/utils/wallet.ts @@ -52,34 +52,41 @@ const listAddresses: any = async ( // Create keypair const createKeyPair: any = (context: vscode.ExtensionContext, path: string, pswd: string) => { - const params = { keyBytes: 32, ivBytes: 16 } - const bareKey = keythereum.create(params) - const options = { - kdf: 'scrypt', - cipher: 'aes-128-ctr' - } - const keyObject = keythereum.dump( - Buffer.from(pswd, 'utf-8'), - bareKey.privateKey, - bareKey.salt, - bareKey.iv, - options - ) - const account: Account = { - pubAddr: keyObject.address, - checksumAddr: ethers.utils.getAddress(keyObject.address) - } - logger.success('Account created!') - logger.log(JSON.stringify(account)) + try { + const params = { keyBytes: 32, ivBytes: 16 } + const bareKey = keythereum.create(params) + const options = { + kdf: 'scrypt', + cipher: 'aes-128-ctr' + } + const keyObject = keythereum.dump( + Buffer.from(pswd, 'utf-8'), + bareKey.privateKey, + bareKey.salt, + bareKey.iv, + options + ) + const account: Account = { + pubAddr: keyObject.address, + checksumAddr: ethers.utils.getAddress(keyObject.address) + } + event.accountCreated.fire({ + successMsg: `New account created: 0x${keyObject.address as string}`, + success: true + }) + logger.log(JSON.stringify(account)) - if (!fs.existsSync(`${path}/keystore`)) { - fs.mkdirSync(`${path}/keystore`) + if (!fs.existsSync(`${path}/keystore`)) { + fs.mkdirSync(`${path}/keystore`) + } + keythereum.exportToFile(keyObject, `${path}/keystore`) + listAddresses(context, path).then((addresses: string[]) => { + event.updateAccountList.fire(addresses) + }).catch((error: any) => logger.error(error)) + return keyObject.address + } catch (error) { + event.accountCreated.fire({ error, success: false }) } - keythereum.exportToFile(keyObject, `${path}/keystore`) - listAddresses(context, path).then((addresses: string[]) => { - event.updateAccountList.fire(addresses) - }).catch((error: any) => logger.error(error)) - return keyObject.address } // Delete privateKey against address