feat: enable deleting a wallet account

This commit is contained in:
Jonathan Rainville 2020-06-10 16:21:23 -04:00 committed by Iuri Matias
parent 7476cf3d16
commit 4d7eee1ebf
9 changed files with 73 additions and 6 deletions

View File

@ -111,6 +111,16 @@ QtObject:
self.accountListChanged() self.accountListChanged()
self.accounts.forceUpdate() self.accounts.forceUpdate()
proc deleteAccount*(self: WalletView, address: string): string {.slot.} =
result = self.status.wallet.deleteAccount(address)
if (result == ""):
let index = self.accounts.getAccountindexByAddress(address)
if (index == -1):
return fmt"Unable to find the account with the address {address}"
self.accounts.deleteAccountAtIndex(index)
self.accountListChanged()
self.accounts.forceUpdate()
proc getAccountList(self: WalletView): QVariant {.slot.} = proc getAccountList(self: WalletView): QVariant {.slot.} =
return newQVariant(self.accounts) return newQVariant(self.accounts)

View File

@ -1,5 +1,6 @@
import NimQml import NimQml
import Tables import Tables
import sequtils as sequtils
import random import random
import ./account_item import ./account_item
import ../../../status/wallet import ../../../status/wallet
@ -30,6 +31,17 @@ QtObject:
proc getAccount*(self: AccountList, index: int): WalletAccount = self.accounts[index] proc getAccount*(self: AccountList, index: int): WalletAccount = self.accounts[index]
proc getAccountindexByAddress*(self: AccountList, address: string): int =
var i = 0
for account in self.accounts:
if (account.address == address):
return i
i = i + 1
return -1
proc deleteAccountAtIndex*(self: AccountList, index: int) =
sequtils.delete(self.accounts, index, index)
method rowCount*(self: AccountList, index: QModelIndex = nil): int = method rowCount*(self: AccountList, index: QModelIndex = nil): int =
return self.accounts.len return self.accounts.len

View File

@ -1,7 +1,7 @@
import libstatus import libstatus
import core import core
import json import json
import utils import utils as utils
import accounts/constants import accounts/constants
import nimcrypto import nimcrypto
import os import os
@ -214,7 +214,7 @@ proc saveAccount*(account: GeneratedAccount, password: string, color: string, ac
proc changeAccount*(account: WalletAccount): string = proc changeAccount*(account: WalletAccount): string =
try: try:
let res= callPrivateRPC("accounts_saveAccounts", %* [ let response = callPrivateRPC("accounts_saveAccounts", %* [
[{ [{
"color": account.iconColor, "color": account.iconColor,
"name": account.name, "name": account.name,
@ -224,11 +224,23 @@ proc changeAccount*(account: WalletAccount): string =
"path": "m/44'/60'/0'/0/1" "path": "m/44'/60'/0'/0/1"
}] }]
]) ])
utils.handleRPCErrors(response)
return "" return ""
except Exception as e: except Exception as e:
error "Error saving the account", msg=e.msg error "Error saving the account", msg=e.msg
result = e.msg result = e.msg
proc deleteAccount*(address: string): string =
try:
let response = callPrivateRPC("accounts_deleteAccount", %* [address])
utils.handleRPCErrors(response)
return ""
except Exception as e:
error "Error removing the account", msg=e.msg
result = e.msg
proc deriveAccounts*(accountId: string): MultiAccounts = proc deriveAccounts*(accountId: string): MultiAccounts =
let deriveJson = %* { let deriveJson = %* {
"accountID": accountId, "accountID": accountId,

View File

@ -33,3 +33,8 @@ proc generateSigningPhrase*(count: int): string =
phrases.add(rng.sample(signing_phrases.phrases)) phrases.add(rng.sample(signing_phrases.phrases))
result = phrases.join(" ") result = phrases.join(" ")
proc handleRPCErrors*(response: string) =
let parsedReponse = parseJson(response)
if (parsedReponse.hasKey("error")):
raise newException(ValueError, parsedReponse["error"]["message"].str)

View File

@ -133,6 +133,9 @@ proc changeAccountSettings*(self: WalletModel, address: string, accountName: str
selectedAccount.iconColor = color selectedAccount.iconColor = color
result = status_accounts.changeAccount(selectedAccount) result = status_accounts.changeAccount(selectedAccount)
proc deleteAccount*(self: WalletModel, address: string): string =
result = status_accounts.deleteAccount(address)
proc toggleAsset*(self: WalletModel, symbol: string, enable: bool, address: string, name: string, decimals: int, color: string) = proc toggleAsset*(self: WalletModel, symbol: string, enable: bool, address: string, name: string, decimals: int, color: string) =
self.tokens = addOrRemoveToken(enable, address, name, symbol, decimals, color) self.tokens = addOrRemoveToken(enable, address, name, symbol, decimals, color)
for account in self.accounts: for account in self.accounts:

View File

@ -7,6 +7,7 @@ import "../../../shared"
ModalPopup { ModalPopup {
property var currentAccount: walletModel.currentAccount property var currentAccount: walletModel.currentAccount
property var changeSelectedAccount
id: popup id: popup
// TODO add icon when we have that feature // TODO add icon when we have that feature
title: qsTr("Status account settings") title: qsTr("Status account settings")
@ -97,9 +98,24 @@ ModalPopup {
btnColor: Theme.white btnColor: Theme.white
textColor: Theme.red textColor: Theme.red
MessageDialog {
id: deleteError
title: "Deleting account failed"
icon: StandardIcon.Critical
standardButtons: StandardButton.Ok
}
onClicked : { onClicked : {
// TODO add a confirmation message // TODO add a confirmation message
console.log('DELETE') const error = walletModel.deleteAccount(currentAccount.address);
if (error) {
deleteError.text = error
deleteError.open()
return
}
// Change active account to the first
changeSelectedAccount(0)
popup.close(); popup.close();
} }
} }

View File

@ -10,6 +10,13 @@ import "./Components"
Item { Item {
property int selectedAccount: 0 property int selectedAccount: 0
property var changeSelectedAccount: function(newIndex) {
if (newIndex > walletModel.accounts) {
return
}
selectedAccount = newIndex
walletModel.setCurrentAccountByIndex(newIndex)
}
id: walletInfoContainer id: walletInfoContainer
width: 340 width: 340
@ -145,8 +152,7 @@ Item {
MouseArea { MouseArea {
anchors.fill: parent anchors.fill: parent
onClicked: { onClicked: {
selectedAccount = index changeSelectedAccount(index)
walletModel.setCurrentAccountByIndex(index)
} }
} }
} }

View File

@ -7,6 +7,7 @@ import "../../../shared"
Item { Item {
property var currentAccount: walletModel.currentAccount property var currentAccount: walletModel.currentAccount
property var changeSelectedAccount
id: walletHeader id: walletHeader
height: walletAddress.y + walletAddress.height height: walletAddress.y + walletAddress.height
@ -79,6 +80,7 @@ Item {
AccountSettingsModal { AccountSettingsModal {
id: accountSettingsModal id: accountSettingsModal
changeSelectedAccount: walletHeader.changeSelectedAccount
} }
Item { Item {

View File

@ -31,6 +31,7 @@ SplitView {
WalletHeader { WalletHeader {
id: walletHeader id: walletHeader
changeSelectedAccount: leftTab.changeSelectedAccount
} }
RowLayout { RowLayout {