1 // Copyright (c) 2011-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 #if defined(HAVE_CONFIG_H)
6 #include <config/bitcoin-config.h>
7 #endif
8
9 #include <qt/walletmodel.h>
10
11 #include <qt/addresstablemodel.h>
12 #include <qt/clientmodel.h>
13 #include <qt/guiconstants.h>
14 #include <qt/guiutil.h>
15 #include <qt/optionsmodel.h>
16 #include <qt/paymentserver.h>
17 #include <qt/recentrequeststablemodel.h>
18 #include <qt/sendcoinsdialog.h>
19 #include <qt/transactiontablemodel.h>
20
21 #include <interfaces/handler.h>
22 #include <interfaces/node.h>
23 #include <key_io.h>
24 #include <node/ui_interface.h>
25 #include <psbt.h>
26 #include <util/system.h> // for GetBoolArg
27 #include <util/translation.h>
28 #include <wallet/coincontrol.h>
29 #include <wallet/wallet.h> // for CRecipient
30
31 #include <stdint.h>
32
33 #include <QDebug>
34 #include <QMessageBox>
35 #include <QSet>
36 #include <QTimer>
37
38
WalletModel(std::unique_ptr<interfaces::Wallet> wallet,ClientModel & client_model,const PlatformStyle * platformStyle,QObject * parent)39 WalletModel::WalletModel(std::unique_ptr<interfaces::Wallet> wallet, ClientModel& client_model, const PlatformStyle *platformStyle, QObject *parent) :
40 QObject(parent),
41 m_wallet(std::move(wallet)),
42 m_client_model(&client_model),
43 m_node(client_model.node()),
44 optionsModel(client_model.getOptionsModel()),
45 addressTableModel(nullptr),
46 transactionTableModel(nullptr),
47 recentRequestsTableModel(nullptr),
48 cachedEncryptionStatus(Unencrypted),
49 timer(new QTimer(this))
50 {
51 fHaveWatchOnly = m_wallet->haveWatchOnly();
52 addressTableModel = new AddressTableModel(this);
53 transactionTableModel = new TransactionTableModel(platformStyle, this);
54 recentRequestsTableModel = new RecentRequestsTableModel(this);
55
56 subscribeToCoreSignals();
57 }
58
~WalletModel()59 WalletModel::~WalletModel()
60 {
61 unsubscribeFromCoreSignals();
62 }
63
startPollBalance()64 void WalletModel::startPollBalance()
65 {
66 // This timer will be fired repeatedly to update the balance
67 connect(timer, &QTimer::timeout, this, &WalletModel::pollBalanceChanged);
68 timer->start(MODEL_UPDATE_DELAY);
69 }
70
setClientModel(ClientModel * client_model)71 void WalletModel::setClientModel(ClientModel* client_model)
72 {
73 m_client_model = client_model;
74 if (!m_client_model) timer->stop();
75 }
76
updateStatus()77 void WalletModel::updateStatus()
78 {
79 EncryptionStatus newEncryptionStatus = getEncryptionStatus();
80
81 if(cachedEncryptionStatus != newEncryptionStatus) {
82 Q_EMIT encryptionStatusChanged();
83 }
84 }
85
pollBalanceChanged()86 void WalletModel::pollBalanceChanged()
87 {
88 // Avoid recomputing wallet balances unless a TransactionChanged or
89 // BlockTip notification was received.
90 if (!fForceCheckBalanceChanged && m_cached_last_update_tip == getLastBlockProcessed()) return;
91
92 // Try to get balances and return early if locks can't be acquired. This
93 // avoids the GUI from getting stuck on periodical polls if the core is
94 // holding the locks for a longer time - for example, during a wallet
95 // rescan.
96 interfaces::WalletBalances new_balances;
97 uint256 block_hash;
98 if (!m_wallet->tryGetBalances(new_balances, block_hash)) {
99 return;
100 }
101
102 if (fForceCheckBalanceChanged || block_hash != m_cached_last_update_tip) {
103 fForceCheckBalanceChanged = false;
104
105 // Balance and number of transactions might have changed
106 m_cached_last_update_tip = block_hash;
107
108 checkBalanceChanged(new_balances);
109 if(transactionTableModel)
110 transactionTableModel->updateConfirmations();
111 }
112 }
113
checkBalanceChanged(const interfaces::WalletBalances & new_balances)114 void WalletModel::checkBalanceChanged(const interfaces::WalletBalances& new_balances)
115 {
116 if(new_balances.balanceChanged(m_cached_balances)) {
117 m_cached_balances = new_balances;
118 Q_EMIT balanceChanged(new_balances);
119 }
120 }
121
updateTransaction()122 void WalletModel::updateTransaction()
123 {
124 // Balance and number of transactions might have changed
125 fForceCheckBalanceChanged = true;
126 }
127
updateAddressBook(const QString & address,const QString & label,bool isMine,const QString & purpose,int status)128 void WalletModel::updateAddressBook(const QString &address, const QString &label,
129 bool isMine, const QString &purpose, int status)
130 {
131 if(addressTableModel)
132 addressTableModel->updateEntry(address, label, isMine, purpose, status);
133 }
134
updateWatchOnlyFlag(bool fHaveWatchonly)135 void WalletModel::updateWatchOnlyFlag(bool fHaveWatchonly)
136 {
137 fHaveWatchOnly = fHaveWatchonly;
138 Q_EMIT notifyWatchonlyChanged(fHaveWatchonly);
139 }
140
validateAddress(const QString & address)141 bool WalletModel::validateAddress(const QString &address)
142 {
143 return IsValidDestinationString(address.toStdString());
144 }
145
prepareTransaction(WalletModelTransaction & transaction,const CCoinControl & coinControl)146 WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransaction &transaction, const CCoinControl& coinControl)
147 {
148 CAmount total = 0;
149 bool fSubtractFeeFromAmount = false;
150 QList<SendCoinsRecipient> recipients = transaction.getRecipients();
151 std::vector<CRecipient> vecSend;
152
153 if(recipients.empty())
154 {
155 return OK;
156 }
157
158 QSet<QString> setAddress; // Used to detect duplicates
159 int nAddresses = 0;
160
161 // Pre-check input data for validity
162 for (const SendCoinsRecipient &rcp : recipients)
163 {
164 if (rcp.fSubtractFeeFromAmount)
165 fSubtractFeeFromAmount = true;
166 { // User-entered bitcoin address / amount:
167 if(!validateAddress(rcp.address))
168 {
169 return InvalidAddress;
170 }
171 if(rcp.amount <= 0)
172 {
173 return InvalidAmount;
174 }
175 setAddress.insert(rcp.address);
176 ++nAddresses;
177
178 CScript scriptPubKey = GetScriptForDestination(DecodeDestination(rcp.address.toStdString()));
179 CRecipient recipient = {scriptPubKey, rcp.amount, rcp.fSubtractFeeFromAmount};
180 vecSend.push_back(recipient);
181
182 total += rcp.amount;
183 }
184 }
185 if(setAddress.size() != nAddresses)
186 {
187 return DuplicateAddress;
188 }
189
190 CAmount nBalance = m_wallet->getAvailableBalance(coinControl);
191
192 if(total > nBalance)
193 {
194 return AmountExceedsBalance;
195 }
196
197 {
198 CAmount nFeeRequired = 0;
199 int nChangePosRet = -1;
200 bilingual_str error;
201
202 auto& newTx = transaction.getWtx();
203 newTx = m_wallet->createTransaction(vecSend, coinControl, !wallet().privateKeysDisabled() /* sign */, nChangePosRet, nFeeRequired, error);
204 transaction.setTransactionFee(nFeeRequired);
205 if (fSubtractFeeFromAmount && newTx)
206 transaction.reassignAmounts(nChangePosRet);
207
208 if(!newTx)
209 {
210 if(!fSubtractFeeFromAmount && (total + nFeeRequired) > nBalance)
211 {
212 return SendCoinsReturn(AmountWithFeeExceedsBalance);
213 }
214 Q_EMIT message(tr("Send Coins"), QString::fromStdString(error.translated),
215 CClientUIInterface::MSG_ERROR);
216 return TransactionCreationFailed;
217 }
218
219 // Reject absurdly high fee. (This can never happen because the
220 // wallet never creates transactions with fee greater than
221 // m_default_max_tx_fee. This merely a belt-and-suspenders check).
222 if (nFeeRequired > m_wallet->getDefaultMaxTxFee()) {
223 return AbsurdFee;
224 }
225 }
226
227 return SendCoinsReturn(OK);
228 }
229
sendCoins(WalletModelTransaction & transaction)230 WalletModel::SendCoinsReturn WalletModel::sendCoins(WalletModelTransaction &transaction)
231 {
232 QByteArray transaction_array; /* store serialized transaction */
233
234 {
235 std::vector<std::pair<std::string, std::string>> vOrderForm;
236 for (const SendCoinsRecipient &rcp : transaction.getRecipients())
237 {
238 if (!rcp.message.isEmpty()) // Message from normal bitcoin:URI (bitcoin:123...?message=example)
239 vOrderForm.emplace_back("Message", rcp.message.toStdString());
240 }
241
242 auto& newTx = transaction.getWtx();
243 wallet().commitTransaction(newTx, {} /* mapValue */, std::move(vOrderForm));
244
245 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
246 ssTx << *newTx;
247 transaction_array.append(&(ssTx[0]), ssTx.size());
248 }
249
250 // Add addresses / update labels that we've sent to the address book,
251 // and emit coinsSent signal for each recipient
252 for (const SendCoinsRecipient &rcp : transaction.getRecipients())
253 {
254 {
255 std::string strAddress = rcp.address.toStdString();
256 CTxDestination dest = DecodeDestination(strAddress);
257 std::string strLabel = rcp.label.toStdString();
258 {
259 // Check if we have a new address or an updated label
260 std::string name;
261 if (!m_wallet->getAddress(
262 dest, &name, /* is_mine= */ nullptr, /* purpose= */ nullptr))
263 {
264 m_wallet->setAddressBook(dest, strLabel, "send");
265 }
266 else if (name != strLabel)
267 {
268 m_wallet->setAddressBook(dest, strLabel, ""); // "" means don't change purpose
269 }
270 }
271 }
272 Q_EMIT coinsSent(this, rcp, transaction_array);
273 }
274
275 checkBalanceChanged(m_wallet->getBalances()); // update balance immediately, otherwise there could be a short noticeable delay until pollBalanceChanged hits
276
277 return SendCoinsReturn(OK);
278 }
279
getOptionsModel()280 OptionsModel *WalletModel::getOptionsModel()
281 {
282 return optionsModel;
283 }
284
getAddressTableModel()285 AddressTableModel *WalletModel::getAddressTableModel()
286 {
287 return addressTableModel;
288 }
289
getTransactionTableModel()290 TransactionTableModel *WalletModel::getTransactionTableModel()
291 {
292 return transactionTableModel;
293 }
294
getRecentRequestsTableModel()295 RecentRequestsTableModel *WalletModel::getRecentRequestsTableModel()
296 {
297 return recentRequestsTableModel;
298 }
299
getEncryptionStatus() const300 WalletModel::EncryptionStatus WalletModel::getEncryptionStatus() const
301 {
302 if(!m_wallet->isCrypted())
303 {
304 return Unencrypted;
305 }
306 else if(m_wallet->isLocked())
307 {
308 return Locked;
309 }
310 else
311 {
312 return Unlocked;
313 }
314 }
315
setWalletEncrypted(bool encrypted,const SecureString & passphrase)316 bool WalletModel::setWalletEncrypted(bool encrypted, const SecureString &passphrase)
317 {
318 if (encrypted) {
319 return m_wallet->encryptWallet(passphrase);
320 }
321 return false;
322 }
323
setWalletLocked(bool locked,const SecureString & passPhrase)324 bool WalletModel::setWalletLocked(bool locked, const SecureString &passPhrase)
325 {
326 if(locked)
327 {
328 // Lock
329 return m_wallet->lock();
330 }
331 else
332 {
333 // Unlock
334 return m_wallet->unlock(passPhrase);
335 }
336 }
337
changePassphrase(const SecureString & oldPass,const SecureString & newPass)338 bool WalletModel::changePassphrase(const SecureString &oldPass, const SecureString &newPass)
339 {
340 m_wallet->lock(); // Make sure wallet is locked before attempting pass change
341 return m_wallet->changeWalletPassphrase(oldPass, newPass);
342 }
343
344 // Handlers for core signals
NotifyUnload(WalletModel * walletModel)345 static void NotifyUnload(WalletModel* walletModel)
346 {
347 qDebug() << "NotifyUnload";
348 bool invoked = QMetaObject::invokeMethod(walletModel, "unload");
349 assert(invoked);
350 }
351
NotifyKeyStoreStatusChanged(WalletModel * walletmodel)352 static void NotifyKeyStoreStatusChanged(WalletModel *walletmodel)
353 {
354 qDebug() << "NotifyKeyStoreStatusChanged";
355 bool invoked = QMetaObject::invokeMethod(walletmodel, "updateStatus", Qt::QueuedConnection);
356 assert(invoked);
357 }
358
NotifyAddressBookChanged(WalletModel * walletmodel,const CTxDestination & address,const std::string & label,bool isMine,const std::string & purpose,ChangeType status)359 static void NotifyAddressBookChanged(WalletModel *walletmodel,
360 const CTxDestination &address, const std::string &label, bool isMine,
361 const std::string &purpose, ChangeType status)
362 {
363 QString strAddress = QString::fromStdString(EncodeDestination(address));
364 QString strLabel = QString::fromStdString(label);
365 QString strPurpose = QString::fromStdString(purpose);
366
367 qDebug() << "NotifyAddressBookChanged: " + strAddress + " " + strLabel + " isMine=" + QString::number(isMine) + " purpose=" + strPurpose + " status=" + QString::number(status);
368 bool invoked = QMetaObject::invokeMethod(walletmodel, "updateAddressBook", Qt::QueuedConnection,
369 Q_ARG(QString, strAddress),
370 Q_ARG(QString, strLabel),
371 Q_ARG(bool, isMine),
372 Q_ARG(QString, strPurpose),
373 Q_ARG(int, status));
374 assert(invoked);
375 }
376
NotifyTransactionChanged(WalletModel * walletmodel,const uint256 & hash,ChangeType status)377 static void NotifyTransactionChanged(WalletModel *walletmodel, const uint256 &hash, ChangeType status)
378 {
379 Q_UNUSED(hash);
380 Q_UNUSED(status);
381 bool invoked = QMetaObject::invokeMethod(walletmodel, "updateTransaction", Qt::QueuedConnection);
382 assert(invoked);
383 }
384
ShowProgress(WalletModel * walletmodel,const std::string & title,int nProgress)385 static void ShowProgress(WalletModel *walletmodel, const std::string &title, int nProgress)
386 {
387 // emits signal "showProgress"
388 bool invoked = QMetaObject::invokeMethod(walletmodel, "showProgress", Qt::QueuedConnection,
389 Q_ARG(QString, QString::fromStdString(title)),
390 Q_ARG(int, nProgress));
391 assert(invoked);
392 }
393
NotifyWatchonlyChanged(WalletModel * walletmodel,bool fHaveWatchonly)394 static void NotifyWatchonlyChanged(WalletModel *walletmodel, bool fHaveWatchonly)
395 {
396 bool invoked = QMetaObject::invokeMethod(walletmodel, "updateWatchOnlyFlag", Qt::QueuedConnection,
397 Q_ARG(bool, fHaveWatchonly));
398 assert(invoked);
399 }
400
NotifyCanGetAddressesChanged(WalletModel * walletmodel)401 static void NotifyCanGetAddressesChanged(WalletModel* walletmodel)
402 {
403 bool invoked = QMetaObject::invokeMethod(walletmodel, "canGetAddressesChanged");
404 assert(invoked);
405 }
406
subscribeToCoreSignals()407 void WalletModel::subscribeToCoreSignals()
408 {
409 // Connect signals to wallet
410 m_handler_unload = m_wallet->handleUnload(std::bind(&NotifyUnload, this));
411 m_handler_status_changed = m_wallet->handleStatusChanged(std::bind(&NotifyKeyStoreStatusChanged, this));
412 m_handler_address_book_changed = m_wallet->handleAddressBookChanged(std::bind(NotifyAddressBookChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
413 m_handler_transaction_changed = m_wallet->handleTransactionChanged(std::bind(NotifyTransactionChanged, this, std::placeholders::_1, std::placeholders::_2));
414 m_handler_show_progress = m_wallet->handleShowProgress(std::bind(ShowProgress, this, std::placeholders::_1, std::placeholders::_2));
415 m_handler_watch_only_changed = m_wallet->handleWatchOnlyChanged(std::bind(NotifyWatchonlyChanged, this, std::placeholders::_1));
416 m_handler_can_get_addrs_changed = m_wallet->handleCanGetAddressesChanged(std::bind(NotifyCanGetAddressesChanged, this));
417 }
418
unsubscribeFromCoreSignals()419 void WalletModel::unsubscribeFromCoreSignals()
420 {
421 // Disconnect signals from wallet
422 m_handler_unload->disconnect();
423 m_handler_status_changed->disconnect();
424 m_handler_address_book_changed->disconnect();
425 m_handler_transaction_changed->disconnect();
426 m_handler_show_progress->disconnect();
427 m_handler_watch_only_changed->disconnect();
428 m_handler_can_get_addrs_changed->disconnect();
429 }
430
431 // WalletModel::UnlockContext implementation
requestUnlock()432 WalletModel::UnlockContext WalletModel::requestUnlock()
433 {
434 bool was_locked = getEncryptionStatus() == Locked;
435 if(was_locked)
436 {
437 // Request UI to unlock wallet
438 Q_EMIT requireUnlock();
439 }
440 // If wallet is still locked, unlock was failed or cancelled, mark context as invalid
441 bool valid = getEncryptionStatus() != Locked;
442
443 return UnlockContext(this, valid, was_locked);
444 }
445
UnlockContext(WalletModel * _wallet,bool _valid,bool _relock)446 WalletModel::UnlockContext::UnlockContext(WalletModel *_wallet, bool _valid, bool _relock):
447 wallet(_wallet),
448 valid(_valid),
449 relock(_relock)
450 {
451 }
452
~UnlockContext()453 WalletModel::UnlockContext::~UnlockContext()
454 {
455 if(valid && relock)
456 {
457 wallet->setWalletLocked(true);
458 }
459 }
460
CopyFrom(UnlockContext && rhs)461 void WalletModel::UnlockContext::CopyFrom(UnlockContext&& rhs)
462 {
463 // Transfer context; old object no longer relocks wallet
464 *this = rhs;
465 rhs.relock = false;
466 }
467
loadReceiveRequests(std::vector<std::string> & vReceiveRequests)468 void WalletModel::loadReceiveRequests(std::vector<std::string>& vReceiveRequests)
469 {
470 vReceiveRequests = m_wallet->getDestValues("rr"); // receive request
471 }
472
saveReceiveRequest(const std::string & sAddress,const int64_t nId,const std::string & sRequest)473 bool WalletModel::saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest)
474 {
475 CTxDestination dest = DecodeDestination(sAddress);
476
477 std::stringstream ss;
478 ss << nId;
479 std::string key = "rr" + ss.str(); // "rr" prefix = "receive request" in destdata
480
481 if (sRequest.empty())
482 return m_wallet->eraseDestData(dest, key);
483 else
484 return m_wallet->addDestData(dest, key, sRequest);
485 }
486
bumpFee(uint256 hash,uint256 & new_hash)487 bool WalletModel::bumpFee(uint256 hash, uint256& new_hash)
488 {
489 CCoinControl coin_control;
490 coin_control.m_signal_bip125_rbf = true;
491 std::vector<bilingual_str> errors;
492 CAmount old_fee;
493 CAmount new_fee;
494 CMutableTransaction mtx;
495 if (!m_wallet->createBumpTransaction(hash, coin_control, errors, old_fee, new_fee, mtx)) {
496 QMessageBox::critical(nullptr, tr("Fee bump error"), tr("Increasing transaction fee failed") + "<br />(" +
497 (errors.size() ? QString::fromStdString(errors[0].translated) : "") +")");
498 return false;
499 }
500
501 const bool create_psbt = m_wallet->privateKeysDisabled();
502
503 // allow a user based fee verification
504 QString questionString = create_psbt ? tr("Do you want to draft a transaction with fee increase?") : tr("Do you want to increase the fee?");
505 questionString.append("<br />");
506 questionString.append("<table style=\"text-align: left;\">");
507 questionString.append("<tr><td>");
508 questionString.append(tr("Current fee:"));
509 questionString.append("</td><td>");
510 questionString.append(BitcoinUnits::formatHtmlWithUnit(getOptionsModel()->getDisplayUnit(), old_fee));
511 questionString.append("</td></tr><tr><td>");
512 questionString.append(tr("Increase:"));
513 questionString.append("</td><td>");
514 questionString.append(BitcoinUnits::formatHtmlWithUnit(getOptionsModel()->getDisplayUnit(), new_fee - old_fee));
515 questionString.append("</td></tr><tr><td>");
516 questionString.append(tr("New fee:"));
517 questionString.append("</td><td>");
518 questionString.append(BitcoinUnits::formatHtmlWithUnit(getOptionsModel()->getDisplayUnit(), new_fee));
519 questionString.append("</td></tr></table>");
520 SendConfirmationDialog confirmationDialog(tr("Confirm fee bump"), questionString);
521 confirmationDialog.exec();
522 QMessageBox::StandardButton retval = static_cast<QMessageBox::StandardButton>(confirmationDialog.result());
523
524 // cancel sign&broadcast if user doesn't want to bump the fee
525 if (retval != QMessageBox::Yes) {
526 return false;
527 }
528
529 WalletModel::UnlockContext ctx(requestUnlock());
530 if(!ctx.isValid())
531 {
532 return false;
533 }
534
535 // Short-circuit if we are returning a bumped transaction PSBT to clipboard
536 if (create_psbt) {
537 PartiallySignedTransaction psbtx(mtx);
538 bool complete = false;
539 const TransactionError err = wallet().fillPSBT(SIGHASH_ALL, false /* sign */, true /* bip32derivs */, psbtx, complete, nullptr);
540 if (err != TransactionError::OK || complete) {
541 QMessageBox::critical(nullptr, tr("Fee bump error"), tr("Can't draft transaction."));
542 return false;
543 }
544 // Serialize the PSBT
545 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
546 ssTx << psbtx;
547 GUIUtil::setClipboard(EncodeBase64(ssTx.str()).c_str());
548 Q_EMIT message(tr("PSBT copied"), "Copied to clipboard", CClientUIInterface::MSG_INFORMATION);
549 return true;
550 }
551
552 // sign bumped transaction
553 if (!m_wallet->signBumpTransaction(mtx)) {
554 QMessageBox::critical(nullptr, tr("Fee bump error"), tr("Can't sign transaction."));
555 return false;
556 }
557 // commit the bumped transaction
558 if(!m_wallet->commitBumpTransaction(hash, std::move(mtx), errors, new_hash)) {
559 QMessageBox::critical(nullptr, tr("Fee bump error"), tr("Could not commit transaction") + "<br />(" +
560 QString::fromStdString(errors[0].translated)+")");
561 return false;
562 }
563 return true;
564 }
565
isWalletEnabled()566 bool WalletModel::isWalletEnabled()
567 {
568 return !gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET);
569 }
570
getWalletName() const571 QString WalletModel::getWalletName() const
572 {
573 return QString::fromStdString(m_wallet->getWalletName());
574 }
575
getDisplayName() const576 QString WalletModel::getDisplayName() const
577 {
578 const QString name = getWalletName();
579 return name.isEmpty() ? "["+tr("default wallet")+"]" : name;
580 }
581
isMultiwallet()582 bool WalletModel::isMultiwallet()
583 {
584 return m_node.walletClient().getWallets().size() > 1;
585 }
586
refresh(bool pk_hash_only)587 void WalletModel::refresh(bool pk_hash_only)
588 {
589 addressTableModel = new AddressTableModel(this, pk_hash_only);
590 }
591
getLastBlockProcessed() const592 uint256 WalletModel::getLastBlockProcessed() const
593 {
594 return m_client_model ? m_client_model->getBestBlockHash() : uint256{};
595 }
596