fix(@desktop/wallet): add account - password checking adjusted

This commit is contained in:
Michał Cieślak 2022-08-12 11:02:56 +02:00 committed by Michał
parent c2ad7281ae
commit 21aeeb8906
2 changed files with 100 additions and 104 deletions

View File

@ -22,134 +22,142 @@ import "../panels"
StatusModal { StatusModal {
id: root id: root
property int marginBetweenInputs: Style.dp(38) readonly property int marginBetweenInputs: Style.dp(38)
property string passwordValidationError: "" readonly property alias passwordValidationError: d.passwordValidationError
property bool loading: false
property var emojiPopup: null property var emojiPopup: null
header.title: qsTr("Generate an account")
closePolicy: Popup.CloseOnEscape closePolicy: Popup.CloseOnEscape
signal afterAddAccount() signal afterAddAccount()
header.title: qsTr("Generate an account") MessageDialog {
id: accountError
title: qsTr("Adding the account failed")
icon: StandardIcon.Critical
standardButtons: StandardButton.Ok
}
Timer {
id: waitTimer
interval: 1000
running: false
onTriggered: d.getDerivedAddressList()
}
Connections {
target: emojiPopup
enabled: root.opened
function onEmojiSelected (emojiText, atCursor) {
accountNameInput.input.icon.emoji = emojiText
}
}
Connections {
target: RootStore
enabled: root.opened
function onDerivedAddressesListChanged() {
d.isPasswordCorrect = RootStore.derivedAddressesError.length === 0
}
function onDerivedAddressesErrorChanged() {
if(Utils.isInvalidPasswordMessage(RootStore.derivedAddressesError))
d.passwordValidationError = qsTr("Wrong password")
}
}
QtObject { QtObject {
id: d id: d
property int numOfItems: 100 readonly property int numOfItems: 100
property int pageNumber: 1 readonly property int pageNumber: 1
property string passwordValidationError: ""
property bool isPasswordCorrect: false
function getDerivedAddressList() { function getDerivedAddressList() {
if(advancedSelection.expandableItem) { if(advancedSelection.expandableItem.addAccountType === SelectGeneratedAccount.AddAccountType.ImportSeedPhrase
var errMessage = "" && !!advancedSelection.expandableItem.path
if(advancedSelection.expandableItem.addAccountType === SelectGeneratedAccount.AddAccountType.ImportSeedPhrase && && !!advancedSelection.expandableItem.mnemonicText) {
!!advancedSelection.expandableItem.path && RootStore.getDerivedAddressListForMnemonic(advancedSelection.expandableItem.mnemonicText,
!!advancedSelection.expandableItem.mnemonicText) { advancedSelection.expandableItem.path, numOfItems, pageNumber)
RootStore.getDerivedAddressListForMnemonic(advancedSelection.expandableItem.mnemonicText, advancedSelection.expandableItem.path, numOfItems, pageNumber) } else if(!!advancedSelection.expandableItem.path && !!advancedSelection.expandableItem.derivedFromAddress
} && (passwordInput.text.length > 0)) {
else if(!!advancedSelection.expandableItem.path && !!advancedSelection.expandableItem.derivedFromAddress && (passwordInput.text.length >= 6)) { RootStore.getDerivedAddressList(passwordInput.text, advancedSelection.expandableItem.derivedFromAddress,
RootStore.getDerivedAddressList(passwordInput.text, advancedSelection.expandableItem.derivedFromAddress, advancedSelection.expandableItem.path, numOfItems, pageNumber) advancedSelection.expandableItem.path, numOfItems, pageNumber)
}
} }
} }
property string derivedPathError: RootStore.derivedAddressesError
onDerivedPathErrorChanged: {
if(Utils.isInvalidPasswordMessage(derivedPathError))
root.passwordValidationError = qsTr("Wrong password")
}
function showPasswordError(errMessage) { function showPasswordError(errMessage) {
if (errMessage) { if (errMessage) {
if (Utils.isInvalidPasswordMessage(errMessage)) { if (Utils.isInvalidPasswordMessage(errMessage)) {
root.passwordValidationError = qsTr("Wrong password") d.passwordValidationError = qsTr("Wrong password")
scroll.contentY = -scroll.padding
} else { } else {
accountError.text = errMessage; accountError.text = errMessage
accountError.open(); accountError.open()
} }
} }
} }
property var waitTimer: Timer {
interval: 1000
running: false
onTriggered: {
d.getDerivedAddressList()
}
}
function generateNewAccount() { function generateNewAccount() {
// TODO the loaidng doesn't work because the function freezes th view. Might need to use threads // TODO the loading doesn't work because the function freezes the view. Might need to use threads
nextButton.loading = true nextButton.loading = true
if (!validate() || !advancedSelection.validate()) { if (!advancedSelection.validate()) {
Global.playErrorSound(); Global.playErrorSound()
return nextButton.loading = false return nextButton.loading = false
} }
let emoji = StatusQUtils.Emoji.deparseFromParse(accountNameInput.input.icon.emoji) let errMessage = ""
var errMessage = "" switch(advancedSelection.expandableItem.addAccountType) {
if(advancedSelection.expandableItem) {
switch(advancedSelection.expandableItem.addAccountType) {
case SelectGeneratedAccount.AddAccountType.GenerateNew: case SelectGeneratedAccount.AddAccountType.GenerateNew:
errMessage = RootStore.generateNewAccount(passwordInput.text, accountNameInput.text, colorSelectionGrid.selectedColor, accountNameInput.input.icon.emoji, advancedSelection.expandableItem.completePath, advancedSelection.expandableItem.derivedFromAddress) errMessage = RootStore.generateNewAccount(passwordInput.text, accountNameInput.text, colorSelectionGrid.selectedColor,
accountNameInput.input.icon.emoji, advancedSelection.expandableItem.completePath,
advancedSelection.expandableItem.derivedFromAddress)
break break
case SelectGeneratedAccount.AddAccountType.ImportSeedPhrase: case SelectGeneratedAccount.AddAccountType.ImportSeedPhrase:
errMessage = RootStore.addAccountsFromSeed(advancedSelection.expandableItem.mnemonicText, passwordInput.text, accountNameInput.text, colorSelectionGrid.selectedColor, accountNameInput.input.icon.emoji, advancedSelection.expandableItem.completePath) errMessage = RootStore.addAccountsFromSeed(advancedSelection.expandableItem.mnemonicText, passwordInput.text,
accountNameInput.text, colorSelectionGrid.selectedColor, accountNameInput.input.icon.emoji,
advancedSelection.expandableItem.completePath)
break break
case SelectGeneratedAccount.AddAccountType.ImportPrivateKey: case SelectGeneratedAccount.AddAccountType.ImportPrivateKey:
errMessage = RootStore.addAccountsFromPrivateKey(advancedSelection.expandableItem.privateKey, passwordInput.text, accountNameInput.text, colorSelectionGrid.selectedColor, accountNameInput.input.icon.emoji) errMessage = RootStore.addAccountsFromPrivateKey(advancedSelection.expandableItem.privateKey, passwordInput.text,
accountNameInput.text, colorSelectionGrid.selectedColor, accountNameInput.input.icon.emoji)
break break
case SelectGeneratedAccount.AddAccountType.WatchOnly: case SelectGeneratedAccount.AddAccountType.WatchOnly:
errMessage = RootStore.addWatchOnlyAccount(advancedSelection.expandableItem.watchAddress, accountNameInput.text, colorSelectionGrid.selectedColor, accountNameInput.input.icon.emoji) errMessage = RootStore.addWatchOnlyAccount(advancedSelection.expandableItem.watchAddress, accountNameInput.text,
colorSelectionGrid.selectedColor, accountNameInput.input.icon.emoji)
break break
}
} else {
errMessage = RootStore.generateNewAccount(passwordInput.text, accountNameInput.text, colorSelectionGrid.selectedColor, accountNameInput.input.icon.emoji, advancedSelection.expandableItem.completePath, advancedSelection.expandableItem.derivedFromAddress)
} }
nextButton.loading = false nextButton.loading = false
d.showPasswordError(errMessage)
root.afterAddAccount(); if (errMessage) {
root.close(); d.showPasswordError(errMessage)
} else {
root.afterAddAccount()
root.close()
}
} }
} }
function validate() {
if (advancedSelection.expandableItem.addAccountType === SelectGeneratedAccount.AddAccountType.WatchOnly) {
return accountNameInput.valid
}
if (passwordInput.text === "") {
passwordValidationError = qsTr("You need to enter a password")
} else if (passwordInput.text.length < 6) {
passwordValidationError = qsTr("Password needs to be 6 characters or more")
} else {
passwordValidationError = ""
}
return passwordValidationError === "" && accountNameInput.valid
}
onOpened: { onOpened: {
passwordValidationError = "";
passwordInput.text = "";
accountNameInput.text = "";
accountNameInput.reset()
accountNameInput.input.icon.emoji = StatusQUtils.Emoji.getRandomEmoji(StatusQUtils.Emoji.size.verySmall) accountNameInput.input.icon.emoji = StatusQUtils.Emoji.getRandomEmoji(StatusQUtils.Emoji.size.verySmall)
colorSelectionGrid.selectedColorIndex = Math.floor(Math.random() * colorSelectionGrid.model.length) colorSelectionGrid.selectedColorIndex = Math.floor(Math.random() * colorSelectionGrid.model.length)
advancedSelection.expanded = false
advancedSelection.reset()
passwordInput.forceActiveFocus(Qt.MouseFocusReason) passwordInput.forceActiveFocus(Qt.MouseFocusReason)
} }
Connections { onClosed: {
enabled: root.opened d.passwordValidationError = ""
target: emojiPopup passwordInput.text = ""
onEmojiSelected: function (emojiText, atCursor) { accountNameInput.reset()
accountNameInput.input.icon.emoji = emojiText advancedSelection.expanded = false
} advancedSelection.reset()
} }
contentItem: StatusScrollView { contentItem: StatusScrollView {
@ -180,13 +188,14 @@ StatusModal {
placeholderText: qsTr("Enter your password...") placeholderText: qsTr("Enter your password...")
label: qsTr("Password") label: qsTr("Password")
textField.echoMode: TextInput.Password textField.echoMode: TextInput.Password
validationError: root.passwordValidationError validationError: d.passwordValidationError
textField.objectName: "accountModalPassword" textField.objectName: "accountModalPassword"
inputLabel.font.pixelSize: 15 inputLabel.font.pixelSize: 15
inputLabel.font.weight: Font.Normal inputLabel.font.weight: Font.Normal
onTextChanged: { onTextChanged: {
root.passwordValidationError = "" d.isPasswordCorrect = false
d.waitTimer.restart() d.passwordValidationError = ""
waitTimer.restart()
} }
onKeyPressed: { onKeyPressed: {
if(event.key === Qt.Key_Tab) { if(event.key === Qt.Key_Tab) {
@ -238,15 +247,11 @@ StatusModal {
property bool isValid: true property bool isValid: true
function validate() { function validate() {
if(expandableItem) { return !!expandableItem && expandableItem.validate()
return expandableItem.validate()
}
} }
function reset() { function reset() {
if(expandableItem) { return !!expandableItem && expandableItem.reset()
return expandableItem.reset()
}
} }
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
@ -257,7 +262,6 @@ StatusModal {
expandable: true expandable: true
expandableComponent: AdvancedAddAccountView { expandableComponent: AdvancedAddAccountView {
width: parent.width width: parent.width
Component.onCompleted: advancedSelection.isValid = Qt.binding(function(){return isValid})
onCalculateDerivedPath: d.getDerivedAddressList() onCalculateDerivedPath: d.getDerivedAddressList()
onEnterPressed: { onEnterPressed: {
if (nextButton.enabled) { if (nextButton.enabled) {
@ -265,6 +269,7 @@ StatusModal {
return return
} }
} }
Component.onCompleted: advancedSelection.isValid = Qt.binding(() => isValid)
} }
} }
} }
@ -273,29 +278,20 @@ StatusModal {
rightButtons: [ rightButtons: [
StatusButton { StatusButton {
id: nextButton id: nextButton
text: loading ?
qsTr("Loading...") : text: loading ? qsTr("Loading...") : qsTr("Add account")
qsTr("Add account")
enabled: { enabled: {
if (loading) { if (loading) {
return false return false
} }
return (advancedSelection.expandableItem.addAccountType === SelectGeneratedAccount.AddAccountType.WatchOnly || passwordInput.text !== "") && return (advancedSelection.expandableItem.addAccountType === SelectGeneratedAccount.AddAccountType.WatchOnly || d.isPasswordCorrect)
accountNameInput.text !== "" && && accountNameInput.text !== "" && advancedSelection.isValid
advancedSelection.isValid
} }
highlighted: focus highlighted: focus
MessageDialog {
id: accountError
title: "Adding the account failed"
icon: StandardIcon.Critical
standardButtons: StandardButton.Ok
}
Keys.onReturnPressed: d.generateNewAccount() Keys.onReturnPressed: d.generateNewAccount()
onClicked : d.generateNewAccount() onClicked : d.generateNewAccount()
} }

View File

@ -181,8 +181,8 @@ QtObject {
globalUtils.copyToClipboard(text) globalUtils.copyToClipboard(text)
} }
function getDerivedAddressList(password, derivedFrom, path, pageSize , pageNumber) { function getDerivedAddressList(password, derivedFrom, path, pageSize, pageNumber) {
walletSectionAccounts.getDerivedAddressList(password, derivedFrom, path, pageSize , pageNumber) walletSectionAccounts.getDerivedAddressList(password, derivedFrom, path, pageSize, pageNumber)
} }
function getDerivedAddressData(index) { function getDerivedAddressData(index) {