From 1ca4fd24ef5d152eb5e4a3e77d715104195ddf34 Mon Sep 17 00:00:00 2001
From: Jacob Brydolf <jacob@brydolf.net>
Date: Mon, 10 Oct 2016 20:20:50 +0200
Subject: [PATCH 1/5] one more variable fix

---
 wizard/WizardRecoveryWallet.qml | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/wizard/WizardRecoveryWallet.qml b/wizard/WizardRecoveryWallet.qml
index 7f6d87f4..1d777397 100644
--- a/wizard/WizardRecoveryWallet.qml
+++ b/wizard/WizardRecoveryWallet.qml
@@ -54,17 +54,18 @@ Item {
         settingsObject['account_name'] = uiItem.accountNameText
         settingsObject['words'] = Utils.lineBreaksToSpaces(uiItem.wordsTextItem.memoText)
         settingsObject['wallet_path'] = uiItem.walletPath
-        settingsObject['restoreHeight'] = parseInt(uiItem.restoreHeight)
+        settingsObject['restore_height'] = parseInt(uiItem.restoreHeight)
         return recoveryWallet(settingsObject)
     }
 
     function recoveryWallet(settingsObject) {
         var testnet = appWindow.persistentSettings.testnet;
-        var restoreHeight = settingsObject.restoreHeight;
+        var restoreHeight = settingsObject.restore_height;
         var wallet = walletManager.recoveryWallet(oshelper.temporaryFilename(), settingsObject.words, testnet, restoreHeight);
         var success = wallet.status === Wallet.Status_Ok;
         if (success) {
             settingsObject['wallet'] = wallet;
+            settingsObject['is_restoring'] = true;
         } else {
             walletManager.closeWallet(wallet);
         }

From 06d628b6961cea576f4ce26931b546c557707720 Mon Sep 17 00:00:00 2001
From: Jacob Brydolf <jacob@brydolf.net>
Date: Mon, 10 Oct 2016 21:36:57 +0200
Subject: [PATCH 2/5] libwalletqt: added isRecovering state

---
 src/libwalletqt/Wallet.cpp | 10 +++++++---
 src/libwalletqt/Wallet.h   |  4 ++--
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/libwalletqt/Wallet.cpp b/src/libwalletqt/Wallet.cpp
index b52963bc..3c8858b5 100644
--- a/src/libwalletqt/Wallet.cpp
+++ b/src/libwalletqt/Wallet.cpp
@@ -115,13 +115,18 @@ bool Wallet::store(const QString &path)
     return m_walletImpl->store(path.toStdString());
 }
 
-bool Wallet::init(const QString &daemonAddress, quint64 upperTransactionLimit)
+bool Wallet::init(const QString &daemonAddress, quint64 upperTransactionLimit, bool isRecovering, quint64 restoreHeight)
 {
     return m_walletImpl->init(daemonAddress.toStdString(), upperTransactionLimit);
 }
 
-void Wallet::initAsync(const QString &daemonAddress, quint64 upperTransactionLimit)
+void Wallet::initAsync(const QString &daemonAddress, quint64 upperTransactionLimit, bool isRecovering, quint64 restoreHeight)
 {
+    if (isRecovering){
+        qDebug() << "RESTORING";
+        m_walletImpl->setRecoveringFromSeed(true);
+        m_walletImpl->setRefreshFromBlockHeight(restoreHeight);
+    }
     m_walletImpl->initAsync(daemonAddress.toStdString(), upperTransactionLimit);
 }
 
@@ -263,6 +268,5 @@ Wallet::Wallet(Bitmonero::Wallet *w, QObject *parent)
 
 Wallet::~Wallet()
 {
-
     Bitmonero::WalletManagerFactory::getWalletManager()->closeWallet(m_walletImpl);
 }
diff --git a/src/libwalletqt/Wallet.h b/src/libwalletqt/Wallet.h
index d794550a..6267f411 100644
--- a/src/libwalletqt/Wallet.h
+++ b/src/libwalletqt/Wallet.h
@@ -74,10 +74,10 @@ public:
     Q_INVOKABLE bool store(const QString &path);
 
     //! initializes wallet
-    Q_INVOKABLE bool init(const QString &daemonAddress, quint64 upperTransactionLimit);
+    Q_INVOKABLE bool init(const QString &daemonAddress, quint64 upperTransactionLimit, bool isRecovering = false, quint64 restoreHeight = 0);
 
     //! initializes wallet asynchronously
-    Q_INVOKABLE void initAsync(const QString &daemonAddress, quint64 upperTransactionLimit);
+    Q_INVOKABLE void initAsync(const QString &daemonAddress, quint64 upperTransactionLimit, bool isRecovering = false, quint64 restoreHeight = 0);
 
     //! connects to daemon
     Q_INVOKABLE bool connectToDaemon();

