Bitcoin ABC  0.22.13
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
68  PaymentRequestExpired
69  };
70 
72  // !wallet->IsCrypted()
74  // wallet->IsCrypted() && wallet->IsLocked()
76  // wallet->IsCrypted() && !wallet->IsLocked()
77  Unlocked
78  };
79 
80  OptionsModel *getOptionsModel();
81  AddressTableModel *getAddressTableModel();
82  TransactionTableModel *getTransactionTableModel();
83  RecentRequestsTableModel *getRecentRequestsTableModel();
84 
85  EncryptionStatus getEncryptionStatus() const;
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
100  SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction,
101  const CCoinControl &coinControl);
102 
103  // Send coins to a list of recipients
104  SendCoinsReturn sendCoins(WalletModelTransaction &transaction);
105 
106  // Wallet encryption
107  bool setWalletEncrypted(bool encrypted, 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:
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)); }
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 
141  UnlockContext requestUnlock();
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 
153  const CChainParams &getChainParams() const;
154 
155  QString getWalletName() const;
156  QString getDisplayName() const;
157 
158  bool isMultiwallet();
159 
161  return addressTableModel;
162  }
163 
164 private:
165  std::unique_ptr<interfaces::Wallet> m_wallet;
166  std::unique_ptr<interfaces::Handler> m_handler_unload;
167  std::unique_ptr<interfaces::Handler> m_handler_status_changed;
168  std::unique_ptr<interfaces::Handler> m_handler_address_book_changed;
169  std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
170  std::unique_ptr<interfaces::Handler> m_handler_show_progress;
171  std::unique_ptr<interfaces::Handler> m_handler_watch_only_changed;
172  std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
175 
177  bool fForceCheckBalanceChanged{false};
178 
179  // Wallet has an options model for wallet-specific options (transaction fee,
180  // for example)
182 
186 
187  // Cache some values to be able to detect changes
191 
192  void subscribeToCoreSignals();
193  void unsubscribeFromCoreSignals();
194  void checkBalanceChanged(const interfaces::WalletBalances &new_balances);
195 
196 Q_SIGNALS:
197  // Signal that balance in wallet changed
198  void balanceChanged(const interfaces::WalletBalances &balances);
199 
200  // Encryption status of wallet changed
201  void encryptionStatusChanged();
202 
203  // Signal emitted when wallet needs to be unlocked
204  // It is valid behaviour for listeners to keep the wallet locked after this
205  // signal; this means that the unlocking failed or was cancelled.
206  void requireUnlock();
207 
208  // Fired when a message should be reported to the user
209  void message(const QString &title, const QString &message,
210  unsigned int style);
211 
212  // Coins sent: from wallet, to recipient, in (serialized) transaction:
213  void coinsSent(interfaces::Wallet &wallet, SendCoinsRecipient recipient,
214  QByteArray transaction);
215 
216  // Show progress dialog e.g. for rescan
217  void showProgress(const QString &title, int nProgress);
218 
219  // Watch-only address added
220  void notifyWatchonlyChanged(bool fHaveWatchonly);
221 
222  // Signal that wallet is about to be removed
223  void unload();
224 
225  // Notify that there are now keys in the keypool
226  void canGetAddressesChanged();
227 
228 public Q_SLOTS:
229  /* Starts a timer to periodically update the balance */
230  void startPollBalance();
231 
232  /* Wallet status might have changed */
233  void updateStatus();
235  void updateTransaction();
237  void updateAddressBook(const QString &address, const QString &label,
238  bool isMine, const QString &purpose, int status);
240  void updateWatchOnlyFlag(bool fHaveWatchonly);
245  void pollBalanceChanged();
246 };
247 
248 #endif // BITCOIN_QT_WALLETMODEL_H
UnlockContext(UnlockContext &&obj)
Definition: walletmodel.h:125
ClientModel & m_client_model
Definition: walletmodel.h:173
Model for list of recently generated payment requests / bitcoincash: URIs.
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:184
std::shared_ptr< CWallet > m_wallet
Definition: wallet.cpp:464
interfaces::Wallet & wallet() const
Definition: walletmodel.h:150
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:185
std::unique_ptr< interfaces::Handler > m_handler_address_book_changed
Definition: walletmodel.h:168
NodeContext & m_node
Definition: chain.cpp:442
std::unique_ptr< interfaces::Handler > m_handler_unload
Definition: walletmodel.h:166
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:56
std::unique_ptr< interfaces::Handler > m_handler_status_changed
Definition: walletmodel.h:167
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:47
Coin Control Features.
Definition: coincontrol.h:19
Collection of wallet balances.
Definition: wallet.h:319
An encapsulated public key.
Definition: pubkey.h:31
Interface for accessing a wallet.
Definition: wallet.h:56
std::unique_ptr< interfaces::Wallet > m_wallet
Definition: walletmodel.h:165
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:92
OptionsModel * optionsModel
Definition: walletmodel.h:181
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:189
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
interfaces::Node & node() const
Definition: walletmodel.h:149
UI model for the transaction table of a wallet.
Model for Bitcoin network client.
Definition: clientmodel.h:34
Qt model of the address book in the core.
std::unique_ptr< interfaces::Handler > m_handler_show_progress
Definition: walletmodel.h:170
UnlockContext & operator=(UnlockContext &&rhs)
Definition: walletmodel.h:126
int cachedNumBlocks
Definition: walletmodel.h:190
std::unique_ptr< interfaces::Handler > m_handler_can_get_addrs_changed
Definition: walletmodel.h:172
AddressTableModel * getAddressTableModel() const
Definition: walletmodel.h:160
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:48
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:47
interfaces::WalletBalances m_cached_balances
Definition: walletmodel.h:188
interfaces::Node & m_node
Definition: walletmodel.h:174
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
Data model for a walletmodel transaction.
AddressTableModel * addressTableModel
Definition: walletmodel.h:183
std::unique_ptr< interfaces::Handler > m_handler_transaction_changed
Definition: walletmodel.h:169
bool fHaveWatchOnly
Definition: walletmodel.h:176
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:45
std::unique_ptr< interfaces::Handler > m_handler_watch_only_changed
Definition: walletmodel.h:171
ClientModel & clientModel() const
Definition: walletmodel.h:151