mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
more progress in verifying signature with ecrecover
This commit is contained in:
parent
0cbeb26f93
commit
b09a39289a
1 changed files with 60 additions and 8 deletions
|
|
@ -7,7 +7,8 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"encoding/json"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -16,6 +17,12 @@ const (
|
|||
CONN_TYPE = "tcp"
|
||||
)
|
||||
|
||||
// This gives context to the signed message and prevents signing of transactions.
|
||||
func signHash(data []byte) []byte {
|
||||
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
|
||||
return crypto.Keccak256([]byte(msg))
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
|
||||
|
|
@ -57,17 +64,62 @@ func handleRequest(conn net.Conn) {
|
|||
//Ecrecover takes 2 args, bytes[] and bytes[]
|
||||
// we need to pass in the hash of the message as bytes
|
||||
// and the bytes array of the signature
|
||||
var msg = []byte(dat["msg"].(string))
|
||||
var sig = []byte(dat["sig"].(string))
|
||||
new_msg := crypto.Keccak256(msg)
|
||||
var address, err = crypto.Ecrecover(new_msg, sig)
|
||||
var msg = dat["msg"].(string)
|
||||
var sig = dat["sig"].(string)
|
||||
fmt.Println("the first sig is ")
|
||||
fmt.Println(sig)
|
||||
var new_byte_array, err2 = hexutil.Decode(msg)
|
||||
var new_sig_byte_array, err3 = hexutil.Decode(sig)
|
||||
if err2 != nil {
|
||||
fmt.Println("the err2 is ")
|
||||
fmt.Println(err2)
|
||||
}
|
||||
if err3 != nil {
|
||||
fmt.Println("the err3 is ")
|
||||
fmt.Println(err3)
|
||||
}
|
||||
var fizz = hexutil.Bytes(new_byte_array)
|
||||
var buzz = hexutil.Bytes(new_sig_byte_array)
|
||||
buzz[64] -= 27
|
||||
fmt.Println("that bytes is")
|
||||
fmt.Println(fizz)
|
||||
//var bytes_msg = []byte(msg)
|
||||
//var foo, err = hex.DecodeString(msg)
|
||||
//fmt.Println("msg is ")
|
||||
//fmt.Println(msg)
|
||||
//fmt.Println("bytes_msg is ")
|
||||
//fmt.Println(bytes_msg)
|
||||
|
||||
//var sig = dat["sig"].(string)
|
||||
//fmt.Println("The sig is ")
|
||||
//fmt.Println(sig)
|
||||
//decoded, er := hex.DecodeString(sig)
|
||||
//if er != nil {
|
||||
// log.Fatal(err)
|
||||
//}
|
||||
new_msg := signHash(fizz)
|
||||
fmt.Println("the new_msg is ")
|
||||
fmt.Println(new_msg)
|
||||
fmt.Println("the buzz is ")
|
||||
fmt.Println(buzz)
|
||||
|
||||
var rpk, err = crypto.Ecrecover(new_msg, buzz)
|
||||
if err != nil {
|
||||
fmt.Println("The error is ")
|
||||
fmt.Println(err)
|
||||
}
|
||||
s := string(address[:])
|
||||
|
||||
pubKey := crypto.ToECDSAPub(rpk)
|
||||
recoveredAddr := crypto.PubkeyToAddress(*pubKey)
|
||||
fmt.Println("the address is ")
|
||||
fmt.Println(s)
|
||||
fmt.Println(recoveredAddr)
|
||||
fmt.Println(recoveredAddr.Hex())
|
||||
//res, _ := new_add.MarshalText()
|
||||
//fmt.Println(hexutil.Encode(address))
|
||||
//s := string(address[:])
|
||||
//
|
||||
//fmt.Println(s)
|
||||
|
||||
//fmt.Println(address)
|
||||
//fmt.Println("Message is ", msg)
|
||||
}
|
||||
|
|
@ -78,4 +130,4 @@ func handleRequest(conn net.Conn) {
|
|||
conn.Write([]byte("Message received."))
|
||||
// Close the connection when you're done with it.
|
||||
conn.Close()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue