2019-01-18 21:20:55 +01:00
|
|
|
// Copyright (c) 2019 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2019-06-21 16:13:15 +02:00
|
|
|
#include <qt/askpassphrasedialog.h>
|
|
|
|
#include <qt/createwalletdialog.h>
|
|
|
|
#include <qt/guiconstants.h>
|
2021-11-06 20:14:18 +01:00
|
|
|
#include <qt/guiutil.h>
|
2019-01-18 21:20:55 +01:00
|
|
|
#include <qt/walletcontroller.h>
|
|
|
|
|
2019-06-21 16:13:15 +02:00
|
|
|
#include <wallet/wallet.h>
|
|
|
|
|
2019-01-18 21:20:55 +01:00
|
|
|
#include <interfaces/handler.h>
|
|
|
|
#include <interfaces/node.h>
|
2022-03-24 21:07:40 +01:00
|
|
|
#include <util/string.h>
|
2022-04-07 06:43:16 +02:00
|
|
|
#include <util/translation.h>
|
2019-01-18 21:20:55 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2019-03-22 11:07:35 +01:00
|
|
|
#include <QApplication>
|
2019-02-12 19:19:05 +01:00
|
|
|
#include <QMessageBox>
|
2019-01-18 21:20:55 +01:00
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QThread>
|
2019-06-21 16:13:15 +02:00
|
|
|
#include <QTimer>
|
2019-03-22 11:07:35 +01:00
|
|
|
#include <QWindow>
|
2019-01-18 21:20:55 +01:00
|
|
|
|
|
|
|
WalletController::WalletController(interfaces::Node& node, OptionsModel* options_model, QObject* parent)
|
|
|
|
: QObject(parent)
|
2019-06-21 16:13:15 +02:00
|
|
|
, m_activity_thread(new QThread(this))
|
|
|
|
, m_activity_worker(new QObject)
|
2019-01-18 21:20:55 +01:00
|
|
|
, m_node(node)
|
|
|
|
, m_options_model(options_model)
|
|
|
|
{
|
|
|
|
m_handler_load_wallet = m_node.handleLoadWallet([this](std::unique_ptr<interfaces::Wallet> wallet) {
|
|
|
|
getOrCreateWallet(std::move(wallet));
|
|
|
|
});
|
|
|
|
|
|
|
|
for (std::unique_ptr<interfaces::Wallet>& wallet : m_node.getWallets()) {
|
|
|
|
getOrCreateWallet(std::move(wallet));
|
|
|
|
}
|
2019-02-12 19:19:05 +01:00
|
|
|
|
2019-06-21 16:13:15 +02:00
|
|
|
m_activity_worker->moveToThread(m_activity_thread);
|
|
|
|
m_activity_thread->start();
|
2019-01-18 21:20:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not using the default destructor because not all member types definitions are
|
|
|
|
// available in the header, just forward declared.
|
2019-02-12 19:19:05 +01:00
|
|
|
WalletController::~WalletController()
|
|
|
|
{
|
2019-06-21 16:13:15 +02:00
|
|
|
m_activity_thread->quit();
|
|
|
|
m_activity_thread->wait();
|
|
|
|
delete m_activity_worker;
|
2019-02-12 19:19:05 +01:00
|
|
|
}
|
2019-01-18 21:20:55 +01:00
|
|
|
|
2019-07-08 16:27:18 +02:00
|
|
|
std::vector<WalletModel*> WalletController::getOpenWallets() const
|
2019-01-18 21:20:55 +01:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
return m_wallets;
|
|
|
|
}
|
|
|
|
|
2019-07-08 16:27:18 +02:00
|
|
|
std::map<std::string, bool> WalletController::listWalletDir() const
|
2019-02-12 19:19:05 +01:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_mutex);
|
2019-07-08 16:27:18 +02:00
|
|
|
std::map<std::string, bool> wallets;
|
|
|
|
for (const std::string& name : m_node.listWalletDir()) {
|
|
|
|
wallets[name] = false;
|
|
|
|
}
|
2019-02-12 19:19:05 +01:00
|
|
|
for (WalletModel* wallet_model : m_wallets) {
|
2019-07-08 16:27:18 +02:00
|
|
|
auto it = wallets.find(wallet_model->wallet().getWalletName());
|
|
|
|
if (it != wallets.end()) it->second = true;
|
2019-02-12 19:19:05 +01:00
|
|
|
}
|
|
|
|
return wallets;
|
|
|
|
}
|
|
|
|
|
2019-02-14 21:46:22 +01:00
|
|
|
void WalletController::closeWallet(WalletModel* wallet_model, QWidget* parent)
|
|
|
|
{
|
|
|
|
QMessageBox box(parent);
|
|
|
|
box.setWindowTitle(tr("Close wallet"));
|
2019-09-16 10:41:05 +02:00
|
|
|
box.setText(tr("Are you sure you wish to close the wallet <i>%1</i>?").arg(GUIUtil::HtmlEscape(wallet_model->getDisplayName())));
|
2019-02-14 21:46:22 +01:00
|
|
|
box.setInformativeText(tr("Closing the wallet for too long can result in having to resync the entire chain if pruning is enabled."));
|
|
|
|
box.setStandardButtons(QMessageBox::Yes|QMessageBox::Cancel);
|
|
|
|
box.setDefaultButton(QMessageBox::Yes);
|
|
|
|
if (box.exec() != QMessageBox::Yes) return;
|
|
|
|
|
|
|
|
// First remove wallet from node.
|
|
|
|
wallet_model->wallet().remove();
|
|
|
|
// Now release the model.
|
|
|
|
removeAndDeleteWallet(wallet_model);
|
|
|
|
}
|
|
|
|
|
2019-01-18 21:20:55 +01:00
|
|
|
WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
|
|
|
// Return model instance if exists.
|
|
|
|
if (!m_wallets.empty()) {
|
|
|
|
std::string name = wallet->getWalletName();
|
|
|
|
for (WalletModel* wallet_model : m_wallets) {
|
|
|
|
if (wallet_model->wallet().getWalletName() == name) {
|
|
|
|
return wallet_model;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instantiate model and register it.
|
|
|
|
WalletModel* wallet_model = new WalletModel(std::move(wallet), m_node, m_options_model, nullptr);
|
2019-08-12 14:15:48 +02:00
|
|
|
// Handler callback runs in a different thread so fix wallet model thread affinity.
|
|
|
|
wallet_model->moveToThread(thread());
|
|
|
|
wallet_model->setParent(this);
|
2019-01-18 21:20:55 +01:00
|
|
|
m_wallets.push_back(wallet_model);
|
|
|
|
|
2019-10-26 13:00:01 +02:00
|
|
|
// WalletModel::startPollBalance needs to be called in a thread managed by
|
|
|
|
// Qt because of startTimer. Considering the current thread can be a RPC
|
|
|
|
// thread, better delegate the calling to Qt with Qt::AutoConnection.
|
|
|
|
const bool called = QMetaObject::invokeMethod(wallet_model, "startPollBalance");
|
|
|
|
assert(called);
|
|
|
|
|
2019-01-18 21:20:55 +01:00
|
|
|
connect(wallet_model, &WalletModel::unload, [this, wallet_model] {
|
2019-03-22 11:07:35 +01:00
|
|
|
// Defer removeAndDeleteWallet when no modal widget is active.
|
|
|
|
// TODO: remove this workaround by removing usage of QDiallog::exec.
|
|
|
|
if (QApplication::activeModalWidget()) {
|
|
|
|
connect(qApp, &QApplication::focusWindowChanged, wallet_model, [this, wallet_model]() {
|
|
|
|
if (!QApplication::activeModalWidget()) {
|
|
|
|
removeAndDeleteWallet(wallet_model);
|
|
|
|
}
|
|
|
|
}, Qt::QueuedConnection);
|
|
|
|
} else {
|
|
|
|
removeAndDeleteWallet(wallet_model);
|
|
|
|
}
|
2019-01-18 21:20:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Re-emit coinsSent signal from wallet model.
|
|
|
|
connect(wallet_model, &WalletModel::coinsSent, this, &WalletController::coinsSent);
|
|
|
|
|
|
|
|
// Notify walletAdded signal on the GUI thread.
|
2019-08-12 14:15:48 +02:00
|
|
|
Q_EMIT walletAdded(wallet_model);
|
2019-01-18 21:20:55 +01:00
|
|
|
|
|
|
|
return wallet_model;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WalletController::removeAndDeleteWallet(WalletModel* wallet_model)
|
|
|
|
{
|
|
|
|
// Unregister wallet model.
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
m_wallets.erase(std::remove(m_wallets.begin(), m_wallets.end(), wallet_model));
|
|
|
|
}
|
|
|
|
Q_EMIT walletRemoved(wallet_model);
|
|
|
|
// Currently this can trigger the unload since the model can hold the last
|
|
|
|
// CWallet shared pointer.
|
|
|
|
delete wallet_model;
|
|
|
|
}
|
2019-02-12 19:19:05 +01:00
|
|
|
|
2019-06-21 16:13:15 +02:00
|
|
|
WalletControllerActivity::WalletControllerActivity(WalletController* wallet_controller, QWidget* parent_widget)
|
|
|
|
: QObject(wallet_controller)
|
|
|
|
, m_wallet_controller(wallet_controller)
|
|
|
|
, m_parent_widget(parent_widget)
|
|
|
|
{
|
|
|
|
}
|
2019-02-12 19:19:05 +01:00
|
|
|
|
2019-06-21 16:13:15 +02:00
|
|
|
WalletControllerActivity::~WalletControllerActivity()
|
|
|
|
{
|
|
|
|
delete m_progress_dialog;
|
|
|
|
}
|
2019-02-12 19:19:05 +01:00
|
|
|
|
2019-06-21 16:13:15 +02:00
|
|
|
void WalletControllerActivity::showProgressDialog(const QString& label_text)
|
2019-02-12 19:19:05 +01:00
|
|
|
{
|
2020-03-28 03:14:08 +01:00
|
|
|
assert(!m_progress_dialog);
|
2019-06-21 16:13:15 +02:00
|
|
|
m_progress_dialog = new QProgressDialog(m_parent_widget);
|
|
|
|
|
|
|
|
m_progress_dialog->setLabelText(label_text);
|
|
|
|
m_progress_dialog->setRange(0, 0);
|
|
|
|
m_progress_dialog->setCancelButton(nullptr);
|
|
|
|
m_progress_dialog->setWindowModality(Qt::ApplicationModal);
|
|
|
|
GUIUtil::PolishProgressDialog(m_progress_dialog);
|
|
|
|
}
|
|
|
|
|
2020-03-28 03:14:08 +01:00
|
|
|
void WalletControllerActivity::destroyProgressDialog()
|
|
|
|
{
|
|
|
|
assert(m_progress_dialog);
|
|
|
|
delete m_progress_dialog;
|
|
|
|
m_progress_dialog = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:13:15 +02:00
|
|
|
CreateWalletActivity::CreateWalletActivity(WalletController* wallet_controller, QWidget* parent_widget)
|
|
|
|
: WalletControllerActivity(wallet_controller, parent_widget)
|
|
|
|
{
|
|
|
|
m_passphrase.reserve(MAX_PASSPHRASE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateWalletActivity::~CreateWalletActivity()
|
|
|
|
{
|
|
|
|
delete m_create_wallet_dialog;
|
|
|
|
delete m_passphrase_dialog;
|
|
|
|
}
|
|
|
|
|
2019-09-16 10:41:05 +02:00
|
|
|
void CreateWalletActivity::askPassphrase()
|
2019-06-21 16:13:15 +02:00
|
|
|
{
|
|
|
|
m_passphrase_dialog = new AskPassphraseDialog(AskPassphraseDialog::Encrypt, m_parent_widget, &m_passphrase);
|
2019-09-16 10:41:05 +02:00
|
|
|
m_passphrase_dialog->setWindowModality(Qt::ApplicationModal);
|
2019-06-21 16:13:15 +02:00
|
|
|
m_passphrase_dialog->show();
|
|
|
|
|
|
|
|
connect(m_passphrase_dialog, &QObject::destroyed, [this] {
|
|
|
|
m_passphrase_dialog = nullptr;
|
|
|
|
});
|
|
|
|
connect(m_passphrase_dialog, &QDialog::accepted, [this] {
|
|
|
|
createWallet();
|
|
|
|
});
|
|
|
|
connect(m_passphrase_dialog, &QDialog::rejected, [this] {
|
|
|
|
Q_EMIT finished();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateWalletActivity::createWallet()
|
|
|
|
{
|
|
|
|
showProgressDialog(tr("Creating Wallet <b>%1</b>...").arg(m_create_wallet_dialog->walletName().toHtmlEscaped()));
|
|
|
|
|
|
|
|
std::string name = m_create_wallet_dialog->walletName().toStdString();
|
|
|
|
uint64_t flags = 0;
|
2019-09-16 10:41:05 +02:00
|
|
|
if (m_create_wallet_dialog->isDisablePrivateKeysChecked()) {
|
2019-06-21 16:13:15 +02:00
|
|
|
flags |= WALLET_FLAG_DISABLE_PRIVATE_KEYS;
|
2019-02-12 19:19:05 +01:00
|
|
|
}
|
2019-09-16 10:41:05 +02:00
|
|
|
if (m_create_wallet_dialog->isMakeBlankWalletChecked()) {
|
2019-06-21 16:13:15 +02:00
|
|
|
flags |= WALLET_FLAG_BLANK_WALLET;
|
2019-02-12 19:19:05 +01:00
|
|
|
}
|
2019-06-21 16:13:15 +02:00
|
|
|
|
|
|
|
QTimer::singleShot(500, worker(), [this, name, flags] {
|
|
|
|
std::unique_ptr<interfaces::Wallet> wallet;
|
|
|
|
WalletCreationStatus status = node().createWallet(m_passphrase, flags, name, m_error_message, m_warning_message, wallet);
|
|
|
|
|
|
|
|
if (status == WalletCreationStatus::SUCCESS) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
|
|
|
|
|
|
|
|
QTimer::singleShot(500, this, &CreateWalletActivity::finish);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateWalletActivity::finish()
|
|
|
|
{
|
2020-03-28 03:14:08 +01:00
|
|
|
destroyProgressDialog();
|
2019-06-21 16:13:15 +02:00
|
|
|
|
2020-05-10 10:42:11 +02:00
|
|
|
if (!m_error_message.empty()) {
|
2022-04-07 06:43:16 +02:00
|
|
|
QMessageBox::critical(m_parent_widget, tr("Create wallet failed"), QString::fromStdString(m_error_message.translated));
|
2019-06-21 16:13:15 +02:00
|
|
|
} else if (!m_warning_message.empty()) {
|
2020-05-13 20:30:31 +02:00
|
|
|
QMessageBox::warning(m_parent_widget, tr("Create wallet warning"), QString::fromStdString(Join(m_warning_message, Untranslated("\n")).translated));
|
2019-06-21 16:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_wallet_model) Q_EMIT created(m_wallet_model);
|
|
|
|
|
|
|
|
Q_EMIT finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateWalletActivity::create()
|
|
|
|
{
|
|
|
|
m_create_wallet_dialog = new CreateWalletDialog(m_parent_widget);
|
|
|
|
m_create_wallet_dialog->setWindowModality(Qt::ApplicationModal);
|
|
|
|
m_create_wallet_dialog->show();
|
|
|
|
|
|
|
|
connect(m_create_wallet_dialog, &QObject::destroyed, [this] {
|
|
|
|
m_create_wallet_dialog = nullptr;
|
|
|
|
});
|
|
|
|
connect(m_create_wallet_dialog, &QDialog::rejected, [this] {
|
|
|
|
Q_EMIT finished();
|
|
|
|
});
|
|
|
|
connect(m_create_wallet_dialog, &QDialog::accepted, [this] {
|
2019-09-16 10:41:05 +02:00
|
|
|
if (m_create_wallet_dialog->isEncryptWalletChecked()) {
|
|
|
|
askPassphrase();
|
2019-06-21 16:13:15 +02:00
|
|
|
} else {
|
|
|
|
createWallet();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenWalletActivity::OpenWalletActivity(WalletController* wallet_controller, QWidget* parent_widget)
|
|
|
|
: WalletControllerActivity(wallet_controller, parent_widget)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenWalletActivity::finish()
|
|
|
|
{
|
2020-03-28 03:14:08 +01:00
|
|
|
destroyProgressDialog();
|
2019-06-21 16:13:15 +02:00
|
|
|
|
2020-05-10 10:42:11 +02:00
|
|
|
if (!m_error_message.empty()) {
|
2022-04-07 06:43:16 +02:00
|
|
|
QMessageBox::critical(m_parent_widget, tr("Open wallet failed"), QString::fromStdString(m_error_message.translated));
|
2019-06-21 16:13:15 +02:00
|
|
|
} else if (!m_warning_message.empty()) {
|
2020-05-13 20:30:31 +02:00
|
|
|
QMessageBox::warning(m_parent_widget, tr("Open wallet warning"), QString::fromStdString(Join(m_warning_message, Untranslated("\n")).translated));
|
2019-06-21 16:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_wallet_model) Q_EMIT opened(m_wallet_model);
|
|
|
|
|
2019-02-12 19:19:05 +01:00
|
|
|
Q_EMIT finished();
|
|
|
|
}
|
2019-06-21 16:13:15 +02:00
|
|
|
|
|
|
|
void OpenWalletActivity::open(const std::string& path)
|
|
|
|
{
|
|
|
|
QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path);
|
|
|
|
|
|
|
|
showProgressDialog(tr("Opening Wallet <b>%1</b>...").arg(name.toHtmlEscaped()));
|
|
|
|
|
|
|
|
QTimer::singleShot(0, worker(), [this, path] {
|
|
|
|
std::unique_ptr<interfaces::Wallet> wallet = node().loadWallet(path, m_error_message, m_warning_message);
|
|
|
|
|
|
|
|
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
|
|
|
|
|
|
|
|
QTimer::singleShot(0, this, &OpenWalletActivity::finish);
|
|
|
|
});
|
|
|
|
}
|