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

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

View file

@ -4,16 +4,19 @@ package main
//to run server 'go run shyftRingWalletConn/*.go' //to run server 'go run shyftRingWalletConn/*.go'
import ( import (
"bufio" "bufio"
"context"
"fmt" "fmt"
"github.com/ShyftNetwork/go-empyrean/common"
"github.com/ShyftNetwork/go-empyrean/common/hexutil" "github.com/ShyftNetwork/go-empyrean/common/hexutil"
"github.com/ShyftNetwork/go-empyrean/core/types"
"github.com/ShyftNetwork/go-empyrean/crypto" "github.com/ShyftNetwork/go-empyrean/crypto"
"github.com/ShyftNetwork/go-empyrean/ethclient"
"github.com/ShyftNetwork/go-empyrean/rlp"
"io" "io"
"net" "net"
"net/http" "net/http"
"os" "os"
"github.com/ShyftNetwork/go-empyrean/ethclient" "sync"
"github.com/ShyftNetwork/go-empyrean/common"
"context"
) )
const ( const (
@ -28,6 +31,8 @@ var testPrivHex = "0123456789012345678901234567890123456789012345678901234567890
var client = &http.Client{} var client = &http.Client{}
var mutex = &sync.Mutex{}
// 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)
@ -59,15 +64,17 @@ func handleRequest(conn net.Conn) {
messages := make(chan []byte) messages := make(chan []byte)
checkBalanceChan := make(chan []byte) checkBalanceChan := make(chan []byte)
sendTransactionChan := make(chan []byte)
go readerConn(conn, messages) go readerConn(conn, messages)
go handleMessages(messages, checkBalanceChan) go handleMessages(messages, checkBalanceChan, sendTransactionChan)
go checkBalance(checkBalanceChan, conn) go checkBalance(checkBalanceChan, conn)
go sendTransaction(sendTransactionChan)
sendRingSignedMsg(conn) sendRingSignedMsg(conn)
} }
func handleMessages(channel chan []byte, checkBalancesChan chan []byte) { func handleMessages(channel chan []byte, checkBalancesChan chan []byte, sendTransactionChan chan []byte) {
var prevMsg []byte var prevMsg []byte
var addressOfClient []byte var addressOfClient []byte
var signatureFromClient []byte var signatureFromClient []byte
@ -81,7 +88,13 @@ func handleMessages(channel chan []byte, checkBalancesChan chan []byte) {
s := string(prevMsg[:]) s := string(prevMsg[:])
if s == "-- ADDRESS --" { if s == "-- ADDRESS --" {
addressOfClient = msg addressOfClient = msg
checkBalancesChan <- addressOfClient //checkBalancesChan <- addressOfClient
}
if s == "-- GET_BALANCE --" {
checkBalancesChan <- msg
}
if s == "-- SEND_TRANSACTION --" {
sendTransactionChan <- msg
} }
if s == "-- SIGNATURE --" { if s == "-- SIGNATURE --" {
signatureFromClient = msg signatureFromClient = msg
@ -114,7 +127,9 @@ func handleMessages(channel chan []byte, checkBalancesChan chan []byte) {
pubKey := crypto.ToECDSAPub(rpk) pubKey := crypto.ToECDSAPub(rpk)
recoveredAddr := crypto.PubkeyToAddress(*pubKey) recoveredAddr := crypto.PubkeyToAddress(*pubKey)
fmt.Println("ADDRESS IS ::", recoveredAddr.Hex()) fmt.Println("Client connected with address :", recoveredAddr.Hex())
signatureFromClient = nil
msgFromClient = nil
} }
} }
} }
@ -143,25 +158,50 @@ func readerConn(conn net.Conn, channel chan []byte) {
} }
func checkBalance(checkBalanceChan chan []byte, conn net.Conn) { func checkBalance(checkBalanceChan chan []byte, conn net.Conn) {
address := <-checkBalanceChan
c, err := ethclient.Dial("http://127.0.0.1:8545") c, err := ethclient.Dial("http://127.0.0.1:8545")
if err != nil { if err != nil {
fmt.Println("Eth Client not initialized: " , err) fmt.Println("Eth Client not initialized: ", err)
} }
balance, error := c.BalanceAt(context.Background(), common.HexToAddress(string(address[:])),nil) for {
if error != nil { address := <-checkBalanceChan
fmt.Println("Balance at error ", error) fmt.Println("the address is ", string(address[:]))
balance, error := c.BalanceAt(context.Background(), common.HexToAddress(string(address[:])), nil)
if error != nil {
fmt.Println("Balance at error ", error)
}
mutex.Lock()
fmt.Println("The balance for address ", string(address[:]), " is ", balance)
conn.Write([]byte("Broadcasting Balance"))
conn.Write([]byte("\n"))
conn.Write([]byte(balance.String()))
conn.Write([]byte("\n"))
mutex.Unlock()
} }
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){ func sendTransaction(sendTransactionChan chan []byte) {
c, err := ethclient.Dial("http://127.0.0.1:8545")
if err != nil {
fmt.Println("Eth Client not initialized: ", err)
}
for {
signedTransactionBytes := <-sendTransactionChan
signedTransaction := string(signedTransactionBytes[:])
bytes, err := hexutil.Decode(signedTransaction)
if err != nil {
fmt.Println("error decoding signed transaction into bytes ", bytes)
}
var tx types.Transaction
rlp.DecodeBytes(bytes, &tx)
fmt.Println(tx.String())
c.SendTransaction(context.Background(), &tx)
}
}
func sendRingSignedMsg(conn net.Conn) {
key, _ := crypto.HexToECDSA(testPrivHex) key, _ := crypto.HexToECDSA(testPrivHex)
f_msg := "Hello World" f_msg := "Hello World"
@ -174,10 +214,12 @@ func sendRingSignedMsg(conn net.Conn){
fmt.Println("The crypto.Sign err is ", err) fmt.Println("The crypto.Sign err is ", err)
} }
mutex.Lock()
conn.Write([]byte("Broadcasting Message")) conn.Write([]byte("Broadcasting Message"))
conn.Write([]byte("\n")) conn.Write([]byte("\n"))
conn.Write([]byte(f_msg)) conn.Write([]byte(f_msg))
conn.Write([]byte("\n")) conn.Write([]byte("\n"))
conn.Write(new_sig) conn.Write(new_sig)
conn.Write([]byte("\n")) conn.Write([]byte("\n"))
mutex.Unlock()
} }