Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
walletmodel.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2019 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/walletmodel.h>
6
7#include <cashaddrenc.h>
8#include <common/args.h> // for GetBoolArg
10#include <interfaces/node.h>
11#include <key_io.h>
12#include <node/ui_interface.h>
13#include <psbt.h>
15#include <qt/clientmodel.h>
16#include <qt/guiconstants.h>
17#include <qt/paymentserver.h>
20#include <util/translation.h>
21#include <wallet/coincontrol.h>
22#include <wallet/wallet.h> // for CRecipient
23
24#include <QDebug>
25#include <QSet>
26#include <QTimer>
27
28#include <cstdint>
29#include <functional>
30
31WalletModel::WalletModel(std::unique_ptr<interfaces::Wallet> wallet,
33 const PlatformStyle *platformStyle, QObject *parent)
34 : QObject(parent), m_wallet(std::move(wallet)),
35 m_client_model(&client_model), m_node(client_model.node()),
36 optionsModel(client_model.getOptionsModel()), addressTableModel(nullptr),
37 transactionTableModel(nullptr), recentRequestsTableModel(nullptr),
38 cachedEncryptionStatus(Unencrypted), timer(new QTimer(this)) {
39 fHaveWatchOnly = m_wallet->haveWatchOnly();
41 transactionTableModel = new TransactionTableModel(platformStyle, this);
43
45}
46
50
52 // This timer will be fired repeatedly to update the balance
53 connect(timer, &QTimer::timeout, this, &WalletModel::pollBalanceChanged);
55}
56
63
71
73 // Avoid recomputing wallet balances unless a TransactionChanged or
74 // BlockTip notification was received.
77 return;
78 }
79
80 // Try to get balances and return early if locks can't be acquired. This
81 // avoids the GUI from getting stuck on periodical polls if the core is
82 // holding the locks for a longer time - for example, during a wallet
83 // rescan.
85 BlockHash block_hash;
86 if (!m_wallet->tryGetBalances(new_balances, block_hash)) {
87 return;
88 }
89
92
93 // Balance and number of transactions might have changed
94 m_cached_last_update_tip = block_hash;
95
99 }
100 }
101}
102
110
112 // Balance and number of transactions might have changed
114}
115
117 const QString &label, bool isMine,
118 const QString &purpose, int status) {
119 if (addressTableModel) {
120 addressTableModel->updateEntry(address, label, isMine, purpose, status);
121 }
122}
123
128
130 return IsValidDestinationString(address.toStdString(), getChainParams());
131}
132
135 const CCoinControl &coinControl) {
136 Amount total = Amount::zero();
137 bool fSubtractFeeFromAmount = false;
138 QList<SendCoinsRecipient> recipients = transaction.getRecipients();
139 std::vector<CRecipient> vecSend;
140
141 if (recipients.empty()) {
142 return OK;
143 }
144
145 // Used to detect duplicates
146 QSet<QString> setAddress;
147 int nAddresses = 0;
148
149 // Pre-check input data for validity
150 for (const SendCoinsRecipient &rcp : recipients) {
151 if (rcp.fSubtractFeeFromAmount) {
152 fSubtractFeeFromAmount = true;
153 }
154
155#ifdef ENABLE_BIP70
156 // PaymentRequest...
157 if (rcp.paymentRequest.IsInitialized()) {
159 const payments::PaymentDetails &details =
160 rcp.paymentRequest.getDetails();
161 for (int i = 0; i < details.outputs_size(); i++) {
162 const payments::Output &out = details.outputs(i);
163 if (out.amount() <= 0) {
164 continue;
165 }
166
167 subtotal += int64_t(out.amount()) * SATOSHI;
168 const uint8_t *scriptStr = (const uint8_t *)out.script().data();
169 CScript scriptPubKey(scriptStr,
170 scriptStr + out.script().size());
171 Amount nAmount = int64_t(out.amount()) * SATOSHI;
172 CRecipient recipient = {scriptPubKey, nAmount,
173 rcp.fSubtractFeeFromAmount};
174 vecSend.push_back(recipient);
175 }
176
177 if (subtotal <= Amount::zero()) {
178 return InvalidAmount;
179 }
180 total += subtotal;
181 }
182
183 // User-entered bitcoin address / amount:
184 else
185#endif
186 {
187 if (!validateAddress(rcp.address)) {
188 return InvalidAddress;
189 }
190 if (rcp.amount <= Amount::zero()) {
191 return InvalidAmount;
192 }
193 setAddress.insert(rcp.address);
194 ++nAddresses;
195
196 CScript scriptPubKey = GetScriptForDestination(
197 DecodeDestination(rcp.address.toStdString(), getChainParams()));
198 CRecipient recipient = {scriptPubKey, Amount(rcp.amount),
199 rcp.fSubtractFeeFromAmount};
200 vecSend.push_back(recipient);
201
202 total += rcp.amount;
203 }
204 }
205 if (setAddress.size() != nAddresses) {
206 return DuplicateAddress;
207 }
208
209 Amount nBalance = m_wallet->getAvailableBalance(coinControl);
210
211 if (total > nBalance) {
213 }
214
216 int nChangePosRet = -1;
218
219 auto &newTx = transaction.getWtx();
220 newTx = m_wallet->createTransaction(
221 vecSend, coinControl, !wallet().privateKeysDisabled() /* sign */,
223 transaction.setTransactionFee(nFeeRequired);
224 if (fSubtractFeeFromAmount && newTx) {
225 transaction.reassignAmounts(nChangePosRet);
226 }
227
228 if (!newTx) {
229 if (!fSubtractFeeFromAmount && (total + nFeeRequired) > nBalance) {
231 }
232 Q_EMIT message(tr("Send Coins"),
233 QString::fromStdString(error.translated),
236 }
237
238 // Reject absurdly high fee. (This can never happen because the
239 // wallet never creates transactions with fee greater than
240 // m_default_max_tx_fee. This merely a belt-and-suspenders check).
241 if (nFeeRequired > m_wallet->getDefaultMaxTxFee()) {
242 return AbsurdFee;
243 }
244
245 return SendCoinsReturn(OK);
246}
247
250 /* store serialized transaction */
252
253 std::vector<std::pair<std::string, std::string>> vOrderForm;
254 for (const SendCoinsRecipient &rcp : transaction.getRecipients()) {
255#ifdef ENABLE_BIP70
256 if (rcp.paymentRequest.IsInitialized()) {
257 // Make sure any payment requests involved are still valid.
258 if (PaymentServer::verifyExpired(rcp.paymentRequest.getDetails())) {
260 }
261
262 // Store PaymentRequests in wtx.vOrderForm in wallet.
263 std::string value;
264 rcp.paymentRequest.SerializeToString(&value);
265 vOrderForm.emplace_back("PaymentRequest", std::move(value));
266 } else
267#endif
268 {
269 if (!rcp.message.isEmpty()) {
270 // Message from normal bitcoincash:URI
271 // (bitcoincash:123...?message=example)
272 vOrderForm.emplace_back("Message", rcp.message.toStdString());
273 }
274 }
275 }
276
277 auto &newTx = transaction.getWtx();
278 wallet().commitTransaction(newTx, {} /* mapValue */, std::move(vOrderForm));
279
281 ssTx << *newTx;
282 transaction_array.append((const char *)ssTx.data(), ssTx.size());
283
284 // Add addresses / update labels that we've sent to the address book, and
285 // emit coinsSent signal for each recipient
286 for (const SendCoinsRecipient &rcp : transaction.getRecipients()) {
287 // Don't touch the address book when we have a payment request
288#ifdef ENABLE_BIP70
289 if (!rcp.paymentRequest.IsInitialized())
290#endif
291 {
292 std::string strAddress = rcp.address.toStdString();
293 CTxDestination dest =
295 std::string strLabel = rcp.label.toStdString();
296 // Check if we have a new address or an updated label
297 std::string name;
298 if (!m_wallet->getAddress(dest, &name, /* is_mine= */ nullptr,
299 /* purpose= */ nullptr)) {
300 m_wallet->setAddressBook(dest, strLabel, "send");
301 } else if (name != strLabel) {
302 // "" means don't change purpose
303 m_wallet->setAddressBook(dest, strLabel, "");
304 }
305 }
306 Q_EMIT coinsSent(this->wallet(), rcp, transaction_array);
307 }
308
309 // update balance immediately, otherwise there could be a short noticeable
310 // delay until pollBalanceChanged hits
311 checkBalanceChanged(m_wallet->getBalances());
312
313 return SendCoinsReturn(OK);
314}
315
319
323
327
331
333 if (!m_wallet->isCrypted()) {
334 return Unencrypted;
335 } else if (m_wallet->isLocked()) {
336 return Locked;
337 } else {
338 return Unlocked;
339 }
340}
341
343 return m_wallet->encryptWallet(passphrase);
344}
345
347 if (locked) {
348 // Lock
349 return m_wallet->lock();
350 } else {
351 // Unlock
352 return m_wallet->unlock(passPhrase);
353 }
354}
355
357 const SecureString &newPass) {
358 // Make sure wallet is locked before attempting pass change
359 m_wallet->lock();
360 return m_wallet->changeWalletPassphrase(oldPass, newPass);
361}
362
363// Handlers for core signals
364static void NotifyUnload(WalletModel *walletModel) {
365 qDebug() << "NotifyUnload";
366 bool invoked = QMetaObject::invokeMethod(walletModel, "unload");
368}
369
371 qDebug() << "NotifyKeyStoreStatusChanged";
372 bool invoked = QMetaObject::invokeMethod(walletmodel, "updateStatus",
373 Qt::QueuedConnection);
375}
376
378 const CTxDestination &address,
379 const std::string &label, bool isMine,
380 const std::string &purpose,
381 ChangeType status) {
382 QString strAddress = QString::fromStdString(
383 EncodeCashAddr(address, walletmodel->getChainParams()));
384 QString strLabel = QString::fromStdString(label);
385 QString strPurpose = QString::fromStdString(purpose);
386
387 qDebug() << "NotifyAddressBookChanged: " + strAddress + " " + strLabel +
388 " isMine=" + QString::number(isMine) +
389 " purpose=" + strPurpose +
390 " status=" + QString::number(status);
391 bool invoked = QMetaObject::invokeMethod(
392 walletmodel, "updateAddressBook", Qt::QueuedConnection,
394 Q_ARG(bool, isMine), Q_ARG(QString, strPurpose), Q_ARG(int, status));
396}
397
399 ChangeType status) {
400 Q_UNUSED(hash);
401 Q_UNUSED(status);
402 bool invoked = QMetaObject::invokeMethod(walletmodel, "updateTransaction",
403 Qt::QueuedConnection);
405}
406
407static void ShowProgress(WalletModel *walletmodel, const std::string &title,
408 int nProgress) {
409 // emits signal "showProgress"
410 bool invoked = QMetaObject::invokeMethod(
411 walletmodel, "showProgress", Qt::QueuedConnection,
412 Q_ARG(QString, QString::fromStdString(title)), Q_ARG(int, nProgress));
414}
415
417 bool fHaveWatchonly) {
418 bool invoked = QMetaObject::invokeMethod(walletmodel, "updateWatchOnlyFlag",
419 Qt::QueuedConnection,
420 Q_ARG(bool, fHaveWatchonly));
422}
423
425 bool invoked =
426 QMetaObject::invokeMethod(walletmodel, "canGetAddressesChanged");
428}
429
431 // Connect signals to wallet
432 m_handler_unload = m_wallet->handleUnload(std::bind(&NotifyUnload, this));
433 m_handler_status_changed = m_wallet->handleStatusChanged(
434 std::bind(&NotifyKeyStoreStatusChanged, this));
435 m_handler_address_book_changed = m_wallet->handleAddressBookChanged(
436 std::bind(NotifyAddressBookChanged, this, std::placeholders::_1,
437 std::placeholders::_2, std::placeholders::_3,
438 std::placeholders::_4, std::placeholders::_5));
439 m_handler_transaction_changed = m_wallet->handleTransactionChanged(
440 std::bind(NotifyTransactionChanged, this, std::placeholders::_1,
441 std::placeholders::_2));
442 m_handler_show_progress = m_wallet->handleShowProgress(std::bind(
443 ShowProgress, this, std::placeholders::_1, std::placeholders::_2));
444 m_handler_watch_only_changed = m_wallet->handleWatchOnlyChanged(
445 std::bind(NotifyWatchonlyChanged, this, std::placeholders::_1));
446 m_handler_can_get_addrs_changed = m_wallet->handleCanGetAddressesChanged(
447 std::bind(NotifyCanGetAddressesChanged, this));
448}
449
451 // Disconnect signals from wallet
452 m_handler_unload->disconnect();
453 m_handler_status_changed->disconnect();
454 m_handler_address_book_changed->disconnect();
455 m_handler_transaction_changed->disconnect();
456 m_handler_show_progress->disconnect();
457 m_handler_watch_only_changed->disconnect();
459}
460
461// WalletModel::UnlockContext implementation
464 if (was_locked) {
465 // Request UI to unlock wallet
467 }
468 // If wallet is still locked, unlock was failed or cancelled, mark context
469 // as invalid
470 bool valid = getEncryptionStatus() != Locked;
471
472 return UnlockContext(this, valid, was_locked);
473}
474
478
480 if (valid && relock) {
481 wallet->setWalletLocked(true);
482 }
483}
484
486 // Transfer context; old object no longer relocks wallet
487 *this = rhs;
488 rhs.relock = false;
489}
490
492 std::vector<std::string> &vReceiveRequests) {
493 // receive request
494 vReceiveRequests = m_wallet->getDestValues("rr");
495}
496
498 const int64_t nId,
499 const std::string &sRequest) {
501
502 std::stringstream ss;
503 ss << nId;
504 // "rr" prefix = "receive request" in destdata
505 std::string key = "rr" + ss.str();
506
507 return sRequest.empty() ? m_wallet->eraseDestData(dest, key)
508 : m_wallet->addDestData(dest, key, sRequest);
509}
510
512 return !gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET);
513}
514
516 return QString::fromStdString(m_wallet->getWalletName());
517}
518
520 const QString name = getWalletName();
521 return name.isEmpty() ? "[" + tr("default wallet") + "]" : name;
522}
523
525 return m_node.walletClient().getWallets().size() > 1;
526}
527
529 return Params();
530}
531
static constexpr Amount SATOSHI
Definition amount.h:143
ArgsManager gArgs
Definition args.cpp:38
std::string EncodeCashAddr(const CTxDestination &dst, const CChainParams &params)
const CChainParams & Params()
Return the currently selected parameters.
Qt model of the address book in the core.
void updateEntry(const QString &address, const QString &label, bool isMine, const QString &purpose, int status)
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition args.cpp:556
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition chainparams.h:80
Coin Control Features.
Definition coincontrol.h:21
Double ended buffer combining vector and stream-like interfaces.
Definition streams.h:177
Model for Bitcoin network client.
Definition clientmodel.h:43
BlockHash getBestBlockHash() EXCLUSIVE_LOCKS_REQUIRED(!m_cached_tip_mutex)
Interface from Qt to configuration data structure for Bitcoin client.
Model for list of recently generated payment requests / bitcoincash: URIs.
UI model for the transaction table of a wallet.
UnlockContext(WalletModel *wallet, bool valid, bool relock)
void CopyFrom(UnlockContext &&rhs)
Interface to Bitcoin wallet from Qt view code.
Definition walletmodel.h:47
OptionsModel * optionsModel
bool validateAddress(const QString &address)
AddressTableModel * addressTableModel
void loadReceiveRequests(std::vector< std::string > &vReceiveRequests)
EncryptionStatus cachedEncryptionStatus
ClientModel * m_client_model
std::unique_ptr< interfaces::Handler > m_handler_watch_only_changed
BlockHash m_cached_last_update_tip
interfaces::Node & m_node
std::unique_ptr< interfaces::Handler > m_handler_transaction_changed
void startPollBalance()
void pollBalanceChanged()
Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so.
SendCoinsReturn sendCoins(WalletModelTransaction &transaction)
RecentRequestsTableModel * recentRequestsTableModel
TransactionTableModel * transactionTableModel
bool setWalletEncrypted(const SecureString &passphrase)
void notifyWatchonlyChanged(bool fHaveWatchonly)
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
BlockHash getLastBlockProcessed() const
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
void message(const QString &title, const QString &message, unsigned int style)
void coinsSent(interfaces::Wallet &wallet, SendCoinsRecipient recipient, QByteArray transaction)
void setClientModel(ClientModel *client_model)
const CChainParams & getChainParams() const
bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest)
void updateStatus()
AddressTableModel * getAddressTableModel()
OptionsModel * getOptionsModel()
std::unique_ptr< interfaces::Handler > m_handler_can_get_addrs_changed
std::unique_ptr< interfaces::Handler > m_handler_unload
SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const CCoinControl &coinControl)
EncryptionStatus getEncryptionStatus() const
interfaces::Wallet & wallet() const
RecentRequestsTableModel * getRecentRequestsTableModel()
std::unique_ptr< interfaces::Handler > m_handler_status_changed
interfaces::WalletBalances m_cached_balances
bool fForceCheckBalanceChanged
QString getDisplayName() const
void checkBalanceChanged(const interfaces::WalletBalances &new_balances)
bool isMultiwallet()
void unsubscribeFromCoreSignals()
void requireUnlock()
void updateTransaction()
New transaction, or transaction changed status.
void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status)
New, updated or removed address book entry.
QTimer * timer
bool fHaveWatchOnly
WalletModel(std::unique_ptr< interfaces::Wallet > wallet, ClientModel &client_model, const PlatformStyle *platformStyle, QObject *parent=nullptr)
void updateWatchOnlyFlag(bool fHaveWatchonly)
Watch-only added.
std::unique_ptr< interfaces::Handler > m_handler_address_book_changed
void encryptionStatusChanged()
std::unique_ptr< interfaces::Wallet > m_wallet
UnlockContext requestUnlock()
void balanceChanged(const interfaces::WalletBalances &balances)
static bool isWalletEnabled()
QString getWalletName() const
std::unique_ptr< interfaces::Handler > m_handler_show_progress
@ AmountWithFeeExceedsBalance
Definition walletmodel.h:63
@ TransactionCreationFailed
Definition walletmodel.h:66
@ AmountExceedsBalance
Definition walletmodel.h:62
@ PaymentRequestExpired
Definition walletmodel.h:68
void subscribeToCoreSignals()
TransactionTableModel * getTransactionTableModel()
Data model for a walletmodel transaction.
virtual WalletClient & walletClient()=0
Get wallet client.
virtual std::vector< std::unique_ptr< Wallet > > getWallets()=0
Return interfaces for accessing wallets (if any).
virtual void commitTransaction(CTransactionRef tx, WalletValueMap value_map, WalletOrderForm order_form)=0
Commit transaction.
static const int MODEL_UPDATE_DELAY
bool IsValidDestinationString(const std::string &str, const CChainParams &params)
Definition key_io.cpp:183
CTxDestination DecodeDestination(const std::string &addr, const CChainParams &params)
Definition key_io.cpp:174
bool error(const char *fmt, const Args &...args)
Definition logging.h:226
Definition init.h:28
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition rcu.h:259
NodeContext & m_node
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
const char * name
Definition rest.cpp:47
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition secure.h:55
@ SER_NETWORK
Definition serialize.h:152
static void ShowProgress(SplashScreen *splash, const std::string &title, int nProgress, bool resume_possible)
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition standard.cpp:240
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition standard.h:85
static constexpr Amount zero() noexcept
Definition amount.h:32
int64_t amount
Definition amount.h:21
A BlockHash is a unqiue identifier for a block.
Definition blockhash.h:13
A TxId is the identifier of a transaction.
Definition txid.h:14
Bilingual messages:
Definition translation.h:17
Collection of wallet balances.
Definition wallet.h:347
ChangeType
General change type (added, updated, removed).
assert(!tx.IsCoinBase())
static const int PROTOCOL_VERSION
network protocol versioning
Definition version.h:11
std::shared_ptr< CWallet > m_wallet
static const bool DEFAULT_DISABLE_WALLET
Definition wallet.h:106
static void NotifyUnload(WalletModel *walletModel)
static void NotifyWatchonlyChanged(WalletModel *walletmodel, bool fHaveWatchonly)
static void NotifyCanGetAddressesChanged(WalletModel *walletmodel)
static void NotifyAddressBookChanged(WalletModel *walletmodel, const CTxDestination &address, const std::string &label, bool isMine, const std::string &purpose, ChangeType status)
static void NotifyTransactionChanged(WalletModel *walletmodel, const TxId &hash, ChangeType status)
static void ShowProgress(WalletModel *walletmodel, const std::string &title, int nProgress)
static void NotifyKeyStoreStatusChanged(WalletModel *walletmodel)