Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Ethereum Cookbook

You're reading from   Ethereum Cookbook Over 100 recipes covering Ethereum-based tokens, games, wallets, smart contracts, protocols, and Dapps

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789133998
Length 404 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Manoj P R Manoj P R
Author Profile Icon Manoj P R
Manoj P R
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started 2. Smart Contract Development FREE CHAPTER 3. Interacting with the Contract 4. The Truffle Suite 5. Tokens and ICOs 6. Games and DAOs 7. Advanced Solidity 8. Smart Contract Security 9. Design Decisions 10. Other Protocols and Applications 11. Miscellaneous 12. Other Books You May Enjoy

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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime