From 764124538757f334f5bc3b478893a8ca8da13b90 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Thu, 26 Jul 2018 17:04:17 -0400 Subject: [PATCH 01/12] fix import dependencies --- shyftRingWalletConn/app.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index 0da4d787e3..dcb6297daf 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -6,10 +6,9 @@ import ( "net" "fmt" "os" - //"github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" - "bytes" - "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ShyftNetwork/go-empyrean/crypto" + "bytes" + "github.com/ShyftNetwork/go-empyrean/common/hexutil" ) const ( From 4f534972195b4aa9160c86f97f4035c4f52b1b36 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Thu, 26 Jul 2018 17:18:42 -0400 Subject: [PATCH 02/12] add some error handling for closed connections --- shyftRingWalletConn/app.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index dcb6297daf..7fcff91f51 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -9,6 +9,7 @@ import ( "github.com/ShyftNetwork/go-empyrean/crypto" "bytes" "github.com/ShyftNetwork/go-empyrean/common/hexutil" + "io" ) const ( @@ -62,10 +63,18 @@ func handleRequest(conn net.Conn) { for { msg, err := conn.Read(buf) - - if err == nil { - msgBuf = append(msgBuf, buf[:msg]...) + 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 + } + + msgBuf = append(msgBuf, buf[:msg]...) index := bytes.IndexByte(msgBuf, 0x0a) for index != -1 { newMsg := msgBuf[:index] From 4833f428e52f1afe5dd893334e2a7c63d09f205e Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Thu, 26 Jul 2018 17:19:28 -0400 Subject: [PATCH 03/12] fix formatting of app.go --- shyftRingWalletConn/app.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index 7fcff91f51..b91d26bfab 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -3,13 +3,13 @@ package main //@NOTE SHYFT main func for api, sets up router and spins up a server //to run server 'go run shyftRingWalletConn/*.go' import ( - "net" - "fmt" - "os" - "github.com/ShyftNetwork/go-empyrean/crypto" - "bytes" - "github.com/ShyftNetwork/go-empyrean/common/hexutil" - "io" + "bytes" + "fmt" + "github.com/ShyftNetwork/go-empyrean/common/hexutil" + "github.com/ShyftNetwork/go-empyrean/crypto" + "io" + "net" + "os" ) const ( @@ -131,7 +131,7 @@ func handleRequest(conn net.Conn) { fmt.Println("HASH IS ::", hexutil.Encode(new_msg2)) //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 { fmt.Println("The crypto.Sign err is ", err) } @@ -145,4 +145,4 @@ func handleRequest(conn net.Conn) { conn.Write(new_sig) conn.Write([]byte("\n")) }() -} \ No newline at end of file +} From 8cc8da8ea5cfbf6a1bc51c89d36ad592cc2472c7 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Fri, 27 Jul 2018 14:03:15 -0400 Subject: [PATCH 04/12] refactor to use bufReader.ReadBytes when accepting incoming messages from the connection socket --- shyftRingWalletConn/app.go | 95 ++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 50 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index b91d26bfab..eced77f9a3 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -3,7 +3,8 @@ package main //@NOTE SHYFT main func for api, sets up router and spins up a server //to run server 'go run shyftRingWalletConn/*.go' import ( - "bytes" + //"bytes" + "bufio" "fmt" "github.com/ShyftNetwork/go-empyrean/common/hexutil" "github.com/ShyftNetwork/go-empyrean/crypto" @@ -13,9 +14,10 @@ import ( ) const ( - CONN_HOST = "localhost" - CONN_PORT = "3333" - CONN_TYPE = "tcp" + CONN_HOST = "localhost" + CONN_PORT = "3333" + CONN_TYPE = "tcp" + NEW_LINE_BYTE = 0x0a ) var testAddrHex = "14791697260E4c9A71f18484C9f997B308e59325" @@ -50,19 +52,17 @@ func main() { // Handles incoming requests. func handleRequest(conn net.Conn) { - // Make a buffer to hold incoming data. - // Read the incoming connection into the buffer. go func() { - buf := make([]byte, 1024) - msgBuf := make([]byte, 0) var prevMsg []byte var addressOfClient []byte var signatureFromClient []byte var msgFromClient []byte + bufReader := bufio.NewReader(conn) for { - msg, err := conn.Read(buf) + msg, err := bufReader.ReadBytes(NEW_LINE_BYTE) + if err == io.EOF { fmt.Println("END OF FILE, CLOSING CONNECTION") conn.Close() @@ -74,28 +74,23 @@ func handleRequest(conn net.Conn) { break } - 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 { - s := string(prevMsg[:]) - if s == "-- ADDRESS --" { - addressOfClient = newMsg - } - if s == "-- SIGNATURE --" { - signatureFromClient = newMsg - } - if s == "-- MESSAGE --" { - msgFromClient = newMsg - } - prevMsg = nil - } else { - prevMsg = newMsg + msg = msg[:len(msg)-1] // remove trailing new line byte + + // similar to shift in bash + if prevMsg != nil { + s := string(prevMsg[:]) + if s == "-- ADDRESS --" { + addressOfClient = msg } + if s == "-- SIGNATURE --" { + signatureFromClient = msg + } + if s == "-- MESSAGE --" { + msgFromClient = msg + } + prevMsg = nil + } else { + prevMsg = msg } if addressOfClient != nil && signatureFromClient != nil && msgFromClient != nil { @@ -122,27 +117,27 @@ func handleRequest(conn net.Conn) { } } }() - go func() { - key, _ := crypto.HexToECDSA(testPrivHex) - f_msg := "Hello World" - first_message := []byte(f_msg) - new_msg2 := crypto.Keccak256(first_message) - fmt.Println("HASH IS ::", hexutil.Encode(new_msg2)) + key, _ := crypto.HexToECDSA(testPrivHex) - //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) - } - hex_sig := hexutil.Encode(new_sig) - fmt.Println("HEX SIG ::", hex_sig) + f_msg := "Hello World" + first_message := []byte(f_msg) + new_msg2 := crypto.Keccak256(first_message) + fmt.Println("HASH IS ::", hexutil.Encode(new_msg2)) + + //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) + } + hex_sig := hexutil.Encode(new_sig) + fmt.Println("HEX SIG ::", hex_sig) + + 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")) - 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")) - }() } From bd83244bbab0edb856b4776d5415a2c19cf16b0e Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Fri, 27 Jul 2018 15:01:34 -0400 Subject: [PATCH 05/12] refactor reading of socket into a function --- shyftRingWalletConn/app.go | 131 +++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 65 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index eced77f9a3..b852547952 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -3,7 +3,6 @@ package main //@NOTE SHYFT main func for api, sets up router and spins up a server //to run server 'go run shyftRingWalletConn/*.go' import ( - //"bytes" "bufio" "fmt" "github.com/ShyftNetwork/go-empyrean/common/hexutil" @@ -53,70 +52,7 @@ func main() { // Handles incoming requests. func handleRequest(conn net.Conn) { - go func() { - var prevMsg []byte - var addressOfClient []byte - var signatureFromClient []byte - var msgFromClient []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 - - // similar to shift in bash - if prevMsg != nil { - s := string(prevMsg[:]) - if s == "-- ADDRESS --" { - addressOfClient = msg - } - if s == "-- SIGNATURE --" { - signatureFromClient = msg - } - if s == "-- MESSAGE --" { - msgFromClient = msg - } - prevMsg = nil - } else { - prevMsg = msg - } - - if addressOfClient != nil && signatureFromClient != nil && msgFromClient != nil { - sig := string(signatureFromClient[:]) - var sigByteArr, error = hexutil.Decode(sig) - - if error != nil { - fmt.Println(error) - } - - var sigHex = hexutil.Bytes(sigByteArr) - sigHex[64] -= 27 - - signedMsgHash := signHash(msgFromClient) - - var rpk, err = crypto.Ecrecover(signedMsgHash, sigHex) - if err != nil { - fmt.Println(err) - } - - pubKey := crypto.ToECDSAPub(rpk) - recoveredAddr := crypto.PubkeyToAddress(*pubKey) - fmt.Println("ADDRESS IS ::", recoveredAddr.Hex()) - } - } - }() + go readerConn(conn) key, _ := crypto.HexToECDSA(testPrivHex) @@ -141,3 +77,68 @@ func handleRequest(conn net.Conn) { conn.Write([]byte("\n")) } + +func readerConn(conn net.Conn) { + var prevMsg []byte + var addressOfClient []byte + var signatureFromClient []byte + var msgFromClient []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 + + // similar to shift in bash + if prevMsg != nil { + s := string(prevMsg[:]) + if s == "-- ADDRESS --" { + addressOfClient = msg + } + if s == "-- SIGNATURE --" { + signatureFromClient = msg + } + if s == "-- MESSAGE --" { + msgFromClient = msg + } + prevMsg = nil + } else { + prevMsg = msg + } + + if addressOfClient != nil && signatureFromClient != nil && msgFromClient != nil { + sig := string(signatureFromClient[:]) + var sigByteArr, error = hexutil.Decode(sig) + + if error != nil { + fmt.Println(error) + } + + var sigHex = hexutil.Bytes(sigByteArr) + sigHex[64] -= 27 + + signedMsgHash := signHash(msgFromClient) + + var rpk, err = crypto.Ecrecover(signedMsgHash, sigHex) + if err != nil { + fmt.Println(err) + } + + pubKey := crypto.ToECDSAPub(rpk) + recoveredAddr := crypto.PubkeyToAddress(*pubKey) + fmt.Println("ADDRESS IS ::", recoveredAddr.Hex()) + } + } +} From cad6a4ae24fab6c57dd46f74ac06277446c23610 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Fri, 27 Jul 2018 15:33:56 -0400 Subject: [PATCH 06/12] refactor into new connections that use channels --- shyftRingWalletConn/app.go | 52 ++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index b852547952..2c9d5c5eb7 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -5,11 +5,13 @@ package main import ( "bufio" "fmt" - "github.com/ShyftNetwork/go-empyrean/common/hexutil" + //"github.com/ShyftNetwork/go-empyrean/common/hexutil" "github.com/ShyftNetwork/go-empyrean/crypto" "io" "net" "os" + + "github.com/ShyftNetwork/go-empyrean/common/hexutil" ) const ( @@ -52,7 +54,10 @@ func main() { // Handles incoming requests. func handleRequest(conn net.Conn) { - go readerConn(conn) + messages := make(chan []byte) + + go readerConn(conn, messages) + go handleMessages(messages) key, _ := crypto.HexToECDSA(testPrivHex) @@ -78,30 +83,16 @@ func handleRequest(conn net.Conn) { } -func readerConn(conn net.Conn) { +func handleMessages(channel chan []byte) { var prevMsg []byte var addressOfClient []byte var signatureFromClient []byte var msgFromClient []byte - bufReader := bufio.NewReader(conn) for { - msg, err := bufReader.ReadBytes(NEW_LINE_BYTE) + msg := <-channel - 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 - - // similar to shift in bash + //similar to shift in bash if prevMsg != nil { s := string(prevMsg[:]) if s == "-- ADDRESS --" { @@ -142,3 +133,26 @@ func readerConn(conn net.Conn) { } } } + +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 + } +} From aff5dbd48db4c828f86305166663a6acd060351e Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Fri, 27 Jul 2018 15:34:53 -0400 Subject: [PATCH 07/12] rm redundant import --- shyftRingWalletConn/app.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index 2c9d5c5eb7..d44c5448e1 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -5,12 +5,10 @@ package main import ( "bufio" "fmt" - //"github.com/ShyftNetwork/go-empyrean/common/hexutil" "github.com/ShyftNetwork/go-empyrean/crypto" "io" "net" "os" - "github.com/ShyftNetwork/go-empyrean/common/hexutil" ) From 142140a3ef74cb071e8f3ea0de14c1128b52712e Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Fri, 27 Jul 2018 22:25:58 -0400 Subject: [PATCH 08/12] factor out more functions --- shyftRingWalletConn/app.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index d44c5448e1..b8a87beca4 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -5,11 +5,11 @@ package main import ( "bufio" "fmt" + "github.com/ShyftNetwork/go-empyrean/common/hexutil" "github.com/ShyftNetwork/go-empyrean/crypto" "io" "net" "os" - "github.com/ShyftNetwork/go-empyrean/common/hexutil" ) const ( @@ -53,24 +53,24 @@ func main() { func handleRequest(conn net.Conn) { messages := make(chan []byte) + checkBalanceChan := make(chan []byte) go readerConn(conn, messages) - go handleMessages(messages) + go handleMessages(messages, checkBalanceChan) + + go checkBalance(checkBalanceChan) key, _ := crypto.HexToECDSA(testPrivHex) f_msg := "Hello World" first_message := []byte(f_msg) new_msg2 := crypto.Keccak256(first_message) - fmt.Println("HASH IS ::", hexutil.Encode(new_msg2)) //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) } - hex_sig := hexutil.Encode(new_sig) - fmt.Println("HEX SIG ::", hex_sig) conn.Write([]byte("Broadcasting Message")) conn.Write([]byte("\n")) @@ -78,10 +78,9 @@ func handleRequest(conn net.Conn) { conn.Write([]byte("\n")) conn.Write(new_sig) conn.Write([]byte("\n")) - } -func handleMessages(channel chan []byte) { +func handleMessages(channel chan []byte, checkBalancesChan chan []byte) { var prevMsg []byte var addressOfClient []byte var signatureFromClient []byte @@ -95,6 +94,7 @@ func handleMessages(channel chan []byte) { s := string(prevMsg[:]) if s == "-- ADDRESS --" { addressOfClient = msg + checkBalancesChan <- addressOfClient } if s == "-- SIGNATURE --" { signatureFromClient = msg @@ -154,3 +154,8 @@ func readerConn(conn net.Conn, channel chan []byte) { channel <- msg } } + +func checkBalance(checkBalanceChan chan []byte) { + address := <-checkBalanceChan + fmt.Println("The address for balance check is ", address) +} From 953d40533077e3d75e121dc6a7e1daa6384b88d8 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Fri, 27 Jul 2018 22:28:28 -0400 Subject: [PATCH 09/12] factor out another fn --- shyftRingWalletConn/app.go | 41 ++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index b8a87beca4..009914fed9 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -57,27 +57,9 @@ func handleRequest(conn net.Conn) { go readerConn(conn, messages) go handleMessages(messages, checkBalanceChan) - go checkBalance(checkBalanceChan) - 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")) + sendRingSignedMsg(conn) } func handleMessages(channel chan []byte, checkBalancesChan chan []byte) { @@ -159,3 +141,24 @@ func checkBalance(checkBalanceChan chan []byte) { address := <-checkBalanceChan fmt.Println("The address for balance check is ", address) } + +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")) +} \ No newline at end of file From 196a0a91a563a55b4d957706b6f7b2e6d2ad1ec1 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Tue, 31 Jul 2018 12:49:34 -0400 Subject: [PATCH 10/12] add getbalance rpc call --- shyftRingWalletConn/app.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index 009914fed9..5ddaa4e255 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -9,6 +9,9 @@ import ( "github.com/ShyftNetwork/go-empyrean/crypto" "io" "net" + "net/http" + "bytes" + "encoding/json" "os" ) @@ -22,6 +25,8 @@ const ( var testAddrHex = "14791697260E4c9A71f18484C9f997B308e59325" var testPrivHex = "0123456789012345678901234567890123456789012345678901234567890123" +var client = &http.Client{} + // 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) @@ -29,7 +34,6 @@ func signHash(data []byte) []byte { } func main() { - l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT) if err != nil { fmt.Println("Error listening:", err.Error()) @@ -139,7 +143,8 @@ func readerConn(conn net.Conn, channel chan []byte) { func checkBalance(checkBalanceChan chan []byte) { address := <-checkBalanceChan - fmt.Println("The address for balance check is ", address) + bal := getBalance(string(address[:]), client) + fmt.Println("The balance for address ", address, " is ", bal) } func sendRingSignedMsg(conn net.Conn){ @@ -161,4 +166,25 @@ func sendRingSignedMsg(conn net.Conn){ conn.Write([]byte("\n")) conn.Write(new_sig) conn.Write([]byte("\n")) +} + +// http request to the rpc server to get balance for an address +func getBalance(address string, client *http.Client) string { + jsonStr := fmt.Sprintf(`{"jsonrpc":"2.0","method":"eth_getBalance","params":["%s", "latest"],"id":67}` , address) + jsonBytes := []byte(jsonStr) + fmt.Println(string(jsonBytes)) + + req, err := http.NewRequest("POST", "http://localhost:8545", bytes.NewBuffer(jsonBytes)) + if err != nil { + fmt.Println("Error: " , err) + } + req.Header.Set("Content-Type", "application/json") + resp, err := client.Do(req) + if err != nil { + fmt.Println("Error: ", err) + } + var target map[string]interface{} + decoder := json.NewDecoder(resp.Body) + decoder.Decode(&target) + return target["result"].(string) } \ No newline at end of file From fde20744f129b2688ebafc4e42e997d5912af356 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Tue, 31 Jul 2018 14:46:02 -0400 Subject: [PATCH 11/12] send balance back to wallet --- shyftRingWalletConn/app.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index 5ddaa4e255..93ccd26b86 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -61,7 +61,7 @@ func handleRequest(conn net.Conn) { go readerConn(conn, messages) go handleMessages(messages, checkBalanceChan) - go checkBalance(checkBalanceChan) + go checkBalance(checkBalanceChan, conn) sendRingSignedMsg(conn) } @@ -141,10 +141,14 @@ func readerConn(conn net.Conn, channel chan []byte) { } } -func checkBalance(checkBalanceChan chan []byte) { +func checkBalance(checkBalanceChan chan []byte, conn net.Conn) { address := <-checkBalanceChan bal := getBalance(string(address[:]), client) fmt.Println("The balance for address ", address, " is ", bal) + conn.Write([]byte("Broadcasting Balance")) + conn.Write([]byte("\n")) + conn.Write([]byte(bal)) + conn.Write([]byte("\n")) } func sendRingSignedMsg(conn net.Conn){ From 48486e2bb202216390a812718ad2d69dffd7be76 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Tue, 31 Jul 2018 15:13:00 -0400 Subject: [PATCH 12/12] refactor to use ethclient --- shyftRingWalletConn/app.go | 41 ++++++++++++++------------------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index 93ccd26b86..fe0daf40e9 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -10,9 +10,10 @@ import ( "io" "net" "net/http" - "bytes" - "encoding/json" "os" + "github.com/ShyftNetwork/go-empyrean/ethclient" + "github.com/ShyftNetwork/go-empyrean/common" + "context" ) const ( @@ -143,11 +144,20 @@ func readerConn(conn net.Conn, channel chan []byte) { func checkBalance(checkBalanceChan chan []byte, conn net.Conn) { address := <-checkBalanceChan - bal := getBalance(string(address[:]), client) - fmt.Println("The balance for address ", address, " is ", bal) + 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(bal)) + conn.Write([]byte(balance.String())) conn.Write([]byte("\n")) } @@ -170,25 +180,4 @@ func sendRingSignedMsg(conn net.Conn){ conn.Write([]byte("\n")) conn.Write(new_sig) conn.Write([]byte("\n")) -} - -// http request to the rpc server to get balance for an address -func getBalance(address string, client *http.Client) string { - jsonStr := fmt.Sprintf(`{"jsonrpc":"2.0","method":"eth_getBalance","params":["%s", "latest"],"id":67}` , address) - jsonBytes := []byte(jsonStr) - fmt.Println(string(jsonBytes)) - - req, err := http.NewRequest("POST", "http://localhost:8545", bytes.NewBuffer(jsonBytes)) - if err != nil { - fmt.Println("Error: " , err) - } - req.Header.Set("Content-Type", "application/json") - resp, err := client.Do(req) - if err != nil { - fmt.Println("Error: ", err) - } - var target map[string]interface{} - decoder := json.NewDecoder(resp.Body) - decoder.Decode(&target) - return target["result"].(string) } \ No newline at end of file