* fix 1741

save accounts with SaveAccountAndStartNodeWithKey

* initialize keycard accountManager with the proper addresses
This commit is contained in:
yenda 2019-12-18 16:09:04 +01:00 committed by GitHub
parent 0f06b4ca6a
commit 655031616c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 12 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -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

View File

@ -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