Permalink
Browse files

wizard: allow calendar date for restoration height

  • Loading branch information...
mmbyday committed Dec 15, 2018
1 parent 36eb1f8 commit 4a0a36e3997bce6075acb579a36f9896d969c104
Showing with 56 additions and 6 deletions.
  1. +13 −6 wizard/WizardManageWalletUI.qml
  2. +43 −0 wizard/utils.js
@@ -80,11 +80,17 @@ ColumnLayout {
console.log("checking key fields")
wizard.nextButton.enabled = checkFields();
} else if (recoverMode && recoverFromSeedMode) {
wizard.nextButton.enabled = checkSeed()
wizard.nextButton.enabled = checkSeed() && checkRestoreHeight()
if (restoreHeight.indexOf('-') === 4 && restoreHeight.length === 10)
restoreHeight = Utils.getApproximateBlockchainHeight(restoreHeight);
} else
wizard.nextButton.enabled = true;
}

function checkRestoreHeight() {
return (parseInt(restoreHeight) >= 0 || restoreHeight === "") && restoreHeight.indexOf("-") === -1;
}

function checkSeed() {
console.log("Checking seed")
var wordsArray = Utils.lineBreaksToSpaces(uiItem.wordsTextItem.memoText).split(" ");
@@ -304,18 +310,19 @@ ColumnLayout {
placeholderFontBold: true
placeholderFontFamily: "Arial"
placeholderColor: Style.legacy_placeholderFontColor
placeholderText: qsTr("Restore height (optional)") + translationManager.emptyString
placeholderText: qsTr("Wallet creation date as YYYY-MM-DD or Restore height (optional)") + translationManager.emptyString
placeholderOpacity: 1.0
validator: IntValidator {
bottom:0
}
borderColor: Qt.rgba(0, 0, 0, 0.15)
backgroundColor: "white"
fontColor: "black"
fontBold: false
validator: RegExpValidator {
regExp: /^(\d+|\d{4}-\d{2}-\d{2})$/
}
onTextUpdated: checkNextButton()
}
}

// Subaddress lookahead
RowLayout {
visible: recoverFromDevice
@@ -22,3 +22,46 @@ function usefulName(path) {
return path
return path.replace(/.*[\/\\]/, '').replace(/\.keys$/, '')
}

//usage: getApproximateBlockchainHeight("March 18 2016") or getApproximateBlockchainHeight("2016-11-11")
//returns estimated block height with 1 month buffer prior to requested date.
function getApproximateBlockchainHeight(_date){
// time of monero birth 2014-04-18 10:49:53 (1397818193)
var moneroBirthTime = 1397818193;
// avg seconds per block in v1
var secondsPerBlockV1 = 60;
// time of v2 fork 2016-03-23 15:57:38 (1458748658)
var forkTime = 1458748658;
// v2 fork block
var forkBlock = 1009827;
// avg seconds per block in V2
var secondsPerBlockV2 = 120;
// time in UTC
var requestedTime = Math.floor(new Date(_date) / 1000);
var approxBlockchainHeight;
var secondsPerBlock;
// before monero's birth
if (requestedTime < moneroBirthTime){
console.log("Calculated blockchain height: 0, requestedTime < moneroBirthTime " );
return 0;
}
// time between during v1
if (requestedTime > moneroBirthTime && requestedTime < forkTime){
approxBlockchainHeight = Math.floor((requestedTime - moneroBirthTime)/secondsPerBlockV1);
console.log("Calculated blockchain height: " + approxBlockchainHeight );
secondsPerBlock = secondsPerBlockV1;
}
// time is during V2
else{
approxBlockchainHeight = Math.floor(forkBlock + (requestedTime - forkTime)/secondsPerBlockV2);
console.log("Calculated blockchain height: " + approxBlockchainHeight );
secondsPerBlock = secondsPerBlockV2;
}
var blocksPerMonth = 60*60*24*30/secondsPerBlock;
if(approxBlockchainHeight - blocksPerMonth > 0){
return approxBlockchainHeight - blocksPerMonth;
}
else{
return 0;
}
}

0 comments on commit 4a0a36e

Please sign in to comment.