Creating an Ethereum Wallet with Go-Ethereum

Step 1: We begin by importing all the important packages:

import (
   “context”
   “fmt”

“log”

      “github.com/ethereum/go-ethereum”
      “github.com/ethereum/go-ethereum/accounts”
      “github.com/ethereum/go-ethereum/common”
      “github.com/ethereum/go-ethereum/crypto”
      “github.com/ethereum/go-ethereum/ethclient”
  )

Step 2: Generate a new Ethereum account by creating a new private key and deriving the corresponding public key and address. In this code, we generate a new private key using crypto.GenerateKey() function. If there’s an error, the program exits with a fatal error log message.

Go




privateKey, err := crypto.GenerateKey()
if err != nil {
    log.Fatal(err)
}
  
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
    log.Fatal("Error casting public key to ECDSA")
}
  
address := crypto.PubkeyToAddress(*publicKeyECDSA)


Step 3: Print the newly created Ethereum address:

fmt.Println(“New Wallet Address:”, address.Hex())

Code:

Go




package main
  
import (
    "context"
    "crypto/ecdsa"
    "fmt"
    "log"
  
    "github.com/ethereum/go-ethereum"
    "github.com/ethereum/go-ethereum/accounts"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/ethclient"
)
  
func main() {
    // Generate a new private key
    privateKey, err := crypto.GenerateKey()
    if err != nil {
        log.Fatal(err)
    }
  
    // Obtain the public key from the generated private key
    publicKey := privateKey.Public()
    publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
    if !ok {
        log.Fatal("Error casting public key to ECDSA")
    }
  
    // Derive the Ethereum address from the public key
    address := crypto.PubkeyToAddress(*publicKeyECDSA)
    fmt.Println("New Wallet Address:", address.Hex())
  
    // Other Ethereum-related operations can be performed here
  
    // Example: Connect to an Ethereum client (such as Infura)
    // Replace "<YOUR_INFURA_API_KEY>" with your actual Infura API key
    infuraAPIKey := "<YOUR_INFURA_API_KEY>"
    client, err := ethclient.Dial("https://mainnet.infura.io/v3/" + infuraAPIKey)
    if err != nil {
        log.Fatal(err)
    }
  
    // Other Ethereum operations using the client can be performed here
}


Output: Once you have entered your address and infura API in the above code your output might look like this:

 

Ethereum Development with Golang

Ethereum is a blockchain-based platform that enables developers to create decentralized applications (dApps) and smart contracts. Ethereum’s native cryptocurrency is Ether (ETH), which is used to pay transaction fees and incentivize miners to secure the network.

Go, also known as Golang, is a programming language developed by Google. It is a compiled language that is designed to be efficient, concise, and easy to use. Go is often used for building web applications, network servers, and other types of software that require high performance.

Similar Reads

Getting Started with Go-Ethereum

Ethereum Go, also known as Geth, is the official Go implementation of the Ethereum protocol. Geth provides a command-line interface (CLI) for interacting with the Ethereum network, as well as an API for building decentralized applications. The relationship between Ethereum and Go is that Geth is one of the software clients that can connect to the Ethereum network. Developers can use Geth to interact with the Ethereum network and build decentralized applications using the Go programming language....

Connecting to an Ethereum Node using Infura and Go

Step 1:  After we have installed go-ethereum, create a new Go module for your project by running the following command:...

Querying Ethereum Wallet Balances with Geth

...

Creating an Ethereum Wallet with Go-Ethereum

...

Making Ethereum Transactions in Go using Go-Ethereum

...