Bitcoin Core  0.20.99
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
69  PaymentRequestExpired
70  };
71 
73  {
74  Unencrypted, // !wallet->IsCrypted()
75  Locked, // wallet->IsCrypted() && wallet->IsLocked()
76  Unlocked // wallet->IsCrypted() && !wallet->IsLocked()
77  };
78 
79  OptionsModel *getOptionsModel();
80  AddressTableModel *getAddressTableModel();
81  TransactionTableModel *getTransactionTableModel();
82  RecentRequestsTableModel *getRecentRequestsTableModel();
83 
84  EncryptionStatus getEncryptionStatus() const;
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
105  SendCoinsReturn sendCoins(WalletModelTransaction &transaction);
106 
107  // Wallet encryption
108  bool setWalletEncrypted(bool encrypted, 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:
117  UnlockContext(WalletModel *wallet, bool valid, bool relock);
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 
136  UnlockContext requestUnlock();
137 
138  void loadReceiveRequests(std::vector<std::string>& vReceiveRequests);
139  bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest);
140 
141  bool bumpFee(uint256 hash, uint256& new_hash);
142 
143  static bool isWalletEnabled();
144 
145  interfaces::Node& node() const { return m_node; }
146  interfaces::Wallet& wallet() const { return *m_wallet; }
147  ClientModel& clientModel() const { return *m_client_model; }
148  void setClientModel(ClientModel* client_model);
149 
150  QString getWalletName() const;
151  QString getDisplayName() const;
152 
153  bool isMultiwallet();
154 
155  AddressTableModel* getAddressTableModel() const { return addressTableModel; }
156 
157  void refresh(bool pk_hash_only = false);
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_watch_only_changed;
166  std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
169 
171  bool fForceCheckBalanceChanged{false};
172 
173  // Wallet has an options model for wallet-specific options
174  // (transaction fee, for example)
176 
180 
181  // Cache some values to be able to detect changes
184  QTimer* timer;
185 
186  // Block hash denoting when the last balance update was done.
187  uint256 m_cached_last_update_tip{};
188 
189  void subscribeToCoreSignals();
190  void unsubscribeFromCoreSignals();
191  void checkBalanceChanged(const interfaces::WalletBalances& new_balances);
192 
193 Q_SIGNALS:
194  // Signal that balance in wallet changed
195  void balanceChanged(const interfaces::WalletBalances& balances);
196 
197  // Encryption status of wallet changed
198  void encryptionStatusChanged();
199 
200  // Signal emitted when wallet needs to be unlocked
201  // It is valid behaviour for listeners to keep the wallet locked after this signal;
202  // this means that the unlocking failed or was cancelled.
203  void requireUnlock();
204 
205  // Fired when a message should be reported to the user
206  void message(const QString &title, const QString &message, unsigned int style);
207 
208  // Coins sent: from wallet, to recipient, in (serialized) transaction:
209  void coinsSent(WalletModel* wallet, SendCoinsRecipient recipient, QByteArray transaction);
210 
211  // Show progress dialog e.g. for rescan
212  void showProgress(const QString &title, int nProgress);
213 
214  // Watch-only address added
215  void notifyWatchonlyChanged(bool fHaveWatchonly);
216 
217  // Signal that wallet is about to be removed
218  void unload();
219 
220  // Notify that there are now keys in the keypool
221  void canGetAddressesChanged();
222 
223 public Q_SLOTS:
224  /* Starts a timer to periodically update the balance */
225  void startPollBalance();
226 
227  /* Wallet status might have changed */
228  void updateStatus();
229  /* New transaction, or transaction changed status */
230  void updateTransaction();
231  /* New, updated or removed address book entry */
232  void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
233  /* Watch-only added */
234  void updateWatchOnlyFlag(bool fHaveWatchonly);
235  /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
236  void pollBalanceChanged();
237 };
238 
239 #endif // BITCOIN_QT_WALLETMODEL_H
UnlockContext(UnlockContext &&obj)
Definition: walletmodel.h:125
Model for list of recently generated payment requests / bitcoin: URIs.
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:178
std::shared_ptr< CWallet > m_wallet
Definition: wallet.cpp:480
interfaces::Wallet & wallet() const
Definition: walletmodel.h:146
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:179
std::unique_ptr< interfaces::Handler > m_handler_address_book_changed
Definition: walletmodel.h:162
NodeContext & m_node
Definition: chain.cpp:382
std::unique_ptr< interfaces::Handler > m_handler_unload
Definition: walletmodel.h:160
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:60
std::unique_ptr< interfaces::Handler > m_handler_status_changed
Definition: walletmodel.h:161
OutputType
Definition: outputtype.h:17
Coin Control Features.
Definition: coincontrol.h:22
Collection of wallet balances.
Definition: wallet.h:321
An encapsulated public key.
Definition: pubkey.h:30
Interface for accessing a wallet.
Definition: wallet.h:50
std::unique_ptr< interfaces::Wallet > m_wallet
Definition: walletmodel.h:159
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:92
OptionsModel * optionsModel
Definition: walletmodel.h:175
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:183
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
interfaces::Node & node() const
Definition: walletmodel.h:145
UI model for the transaction table of a wallet.
Model for Bitcoin network client.
Definition: clientmodel.h:46
Qt model of the address book in the core.
std::unique_ptr< interfaces::Handler > m_handler_show_progress
Definition: walletmodel.h:164
UnlockContext & operator=(UnlockContext &&rhs)
Definition: walletmodel.h:126
ClientModel * m_client_model
Definition: walletmodel.h:167
256-bit opaque blob.
Definition: uint256.h:120
QTimer * timer
Definition: walletmodel.h:184
std::unique_ptr< interfaces::Handler > m_handler_can_get_addrs_changed
Definition: walletmodel.h:166
AddressTableModel * getAddressTableModel() const
Definition: walletmodel.h:155
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:36
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:51
interfaces::WalletBalances m_cached_balances
Definition: walletmodel.h:182
interfaces::Node & m_node
Definition: walletmodel.h:168
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
Data model for a walletmodel transaction.
AddressTableModel * addressTableModel
Definition: walletmodel.h:177
std::unique_ptr< interfaces::Handler > m_handler_transaction_changed
Definition: walletmodel.h:163
bool fHaveWatchOnly
Definition: walletmodel.h:170
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:42
std::unique_ptr< interfaces::Handler > m_handler_watch_only_changed
Definition: walletmodel.h:165
ClientModel & clientModel() const
Definition: walletmodel.h:147