Bitcoin Core  26.1.0
P2P Digital Currency
walletmodel.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2022 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 
15 
16 #include <interfaces/wallet.h>
18 
19 #include <vector>
20 
21 #include <QObject>
22 
23 enum class OutputType;
24 
25 class AddressTableModel;
26 class ClientModel;
27 class OptionsModel;
28 class PlatformStyle;
30 class SendCoinsRecipient;
33 
34 class CKeyID;
35 class COutPoint;
36 class CPubKey;
37 class uint256;
38 
39 namespace interfaces {
40 class Node;
41 } // namespace interfaces
42 namespace wallet {
43 class CCoinControl;
44 } // namespace wallet
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
69  };
70 
72  {
73  NoKeys, // wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)
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) const;
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
103 
104  // Send coins to a list of recipients
105  void sendCoins(WalletModelTransaction& transaction);
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  // RAII object for unlocking wallet, returned by requestUnlock()
115  {
116  public:
118  ~UnlockContext();
119 
120  bool isValid() const { return valid; }
121 
122  // Disable unused copy/move constructors/assignments explicitly.
123  UnlockContext(const UnlockContext&) = delete;
124  UnlockContext(UnlockContext&&) = delete;
125  UnlockContext& operator=(const UnlockContext&) = delete;
127 
128  private:
130  const bool valid;
131  const bool relock;
132  };
133 
135 
136  bool bumpFee(uint256 hash, uint256& new_hash);
137  bool displayAddress(std::string sAddress) const;
138 
139  static bool isWalletEnabled();
140 
141  interfaces::Node& node() const { return m_node; }
142  interfaces::Wallet& wallet() const { return *m_wallet; }
143  ClientModel& clientModel() const { return *m_client_model; }
144  void setClientModel(ClientModel* client_model);
145 
146  QString getWalletName() const;
147  QString getDisplayName() const;
148 
149  bool isMultiwallet() const;
150 
151  void refresh(bool pk_hash_only = false);
152 
154 
155  // Retrieve the cached wallet balance
157 
158  // If coin control has selected outputs, searches the total amount inside the wallet.
159  // Otherwise, uses the wallet's cached available balance.
161 
162 private:
163  std::unique_ptr<interfaces::Wallet> m_wallet;
164  std::unique_ptr<interfaces::Handler> m_handler_unload;
165  std::unique_ptr<interfaces::Handler> m_handler_status_changed;
166  std::unique_ptr<interfaces::Handler> m_handler_address_book_changed;
167  std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
168  std::unique_ptr<interfaces::Handler> m_handler_show_progress;
169  std::unique_ptr<interfaces::Handler> m_handler_watch_only_changed;
170  std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
173 
176 
177  // Wallet has an options model for wallet-specific options
178  // (transaction fee, for example)
180 
184 
185  // Cache some values to be able to detect changes
188  QTimer* timer;
189 
190  // Block hash denoting when the last balance update was done.
192 
193  void subscribeToCoreSignals();
195  void checkBalanceChanged(const interfaces::WalletBalances& new_balances);
196 
197 Q_SIGNALS:
198  // Signal that balance in wallet changed
199  void balanceChanged(const interfaces::WalletBalances& balances);
200 
201  // Encryption status of wallet changed
203 
204  // Signal emitted when wallet needs to be unlocked
205  // It is valid behaviour for listeners to keep the wallet locked after this signal;
206  // this means that the unlocking failed or was cancelled.
207  void requireUnlock();
208 
209  // Fired when a message should be reported to the user
210  void message(const QString &title, const QString &message, unsigned int style);
211 
212  // Coins sent: from wallet, to recipient, in (serialized) transaction:
213  void coinsSent(WalletModel* wallet, SendCoinsRecipient recipient, QByteArray transaction);
214 
215  // Show progress dialog e.g. for rescan
216  void showProgress(const QString &title, int nProgress);
217 
218  // Watch-only address added
219  void notifyWatchonlyChanged(bool fHaveWatchonly);
220 
221  // Signal that wallet is about to be removed
222  void unload();
223 
224  // Notify that there are now keys in the keypool
225  void canGetAddressesChanged();
226 
227  void timerTimeout();
228 
229 public Q_SLOTS:
230  /* Starts a timer to periodically update the balance */
231  void startPollBalance();
232 
233  /* Wallet status might have changed */
234  void updateStatus();
235  /* New transaction, or transaction changed status */
236  void updateTransaction();
237  /* New, updated or removed address book entry */
238  void updateAddressBook(const QString &address, const QString &label, bool isMine, wallet::AddressPurpose purpose, int status);
239  /* Watch-only added */
240  void updateWatchOnlyFlag(bool fHaveWatchonly);
241  /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
242  void pollBalanceChanged();
243 };
244 
245 #endif // BITCOIN_QT_WALLETMODEL_H
Model for list of recently generated payment requests / bitcoin: URIs.
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:182
OptionsModel * getOptionsModel() const
interfaces::Wallet & wallet() const
Definition: walletmodel.h:142
void coinsSent(WalletModel *wallet, SendCoinsRecipient recipient, QByteArray transaction)
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:183
std::unique_ptr< interfaces::Handler > m_handler_address_book_changed
Definition: walletmodel.h:166
SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const wallet::CCoinControl &coinControl)
static bool isWalletEnabled()
void canGetAddressesChanged()
void startPollBalance()
Definition: walletmodel.cpp:64
UnlockContext requestUnlock()
std::unique_ptr< interfaces::Handler > m_handler_unload
Definition: walletmodel.h:164
void unsubscribeFromCoreSignals()
interfaces::WalletBalances getCachedBalance() const
TransactionTableModel * getTransactionTableModel() const
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:58
uint256 m_cached_last_update_tip
Definition: walletmodel.h:191
bool validateAddress(const QString &address) const
void showProgress(const QString &title, int nProgress)
std::unique_ptr< interfaces::Handler > m_handler_status_changed
Definition: walletmodel.h:165
OutputType
Definition: outputtype.h:17
void updateStatus()
Definition: walletmodel.cpp:84
EncryptionStatus getEncryptionStatus() const
bool bumpFee(uint256 hash, uint256 &new_hash)
UnlockContext(WalletModel *wallet, bool valid, bool relock)
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
AddressPurpose
Address purpose field that has been been stored with wallet sending and receiving addresses since BIP...
Definition: types.h:60
uint256 getLastBlockProcessed() const
RecentRequestsTableModel * getRecentRequestsTableModel() const
void setClientModel(ClientModel *client_model)
Definition: walletmodel.cpp:78
void updateTransaction()
Collection of wallet balances.
Definition: wallet.h:371
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
An encapsulated public key.
Definition: pubkey.h:33
Interface for accessing a wallet.
Definition: wallet.h:60
std::unique_ptr< interfaces::Wallet > m_wallet
Definition: walletmodel.h:163
bool setWalletEncrypted(const SecureString &passphrase)
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:92
void refresh(bool pk_hash_only=false)
void encryptionStatusChanged()
UnlockContext & operator=(const UnlockContext &)=delete
OptionsModel * optionsModel
Definition: walletmodel.h:179
QString getWalletName() const
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:187
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:35
interfaces::Node & node() const
Definition: walletmodel.h:141
UI model for the transaction table of a wallet.
Model for Bitcoin network client.
Definition: clientmodel.h:53
bool isMultiwallet() const
Qt model of the address book in the core.
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
std::unique_ptr< interfaces::Handler > m_handler_show_progress
Definition: walletmodel.h:168
ClientModel * m_client_model
Definition: walletmodel.h:171
void updateWatchOnlyFlag(bool fHaveWatchonly)
256-bit opaque blob.
Definition: uint256.h:106
QTimer * timer
Definition: walletmodel.h:188
bool fForceCheckBalanceChanged
Definition: walletmodel.h:175
std::unique_ptr< interfaces::Handler > m_handler_can_get_addrs_changed
Definition: walletmodel.h:170
void updateAddressBook(const QString &address, const QString &label, bool isMine, wallet::AddressPurpose purpose, int status)
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:40
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:51
interfaces::WalletBalances m_cached_balances
Definition: walletmodel.h:186
WalletModel(std::unique_ptr< interfaces::Wallet > wallet, ClientModel &client_model, const PlatformStyle *platformStyle, QObject *parent=nullptr)
Definition: walletmodel.cpp:43
interfaces::Node & m_node
Definition: walletmodel.h:172
void message(const QString &title, const QString &message, unsigned int style)
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:23
void notifyWatchonlyChanged(bool fHaveWatchonly)
Data model for a walletmodel transaction.
QString getDisplayName() const
AddressTableModel * getAddressTableModel() const
AddressTableModel * addressTableModel
Definition: walletmodel.h:181
CAmount getAvailableBalance(const wallet::CCoinControl *control)
void sendCoins(WalletModelTransaction &transaction)
std::unique_ptr< interfaces::Handler > m_handler_transaction_changed
Definition: walletmodel.h:167
bool fHaveWatchOnly
Definition: walletmodel.h:174
void checkBalanceChanged(const interfaces::WalletBalances &new_balances)
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:69
void balanceChanged(const interfaces::WalletBalances &balances)
Coin Control Features.
Definition: coincontrol.h:28
std::unique_ptr< interfaces::Handler > m_handler_watch_only_changed
Definition: walletmodel.h:169
void pollBalanceChanged()
Definition: walletmodel.cpp:93
ClientModel & clientModel() const
Definition: walletmodel.h:143
void subscribeToCoreSignals()
bool displayAddress(std::string sAddress) const