Bitcoin ABC  0.24.7
P2P Digital Currency
walletmodel.h
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 #ifndef BITCOIN_QT_WALLETMODEL_H
6 #define BITCOIN_QT_WALLETMODEL_H
7 
8 #include <chainparams.h>
9 #include <interfaces/wallet.h>
10 
11 #if defined(HAVE_CONFIG_H)
12 #include <config/bitcoin-config.h>
13 #endif
14 
17 
18 #include <QObject>
19 
20 #include <memory>
21 #include <vector>
22 
23 class AddressTableModel;
24 class ClientModel;
25 class OptionsModel;
26 class PlatformStyle;
28 class SendCoinsRecipient;
31 
32 class CCoinControl;
33 class CKeyID;
34 class COutPoint;
35 class COutput;
36 class CPubKey;
37 
38 namespace interfaces {
39 class Node;
40 } // namespace interfaces
41 
42 QT_BEGIN_NAMESPACE
43 class QTimer;
44 QT_END_NAMESPACE
45 
47 class WalletModel : public QObject {
48  Q_OBJECT
49 
50 public:
51  explicit WalletModel(std::unique_ptr<interfaces::Wallet> wallet,
52  ClientModel &client_model,
53  const PlatformStyle *platformStyle,
54  QObject *parent = nullptr);
55  ~WalletModel();
56 
57  // Returned by sendCoins
58  enum StatusCode {
59  OK,
65  // Error returned when wallet is still locked
69  };
70 
72  // !wallet->IsCrypted()
74  // wallet->IsCrypted() && wallet->IsLocked()
76  // wallet->IsCrypted() && !wallet->IsLocked()
78  };
79 
84 
86 
87  // Check address for validity
88  bool validateAddress(const QString &address);
89 
90  // Return status record for SendCoins, contains error id + information
91  struct SendCoinsReturn {
93  QString _reasonCommitFailed = "")
94  : status(_status), reasonCommitFailed(_reasonCommitFailed) {}
97  };
98 
99  // prepare transaction for getting txfee before sending coins
101  const CCoinControl &coinControl);
102 
103  // Send coins to a list of recipients
105 
106  // Wallet encryption
107  bool setWalletEncrypted(bool encrypted, const SecureString &passphrase);
108  // Passphrase only needed when unlocking
109  bool setWalletLocked(bool locked,
110  const SecureString &passPhrase = SecureString());
111  bool changePassphrase(const SecureString &oldPass,
112  const SecureString &newPass);
113 
114  // RAI object for unlocking wallet, returned by requestUnlock()
116  public:
118  ~UnlockContext();
119 
120  bool isValid() const { return valid; }
121 
122  // Copy constructor is disabled.
123  UnlockContext(const UnlockContext &) = delete;
124  // Move operator and constructor transfer the context
125  UnlockContext(UnlockContext &&obj) { CopyFrom(std::move(obj)); }
127  CopyFrom(std::move(rhs));
128  return *this;
129  }
130 
131  private:
133  bool valid;
134  // mutable, as it can be set to false by copying
135  mutable bool relock;
136 
137  UnlockContext &operator=(const UnlockContext &) = default;
138  void CopyFrom(UnlockContext &&rhs);
139  };
140 
142 
143  void loadReceiveRequests(std::vector<std::string> &vReceiveRequests);
144  bool saveReceiveRequest(const std::string &sAddress, const int64_t nId,
145  const std::string &sRequest);
146 
147  static bool isWalletEnabled();
148 
149  interfaces::Node &node() const { return m_node; }
150  interfaces::Wallet &wallet() const { return *m_wallet; }
151  ClientModel &clientModel() const { return *m_client_model; }
152  void setClientModel(ClientModel *client_model);
153 
154  const CChainParams &getChainParams() const;
155 
156  QString getWalletName() const;
157  QString getDisplayName() const;
158 
159  bool isMultiwallet();
160 
162  return addressTableModel;
163  }
164 
166 
167 private:
168  std::unique_ptr<interfaces::Wallet> m_wallet;
169  std::unique_ptr<interfaces::Handler> m_handler_unload;
170  std::unique_ptr<interfaces::Handler> m_handler_status_changed;
171  std::unique_ptr<interfaces::Handler> m_handler_address_book_changed;
172  std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
173  std::unique_ptr<interfaces::Handler> m_handler_show_progress;
174  std::unique_ptr<interfaces::Handler> m_handler_watch_only_changed;
175  std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
178 
181 
182  // Wallet has an options model for wallet-specific options (transaction fee,
183  // for example)
185 
189 
190  // Cache some values to be able to detect changes
193  QTimer *timer;
194 
195  // Block hash denoting when the last balance update was done.
197 
198  void subscribeToCoreSignals();
200  void checkBalanceChanged(const interfaces::WalletBalances &new_balances);
201 
202 Q_SIGNALS:
203  // Signal that balance in wallet changed
204  void balanceChanged(const interfaces::WalletBalances &balances);
205 
206  // Encryption status of wallet changed
208 
209  // Signal emitted when wallet needs to be unlocked
210  // It is valid behaviour for listeners to keep the wallet locked after this
211  // signal; this means that the unlocking failed or was cancelled.
212  void requireUnlock();
213 
214  // Fired when a message should be reported to the user
215  void message(const QString &title, const QString &message,
216  unsigned int style);
217 
218  // Coins sent: from wallet, to recipient, in (serialized) transaction:
220  QByteArray transaction);
221 
222  // Show progress dialog e.g. for rescan
223  void showProgress(const QString &title, int nProgress);
224 
225  // Watch-only address added
226  void notifyWatchonlyChanged(bool fHaveWatchonly);
227 
228  // Signal that wallet is about to be removed
229  void unload();
230 
231  // Notify that there are now keys in the keypool
232  void canGetAddressesChanged();
233 
234 public Q_SLOTS:
235  /* Starts a timer to periodically update the balance */
236  void startPollBalance();
237 
238  /* Wallet status might have changed */
239  void updateStatus();
241  void updateTransaction();
243  void updateAddressBook(const QString &address, const QString &label,
244  bool isMine, const QString &purpose, int status);
246  void updateWatchOnlyFlag(bool fHaveWatchonly);
251  void pollBalanceChanged();
252 };
253 
254 #endif // BITCOIN_QT_WALLETMODEL_H
WalletModel::changePassphrase
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
Definition: walletmodel.cpp:359
WalletModel::recentRequestsTableModel
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:188
interfaces
Definition: dummywallet.cpp:12
WalletModel::updateStatus
void updateStatus()
Definition: walletmodel.cpp:63
WalletModel::getOptionsModel
OptionsModel * getOptionsModel()
Definition: walletmodel.cpp:315
WalletModel::unload
void unload()
WalletModel::EncryptionStatus
EncryptionStatus
Definition: walletmodel.h:71
WalletModel::UnlockContext::isValid
bool isValid() const
Definition: walletmodel.h:120
WalletModel::transactionTableModel
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:187
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:47
WalletModel::m_handler_status_changed
std::unique_ptr< interfaces::Handler > m_handler_status_changed
Definition: walletmodel.h:170
WalletModel::m_handler_show_progress
std::unique_ptr< interfaces::Handler > m_handler_show_progress
Definition: walletmodel.h:173
WalletModel::TransactionCreationFailed
@ TransactionCreationFailed
Definition: walletmodel.h:66
AddressTableModel
Qt model of the address book in the core.
Definition: addresstablemodel.h:24
WalletModel::isWalletEnabled
static bool isWalletEnabled()
Definition: walletmodel.cpp:514
WalletModel::getAddressTableModel
AddressTableModel * getAddressTableModel()
Definition: walletmodel.cpp:319
WalletModel::DuplicateAddress
@ DuplicateAddress
Definition: walletmodel.h:64
CCoinControl
Coin Control Features.
Definition: coincontrol.h:21
WalletModel::sendCoins
SendCoinsReturn sendCoins(WalletModelTransaction &transaction)
Definition: walletmodel.cpp:248
WalletModel::AmountWithFeeExceedsBalance
@ AmountWithFeeExceedsBalance
Definition: walletmodel.h:63
WalletModel::Unlocked
@ Unlocked
Definition: walletmodel.h:77
WalletModel::AbsurdFee
@ AbsurdFee
Definition: walletmodel.h:67
WalletModel::m_handler_watch_only_changed
std::unique_ptr< interfaces::Handler > m_handler_watch_only_changed
Definition: walletmodel.h:174
CChainParams
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:47
WalletModel::m_handler_transaction_changed
std::unique_ptr< interfaces::Handler > m_handler_transaction_changed
Definition: walletmodel.h:172
WalletModel::isMultiwallet
bool isMultiwallet()
Definition: walletmodel.cpp:527
WalletModel::m_wallet
std::unique_ptr< interfaces::Wallet > m_wallet
Definition: walletmodel.h:168
WalletModel::canGetAddressesChanged
void canGetAddressesChanged()
WalletModel::getTransactionTableModel
TransactionTableModel * getTransactionTableModel()
Definition: walletmodel.cpp:323
WalletModel::~WalletModel
~WalletModel()
Definition: walletmodel.cpp:46
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
chainparams.h
WalletModel::coinsSent
void coinsSent(interfaces::Wallet &wallet, SendCoinsRecipient recipient, QByteArray transaction)
WalletModel::requestUnlock
UnlockContext requestUnlock()
Definition: walletmodel.cpp:465
WalletModel::prepareTransaction
SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const CCoinControl &coinControl)
Definition: walletmodel.cpp:133
WalletModel::setClientModel
void setClientModel(ClientModel *client_model)
Definition: walletmodel.cpp:56
WalletModel::node
interfaces::Node & node() const
Definition: walletmodel.h:149
WalletModel::m_handler_can_get_addrs_changed
std::unique_ptr< interfaces::Handler > m_handler_can_get_addrs_changed
Definition: walletmodel.h:175
WalletModel::addressTableModel
AddressTableModel * addressTableModel
Definition: walletmodel.h:186
SendCoinsRecipient
Definition: sendcoinsrecipient.h:23
OptionsModel
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:49
SecureString
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:56
WalletModel::wallet
interfaces::Wallet & wallet() const
Definition: walletmodel.h:150
interfaces::WalletBalances
Collection of wallet balances.
Definition: wallet.h:351
WalletModel::showProgress
void showProgress(const QString &title, int nProgress)
WalletModel::AmountExceedsBalance
@ AmountExceedsBalance
Definition: walletmodel.h:62
WalletModel::m_handler_address_book_changed
std::unique_ptr< interfaces::Handler > m_handler_address_book_changed
Definition: walletmodel.h:171
WalletModel::encryptionStatusChanged
void encryptionStatusChanged()
WalletModel::unsubscribeFromCoreSignals
void unsubscribeFromCoreSignals()
Definition: walletmodel.cpp:453
WalletModel::m_cached_balances
interfaces::WalletBalances m_cached_balances
Definition: walletmodel.h:191
WalletModel::validateAddress
bool validateAddress(const QString &address)
Definition: walletmodel.cpp:128
WalletModel::updateWatchOnlyFlag
void updateWatchOnlyFlag(bool fHaveWatchonly)
Watch-only added.
Definition: walletmodel.cpp:123
WalletModel::saveReceiveRequest
bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest)
Definition: walletmodel.cpp:500
WalletModel::SendCoinsReturn::status
StatusCode status
Definition: walletmodel.h:95
WalletModel::message
void message(const QString &title, const QString &message, unsigned int style)
RecentRequestsTableModel
Model for list of recently generated payment requests / bitcoincash: URIs.
Definition: recentrequeststablemodel.h:51
WalletModel::getChainParams
const CChainParams & getChainParams() const
Definition: walletmodel.cpp:531
WalletModel::SendCoinsReturn::SendCoinsReturn
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:92
WalletModel::getEncryptionStatus
EncryptionStatus getEncryptionStatus() const
Definition: walletmodel.cpp:331
WalletModel::StatusCode
StatusCode
Definition: walletmodel.h:58
WalletModel::Unencrypted
@ Unencrypted
Definition: walletmodel.h:73
WalletModel::loadReceiveRequests
void loadReceiveRequests(std::vector< std::string > &vReceiveRequests)
Definition: walletmodel.cpp:494
WalletModel::getWalletName
QString getWalletName() const
Definition: walletmodel.cpp:518
WalletModel::UnlockContext::valid
bool valid
Definition: walletmodel.h:133
WalletModel::UnlockContext::operator=
UnlockContext & operator=(UnlockContext &&rhs)
Definition: walletmodel.h:126
WalletModel::checkBalanceChanged
void checkBalanceChanged(const interfaces::WalletBalances &new_balances)
Definition: walletmodel.cpp:102
WalletModel::getRecentRequestsTableModel
RecentRequestsTableModel * getRecentRequestsTableModel()
Definition: walletmodel.cpp:327
WalletModel::Locked
@ Locked
Definition: walletmodel.h:75
interfaces::Wallet
Interface for accessing a wallet.
Definition: wallet.h:59
interfaces::Node
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:55
BlockHash
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
secure.h
WalletModel::getAddressTableModel
AddressTableModel * getAddressTableModel() const
Definition: walletmodel.h:161
ClientModel
Model for Bitcoin network client.
Definition: clientmodel.h:36
WalletModel::m_handler_unload
std::unique_ptr< interfaces::Handler > m_handler_unload
Definition: walletmodel.h:169
TransactionTableModel
UI model for the transaction table of a wallet.
Definition: transactiontablemodel.h:27
CPubKey
An encapsulated public key.
Definition: pubkey.h:31
WalletModel::OK
@ OK
Definition: walletmodel.h:59
WalletModel::getLastBlockProcessed
BlockHash getLastBlockProcessed() const
Definition: walletmodel.cpp:535
WalletModel::setWalletEncrypted
bool setWalletEncrypted(bool encrypted, const SecureString &passphrase)
Definition: walletmodel.cpp:341
WalletModel::optionsModel
OptionsModel * optionsModel
Definition: walletmodel.h:184
WalletModel::updateTransaction
void updateTransaction()
New transaction, or transaction changed status.
Definition: walletmodel.cpp:110
WalletModel::UnlockContext
Definition: walletmodel.h:115
WalletModel::InvalidAmount
@ InvalidAmount
Definition: walletmodel.h:60
WalletModel::UnlockContext::UnlockContext
UnlockContext(WalletModel *wallet, bool valid, bool relock)
Definition: walletmodel.cpp:478
walletmodeltransaction.h
WalletModel::timer
QTimer * timer
Definition: walletmodel.h:193
WalletModel::InvalidAddress
@ InvalidAddress
Definition: walletmodel.h:61
WalletModel::fForceCheckBalanceChanged
bool fForceCheckBalanceChanged
Definition: walletmodel.h:180
WalletModel::fHaveWatchOnly
bool fHaveWatchOnly
Definition: walletmodel.h:179
WalletModel::requireUnlock
void requireUnlock()
WalletModel::SendCoinsReturn
Definition: walletmodel.h:91
WalletModel::m_client_model
ClientModel * m_client_model
Definition: walletmodel.h:176
WalletModel::updateAddressBook
void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status)
New, updated or removed address book entry.
Definition: walletmodel.cpp:115
WalletModel::SendCoinsReturn::reasonCommitFailed
QString reasonCommitFailed
Definition: walletmodel.h:96
wallet.h
WalletModel::m_node
interfaces::Node & m_node
Definition: walletmodel.h:177
WalletModel::subscribeToCoreSignals
void subscribeToCoreSignals()
Definition: walletmodel.cpp:433
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
WalletModel::UnlockContext::wallet
WalletModel * wallet
Definition: walletmodel.h:132
WalletModel::startPollBalance
void startPollBalance()
Definition: walletmodel.cpp:50
WalletModel::PaymentRequestExpired
@ PaymentRequestExpired
Definition: walletmodel.h:68
WalletModel::getDisplayName
QString getDisplayName() const
Definition: walletmodel.cpp:522
WalletModel::WalletModel
WalletModel(std::unique_ptr< interfaces::Wallet > wallet, ClientModel &client_model, const PlatformStyle *platformStyle, QObject *parent=nullptr)
Definition: walletmodel.cpp:30
WalletModel::UnlockContext::CopyFrom
void CopyFrom(UnlockContext &&rhs)
Definition: walletmodel.cpp:488
WalletModel::balanceChanged
void balanceChanged(const interfaces::WalletBalances &balances)
PlatformStyle
Definition: platformstyle.h:13
WalletModel::pollBalanceChanged
void pollBalanceChanged()
Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so.
Definition: walletmodel.cpp:71
COutput
Definition: wallet.h:597
WalletModel::clientModel
ClientModel & clientModel() const
Definition: walletmodel.h:151
WalletModelTransaction
Data model for a walletmodel transaction.
Definition: walletmodeltransaction.h:22
WalletModel::notifyWatchonlyChanged
void notifyWatchonlyChanged(bool fHaveWatchonly)
WalletModel::cachedEncryptionStatus
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:192
WalletModel::UnlockContext::UnlockContext
UnlockContext(UnlockContext &&obj)
Definition: walletmodel.h:125
WalletModel::setWalletLocked
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
Definition: walletmodel.cpp:349
WalletModel::UnlockContext::~UnlockContext
~UnlockContext()
Definition: walletmodel.cpp:482
WalletModel::UnlockContext::relock
bool relock
Definition: walletmodel.h:135
WalletModel::m_cached_last_update_tip
BlockHash m_cached_last_update_tip
Definition: walletmodel.h:196