Skip to content

Commit

Permalink
add default user for old endpoints (#1665)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj authored Nov 27, 2023
1 parent 378aec6 commit 3af59e7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
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)
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

0 comments on commit 3af59e7

Please sign in to comment.