BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
walletmodel.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2018 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 <amount.h>
9 #include <key.h>
10 #include <serialize.h>
11 #include <script/standard.h>
12 
13 #if defined(HAVE_CONFIG_H)
14 #include <config/bitcoin-config.h>
15 #endif
16 
17 #ifdef ENABLE_BIP70
18 #include <qt/paymentrequestplus.h>
19 #endif
21 
22 #include <interfaces/wallet.h>
24 
25 #include <map>
26 #include <vector>
27 
28 #include <QObject>
29 
30 enum class OutputType;
31 
32 class AddressTableModel;
33 class OptionsModel;
34 class PlatformStyle;
38 
39 class CCoinControl;
40 class CKeyID;
41 class COutPoint;
42 class COutput;
43 class CPubKey;
44 class uint256;
45 
46 namespace interfaces {
47 class Node;
48 } // namespace interfaces
49 
50 QT_BEGIN_NAMESPACE
51 class QTimer;
52 QT_END_NAMESPACE
53 
55 {
56 public:
58  explicit SendCoinsRecipient(const QString &addr, const QString &_label, const CAmount& _amount, const QString &_message):
59  address(addr), label(_label), amount(_amount), message(_message), fSubtractFeeFromAmount(false), nVersion(SendCoinsRecipient::CURRENT_VERSION) {}
60 
61  // If from an unauthenticated payment request, this is used for storing
62  // the addresses, e.g. address-A<br />address-B<br />address-C.
63  // Info: As we don't need to process addresses in here when using
64  // payment requests, we can abuse it for displaying an address list.
65  // Todo: This is a hack, should be replaced with a cleaner solution!
66  QString address;
67  QString label;
69  // If from a payment request, this is used for storing the memo
70  QString message;
71 
72 #ifdef ENABLE_BIP70
73  // If from a payment request, paymentRequest.IsInitialized() will be true
74  PaymentRequestPlus paymentRequest;
75 #else
76  // If building with BIP70 is disabled, keep the payment request around as
77  // serialized string to ensure load/store is lossless
78  std::string sPaymentRequest;
79 #endif
80  // Empty if no authentication or invalid signature/cert/etc.
82 
83  bool fSubtractFeeFromAmount; // memory only
84 
85  static const int CURRENT_VERSION = 1;
86  int nVersion;
87 
89 
90  template <typename Stream, typename Operation>
91  inline void SerializationOp(Stream& s, Operation ser_action) {
92  std::string sAddress = address.toStdString();
93  std::string sLabel = label.toStdString();
94  std::string sMessage = message.toStdString();
95 #ifdef ENABLE_BIP70
96  std::string sPaymentRequest;
97  if (!ser_action.ForRead() && paymentRequest.IsInitialized())
98  paymentRequest.SerializeToString(&sPaymentRequest);
99 #endif
100  std::string sAuthenticatedMerchant = authenticatedMerchant.toStdString();
101 
102  READWRITE(this->nVersion);
103  READWRITE(sAddress);
104  READWRITE(sLabel);
105  READWRITE(amount);
106  READWRITE(sMessage);
108  READWRITE(sAuthenticatedMerchant);
109 
110  if (ser_action.ForRead())
111  {
112  address = QString::fromStdString(sAddress);
113  label = QString::fromStdString(sLabel);
114  message = QString::fromStdString(sMessage);
115 #ifdef ENABLE_BIP70
116  if (!sPaymentRequest.empty())
117  paymentRequest.parse(QByteArray::fromRawData(sPaymentRequest.data(), sPaymentRequest.size()));
118 #endif
119  authenticatedMerchant = QString::fromStdString(sAuthenticatedMerchant);
120  }
121  }
122 };
123 
125 class WalletModel : public QObject
126 {
127  Q_OBJECT
128 
129 public:
130  explicit WalletModel(std::unique_ptr<interfaces::Wallet> wallet, interfaces::Node& node, const PlatformStyle *platformStyle, OptionsModel *optionsModel, QObject *parent = 0);
131  ~WalletModel();
132 
133  enum StatusCode // Returned by sendCoins
134  {
135  OK,
141  TransactionCreationFailed, // Error returned when wallet is still locked
145  };
146 
148  {
149  Unencrypted, // !wallet->IsCrypted()
150  Locked, // wallet->IsCrypted() && wallet->IsLocked()
151  Unlocked // wallet->IsCrypted() && !wallet->IsLocked()
152  };
153 
158 
160 
161  // Check address for validity
162  bool validateAddress(const QString &address);
163 
164  // Return status record for SendCoins, contains error id + information
166  {
167  SendCoinsReturn(StatusCode _status = OK, QString _reasonCommitFailed = "")
168  : status(_status),
169  reasonCommitFailed(_reasonCommitFailed)
170  {
171  }
174  };
175 
176  // prepare transaction for getting txfee before sending coins
177  SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const CCoinControl& coinControl);
178 
179  // Send coins to a list of recipients
181 
182  // Wallet encryption
183  bool setWalletEncrypted(bool encrypted, const SecureString &passphrase);
184  // Passphrase only needed when unlocking
185  bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
186  bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
187 
188  // RAI object for unlocking wallet, returned by requestUnlock()
190  {
191  public:
193  ~UnlockContext();
194 
195  bool isValid() const { return valid; }
196 
197  // Copy operator and constructor transfer the context
198  UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
199  UnlockContext& operator=(const UnlockContext& rhs) { CopyFrom(rhs); return *this; }
200  private:
202  bool valid;
203  mutable bool relock; // mutable, as it can be set to false by copying
204 
205  void CopyFrom(const UnlockContext& rhs);
206  };
207 
209 
210  void loadReceiveRequests(std::vector<std::string>& vReceiveRequests);
211  bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest);
212 
213  bool bumpFee(uint256 hash, uint256& new_hash);
214 
215  static bool isWalletEnabled();
216  bool privateKeysDisabled() const;
217 
218  interfaces::Node& node() const { return m_node; }
219  interfaces::Wallet& wallet() const { return *m_wallet; }
220 
221  QString getWalletName() const;
222 
223  bool isMultiwallet();
224 
226 private:
227  std::unique_ptr<interfaces::Wallet> m_wallet;
228  std::unique_ptr<interfaces::Handler> m_handler_unload;
229  std::unique_ptr<interfaces::Handler> m_handler_status_changed;
230  std::unique_ptr<interfaces::Handler> m_handler_address_book_changed;
231  std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
232  std::unique_ptr<interfaces::Handler> m_handler_show_progress;
233  std::unique_ptr<interfaces::Handler> m_handler_watch_only_changed;
235 
238 
239  // Wallet has an options model for wallet-specific options
240  // (transaction fee, for example)
242 
246 
247  // Cache some values to be able to detect changes
251 
252  QTimer *pollTimer;
253 
254  void subscribeToCoreSignals();
256  void checkBalanceChanged(const interfaces::WalletBalances& new_balances);
257 
258 Q_SIGNALS:
259  // Signal that balance in wallet changed
260  void balanceChanged(const interfaces::WalletBalances& balances);
261 
262  // Encryption status of wallet changed
264 
265  // Signal emitted when wallet needs to be unlocked
266  // It is valid behaviour for listeners to keep the wallet locked after this signal;
267  // this means that the unlocking failed or was cancelled.
268  void requireUnlock();
269 
270  // Fired when a message should be reported to the user
271  void message(const QString &title, const QString &message, unsigned int style);
272 
273  // Coins sent: from wallet, to recipient, in (serialized) transaction:
274  void coinsSent(WalletModel* wallet, SendCoinsRecipient recipient, QByteArray transaction);
275 
276  // Show progress dialog e.g. for rescan
277  void showProgress(const QString &title, int nProgress);
278 
279  // Watch-only address added
280  void notifyWatchonlyChanged(bool fHaveWatchonly);
281 
282  // Signal that wallet is about to be removed
283  void unload();
284 
285 public Q_SLOTS:
286  /* Wallet status might have changed */
287  void updateStatus();
288  /* New transaction, or transaction changed status */
289  void updateTransaction();
290  /* New, updated or removed address book entry */
291  void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
292  /* Watch-only added */
293  void updateWatchOnlyFlag(bool fHaveWatchonly);
294  /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
295  void pollBalanceChanged();
296 };
297 
298 #endif // BITCOIN_QT_WALLETMODEL_H
void loadReceiveRequests(std::vector< std::string > &vReceiveRequests)
Model for list of recently generated payment requests / bitcoin: URIs.
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:244
interfaces::Wallet & wallet() const
Definition: walletmodel.h:219
void coinsSent(WalletModel *wallet, SendCoinsRecipient recipient, QByteArray transaction)
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:245
UnlockContext & operator=(const UnlockContext &rhs)
Definition: walletmodel.h:199
std::unique_ptr< interfaces::Handler > m_handler_address_book_changed
Definition: walletmodel.h:230
static bool isWalletEnabled()
WalletModel(std::unique_ptr< interfaces::Wallet > wallet, interfaces::Node &node, const PlatformStyle *platformStyle, OptionsModel *optionsModel, QObject *parent=0)
Definition: walletmodel.cpp:35
static const int CURRENT_VERSION
Definition: walletmodel.h:85
UnlockContext requestUnlock()
std::unique_ptr< interfaces::Handler > m_handler_unload
Definition: walletmodel.h:228
void unsubscribeFromCoreSignals()
void unload()
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:56
bool SerializeToString(std::string *output) const
SendCoinsReturn sendCoins(WalletModelTransaction &transaction)
void showProgress(const QString &title, int nProgress)
std::unique_ptr< interfaces::Handler > m_handler_status_changed
Definition: walletmodel.h:229
SendCoinsRecipient(const QString &addr, const QString &_label, const CAmount &_amount, const QString &_message)
Definition: walletmodel.h:58
AddressTableModel * getAddressTableModel()
OutputType
Definition: outputtype.h:15
Coin Control Features.
Definition: coincontrol.h:16
void updateStatus()
Definition: walletmodel.cpp:62
EncryptionStatus getEncryptionStatus() const
bool bumpFee(uint256 hash, uint256 &new_hash)
UnlockContext(WalletModel *wallet, bool valid, bool relock)
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
void updateTransaction()
Collection of wallet balances.
Definition: wallet.h:310
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
An encapsulated public key.
Definition: pubkey.h:30
Interface for accessing a wallet.
Definition: wallet.h:46
bool privateKeysDisabled() const
std::unique_ptr< interfaces::Wallet > m_wallet
Definition: walletmodel.h:227
SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const CCoinControl &coinControl)
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:167
void encryptionStatusChanged()
OptionsModel * optionsModel
Definition: walletmodel.h:241
QString getWalletName() const
TransactionTableModel * getTransactionTableModel()
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:249
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:218
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletmodel.h:91
void CopyFrom(const UnlockContext &rhs)
UI model for the transaction table of a wallet.
bool parse(const QByteArray &data)
Qt model of the address book in the core.
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
std::unique_ptr< interfaces::Handler > m_handler_show_progress
Definition: walletmodel.h:232
UnlockContext(const UnlockContext &obj)
Definition: walletmodel.h:198
QTimer * pollTimer
Definition: walletmodel.h:252
bool validateAddress(const QString &address)
void updateWatchOnlyFlag(bool fHaveWatchonly)
256-bit opaque blob.
Definition: uint256.h:122
int cachedNumBlocks
Definition: walletmodel.h:250
void requireUnlock()
bool fForceCheckBalanceChanged
Definition: walletmodel.h:237
AddressTableModel * getAddressTableModel() const
Definition: walletmodel.h:225
RecentRequestsTableModel * getRecentRequestsTableModel()
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:29
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:125
interfaces::WalletBalances m_cached_balances
Definition: walletmodel.h:248
bool setWalletEncrypted(bool encrypted, const SecureString &passphrase)
interfaces::Node & m_node
Definition: walletmodel.h:234
void message(const QString &title, const QString &message, unsigned int style)
A reference to a CKey: the Hash360 of its serialized public key.
Definition: pubkey.h:20
void notifyWatchonlyChanged(bool fHaveWatchonly)
Data model for a walletmodel transaction.
AddressTableModel * addressTableModel
Definition: walletmodel.h:243
bool fSubtractFeeFromAmount
Definition: walletmodel.h:83
std::unique_ptr< interfaces::Handler > m_handler_transaction_changed
Definition: walletmodel.h:231
bool isMultiwallet()
bool fHaveWatchOnly
Definition: walletmodel.h:236
void checkBalanceChanged(const interfaces::WalletBalances &new_balances)
Definition: walletmodel.cpp:96
std::string sPaymentRequest
Definition: walletmodel.h:78
#define READWRITE(...)
Definition: serialize.h:173
bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest)
QString authenticatedMerchant
Definition: walletmodel.h:81
void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status)
Top-level interface for a bitcoin node (bsha3d process).
Definition: node.h:35
void balanceChanged(const interfaces::WalletBalances &balances)
std::unique_ptr< interfaces::Handler > m_handler_watch_only_changed
Definition: walletmodel.h:233
void pollBalanceChanged()
Definition: walletmodel.cpp:71
OptionsModel * getOptionsModel()
void subscribeToCoreSignals()