If you have been following the blockchain series closely involving Ethereum, then you are at the right place. In this second installment of blockchain with the Golang series, we will delve into Ethereum Wallets. But before we start with the programming, let us start with the theoretical aspect:
What are Ethereum Wallets?
An Ethereum Wallet, also known as a client, holds the private key, otherwise referred to as the “secret password” that grants you control over your coins. It provides you with a public Ethereum address which people can use to transfer Ethereum’s currency known as Ether.
Ethereum generally refers to a network of independent computers, which work together as a supercomputer. The supercomputer, in turn, executes pieces of codes known as smart contracts.
However, an interaction with contracts requires a more complex communication than what is involved in Bitcoin. In order to fulfill this communication, Ethereum wallets play a pivotal role.
Generate Ethereum Wallets Using Golang
Now, we will get into the programming aspect.
Creating a New Directory for the Program
In order to get started, we have to create a new directory. This is where we will be saving the project. Let us name this directory ‘ethWall’. In my case, I’m using Windows system, so I’ll go to Command Prompt to type the following command:
Now, if you are using a Mac or Linux system, then you have to use Terminal. Furthermore, as I type code ., the VS Code gets launched. In your case, you might have other text editors or IDEs open for you.
Meanwhile, if you are doing blockchain with Golang, and you stumble upon an issue, then you should not hesitate to contact the developers at Golang.Company. They will assist you in creating scalable and robust decentralized systems, ledgers, tokens and much more.
How to Write a Program to Generate Ethereum Wallets using Golang?
Ok. Now, let us understand the program. Here, we are taking a look at how we can generate Ethereum wallet addresses so that we are able to use them within the decentralized applications.
We are starting off with package main. And then we are importing several packages for the execution of this code. These are “fmt”, “log”, “github.com/ethereum/go-ethereum/crypto”, “github.com/ethereum/go-ethereum/common/hexutil”.
Now, we have to go to the Terminal of the VS Code, and we have to type
go mode init github.com/golang-company/ethWall
Now, this will lead to the creation of the go.mod file.
Following this, we will ‘get’ the packages “github.com/ethereum/go-ethereum/crypto”, “github.com/ethereum/go-ethereum/common/hexutil”. For this, we will type:
go get github.com/ethereum/go-ethereum/crypto
go get github.com/ethereum/go-ethereum/common/hexutil
Next, we will go into the main function and we will use the crypto library to generate the new wallet. For this, we use crypto.GenerateKey(). This function call will return two values. These are the privateKey and the error (err).
Following this, we will apply an if statement. In case, the error is not equals to nil (if err!=nil), we can just do a log.Fatal and we print out “Not generating new keys”)
The next thing that we have to accomplish is to convert the privateKey that is returned to us from the ECDSA format into a bytes format and we can actually do that with the help of the crypto library. And we do this with the help of the following statement. privateKeyBytes := crypto.FromECDSA(privateKey)
From the above statement, you can see that it will return privateKeyBytes. Now that we have the bytes at our disposal, we have to encode them so that they are in the hexadecimal format. And we do that by using hexutil.Encode(privateKeyBytes)
As you can see, we are passing the privateKeyBytes. This whole statement will return to us a hexadecimal formatted private key so it is just printed out to our console. And this can be done with the help of fmt.Println.
Finally, we will save this and see what output it presents to us.
Hopefully, you have understood the concept of generating ethereum wallets. If you were unable to understand the program, then you should go through it once again. Most importantly, you should type down the code and check the output for yourself. Once you get a grasp of it, you will see that you are able to handle intricate projects.