Wallet: implement async wallet storing

This commit is contained in:
xiphon 2020-04-26 02:23:51 +00:00
parent 4141832a4d
commit cfdba59584
6 changed files with 61 additions and 38 deletions

View file

@ -972,7 +972,11 @@ ApplicationWindow {
informationPopup.open() informationPopup.open()
currentWallet.refresh() currentWallet.refresh()
currentWallet.disposeTransaction(transaction) currentWallet.disposeTransaction(transaction)
currentWallet.store(); currentWallet.storeAsync(function(success) {
if (!success) {
appWindow.showStatusMessage(qsTr("Failed to store the wallet"), 3);
}
});
} }
// called on "getProof" // called on "getProof"

View file

@ -48,7 +48,6 @@
#include <QtConcurrent/QtConcurrent> #include <QtConcurrent/QtConcurrent>
#include <QList> #include <QList>
#include <QVector> #include <QVector>
#include <QMutex>
#include <QMutexLocker> #include <QMutexLocker>
namespace { namespace {
@ -230,9 +229,19 @@ QString Wallet::path() const
return QDir::toNativeSeparators(QString::fromStdString(m_walletImpl->path())); return QDir::toNativeSeparators(QString::fromStdString(m_walletImpl->path()));
} }
bool Wallet::store(const QString &path) void Wallet::storeAsync(const QJSValue &callback, const QString &path /* = "" */)
{ {
return m_walletImpl->store(path.toStdString()); const auto future = m_scheduler.run(
[this, path] {
QMutexLocker locker(&m_storeMutex);
return QJSValueList({m_walletImpl->store(path.toStdString())});
},
callback);
if (!future.first)
{
QJSValue(callback).call(QJSValueList({false}));
}
} }
bool Wallet::init(const QString &daemonAddress, bool trustedDaemon, quint64 upperTransactionLimit, bool isRecovering, bool isRecoveringFromDevice, quint64 restoreHeight) bool Wallet::init(const QString &daemonAddress, bool trustedDaemon, quint64 upperTransactionLimit, bool isRecovering, bool isRecoveringFromDevice, quint64 restoreHeight)

View file

@ -144,7 +144,7 @@ public:
//! saves wallet to the file by given path //! saves wallet to the file by given path
//! empty path stores in current location //! empty path stores in current location
Q_INVOKABLE bool store(const QString &path = ""); Q_INVOKABLE void storeAsync(const QJSValue &callback, const QString &path = "");
//! initializes wallet asynchronously //! initializes wallet asynchronously
Q_INVOKABLE void initAsync(const QString &daemonAddress, bool trustedDaemon = false, quint64 upperTransactionLimit = 0, bool isRecovering = false, bool isRecoveringFromDevice = false, quint64 restoreHeight = 0); Q_INVOKABLE void initAsync(const QString &daemonAddress, bool trustedDaemon = false, quint64 upperTransactionLimit = 0, bool isRecovering = false, bool isRecoveringFromDevice = false, quint64 restoreHeight = 0);
@ -434,6 +434,7 @@ private:
QString m_daemonPassword; QString m_daemonPassword;
Monero::WalletListener *m_walletListener; Monero::WalletListener *m_walletListener;
FutureScheduler m_scheduler; FutureScheduler m_scheduler;
QMutex m_storeMutex;
}; };

View file

@ -344,41 +344,48 @@ Rectangle {
wizardController.tmpWalletFilename = tmp_wallet_filename wizardController.tmpWalletFilename = tmp_wallet_filename
} }
function writeWallet() { function writeWallet(onSuccess) {
// Save wallet files in user specified location // Save wallet files in user specified location
var new_wallet_filename = Wizard.createWalletPath( var new_wallet_filename = Wizard.createWalletPath(
isIOS, isIOS,
wizardController.walletOptionsLocation, wizardController.walletOptionsLocation,
wizardController.walletOptionsName); wizardController.walletOptionsName);
if(isIOS) { const handler = function(success) {
console.log("saving in ios: " + moneroAccountsDir + new_wallet_filename) if (!success) {
wizardController.m_wallet.store(moneroAccountsDir + new_wallet_filename); appWindow.showStatusMessage(qsTr("Failed to store the wallet"), 3);
} else { return;
console.log("saving in wizard: " + new_wallet_filename) }
wizardController.m_wallet.store(new_wallet_filename);
// make sure temporary wallet files are deleted
console.log("Removing temporary wallet: " + wizardController.tmpWalletFilename)
oshelper.removeTemporaryWallet(wizardController.tmpWalletFilename)
// protecting wallet with password
wizardController.m_wallet.setPassword(wizardController.walletOptionsPassword);
// save to persistent settings
persistentSettings.language = wizardController.language_language
persistentSettings.locale = wizardController.language_locale
persistentSettings.account_name = wizardController.walletOptionsName
persistentSettings.wallet_path = wizardController.m_wallet.path;
persistentSettings.restore_height = (isNaN(walletOptionsRestoreHeight))? 0 : walletOptionsRestoreHeight
persistentSettings.allow_background_mining = false
persistentSettings.is_recovering = (wizardController.walletOptionsIsRecovering === undefined) ? false : wizardController.walletOptionsIsRecovering
persistentSettings.is_recovering_from_device = (wizardController.walletOptionsIsRecoveringFromDevice === undefined) ? false : wizardController.walletOptionsIsRecoveringFromDevice
restart();
onSuccess();
};
if (isIOS) {
new_wallet_filename = moneroAccountsDir + new_wallet_filename;
} }
console.log("saving new wallet to", new_wallet_filename);
// make sure temporary wallet files are deleted wizardController.m_wallet.storeAsync(handler, new_wallet_filename);
console.log("Removing temporary wallet: " + wizardController.tmpWalletFilename)
oshelper.removeTemporaryWallet(wizardController.tmpWalletFilename)
// protecting wallet with password
wizardController.m_wallet.setPassword(wizardController.walletOptionsPassword);
// save to persistent settings
persistentSettings.language = wizardController.language_language
persistentSettings.locale = wizardController.language_locale
persistentSettings.account_name = wizardController.walletOptionsName
persistentSettings.wallet_path = wizardController.m_wallet.path;
persistentSettings.restore_height = (isNaN(walletOptionsRestoreHeight))? 0 : walletOptionsRestoreHeight
persistentSettings.allow_background_mining = false
persistentSettings.is_recovering = (wizardController.walletOptionsIsRecovering === undefined) ? false : wizardController.walletOptionsIsRecovering
persistentSettings.is_recovering_from_device = (wizardController.walletOptionsIsRecoveringFromDevice === undefined) ? false : wizardController.walletOptionsIsRecoveringFromDevice
restart();
} }
function recoveryWallet() { function recoveryWallet() {

View file

@ -77,9 +77,10 @@ Rectangle {
} }
} }
onNextClicked: { onNextClicked: {
wizardController.writeWallet(); wizardController.writeWallet(function() {
wizardController.useMoneroClicked(); wizardController.useMoneroClicked();
wizardController.walletOptionsIsRecoveringFromDevice = false; wizardController.walletOptionsIsRecoveringFromDevice = false;
});
} }
} }
} }

View file

@ -78,8 +78,9 @@ Rectangle {
} }
onNextClicked: { onNextClicked: {
wizardController.recoveryWallet(); wizardController.recoveryWallet();
wizardController.writeWallet(); wizardController.writeWallet(function() {
wizardController.useMoneroClicked(); wizardController.useMoneroClicked();
});
} }
} }
} }