From 7fd9fefdef71da729738151339884f9ee81d6d36 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Wed, 24 May 2023 16:02:53 +0100 Subject: [PATCH] Add LoginAccount endpoint This commit adds LoginAccount endpoint. This makes it consistent with CreateAccount and RestoreAccount as they use similar config. The notable difference with the previous endpoint is the API, which is the same as CreateAccount/RestoreAccount, and the fact that it will override your networks configuration. Storing them in the config is now not needed anymore, as that's always driven from the backend, and we won't allow custom networks in the new wallet. --- api/default_networks.go | 4 +- api/default_networks_test.go | 10 +++- api/geth_backend.go | 91 +++++++++++++++++++++++++++++ mobile/status.go | 24 ++++++++ protocol/requests/create_account.go | 13 +++-- protocol/requests/login.go | 20 +++++++ rpc/verif_proxy.go | 5 +- rpc/verif_proxy_test.go | 7 +-- 8 files changed, 159 insertions(+), 15 deletions(-) create mode 100644 protocol/requests/login.go diff --git a/api/default_networks.go b/api/default_networks.go index ecc371c29..8a82c6861 100644 --- a/api/default_networks.go +++ b/api/default_networks.go @@ -141,7 +141,7 @@ var goerliGanacheTokenOverrides = params.TokenOverride{ Address: ganacheTokenAddress, } -func setRPCs(networks []params.Network, request *requests.CreateAccount) []params.Network { +func setRPCs(networks []params.Network, request *requests.WalletSecretsConfig) []params.Network { var networksWithRPC []params.Network @@ -187,5 +187,5 @@ func setRPCs(networks []params.Network, request *requests.CreateAccount) []param } func buildDefaultNetworks(request *requests.CreateAccount) []params.Network { - return setRPCs(defaultNetworks, request) + return setRPCs(defaultNetworks, &request.WalletSecretsConfig) } diff --git a/api/default_networks_test.go b/api/default_networks_test.go index 06909ae73..371f6fcb0 100644 --- a/api/default_networks_test.go +++ b/api/default_networks_test.go @@ -13,8 +13,10 @@ func TestBuildDefaultNetworks(t *testing.T) { poktToken := "poket-token" infuraToken := "infura-token" request := &requests.CreateAccount{ - PoktToken: poktToken, - InfuraToken: infuraToken, + WalletSecretsConfig: requests.WalletSecretsConfig{ + PoktToken: poktToken, + InfuraToken: infuraToken, + }, } actualNetworks := buildDefaultNetworks(request) @@ -55,7 +57,9 @@ func TestBuildDefaultNetworks(t *testing.T) { func TestBuildDefaultNetworksGanache(t *testing.T) { ganacheURL := "ganacheurl" request := &requests.CreateAccount{ - GanacheURL: ganacheURL, + WalletSecretsConfig: requests.WalletSecretsConfig{ + GanacheURL: ganacheURL, + }, } actualNetworks := buildDefaultNetworks(request) diff --git a/api/geth_backend.go b/api/geth_backend.go index a3a063430..064b99507 100644 --- a/api/geth_backend.go +++ b/api/geth_backend.go @@ -416,6 +416,97 @@ func (b *GethStatusBackend) OverwriteNodeConfigValues(conf *params.NodeConfig, n return conf, nil } +func (b *GethStatusBackend) overrideNetworks(conf *params.NodeConfig, request *requests.Login) { + conf.Networks = setRPCs(defaultNetworks, &request.WalletSecretsConfig) +} + +func (b *GethStatusBackend) LoginAccount(request *requests.Login) error { + err := b.loginAccount(request) + if err != nil { + // Stop node for clean up + _ = b.StopNode() + } + signal.SendLoggedIn(err) + return err +} + +func (b *GethStatusBackend) loginAccount(request *requests.Login) error { + if err := request.Validate(); err != nil { + return err + } + + password := request.Password + + acc := multiaccounts.Account{ + KeyUID: request.KeyUID, + KDFIterations: request.KdfIterations, + } + + if acc.KDFIterations == 0 { + acc.KDFIterations = sqlite.ReducedKDFIterationsNumber + } + + err := b.ensureAppDBOpened(acc, password) + if err != nil { + return err + } + + err = b.loadNodeConfig(nil) + if err != nil { + return err + } + + b.overrideNetworks(b.config, request) + + err = b.setupLogSettings() + if err != nil { + return err + } + + b.account = &acc + accountsDB, err := accounts.NewDB(b.appDB) + if err != nil { + return err + } + chatAddr, err := accountsDB.GetChatAddress() + if err != nil { + return err + } + walletAddr, err := accountsDB.GetWalletAddress() + if err != nil { + return err + } + watchAddrs, err := accountsDB.GetWalletAddresses() + if err != nil { + return err + } + login := account.LoginParams{ + Password: password, + ChatAddress: chatAddr, + WatchAddresses: watchAddrs, + MainAccount: walletAddr, + } + + err = b.StartNode(b.config) + if err != nil { + b.log.Info("failed to start node") + return err + } + + err = b.SelectAccount(login) + if err != nil { + return err + } + err = b.multiaccountsDB.UpdateAccountTimestamp(acc.KeyUID, time.Now().Unix()) + if err != nil { + b.log.Info("failed to update account") + return err + } + + return nil + +} + func (b *GethStatusBackend) startNodeWithAccount(acc multiaccounts.Account, password string, inputNodeCfg *params.NodeConfig) error { err := b.ensureAppDBOpened(acc, password) if err != nil { diff --git a/mobile/status.go b/mobile/status.go index a09d8ac1e..0d28e4a19 100644 --- a/mobile/status.go +++ b/mobile/status.go @@ -287,6 +287,30 @@ func CreateAccountAndLogin(requestJSON string) string { return makeJSONResponse(nil) } +func LoginAccount(requestJSON string) string { + var request requests.Login + err := json.Unmarshal([]byte(requestJSON), &request) + if err != nil { + return makeJSONResponse(err) + } + + err = request.Validate() + if err != nil { + return makeJSONResponse(err) + } + + api.RunAsync(func() error { + err := statusBackend.LoginAccount(&request) + if err != nil { + log.Error("loginAccount error", err) + return err + } + log.Debug("loginAccount started node") + return nil + }) + return makeJSONResponse(nil) +} + func RestoreAccountAndLogin(requestJSON string) string { var request requests.RestoreAccount err := json.Unmarshal([]byte(requestJSON), &request) diff --git a/protocol/requests/create_account.go b/protocol/requests/create_account.go index 65353efdb..cdfddacdc 100644 --- a/protocol/requests/create_account.go +++ b/protocol/requests/create_account.go @@ -36,10 +36,15 @@ type CreateAccount struct { CurrentNetwork string `json:"currentNetwork"` NetworkID uint64 `json:"networkId"` - PoktToken string `json:"poktToken"` - InfuraToken string `json:"infuraToken"` - InfuraSecret string `json:"infuraSecret"` - OpenseaAPIKey string `json:"openseaApiKey"` + + WalletSecretsConfig +} + +type WalletSecretsConfig struct { + PoktToken string `json:"poktToken"` + InfuraToken string `json:"infuraToken"` + InfuraSecret string `json:"infuraSecret"` + OpenseaAPIKey string `json:"openseaApiKey"` // Testing GanacheURL string `json:"ganacheURL"` diff --git a/protocol/requests/login.go b/protocol/requests/login.go new file mode 100644 index 000000000..a33eebb1d --- /dev/null +++ b/protocol/requests/login.go @@ -0,0 +1,20 @@ +package requests + +import "errors" + +var ErrLoginInvalidKeyUID = errors.New("login: invalid key-uid") + +type Login struct { + Password string `json:"password"` + KeyUID string `json:"keyUid"` + KdfIterations int `json:"kdfIterations"` + + WalletSecretsConfig +} + +func (c *Login) Validate() error { + if c.KeyUID == "" { + return ErrLoginInvalidKeyUID + } + return nil +} diff --git a/rpc/verif_proxy.go b/rpc/verif_proxy.go index b23468efc..029d67271 100644 --- a/rpc/verif_proxy.go +++ b/rpc/verif_proxy.go @@ -6,14 +6,15 @@ package rpc import ( "context" "fmt" -) -import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/log" + gethrpc "github.com/ethereum/go-ethereum/rpc" + proxy "github.com/siphiuel/lc-proxy-wrapper" + "github.com/status-im/status-go/params" ) diff --git a/rpc/verif_proxy_test.go b/rpc/verif_proxy_test.go index 53efd4607..28d31bc41 100644 --- a/rpc/verif_proxy_test.go +++ b/rpc/verif_proxy_test.go @@ -14,16 +14,15 @@ import ( "net/http" "net/http/httptest" -) -import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - gethrpc "github.com/ethereum/go-ethereum/rpc" - "github.com/status-im/status-go/params" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + + gethrpc "github.com/ethereum/go-ethereum/rpc" + "github.com/status-im/status-go/params" ) type ProxySuite struct {