From dd679c90139e1b146ae81c45ed430a5ce046fb00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDiga=20Kokelj?= Date: Thu, 23 Nov 2023 12:31:23 +0100 Subject: [PATCH] code review fixes --- .../container/walletextension_container.go | 5 ++--- .../useraccountmanager/user_account_manager.go | 5 +++-- tools/walletextension/wallet_extension.go | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tools/walletextension/container/walletextension_container.go b/tools/walletextension/container/walletextension_container.go index f7890d2620..3e2ebc94c4 100644 --- a/tools/walletextension/container/walletextension_container.go +++ b/tools/walletextension/container/walletextension_container.go @@ -37,18 +37,17 @@ func NewWalletExtensionContainerFromConfig(config config.Config, logger gethlog. unAuthedClient, err := rpc.NewNetworkClient(hostRPCBindAddr) if err != nil { logger.Crit("unable to create temporary client for request ", log.ErrKey, err) + os.Exit(1) } // start the database databaseStorage, err := storage.New(config.DBType, config.DBConnectionURL, config.DBPathOverride) if err != nil { logger.Crit("unable to create database to store viewing keys ", log.ErrKey, err) + os.Exit(1) } userAccountManager := useraccountmanager.NewUserAccountManager(unAuthedClient, logger, databaseStorage, hostRPCBindAddr) - // Get all the data from the database and add all the clients for all users - // todo (@ziga) - implement lazy loading for clients to reduce number of connections and speed up loading - // add default user (when no UserID is provided in the query parameter - for WE endpoints) userAccountManager.AddAndReturnAccountManager(hex.EncodeToString([]byte(wecommon.DefaultUser))) diff --git a/tools/walletextension/useraccountmanager/user_account_manager.go b/tools/walletextension/useraccountmanager/user_account_manager.go index 7d3c9771e9..0c787a8b07 100644 --- a/tools/walletextension/useraccountmanager/user_account_manager.go +++ b/tools/walletextension/useraccountmanager/user_account_manager.go @@ -63,12 +63,13 @@ func (m *UserAccountManager) GetUserAccountManager(userID string) (*accountmanag } // log that we don't have a storage, but still return existing userAccountManager + // this should never happen, but is useful for tests if m.storage == nil { m.logger.Error("storage is nil in UserAccountManager") return userAccManager, nil } - databseAccounts, err := m.storage.GetAccounts(userIDbytes) + databaseAccounts, err := m.storage.GetAccounts(userIDbytes) if err != nil { return nil, err } @@ -78,7 +79,7 @@ func (m *UserAccountManager) GetUserAccountManager(userID string) (*accountmanag return nil, err } - for _, account := range databseAccounts { + for _, account := range databaseAccounts { addressHexString := common.BytesToAddress(account.AccountAddress).Hex() // check if a client for the current address already exists (and skip it if it does) if addressAlreadyExists(addressHexString, addressesWithClients) { diff --git a/tools/walletextension/wallet_extension.go b/tools/walletextension/wallet_extension.go index f7aec8aaa1..eb3b9aaf26 100644 --- a/tools/walletextension/wallet_extension.go +++ b/tools/walletextension/wallet_extension.go @@ -144,6 +144,11 @@ func (w *WalletExtension) SubmitViewingKey(address gethcommon.Address, signature signature[64] -= 27 vk.Signature = signature + + err := w.storage.AddUser([]byte(common.DefaultUser), crypto.FromECDSA(vk.PrivateKey.ExportECDSA())) + if err != nil { + return fmt.Errorf("error saving user: %s", common.DefaultUser) + } // create an encrypted RPC client with the signed VK and register it with the enclave // todo (@ziga) - Create the clients lazily, to reduce connections to the host. client, err := rpc.NewEncNetworkClient(w.hostAddr, vk, w.logger) @@ -157,11 +162,6 @@ func (w *WalletExtension) SubmitViewingKey(address gethcommon.Address, signature defaultAccountManager.AddClient(address, client) - err = w.storage.AddUser([]byte(common.DefaultUser), crypto.FromECDSA(vk.PrivateKey.ExportECDSA())) - if err != nil { - return fmt.Errorf("error saving user: %s", common.DefaultUser) - } - err = w.storage.AddAccount([]byte(common.DefaultUser), vk.Account.Bytes(), vk.Signature) if err != nil { return fmt.Errorf("error saving account %s for user %s", vk.Account.Hex(), common.DefaultUser)