Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add default user for old endpoints #1665

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tools/walletextension/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func ethRequestHandler(walletExt *walletextension.WalletExtension, conn userconn
// Get userID
hexUserID, err := getUserID(conn, 1)
if err != nil || !walletExt.UserExists(hexUserID) {
walletExt.Logger().Error("user not found in the query params: %w. Using the default user", log.ErrKey, err)
walletExt.Logger().Info("user not found in the query params: %w. Using the default user", log.ErrKey, err)
hexUserID = hex.EncodeToString([]byte(common.DefaultUser)) // todo (@ziga) - this can be removed once old WE endpoints are removed
}

Expand Down Expand Up @@ -342,7 +342,7 @@ func queryRequestHandler(walletExt *walletextension.WalletExtension, conn userco
hexUserID, err := getUserID(conn, 2)
if err != nil {
handleError(conn, walletExt.Logger(), fmt.Errorf("user ('u') not found in query parameters"))
walletExt.Logger().Error("user not found in the query params", log.ErrKey, err)
walletExt.Logger().Info("user not found in the query params", log.ErrKey, err)
return
}
address, err := getQueryParameter(conn.ReadRequestParams(), common.AddressQueryParameter)
Expand Down Expand Up @@ -394,7 +394,7 @@ func revokeRequestHandler(walletExt *walletextension.WalletExtension, conn userc
hexUserID, err := getUserID(conn, 2)
if err != nil {
handleError(conn, walletExt.Logger(), fmt.Errorf("user ('u') not found in query parameters"))
walletExt.Logger().Error("user not found in the query params", log.ErrKey, err)
walletExt.Logger().Info("user not found in the query params", log.ErrKey, err)
return
}

Expand Down
43 changes: 41 additions & 2 deletions tools/walletextension/container/walletextension_container.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package container

import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"net/http"
"os"

"github.com/ethereum/go-ethereum/common"

"github.com/ethereum/go-ethereum/crypto"

"github.com/ten-protocol/go-ten/go/common/log"
"github.com/ten-protocol/go-ten/go/common/stopcontrol"
"github.com/ten-protocol/go-ten/go/rpc"
Expand Down Expand Up @@ -49,18 +54,52 @@ func NewWalletExtensionContainerFromConfig(config config.Config, logger gethlog.
userAccountManager := useraccountmanager.NewUserAccountManager(unAuthedClient, logger, databaseStorage, hostRPCBindAddr)

// add default user (when no UserID is provided in the query parameter - for WE endpoints)
userAccountManager.AddAndReturnAccountManager(hex.EncodeToString([]byte(wecommon.DefaultUser)))
defaultUserAccountManager := userAccountManager.AddAndReturnAccountManager(hex.EncodeToString([]byte(wecommon.DefaultUser)))

// add default user to the database (temporary fix before removing wallet extension endpoints)
accountPrivateKey, err := crypto.GenerateKey()
if err != nil {
logger.Error("Unable to generate key pair for default user", log.ErrKey, err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for all these error handlings in the constructor, I think the gateway should panic.
There is no point continuing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of os.Exit(1) is consistent with the error handling strategy in the constructor, effectively halting the program on critical errors. However, consider using panic to provide a stack trace for better debugging.

os.Exit(1)
}

// get all users and their private keys from the database
allUsers, err := databaseStorage.GetAllUsers()
if err != nil {
logger.Error(fmt.Errorf("error getting all users from database, %w", err).Error())
os.Exit(1)
}

// iterate over users create accountManagers and add all accounts to them per user
// iterate over users create accountManagers and add all defaultUserAccounts to them per user
for _, user := range allUsers {
userAccountManager.AddAndReturnAccountManager(hex.EncodeToString(user.UserID))
logger.Info(fmt.Sprintf("account manager added for user: %s", hex.EncodeToString(user.UserID)))

// to ensure backwards compatibility we want to load clients for the default user
// TODO @ziga - this code needs to be removed when removing old wallet extension endpoints
if bytes.Equal(user.UserID, []byte(wecommon.DefaultUser)) {
accounts, err := databaseStorage.GetAccounts(user.UserID)
if err != nil {
logger.Error(fmt.Errorf("error getting accounts for user: %s, %w", hex.EncodeToString(user.UserID), err).Error())
os.Exit(1)
}
for _, account := range accounts {
encClient, err := wecommon.CreateEncClient(hostRPCBindAddr, account.AccountAddress, user.PrivateKey, account.Signature, logger)
if err != nil {
logger.Error(fmt.Errorf("error creating new client, %w", err).Error())
os.Exit(1)
}

// add a client to default user
defaultUserAccountManager.AddClient(common.BytesToAddress(account.AccountAddress), encClient)
}
}
}
// TODO @ziga - remove this when removing wallet extension endpoints
err = databaseStorage.AddUser([]byte(wecommon.DefaultUser), crypto.FromECDSA(accountPrivateKey))
if err != nil {
logger.Error("Unable to save default user to the database", log.ErrKey, err)
os.Exit(1)
}

// captures version in the env vars
Expand Down
2 changes: 1 addition & 1 deletion tools/walletextension/test/wallet_extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func TestGetStorageAtForReturningUserID(t *testing.T) {
}

// make a request to GetStorageAt with correct parameters, but userID that is not present in the database
invalidUserID := "abc123"
invalidUserID := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
respBody2 := makeHTTPEthJSONReqWithUserID(walletHTTPPort, rpc.GetStorageAt, []interface{}{"getUserID", "0", nil}, invalidUserID)

if !strings.Contains(string(respBody2), "method eth_getStorageAt cannot be called with an unauthorised client - no signed viewing keys found") {
Expand Down
11 changes: 11 additions & 0 deletions tools/walletextension/wallet_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"errors"
"fmt"

"github.com/ten-protocol/go-ten/tools/walletextension/accountmanager"

"github.com/ten-protocol/go-ten/tools/walletextension/config"

"github.com/ten-protocol/go-ten/go/common/log"
Expand Down Expand Up @@ -338,6 +340,15 @@ func (w *WalletExtension) getStorageAtInterceptor(request *common.RPCRequest, he
return nil
}

// check if we have default user (we don't want to send userID of it out)
if hexUserID == hex.EncodeToString([]byte(common.DefaultUser)) {
response := map[string]interface{}{}
response[common.JSONKeyRPCVersion] = jsonrpc.Version
response[common.JSONKeyID] = request.ID
response[common.JSONKeyResult] = fmt.Sprintf(accountmanager.ErrNoViewingKey, "eth_getStorageAt")
return response
}

_, err = w.storage.GetUserPrivateKey(userID)
if err != nil {
w.logger.Info("Trying to get userID, but it is not present in our database: ", log.ErrKey, err)
Expand Down
Loading