diff --git a/api/backend.go b/api/backend.go index 0b295e6afce..d1dbdc78253 100644 --- a/api/backend.go +++ b/api/backend.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "math/big" + "os" "path/filepath" "sync" "time" @@ -173,6 +174,20 @@ func (b *StatusBackend) SaveAccount(account multiaccounts.Account) error { return b.multiaccountsDB.SaveAccount(account) } +// DeleteAccount +func (b *StatusBackend) DeleteAccount(address common.Address) error { + b.mu.Lock() + defer b.mu.Unlock() + if b.multiaccountsDB == nil { + return errors.New("accoutns db wasn't initialized") + } + return b.multiaccountsDB.DeleteAccount(address) +} + +func accountDBPath(root string, address common.Address) string { + return filepath.Join(root, fmt.Sprintf("app-%x.sql", address)) +} + func (b *StatusBackend) ensureAppDBOpened(account multiaccounts.Account, password string) (err error) { b.mu.Lock() defer b.mu.Unlock() @@ -182,7 +197,7 @@ func (b *StatusBackend) ensureAppDBOpened(account multiaccounts.Account, passwor if len(b.rootDataDir) == 0 { return errors.New("root datadir wasn't provided") } - path := filepath.Join(b.rootDataDir, fmt.Sprintf("app-%x.sql", account.Address)) + path := accountDBPath(b.rootDataDir, account.Address) b.appDB, err = appdatabase.InitializeDB(path, password) if err != nil { return err @@ -1052,3 +1067,23 @@ func (b *StatusBackend) SignHash(hexEncodedHash string) (string, error) { hexEncodedSignature := hexutil.Encode(signature) return hexEncodedSignature, nil } + +func (b *StatusBackend) DeleteAccountDatabase(address common.Address) error { + b.mu.Lock() + defer b.mu.Unlock() + err := b.closeAppDB() + if err != nil { + return err + } + if len(b.rootDataDir) == 0 { + return errors.New("root datadir wasn't provided") + } + + path := accountDBPath(b.rootDataDir, address) + err = os.RemoveAll(path) + if err != nil { + return err + } + + return nil +} diff --git a/lib/library.go b/lib/library.go index d698dc7e1cf..32e9a6e9338 100644 --- a/lib/library.go +++ b/lib/library.go @@ -383,6 +383,24 @@ func SaveAccountAndLogin(accountData, password, configJSON, subaccountData *C.ch return makeJSONResponse(nil) } +// DeleteAccount delete account's database and delete account from multiaccount database +//export DeleteAccount +func DeleteAccount(addressChars *C.char) *C.char { + address := common.HexToAddressi(C.GoString(addressChars)) + err := statusBackend.DeleteAccountDatabase(address) + if err != nil { + log.Error("failed to delete account's database", "address", address, "error", err) + return err + } + err = statusBackend.DeleteAccount(address) + if err != nil { + log.Error("failed to delete account", "address", address, "error", err) + return err + } + + return nil +} + // InitKeystore initialize keystore before doing any operations with keys. //export InitKeystore func InitKeystore(keydir *C.char) *C.char { diff --git a/mobile/status.go b/mobile/status.go index 6a7f6b819be..e5d64bdb327 100644 --- a/mobile/status.go +++ b/mobile/status.go @@ -371,6 +371,23 @@ func SaveAccountAndLogin(accountData, password, configJSON, subaccountData strin return makeJSONResponse(nil) } +// DeleteAccount delete account's database and delete account from multiaccount database +func DeleteAccount(addressString string) string { + address := common.HexToAddress(addressString) + err := statusBackend.DeleteAccountDatabase(address) + if err != nil { + log.Error("failed to delete account's database", "address", address, "error", err) + return makeJSONResponse(err) + } + err = statusBackend.DeleteAccount(address) + if err != nil { + log.Error("failed to delete account from multiaccount database", "address", address, "error", err) + return makeJSONResponse(err) + } + + return makeJSONResponse(nil) +} + // InitKeystore initialize keystore before doing any operations with keys. func InitKeystore(keydir string) string { err := statusBackend.AccountManager().InitKeystore(keydir)