1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package main
import ( "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" "log" "strings" )
func main() { hexStr := "0x68656c6c6f" sig := "0x7c4f5f867466a9244d9c5e59d7b0f0a57b56209789f13997cff86485875eb6bc2e7e802ed7c05c6dc6f5bf27735e43a0240d3404571fbbb222c769f56488e77f1c" data, err := hexutil.Decode(hexStr) if err != nil { log.Fatal(err) } recovered := recoverPubKey(data, sig) log.Println(recovered) }
func recoverPubKey(data []byte, sig string) string { hash := accounts.TextHash(data) s, err := hexutil.Decode(sig) if err != nil { return "" } if s[crypto.RecoveryIDOffset] == 27 || s[crypto.RecoveryIDOffset] == 28 { s[crypto.RecoveryIDOffset] -= 27 } recovered, err := crypto.SigToPub(hash, s) if err != nil { return "" } addr := crypto.PubkeyToAddress(*recovered) return strings.ToLower(addr.Hex()) }
|