Account

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:

Goerli testnet faucet: https://goerlifaucet.com/

Sepolia testnet faucet: https://faucet.sepolia.dev/

Polygon mumbai faucet: https://faucet.polygon.technology/

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 };
  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.

Last updated