Bitcoin Core  0.19.99
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 #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 OptionsModel;
28 class PlatformStyle;
30 class SendCoinsRecipient;
33 
34 class CCoinControl;
35 class CKeyID;
36 class COutPoint;
37 class COutput;
38 class CPubKey;
39 class uint256;
40 
41 namespace interfaces {
42 class Node;
43 } // namespace interfaces
44 
45 QT_BEGIN_NAMESPACE
46 class QTimer;
47 QT_END_NAMESPACE
48 
50 class WalletModel : public QObject
51 {
52  Q_OBJECT
53 
54 public:
55  explicit WalletModel(std::unique_ptr<interfaces::Wallet> wallet, interfaces::Node& node, const PlatformStyle *platformStyle, OptionsModel *optionsModel, QObject *parent = nullptr);
56  ~WalletModel();
57 
58  enum StatusCode // Returned by sendCoins
59  {
60  OK,
66  TransactionCreationFailed, // Error returned when wallet is still locked
68  PaymentRequestExpired
69  };
70 
72  {
73  Unencrypted, // !wallet->IsCrypted()
74  Locked, // wallet->IsCrypted() && wallet->IsLocked()
75  Unlocked // wallet->IsCrypted() && !wallet->IsLocked()
76  };
77 
78  OptionsModel *getOptionsModel();
79  AddressTableModel *getAddressTableModel();
80  TransactionTableModel *getTransactionTableModel();
81  RecentRequestsTableModel *getRecentRequestsTableModel();
82 
83  EncryptionStatus getEncryptionStatus() const;
84 
85  // Check address for validity
86  bool validateAddress(const QString &address);
87 
88  // Return status record for SendCoins, contains error id + information
90  {
91  SendCoinsReturn(StatusCode _status = OK, QString _reasonCommitFailed = "")
92  : status(_status),
93  reasonCommitFailed(_reasonCommitFailed)
94  {
95  }
98  };
99 
100  // prepare transaction for getting txfee before sending coins
101  SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, 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, const SecureString &passPhrase=SecureString());
110  bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
111 
112  // RAI object for unlocking wallet, returned by requestUnlock()
114  {
115  public:
116  UnlockContext(WalletModel *wallet, bool valid, bool relock);
117  ~UnlockContext();
118 
119  bool isValid() const { return valid; }
120 
121  // Copy constructor is disabled.
122  UnlockContext(const UnlockContext&) = delete;
123  // Move operator and constructor transfer the context
124  UnlockContext(UnlockContext&& obj) { CopyFrom(std::move(obj)); }
125  UnlockContext& operator=(UnlockContext&& rhs) { CopyFrom(std::move(rhs)); return *this; }
126  private:
128  bool valid;
129  mutable bool relock; // mutable, as it can be set to false by copying
130 
131  UnlockContext& operator=(const UnlockContext&) = default;
132  void CopyFrom(UnlockContext&& rhs);
133  };
134 
135  UnlockContext requestUnlock();
136 
137  void loadReceiveRequests(std::vector<std::string>& vReceiveRequests);
138  bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest);
139 
140  bool bumpFee(uint256 hash, uint256& new_hash);
141 
142  static bool isWalletEnabled();
143 
144  interfaces::Node& node() const { return m_node; }
145  interfaces::Wallet& wallet() const { return *m_wallet; }
146 
147  QString getWalletName() const;
148  QString getDisplayName() const;
149 
150  bool isMultiwallet();
151 
152  AddressTableModel* getAddressTableModel() const { return addressTableModel; }
153 private:
154  std::unique_ptr<interfaces::Wallet> m_wallet;
155  std::unique_ptr<interfaces::Handler> m_handler_unload;
156  std::unique_ptr<interfaces::Handler> m_handler_status_changed;
157  std::unique_ptr<interfaces::Handler> m_handler_address_book_changed;
158  std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
159  std::unique_ptr<interfaces::Handler> m_handler_show_progress;
160  std::unique_ptr<interfaces::Handler> m_handler_watch_only_changed;
161  std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
163 
165  bool fForceCheckBalanceChanged{false};
166 
167  // Wallet has an options model for wallet-specific options
168  // (transaction fee, for example)
170 
174 
175  // Cache some values to be able to detect changes
179 
180  void subscribeToCoreSignals();
181  void unsubscribeFromCoreSignals();
182  void checkBalanceChanged(const interfaces::WalletBalances& new_balances);
183 
184 Q_SIGNALS:
185  // Signal that balance in wallet changed
186  void balanceChanged(const interfaces::WalletBalances& balances);
187 
188  // Encryption status of wallet changed
189  void encryptionStatusChanged();
190 
191  // Signal emitted when wallet needs to be unlocked
192  // It is valid behaviour for listeners to keep the wallet locked after this signal;
193  // this means that the unlocking failed or was cancelled.
194  void requireUnlock();
195 
196  // Fired when a message should be reported to the user
197  void message(const QString &title, const QString &message, unsigned int style);
198 
199  // Coins sent: from wallet, to recipient, in (serialized) transaction:
200  void coinsSent(WalletModel* wallet, SendCoinsRecipient recipient, QByteArray transaction);
201 
202  // Show progress dialog e.g. for rescan
203  void showProgress(const QString &title, int nProgress);
204 
205  // Watch-only address added
206  void notifyWatchonlyChanged(bool fHaveWatchonly);
207 
208  // Signal that wallet is about to be removed
209  void unload();
210 
211  // Notify that there are now keys in the keypool
212  void canGetAddressesChanged();
213 
214 public Q_SLOTS:
215  /* Starts a timer to periodically update the balance */
216  void startPollBalance();
217 
218  /* Wallet status might have changed */
219  void updateStatus();
220  /* New transaction, or transaction changed status */
221  void updateTransaction();
222  /* New, updated or removed address book entry */
223  void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
224  /* Watch-only added */
225  void updateWatchOnlyFlag(bool fHaveWatchonly);
226  /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
227  void pollBalanceChanged();
228 };
229 
230 #endif // BITCOIN_QT_WALLETMODEL_H
UnlockContext(UnlockContext &&obj)
Definition: walletmodel.h:124
Model for list of recently generated payment requests / bitcoin: URIs.
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:172
std::shared_ptr< CWallet > m_wallet
Definition: wallet.cpp:501
interfaces::Wallet & wallet() const
Definition: walletmodel.h:145
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:173
std::unique_ptr< interfaces::Handler > m_handler_address_book_changed
Definition: walletmodel.h:157
NodeContext & m_node
Definition: chain.cpp:382
std::unique_ptr< interfaces::Handler > m_handler_unload
Definition: walletmodel.h:155
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:156
OutputType
Definition: outputtype.h:17
Coin Control Features.
Definition: coincontrol.h:22
Collection of wallet balances.
Definition: wallet.h:317
An encapsulated public key.
Definition: pubkey.h:30
Interface for accessing a wallet.
Definition: wallet.h:49
std::unique_ptr< interfaces::Wallet > m_wallet
Definition: walletmodel.h:154
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:91
OptionsModel * optionsModel
Definition: walletmodel.h:169
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:177
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:144
UI model for the transaction table of a wallet.
Qt model of the address book in the core.
std::unique_ptr< interfaces::Handler > m_handler_show_progress
Definition: walletmodel.h:159
UnlockContext & operator=(UnlockContext &&rhs)
Definition: walletmodel.h:125
256-bit opaque blob.
Definition: uint256.h:120
int cachedNumBlocks
Definition: walletmodel.h:178
std::unique_ptr< interfaces::Handler > m_handler_can_get_addrs_changed
Definition: walletmodel.h:161
AddressTableModel * getAddressTableModel() const
Definition: walletmodel.h:152
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:50
interfaces::WalletBalances m_cached_balances
Definition: walletmodel.h:176
interfaces::Node & m_node
Definition: walletmodel.h:162
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:171
std::unique_ptr< interfaces::Handler > m_handler_transaction_changed
Definition: walletmodel.h:158
bool fHaveWatchOnly
Definition: walletmodel.h:164
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:39
std::unique_ptr< interfaces::Handler > m_handler_watch_only_changed
Definition: walletmodel.h:160