From 242c85cd6a820f0b17d99758f7e670d12afd5e3c Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Wed, 1 Feb 2023 09:46:15 +0100 Subject: [PATCH] fix: `DeleteAccount` and `AddMigratedKeyPair` declaration change Both functions `DeleteAccount` and `AddMigratedKeyPair` require password to be provided in order to delete account from the keystore properly (removing account from the cache and deleting corresponding local keystore file). Password parameter can be also an empty string, since there are cases when an account is not added to the keystore (in case of keycard account), so we have nothing to delete. --- services/accounts/accounts.go | 51 ++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/services/accounts/accounts.go b/services/accounts/accounts.go index 7362b365f..06a3ccc32 100644 --- a/services/accounts/accounts.go +++ b/services/accounts/accounts.go @@ -72,26 +72,38 @@ func (api *API) GetAccounts(ctx context.Context) ([]*accounts.Account, error) { return accounts, nil } -func (api *API) DeleteAccount(ctx context.Context, address types.Address) error { - acc, err := api.db.GetAccountByAddress(address) - if err != nil { - return err - } - if acc.Type != accounts.AccountTypeWatch { - err = api.manager.DeleteAccount(address, "") - var e *account.ErrCannotLocateKeyFile - if err != nil && !errors.As(err, &e) { +func (api *API) DeleteAccount(ctx context.Context, address types.Address, password string) error { + if len(password) > 0 { + acc, err := api.db.GetAccountByAddress(address) + if err != nil { return err } + if acc.Type != accounts.AccountTypeWatch { + err = api.manager.DeleteAccount(address, password) + var e *account.ErrCannotLocateKeyFile + if err != nil && !errors.As(err, &e) { + return err + } + + allAccountsOfKeypairWithKeyUID, err := api.db.GetAccountsByKeyUID(acc.KeyUID) + if err != nil { + return err + } + + lastAcccountOfKeypairWithTheSameKey := len(allAccountsOfKeypairWithKeyUID) == 1 + if lastAcccountOfKeypairWithTheSameKey { + err = api.manager.DeleteAccount(types.Address(common.HexToAddress(acc.DerivedFrom)), password) + var e *account.ErrCannotLocateKeyFile + if err != nil && !errors.As(err, &e) { + return err + } + } + } } return (*api.messenger).DeleteAccount(address) } -func (api *API) DeleteAccountForMigratedKeypair(ctx context.Context, address types.Address) error { - return (*api.messenger).DeleteAccount(address) -} - func (api *API) AddAccountWatch(ctx context.Context, address string, name string, color string, emoji string) error { account := &accounts.Account{ Address: types.Address(common.HexToAddress(address)), @@ -429,7 +441,7 @@ func (api *API) VerifyPassword(password string) bool { return err == nil } -func (api *API) AddMigratedKeyPair(ctx context.Context, kcUID string, kpName string, keyUID string, accountAddresses []string, keyStoreDir string) error { +func (api *API) AddMigratedKeyPair(ctx context.Context, kcUID string, kpName string, keyUID string, accountAddresses []string, password string) error { var addresses []types.Address for _, addr := range accountAddresses { addresses = append(addresses, types.Address(common.HexToAddress(addr))) @@ -441,10 +453,13 @@ func (api *API) AddMigratedKeyPair(ctx context.Context, kcUID string, kpName str } // Once we migrate a keypair, corresponding keystore files need to be deleted. - for _, addr := range addresses { - // This action deletes an account from the keystore, no need to check for error in this context here, cause if this - // action fails from whichever reason the account is still successfully migrated since keystore won't be used any more. - _ = api.manager.DeleteAccount(addr, "") + if len(password) > 0 { + for _, addr := range addresses { + err = api.manager.DeleteAccount(addr, password) + if err != nil { + return err + } + } } return nil }