feat: add modal to add an account with a private key

This commit is contained in:
Jonathan Rainville 2020-06-09 10:40:56 -04:00 committed by Iuri Matias
parent b8c5059de7
commit 2ade9e938d
8 changed files with 117 additions and 5 deletions

View File

@ -98,6 +98,9 @@ QtObject:
proc addAccountsFromSeed*(self: WalletView, seed: string, password: string, accountName: string, color: string) {.slot.} =
self.status.wallet.addAccountsFromSeed(seed, password, accountName, color)
proc addAccountsFromPrivateKey*(self: WalletView, privateKey: string, password: string, accountName: string, color: string) {.slot.} =
self.status.wallet.addAccountsFromPrivateKey(privateKey, password, accountName, color)
proc getAccountList(self: WalletView): QVariant {.slot.} =
return newQVariant(self.accounts)

View File

@ -174,8 +174,18 @@ proc multiAccountImportMnemonic*(mnemonic: string): GeneratedAccount =
let importResult = $libstatus.multiAccountImportMnemonic($mnemonicJson)
result = Json.decode(importResult, GeneratedAccount)
proc saveAccount*(account: GeneratedAccount, password: string, color: string, accountType: string): DerivedAccount =
let storeDerivedResult = storeDerivedAccounts(account, password)
proc MultiAccountImportPrivateKey*(privateKey: string): GeneratedAccount =
let privateKeyJson = %* {
"privateKey": privateKey
}
# libstatus.MultiAccountImportPrivateKey never results in an error given ANY input
let importResult = $libstatus.MultiAccountImportPrivateKey($privateKeyJson)
result = Json.decode(importResult, GeneratedAccount)
proc saveAccount*(account: GeneratedAccount, password: string, color: string, accountType: string, isADerivedAccount = true): DerivedAccount =
# Only store derived accounts. Private key accounts are not multiaccounts
if (isADerivedAccount):
discard storeDerivedAccounts(account, password)
var address = account.derived.defaultWallet.address
var publicKey = account.derived.defaultWallet.publicKey

View File

