Bitcoin Core  21.99.0
P2P Digital Currency
walletcontroller.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <qt/walletcontroller.h>
6 
8 #include <qt/clientmodel.h>
10 #include <qt/guiconstants.h>
11 #include <qt/guiutil.h>
12 #include <qt/walletmodel.h>
13 
14 #include <interfaces/handler.h>
15 #include <interfaces/node.h>
16 #include <util/string.h>
17 #include <util/threadnames.h>
18 #include <util/translation.h>
19 #include <wallet/wallet.h>
20 
21 #include <algorithm>
22 
23 #include <QApplication>
24 #include <QMessageBox>
25 #include <QMutexLocker>
26 #include <QThread>
27 #include <QTimer>
28 #include <QWindow>
29 
30 WalletController::WalletController(ClientModel& client_model, const PlatformStyle* platform_style, QObject* parent)
31  : QObject(parent)
32  , m_activity_thread(new QThread(this))
33  , m_activity_worker(new QObject)
34  , m_client_model(client_model)
35  , m_node(client_model.node())
36  , m_platform_style(platform_style)
37  , m_options_model(client_model.getOptionsModel())
38 {
39  m_handler_load_wallet = m_node.walletClient().handleLoadWallet([this](std::unique_ptr<interfaces::Wallet> wallet) {
40  getOrCreateWallet(std::move(wallet));
41  });
42 
43  for (std::unique_ptr<interfaces::Wallet>& wallet : m_node.walletClient().getWallets()) {
44  getOrCreateWallet(std::move(wallet));
45  }
46 
47  m_activity_worker->moveToThread(m_activity_thread);
48  m_activity_thread->start();
49  QTimer::singleShot(0, m_activity_worker, []() {
50  util::ThreadRename("qt-walletctrl");
51  });
52 }
53 
54 // Not using the default destructor because not all member types definitions are
55 // available in the header, just forward declared.
57 {
58  m_activity_thread->quit();
59  m_activity_thread->wait();
60  delete m_activity_worker;
61 }
62 
63 std::vector<WalletModel*> WalletController::getOpenWallets() const
64 {
65  QMutexLocker locker(&m_mutex);
66  return m_wallets;
67 }
68 
69 std::map<std::string, bool> WalletController::listWalletDir() const
70 {
71  QMutexLocker locker(&m_mutex);
72  std::map<std::string, bool> wallets;
73  for (const std::string& name : m_node.walletClient().listWalletDir()) {
74  wallets[name] = false;
75  }
76  for (WalletModel* wallet_model : m_wallets) {
77  auto it = wallets.find(wallet_model->wallet().getWalletName());
78  if (it != wallets.end()) it->second = true;
79  }
80  return wallets;
81 }
82 
83 void WalletController::closeWallet(WalletModel* wallet_model, QWidget* parent)
84 {
85  QMessageBox box(parent);
86  box.setWindowTitle(tr("Close wallet"));
87  box.setText(tr("Are you sure you wish to close the wallet <i>%1</i>?").arg(GUIUtil::HtmlEscape(wallet_model->getDisplayName())));
88  box.setInformativeText(tr("Closing the wallet for too long can result in having to resync the entire chain if pruning is enabled."));
89  box.setStandardButtons(QMessageBox::Yes|QMessageBox::Cancel);
90  box.setDefaultButton(QMessageBox::Yes);
91  if (box.exec() != QMessageBox::Yes) return;
92 
93  // First remove wallet from node.
94  wallet_model->wallet().remove();
95  // Now release the model.
96  removeAndDeleteWallet(wallet_model);
97 }
98 
99 void WalletController::closeAllWallets(QWidget* parent)
100 {
101  QMessageBox::StandardButton button = QMessageBox::question(parent, tr("Close all wallets"),
102  tr("Are you sure you wish to close all wallets?"),
103  QMessageBox::Yes|QMessageBox::Cancel,
104  QMessageBox::Yes);
105  if (button != QMessageBox::Yes) return;
106 
107  QMutexLocker locker(&m_mutex);
108  for (WalletModel* wallet_model : m_wallets) {
109  wallet_model->wallet().remove();
110  Q_EMIT walletRemoved(wallet_model);
111  delete wallet_model;
112  }
113  m_wallets.clear();
114 }
115 
116 WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet)
117 {
118  QMutexLocker locker(&m_mutex);
119 
120  // Return model instance if exists.
121  if (!m_wallets.empty()) {
122  std::string name = wallet->getWalletName();
123  for (WalletModel* wallet_model : m_wallets) {
124  if (wallet_model->wallet().getWalletName() == name) {
125  return wallet_model;
126  }
127  }
128  }
129 
130  // Instantiate model and register it.
131  WalletModel* wallet_model = new WalletModel(std::move(wallet), m_client_model, m_platform_style, nullptr);
132  // Handler callback runs in a different thread so fix wallet model thread affinity.
133  wallet_model->moveToThread(thread());
134  wallet_model->setParent(this);
135  m_wallets.push_back(wallet_model);
136 
137  // WalletModel::startPollBalance needs to be called in a thread managed by
138  // Qt because of startTimer. Considering the current thread can be a RPC
139  // thread, better delegate the calling to Qt with Qt::AutoConnection.
140  const bool called = QMetaObject::invokeMethod(wallet_model, "startPollBalance");
141  assert(called);
142 
143  connect(wallet_model, &WalletModel::unload, this, [this, wallet_model] {
144  // Defer removeAndDeleteWallet when no modal widget is active.
145  // TODO: remove this workaround by removing usage of QDiallog::exec.
146  if (QApplication::activeModalWidget()) {
147  connect(qApp, &QApplication::focusWindowChanged, wallet_model, [this, wallet_model]() {
148  if (!QApplication::activeModalWidget()) {
149  removeAndDeleteWallet(wallet_model);
150  }
151  }, Qt::QueuedConnection);
152  } else {
153  removeAndDeleteWallet(wallet_model);
154  }
155  }, Qt::QueuedConnection);
156 
157  // Re-emit coinsSent signal from wallet model.
158  connect(wallet_model, &WalletModel::coinsSent, this, &WalletController::coinsSent);
159 
160  // Notify walletAdded signal on the GUI thread.
161  Q_EMIT walletAdded(wallet_model);
162 
163  return wallet_model;
164 }
165 
167 {
168  // Unregister wallet model.
169  {
170  QMutexLocker locker(&m_mutex);
171  m_wallets.erase(std::remove(m_wallets.begin(), m_wallets.end(), wallet_model));
172  }
173  Q_EMIT walletRemoved(wallet_model);
174  // Currently this can trigger the unload since the model can hold the last
175  // CWallet shared pointer.
176  delete wallet_model;
177 }
178 
179 WalletControllerActivity::WalletControllerActivity(WalletController* wallet_controller, QWidget* parent_widget)
180  : QObject(wallet_controller)
181  , m_wallet_controller(wallet_controller)
182  , m_parent_widget(parent_widget)
183 {
184 }
185 
187 {
188  delete m_progress_dialog;
189 }
190 
191 void WalletControllerActivity::showProgressDialog(const QString& label_text)
192 {
193  assert(!m_progress_dialog);
194  m_progress_dialog = new QProgressDialog(m_parent_widget);
195 
196  m_progress_dialog->setLabelText(label_text);
197  m_progress_dialog->setRange(0, 0);
198  m_progress_dialog->setCancelButton(nullptr);
199  m_progress_dialog->setWindowModality(Qt::ApplicationModal);
201 }
202 
204 {
205  assert(m_progress_dialog);
206  delete m_progress_dialog;
207  m_progress_dialog = nullptr;
208 }
209 
210 CreateWalletActivity::CreateWalletActivity(WalletController* wallet_controller, QWidget* parent_widget)
211  : WalletControllerActivity(wallet_controller, parent_widget)
212 {
214 }
215 
217 {
218  delete m_create_wallet_dialog;
219  delete m_passphrase_dialog;
220 }
221 
223 {
225  m_passphrase_dialog->setWindowModality(Qt::ApplicationModal);
226  m_passphrase_dialog->show();
227 
228  connect(m_passphrase_dialog, &QObject::destroyed, [this] {
229  m_passphrase_dialog = nullptr;
230  });
231  connect(m_passphrase_dialog, &QDialog::accepted, [this] {
232  createWallet();
233  });
234  connect(m_passphrase_dialog, &QDialog::rejected, [this] {
235  Q_EMIT finished();
236  });
237 }
238 
240 {
241  showProgressDialog(tr("Creating Wallet <b>%1</b>...").arg(m_create_wallet_dialog->walletName().toHtmlEscaped()));
242 
243  std::string name = m_create_wallet_dialog->walletName().toStdString();
244  uint64_t flags = 0;
247  }
249  flags |= WALLET_FLAG_BLANK_WALLET;
250  }
252  flags |= WALLET_FLAG_DESCRIPTORS;
253  }
254 
255  QTimer::singleShot(500, worker(), [this, name, flags] {
256  std::unique_ptr<interfaces::Wallet> wallet = node().walletClient().createWallet(name, m_passphrase, flags, m_error_message, m_warning_message);
257 
258  if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
259 
260  QTimer::singleShot(500, this, &CreateWalletActivity::finish);
261  });
262 }
263 
265 {
267 
268  if (!m_error_message.empty()) {
269  QMessageBox::critical(m_parent_widget, tr("Create wallet failed"), QString::fromStdString(m_error_message.translated));
270  } else if (!m_warning_message.empty()) {
271  QMessageBox::warning(m_parent_widget, tr("Create wallet warning"), QString::fromStdString(Join(m_warning_message, Untranslated("\n")).translated));
272  }
273 
275 
276  Q_EMIT finished();
277 }
278 
280 {
282  m_create_wallet_dialog->setWindowModality(Qt::ApplicationModal);
283  m_create_wallet_dialog->show();
284 
285  connect(m_create_wallet_dialog, &QObject::destroyed, [this] {
286  m_create_wallet_dialog = nullptr;
287  });
288  connect(m_create_wallet_dialog, &QDialog::rejected, [this] {
289  Q_EMIT finished();
290  });
291  connect(m_create_wallet_dialog, &QDialog::accepted, [this] {
293  askPassphrase();
294  } else {
295  createWallet();
296  }
297  });
298 }
299 
300 OpenWalletActivity::OpenWalletActivity(WalletController* wallet_controller, QWidget* parent_widget)
301  : WalletControllerActivity(wallet_controller, parent_widget)
302 {
303 }
304 
306 {
308 
309  if (!m_error_message.empty()) {
310  QMessageBox::critical(m_parent_widget, tr("Open wallet failed"), QString::fromStdString(m_error_message.translated));
311  } else if (!m_warning_message.empty()) {
312  QMessageBox::warning(m_parent_widget, tr("Open wallet warning"), QString::fromStdString(Join(m_warning_message, Untranslated("\n")).translated));
313  }
314 
315  if (m_wallet_model) Q_EMIT opened(m_wallet_model);
316 
317  Q_EMIT finished();
318 }
319 
320 void OpenWalletActivity::open(const std::string& path)
321 {
322  QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path);
323 
324  showProgressDialog(tr("Opening Wallet <b>%1</b>...").arg(name.toHtmlEscaped()));
325 
326  QTimer::singleShot(0, worker(), [this, path] {
327  std::unique_ptr<interfaces::Wallet> wallet = node().walletClient().loadWallet(path, m_error_message, m_warning_message);
328 
329  if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
330 
331  QTimer::singleShot(0, this, &OpenWalletActivity::finish);
332  });
333 }
std::vector< WalletModel * > m_wallets
static const int MAX_PASSPHRASE_SIZE
Definition: guiconstants.h:14
std::deque< CInv >::iterator it
interfaces::Wallet & wallet() const
Definition: walletmodel.h:146
bool isDescriptorWalletChecked() const
void coinsSent(WalletModel *wallet, SendCoinsRecipient recipient, QByteArray transaction)
virtual std::vector< std::string > listWalletDir()=0
Return available wallets in wallet directory.
interfaces::Node & node() const
bool isEncryptWalletChecked() const
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
Definition: string.h:36
bool empty() const
Definition: translation.h:27
CreateWalletDialog * m_create_wallet_dialog
NodeContext & m_node
Definition: chain.cpp:411
void closeAllWallets(QWidget *parent=nullptr)
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:40
void opened(WalletModel *wallet_model)
const PlatformStyle *const m_platform_style
CreateWalletActivity(WalletController *wallet_controller, QWidget *parent_widget)
void unload()
void showProgressDialog(const QString &label_text)
void ThreadRename(std::string &&)
Rename a thread both in terms of an internal (in-memory) name as well as its system thread name...
Definition: threadnames.cpp:57
Ask passphrase twice and encrypt.
QString walletName() const
OpenWalletActivity(WalletController *wallet_controller, QWidget *parent_widget)
Controller between interfaces::Node, WalletModel instances and the GUI.
QProgressDialog * m_progress_dialog
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:215
std::string translated
Definition: translation.h:18
bool isDisablePrivateKeysChecked() const
QObject *const m_activity_worker
void coinsSent(WalletModel *wallet_model, SendCoinsRecipient recipient, QByteArray transaction)
std::map< std::string, bool > listWalletDir() const
Returns all wallet names in the wallet dir mapped to whether the wallet is loaded.
bool isMakeBlankWalletChecked() const
void created(WalletModel *wallet_model)
void closeWallet(WalletModel *wallet_model, QWidget *parent=nullptr)
const char * name
Definition: rest.cpp:41
virtual void remove()=0
QWidget *const m_parent_widget
Flag set when a wallet contains no HD seed and no private keys, scripts, addresses, and other watch only things, and is therefore "blank.".
Definition: walletutil.h:59
void open(const std::string &path)
virtual std::unique_ptr< Handler > handleLoadWallet(LoadWalletFn fn)=0
std::vector< bilingual_str > m_warning_message
ClientModel & m_client_model
interfaces::Node & m_node
void walletAdded(WalletModel *wallet_model)
void PolishProgressDialog(QProgressDialog *dialog)
Definition: guiutil.cpp:876
Model for Bitcoin network client.
Definition: clientmodel.h:46
AskPassphraseDialog * m_passphrase_dialog
int flags
Definition: bitcoin-tx.cpp:506
virtual std::unique_ptr< Wallet > loadWallet(const std::string &name, bilingual_str &error, std::vector< bilingual_str > &warnings)=0
Load existing wallet.
std::vector< WalletModel * > getOpenWallets() const
Returns wallet models currently open.
virtual WalletClient & walletClient()=0
Get wallet client.
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:51
Multifunctional dialog to ask for passphrases.
Dialog for creating wallets.
WalletModel * getOrCreateWallet(std::unique_ptr< interfaces::Wallet > wallet)
void walletRemoved(WalletModel *wallet_model)
virtual std::unique_ptr< Wallet > createWallet(const std::string &name, const SecureString &passphrase, uint64_t wallet_creation_flags, bilingual_str &error, std::vector< bilingual_str > &warnings)=0
Create new wallet.
QObject * worker() const
WalletControllerActivity(WalletController *wallet_controller, QWidget *parent_widget)
QString getDisplayName() const
QThread *const m_activity_thread
WalletController(ClientModel &client_model, const PlatformStyle *platform_style, QObject *parent)
std::unique_ptr< interfaces::Handler > m_handler_load_wallet
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:62
void removeAndDeleteWallet(WalletModel *wallet_model)
WalletController *const m_wallet_controller
virtual std::vector< std::unique_ptr< Wallet > > getWallets()=0
Return interfaces for accessing wallets (if any).