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  bool privateKeysDisabled() const;
144  bool canGetAddresses() const;
145 
146  interfaces::Node& node() const { return m_node; }
147  interfaces::Wallet& wallet() const { return *m_wallet; }
148 
149  QString getWalletName() const;
150  QString getDisplayName() const;
151 
152  bool isMultiwallet();
153 
154  AddressTableModel* getAddressTableModel() const { return addressTableModel; }
155 private:
156  std::unique_ptr<interfaces::Wallet> m_wallet;
157  std::unique_ptr<interfaces::Handler> m_handler_unload;
158  std::unique_ptr<interfaces::Handler> m_handler_status_changed;
159  std::unique_ptr<interfaces::Handler> m_handler_address_book_changed;
160  std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
161  std::unique_ptr<interfaces::Handler> m_handler_show_progress;
162  std::unique_ptr<interfaces::Handler> m_handler_watch_only_changed;
163  std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
165 
167  bool fForceCheckBalanceChanged{false};
168 
169  // Wallet has an options model for wallet-specific options
170  // (transaction fee, for example)
172 
176 
177  // Cache some values to be able to detect changes
181 
182  void subscribeToCoreSignals();
183  void unsubscribeFromCoreSignals();
184  void checkBalanceChanged(const interfaces::WalletBalances& new_balances);
185 
186 Q_SIGNALS:
187  // Signal that balance in wallet changed
188  void balanceChanged(const interfaces::WalletBalances& balances);
189 
190  // Encryption status of wallet changed
191  void encryptionStatusChanged();
192 
193  // Signal emitted when wallet needs to be unlocked
194  // It is valid behaviour for listeners to keep the wallet locked after this signal;
195  // this means that the unlocking failed or was cancelled.
196  void requireUnlock();
197 
198  // Fired when a message should be reported to the user
199  void message(const QString &title, const QString &message, unsigned int style);
200 
201  // Coins sent: from wallet, to recipient, in (serialized) transaction:
202  void coinsSent(WalletModel* wallet, SendCoinsRecipient recipient, QByteArray transaction);
203 
204  // Show progress dialog e.g. for rescan
205  void showProgress(const QString &title, int nProgress);
206 
207  // Watch-only address added
208  void notifyWatchonlyChanged(bool fHaveWatchonly);
209 
210  // Signal that wallet is about to be removed
211  void unload();
212 
213  // Notify that there are now keys in the keypool
214  void canGetAddressesChanged();
215 
216 public Q_SLOTS:
217  /* Starts a timer to periodically update the balance */
218  void startPollBalance();
219 
220  /* Wallet status might have changed */
221  void updateStatus();
222  /* New transaction, or transaction changed status */
223  void updateTransaction();
224  /* New, updated or removed address book entry */
225  void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
226  /* Watch-only added */
227  void updateWatchOnlyFlag(bool fHaveWatchonly);
228  /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
229  void pollBalanceChanged();
230 };
231 
232 #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:174
std::shared_ptr< CWallet > m_wallet
Definition: wallet.cpp:512
interfaces::Wallet & wallet() const
Definition: walletmodel.h:147
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:175
std::unique_ptr< interfaces::Handler > m_handler_address_book_changed
Definition: walletmodel.h:159
NodeContext & m_node
Definition: chain.cpp:374
std::unique_ptr< interfaces::Handler > m_handler_unload
Definition: walletmodel.h:157
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:158
OutputType
Definition: outputtype.h:17
Coin Control Features.
Definition: coincontrol.h:22
Collection of wallet balances.
Definition: wallet.h:314
An encapsulated public key.
Definition: pubkey.h:30
Interface for accessing a wallet.
Definition: wallet.h:48
std::unique_ptr< interfaces::Wallet > m_wallet
Definition: walletmodel.h:156
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:91
OptionsModel * optionsModel
Definition: walletmodel.h:171
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:179
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:146
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:161
UnlockContext & operator=(UnlockContext &&rhs)
Definition: walletmodel.h:125
256-bit opaque blob.
Definition: uint256.h:120
int cachedNumBlocks
Definition: walletmodel.h:180
std::unique_ptr< interfaces::Handler > m_handler_can_get_addrs_changed
Definition: walletmodel.h:163
AddressTableModel * getAddressTableModel() const
Definition: walletmodel.h:154
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:178
interfaces::Node & m_node
Definition: walletmodel.h:164
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:173
std::unique_ptr< interfaces::Handler > m_handler_transaction_changed
Definition: walletmodel.h:160
bool fHaveWatchOnly
Definition: walletmodel.h:166
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:162