Created
September 11, 2023 14:26
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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