Merge pull request #50 from ShyftNetwork/shyft-ring-wallet-connectivity-v0.01

Shyft ring wallet connectivity v0.01
This commit is contained in:
Gregory Markou 2018-08-02 17:06:45 +03:00 committed by GitHub
commit ab56079a14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,24 +3,31 @@ package main
//@NOTE SHYFT main func for api, sets up router and spins up a server //@NOTE SHYFT main func for api, sets up router and spins up a server
//to run server 'go run shyftRingWalletConn/*.go' //to run server 'go run shyftRingWalletConn/*.go'
import ( import (
"net" "bufio"
"fmt" "fmt"
"os" "github.com/ShyftNetwork/go-empyrean/common/hexutil"
//"github.com/ethereum/go-ethereum/common" "github.com/ShyftNetwork/go-empyrean/crypto"
"github.com/ethereum/go-ethereum/crypto" "io"
"bytes" "net"
"github.com/ethereum/go-ethereum/common/hexutil" "net/http"
"os"
"github.com/ShyftNetwork/go-empyrean/ethclient"
"github.com/ShyftNetwork/go-empyrean/common"
"context"
) )
const ( const (
CONN_HOST = "localhost" CONN_HOST = "localhost"
CONN_PORT = "3333" CONN_PORT = "3333"
CONN_TYPE = "tcp" CONN_TYPE = "tcp"
NEW_LINE_BYTE = 0x0a
) )
var testAddrHex = "14791697260E4c9A71f18484C9f997B308e59325" var testAddrHex = "14791697260E4c9A71f18484C9f997B308e59325"
var testPrivHex = "0123456789012345678901234567890123456789012345678901234567890123" var testPrivHex = "0123456789012345678901234567890123456789012345678901234567890123"
var client = &http.Client{}
// This gives context to the signed message and prevents signing of transactions. // This gives context to the signed message and prevents signing of transactions.
func signHash(data []byte) []byte { func signHash(data []byte) []byte {
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data) msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
@ -28,7 +35,6 @@ func signHash(data []byte) []byte {
} }
func main() { func main() {
l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT) l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil { if err != nil {
fmt.Println("Error listening:", err.Error()) fmt.Println("Error listening:", err.Error())
@ -50,91 +56,128 @@ func main() {
// Handles incoming requests. // Handles incoming requests.
func handleRequest(conn net.Conn) { func handleRequest(conn net.Conn) {
// Make a buffer to hold incoming data.
// Read the incoming connection into the buffer.
go func() { messages := make(chan []byte)
buf := make([]byte, 1024) checkBalanceChan := make(chan []byte)
msgBuf := make([]byte, 0)
var prevMsg []byte
var addressOfClient []byte
var signatureFromClient []byte
var msgFromClient []byte
for { go readerConn(conn, messages)
msg, err := conn.Read(buf) go handleMessages(messages, checkBalanceChan)
go checkBalance(checkBalanceChan, conn)
if err == nil { sendRingSignedMsg(conn)
msgBuf = append(msgBuf, buf[:msg]...) }
}
index := bytes.IndexByte(msgBuf, 0x0a) func handleMessages(channel chan []byte, checkBalancesChan chan []byte) {
for index != -1 { var prevMsg []byte
newMsg := msgBuf[:index] var addressOfClient []byte
rest := msgBuf[(index + 1):len(msgBuf)] var signatureFromClient []byte
msgBuf = rest var msgFromClient []byte
index = bytes.IndexByte(msgBuf, 0x0a)
if prevMsg != nil { for {
s := string(prevMsg[:]) msg := <-channel
if s == "-- ADDRESS --" {
addressOfClient = newMsg //similar to shift in bash
} if prevMsg != nil {
if s == "-- SIGNATURE --" { s := string(prevMsg[:])
signatureFromClient = newMsg if s == "-- ADDRESS --" {
} addressOfClient = msg
if s == "-- MESSAGE --" { checkBalancesChan <- addressOfClient
msgFromClient = newMsg }
} if s == "-- SIGNATURE --" {
prevMsg = nil signatureFromClient = msg
} else { }
prevMsg = newMsg if s == "-- MESSAGE --" {
} msgFromClient = msg
} }
prevMsg = nil
if addressOfClient != nil && signatureFromClient != nil && msgFromClient != nil { } else {
sig := string(signatureFromClient[:]) prevMsg = msg
var sigByteArr, error = hexutil.Decode(sig) }
if error != nil { if addressOfClient != nil && signatureFromClient != nil && msgFromClient != nil {
fmt.Println(error) sig := string(signatureFromClient[:])
} var sigByteArr, error = hexutil.Decode(sig)
var sigHex = hexutil.Bytes(sigByteArr) if error != nil {
sigHex[64] -= 27 fmt.Println(error)
}
signedMsgHash := signHash(msgFromClient)
var sigHex = hexutil.Bytes(sigByteArr)
var rpk, err = crypto.Ecrecover(signedMsgHash, sigHex) sigHex[64] -= 27
if err != nil {
fmt.Println(err) signedMsgHash := signHash(msgFromClient)
}
var rpk, err = crypto.Ecrecover(signedMsgHash, sigHex)
pubKey := crypto.ToECDSAPub(rpk) if err != nil {
recoveredAddr := crypto.PubkeyToAddress(*pubKey) fmt.Println(err)
fmt.Println("ADDRESS IS ::", recoveredAddr.Hex()) }
}
} pubKey := crypto.ToECDSAPub(rpk)
}() recoveredAddr := crypto.PubkeyToAddress(*pubKey)
go func() { fmt.Println("ADDRESS IS ::", recoveredAddr.Hex())
key, _ := crypto.HexToECDSA(testPrivHex) }
}
f_msg := "Hello World" }
first_message := []byte(f_msg)
new_msg2 := crypto.Keccak256(first_message) func readerConn(conn net.Conn, channel chan []byte) {
fmt.Println("HASH IS ::", hexutil.Encode(new_msg2)) bufReader := bufio.NewReader(conn)
//send_message := append(new_msg2, []byte{byte(10)}...) for {
new_sig , err := crypto.Sign(new_msg2, key) msg, err := bufReader.ReadBytes(NEW_LINE_BYTE)
if err != nil {
fmt.Println("The crypto.Sign err is ", err) if err == io.EOF {
} fmt.Println("END OF FILE, CLOSING CONNECTION")
hex_sig := hexutil.Encode(new_sig) conn.Close()
fmt.Println("HEX SIG ::", hex_sig) conn = nil
break
conn.Write([]byte("Broadcasting Message")) }
conn.Write([]byte("\n")) if err != nil {
conn.Write([]byte(f_msg)) fmt.Println("Connection error: ", err)
conn.Write([]byte("\n")) break
conn.Write(new_sig) }
conn.Write([]byte("\n"))
}() msg = msg[:len(msg)-1] // remove trailing new line byte
channel <- msg
}
}
func checkBalance(checkBalanceChan chan []byte, conn net.Conn) {
address := <-checkBalanceChan
c, err := ethclient.Dial("http://127.0.0.1:8545")
if err != nil {
fmt.Println("Eth Client not initialized: " , err)
}
balance, error := c.BalanceAt(context.Background(), common.HexToAddress(string(address[:])),nil)
if error != nil {
fmt.Println("Balance at error ", error)
}
fmt.Println("the bal is ", balance)
fmt.Println("The balance for address ", address, " is ", balance)
conn.Write([]byte("Broadcasting Balance"))
conn.Write([]byte("\n"))
conn.Write([]byte(balance.String()))
conn.Write([]byte("\n"))
}
func sendRingSignedMsg(conn net.Conn){
key, _ := crypto.HexToECDSA(testPrivHex)
f_msg := "Hello World"
first_message := []byte(f_msg)
new_msg2 := crypto.Keccak256(first_message)
//send_message := append(new_msg2, []byte{byte(10)}...)
new_sig, err := crypto.Sign(new_msg2, key)
if err != nil {
fmt.Println("The crypto.Sign err is ", err)
}
conn.Write([]byte("Broadcasting Message"))
conn.Write([]byte("\n"))
conn.Write([]byte(f_msg))
conn.Write([]byte("\n"))
conn.Write(new_sig)
conn.Write([]byte("\n"))
} }