Ethcode docs
  • Introduction
  • Installation
    • How to install?
  • Configurations
    • Extension Settings
    • Set transaction gas strategy
  • How to Use?
    • Account
    • Initialize Ethcode
    • Compiling contracts
    • Add ABI into Ethcode
    • Deploy contract to network
    • Calling contracts
    • Create rental NFT
  • API Documentation
    • Getting started with extension API
    • wallet
    • contract
    • provider
    • events
    • status
  • Help and support
    • Support
Powered by GitBook
On this page
  • Create Ethereum Account
  • Import Ethereum Account
  • Export Ethereum Account
  • Use with hardhat wallets
  1. How to Use?

Account

PreviousSet transaction gas strategyNextInitialize Ethcode

Last updated 2 years ago

Ethcode provides developers with all the required features of Ethereum account. Inside Ethcode developers can create, import, and Export an Ethereum account to do operations on EVM-compatible systems. below is the explanation of each feature associated with the account.

Create Ethereum Account

An Ethereum account is 42 characters long hexadecimal address. Developers can use an inbuilt Ethereum account to do any deployment and contract call process in Ethcode.

  1. Ctrl + shift + P - shows command palate of VScode.

  2. Ethcode: Activate Extension - this command activates the Ethcode extension.

3. Create ethereum account- command is for creating an Ethereum account in Ethcode.

4. Set the desired password for the new Ethereum account.

You should remember the password of your newly created Ethereum account to do any interaction with smart contracts.

After successful account creation now it's time to fund your Ethereum account with faucet tokens.

you can get free tokens from mentioned faucets:

Import Ethereum Account

If anyone wants to use accounts created outside Ethcode like Metamask. Ethcode provides the feature of importing the Ethereum account stored in Keystore format.

  1. Import ethereum account- command for importing Ethereum account stored in Keystore format.

2. Select keystore file where it is saved in your local machine.

Export Ethereum Account

Developers love uniformity that's why Ethcode contains one more exciting feature called Export account. you can export the Ethereum account that you use in Ethcode in Keystore format and use that account on others wallets too.

  1. Ethcode: Export Account- command to export account from Ethcode in Keystore format.

2. Select account from the Quick menu.

3. Select the directory where you want to save the Keystore file.

Use with hardhat wallets

While using with hardhat, one might select hardhat accounts and be able to use that account for readonly calls without any further modifications. But if user requires to sign transactions with a hardhat account using ethcode here are the steps.

  1. Export a hardhat wallet

  • create account.ts file

// account.ts
import { config } from 'hardhat';
import { ethers } from 'ethers';
import { createFromPrivateKey } from './keyeth';
import { HardhatNetworkHDAccountsConfig } from 'hardhat/types';
import keyethereum from 'keythereum';

const accounts: HardhatNetworkHDAccountsConfig = config.networks.hardhat.accounts as HardhatNetworkHDAccountsConfig;
const index = 0; // index of hardhat account that you want to export
const wallet = ethers.Wallet.fromMnemonic(accounts.mnemonic, accounts.path + `/${index}`);
const params = { keyBytes: 32, ivBytes: 16, privateKey: wallet.privateKey };
try {
  const bareKey = createFromPrivateKey(params);
  const options = {
    kdf: 'scrypt',
    cipher: 'aes-128-ctr'
  };
  if (bareKey) {
    const keyObject = keyethereum.dump(
      Buffer.from('', 'utf-8'),
      bareKey.privateKey,
      bareKey.salt,
      bareKey.iv
    );
    console.log(keyObject);
    // path to export your key. Default current directory.
    keyethereum.exportToFile(keyObject, `./`);
  }
} catch (error) {
  console.error(error);
}
  • create keyeth.ts file

// keyeth.ts
import { getRandomBytesSync, getRandomBytes } from "ethereum-cryptography/random";
import { privateKeyVerify } from "ethereum-cryptography/secp256k1-compat";
import keythereum from "keythereum";


function isFunction(f: any): f is Function {
  return typeof f === "function";
}


interface KeyParams {
  keyBytes?: number;
  ivBytes?: number;
  privateKey: string;
}


interface PrivateKeyObject {
  privateKey: Buffer;
  iv: Buffer;
  salt: Buffer;
}


