Bitcoin Core  31.0.0
P2P Digital Currency
walletmodel.h
Go to the documentation of this file.
1 // Copyright (c) 2011-present 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 <key.h>
9 
11 
12 #include <interfaces/wallet.h>
15 
16 #include <vector>
17 
18 #include <QObject>
19 
20 enum class OutputType;
21 
22 class AddressTableModel;
23 class ClientModel;
24 class OptionsModel;
25 class PlatformStyle;
27 class SendCoinsRecipient;
30 
31 class CKeyID;
32 class COutPoint;
33 class CPubKey;
34 class uint256;
35 
36 namespace interfaces {
37 class Node;
38 } // namespace interfaces
39 namespace wallet {
40 class CCoinControl;
41 } // namespace wallet
42 
43 QT_BEGIN_NAMESPACE
44 class QTimer;
45 QT_END_NAMESPACE
46 
48 class WalletModel : public QObject
49 {
50  Q_OBJECT
51 
52 public:
53  explicit WalletModel(std::unique_ptr<interfaces::Wallet> wallet, ClientModel& client_model, const PlatformStyle *platformStyle, QObject *parent = nullptr);
54  ~WalletModel();
55 
56  enum StatusCode // Returned by sendCoins
57  {
58  OK,
63  TransactionCreationFailed, // Error returned when wallet is still locked
65  };
66 
68  {
69  NoKeys, // wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)
70  Unencrypted, // !wallet->HasEncryptionKeys()
71  Locked, // wallet->HasEncryptionKeys() && wallet->IsLocked()
72  Unlocked // wallet->HasEncryptionKeys() && !wallet->IsLocked()
73  };
74 
79 
81 
82  // Check address for validity
83  bool validateAddress(const QString& address) const;
84 
85  // Return status record for SendCoins, contains error id + information
87  {
88  SendCoinsReturn(StatusCode _status = OK, QString _reasonCommitFailed = "")
89  : status(_status),
90  reasonCommitFailed(_reasonCommitFailed)
91  {
92  }
95  };
96 
97  // prepare transaction for getting txfee before sending coins
99 
100  // Send coins to a list of recipients
101  void sendCoins(WalletModelTransaction& transaction);
102 
103  // Wallet encryption
104  bool setWalletEncrypted(const SecureString& passphrase);
105  // Passphrase only needed when unlocking
106  bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
107  bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
108 
109  // RAII object for unlocking wallet, returned by requestUnlock()
111  {
112  public:
114  ~UnlockContext();
115 
116  bool isValid() const { return valid; }
117 
118  // Disable unused copy/move constructors/assignments explicitly.
119  UnlockContext(const UnlockContext&) = delete;
120  UnlockContext(UnlockContext&&) = delete;
121  UnlockContext& operator=(const UnlockContext&) = delete;
123 
124  private:
126  const bool valid;
127  const bool relock;
128  };
129 
131 
132  bool bumpFee(Txid hash, Txid& new_hash);
133  void displayAddress(std::string sAddress) const;
134 
135  static bool isWalletEnabled();
136 
137  interfaces::Node& node() const { return m_node; }
138  interfaces::Wallet& wallet() const { return *m_wallet; }
139  ClientModel& clientModel() const { return *m_client_model; }
140  void setClientModel(ClientModel* client_model);
141 
142  QString getWalletName() const;
143  QString getDisplayName() const;
144 
145  bool isMultiwallet() const;
146 
147  void refresh(bool pk_hash_only = false);
148 
150 
151  // Retrieve the cached wallet balance
153 
154  // If coin control has selected outputs, searches the total amount inside the wallet.
155  // Otherwise, uses the wallet's cached available balance.
157 
158 private:
159  std::unique_ptr<interfaces::Wallet> m_wallet;
160  std::unique_ptr<interfaces::Handler> m_handler_unload;
161  std::unique_ptr<interfaces::Handler> m_handler_status_changed;
162  std::unique_ptr<interfaces::Handler> m_handler_address_book_changed;
163  std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
164  std::unique_ptr<interfaces::Handler> m_handler_show_progress;
165  std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
168 
170 
171  // Wallet has an options model for wallet-specific options
172  // (transaction fee, for example)
174 
178 
179  // Cache some values to be able to detect changes
182  QTimer* timer;
183 
184  // Block hash denoting when the last balance update was done.
186 
187  void subscribeToCoreSignals();
189  void checkBalanceChanged(const interfaces::WalletBalances& new_balances);
190 
191 Q_SIGNALS:
192  // Signal that balance in wallet changed
193  void balanceChanged(const interfaces::WalletBalances& balances);
194 
195  // Encryption status of wallet changed
197 
198  // Signal emitted when wallet needs to be unlocked
199  // It is valid behaviour for listeners to keep the wallet locked after this signal;
200  // this means that the unlocking failed or was cancelled.
201  void requireUnlock();
202 
203  // Fired when a message should be reported to the user
204  void message(const QString &title, const QString &message, unsigned int style);
205 
206  // Coins sent: from wallet, to recipient, in (serialized) transaction:
207  void coinsSent(WalletModel* wallet, SendCoinsRecipient recipient, QByteArray transaction);
208 
209  // Show progress dialog e.g. for rescan
210  void showProgress(const QString &title, int nProgress);
211 
212  // Signal that wallet is about to be removed
213  void unload();
214 
215  // Notify that there are now keys in the keypool
216  void canGetAddressesChanged();
217 
218  void timerTimeout();
219 
220 public Q_SLOTS:
221  /* Starts a timer to periodically update the balance */
222  void startPollBalance();
223 
224  /* Wallet status might have changed */
225  void updateStatus();
226  /* New transaction, or transaction changed status */
227  void updateTransaction();
228  /* New, updated or removed address book entry */
229  void updateAddressBook(const QString &address, const QString &label, bool isMine, wallet::AddressPurpose purpose, int status);
230  /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
231  void pollBalanceChanged();
232 };
233 
234 #endif // BITCOIN_QT_WALLETMODEL_H
Model for list of recently generated payment requests / bitcoin: URIs.
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:176
OptionsModel * getOptionsModel() const
interfaces::Wallet & wallet() const
Definition: walletmodel.h:138
void coinsSent(WalletModel *wallet, SendCoinsRecipient recipient, QByteArray transaction)
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:177
std::unique_ptr< interfaces::Handler > m_handler_address_book_changed
Definition: walletmodel.h:162
SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const wallet::CCoinControl &coinControl)
static bool isWalletEnabled()
void canGetAddressesChanged()
void startPollBalance()
Definition: walletmodel.cpp:63
UnlockContext requestUnlock()
std::unique_ptr< interfaces::Handler > m_handler_unload
Definition: walletmodel.h:160
void unsubscribeFromCoreSignals()
interfaces::WalletBalances getCachedBalance() const
void unload()
TransactionTableModel * getTransactionTableModel() const
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:53
uint256 m_cached_last_update_tip
Definition: walletmodel.h:185
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:161
OutputType
Definition: outputtype.h:18
void updateStatus()
Definition: walletmodel.cpp:83
EncryptionStatus getEncryptionStatus() const
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:28
uint256 getLastBlockProcessed() const
RecentRequestsTableModel * getRecentRequestsTableModel() const
void setClientModel(ClientModel *client_model)
Definition: walletmodel.cpp:77
void updateTransaction()
Collection of wallet balances.
Definition: wallet.h:367
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
An encapsulated public key.
Definition: pubkey.h:33
Interface for accessing a wallet.
Definition: wallet.h:65
std::unique_ptr< interfaces::Wallet > m_wallet
Definition: walletmodel.h:159
bool setWalletEncrypted(const SecureString &passphrase)
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:88
void refresh(bool pk_hash_only=false)
void encryptionStatusChanged()
UnlockContext & operator=(const UnlockContext &)=delete
OptionsModel * optionsModel
Definition: walletmodel.h:173
QString getWalletName() const
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:181
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:28
interfaces::Node & node() const
Definition: walletmodel.h:137
UI model for the transaction table of a wallet.
Model for Bitcoin network client.
Definition: clientmodel.h:56
bool isMultiwallet() const
Qt model of the address book in the core.
void displayAddress(std::string sAddress) const
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
std::unique_ptr< interfaces::Handler > m_handler_show_progress
Definition: walletmodel.h:164
ClientModel * m_client_model
Definition: walletmodel.h:166
256-bit opaque blob.
Definition: uint256.h:195
void requireUnlock()
QTimer * timer
Definition: walletmodel.h:182
bool fForceCheckBalanceChanged
Definition: walletmodel.h:169
std::unique_ptr< interfaces::Handler > m_handler_can_get_addrs_changed
Definition: walletmodel.h:165
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:42
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:48
interfaces::WalletBalances m_cached_balances
Definition: walletmodel.h:180
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:167
bool bumpFee(Txid hash, Txid &new_hash)
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
Data model for a walletmodel transaction.
QString getDisplayName() const
AddressTableModel * getAddressTableModel() const
AddressTableModel * addressTableModel
Definition: walletmodel.h:175
CAmount getAvailableBalance(const wallet::CCoinControl *control)
void sendCoins(WalletModelTransaction &transaction)
std::unique_ptr< interfaces::Handler > m_handler_transaction_changed
Definition: walletmodel.h:163
void timerTimeout()
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:83
void pollBalanceChanged()
Definition: walletmodel.cpp:92
ClientModel & clientModel() const
Definition: walletmodel.h:139
void subscribeToCoreSignals()