Bitcoin ABC  0.29.2
P2P Digital Currency
walletmodel.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2019 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 <chainparams.h>
9 #include <interfaces/wallet.h>
10 
11 #if defined(HAVE_CONFIG_H)
12 #include <config/bitcoin-config.h>
13 #endif
14 
17 
18 #include <QObject>
19 
20 #include <memory>
21 #include <vector>
22 
23 class AddressTableModel;
24 class ClientModel;
25 class OptionsModel;
26 class PlatformStyle;
28 class SendCoinsRecipient;
31 
32 class CCoinControl;
33 class CKeyID;
34 class COutPoint;
35 class COutput;
36 class CPubKey;
37 
38 namespace interfaces {
39 class Node;
40 } // namespace interfaces
41 
42 QT_BEGIN_NAMESPACE
43 class QTimer;
44 QT_END_NAMESPACE
45 
47 class WalletModel : public QObject {
48  Q_OBJECT
49 
50 public:
51  explicit WalletModel(std::unique_ptr<interfaces::Wallet> wallet,
52  ClientModel &client_model,
53  const PlatformStyle *platformStyle,
54  QObject *parent = nullptr);
55  ~WalletModel();
56 
57  // Returned by sendCoins
58  enum StatusCode {
59  OK,
65  // Error returned when wallet is still locked
69  };
70 
72  // !wallet->IsCrypted()
74  // wallet->IsCrypted() && wallet->IsLocked()
76  // wallet->IsCrypted() && !wallet->IsLocked()
77  Unlocked
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
91  struct SendCoinsReturn {
93  QString _reasonCommitFailed = "")
94  : status(_status), reasonCommitFailed(_reasonCommitFailed) {}
97  };
98 
99  // prepare transaction for getting txfee before sending coins
101  const CCoinControl &coinControl);
102 
103  // Send coins to a list of recipients
105 
106  // Wallet encryption
107  bool setWalletEncrypted(const SecureString &passphrase);
108  // Passphrase only needed when unlocking
109  bool setWalletLocked(bool locked,
110  const SecureString &passPhrase = SecureString());
111  bool changePassphrase(const SecureString &oldPass,
112  const SecureString &newPass);
113 
114  // RAI object for unlocking wallet, returned by requestUnlock()
116  public:
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)); }
127  CopyFrom(std::move(rhs));
128  return *this;
129  }
130 
131  private:
133  bool valid;
134  // mutable, as it can be set to false by copying
135  mutable bool relock;
136 
137  UnlockContext &operator=(const UnlockContext &) = default;
138  void CopyFrom(UnlockContext &&rhs);
139  };
140 
142 
143  void loadReceiveRequests(std::vector<std::string> &vReceiveRequests);
144  bool saveReceiveRequest(const std::string &sAddress, const int64_t nId,
145  const std::string &sRequest);
146 
147  static bool isWalletEnabled();
148 
149  interfaces::Node &node() const { return m_node; }
150  interfaces::Wallet &wallet() const { return *m_wallet; }
151  ClientModel &clientModel() const { return *m_client_model; }
152  void setClientModel(ClientModel *client_model);
153 
154  const CChainParams &getChainParams() const;
155 
156  QString getWalletName() const;
157  QString getDisplayName() const;
158 
159  bool isMultiwallet();
160 
162  return addressTableModel;
163  }
164 
166 
167 private:
168  std::unique_ptr<interfaces::Wallet> m_wallet;
169  std::unique_ptr<interfaces::Handler> m_handler_unload;
170  std::unique_ptr<interfaces::Handler> m_handler_status_changed;
171  std::unique_ptr<interfaces::Handler> m_handler_address_book_changed;
172  std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
173  std::unique_ptr<interfaces::Handler> m_handler_show_progress;
174  std::unique_ptr<interfaces::Handler> m_handler_watch_only_changed;
175  std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
178 
181 
182  // Wallet has an options model for wallet-specific options (transaction fee,
183  // for example)
185 
189 
190  // Cache some values to be able to detect changes
193  QTimer *timer;
194 
195  // Block hash denoting when the last balance update was done.
197 
198  void subscribeToCoreSignals();
200  void checkBalanceChanged(const interfaces::WalletBalances &new_balances);
201 
202 Q_SIGNALS:
203  // Signal that balance in wallet changed
205 
206  // Encryption status of wallet changed
208 
209  // Signal emitted when wallet needs to be unlocked
210  // It is valid behaviour for listeners to keep the wallet locked after this
211  // signal; this means that the unlocking failed or was cancelled.
213 
214  // Fired when a message should be reported to the user
215  void message(const QString &title, const QString &message,
216  unsigned int style);
217 
218  // Coins sent: from wallet, to recipient, in (serialized) transaction:
220  QByteArray transaction);
221 
222  // Show progress dialog e.g. for rescan
223  void showProgress(const QString &title, int nProgress);
224 
225  // Watch-only address added
226  void notifyWatchonlyChanged(bool fHaveWatchonly);
227 
228  // Signal that wallet is about to be removed
229  void unload();
230 
231  // Notify that there are now keys in the keypool
233 
234 public Q_SLOTS:
235  /* Starts a timer to periodically update the balance */
236  void startPollBalance();
237 
238  /* Wallet status might have changed */
239  void updateStatus();
241  void updateTransaction();
243  void updateAddressBook(const QString &address, const QString &label,
244  bool isMine, const QString &purpose, int status);
246  void updateWatchOnlyFlag(bool fHaveWatchonly);
251  void pollBalanceChanged();
252 };
253 
254 #endif // BITCOIN_QT_WALLETMODEL_H
Qt model of the address book in the core.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:74
Coin Control Features.
Definition: coincontrol.h:21
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
Definition: spend.h:19
An encapsulated public key.
Definition: pubkey.h:31
Model for Bitcoin network client.
Definition: clientmodel.h:39
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:48
Model for list of recently generated payment requests / bitcoincash: URIs.
UI model for the transaction table of a wallet.
UnlockContext(WalletModel *wallet, bool valid, bool relock)
UnlockContext & operator=(UnlockContext &&rhs)
Definition: walletmodel.h:126
UnlockContext & operator=(const UnlockContext &)=default
UnlockContext(const UnlockContext &)=delete
UnlockContext(UnlockContext &&obj)
Definition: walletmodel.h:125
void CopyFrom(UnlockContext &&rhs)
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:47
OptionsModel * optionsModel
Definition: walletmodel.h:184
bool validateAddress(const QString &address)
AddressTableModel * addressTableModel
Definition: walletmodel.h:186
void loadReceiveRequests(std::vector< std::string > &vReceiveRequests)
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:192
ClientModel * m_client_model
Definition: walletmodel.h:176
std::unique_ptr< interfaces::Handler > m_handler_watch_only_changed
Definition: walletmodel.h:174
BlockHash m_cached_last_update_tip
Definition: walletmodel.h:196
interfaces::Node & m_node
Definition: walletmodel.h:177
std::unique_ptr< interfaces::Handler > m_handler_transaction_changed
Definition: walletmodel.h:172
void startPollBalance()
Definition: walletmodel.cpp:51
void pollBalanceChanged()
Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so.
Definition: walletmodel.cpp:72
SendCoinsReturn sendCoins(WalletModelTransaction &transaction)
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:188
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:187
bool setWalletEncrypted(const SecureString &passphrase)
ClientModel & clientModel() const
Definition: walletmodel.h:151
void notifyWatchonlyChanged(bool fHaveWatchonly)
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
BlockHash getLastBlockProcessed() const
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)
void coinsSent(interfaces::Wallet &wallet, SendCoinsRecipient recipient, QByteArray transaction)
void setClientModel(ClientModel *client_model)
Definition: walletmodel.cpp:57
const CChainParams & getChainParams() const
bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest)
void updateStatus()
Definition: walletmodel.cpp:64
AddressTableModel * getAddressTableModel()
OptionsModel * getOptionsModel()
std::unique_ptr< interfaces::Handler > m_handler_can_get_addrs_changed
Definition: walletmodel.h:175
std::unique_ptr< interfaces::Handler > m_handler_unload
Definition: walletmodel.h:169
interfaces::Wallet & wallet() const
Definition: walletmodel.h:150
SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const CCoinControl &coinControl)
EncryptionStatus getEncryptionStatus() const
RecentRequestsTableModel * getRecentRequestsTableModel()
std::unique_ptr< interfaces::Handler > m_handler_status_changed
Definition: walletmodel.h:170
interfaces::WalletBalances m_cached_balances
Definition: walletmodel.h:191
bool fForceCheckBalanceChanged
Definition: walletmodel.h:180
QString getDisplayName() const
void checkBalanceChanged(const interfaces::WalletBalances &new_balances)
bool isMultiwallet()
void unsubscribeFromCoreSignals()
void requireUnlock()
void updateTransaction()
New transaction, or transaction changed status.
void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status)
New, updated or removed address book entry.
void canGetAddressesChanged()
QTimer * timer
Definition: walletmodel.h:193
bool fHaveWatchOnly
Definition: walletmodel.h:179
WalletModel(std::unique_ptr< interfaces::Wallet > wallet, ClientModel &client_model, const PlatformStyle *platformStyle, QObject *parent=nullptr)
Definition: walletmodel.cpp:31
void updateWatchOnlyFlag(bool fHaveWatchonly)
Watch-only added.
std::unique_ptr< interfaces::Handler > m_handler_address_book_changed
Definition: walletmodel.h:171
void encryptionStatusChanged()
interfaces::Node & node() const
Definition: walletmodel.h:149
std::unique_ptr< interfaces::Wallet > m_wallet
Definition: walletmodel.h:168
UnlockContext requestUnlock()
AddressTableModel * getAddressTableModel() const
Definition: walletmodel.h:161
void balanceChanged(const interfaces::WalletBalances &balances)
static bool isWalletEnabled()
QString getWalletName() const
std::unique_ptr< interfaces::Handler > m_handler_show_progress
Definition: walletmodel.h:173
void unload()
@ AmountWithFeeExceedsBalance
Definition: walletmodel.h:63
@ TransactionCreationFailed
Definition: walletmodel.h:66
@ AmountExceedsBalance
Definition: walletmodel.h:62
@ DuplicateAddress
Definition: walletmodel.h:64
@ PaymentRequestExpired
Definition: walletmodel.h:68
void subscribeToCoreSignals()
TransactionTableModel * getTransactionTableModel()
Data model for a walletmodel transaction.
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:58
Interface for accessing a wallet.
Definition: wallet.h:59
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:55
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:92
Collection of wallet balances.
Definition: wallet.h:347