From 635f15bf221bf5b7a2a1f6ac05d772df6868de6a Mon Sep 17 00:00:00 2001 From: fastendev Date: Tue, 11 Oct 2022 06:56:23 -0400 Subject: [PATCH 1/3] update source --- package.json | 7 ++++++- src/extension.ts | 7 ++++++- src/utils/wallet.ts | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 53f1249..c7371f0 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "onCommand:ethcode.compiled-json.load", "onCommand:ethcode.compiled-json.load.all", "onCommand:ethcode.compiled-json.select", - "onCommand:ethcode.network.select" + "onCommand:ethcode.network.select", + "onCommand:ethcode.account.import" ], "main": "./build/src/extension.js", "extensionDependencies": [ @@ -131,6 +132,10 @@ { "command": "ethcode.contract.call", "title": "Ethcode: Contract call" + }, + { + "command": "ethcode.account.import", + "title": "Ethcode: Import Account" } ], "keybindings": [ diff --git a/src/extension.ts b/src/extension.ts index 2d6da5b..9366e70 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -11,7 +11,7 @@ import { updateSelectedNetwork, } from './utils/networks'; import { logger } from './lib'; -import { createKeyPair, deleteKeyPair, selectAccount } from './utils/wallet'; +import { createKeyPair, deleteKeyPair, selectAccount, importKeyPair } from './utils/wallet'; import { parseBatchCompiledJSON, parseCompiledJSONPayload, selectContract } from './utils'; // eslint-disable-next-line import/prefer-default-export @@ -83,6 +83,11 @@ export async function activate(context: vscode.ExtensionContext) { callContractMethod(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 = { diff --git a/src/utils/wallet.ts b/src/utils/wallet.ts index e9b07ea..58564b1 100644 --- a/src/utils/wallet.ts +++ b/src/utils/wallet.ts @@ -90,6 +90,42 @@ 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": ["*"], + }, + }; + + 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]}`); + + 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 { @@ -140,5 +176,6 @@ export { createKeyPair, deleteKeyPair, extractPvtKey, - selectAccount + selectAccount, + importKeyPair } From 3c392b96b25df3aefa5d6dec66ad3a84a22dcc2e Mon Sep 17 00:00:00 2001 From: fastendev Date: Wed, 12 Oct 2022 01:42:07 -0400 Subject: [PATCH 2/3] bug fix account duplication --- src/utils/wallet.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/utils/wallet.ts b/src/utils/wallet.ts index 58564b1..0267fb4 100644 --- a/src/utils/wallet.ts +++ b/src/utils/wallet.ts @@ -102,6 +102,8 @@ const importKeyPair = async (context: vscode.ExtensionContext) => { }, }; + const addresses = await listAddresses(context, context.extensionPath); + vscode.window.showOpenDialog(options).then((fileUri) => { if (fileUri && fileUri[0]) { const arrFilePath = fileUri[0].fsPath.split("\\"); @@ -109,16 +111,22 @@ const importKeyPair = async (context: vscode.ExtensionContext) => { const arr = file.split("--"); const address = toChecksumAddress(`0x${arr[arr.length - 1]}`); - 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); + 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) { From df8931cdc5a8212adf4a2afe66a4fdcff2f4f97b Mon Sep 17 00:00:00 2001 From: Aniket Singh Date: Mon, 17 Oct 2022 21:04:01 +0530 Subject: [PATCH 3/3] set gas strategy --- package.json | 5 +++ src/extension.ts | 84 +++++++++++++++++++++++++++--------------------- 2 files changed, 52 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index 59b6a7b..e449bfd 100644 --- a/package.json +++ b/package.json @@ -116,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" diff --git a/src/extension.ts b/src/extension.ts index f7ff687..1f5ce41 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,112 +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, importKeyPair, 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) + 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!"); }) ); }