Skip to content

Instantly share code, notes, and snippets.

@pavleprica
Created September 11, 2023 14:26
Show Gist options
  • Select an option

  • Save pavleprica/4711858cf4473cab0b9a4996b01a8b61 to your computer and use it in GitHub Desktop.

Select an option

Save pavleprica/4711858cf4473cab0b9a4996b01a8b61 to your computer and use it in GitHub Desktop.
Generates a ETH private key and prints the key, pub key and address
package main
import (
"crypto/ecdsa"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
func main() {
privateKey, err := crypto.GenerateKey()
if err != nil {
panic(err)
}
privateKeyBytes := crypto.FromECDSA(privateKey)
fmt.Println("Private key:", hexutil.Encode(privateKeyBytes))
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
panic("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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment