From cf8941c1d816306e1e5b5c91c8d2ad41604bbd93 Mon Sep 17 00:00:00 2001 From: Anthony Laibe <491074+alaibe@users.noreply.github.com> Date: Thu, 12 May 2022 13:06:58 +0200 Subject: [PATCH] fix: delete account sync keystore (#2652) --- account/accounts.go | 39 +++++++++++++++++++++++++++++++++++ services/accounts/accounts.go | 5 +++++ 2 files changed, 44 insertions(+) diff --git a/account/accounts.go b/account/accounts.go index cc55cd7fd..99141f9e2 100644 --- a/account/accounts.go +++ b/account/accounts.go @@ -598,3 +598,42 @@ func (m *Manager) ReEncryptKeyStoreDir(keyDirPath, oldPass, newPass string) erro return nil } + +func (m *Manager) DeleteAccount(keyDirPath string, address types.Address) error { + var err error + var foundKeyFile string + err = filepath.Walk(keyDirPath, func(path string, fileInfo os.FileInfo, err error) error { + if err != nil { + return err + } + if len(foundKeyFile) > 0 || fileInfo.IsDir() { + return nil + } + + rawKeyFile, e := ioutil.ReadFile(path) + if e != nil { + return fmt.Errorf("invalid account key file: %v", e) + } + + var accountKey struct { + Address string `json:"address"` + } + if e := json.Unmarshal(rawKeyFile, &accountKey); e != nil { + return fmt.Errorf("failed to read key file: %s", e) + } + + if types.HexToAddress("0x"+accountKey.Address).Hex() == address.Hex() { + foundKeyFile = path + } + + return nil + }) + if err != nil { + return fmt.Errorf("cannot traverse key store folder: %v", err) + } + + if len(foundKeyFile) == 0 { + return fmt.Errorf("cannot locate account for address: %s", address.Hex()) + } + return os.Remove(foundKeyFile) +} diff --git a/services/accounts/accounts.go b/services/accounts/accounts.go index 4acae8352..ceb8b76fc 100644 --- a/services/accounts/accounts.go +++ b/services/accounts/accounts.go @@ -70,6 +70,11 @@ func (api *API) GetAccounts(ctx context.Context) ([]accounts.Account, error) { } func (api *API) DeleteAccount(ctx context.Context, address types.Address) error { + err := api.manager.DeleteAccount(api.config.KeyStoreDir, address) + if err != nil { + return err + } + return api.db.DeleteAccount(address) }