fix: fix seed phrase modal UI logic

Fixes #1891
This commit is contained in:
Jonathan Rainville 2021-04-12 11:40:49 -04:00 committed by Iuri Matias
parent 707604f250
commit 757eb2bc9e
3 changed files with 30 additions and 11 deletions

View File

@ -125,10 +125,10 @@ QtObject {
function isMnemonic(value) { function isMnemonic(value) {
if(!value.match(/^([a-z\s]+)$/)){ if(!value.match(/^([a-z\s]+)$/)){
console.log('allo')
return false; return false;
} }
var len = value.split(/\s|,/).length; return Utils.seedPhraseValidWordCount(value);
return len >= 12 && len <= 24 && len % 3 == 0;
} }
function compactAddress(addr, numberOfChars) { function compactAddress(addr, numberOfChars) {
@ -428,20 +428,24 @@ QtObject {
} }
} }
function isPunct(c) {
return /(!|\@|#|\$|%|\^|&|\*|\(|\)|_|\+|\||-|=|\\|{|}|[|]|"|;|'|<|>|\?|,|\.|\/)/.test(c)
}
function getTick(wordCount) { function getTick(wordCount) {
return (wordCount === 12 || wordCount === 15 || return (wordCount === 12 || wordCount === 15 ||
wordCount === 18 || wordCount === 21 || wordCount === 24) wordCount === 18 || wordCount === 21 || wordCount === 24)
? "✓ " : ""; ? "✓ " : "";
} }
function isValidNumberOfWords(wordCount) {
return !!getTick(wordCount);
}
function countWords(text) { function countWords(text) {
if (text.trim() === "") if (text.trim() === "")
return 0; return 0;
return text.trim().split(" ").length; return text.trim().replace(/ +/g, " ").split(" ").length;
}
function seedPhraseValidWordCount(text) {
return isValidNumberOfWords(countWords(text))
} }
/** /**
@ -524,4 +528,9 @@ QtObject {
function deduplicate(array) { function deduplicate(array) {
return Array.from(new Set(array)) return Array.from(new Set(array))
} }
// Leave this function at the bottom of the file as QT Creator messes up the code color after this
function isPunct(c) {
return /(!|\@|#|\$|%|\^|&|\*|\(|\)|_|\+|\||-|=|\\|{|}|[|]|"|;|'|<|>|\?|,|\.|\/)/.test(c)
}
} }

View File

@ -8,6 +8,7 @@ import "../shared/status"
ModalPopup { ModalPopup {
property var onConfirmSeedClick: function () {} property var onConfirmSeedClick: function () {}
property alias error: errorText.text property alias error: errorText.text
property bool correctWordCount: Utils.seedPhraseValidWordCount(mnemonicTextField.text)
id: popup id: popup
//% "Enter seed phrase" //% "Enter seed phrase"
title: qsTrId("enter-seed-phrase") title: qsTrId("enter-seed-phrase")
@ -38,7 +39,9 @@ ModalPopup {
selectByKeyboard: true selectByKeyboard: true
selectionColor: Style.current.secondaryBackground selectionColor: Style.current.secondaryBackground
selectedTextColor: Style.current.secondaryText selectedTextColor: Style.current.secondaryText
Keys.onReleased: errorText.text = ""
color: Style.current.textColor color: Style.current.textColor
Keys.onReturnPressed: { Keys.onReturnPressed: {
@ -49,16 +52,18 @@ ModalPopup {
} }
StyledText { StyledText {
visible: errorText.text === ""
text: Utils.seedPhraseWordCountText(mnemonicTextField.text) text: Utils.seedPhraseWordCountText(mnemonicTextField.text)
anchors.right: parent.right anchors.right: parent.right
anchors.top: mnemonicTextField.bottom anchors.top: mnemonicTextField.bottom
anchors.topMargin: Style.current.smallPadding anchors.topMargin: Style.current.smallPadding
color: Style.current.secondaryText color: correctWordCount ? Style.current.textColor : Style.current.secondaryText
} }
StyledText { StyledText {
id: errorText id: errorText
visible: !!text && text != "" visible: !!text && text != ""
wrapMode: Text.WordWrap
color: Style.current.danger color: Style.current.danger
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right
@ -87,7 +92,7 @@ ModalPopup {
icon.name: "arrow-right" icon.name: "arrow-right"
icon.width: 20 icon.width: 20
icon.height: 16 icon.height: 16
enabled: mnemonicTextField.text.length > 0 enabled: correctWordCount
onClicked : { onClicked : {
if (mnemonicTextField.text === "") { if (mnemonicTextField.text === "") {

View File

@ -17,11 +17,16 @@ Item {
onConfirmSeedClick: function (mnemonic) { onConfirmSeedClick: function (mnemonic) {
error = ""; error = "";
if(!Utils.isMnemonic(mnemonic)){ if (!Utils.isMnemonic(mnemonic)) {
//% "Invalid seed phrase" //% "Invalid seed phrase"
error = qsTrId("custom-seed-phrase") error = qsTrId("custom-seed-phrase")
} else { } else {
error = onboardingModel.validateMnemonic(mnemonic) error = onboardingModel.validateMnemonic(mnemonic)
const regex = new RegExp('word [a-z]+ not found in the dictionary', 'i');
if (regex.test(error)) {
error = qsTr('Invalid seed phrase') + '. ' +
qsTr("This seed phrase doesn't match our supported dictionary. Check for misspelled words.")
}
} }
if (error === "") { if (error === "") {