Skip to content

Commit

Permalink
fix 1741 (#1742)
Browse files Browse the repository at this point in the history
* fix 1741

save accounts with SaveAccountAndStartNodeWithKey

* initialize keycard accountManager with the proper addresses
  • Loading branch information
yenda authored Dec 18, 2019
1 parent 0f06b4c commit 6550316
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion api/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 8 additions & 5 deletions api/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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())

Expand All @@ -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)
}
22 changes: 18 additions & 4 deletions api/geth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions mobile/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 6550316

Please sign in to comment.