-1

I have a public key that gets generated using the secpk256 curve, I am trying to determine the ethereum address of this public key. Basically I obtain the key as a simple string and then try to use geths crypto functions to get the public address of this public key.

Heres a sample hex encoded public key string: 02ccb8bc17397c55242a27d1681bf48b5b40a734205760882cd83f92aca4f1cf45 heres my code sample:

package main

import (
    "github.com/ethereum/go-ethereum/crypto"
)


publickey := "02ccb8bc17397c55242a27d1681bf48b5b40a734205760882cd83f92aca4f1cf45"
ecdsaPub, err := crypto.UnmarshalPubkey(publickey)
if err != nil {
    return "", err
}
ethAddress := crypto.PubkeyToAddress(*ecdsaPub).String()
Volker
  • 31,554
  • 5
  • 67
  • 68
Khalil Claybon
  • 123
  • 1
  • 7

1 Answers1

-1

you should DecompressPubkey

package main

import (
    "crypto/elliptic"
    "encoding/hex"
    "fmt"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/crypto/secp256k1"
)


func main() {

    pubBytes, err := hex.DecodeString("03bbba49a934014049d99a7f5c809fd0da59b1cb47dcbd0b3fe097adc5eaa5ec42")
    if err != nil {
        fmt.Println(err, "--1")
    }
    if pubkey1, err := crypto.DecompressPubkey(pubBytes); err != nil {
        fmt.Println(err)
        return
    } else {
        pubkey := elliptic.Marshal(secp256k1.S256(), pubkey1.X, pubkey1.Y)
        fmt.Println(hex.EncodeToString(pubkey))
        //output :04bbba49a934014049d99a7f5c809fd0da59b1cb47dcbd0b3fe097adc5eaa5ec424b3e22bae2d8f20d4bf8c0c85ec1efbce4b91196962199cd743dd6f72433559f
    }

}
faker
  • 151
  • 8
  • hmmm yes it looks like this public keys is compressed. I guess I have to decompress it manually ? @faker – Khalil Claybon Oct 24 '18 at 15:50
  • 1
    just like this `pubBytes,err :=hex.DecodeString("03bbba49a934014049d99a7f5c809fd0da59b1cb47dcbd0b3fe097adc5eaa5ec42") if err != nil{ fmt.Println(err,"--1") } if pubkey1,err :=crypto.DecompressPubkey(pubBytes);err !=nil{ fmt.Println(err) return }else { pubkey := elliptic.Marshal(secp256k1.S256(), pubkey1.X, pubkey1.Y) fmt.Println(hex.EncodeToString(pubkey)) }` – faker Oct 25 '18 at 03:31
  • you shoul check the examples in go-ethereum/crypto ,such as this file – faker Oct 25 '18 at 03:33
  • Your comment seems to be the answer, while the answer seems to be just a generation of a key pair / public key. – Maarten Bodewes Oct 26 '18 at 02:39