QuickNode Raises $60M Series B!
Read the Letter from our CEO.

How to generate a new Ethereum address in Go

September 09, 2022

Overview

Golang is very popular among backend developers for building infrastructures and microservices. Go is a procedural programming language. Developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google, then launched in 2009 as an open-source programming language, Go is efficient for managing dependencies. Programs in Go are assembled by using packages.
Go has gained a lot of market share in past years and is currently used by major corporations. In this guide, we will cover creating an Ethereum address in Go using the Go-Ethereum Client library.


For our purposes today, we’ll use crypto and common packages of go-ethereum.

Prerequisites
  • Go version 1.13 or above installed.
  • A text editor
  • CLI

What is an Ethereum address?

Authentication on any internet platform requires a username and password. You can think of an Ethereum address as your username, with the corresponding private key as your password (that you do not need to memorize). An Ethereum address is your unique identity on the blockchain, and it looks like this “0x6E0d01A76C3Cf4288372a29124A26D4353EE51BE”. The actual address is public and can be shared, but your private key must obviously be kept secret.  Your own Ethereum address is required for at least the following operations on the ETH network.

  • Receiving/Sending ETH currency
  • Signing/Sending transactions
  • Connecting to Applications

The following outlines the process of generating an Ethereum address using a private key.

  • A random private key of 64 (hex) characters (256 bits / 32 bytes) is generated first. 
    For example: 

what is an ethereum address

Copy
0xf4a2b939592564feb35ab10a8e04f6f2fe0943579fb3c9c33505298978b74893

  •  A 128 (hex) character (64 bytes) public key is then derived from the generated private key using Elliptic Curve Digital Signature Algorithm (ECDSA). 
    For example: 

what is an ethereum address

Copy
0x04345f1a86ebf24a6dbeff80f6a2a574d46efaa3ad3988de94aa68b695f09db9ddca37439f99548da0a1fe4acf4721a945a599a5d789c18a06b20349e803fdbbe3

  • The Keccak-256 hash function is then applied to (128 characters / 64 bytes) the public key to obtain a 64 character (32 bytes) hash string. The last 40 characters / 20 bytes of this string prefixed with 0x become the final Ethereum address. 
    For example:

what is an ethereum address

Copy
0xd5e099c71b797516c10ed0f0d895f429c2781142

What is go-ethereum?

go-ethereum is the official GOlang implementation of Ethereum used to develop Ethereum nodes. go-ethereum has many packages for interactions and transactions with the Ethereum blockchain in Go.

Let’s see how we can easily generate a new Ethereum address in Go.

Generating an Ethereum address in Go

The first step here will be to check if Go is installed on our system. To do so, copy-paste the following in your terminal/cmd:

generating an ethereum address in go

Copy
$ go version

If Go is not installed, follow the official installation guide for your specific OS.

Make sure you have the GCC compiler installed as well. 
For Ubuntu - use apt-get install build-essentials

For Mac - using homebrew

generating an ethereum address in go

Copy
$ brew install gcc
another faster option is getting XCode Command Line Tools using 

generating an ethereum address in go

Copy
$ xcode-select --install

If you’re not familiar with Go, we recommend running their interactive tutorial to cover the basic syntax, methods, and concurrency handling. There are some exercises you can run without leaving your environment. Simply type the command below into your command line/terminal to run the tutorial locally:

generating an ethereum address in go

Copy
$ go get golang.org/x/tour
Now type:

generating an ethereum address in go

Copy
$ tour

Start by creating a file named address.go and copy-paste the following code into it. This code will create a private key, a public key using the private key, and an Ethereum address.

generating an ethereum address in go

Copy
package main

import (
    "crypto/ecdsa"
    "fmt"
    "log"

    "github.com/ethereum/go-ethereum/common/hexutil"
    "github.com/ethereum/go-ethereum/crypto"
)

func main() {
    privateKey, err := crypto.GenerateKey()
    if err != nil {
        log.Fatal(err)
    }

    privateKeyBytes := crypto.FromECDSA(privateKey)
    fmt.Println("SAVE BUT DO NOT SHARE THIS (Private Key):", hexutil.Encode(privateKeyBytes))

    publicKey := privateKey.Public()
    publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
    if !ok {
        log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
    }

    publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
    fmt.Println("Public Key:", hexutil.Encode(publicKeyBytes)) 

    address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
    fmt.Println("Address:", address)
}

Explanation of the code above

Line 1-10: Declaring the main package and adding necessary dependencies required to generate keys and address.

Line 12: Invoking the main function.

Line 13: Generating a random private key using the GenerateKey method from the crypto package.

Line 14-16: Checking for errors.

Line 18: Converting the private key to bytes using the FromECDSA method of the crypto/ecdsa package.

Line 19: Converting the private key to a hexadecimal string using the Encode method of hexutil package and printing the new string with a warning message.

Line 21: Generating a Public key from the Private key using the Public method of crypto package.

Line 22-28: Checking the type of Public key and then converting it to a hexadecimal string using the same process we saw for the Private key and printing the Public Key with a message.

Line 30: Generating an Ethereum address using PubkeyToAddress method of the crypto package, which accepts ECDSA public key, and returns an Ethereum address and storing it in a variable address.

Line 31: Printing the address along with a message “Address:”

Next, let’s create a module to track dependencies. If you’re not familiar with go, this is an essential step in setting up your project’s dependencies. With Go it’s relatively easy.
 
Simply type:

generating an ethereum address in go

Copy
$ go mod init address

This will ensure the crypto and common packages included in your code are downloaded from GitHub and installed locally. It happens automatically, and the latest version should be pulled into your environment along with built-in Go modules. 

Now, let’s run our module.

generating an ethereum address in go

Copy
$ go run address.go 

If you followed the instructions correctly and everything goes right, it must output something like this. The first line displays the private key, the Public key is second, and the third line displays your Ethereum address.


Conclusion

Congratulations on creating a new Ethereum address with Go. This address can be used to interact with the Ethereum blockchain network and perform various operations. Look out for more easy-to-follow guides from QuikNode - your provider of affordable and lightning-fast Ethereum nodes. Learn how to get started with QuikNode and spin up your own Ethereum node in minutes and build the next big dApp!

Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you’ll ever meet :)

Related articles 37

How to Access Bitcoin Mempool
Published: Aug 16, 2021
Updated: Sep 21, 2022

Bitcoin is the father of blockchain technology. With Bitcoin started a new era of blockchain and decentralization. Bitcoin enabled everyone to make the peer-to-peer transactions they enjoy...

Continue reading
How to access Ethereum Mempool
Published: Oct 15, 2020
Updated: Sep 15, 2022

On Ethereum, when a transaction is sent, before being added to a block, it resides in what is called a Mempool. To receive information about this transaction, the Mempool must be queried. This...

Continue reading
The Web3 Developer Stack
Published: Jul 3, 2021
Updated: Sep 15, 2022

A developer stack is a bag of technologies a developer possesses. For example, MEAN (MongoDB, Express.js, AngularJS/Angular, and Node.js) and MERN (MongoDB, Express.js, React, and Node.js) are...

Continue reading
Introduction to Scaffold-ETH 🏗
Published: Oct 7, 2021
Updated: Sep 9, 2022

Developing applications involves juggling several moving pieces like front-ends, back-ends, and databases. But developing a decentralized application on a blockchain adds a few more elements...

Continue reading
How to create your own DAO with Aragon
Published: Aug 8, 2021
Updated: Sep 9, 2022

Blockchain provides us with the power of decentralization. Decentralization means the transfer of power to users/members rather than having a single centralized authority governing everything;...

Continue reading
How to connect to Ethereum using PHP
Published: Jun 11, 2021
Updated: Sep 9, 2022

PHP is a very popular choice among developers and has a vast community due to its long presence in web development. In this guide, we’ll cover how to connect to Ethereum with PHP using the

Continue reading
How to use Subspace with QuickNode
Published: Sep 8, 2020
Updated: Sep 9, 2022

In this guide, we'll understand a bit about reactive development and how to use Subspace with QuickNode.JavaScript is the programming language behind most of the internet apps and...

Continue reading