diff --git a/package.json b/package.json index 25120f7..e449bfd 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "onCommand:ethcode.compiled-json.load.all", "onCommand:ethcode.compiled-json.select", "onCommand:ethcode.network.select", - "onCommand:ethcode.account.export" + "onCommand:ethcode.account.export", + "onCommand:ethcode.account.import" ], "main": "./build/src/extension.js", "extensionDependencies": [ @@ -115,6 +116,11 @@ "title": "Get transaction info", "category": "Ethcode" }, + { + "command": "ethcode.transaction.gas.set", + "title": "Set transaction gas strategy", + "category": "Ethcode" + }, { "command": "ethcode.compiled-json.load", "title": "Ethcode: Load compiled JSON output" @@ -136,6 +142,10 @@ { "command": "ethcode.account.export", "title": "Ethcode: Export Account" + }, + { + "command": "ethcode.account.import", + "title": "Ethcode: Import Account" } ], "keybindings": [ diff --git a/src/extension.ts b/src/extension.ts index 30577b6..1f5ce41 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,108 +1,122 @@ -import { ethers } from 'ethers'; -import * as vscode from 'vscode'; -import { InputBoxOptions, window, commands } from 'vscode'; -import { - GanacheAddressType, -} from './types'; +import { ethers } from "ethers"; +import * as vscode from "vscode"; +import { InputBoxOptions, window, commands } from "vscode"; +import { GanacheAddressType } from "./types"; import { callContractMethod, deployContract, displayBalance, setTransactionGas, updateSelectedNetwork, -} from './utils/networks'; -import { logger } from './lib'; -import { createKeyPair, deleteKeyPair, selectAccount, exportKeyPair } from './utils/wallet'; -import { parseBatchCompiledJSON, parseCompiledJSONPayload, selectContract } from './utils'; +} from "./utils/networks"; +import { logger } from "./lib"; +import { + createKeyPair, + deleteKeyPair, + selectAccount, + importKeyPair, + exportKeyPair, +} from "./utils/wallet"; +import { + parseBatchCompiledJSON, + parseCompiledJSONPayload, + selectContract, +} from "./utils"; // eslint-disable-next-line import/prefer-default-export export async function activate(context: vscode.ExtensionContext) { context.subscriptions.push( - // Create new account with password - commands.registerCommand('ethcode.account.create', async () => { + commands.registerCommand("ethcode.account.create", async () => { try { const pwdInpOpt: InputBoxOptions = { ignoreFocusOut: true, password: true, - placeHolder: 'Password', + placeHolder: "Password", }; const password = await window.showInputBox(pwdInpOpt); - createKeyPair(context, context.extensionPath, password || ''); + createKeyPair(context, context.extensionPath, password || ""); } catch (error) { logger.error(error); } }), // Delete selected account with password - commands.registerCommand('ethcode.account.delete', async () => { + commands.registerCommand("ethcode.account.delete", async () => { deleteKeyPair(context); }), // Deploy ContractcallContractMethod - commands.registerCommand('ethcode.contract.deploy', async () => { + commands.registerCommand("ethcode.contract.deploy", async () => { deployContract(context); }), // select ethereum networks - commands.registerCommand('ethcode.network.select', () => { + commands.registerCommand("ethcode.network.select", () => { updateSelectedNetwork(context); }), // Select Ethereum Account - commands.registerCommand('ethcode.account.select', () => { + commands.registerCommand("ethcode.account.select", () => { selectAccount(context); }), // Get account balance - commands.registerCommand('ethcode.account.balance', async () => { + commands.registerCommand("ethcode.account.balance", async () => { displayBalance(context); }), - // Get gas estimate - commands.registerCommand('ethcode.transaction.gas.get', async () => { + // Set gas strategy + commands.registerCommand("ethcode.transaction.gas.set", async () => { + setTransactionGas(context); }), // Load combined JSON output - commands.registerCommand('ethcode.compiled-json.load', () => { - const editorContent = window.activeTextEditor ? window.activeTextEditor.document.getText() : undefined; + commands.registerCommand("ethcode.compiled-json.load", () => { + const editorContent = window.activeTextEditor + ? window.activeTextEditor.document.getText() + : undefined; parseCompiledJSONPayload(context, editorContent); }), // Load all combined JSON output - commands.registerCommand('ethcode.compiled-json.load.all', async () => { + commands.registerCommand("ethcode.compiled-json.load.all", async () => { parseBatchCompiledJSON(context); }), // Select a compiled json from the list - commands.registerCommand('ethcode.compiled-json.select', () => { + commands.registerCommand("ethcode.compiled-json.select", () => { selectContract(context); }), // Call contract method - commands.registerCommand('ethcode.contract.call', async () => { + commands.registerCommand("ethcode.contract.call", async () => { callContractMethod(context); }), //Export Account - commands.registerCommand('ethcode.account.export', async() => { + commands.registerCommand("ethcode.account.export", async () => { exportKeyPair(context); }), + //Import Key pair + commands.registerCommand("ethcode.account.import", async () => { + importKeyPair(context); + }), // Set custom gas estimate // commands.registerCommand('ethcode.transaction.gas.set', async () => { - // const gasInp: InputBoxOptions = { - // ignoreFocusOut: false, - // placeHolder: 'Enter custom gas', - // }; + // const gasInp: InputBoxOptions = { + // ignoreFocusOut: false, + // placeHolder: 'Enter custom gas', + // }; - // const gas = await window.showInputBox(gasInp); - // context.workspaceState.update('gasEstimate', gas); + // const gas = await window.showInputBox(gasInp); + // context.workspaceState.update('gasEstimate', gas); // }), // Activate - commands.registerCommand('ethcode.activate', async () => { - logger.success('Welcome to Ethcode!'); + commands.registerCommand("ethcode.activate", async () => { + logger.success("Welcome to Ethcode!"); }) ); } diff --git a/src/utils/wallet.ts b/src/utils/wallet.ts index 367f7f7..eafaaad 100644 --- a/src/utils/wallet.ts +++ b/src/utils/wallet.ts @@ -112,6 +112,50 @@ const deleteKeyPair = async (context: vscode.ExtensionContext) => { } }; +//Import Key pair + +const importKeyPair = async (context: vscode.ExtensionContext) => { + try { + const options: vscode.OpenDialogOptions = { + canSelectMany: false, + openLabel: "Open", + filters: { + "All files": ["*"], + }, + }; + + const addresses = await listAddresses(context, context.extensionPath); + + vscode.window.showOpenDialog(options).then((fileUri) => { + if (fileUri && fileUri[0]) { + const arrFilePath = fileUri[0].fsPath.split("\\"); + const file = arrFilePath[arrFilePath.length - 1]; + const arr = file.split("--"); + const address = toChecksumAddress(`0x${arr[arr.length - 1]}`); + + const already = addresses.find((element: string) => toChecksumAddress(element) === address) + + if(already !== undefined) { + logger.log(`Account ${address} is already exist.`) + } else { + fs.copyFile( + fileUri[0].fsPath, + `${context.extensionPath}/keystore/${file}`, + (err) => { + if (err) throw err; + } + ); + + logger.success(`Account ${address} is successfully imported!`); + listAddresses(context, context.extensionPath); + } + } + }); + } catch (error) { + logger.error(error); + } +}; + // extract privateKey against address const extractPvtKey = async (keyStorePath: string, address: string) => { try { @@ -240,4 +284,5 @@ export { deleteKeyPair, extractPvtKey, selectAccount, -}; + importKeyPair +}