2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2019-2020 The Bitcoin Core developers
|
2019-01-04 15:49:26 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2019-11-19 08:45:32 -03:00
|
|
|
#include <qt/walletcontroller.h>
|
|
|
|
|
2019-05-24 17:14:16 -04:00
|
|
|
#include <qt/askpassphrasedialog.h>
|
2018-08-01 13:38:45 -04:00
|
|
|
#include <qt/clientmodel.h>
|
2019-05-24 17:14:16 -04:00
|
|
|
#include <qt/createwalletdialog.h>
|
|
|
|
#include <qt/guiconstants.h>
|
2019-06-21 10:13:15 -04:00
|
|
|
#include <qt/guiutil.h>
|
2019-11-19 08:45:32 -03:00
|
|
|
#include <qt/walletmodel.h>
|
2019-05-24 17:14:16 -04:00
|
|
|
|
2019-01-04 15:49:26 -03:00
|
|
|
#include <interfaces/handler.h>
|
|
|
|
#include <interfaces/node.h>
|
2019-10-06 18:52:05 -03:00
|
|
|
#include <util/string.h>
|
2020-04-27 13:54:43 -04:00
|
|
|
#include <util/threadnames.h>
|
2019-08-19 18:12:35 -04:00
|
|
|
#include <util/translation.h>
|
2019-11-19 08:45:32 -03:00
|
|
|
#include <wallet/wallet.h>
|
2019-01-04 15:49:26 -03:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2019-03-17 14:54:35 -03:00
|
|
|
#include <QApplication>
|
2019-01-12 08:34:05 -03:00
|
|
|
#include <QMessageBox>
|
2019-01-04 15:49:26 -03:00
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QThread>
|
2019-06-21 10:13:15 -04:00
|
|
|
#include <QTimer>
|
2019-03-17 14:54:35 -03:00
|
|
|
#include <QWindow>
|
2019-01-04 15:49:26 -03:00
|
|
|
|
2018-08-01 13:38:45 -04:00
|
|
|
WalletController::WalletController(ClientModel& client_model, const PlatformStyle* platform_style, QObject* parent)
|
2019-01-04 15:49:26 -03:00
|
|
|
: QObject(parent)
|
2019-06-21 10:13:15 -04:00
|
|
|
, m_activity_thread(new QThread(this))
|
|
|
|
, m_activity_worker(new QObject)
|
2018-08-01 13:38:45 -04:00
|
|
|
, m_client_model(client_model)
|
|
|
|
, m_node(client_model.node())
|
2019-01-04 15:49:26 -03:00
|
|
|
, m_platform_style(platform_style)
|
2018-08-01 13:38:45 -04:00
|
|
|
, m_options_model(client_model.getOptionsModel())
|
2019-01-04 15:49:26 -03:00
|
|
|
{
|
2020-05-28 09:48:30 -04:00
|
|
|
m_handler_load_wallet = m_node.walletClient().handleLoadWallet([this](std::unique_ptr<interfaces::Wallet> wallet) {
|
2019-01-04 15:49:26 -03:00
|
|
|
getOrCreateWallet(std::move(wallet));
|
|
|
|
});
|
|
|
|
|
2020-05-28 09:48:30 -04:00
|
|
|
for (std::unique_ptr<interfaces::Wallet>& wallet : m_node.walletClient().getWallets()) {
|
2019-01-04 15:49:26 -03:00
|
|
|
getOrCreateWallet(std::move(wallet));
|
|
|
|
}
|
2019-01-21 13:57:22 -03:00
|
|
|
|
2019-06-21 10:13:15 -04:00
|
|
|
m_activity_worker->moveToThread(m_activity_thread);
|
|
|
|
m_activity_thread->start();
|
2020-04-27 13:54:43 -04:00
|
|
|
QTimer::singleShot(0, m_activity_worker, []() {
|
|
|
|
util::ThreadRename("qt-walletctrl");
|
|
|
|
});
|
2019-01-04 15:49:26 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not using the default destructor because not all member types definitions are
|
|
|
|
// available in the header, just forward declared.
|
2019-01-21 13:57:22 -03:00
|
|
|
WalletController::~WalletController()
|
|
|
|
{
|
2019-06-21 10:13:15 -04:00
|
|
|
m_activity_thread->quit();
|
|
|
|
m_activity_thread->wait();
|
|
|
|
delete m_activity_worker;
|
2019-01-21 13:57:22 -03:00
|
|
|
}
|
2019-01-04 15:49:26 -03:00
|
|
|
|
2019-05-27 17:14:29 -04:00
|
|
|
std::vector<WalletModel*> WalletController::getOpenWallets() const
|
2019-01-04 15:49:26 -03:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
return m_wallets;
|
|
|
|
}
|
|
|
|
|
2019-05-27 14:07:05 -04:00
|
|
|
std::map<std::string, bool> WalletController::listWalletDir() const
|
2019-01-12 08:34:05 -03:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_mutex);
|
2019-05-27 14:07:05 -04:00
|
|
|
std::map<std::string, bool> wallets;
|
2020-05-28 09:48:30 -04:00
|
|
|
for (const std::string& name : m_node.walletClient().listWalletDir()) {
|
2019-05-27 14:07:05 -04:00
|
|
|
wallets[name] = false;
|
|
|
|
}
|
2019-01-12 08:34:05 -03:00
|
|
|
for (WalletModel* wallet_model : m_wallets) {
|
2019-05-27 14:07:05 -04:00
|
|
|
auto it = wallets.find(wallet_model->wallet().getWalletName());
|
|
|
|
if (it != wallets.end()) it->second = true;
|
2019-01-12 08:34:05 -03:00
|
|
|
}
|
|
|
|
return wallets;
|
|
|
|
}
|
|
|
|
|
2019-01-17 21:21:36 -03:00
|
|
|
void WalletController::closeWallet(WalletModel* wallet_model, QWidget* parent)
|
|
|
|
{
|
|
|
|
QMessageBox box(parent);
|
|
|
|
box.setWindowTitle(tr("Close wallet"));
|
2019-09-07 05:52:37 -04:00
|
|
|
box.setText(tr("Are you sure you wish to close the wallet <i>%1</i>?").arg(GUIUtil::HtmlEscape(wallet_model->getDisplayName())));
|
2019-01-17 21:21:36 -03: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-17 22:05:25 -03:00
|
|
|
void WalletController::closeAllWallets(QWidget* parent)
|
|
|
|
{
|
|
|
|
QMessageBox::StandardButton button = QMessageBox::question(parent, tr("Close all wallets"),
|
|
|
|
tr("Are you sure you wish to close all wallets?"),
|
|
|
|
QMessageBox::Yes|QMessageBox::Cancel,
|
|
|
|
QMessageBox::Yes);
|
|
|
|
if (button != QMessageBox::Yes) return;
|
|
|
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
for (WalletModel* wallet_model : m_wallets) {
|
|
|
|
wallet_model->wallet().remove();
|
|
|
|
Q_EMIT walletRemoved(wallet_model);
|
|
|
|
delete wallet_model;
|
|
|
|
}
|
|
|
|
m_wallets.clear();
|
|
|
|
}
|
|
|
|
|
2019-01-04 15:49:26 -03: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.
|
2018-08-01 13:38:45 -04:00
|
|
|
WalletModel* wallet_model = new WalletModel(std::move(wallet), m_client_model, m_platform_style, nullptr);
|
2019-07-09 09:25:55 -04:00
|
|
|
// Handler callback runs in a different thread so fix wallet model thread affinity.
|
|
|
|
wallet_model->moveToThread(thread());
|
2020-08-14 13:53:35 -04:00
|
|
|
// setParent(parent) must be called in the thread which created the parent object. More details in #18948.
|
|
|
|
GUIUtil::ObjectInvoke(this, [wallet_model, this] {
|
|
|
|
wallet_model->setParent(this);
|
|
|
|
}, GUIUtil::blockingGUIThreadConnection());
|
|
|
|
|
2019-01-04 15:49:26 -03:00
|
|
|
m_wallets.push_back(wallet_model);
|
|
|
|
|
2019-10-12 18:26:47 -03: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);
|
|
|
|
|
2020-03-19 19:53:33 -03:00
|
|
|
connect(wallet_model, &WalletModel::unload, this, [this, wallet_model] {
|
2019-03-17 14:54:35 -03: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);
|
|
|
|
}
|
2020-03-19 19:53:33 -03:00
|
|
|
}, Qt::QueuedConnection);
|
2019-01-04 15:49:26 -03:00
|
|
|
|
|
|
|
// Re-emit coinsSent signal from wallet model.
|
|
|
|
connect(wallet_model, &WalletModel::coinsSent, this, &WalletController::coinsSent);
|
|
|
|
|
|
|
|
// Notify walletAdded signal on the GUI thread.
|
2019-07-09 09:25:55 -04:00
|
|
|
Q_EMIT walletAdded(wallet_model);
|
2019-01-04 15:49:26 -03: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-01-21 13:58:20 -03:00
|
|
|
|
2019-06-21 10:13:15 -04:00
|
|
|
WalletControllerActivity::WalletControllerActivity(WalletController* wallet_controller, QWidget* parent_widget)
|
|
|
|
: QObject(wallet_controller)
|
|
|
|
, m_wallet_controller(wallet_controller)
|
|
|
|
, m_parent_widget(parent_widget)
|
|
|
|
{
|
|
|
|
}
|
2019-01-21 13:58:20 -03:00
|
|
|
|
2019-06-21 10:13:15 -04:00
|
|
|
WalletControllerActivity::~WalletControllerActivity()
|
|
|
|
{
|
|
|
|
delete m_progress_dialog;
|
|
|
|
}
|
2019-01-21 13:58:20 -03:00
|
|
|
|
2019-06-21 10:13:15 -04:00
|
|
|
void WalletControllerActivity::showProgressDialog(const QString& label_text)
|
2019-01-21 13:58:20 -03:00
|
|
|
{
|
2020-03-30 07:41:20 -03:00
|
|
|
assert(!m_progress_dialog);
|
2019-06-21 10:13:15 -04: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-30 07:41:20 -03:00
|
|
|
void WalletControllerActivity::destroyProgressDialog()
|
|
|
|
{
|
|
|
|
assert(m_progress_dialog);
|
|
|
|
delete m_progress_dialog;
|
|
|
|
m_progress_dialog = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-05-24 17:14:16 -04: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-07 05:08:28 -04:00
|
|
|
void CreateWalletActivity::askPassphrase()
|
2019-05-24 17:14:16 -04:00
|
|
|
{
|
|
|
|
m_passphrase_dialog = new AskPassphraseDialog(AskPassphraseDialog::Encrypt, m_parent_widget, &m_passphrase);
|
2019-09-07 06:07:54 -04:00
|
|
|
m_passphrase_dialog->setWindowModality(Qt::ApplicationModal);
|
2019-05-24 17:14:16 -04: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-07 05:08:28 -04:00
|
|
|
if (m_create_wallet_dialog->isDisablePrivateKeysChecked()) {
|
2019-05-24 17:14:16 -04:00
|
|
|
flags |= WALLET_FLAG_DISABLE_PRIVATE_KEYS;
|
|
|
|
}
|
2019-09-07 05:08:28 -04:00
|
|
|
if (m_create_wallet_dialog->isMakeBlankWalletChecked()) {
|
2019-05-24 17:14:16 -04:00
|
|
|
flags |= WALLET_FLAG_BLANK_WALLET;
|
|
|
|
}
|
2019-07-11 18:21:21 -04:00
|
|
|
if (m_create_wallet_dialog->isDescriptorWalletChecked()) {
|
|
|
|
flags |= WALLET_FLAG_DESCRIPTORS;
|
|
|
|
}
|
2019-05-24 17:14:16 -04:00
|
|
|
|
|
|
|
QTimer::singleShot(500, worker(), [this, name, flags] {
|
2020-08-04 17:55:13 -04:00
|
|
|
std::unique_ptr<interfaces::Wallet> wallet = node().walletClient().createWallet(name, m_passphrase, flags, m_error_message, m_warning_message);
|
2019-05-24 17:14:16 -04:00
|
|
|
|
2020-08-04 17:55:13 -04:00
|
|
|
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
|
2019-05-24 17:14:16 -04:00
|
|
|
|
|
|
|
QTimer::singleShot(500, this, &CreateWalletActivity::finish);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateWalletActivity::finish()
|
|
|
|
{
|
2020-03-30 07:41:20 -03:00
|
|
|
destroyProgressDialog();
|
2019-05-24 17:14:16 -04:00
|
|
|
|
2020-05-10 04:42:11 -04:00
|
|
|
if (!m_error_message.empty()) {
|
2019-08-19 18:12:35 -04:00
|
|
|
QMessageBox::critical(m_parent_widget, tr("Create wallet failed"), QString::fromStdString(m_error_message.translated));
|
2019-05-24 17:14:16 -04:00
|
|
|
} else if (!m_warning_message.empty()) {
|
2020-05-10 14:28:29 -04:00
|
|
|
QMessageBox::warning(m_parent_widget, tr("Create wallet warning"), QString::fromStdString(Join(m_warning_message, Untranslated("\n")).translated));
|
2019-05-24 17:14:16 -04: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-07 05:08:28 -04:00
|
|
|
if (m_create_wallet_dialog->isEncryptWalletChecked()) {
|
|
|
|
askPassphrase();
|
2019-05-24 17:14:16 -04:00
|
|
|
} else {
|
|
|
|
createWallet();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-21 10:13:15 -04:00
|
|
|
OpenWalletActivity::OpenWalletActivity(WalletController* wallet_controller, QWidget* parent_widget)
|
|
|
|
: WalletControllerActivity(wallet_controller, parent_widget)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenWalletActivity::finish()
|
|
|
|
{
|
2020-03-30 07:41:20 -03:00
|
|
|
destroyProgressDialog();
|
2019-06-21 10:13:15 -04:00
|
|
|
|
2020-05-10 04:42:11 -04:00
|
|
|
if (!m_error_message.empty()) {
|
2019-08-19 18:12:35 -04:00
|
|
|
QMessageBox::critical(m_parent_widget, tr("Open wallet failed"), QString::fromStdString(m_error_message.translated));
|
2019-06-21 10:13:15 -04:00
|
|
|
} else if (!m_warning_message.empty()) {
|
2020-05-10 14:28:29 -04:00
|
|
|
QMessageBox::warning(m_parent_widget, tr("Open wallet warning"), QString::fromStdString(Join(m_warning_message, Untranslated("\n")).translated));
|
2019-01-21 13:58:20 -03:00
|
|
|
}
|
2019-06-21 10:13:15 -04:00
|
|
|
|
|
|
|
if (m_wallet_model) Q_EMIT opened(m_wallet_model);
|
|
|
|
|
2019-01-21 13:58:20 -03:00
|
|
|
Q_EMIT finished();
|
|
|
|
}
|
2019-06-21 10:13:15 -04: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] {
|
2020-05-28 09:48:30 -04:00
|
|
|
std::unique_ptr<interfaces::Wallet> wallet = node().walletClient().loadWallet(path, m_error_message, m_warning_message);
|
2019-06-21 10:13:15 -04:00
|
|
|
|
|
|
|
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
|
|
|
|
|
|
|
|
QTimer::singleShot(0, this, &OpenWalletActivity::finish);
|
|
|
|
});
|
|
|
|
}
|