我有一个使用secpk256曲线生成的公钥,我试图确定此公钥的以太坊地址。基本上,我以简单的字符串形式获取密钥,然后尝试使用geths加密函数来获取此公共密钥的公共地址。
这里有一个示例十六进制编码的公共密钥字符串: 02ccb8bc17397c55242a27d1681bf48b5b40a734205760882cd83f92aca4f1cf45 继承人我的代码示例:
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()
答案 0 :(得分:-1)
您应该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
}
}