Go Ethereum : Using geth and creating account

Solidity Programming Language
6 min readMar 13, 2022

Thanks: I prepare this lesson thanks to newline course. Most of the written materials belongs to newline course.

https://www.newline.co/courses/intro-to-programming-ethereum-dapps/using-geth-and-creating-accounts

START

Firstly we need to download go ethereum from official web site

https://geth.ethereum.org/downloads/

I choose Linux version and download it.

I extract this folder and save “geth” file. We will use it.

Save the “geth” file

I write the following commands in terminal.

mkdir go

cd go

mkdir bin

home/go/bin

This is our physical position in the our computer.

I put “geth” file to the this location

geth file is in the home

This is the private node

We are not joining the production Ethereum network.

We’re not going to download the history of everything that has happened in Ethereum thus far.

Instead, we’re going to start our own private network.

We’re going to configure our local test blockchain by using this configuration file, genesis.json.

You can save genesis.json file to the go folder.

Our node is starting

./bin/geth --datadir=./datadir init genesis.json

We create datadir folder. Would you like to see more detail?

You need to install tree with the following code

sudo snap install tree

It is time to see!

tree datadir

datadir has geth and keystore.

1-The geth directory holds our blockchain data.

2-The keystore directory, will hold our accounts keys.

I create one account.

./bin/geth --datadir=./datadir account new

It is the result.

Dont forget your password because you will use it again.

My password is 123456

You can control your public address in etherscan

We check out our datadir folder structure.

tree datadir

¡Orale Cabron! We have a new file in keystore.

Please focus on KEYSTORE.

KEYSTORE has our public address.

UTC--2022-03-13T15-06-39.931303932Z--5921a6ee66bb12b4c27592e3d6452b2226dbaa7f

0x5921A6Ee66Bb12B4C27592e3D6452B2226Dbaa7

For more readability

sudo snap install jq

Then the formula

cat datadir/keystore/ + YOUR KEYSTORE + | jq '.'
cat datadir/keystore/UTC--2022-03-13T15-06-39.931303932Z--5921a6ee66bb12b4c27592e3d6452b2226dbaa7f | jq '.'

This is geth javascript console

./bin/geth --datadir=./datadir console

We are in the console

Ether is created as a reward for mining new blocks.

We want to send ether to someone else:

  • we create a transaction
  • these transactions are grouped into blocks
  • miners verify that these blocks are valid and then
  • they are rewarded ether for doing this

We can get ether by being a miner ourselves.

miner.start(1)

When you call this, you might need to wait a few seconds before you see “Commit new sealing work” then “Successfully sealed a new block”

If you dont see new block, you can see this link

Let it run for a bit and then stop the miner:

miner.stop()

You can find out the number of blocks we mined by calling eth.blockNumber:

eth.blockNumber

I have 38 blocks

I have 190 ether. Wav!

web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")

I create the new account

personal.newAccount(“123456”)

123456 is my password

This is new account

New address is 0x2A1a50f37F6514DCD4500CFC50D880AB0130721f

I check my accounts

eth.accounts
eth.accounts[0]: 0x5921A6Ee66Bb12B4C27592e3D6452B2226Dbaa7F
eth.accounts[1]: 0x2A1a50f37F6514DCD4500CFC50D880AB0130721

To send ether we need to specify:

  • the from address
  • the to address and
  • the value of the transaction

We’ll do this with the sendTransaction method:

But fistly you unlock the account

personal.unlockAccount(eth.accounts[0], "123456")

Now you can sent

eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(10, "ether")})

We want to see more detail. We take hash value and analysis it

eth.getTransaction(“0xc51c384428335331761f089618a714ca23c1850a27a6f33e819f6b1e2925c1f1”)

Since we submitted a transaction, sending 3 ether from one account to another, our balances should have changed, right? Let’s check:

eth.getBalance(eth.accounts[1])

What is the problem ?

Well, it’s because even though we submitted a transaction, that block hasn’t been processed by any miners.

Take look at the information returned by getTransaction again.

Notice that the blockNumber says "null" and the blockHash is just zero.

This transaction hasn't been included in any block, so our account balances haven't changed.

In order to process this transaction, we need our miners to include it in a block. We can do that by starting our miner again:

miner.start(1)
miner.stop()

Now check it

eth.getBalance(eth.accounts[1])

or ether amount

web3.fromWei(eth.getBalance(eth.accounts[1]), "ether")

Let’s take a look at our transaction hash again:

eth.getTransaction(“0xc51c384428335331761f089618a714ca23c1850a27a6f33e819f6b1e2925c1f1”)

Notice that we now have a blockNumberas 39 and as well as a blockHash. We can take a closer look at the block this transaction was included in by using eth.getBlock:

eth.getBlock(“0x30076086baf5bfae7ed0326bb24d75a473955bfd96e1829e6e1a091e8f2ce671”)

There’s a lot of data in here that will make more sense as we get further along, but take a look at the array of transactions, notice that it contains our transaction's hash.

Each block stores a reference to it’s parent block. This is block number N, but it’s parent is block number N-1. We could look up the parent block by using parentHash. If we looked at parentHash we would see a list of transactions there, and then we could look at it's parent, and so on, all the way back to the original genesis block.

This idea of a block being parented by another block is the “chain” part of a blockchain. Every transaction that has ever happened in Ethereum can be found in this way.

Dr. Engin YILMAZ

2022

Ankara

--

--

Solidity Programming Language

Solidity basics for beginners: Learn the fundamentals of smart contract development and build your first DApp! #Solidity #Foundry #Ethereum #Opcodes #DApps