function createFromPrivateKey(
  params: KeyParams, cb?: any
): PrivateKeyObject | void {
  const keyBytes = params.keyBytes || 32;
  const ivBytes = params.ivBytes || 16;
  const privateKey = Buffer.from(params.privateKey.slice(2), 'hex');


  function checkBoundsAndCreateObject(randomBytes: Uint8Array): any {
    randomBytes = Buffer.from(randomBytes);
    if (!privateKeyVerify(privateKey)) {
      return keythereum.create({ keyBytes, ivBytes }, cb);
    }
    return {
      privateKey,
      iv: randomBytes.slice(keyBytes, keyBytes + ivBytes),
      salt: randomBytes.slice(keyBytes + ivBytes)
    };
  }


  // synchronous key generation if callback not provided
  if (!isFunction(cb)) {
    return checkBoundsAndCreateObject(getRandomBytesSync(keyBytes + ivBytes + keyBytes));
  }


  // asynchronous key generation
  getRandomBytes(keyBytes + ivBytes + keyBytes).then(
    function (randomBytes: Uint8Array) {
      cb(checkBoundsAndCreateObject(randomBytes));
    }, function (err: any) {
      cb(err);
    }
  );
}

export { createFromPrivateKey };
  • create account.js file

// account.js
require('hardhat')
const keythereum = require('keythereum')
const keyeth = require('./keyeth')

const accounts = config.networks.hardhat.accounts
const index = 0 // index of hardhat account that you want to export
const wallet = ethers.Wallet.fromMnemonic(accounts.mnemonic, accounts.path + `/${index}`)
const params = { keyBytes: 32, ivBytes: 16, privateKey: wallet.privateKey }
const bareKey = keyeth.createFromPrivateKey(params)
const options = {
  kdf: 'scrypt',
  cipher: 'aes-128-ctr'
}
const keyObject = keythereum.dump(
  Buffer.from('', 'utf-8'),
  bareKey.privateKey,
  bareKey.salt,
  bareKey.iv,
  options
)
console.log(keyObject)
// path to export your key. Default current directory.
keythereum.exportToFile(keyObject, `./`)
  • create keyeth.js file

// keyeth.js
var random = require("ethereum-cryptography/random");
var secp256k1 = require("ethereum-cryptography/secp256k1-compat");

function isFunction(f) {
  return typeof f === "function";
}

function createFromPrivateKey(params, cb) {
  var keyBytes, ivBytes, self = this;
  params = params || {};
  keyBytes = params.keyBytes || this.constants.keyBytes;
  ivBytes = params.ivBytes || this.constants.ivBytes;
  var privateKey = Buffer.from(params.privateKey.slice(2), 'hex');

  function checkBoundsAndCreateObject(randomBytes) {
    randomBytes = Buffer.from(randomBytes);
    if (!secp256k1.privateKeyVerify(privateKey)) return keythereum.create({ keyBytes, ivBytes }, cb);
    return {
      privateKey: privateKey,
      iv: randomBytes.slice(keyBytes, keyBytes + ivBytes),
      salt: randomBytes.slice(keyBytes + ivBytes)
    };
  }

  // synchronous key generation if callback not provided
  if (!isFunction(cb)) {
    return checkBoundsAndCreateObject(random.getRandomBytesSync(keyBytes + ivBytes + keyBytes));
  }

  // asynchronous key generation
  random.getRandomBytes(keyBytes + ivBytes + keyBytes).then(function (randomBytes) {
    cb(checkBoundsAndCreateObject(randomBytes));
  }, function (err) {
    cb(err);
  });
}

exports.createFromPrivateKey = createFromPrivateKey;
  1. Import the hardhat wallet into ethcode

  1. Now you should be able to sign your transaction using the hardhat wallet you imported. When asked for password use nothing and hit enter.

Goerli testnet faucet:

Sepolia testnet faucet:

Polygon mumbai faucet:

https://goerlifaucet.com/
https://faucet.sepolia.dev/
https://faucet.polygon.technology/
Activate ETHcode
Create Ethereum account
new account passord
account created
import account command
keystore file
account imported
export account command
account quickpick menu
account exported
Import wallet