Book Image

Ethereum Cookbook

By : Manoj P R
Book Image

Ethereum Cookbook

By: Manoj P R

Overview of this book

Ethereum and Blockchain will change the way software is built for business transactions. Most industries have been looking to leverage these new technologies to gain efficiencies and create new business models and opportunities. The Ethereum Cookbook covers various solutions such as setting up Ethereum, writing smart contracts, and creating tokens, among others. You’ll learn about the security vulnerabilities, along with other protocols of Ethereum. Once you have understood the basics, you’ll move on to exploring various design decisions and tips to make your application scalable and secure. In addition to this, you’ll work with various Ethereum packages such as Truffle, Web3, and Ganache. By the end of this book, you’ll have comprehensively grasped the Ethereum principles and ecosystem.
Table of Contents (13 chapters)

Understanding everything about accounts

Ethereum accounts are made up of 20-byte addresses, and you can exchange data and values between two accounts with state transitions. In general, there are two types of account in Ethereum: EOA and contract accounts. Depending on the type of account, there are four fields associated with each account: balance, contract code, storage, and nonce.

EOAs are controlled by a private key, and contract accounts are controlled by the contract code. A normal user can create an EOA by using a wallet such as MetaMask or MyCrypto. As a developer, you need to know more about these accounts and other ways to create them. In this recipe, you will learn how to create and maintain EOAs in Ethereum.

Getting ready

You will need a working installation of geth in your system to run web3 commands. Other prerequisites, if any, are listed along with each command.

How to do it...

  1. Create an account using the account command in the geth command-line tool. This will create an Ethereum account for you and will return the EOA address:
$ geth account new

If you are using the JavaScript console or Web3JS, use the following command to create an account:

> web3.personal.newAccount("<password>")
  1. Before you do any transaction from this account, unlock it using the password. Use the third parameter to control the duration of unlocking. To lock the account after the transaction, use the lockAccount function:
> personal.unlockAccount("<address>", "<password>")
> personal.lockAccount("<your_address>")
  1. Creating an account using this method also creates an encrypted KeyFile. The file is located in your key store with the format UTC--{year}-{month}--{account}. The contents of the file also includes an encrypted version of your private key:
{
"address":"4f9e3e25a8e6ddaf976dcbf0b92285b1bb139ce2",
"crypto":{
"cipher":"aes-128-ctr",
"ciphertext":"0b0ae5548acc4d08134f4fe...53cb8c08eb9563a76aeb082c",
"cipherparams":{
"iv":"6079a38e159b95f88e0f03fbdae0f526"
},
"kdf":"scrypt",
"kdfparams {
"dklen":32,
"n":262144,
"p":1,
"r":8,
"salt":"7ed09d37d80048c21490fc9...dc10b3cef10e544017"
},
"mac":"0d8ac3314c4e9c9aaac515995c...43bf46f17d2d78bb84a6"
},
"id":"7b6630ea-8fc8-4c35-a54f-272283c8f579",
"version":3
}
  1. You can also create a private key externally and import it into other wallets. Ethereum follows the elliptical curve for private keys. You can either create one with OpenSSL or with wallets such as MyCrypto. Once a private key is created, you can import it using the following command. Make sure to give it a strong password to encrypt it:
$ geth account import <path to PrivateKey file>

You can also import the private key using the JavaScript console:

> web3.personal.importRawKey("<private key>", "<password>")
Please do not share your private keys with any website, business, or individual. You may need to provide your private keys to MetaMask and other similar wallets, which exist to manage accounts, in order to use them. Do this with caution and ensure that you can trust them.
  1. If you want to find the address from the private key, a series of steps have to be followed. Convert the private key to a 64-byte-long public key. Then, take the Keccak-256 hash of the public key, and the last 20 bytes will be the Ethereum address for that private key.
  2. You can also create an account securely by using the mnemonic phrase. This mnemonic is a 12-word phrase that can be used as a seed to create EOAs. This is easy to store and wallets such as MetaMask follow this pattern.

There's more...

If you need a safe and secure way to store your private key or account, then the recommended way is to use a hardware wallet. The only way someone can use the account is by physically interacting with the wallet. This reduces the potential security threat if someone gets into your system maliciously.