From dfe8146f5c48b8ea67c417b9e2b406c1ef80a3a7 Mon Sep 17 00:00:00 2001 From: rating89us <45968869+rating89us@users.noreply.github.com> Date: Tue, 15 Jun 2021 13:44:47 +0200 Subject: [PATCH] SettingsInfo, WizardRestoreWallet1, WizardCreateDevice1: Correct restore height date when typed in wrong format --- js/Utils.js | 15 +++++++++++++++ pages/settings/SettingsInfo.qml | 8 +------- wizard/WizardCreateDevice1.qml | 9 +-------- wizard/WizardRestoreWallet1.qml | 10 +--------- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/js/Utils.js b/js/Utils.js index 61f4509b..88b0f66a 100644 --- a/js/Utils.js +++ b/js/Utils.js @@ -115,3 +115,18 @@ function capitalize(s){ function removeTrailingZeros(value) { return (value + '').replace(/(\.\d*?)0+$/, '$1').replace(/\.$/, ''); } + +function parseDateStringOrRestoreHeightAsInteger(value) { + // Parse date string or restore height as integer + var restoreHeight = 0; + if (value.indexOf('-') === 4 && value.length === 10) { + restoreHeight = Wizard.getApproximateBlockchainHeight(value, Utils.netTypeToString()); + } else if (parseInt(value.substring(0, 4)) >= 2014 && parseInt(value.substring(0, 4)) <= 2025 && value.length === 8) { + // Correct date typed in a wrong format (20201225 instead of 2020-12-25) + var restoreHeightHyphenated = value.substring(0, 4) + "-" + value.substring(4, 6) + "-" + value.substring(6, 8); + restoreHeight = Wizard.getApproximateBlockchainHeight(restoreHeightHyphenated, Utils.netTypeToString()); + } else { + restoreHeight = parseInt(value); + } + return restoreHeight; +} diff --git a/pages/settings/SettingsInfo.qml b/pages/settings/SettingsInfo.qml index 77342ec0..eac48b07 100644 --- a/pages/settings/SettingsInfo.qml +++ b/pages/settings/SettingsInfo.qml @@ -189,13 +189,7 @@ Rectangle { inputDialog.onAcceptedCallback = function() { var _restoreHeight; if (inputDialog.inputText) { - var restoreHeightText = inputDialog.inputText; - // Parse date string or restore height as integer - if(restoreHeightText.indexOf('-') === 4 && restoreHeightText.length === 10) { - _restoreHeight = Wizard.getApproximateBlockchainHeight(restoreHeightText, Utils.netTypeToString()); - } else { - _restoreHeight = parseInt(restoreHeightText) - } + _restoreHeight = Utils.parseDateStringOrRestoreHeightAsInteger(inputDialog.inputText); } if (!isNaN(_restoreHeight)) { if(_restoreHeight >= 0) { diff --git a/wizard/WizardCreateDevice1.qml b/wizard/WizardCreateDevice1.qml index 97623283..1dc877e4 100644 --- a/wizard/WizardCreateDevice1.qml +++ b/wizard/WizardCreateDevice1.qml @@ -183,15 +183,8 @@ Rectangle { wizardController.walletOptionsDeviceName = wizardCreateDevice1.deviceName; if(lookahead.text) wizardController.walletOptionsSubaddressLookahead = lookahead.text; - var _restoreHeight = 0; if(restoreHeight.text){ - // Parse date string or restore height as integer - if(restoreHeight.text.indexOf('-') === 4 && restoreHeight.text.length === 10){ - _restoreHeight = Wizard.getApproximateBlockchainHeight(restoreHeight.text, Utils.netTypeToString()); - } else { - _restoreHeight = parseInt(restoreHeight.text) - } - wizardController.walletOptionsRestoreHeight = _restoreHeight; + wizardController.walletOptionsRestoreHeight = Utils.parseDateStringOrRestoreHeightAsInteger(restoreHeight.text); } wizardController.walletCreatedFromDevice.connect(onCreateWalletFromDeviceCompleted); diff --git a/wizard/WizardRestoreWallet1.qml b/wizard/WizardRestoreWallet1.qml index 0bf76ca9..4eccc662 100644 --- a/wizard/WizardRestoreWallet1.qml +++ b/wizard/WizardRestoreWallet1.qml @@ -314,16 +314,8 @@ Rectangle { break; } - var _restoreHeight = 0; if(restoreHeight.text){ - // Parse date string or restore height as integer - if(restoreHeight.text.indexOf('-') === 4 && restoreHeight.text.length === 10){ - _restoreHeight = Wizard.getApproximateBlockchainHeight(restoreHeight.text, Utils.netTypeToString()); - } else { - _restoreHeight = parseInt(restoreHeight.text) - } - - wizardController.walletOptionsRestoreHeight = _restoreHeight; + wizardController.walletOptionsRestoreHeight = Utils.parseDateStringOrRestoreHeightAsInteger(restoreHeight.text); } wizardStateView.state = "wizardRestoreWallet2";