@ -12,6 +12,8 @@ proc multiAccountStoreDerivedAccounts*(paramsJSON: cstring): cstring {.importc:
proc multiAccountImportMnemonic*(paramsJSON: cstring): cstring {.importc: "MultiAccountImportMnemonic".}
proc MultiAccountImportPrivateKey*(paramsJSON: cstring): cstring {.importc: "MultiAccountImportPrivateKey".}
proc multiAccountDeriveAddresses*(paramsJSON: cstring): cstring {.importc: "MultiAccountDeriveAddresses".}
proc saveAccountAndLogin*(accountData: cstring, password: cstring, settingsJSON: cstring, configJSON: cstring, subaccountData: cstring): cstring {.importc: "SaveAccountAndLogin".}

View File

@ -133,12 +133,12 @@ proc getTotalFiatBalance*(self: WalletModel): string =
var newBalance = 0.0
fmt"{newBalance:.2f} {self.defaultCurrency}"
proc addNewGeneratedAccount(self: WalletModel, generatedAccount: GeneratedAccount, password: string, accountName: string, color: string, accountType: string) =
proc addNewGeneratedAccount(self: WalletModel, generatedAccount: GeneratedAccount, password: string, accountName: string, color: string, accountType: string, isADerivedAccount = true) =
generatedAccount.name = accountName
var derivedAccount: DerivedAccount
try:
derivedAccount = status_accounts.saveAccount(generatedAccount, password, color, accountType)
derivedAccount = status_accounts.saveAccount(generatedAccount, password, color, accountType, isADerivedAccount)
except:
error "Error storing the new account. Bad password?"
return
@ -162,6 +162,10 @@ proc addAccountsFromSeed*(self: WalletModel, seed: string, password: string, acc
let generatedAccount = status_accounts.multiAccountImportMnemonic(mnemonic)
self.addNewGeneratedAccount(generatedAccount, password, accountName, color, constants.SEED)
proc addAccountsFromPrivateKey*(self: WalletModel, privateKey: string, password: string, accountName: string, color: string) =
let generatedAccount = status_accounts.MultiAccountImportPrivateKey(privateKey)
self.addNewGeneratedAccount(generatedAccount, password, accountName, color, constants.SEED, false)
proc toggleAsset*(self: WalletModel, symbol: string, enable: bool, address: string, name: string, decimals: int, color: string) =
if enable:
discard status_tokens.addCustomToken(address, name, symbol, decimals, color)

View File

@ -83,6 +83,9 @@ Rectangle {
AddAccountWithSeed {
id: addAccountWithSeedModal
}
AddAccountWithPrivateKey {
id: addAccountWithPrivateKeydModal
}
PopupMenu {
id: newAccountMenu
@ -112,7 +115,7 @@ Rectangle {
text: qsTr("Enter a private key")
icon.source: "../../../img/enter_private_key.svg"
onTriggered: {
console.log("TODO: Enter a private key")
addAccountWithPrivateKeydModal.open()
}
}
onAboutToHide: {

View File

@ -0,0 +1,88 @@
import QtQuick 2.12
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
import "../../../../imports"
import "../../../../shared"
ModalPopup {
id: popup
title: qsTr("Add account from private key")
height: 600
property int marginBetweenInputs: 38
property string selectedColor: Constants.accountColors[0]
onOpened: {
passwordInput.text = ""
passwordInput.forceActiveFocus(Qt.MouseFocusReason)
}
Input {
id: passwordInput
placeholderText: qsTr("Enter your password…")
label: qsTr("Password")
textField.echoMode: TextInput.Password
}
Input {
id: accountPKeyInput
anchors.top: passwordInput.bottom
anchors.topMargin: marginBetweenInputs
placeholderText: qsTr("Paste the contents of your private key")
label: qsTr("Private key")
isTextArea: true
customHeight: 88
}
Input {
id: accountNameInput
anchors.top: accountPKeyInput.bottom
anchors.topMargin: marginBetweenInputs
placeholderText: qsTr("Enter an account name...")
label: qsTr("Account name")
}
Input {
id: accountColorInput
anchors.top: accountNameInput.bottom
anchors.topMargin: marginBetweenInputs
bgColor: selectedColor
label: qsTr("Account color")
selectOptions: Constants.accountColors.map(color => {
return {
text: "",
bgColor: color,
height: 52,
onClicked: function () {
selectedColor = color
}
}
})
}
footer: StyledButton {
anchors.top: parent.top
anchors.topMargin: Theme.padding
anchors.right: parent.right
anchors.rightMargin: Theme.padding
label: "Add account >"
disabled: passwordInput.text === "" || accountNameInput.text === "" || accountPKeyInput.textAreaText === ""
onClicked : {
// TODO add message to show validation errors
if (passwordInput.text === "" || accountNameInput.text === "" || accountPKeyInput.textAreaText === "") return;
walletModel.addAccountsFromPrivateKey(accountPKeyInput.textAreaText, passwordInput.text, accountNameInput.text, selectedColor)
// TODO manage errors adding account
popup.close();
}
}
}
/*##^##
Designer {
D{i:0;formeditorColor:"#ffffff";height:500;width:400}
}
##^##*/

View File

@ -4,3 +4,4 @@ TokenSettingsModalContent 1.0 TokenSettingsModalContent.qml
AddAccount 1.0 AddAccount.qml
GenerateAccountModal 1.0 GenerateAccountModal.qml
AddAccountWithSeed 1.0 AddAccountWithSeed.qml
AddAccountWithPrivateKey 1.0 AddAccountWithPrivateKey.qml

View File

@ -80,6 +80,7 @@ DISTFILES += \
app/AppLayouts/Wallet/AssetsTab.qml \
app/AppLayouts/Wallet/CollectiblesTab.qml \
app/AppLayouts/Wallet/Components/AddAccount.qml \
app/AppLayouts/Wallet/Components/AddAccountWithPrivateKey.qml \
app/AppLayouts/Wallet/Components/AddAccountWithSeed.qml \
app/AppLayouts/Wallet/Components/GenerateAccountModal.qml \
app/AppLayouts/Wallet/Components/SendModalContent.qml \