Bitcoin Core  22.99.0
P2P Digital Currency
walletmodel.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2021 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 CKeyID;
36 class COutPoint;
37 class CPubKey;
38 class uint256;
39 
40 namespace interfaces {
41 class Node;
42 } // namespace interfaces
43 namespace wallet {
44 class CCoinControl;
45 } // namespace wallet
46 
47 QT_BEGIN_NAMESPACE
48 class QTimer;
49 QT_END_NAMESPACE
50 
52 class WalletModel : public QObject
53 {
54  Q_OBJECT
55 
56 public:
57  explicit WalletModel(std::unique_ptr<interfaces::Wallet> wallet, ClientModel& client_model, const PlatformStyle *platformStyle, QObject *parent = nullptr);
58  ~WalletModel();
59 
60  enum StatusCode // Returned by sendCoins
61  {
62  OK,
68  TransactionCreationFailed, // Error returned when wallet is still locked
71  };
72 
74  {
75  Unencrypted, // !wallet->IsCrypted()
76  Locked, // wallet->IsCrypted() && wallet->IsLocked()
77  Unlocked // 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
92  {
93  SendCoinsReturn(StatusCode _status = OK, QString _reasonCommitFailed = "")
94  : status(_status),
95  reasonCommitFailed(_reasonCommitFailed)
96  {
97  }
100  };
101 
102  // prepare transaction for getting txfee before sending coins
104 
105  // Send coins to a list of recipients
107 
108  // Wallet encryption
109  bool setWalletEncrypted(const SecureString& passphrase);
110  // Passphrase only needed when unlocking
111  bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
112  bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
113 
114  // RAI object for unlocking wallet, returned by requestUnlock()
116  {
117  public:
119  ~UnlockContext();
120 
121  bool isValid() const { return valid; }
122 
123  // Copy constructor is disabled.
124  UnlockContext(const UnlockContext&) = delete;
125  // Move operator and constructor transfer the context
126  UnlockContext(UnlockContext&& obj) { CopyFrom(std::move(obj)); }
127  UnlockContext& operator=(UnlockContext&& rhs) { CopyFrom(std::move(rhs)); return *this; }
128  private:
130  bool valid;
131  mutable bool relock; // mutable, as it can be set to false by copying
132 
133  UnlockContext& operator=(const UnlockContext&) = default;
134  void CopyFrom(UnlockContext&& rhs);
135  };
136 
138 
139  bool bumpFee(uint256 hash, uint256& new_hash);
140  bool displayAddress(std::string sAddress);
141 
142  static bool isWalletEnabled();
143 
144  interfaces::Node& node() const { return m_node; }
145  interfaces::Wallet& wallet() const { return *m_wallet; }
146  ClientModel& clientModel() const { return *m_client_model; }
147  void setClientModel(ClientModel* client_model);
148 
149  QString getWalletName() const;
150  QString getDisplayName() const;
151 
152  bool isMultiwallet();
153 
155 
156  void refresh(bool pk_hash_only = false);
157 
159 
160 private:
161  std::unique_ptr<interfaces::Wallet> m_wallet;
162  std::unique_ptr<interfaces::Handler> m_handler_unload;
163  std::unique_ptr<interfaces::Handler> m_handler_status_changed;
164  std::unique_ptr<interfaces::Handler> m_handler_address_book_changed;
165  std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
166  std::unique_ptr<interfaces::Handler> m_handler_show_progress;
167  std::unique_ptr<interfaces::Handler> m_handler_watch_only_changed;
168  std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
171 
174 
175  // Wallet has an options model for wallet-specific options
176  // (transaction fee, for example)
178 
182 
183  // Cache some values to be able to detect changes
186  QTimer* timer;
187 
188  // Block hash denoting when the last balance update was done.
190 
191  void subscribeToCoreSignals();
193  void checkBalanceChanged(const interfaces::WalletBalances& new_balances);
194 
195 Q_SIGNALS:
196  // Signal that balance in wallet changed
197  void balanceChanged(const interfaces::WalletBalances& balances);
198 
199  // Encryption status of wallet changed
201 
202  // Signal emitted when wallet needs to be unlocked
203  // It is valid behaviour for listeners to keep the wallet locked after this signal;
204  // this means that the unlocking failed or was cancelled.
205  void requireUnlock();
206 
207  // Fired when a message should be reported to the user
208  void message(const QString &title, const QString &message, unsigned int style);
209 
210  // Coins sent: from wallet, to recipient, in (serialized) transaction:
211  void coinsSent(WalletModel* wallet, SendCoinsRecipient recipient, QByteArray transaction);
212 
213  // Show progress dialog e.g. for rescan
214  void showProgress(const QString &title, int nProgress);
215 
216  // Watch-only address added
217  void notifyWatchonlyChanged(bool fHaveWatchonly);
218 
219  // Signal that wallet is about to be removed
220  void unload();
221 
222  // Notify that there are now keys in the keypool
223  void canGetAddressesChanged();
224 
225  void timerTimeout();
226 
227 public Q_SLOTS:
228  /* Starts a timer to periodically update the balance */
229  void startPollBalance();
230 
231  /* Wallet status might have changed */
232  void updateStatus();
233  /* New transaction, or transaction changed status */
234  void updateTransaction();
235  /* New, updated or removed address book entry */
236  void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
237  /* Watch-only added */
238  void updateWatchOnlyFlag(bool fHaveWatchonly);
239  /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
240  void pollBalanceChanged();
241 };
242 
243 #endif // BITCOIN_QT_WALLETMODEL_H
WalletModel::changePassphrase
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
Definition: walletmodel.cpp:342
WalletModel::recentRequestsTableModel
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:181
interfaces
Definition: dummywallet.cpp:10
WalletModel::updateStatus
void updateStatus()
Definition: walletmodel.cpp:84
WalletModel::getOptionsModel
OptionsModel * getOptionsModel()
Definition: walletmodel.cpp:287
WalletModel::refresh
void refresh(bool pk_hash_only=false)
Definition: walletmodel.cpp:593
WalletModel::bumpFee
bool bumpFee(uint256 hash, uint256 &new_hash)
Definition: walletmodel.cpp:472
OutputType
OutputType
Definition: outputtype.h:18
WalletModel::unload
void unload()
WalletModel::getLastBlockProcessed
uint256 getLastBlockProcessed() const
Definition: walletmodel.cpp:598
WalletModel::EncryptionStatus
EncryptionStatus
Definition: walletmodel.h:73
WalletModel::UnlockContext::isValid
bool isValid() const
Definition: walletmodel.h:121
WalletModel::transactionTableModel
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:180
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:52
WalletModel::m_handler_status_changed
std::unique_ptr< interfaces::Handler > m_handler_status_changed
Definition: walletmodel.h:163
WalletModel::m_handler_show_progress
std::unique_ptr< interfaces::Handler > m_handler_show_progress
Definition: walletmodel.h:166
WalletModel::TransactionCreationFailed
@ TransactionCreationFailed
Definition: walletmodel.h:68
AddressTableModel
Qt model of the address book in the core.
Definition: addresstablemodel.h:23
WalletModel::isWalletEnabled
static bool isWalletEnabled()
Definition: walletmodel.cpp:572
WalletModel::getAddressTableModel
AddressTableModel * getAddressTableModel()
Definition: walletmodel.cpp:292
WalletModel::DuplicateAddress
@ DuplicateAddress
Definition: walletmodel.h:67
WalletModel::sendCoins
SendCoinsReturn sendCoins(WalletModelTransaction &transaction)
Definition: walletmodel.cpp:237
WalletModel::AmountWithFeeExceedsBalance
@ AmountWithFeeExceedsBalance
Definition: walletmodel.h:66
WalletModel::Unlocked
@ Unlocked
Definition: walletmodel.h:77
WalletModel::AbsurdFee
@ AbsurdFee
Definition: walletmodel.h:69
WalletModel::m_handler_watch_only_changed
std::unique_ptr< interfaces::Handler > m_handler_watch_only_changed
Definition: walletmodel.h:167
WalletModel::m_handler_transaction_changed
std::unique_ptr< interfaces::Handler > m_handler_transaction_changed
Definition: walletmodel.h:165
WalletModel::isMultiwallet
bool isMultiwallet()
Definition: walletmodel.cpp:588
WalletModel::m_wallet
std::unique_ptr< interfaces::Wallet > m_wallet
Definition: walletmodel.h:161
WalletModel::prepareTransaction
SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const wallet::CCoinControl &coinControl)
Definition: walletmodel.cpp:153
WalletModel::canGetAddressesChanged
void canGetAddressesChanged()
WalletModel::getTransactionTableModel
TransactionTableModel * getTransactionTableModel()
Definition: walletmodel.cpp:297
wallet
Definition: node.h:38
WalletModel::~WalletModel
~WalletModel()
Definition: walletmodel.cpp:63
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:23
WalletModel::m_cached_last_update_tip
uint256 m_cached_last_update_tip
Definition: walletmodel.h:189
bitcoin-config.h
WalletModel::requestUnlock
UnlockContext requestUnlock()
Definition: walletmodel.cpp:436
WalletModel::setClientModel
void setClientModel(ClientModel *client_model)
Definition: walletmodel.cpp:78
WalletModel::node
interfaces::Node & node() const
Definition: walletmodel.h:144
WalletModel::m_handler_can_get_addrs_changed
std::unique_ptr< interfaces::Handler > m_handler_can_get_addrs_changed
Definition: walletmodel.h:168
WalletModel::addressTableModel
AddressTableModel * addressTableModel
Definition: walletmodel.h:179
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:59
WalletModel::wallet
interfaces::Wallet & wallet() const
Definition: walletmodel.h:145
interfaces::WalletBalances
Collection of wallet balances.
Definition: wallet.h:365
WalletModel::showProgress
void showProgress(const QString &title, int nProgress)
WalletModel::AmountExceedsBalance
@ AmountExceedsBalance
Definition: walletmodel.h:65
WalletModel::m_handler_address_book_changed
std::unique_ptr< interfaces::Handler > m_handler_address_book_changed
Definition: walletmodel.h:164
WalletModel::encryptionStatusChanged
void encryptionStatusChanged()
WalletModel::unsubscribeFromCoreSignals
void unsubscribeFromCoreSignals()
Definition: walletmodel.cpp:423
WalletModel::m_cached_balances
interfaces::WalletBalances m_cached_balances
Definition: walletmodel.h:184
WalletModel::validateAddress
bool validateAddress(const QString &address)
Definition: walletmodel.cpp:148
WalletModel::updateWatchOnlyFlag
void updateWatchOnlyFlag(bool fHaveWatchonly)
Definition: walletmodel.cpp:142
WalletModel::SendCoinsReturn::status
StatusCode status
Definition: walletmodel.h:98
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:323
WalletModel::SendCoinsReturn::SendCoinsReturn
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:93
WalletModel::getEncryptionStatus
EncryptionStatus getEncryptionStatus() const
Definition: walletmodel.cpp:307
WalletModel::StatusCode
StatusCode
Definition: walletmodel.h:60
WalletModel::Unencrypted
@ Unencrypted
Definition: walletmodel.h:75
standard.h
WalletModel::getWalletName
QString getWalletName() const
Definition: walletmodel.cpp:577
WalletModel::coinsSent
void coinsSent(WalletModel *wallet, SendCoinsRecipient recipient, QByteArray transaction)
WalletModel::UnlockContext::valid
bool valid
Definition: walletmodel.h:130
uint256
256-bit opaque blob.
Definition: uint256.h:124
WalletModel::UnlockContext::operator=
UnlockContext & operator=(UnlockContext &&rhs)
Definition: walletmodel.h:127
WalletModel::checkBalanceChanged
void checkBalanceChanged(const interfaces::WalletBalances &new_balances)
Definition: walletmodel.cpp:121
WalletModel::getRecentRequestsTableModel
RecentRequestsTableModel * getRecentRequestsTableModel()
Definition: walletmodel.cpp:302
WalletModel::Locked
@ Locked
Definition: walletmodel.h:76
interfaces::Wallet
Interface for accessing a wallet.
Definition: wallet.h:56
interfaces::Node
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:68
secure.h
WalletModel::getAddressTableModel
AddressTableModel * getAddressTableModel() const
Definition: walletmodel.h:154
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:162
TransactionTableModel
UI model for the transaction table of a wallet.
Definition: transactiontablemodel.h:26
CPubKey
An encapsulated public key.
Definition: pubkey.h:33
WalletModel::OK
@ OK
Definition: walletmodel.h:62
WalletModel::optionsModel
OptionsModel * optionsModel
Definition: walletmodel.h:177
WalletModel::updateTransaction
void updateTransaction()
Definition: walletmodel.cpp:129
WalletModel::UnlockContext
Definition: walletmodel.h:115
WalletModel::InvalidAmount
@ InvalidAmount
Definition: walletmodel.h:63
WalletModel::UnlockContext::UnlockContext
UnlockContext(WalletModel *wallet, bool valid, bool relock)
Definition: walletmodel.cpp:450
walletmodeltransaction.h
WalletModel::timerTimeout
void timerTimeout()
WalletModel::timer
QTimer * timer
Definition: walletmodel.h:186
WalletModel::InvalidAddress
@ InvalidAddress
Definition: walletmodel.h:64
WalletModel::fForceCheckBalanceChanged
bool fForceCheckBalanceChanged
Definition: walletmodel.h:173
WalletModel::fHaveWatchOnly
bool fHaveWatchOnly
Definition: walletmodel.h:172
WalletModel::requireUnlock
void requireUnlock()
WalletModel::SendCoinsReturn
Definition: walletmodel.h:91
WalletModel::m_client_model
ClientModel * m_client_model
Definition: walletmodel.h:169
WalletModel::updateAddressBook
void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status)
Definition: walletmodel.cpp:135
WalletModel::SendCoinsReturn::reasonCommitFailed
QString reasonCommitFailed
Definition: walletmodel.h:99
wallet.h
WalletModel::m_node
interfaces::Node & m_node
Definition: walletmodel.h:170
WalletModel::subscribeToCoreSignals
void subscribeToCoreSignals()
Definition: walletmodel.cpp:411
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:129
WalletModel::startPollBalance
void startPollBalance()
Definition: walletmodel.cpp:68
WalletModel::PaymentRequestExpired
@ PaymentRequestExpired
Definition: walletmodel.h:70
WalletModel::getDisplayName
QString getDisplayName() const
Definition: walletmodel.cpp:582
WalletModel::WalletModel
WalletModel(std::unique_ptr< interfaces::Wallet > wallet, ClientModel &client_model, const PlatformStyle *platformStyle, QObject *parent=nullptr)
Definition: walletmodel.cpp:43
WalletModel::UnlockContext::CopyFrom
void CopyFrom(UnlockContext &&rhs)
Definition: walletmodel.cpp:465
WalletModel::balanceChanged
void balanceChanged(const interfaces::WalletBalances &balances)
PlatformStyle
Definition: platformstyle.h:13
WalletModel::pollBalanceChanged
void pollBalanceChanged()
Definition: walletmodel.cpp:93
WalletModel::clientModel
ClientModel & clientModel() const
Definition: walletmodel.h:146
WalletModel::displayAddress
bool displayAddress(std::string sAddress)
Definition: walletmodel.cpp:560
wallet::CCoinControl
Coin Control Features.
Definition: coincontrol.h:29
WalletModelTransaction
Data model for a walletmodel transaction.
Definition: walletmodeltransaction.h:22
WalletModel::notifyWatchonlyChanged
void notifyWatchonlyChanged(bool fHaveWatchonly)
WalletModel::cachedEncryptionStatus
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:185
WalletModel::UnlockContext::UnlockContext
UnlockContext(UnlockContext &&obj)
Definition: walletmodel.h:126
WalletModel::setWalletLocked
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
Definition: walletmodel.cpp:328
WalletModel::UnlockContext::~UnlockContext
~UnlockContext()
Definition: walletmodel.cpp:457
WalletModel::UnlockContext::relock
bool relock
Definition: walletmodel.h:131