fix: delete account sync keystore (#2652)

This commit is contained in:
Anthony Laibe 2022-05-12 13:06:58 +02:00 committed by GitHub
parent a244d77657
commit cf8941c1d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View File

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

View File

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