mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
194 lines
No EOL
4.5 KiB
Go
194 lines
No EOL
4.5 KiB
Go
package main
|
|
|
|
//@NOTE SHYFT main func for api, sets up router and spins up a server
|
|
//to run server 'go run shyftRingWalletConn/*.go'
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"github.com/ShyftNetwork/go-empyrean/common/hexutil"
|
|
"github.com/ShyftNetwork/go-empyrean/crypto"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"bytes"
|
|
"encoding/json"
|
|
"os"
|
|
)
|
|
|
|
const (
|
|
CONN_HOST = "localhost"
|
|
CONN_PORT = "3333"
|
|
CONN_TYPE = "tcp"
|
|
NEW_LINE_BYTE = 0x0a
|
|
)
|
|
|
|
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)
|
|
return crypto.Keccak256([]byte(msg))
|
|
}
|
|
|
|
func main() {
|
|
l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
|
|
if err != nil {
|
|
fmt.Println("Error listening:", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
defer l.Close()
|
|
|
|
for {
|
|
// Listen for an incoming connection.
|
|
conn, err := l.Accept()
|
|
if err != nil {
|
|
fmt.Println("Error accepting: ", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
// Handle connections in a new goroutine.
|
|
go handleRequest(conn)
|
|
}
|
|
}
|
|
|
|
// Handles incoming requests.
|
|
func handleRequest(conn net.Conn) {
|
|
|
|
messages := make(chan []byte)
|
|
checkBalanceChan := make(chan []byte)
|
|
|
|
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 addressOfClient []byte
|
|
var signatureFromClient []byte
|
|
var msgFromClient []byte
|
|
|
|
for {
|
|
msg := <-channel
|
|
|
|
//similar to shift in bash
|
|
if prevMsg != nil {
|
|
s := string(prevMsg[:])
|
|
if s == "-- ADDRESS --" {
|
|
addressOfClient = msg
|
|
checkBalancesChan <- addressOfClient
|
|
}
|
|
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())
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
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){
|
|
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"))
|
|
}
|
|
|
|
// 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)
|
|
} |