Skip to content

Commit

Permalink
DeleteAccount method
Browse files Browse the repository at this point in the history
DeleteAccount method is added, it deletes account from
multiaccounts database and also removes account's DB file.
The method is needed for keycard reseting functionality.
status-im/status-mobile#9229
  • Loading branch information
rasom committed Nov 12, 2019
1 parent 89659f8 commit 91efdb9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
37 changes: 36 additions & 1 deletion api/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"math/big"
"os"
"path/filepath"
"sync"
"time"
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions lib/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions mobile/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 91efdb9

Please sign in to comment.