From 655031616c46233e93767d8a3d8c54568ca043f2 Mon Sep 17 00:00:00 2001 From: yenda Date: Wed, 18 Dec 2019 16:09:04 +0100 Subject: [PATCH] fix 1741 (#1742) * fix 1741 save accounts with SaveAccountAndStartNodeWithKey * initialize keycard accountManager with the proper addresses --- api/backend.go | 2 +- api/backend_test.go | 13 ++++++++----- api/geth_backend.go | 22 ++++++++++++++++++---- mobile/status.go | 9 +++++++-- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/api/backend.go b/api/backend.go index 5c2eb52c3ff..56a2e282f49 100644 --- a/api/backend.go +++ b/api/backend.go @@ -27,7 +27,7 @@ type StatusBackend interface { OpenAccounts() error GetAccounts() ([]multiaccounts.Account, error) // SaveAccount(account multiaccounts.Account) error - SaveAccountAndStartNodeWithKey(acc multiaccounts.Account, conf *params.NodeConfig, password string, keyHex string) error + SaveAccountAndStartNodeWithKey(acc multiaccounts.Account, password string, conf *params.NodeConfig, subaccs []accounts.Account, keyHex string) error Recover(rpcParams personal.RecoverParams) (types.Address, error) Logout() error diff --git a/api/backend_test.go b/api/backend_test.go index be58866fa92..bafd52dc8d2 100644 --- a/api/backend_test.go +++ b/api/backend_test.go @@ -565,9 +565,11 @@ func TestLoginWithKey(t *testing.T) { utils.Init() b := NewGethStatusBackend() - pkey, err := crypto.GenerateKey() + chatKey, err := crypto.GenerateKey() require.NoError(t, err) - keyUIDHex := sha256.Sum256(crypto.FromECDSAPub(&pkey.PublicKey)) + walletKey, err := crypto.GenerateKey() + require.NoError(t, err) + keyUIDHex := sha256.Sum256(crypto.FromECDSAPub(&chatKey.PublicKey)) keyUID := types.EncodeHex(keyUIDHex[:]) main := multiaccounts.Account{ KeyUID: keyUID, @@ -577,13 +579,14 @@ func TestLoginWithKey(t *testing.T) { defer os.Remove(tmpdir) conf, err := params.NewNodeConfig(tmpdir, 1777) require.NoError(t, err) - keyhex := hex.EncodeToString(crypto.FromECDSA(pkey)) + keyhex := hex.EncodeToString(crypto.FromECDSA(chatKey)) require.NoError(t, b.AccountManager().InitKeystore(conf.KeyStoreDir)) b.UpdateRootDataDir(conf.DataDir) require.NoError(t, b.OpenAccounts()) - require.NoError(t, b.SaveAccountAndStartNodeWithKey(main, conf, "test-pass", keyhex)) + address := crypto.PubkeyToAddress(walletKey.PublicKey) + require.NoError(t, b.SaveAccountAndStartNodeWithKey(main, "test-pass", conf, []accounts.Account{{Address: address, Wallet: true}}, keyhex)) require.NoError(t, b.Logout()) require.NoError(t, b.StopNode()) @@ -594,5 +597,5 @@ func TestLoginWithKey(t *testing.T) { }() extkey, err := b.accountManager.SelectedChatAccount() require.NoError(t, err) - require.Equal(t, crypto.PubkeyToAddress(pkey.PublicKey), extkey.Address) + require.Equal(t, crypto.PubkeyToAddress(chatKey.PublicKey), extkey.Address) } diff --git a/api/geth_backend.go b/api/geth_backend.go index 6a6c4d36f2c..39a79875b9a 100644 --- a/api/geth_backend.go +++ b/api/geth_backend.go @@ -186,7 +186,7 @@ func (b *GethStatusBackend) ensureAppDBOpened(account multiaccounts.Account, pas return nil } -func (b *GethStatusBackend) SaveAccountAndStartNodeWithKey(acc multiaccounts.Account, conf *params.NodeConfig, password string, keyHex string) error { +func (b *GethStatusBackend) SaveAccountAndStartNodeWithKey(acc multiaccounts.Account, password string, conf *params.NodeConfig, subaccs []accounts.Account, keyHex string) error { err := b.SaveAccount(acc) if err != nil { return err @@ -199,11 +199,17 @@ func (b *GethStatusBackend) SaveAccountAndStartNodeWithKey(acc multiaccounts.Acc if err != nil { return err } + err = accounts.NewDB(b.appDB).SaveAccounts(subaccs) + if err != nil { + return err + } return b.StartNodeWithKey(acc, password, keyHex) } // StartNodeWithKey instead of loading addresses from database this method derives address from key // and uses it in application. +// TODO: we should use a proper struct with optional values instead of duplicating the regular functions +// with small variants for keycard, this created too many bugs func (b *GethStatusBackend) startNodeWithKey(acc multiaccounts.Account, password string, keyHex string) error { err := b.ensureAppDBOpened(acc, password) if err != nil { @@ -216,7 +222,15 @@ func (b *GethStatusBackend) startNodeWithKey(acc multiaccounts.Account, password if err := logutils.OverrideRootLogWithConfig(conf, false); err != nil { return err } - + accountsDB := accounts.NewDB(b.appDB) + walletAddr, err := accountsDB.GetWalletAddress() + if err != nil { + return err + } + watchAddrs, err := accountsDB.GetAddresses() + if err != nil { + return err + } chatKey, err := ethcrypto.HexToECDSA(keyHex) if err != nil { return err @@ -226,11 +240,11 @@ func (b *GethStatusBackend) startNodeWithKey(acc multiaccounts.Account, password return err } b.accountManager.SetChatAccount(chatKey) - chatAcc, err := b.accountManager.SelectedChatAccount() + _, err = b.accountManager.SelectedChatAccount() if err != nil { return err } - b.accountManager.SetAccountAddresses(chatAcc.Address) + b.accountManager.SetAccountAddresses(walletAddr, watchAddrs...) err = b.injectAccountIntoServices() if err != nil { return err diff --git a/mobile/status.go b/mobile/status.go index 8cd18242296..aab2e613ef4 100644 --- a/mobile/status.go +++ b/mobile/status.go @@ -376,7 +376,7 @@ func InitKeystore(keydir string) string { } // SaveAccountAndLoginWithKeycard saves account in status-go database.. -func SaveAccountAndLoginWithKeycard(accountData, password, configJSON, keyHex string) string { +func SaveAccountAndLoginWithKeycard(accountData, password, configJSON, subaccountData string, keyHex string) string { var account multiaccounts.Account err := json.Unmarshal([]byte(accountData), &account) if err != nil { @@ -387,9 +387,14 @@ func SaveAccountAndLoginWithKeycard(accountData, password, configJSON, keyHex st if err != nil { return makeJSONResponse(err) } + var subaccs []accounts.Account + err = json.Unmarshal([]byte(subaccountData), &subaccs) + if err != nil { + return makeJSONResponse(err) + } api.RunAsync(func() error { log.Debug("starting a node, and saving account with configuration", "key-uid", account.KeyUID) - err := statusBackend.SaveAccountAndStartNodeWithKey(account, &conf, password, keyHex) + err := statusBackend.SaveAccountAndStartNodeWithKey(account, password, &conf, subaccs, keyHex) if err != nil { log.Error("failed to start node and save account", "key-uid", account.KeyUID, "error", err) return err