Bitcoin Core  24.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
69  AbsurdFee
70  };
71 
73  {
74  NoKeys, // wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)
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) const;
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
106  void sendCoins(WalletModelTransaction& transaction);
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 
134  void CopyFrom(UnlockContext&& rhs);
135  };
136 
138 
139  bool bumpFee(uint256 hash, uint256& new_hash);
140  bool displayAddress(std::string sAddress) const;
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() const;
153 
154  void refresh(bool pk_hash_only = false);
155 
157 
158  // Retrieve the cached wallet balance
160 
161  // If coin control has selected outputs, searches the total amount inside the wallet.
162  // Otherwise, uses the wallet's cached available balance.
164 
165 private:
166  std::unique_ptr<interfaces::Wallet> m_wallet;
167  std::unique_ptr<interfaces::Handler> m_handler_unload;
168  std::unique_ptr<interfaces::Handler> m_handler_status_changed;
169  std::unique_ptr<interfaces::Handler> m_handler_address_book_changed;
170  std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
171  std::unique_ptr<interfaces::Handler> m_handler_show_progress;
172  std::unique_ptr<interfaces::Handler> m_handler_watch_only_changed;
173  std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
176 
179 
180  // Wallet has an options model for wallet-specific options
181  // (transaction fee, for example)
183 
187 
188  // Cache some values to be able to detect changes
191  QTimer* timer;
192 
193  // Block hash denoting when the last balance update was done.
195 
196  void subscribeToCoreSignals();
198  void checkBalanceChanged(const interfaces::WalletBalances& new_balances);
199 
200 Q_SIGNALS:
201  // Signal that balance in wallet changed
203 
204  // Encryption status of wallet changed
206 
207  // Signal emitted when wallet needs to be unlocked
208  // It is valid behaviour for listeners to keep the wallet locked after this signal;
209  // this means that the unlocking failed or was cancelled.
211 
212  // Fired when a message should be reported to the user
213  void message(const QString &title, const QString &message, unsigned int style);
214 
215  // Coins sent: from wallet, to recipient, in (serialized) transaction:
216  void coinsSent(WalletModel* wallet, SendCoinsRecipient recipient, QByteArray transaction);
217 
218  // Show progress dialog e.g. for rescan
219  void showProgress(const QString &title, int nProgress);
220 
221  // Watch-only address added
222  void notifyWatchonlyChanged(bool fHaveWatchonly);
223 
224  // Signal that wallet is about to be removed
225  void unload();
226 
227  // Notify that there are now keys in the keypool
229 
230  void timerTimeout();
231 
232 public Q_SLOTS:
233  /* Starts a timer to periodically update the balance */
234  void startPollBalance();
235 
236  /* Wallet status might have changed */
237  void updateStatus();
238  /* New transaction, or transaction changed status */
239  void updateTransaction();
240  /* New, updated or removed address book entry */
241  void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
242  /* Watch-only added */
243  void updateWatchOnlyFlag(bool fHaveWatchonly);
244  /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
245  void pollBalanceChanged();
246 };
247 
248 #endif // BITCOIN_QT_WALLETMODEL_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Qt model of the address book in the core.
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:24
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:35
An encapsulated public key.
Definition: pubkey.h:34
Model for Bitcoin network client.
Definition: clientmodel.h:55
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:41
Model for list of recently generated payment requests / bitcoin: URIs.
UI model for the transaction table of a wallet.
UnlockContext(WalletModel *wallet, bool valid, bool relock)
UnlockContext & operator=(UnlockContext &&rhs)
Definition: walletmodel.h:127
UnlockContext & operator=(const UnlockContext &)=default
UnlockContext(const UnlockContext &)=delete
UnlockContext(UnlockContext &&obj)
Definition: walletmodel.h:126
void CopyFrom(UnlockContext &&rhs)
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:53
OptionsModel * optionsModel
Definition: walletmodel.h:182
AddressTableModel * addressTableModel
Definition: walletmodel.h:184
RecentRequestsTableModel * getRecentRequestsTableModel() const
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:190
void refresh(bool pk_hash_only=false)
uint256 m_cached_last_update_tip
Definition: walletmodel.h:194
ClientModel * m_client_model
Definition: walletmodel.h:174
std::unique_ptr< interfaces::Handler > m_handler_watch_only_changed
Definition: walletmodel.h:172
interfaces::Node & m_node
Definition: walletmodel.h:175
std::unique_ptr< interfaces::Handler > m_handler_transaction_changed
Definition: walletmodel.h:170
void startPollBalance()
Definition: walletmodel.cpp:68
void pollBalanceChanged()
Definition: walletmodel.cpp:97
AddressTableModel * getAddressTableModel() const
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:186
SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const wallet::CCoinControl &coinControl)
bool displayAddress(std::string sAddress) const
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:185
bool setWalletEncrypted(const SecureString &passphrase)
ClientModel & clientModel() const
Definition: walletmodel.h:146
void notifyWatchonlyChanged(bool fHaveWatchonly)
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
void showProgress(const QString &title, int nProgress)
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
void message(const QString &title, const QString &message, unsigned int style)
bool validateAddress(const QString &address) const
void sendCoins(WalletModelTransaction &transaction)
void setClientModel(ClientModel *client_model)
Definition: walletmodel.cpp:82
void updateStatus()
Definition: walletmodel.cpp:88
CAmount getAvailableBalance(const wallet::CCoinControl *control)
bool isMultiwallet() const
void timerTimeout()
std::unique_ptr< interfaces::Handler > m_handler_can_get_addrs_changed
Definition: walletmodel.h:173
std::unique_ptr< interfaces::Handler > m_handler_unload
Definition: walletmodel.h:167
interfaces::Wallet & wallet() const
Definition: walletmodel.h:145
EncryptionStatus getEncryptionStatus() const
TransactionTableModel * getTransactionTableModel() const
std::unique_ptr< interfaces::Handler > m_handler_status_changed
Definition: walletmodel.h:168
interfaces::WalletBalances m_cached_balances
Definition: walletmodel.h:189
bool fForceCheckBalanceChanged
Definition: walletmodel.h:178
void coinsSent(WalletModel *wallet, SendCoinsRecipient recipient, QByteArray transaction)
QString getDisplayName() const
OptionsModel * getOptionsModel() const
bool bumpFee(uint256 hash, uint256 &new_hash)
void checkBalanceChanged(const interfaces::WalletBalances &new_balances)
void unsubscribeFromCoreSignals()
void requireUnlock()
void updateTransaction()
void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status)
uint256 getLastBlockProcessed() const
void canGetAddressesChanged()
QTimer * timer
Definition: walletmodel.h:191
bool fHaveWatchOnly
Definition: walletmodel.h:177
WalletModel(std::unique_ptr< interfaces::Wallet > wallet, ClientModel &client_model, const PlatformStyle *platformStyle, QObject *parent=nullptr)
Definition: walletmodel.cpp:43
void updateWatchOnlyFlag(bool fHaveWatchonly)
std::unique_ptr< interfaces::Handler > m_handler_address_book_changed
Definition: walletmodel.h:169
void encryptionStatusChanged()
interfaces::Node & node() const
Definition: walletmodel.h:144
std::unique_ptr< interfaces::Wallet > m_wallet
Definition: walletmodel.h:166
UnlockContext requestUnlock()
void balanceChanged(const interfaces::WalletBalances &balances)
static bool isWalletEnabled()
interfaces::WalletBalances getCachedBalance() const
QString getWalletName() const
std::unique_ptr< interfaces::Handler > m_handler_show_progress
Definition: walletmodel.h:171
void unload()
@ AmountWithFeeExceedsBalance
Definition: walletmodel.h:66
@ TransactionCreationFailed
Definition: walletmodel.h:68
@ AmountExceedsBalance
Definition: walletmodel.h:65
@ DuplicateAddress
Definition: walletmodel.h:67
void subscribeToCoreSignals()
Data model for a walletmodel transaction.
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:70
Interface for accessing a wallet.
Definition: wallet.h:58
256-bit opaque blob.
Definition: uint256.h:119
Coin Control Features.
Definition: coincontrol.h:30
Definition: node.h:39
OutputType
Definition: outputtype.h:17
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:59
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:93
Collection of wallet balances.
Definition: wallet.h:366