Learn Ethereum programming #3. Ethereum accounts.
There are 2 types of accounts on Ethereum, Externally Owned Accounts (EOAs) and contract accounts. EOAs are controlled by private keys, while contract accounts are controlled by the code itself. Contract accounts cannot send transactions, but they can react to transactions by sending messages to other accounts (also known as internal transactions).
Copy trading bots from the top traders. Try it for FREE.
EOAs are created by the users themselves, through the generation of a random number of 32 bytes. Contract accounts are created through transactions sent to address zero. Anyone can create a contract account by simply paying the required gas fee.
EOAs are only registered on Ethereum, so to speak, when they participate in their first transaction, be it a transaction sent by them or for them. There is no need to “warn” the network about creating an account. To better understand this, let’s understand what the world state of Ethereum looks like.
The global state is a key-value database, where the key is a 20-byte number. The key is the account address. The address of EOAs is calculated from the private key, while the address of a contract is calculated from the account that created the contract and its nonce (see later).
As long as an account has not performed a transaction with Ethereum, the value of the key corresponding to that address will be empty. From the first transaction, Ethereum will start storing account information.
Accounts have 4 pieces of information: balance, nonce, code hash e storage hash. The last two fields are only non-null for contract accounts, and hold the contract code hash (the bytecode hash) and its storage hash. The bytecode is the code that is executed by the Ethereum virtual machine. The storage is where all the state variables of a contract are stored.
In the figure below we have a representation of the state of Ethereum. Given a 20-byte key, its value is a set of 4 fields.
We can retrieve information about an account using methods via JSON-RPC. Lets do this.
Retrieving account information
One of the most used methods in Ethereum is to recover the balance of an account: eth_getBalance. Such a method requires 2 parameters: the address of the account and in which block we want to recover the balance. The block number can be informed in hexadecimal or a string with value, “earliest”, “latest”, “pending”, “safe” and “finalized”.
All historical information on Ethereum is stored by nodes called Archive Nodes. Thus, it is possible to recover the balance of any account at any time in the past. Not every node is an archive node. Some nodes store information only about the recent state of Ethereum.
Let’s recover the balance of an account in the last block of the network.
The return of the request can be seen in the figure below.
The result is returned in hexadecimal,
0xb5e620f48000. In decimal, it is worth
200000000000000. This value is in Wei, which is the smallest value of ether and 1 ether worth 10¹⁸ Wei. A very useful site for converting Wei to ether and vice versa is eth-converter.com. The above value is converted to 0.0002 ether.
Nonce is a unique number that indicates how many transactions were carried out by a given account, in the case of external accounts. For contract accounts, the nonce indicates how many contracts this contract has already created.
We can retrieve the nonce of an account using the eth_getTransactionCount method. Let’s retrieve the total transactions of an account.
"params": ["0x27E0155021E63842aFb79F99da8B9592F429f614", "latest"],
In Goerli, the result is as follows:
That means the nonce is 45. The nonce is zero-based, so this account has already sent 46 transactions. When sending the next transaction, we must indicate the current nonce, i.e. 45.
The contract account code, bytecode, can also be retrieved via JSON-RPC, through the eth_getCode method. Let’s look at an example below.
"params": ["0x24a1f370c33aF7476490d8fe70Fde5629A82842F", "latest"],
The result will be the bytecode, a long hexadecimal number.
We can also retrieve information about state variables of a contract account, via the eth_getStorageAt method. However, to better understand about this method, we must first understand the structure of storage. We will see this in a next lesson.
How to Create an Ethereum Account, a brief review
Externally Owned Accounts are a pair of keys, public and private. The public key is derived from the private key, so you only need to define a private key. Basically, the private key is a 32-byte number. We could naively pick a 32-byte number at random, but humans are terrible at picking really random numbers. We’re better off letting software do it for us.
Software that creates and manages accounts are called wallets. We’ll see more about wallets throughout this course. Some Ethereum clients like Geth (Go-Ethereum) have wallet functionality, but currently it is not recommended to use the execution client as a wallet. It is recommended to use another software as wallet.
There are no JSON-RPC methods for creating accounts, because not only is there no need for it, but it wouldn’t be safe to send a private key. What I want to make clear is that it is the user who creates their own accounts remotely. As long as the accounts have not interacted with Ethereum, the network does not keep any information about them.
Thanks for reading!
Comments and suggestions about this article are welcome.
Any contribution is welcome. www.buymeacoffee.com/jpmorais.
Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing