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.
This commit is contained in:
parent
d37b605796
commit
7fd9fefdef
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -13,8 +13,10 @@ func TestBuildDefaultNetworks(t *testing.T) {
|
|||
poktToken := "poket-token"
|
||||
infuraToken := "infura-token"
|
||||
request := &requests.CreateAccount{
|
||||
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{
|
||||
WalletSecretsConfig: requests.WalletSecretsConfig{
|
||||
GanacheURL: ganacheURL,
|
||||
},
|
||||
}
|
||||
|
||||
actualNetworks := buildDefaultNetworks(request)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -36,6 +36,11 @@ type CreateAccount struct {
|
|||
|
||||
CurrentNetwork string `json:"currentNetwork"`
|
||||
NetworkID uint64 `json:"networkId"`
|
||||
|
||||
WalletSecretsConfig
|
||||
}
|
||||
|
||||
type WalletSecretsConfig struct {
|
||||
PoktToken string `json:"poktToken"`
|
||||
InfuraToken string `json:"infuraToken"`
|
||||
InfuraSecret string `json:"infuraSecret"`
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue