Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,11 +24,13 @@ const event: {
account: vscode.EventEmitter<string>
contracts: vscode.EventEmitter<any>
updateAccountList: vscode.EventEmitter<string[]>
accountCreated: vscode.EventEmitter<IAccountCreated>
} = {
network: new vscode.EventEmitter<string>(),
account: new vscode.EventEmitter<string>(),
contracts: new vscode.EventEmitter<any>(),
updateAccountList: new vscode.EventEmitter<string[]>()
updateAccountList: new vscode.EventEmitter<string[]>(),
accountCreated: new vscode.EventEmitter<IAccountCreated>()
}

// PROVIDER
Expand Down
13 changes: 12 additions & 1 deletion src/api/events.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -36,6 +37,14 @@ export interface EventsInterface {
* @type {vscode.EventEmitter<any>}
*/
updateAccountList: vscode.EventEmitter<any>

/**
* An event emitter for account creation.
*
* @event
* @type {vscode.EventEmitter<string>}
*/
accountCreated: vscode.EventEmitter<IAccountCreated>
}

/**
Expand All @@ -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
}
}
7 changes: 7 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ export async function activate (context: ExtensionContext): Promise<API | undefi
events: events()
}

api.events.accountCreated.event((info) => {
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.')
Expand Down
8 changes: 8 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
59 changes: 33 additions & 26 deletions src/utils/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down