From 625ae7794530bc0e0426150bf50fc4660c5c3522 Mon Sep 17 00:00:00 2001
From: Jacob Brydolf <jacob@brydolf.net>
Date: Mon, 10 Oct 2016 21:38:30 +0200
Subject: [PATCH 3/5] wizard: save recovering state

---
 wizard/WizardMain.qml           | 1 +
 wizard/WizardRecoveryWallet.qml | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/wizard/WizardMain.qml b/wizard/WizardMain.qml
index 3168733b..78a8ea06 100644
--- a/wizard/WizardMain.qml
+++ b/wizard/WizardMain.qml
@@ -138,6 +138,7 @@ Rectangle {
         appWindow.persistentSettings.daemon_address = settings.daemon_address
         appWindow.persistentSettings.testnet = settings.testnet
         appWindow.persistentSettings.restore_height = parseInt(settings.restore_height)
+        appWindow.persistentSettings.is_recovering = (settings.is_recovering === undefined)? false : settings.is_recovering
 
     }
 
diff --git a/wizard/WizardRecoveryWallet.qml b/wizard/WizardRecoveryWallet.qml
index 1d777397..f4c5091e 100644
--- a/wizard/WizardRecoveryWallet.qml
+++ b/wizard/WizardRecoveryWallet.qml
@@ -65,7 +65,7 @@ Item {
         var success = wallet.status === Wallet.Status_Ok;
         if (success) {
             settingsObject['wallet'] = wallet;
-            settingsObject['is_restoring'] = true;
+            settingsObject['is_recovering'] = true;
         } else {
             walletManager.closeWallet(wallet);
         }

From a88c031510d1c572dd2556467898b71356f91a2f Mon Sep 17 00:00:00 2001
From: Jacob Brydolf <jacob@brydolf.net>
Date: Mon, 10 Oct 2016 21:40:58 +0200
Subject: [PATCH 4/5] continue recovering from seed on open if not finished

---
 main.qml | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/main.qml b/main.qml
index bba92f88..f90944f8 100644
--- a/main.qml
+++ b/main.qml
@@ -158,7 +158,6 @@ ApplicationWindow {
             }
 
             console.log("using wizard wallet")
-
             connectWallet(wizard.settings['wallet'])
 
             isNewWallet = true
@@ -183,7 +182,9 @@ ApplicationWindow {
         currentWallet.moneySpent.connect(onWalletMoneySent)
         currentWallet.moneyReceived.connect(onWalletMoneyReceived)
         console.log("initializing with daemon address: ", persistentSettings.daemon_address)
-        currentWallet.initAsync(persistentSettings.daemon_address, 0);
+        console.log("Recovering from seed: ", persistentSettings.is_recovering)
+        console.log("restore Height", persistentSettings.restore_height)
+        currentWallet.initAsync(persistentSettings.daemon_address, 0, persistentSettings.is_recovering, persistentSettings.restore_height);
     }
 
     function walletPath() {
@@ -244,7 +245,6 @@ ApplicationWindow {
         leftPanel.daemonProgress.updateProgress(dCurrentBlock,dTargetBlock);
 
         // Store wallet after first refresh. To prevent broken wallet after a crash
-        // TODO: Move this to libwallet?
         if(isNewWallet && currentWallet.blockChainHeight() > 0){
             currentWallet.store(persistentSettings.wallet_path)
             isNewWallet = false
@@ -257,6 +257,11 @@ ApplicationWindow {
             walletInitialized = true
         }
 
+        // recovering from seed is finished after first refresh
+        if(persistentSettings.is_recovering) {
+            persistentSettings.is_recovering = false
+        }
+
         leftPanel.networkStatus.connected = currentWallet.connected
 
         onWalletUpdate();
@@ -445,7 +450,8 @@ ApplicationWindow {
         property bool   testnet: true
         property string daemon_address: "localhost:38081"
         property string payment_id
-        property int restore_height:0
+        property int    restore_height : 0
+        property bool   is_recovering : false
     }
 
     // TODO: replace with customized popups

From a7e4e341e9eaa06a46480a2494726ff5d03d85a3 Mon Sep 17 00:00:00 2001
From: Jacob Brydolf <jacob@brydolf.net>
Date: Mon, 10 Oct 2016 21:42:25 +0200
Subject: [PATCH 5/5] make sure wallet is closed on app close

---
 main.qml | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/main.qml b/main.qml
index f90944f8..5a6bcabb 100644
--- a/main.qml
+++ b/main.qml
@@ -794,4 +794,8 @@ ApplicationWindow {
             }
         }
     }
+    onClosing: {
+        walletManager.closeWallet(currentWallet);
+        console.log("onClosing called");
+    }
 }