From bde8b8e14effa413b810f2ed7e495e6ca55a0dae Mon Sep 17 00:00:00 2001 From: Dhruv Sharma Date: Sun, 2 Jul 2023 12:36:52 +0530 Subject: [PATCH 1/2] feat: added event listener for account creation --- src/api/api.ts | 4 +++- src/api/events.ts | 12 +++++++++- src/extension.ts | 4 ++++ src/utils/wallet.ts | 56 ++++++++++++++++++++++++--------------------- 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/api/api.ts b/src/api/api.ts index 296242c..7869c91 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -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..4e91778 100644 --- a/src/api/events.ts +++ b/src/api/events.ts @@ -36,6 +36,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 +57,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..3f5a29c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -220,6 +220,10 @@ export async function activate (context: ExtensionContext): Promise { + logger.success(msg) + }) + const path_ = workspace.workspaceFolders if (path_ === undefined) { await window.showErrorMessage('No folder selected please open one.') diff --git a/src/utils/wallet.ts b/src/utils/wallet.ts index 7ba0584..39efc21 100644 --- a/src/utils/wallet.ts +++ b/src/utils/wallet.ts @@ -52,34 +52,38 @@ 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(`New account created: 0x${keyObject.address as string}`) + 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('Failed to create account!') } - 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 From 0f32d2ae065a72dee3ac5aee5e11bab43088c92a Mon Sep 17 00:00:00 2001 From: Dhruv Sharma Date: Sun, 2 Jul 2023 13:34:38 +0530 Subject: [PATCH 2/2] fix: implemented account created type and improved error throwing --- src/api/api.ts | 6 +++--- src/api/events.ts | 3 ++- src/extension.ts | 7 +++++-- src/types/types.ts | 8 ++++++++ src/utils/wallet.ts | 7 +++++-- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/api/api.ts b/src/api/api.ts index 7869c91..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,13 +24,13 @@ const event: { account: vscode.EventEmitter contracts: vscode.EventEmitter updateAccountList: vscode.EventEmitter - accountCreated: vscode.EventEmitter + accountCreated: vscode.EventEmitter } = { network: new vscode.EventEmitter(), account: new vscode.EventEmitter(), contracts: new vscode.EventEmitter(), updateAccountList: new vscode.EventEmitter(), - accountCreated: new vscode.EventEmitter() + accountCreated: new vscode.EventEmitter() } // PROVIDER diff --git a/src/api/events.ts b/src/api/events.ts index 4e91778..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. @@ -43,7 +44,7 @@ export interface EventsInterface { * @event * @type {vscode.EventEmitter} */ - accountCreated: vscode.EventEmitter + accountCreated: vscode.EventEmitter } /** diff --git a/src/extension.ts b/src/extension.ts index 3f5a29c..fce35f4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -220,8 +220,11 @@ export async function activate (context: ExtensionContext): Promise { - logger.success(msg) + api.events.accountCreated.event((info) => { + if (info.success) logger.success(info.successMsg) + else { + logger.error(info.error) + } }) const path_ = workspace.workspaceFolders 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 39efc21..bc28f7f 100644 --- a/src/utils/wallet.ts +++ b/src/utils/wallet.ts @@ -70,7 +70,10 @@ const createKeyPair: any = (context: vscode.ExtensionContext, path: string, pswd pubAddr: keyObject.address, checksumAddr: ethers.utils.getAddress(keyObject.address) } - event.accountCreated.fire(`New account created: 0x${keyObject.address as string}`) + event.accountCreated.fire({ + successMsg: `New account created: 0x${keyObject.address as string}`, + success: true + }) logger.log(JSON.stringify(account)) if (!fs.existsSync(`${path}/keystore`)) { @@ -82,7 +85,7 @@ const createKeyPair: any = (context: vscode.ExtensionContext, path: string, pswd }).catch((error: any) => logger.error(error)) return keyObject.address } catch (error) { - event.accountCreated.fire('Failed to create account!') + event.accountCreated.fire({ error, success: false }) } }