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