How to generate a new Ethereum address

How to generate a new Ethereum address

They have different account systems on Bitcoin and Ethereum

Bitcoin and Ethereum are different design ideas for account systems. So their transaction systems are not the same.

Today, I will ignore Bitcoin and more explain about Ethereum.

  • Bitcoin uses Unspent Transaction Output.
  • Ethereum uses the normal account system.

This article is very subjective. If you do not feel comfortable viewing it, please close it as soon as possible.
If you think my article can help you, you can subscribe to this site by using RSS.

What is the wallet’s address on the blockchain?

If you ask me what is the blockchain, I will say the blockchain looks like a simple account book. It records one line of transfer records. Every record least includes three elements:

  1. From where
  2. To where
  3. How much

In the blockchain world, the wallet address is an identity on transfer records.

If you have one wallet address, you could get a ticket to visit the blockchain world.

How to generate a wallet address on blockchain in English?

  1. Unlike the current account system of Web2, a blockchain wallet address is not registered. It is obtained through a series of complex encryption algorithms using a private key.
  2. A series of complex encryption algorithms used to generate the wallet address is public and fixed. This means that anyone can implement the method and generate the wallet address using any programming on any device at any time and no internet connection is required.
  3. Each unique private key corresponds to a unique wallet address.
  4. The private key is the only credential to operate the wallet.
  5. If you lost the private key, it can’t retrievable.

How to generate an Ethereum wallet address in code?

Ethereum has two account types:

  1. Externally owned – controlled by anyone with the private keys
  2. Contract – a smart contract deployed to the network, controlled by code.

Both account types can:

  • Receive, hold and send ETH and tokens
  • Interact with deployed smart contracts

The address format for both accounts is a 42-character hexadecimal string starting with 0x.
etc.

1
2
0x5e97870f263700f46aa00d967821199b9bc5a120
0x06012c8cf97bead5deae237070f9587f8e7a266d

Ethereum wallet address generation principle

Private Key -> Public Key -> Address.

Therefore, address generation requires three steps:

Private Key -> Public Key -> Address.

Therefore, address generation requires three steps:

  1. Generate a random private key size of 64 hexadecimal characters. (256 bits / 32 bytes)
  2. Generate a public key from the private key via the Elliptic Curve Digital Signature Algorithm. (128 hexadecimal / 512 bits / 64 bytes)
  3. Get a 64-character string with the public key via the Keccak-256 hash function, and then take the last 40 characters as the address. (40 hexadecimal / 160 bits / 20bytes )

Command line generation method

First, install sha3sum for keccak-256sum.
https://github.com/maandree/sha3sum

1
2
// on macOS
brew install sha3sum
1
2
3
4
5
6
7
8
9
10
# Generate the private and public keys
openssl ecparam -name secp256k1 -genkey -noout | openssl ec -text -noout > key
# Extract the public key and remove the EC prefix 0x04
cat key | grep pub -A 5 | tail -n +2 | tr -d '\n[:space:]:' | sed 's/^04//' > pub
# Extract the private key and remove the leading zero byte
cat key | grep priv -A 3 | tail -n +2 | tr -d '\n[:space:]:' | sed 's/^00//' > priv
# Generate the hash and take the address part
cat pub | keccak-256sum -x -l | tr -d ' -' | tail -c 41 | awk '{print "0x"$1}'> address
# display your address
cat address

The code generation method on Go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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)
}

Now we have two easy ways to generate the Ethereum wallet address offline and have the private key.

Referrals

Photo by Georgi Srebrev on Unsplash

ETHEREUM ACCOUNTS

Generate Ethereum Private key, Public key, and Address using Bash and OpenSSL

How to generate a new Ethereum address in Go

Author

Ewan Xiao

Posted on

August 2nd 2022

Updated on

May 17th 2024

Licensed under

Comments