Skip to content

Instantly share code, notes, and snippets.

@smallyunet
Created February 20, 2023 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smallyunet/f62c540fba2311a2c4d25e350ef666fb to your computer and use it in GitHub Desktop.
Save smallyunet/f62c540fba2311a2c4d25e350ef666fb to your computer and use it in GitHub Desktop.
Eth private key to address
package main
import (
"encoding/hex"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/wealdtech/go-merkletree/keccak256"
)
func main() {
privateKeyStr := "0x0000000000000000000000000000000000000000000000000000000000000001"
privateKey, err := hexutil.Decode(privateKeyStr)
if err != nil {
panic(err)
}
cv := secp256k1.S256()
puKeyX, puKeyY := cv.ScalarBaseMult(privateKey)
var x [32]byte
var y [32]byte
copy(x[:], puKeyX.Bytes())
copy(y[:], puKeyY.Bytes())
concatXY := append(x[:], y[:]...)
k := keccak256.New()
ethAddr := k.Hash(concatXY[:])
ethAddrHex := "0x" + hex.EncodeToString(ethAddr[:])[24:]
fmt.Println(privateKeyStr, ethAddrHex)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment