From bbb1f899439c44027260088d8221ae7b7c332086 Mon Sep 17 00:00:00 2001 From: otherview Date: Fri, 22 Sep 2023 15:35:31 +0100 Subject: [PATCH 1/4] fix --- integration/manualtests/connection_test.go | 123 ++++++++++++++++++ .../obscurogateway/obscurogateway_test.go | 1 + tools/walletextension/api/server.go | 8 +- tools/walletextension/userconn/user_conn.go | 6 +- 4 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 integration/manualtests/connection_test.go diff --git a/integration/manualtests/connection_test.go b/integration/manualtests/connection_test.go new file mode 100644 index 0000000000..37a19e11fe --- /dev/null +++ b/integration/manualtests/connection_test.go @@ -0,0 +1,123 @@ +package manualtests + +import ( + "context" + "encoding/hex" + "fmt" + "github.com/ethereum/go-ethereum" + gethcommon "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/obscuronet/go-obscuro/tools/walletextension/common" + "github.com/stretchr/testify/require" + "github.com/valyala/fasthttp" + "io" + "log" + "net/http" + "os" + "strings" + "testing" +) + +func TestSubscribeToOG(t *testing.T) { + if os.Getenv(_IDEFlag) == "" { + t.Skipf("set flag %s to run this test in the IDE", _IDEFlag) + } + + // Using http + ogHTTPAddress := "dev-testnet.obscu.ro:443" + ogWSAddress := "dev-testnet.obscu.ro:81" + + // join the network + statusCode, body, err := fasthttp.Get(nil, fmt.Sprintf("https://%s/join/", ogHTTPAddress)) + require.NoError(t, err) // dialing to the given TCP address timed out + fmt.Println(statusCode) + fmt.Println(body) + + // sign the message + messagePayload := signMessage(string(body)) + + // register an account + body, err = registerAccount(ogHTTPAddress, messagePayload) + require.NoError(t, err) + fmt.Println(body) + + // Using WS -> + + // Connect to WebSocket server using the standard geth client + client, err := ethclient.Dial("ws://" + ogWSAddress) + require.NoError(t, err) + + // Create a simple request + at, err := client.BalanceAt(context.Background(), l2Wallet.Address(), nil) + require.NoError(t, err) + + fmt.Println("Balance for account ", l2Wallet.Address().Hex(), " - ", at.String()) + + // Create a subscription + query := ethereum.FilterQuery{ + Addresses: []gethcommon.Address{l2Wallet.Address()}, + } + + logs := make(chan types.Log) + sub, err := client.SubscribeFilterLogs(context.Background(), query, logs) + if err != nil { + log.Fatalf("Failed to subscribe: %v", err) + } + defer sub.Unsubscribe() + + // Listen for events from the contract + for { + select { + case err := <-sub.Err(): + log.Fatalf("Subscription error: %v", err) + case vLog := <-logs: + // Process the contract event + // This is just a simple example printing the block number; you'll want to decode and handle the logs according to your contract's ABI + log.Printf("Received log in block number: %v", vLog.BlockNumber) + } + } +} + +func registerAccount(baseAddress, payload string) ([]byte, error) { + req, err := http.NewRequestWithContext( + context.Background(), + http.MethodPost, + "https://"+baseAddress+"/authenticate/", + strings.NewReader(payload), + ) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json; charset=UTF-8") + + client := &http.Client{} + response, err := client.Do(req) + if err != nil { + return nil, err + } + + defer response.Body.Close() + return io.ReadAll(response.Body) +} + +// { +// "signature": "0xc784adea83ed3ec60528f4747418c85abe553b35a47fd2c95425de654bb9d0d40ede24aec182e6a2ec65c0c7c6aedab7823f21a9b9f7ff5db3a77a9f90dc97b41c", +// "message": "Register e097c4a10d4285d13b377985834b4c57e069b5856cc6c2cd4a038f62da4bc459 for 0x06ed49a32fcc5094abee51a4ffd46dd23b62a191" +// } +func signMessage(userID string) string { + pk := l2Wallet.PrivateKey() + address := l2Wallet.Address() + hexAddress := address.Hex() + + message := fmt.Sprintf("Register %s for %s", userID, strings.ToLower(hexAddress)) + prefixedMessage := fmt.Sprintf(common.PersonalSignMessagePrefix, len(message), message) + + messageHash := crypto.Keccak256([]byte(prefixedMessage)) + sig, err := crypto.Sign(messageHash, pk) + if err != nil { + log.Fatalf("Failed to sign message: %v", err) + } + return fmt.Sprintf("{\"signature\": \"%s\", \"message\": \"%s\"}", hex.EncodeToString(sig), message) +} diff --git a/integration/obscurogateway/obscurogateway_test.go b/integration/obscurogateway/obscurogateway_test.go index 02d1967878..d724717025 100644 --- a/integration/obscurogateway/obscurogateway_test.go +++ b/integration/obscurogateway/obscurogateway_test.go @@ -45,6 +45,7 @@ const ( ) func TestObscuroGateway(t *testing.T) { + t.Skip("Commented it out until more testing is driven from this test") t.Skip("Commented it out until more testing is driven from this test") startPort := integration.StartPortObscuroGatewayUnitTest wallets := createObscuroNetwork(t, startPort) diff --git a/tools/walletextension/api/server.go b/tools/walletextension/api/server.go index 0b7684c16a..a8e5a2963f 100644 --- a/tools/walletextension/api/server.go +++ b/tools/walletextension/api/server.go @@ -2,6 +2,7 @@ package api import ( "context" + "crypto/tls" "embed" "fmt" "io/fs" @@ -88,5 +89,10 @@ func createWSServer(address string, routes []Route) *http.Server { serveMux.HandleFunc(route.Name, route.Func) } - return &http.Server{Addr: address, Handler: serveMux, ReadHeaderTimeout: 10 * time.Second} + return &http.Server{ + Addr: address, + Handler: serveMux, + ReadHeaderTimeout: 10 * time.Second, + TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)), + } } diff --git a/tools/walletextension/userconn/user_conn.go b/tools/walletextension/userconn/user_conn.go index b40e0ed28e..a733a1d9a2 100644 --- a/tools/walletextension/userconn/user_conn.go +++ b/tools/walletextension/userconn/user_conn.go @@ -57,9 +57,9 @@ func NewUserConnWS(resp http.ResponseWriter, req *http.Request, logger gethlog.L // We search all the request's headers. If there's a websocket upgrade header, we upgrade to a websocket connection. conn, err := upgrader.Upgrade(resp, req, nil) if err != nil { - err = fmt.Errorf("unable to upgrade to websocket connection") - logger.Error("unable to upgrade to websocket connection") - httpLogAndSendErr(resp, err.Error()) // todo (@ziga) - Handle error properly for websockets. + err = fmt.Errorf("unable to upgrade to websocket connection - %w", err) + logger.Error("unable to upgrade to websocket connection", log.ErrKey, err) + httpLogAndSendErr(resp, err.Error()) return nil, err } From f9a78b85768ab4d61c25d97cf2278a3c9e00d233 Mon Sep 17 00:00:00 2001 From: otherview Date: Fri, 22 Sep 2023 17:15:36 +0100 Subject: [PATCH 2/4] fix --- integration/manualtests/connection_test.go | 52 ++++++++++++--------- tools/walletextension/api/server.go | 8 +--- tools/walletextension/userconn/user_conn.go | 2 +- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/integration/manualtests/connection_test.go b/integration/manualtests/connection_test.go index 37a19e11fe..00bf8d46ec 100644 --- a/integration/manualtests/connection_test.go +++ b/integration/manualtests/connection_test.go @@ -11,40 +11,46 @@ import ( "github.com/ethereum/go-ethereum/ethclient" "github.com/obscuronet/go-obscuro/tools/walletextension/common" "github.com/stretchr/testify/require" - "github.com/valyala/fasthttp" "io" "log" "net/http" - "os" "strings" "testing" + "time" ) func TestSubscribeToOG(t *testing.T) { - if os.Getenv(_IDEFlag) == "" { - t.Skipf("set flag %s to run this test in the IDE", _IDEFlag) - } - + //if os.Getenv(_IDEFlag) == "" { + // t.Skipf("set flag %s to run this test in the IDE", _IDEFlag) + //} + // Using http - ogHTTPAddress := "dev-testnet.obscu.ro:443" + //ogHTTPAddress := "dev-testnet.obscu.ro:443" ogWSAddress := "dev-testnet.obscu.ro:81" - - // join the network - statusCode, body, err := fasthttp.Get(nil, fmt.Sprintf("https://%s/join/", ogHTTPAddress)) - require.NoError(t, err) // dialing to the given TCP address timed out - fmt.Println(statusCode) - fmt.Println(body) - - // sign the message - messagePayload := signMessage(string(body)) - - // register an account - body, err = registerAccount(ogHTTPAddress, messagePayload) - require.NoError(t, err) - fmt.Println(body) + //ogWSAddress := "51.132.131.47:81" + + //// join the network + //statusCode, userID, err := fasthttp.Get(nil, fmt.Sprintf("https://%s/v1/join/", ogHTTPAddress)) + //require.NoError(t, err) // dialing to the given TCP address timed out + //fmt.Println(statusCode) + //fmt.Println(userID) + // + //// sign the message + //messagePayload := signMessage(string(userID)) + // + //// register an account + //var regAccountResp []byte + //regAccountResp, err = registerAccount(ogHTTPAddress, string(userID), messagePayload) + //require.NoError(t, err) + //fmt.Println(regAccountResp) // Using WS -> + for i := 0; i < 50; i++ { + ethclient.Dial("ws://" + ogWSAddress) + time.Sleep(100 * time.Millisecond) + } + // Connect to WebSocket server using the standard geth client client, err := ethclient.Dial("ws://" + ogWSAddress) require.NoError(t, err) @@ -80,11 +86,11 @@ func TestSubscribeToOG(t *testing.T) { } } -func registerAccount(baseAddress, payload string) ([]byte, error) { +func registerAccount(baseAddress, userID, payload string) ([]byte, error) { req, err := http.NewRequestWithContext( context.Background(), http.MethodPost, - "https://"+baseAddress+"/authenticate/", + "https://"+baseAddress+"/authenticate/?u="+userID, strings.NewReader(payload), ) if err != nil { diff --git a/tools/walletextension/api/server.go b/tools/walletextension/api/server.go index a8e5a2963f..0b7684c16a 100644 --- a/tools/walletextension/api/server.go +++ b/tools/walletextension/api/server.go @@ -2,7 +2,6 @@ package api import ( "context" - "crypto/tls" "embed" "fmt" "io/fs" @@ -89,10 +88,5 @@ func createWSServer(address string, routes []Route) *http.Server { serveMux.HandleFunc(route.Name, route.Func) } - return &http.Server{ - Addr: address, - Handler: serveMux, - ReadHeaderTimeout: 10 * time.Second, - TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)), - } + return &http.Server{Addr: address, Handler: serveMux, ReadHeaderTimeout: 10 * time.Second} } diff --git a/tools/walletextension/userconn/user_conn.go b/tools/walletextension/userconn/user_conn.go index a733a1d9a2..c8b228fdd5 100644 --- a/tools/walletextension/userconn/user_conn.go +++ b/tools/walletextension/userconn/user_conn.go @@ -139,7 +139,7 @@ func (w *userConnWS) WriteResponse(msg []byte) error { // HandleError logs and prints the error, and writes it to the websocket as a JSON object with a single key, "error". func (w *userConnWS) HandleError(msg string) { - w.logger.Error(msg) + w.logger.Error("error in user WS connection - " + msg) errMsg, err := json.Marshal(map[string]interface{}{ common.JSONKeyErr: msg, From f752a93d803604fa399af1e0e66e013ce96ba8b4 Mon Sep 17 00:00:00 2001 From: otherview Date: Fri, 22 Sep 2023 18:10:56 +0100 Subject: [PATCH 3/4] fix fix --- integration/manualtests/connection_test.go | 72 +++++++++---------- .../obscurogateway/obscurogateway_test.go | 1 - 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/integration/manualtests/connection_test.go b/integration/manualtests/connection_test.go index 00bf8d46ec..987d144b48 100644 --- a/integration/manualtests/connection_test.go +++ b/integration/manualtests/connection_test.go @@ -4,55 +4,51 @@ import ( "context" "encoding/hex" "fmt" + "io" + "log" + "net/http" + "strings" + "testing" + "github.com/ethereum/go-ethereum" - gethcommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" "github.com/obscuronet/go-obscuro/tools/walletextension/common" "github.com/stretchr/testify/require" - "io" - "log" - "net/http" - "strings" - "testing" - "time" + "github.com/valyala/fasthttp" + + gethcommon "github.com/ethereum/go-ethereum/common" ) func TestSubscribeToOG(t *testing.T) { - //if os.Getenv(_IDEFlag) == "" { - // t.Skipf("set flag %s to run this test in the IDE", _IDEFlag) - //} + t.Skip("skip manual tests") // Using http - //ogHTTPAddress := "dev-testnet.obscu.ro:443" - ogWSAddress := "dev-testnet.obscu.ro:81" - //ogWSAddress := "51.132.131.47:81" - - //// join the network - //statusCode, userID, err := fasthttp.Get(nil, fmt.Sprintf("https://%s/v1/join/", ogHTTPAddress)) - //require.NoError(t, err) // dialing to the given TCP address timed out - //fmt.Println(statusCode) - //fmt.Println(userID) - // - //// sign the message - //messagePayload := signMessage(string(userID)) - // - //// register an account - //var regAccountResp []byte - //regAccountResp, err = registerAccount(ogHTTPAddress, string(userID), messagePayload) - //require.NoError(t, err) - //fmt.Println(regAccountResp) + ogHTTPAddress := "https://dev-testnet.obscu.ro:443" + ogWSAddress := "wss://dev-testnet.obscu.ro:81" + //ogWSAddress := "ws://51.132.131.47:81" + + // join the network + statusCode, userID, err := fasthttp.Get(nil, fmt.Sprintf("%s/v1/join/", ogHTTPAddress)) + require.NoError(t, err) // dialing to the given TCP address timed out + fmt.Println(statusCode) + fmt.Println(userID) + + // sign the message + messagePayload := signMessage(string(userID)) + + // register an account + var regAccountResp []byte + regAccountResp, err = registerAccount(ogHTTPAddress, string(userID), messagePayload) + require.NoError(t, err) + fmt.Println(string(regAccountResp)) + fmt.Println(hex.EncodeToString(regAccountResp)) // Using WS -> - for i := 0; i < 50; i++ { - ethclient.Dial("ws://" + ogWSAddress) - time.Sleep(100 * time.Millisecond) - } - // Connect to WebSocket server using the standard geth client - client, err := ethclient.Dial("ws://" + ogWSAddress) + client, err := ethclient.Dial(ogWSAddress) require.NoError(t, err) // Create a simple request @@ -90,7 +86,7 @@ func registerAccount(baseAddress, userID, payload string) ([]byte, error) { req, err := http.NewRequestWithContext( context.Background(), http.MethodPost, - "https://"+baseAddress+"/authenticate/?u="+userID, + baseAddress+"/v1/authenticate/?u="+userID, strings.NewReader(payload), ) if err != nil { @@ -125,5 +121,9 @@ func signMessage(userID string) string { if err != nil { log.Fatalf("Failed to sign message: %v", err) } - return fmt.Sprintf("{\"signature\": \"%s\", \"message\": \"%s\"}", hex.EncodeToString(sig), message) + sig[64] += 27 + signature := "0x" + hex.EncodeToString(sig) + payload := fmt.Sprintf("{\"signature\": \"%s\", \"message\": \"%s\"}", signature, message) + fmt.Println(payload) + return payload } diff --git a/integration/obscurogateway/obscurogateway_test.go b/integration/obscurogateway/obscurogateway_test.go index d724717025..02d1967878 100644 --- a/integration/obscurogateway/obscurogateway_test.go +++ b/integration/obscurogateway/obscurogateway_test.go @@ -45,7 +45,6 @@ const ( ) func TestObscuroGateway(t *testing.T) { - t.Skip("Commented it out until more testing is driven from this test") t.Skip("Commented it out until more testing is driven from this test") startPort := integration.StartPortObscuroGatewayUnitTest wallets := createObscuroNetwork(t, startPort) From d55431a942117dfb2d7941dffedfd51e128966ad Mon Sep 17 00:00:00 2001 From: otherview Date: Fri, 22 Sep 2023 18:13:37 +0100 Subject: [PATCH 4/4] added loose fixes --- .github/workflows/manual-deploy-obscuro-gateway-database.yml | 4 +++- integration/manualtests/connection_test.go | 2 +- tools/walletextension/userconn/user_conn.go | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/manual-deploy-obscuro-gateway-database.yml b/.github/workflows/manual-deploy-obscuro-gateway-database.yml index a2c7b566bb..8ab3b8ebaa 100644 --- a/.github/workflows/manual-deploy-obscuro-gateway-database.yml +++ b/.github/workflows/manual-deploy-obscuro-gateway-database.yml @@ -117,4 +117,6 @@ jobs: -e MARIADB_USER=obscurouser \ -e MARIADB_PASSWORD=${{ secrets.OBSCURO_GATEWAY_MARIADB_USER_PWD }} \ -v /home/obscuro/go-obscuro/tools/walletextension/storage/database/001_init.sql:/docker-entrypoint-initdb.d/schema.sql \ - mariadb:11.1.2-jammy' + mariadb:11.1.2-jammy \ + --max_password_errors=2' + diff --git a/integration/manualtests/connection_test.go b/integration/manualtests/connection_test.go index 987d144b48..d7ba3d5f6a 100644 --- a/integration/manualtests/connection_test.go +++ b/integration/manualtests/connection_test.go @@ -27,7 +27,7 @@ func TestSubscribeToOG(t *testing.T) { // Using http ogHTTPAddress := "https://dev-testnet.obscu.ro:443" ogWSAddress := "wss://dev-testnet.obscu.ro:81" - //ogWSAddress := "ws://51.132.131.47:81" + // ogWSAddress := "ws://51.132.131.47:81" // join the network statusCode, userID, err := fasthttp.Get(nil, fmt.Sprintf("%s/v1/join/", ogHTTPAddress)) diff --git a/tools/walletextension/userconn/user_conn.go b/tools/walletextension/userconn/user_conn.go index c8b228fdd5..e2d2790883 100644 --- a/tools/walletextension/userconn/user_conn.go +++ b/tools/walletextension/userconn/user_conn.go @@ -91,7 +91,7 @@ func (h *userConnHTTP) WriteResponse(msg []byte) error { } func (h *userConnHTTP) HandleError(msg string) { - h.logger.Error(msg) + h.logger.Error(fmt.Sprintf("Handling HTTP user error - %s", msg)) httpLogAndSendErr(h.resp, msg) } @@ -139,7 +139,7 @@ func (w *userConnWS) WriteResponse(msg []byte) error { // HandleError logs and prints the error, and writes it to the websocket as a JSON object with a single key, "error". func (w *userConnWS) HandleError(msg string) { - w.logger.Error("error in user WS connection - " + msg) + w.logger.Error(fmt.Sprintf("Handling WS user error - %s", msg)) errMsg, err := json.Marshal(map[string]interface{}{ common.JSONKeyErr: msg,