Inside an Ethereum transaction

Ethereum can be thought of as a transaction based state machine, where transactions can change the state and the state keeps track of interactions. Here we examine at a high level, the constituents of a transaction and explain how most of the gibberish hex values are determined.

We will be using nodejs in this tutorial so we start off by installing the dependencies.

Then creating a file tx.js and requiring the dependencies.

First we start with a private key. Ethereum uses public key cryptography for authentication. More specifically, Elliptic Curve Digital Signature Algorithm (ECDSA) with secp256k1’s curve is used. The private key is just a random 256 bit data except for some restrictions. For example

To derive the corresponding public key

and you should get the following if you print out publicKey

The Ethereum address associated with this private key is the last 160 bit of the SHA3–256 (Keccak) hash of the public key.

As you can observe, it is actually possible for multiple private keys to have the same address. An Ethereum account is associated with each address and each have the following attributes

nonce the count of the number of outgoing transactions, starting with 0
balance the amount of ether in the account
storageRoot the hash associated with the storage of the account
codeHash the hash of the code governing the account, if this is empty then the account is a normal account that can be accessed with its private key else it is a smart contract whose interactions are governed by its code

Next we take a look at a transaction, there are 6 input fields

nonce the count of the number of outgoing transactions, starting with 0
gasPrice the price to determine the amount of ether the transaction will cost
gasLimit the maximum gas that is allowed to be spent to process the transaction
to the account the transaction is sent to, if empty, the transaction will create a contract
value the amount of ether to send
data could be an arbitrary message or function call to a contract or code to create a contract

A transaction to send 1000 wei (1 ether = 10¹⁸ wei) of ether and leaving a 0xc0de message can be constructed as follows

Notice that the from address is not specified, it will be derived from the signature after signing with the private key. To sign the transaction

The transaction can then be sent to the network and will be tracked by a 256 bit transaction id. This transaction can be viewed at Etherscan. The transaction id is the hash of the transaction

Next we look at what makes up the data for a function call. Take for example the data of this transaction to a contract

In order to know which function it is calling, the functions of the contract must be known beforehand to create a hash table. The first 32 bit a9059cbb is the first 32 bit of the hash of the function. In this case the function is transfer(address _to, uint256 _value) and its hash is

This is followed by 256 bit for each argument, so in this case the address is

and the unsigned integer is

Next, as discussed above, by omitting the to field, a contract will be created. But how is the contract’s address determined? Take for example this transaction

The contract address is the last 160 bit hash of the sender address and its nonce can be determined beforehand. For this transaction, the sender and nonce can be found by

Thus the contract address is

Now we have gotten to know a little bit more about these hexadecimals!

Ethereum and smart contracts have huge potential to disrupt many industries. There are many resources online and you can find a few below to continue your journey on exploring Ethereum!

Ethereum main site https://www.ethereum.org/
Mist, one of Ethereum’s client https://github.com/ethereum/mist/releases
Solidity http://solidity.readthedocs.io/en/latest/
Web3 api https://github.com/ethereum/wiki/wiki/JavaScript-API
Community discussions https://www.reddit.com/r/ethereum/

If you have any issues regarding this article, you can raise it at our Github under the nightlyHacks repo.

Follow us on Twitter and Medium for more updates!

--

--

https://codetract.io/ CodeTract is a startup focused on building smart contracts enforced by blockchains

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CodeTract

https://codetract.io/ CodeTract is a startup focused on building smart contracts enforced by blockchains