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"
"github.com/ShyftNetwork/go-empyrean/common/hexutil"
"github.com/ShyftNetwork/go-empyrean/crypto"
"io"
"net"
"net/http"
"os" "os"
//"github.com/ethereum/go-ethereum/common" "github.com/ShyftNetwork/go-empyrean/ethclient"
"github.com/ethereum/go-ethereum/crypto" "github.com/ShyftNetwork/go-empyrean/common"
"bytes" "context"
"github.com/ethereum/go-ethereum/common/hexutil"
) )
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,44 +56,42 @@ 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)
go readerConn(conn, messages)
go handleMessages(messages, checkBalanceChan)
go checkBalance(checkBalanceChan, conn)
sendRingSignedMsg(conn)
}
func handleMessages(channel chan []byte, checkBalancesChan chan []byte) {
var prevMsg []byte var prevMsg []byte
var addressOfClient []byte var addressOfClient []byte
var signatureFromClient []byte var signatureFromClient []byte
var msgFromClient []byte var msgFromClient []byte
for { for {
msg, err := conn.Read(buf) msg := <-channel
if err == nil { //similar to shift in bash
msgBuf = append(msgBuf, buf[:msg]...)
}
index := bytes.IndexByte(msgBuf, 0x0a)
for index != -1 {
newMsg := msgBuf[:index]
rest := msgBuf[(index + 1):len(msgBuf)]
msgBuf = rest
index = bytes.IndexByte(msgBuf, 0x0a)
if prevMsg != nil { if prevMsg != nil {
s := string(prevMsg[:]) s := string(prevMsg[:])
if s == "-- ADDRESS --" { if s == "-- ADDRESS --" {
addressOfClient = newMsg addressOfClient = msg
checkBalancesChan <- addressOfClient
} }
if s == "-- SIGNATURE --" { if s == "-- SIGNATURE --" {
signatureFromClient = newMsg signatureFromClient = msg
} }
if s == "-- MESSAGE --" { if s == "-- MESSAGE --" {
msgFromClient = newMsg msgFromClient = msg
} }
prevMsg = nil prevMsg = nil
} else { } else {
prevMsg = newMsg prevMsg = msg
}
} }
if addressOfClient != nil && signatureFromClient != nil && msgFromClient != nil { if addressOfClient != nil && signatureFromClient != nil && msgFromClient != nil {
@ -113,22 +117,62 @@ func handleRequest(conn net.Conn) {
fmt.Println("ADDRESS IS ::", recoveredAddr.Hex()) fmt.Println("ADDRESS IS ::", recoveredAddr.Hex())
} }
} }
}() }
go func() {
func readerConn(conn net.Conn, channel chan []byte) {
bufReader := bufio.NewReader(conn)
for {
msg, err := bufReader.ReadBytes(NEW_LINE_BYTE)
if err == io.EOF {
fmt.Println("END OF FILE, CLOSING CONNECTION")
conn.Close()
conn = nil
break
}
if err != nil {
fmt.Println("Connection error: ", err)
break
}
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) key, _ := crypto.HexToECDSA(testPrivHex)
f_msg := "Hello World" f_msg := "Hello World"
first_message := []byte(f_msg) first_message := []byte(f_msg)
new_msg2 := crypto.Keccak256(first_message) new_msg2 := crypto.Keccak256(first_message)
fmt.Println("HASH IS ::", hexutil.Encode(new_msg2))
//send_message := append(new_msg2, []byte{byte(10)}...) //send_message := append(new_msg2, []byte{byte(10)}...)
new_sig, err := crypto.Sign(new_msg2, key) new_sig, err := crypto.Sign(new_msg2, key)
if err != nil { if err != nil {
fmt.Println("The crypto.Sign err is ", err) fmt.Println("The crypto.Sign err is ", err)
} }
hex_sig := hexutil.Encode(new_sig)
fmt.Println("HEX SIG ::", hex_sig)
conn.Write([]byte("Broadcasting Message")) conn.Write([]byte("Broadcasting Message"))
conn.Write([]byte("\n")) conn.Write([]byte("\n"))
@ -136,5 +180,4 @@ func handleRequest(conn net.Conn) {
conn.Write([]byte("\n")) conn.Write([]byte("\n"))
conn.Write(new_sig) conn.Write(new_sig)
conn.Write([]byte("\n")) conn.Write([]byte("\n"))
}()
} }