Skip to content

Commit

Permalink
fix: delete account sync keystore (#2652)
Browse files Browse the repository at this point in the history
  • Loading branch information
alaibe committed May 12, 2022
1 parent a244d77 commit cf8941c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
39 changes: 39 additions & 0 deletions account/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions services/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit cf8941c

Please sign in to comment.