Dogecoin Core  1.14.2
P2P Digital Currency
walletmodel.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 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 "paymentrequestplus.h"
10 
12 
13 #include <map>
14 #include <vector>
15 
16 #include <QObject>
17 
18 class AddressTableModel;
19 class OptionsModel;
20 class PlatformStyle;
24 
25 class CCoinControl;
26 class CKeyID;
27 class COutPoint;
28 class COutput;
29 class CPubKey;
30 class CWallet;
31 class uint256;
32 
33 QT_BEGIN_NAMESPACE
34 class QTimer;
35 QT_END_NAMESPACE
36 
38 {
39 public:
41  explicit SendCoinsRecipient(const QString &addr, const QString &_label, const CAmount& _amount, const QString &_message):
42  address(addr), label(_label), amount(_amount), message(_message), fSubtractFeeFromAmount(false), nVersion(SendCoinsRecipient::CURRENT_VERSION) {}
43 
44  // If from an unauthenticated payment request, this is used for storing
45  // the addresses, e.g. address-A<br />address-B<br />address-C.
46  // Info: As we don't need to process addresses in here when using
47  // payment requests, we can abuse it for displaying an address list.
48  // Todo: This is a hack, should be replaced with a cleaner solution!
49  QString address;
50  QString label;
52  // If from a payment request, this is used for storing the memo
53  QString message;
54 
55  // If from a payment request, paymentRequest.IsInitialized() will be true
57  // Empty if no authentication or invalid signature/cert/etc.
59 
60  bool fSubtractFeeFromAmount; // memory only
61 
62  static const int CURRENT_VERSION = 1;
63  int nVersion;
64 
66 
67  template <typename Stream, typename Operation>
68  inline void SerializationOp(Stream& s, Operation ser_action) {
69  std::string sAddress = address.toStdString();
70  std::string sLabel = label.toStdString();
71  std::string sMessage = message.toStdString();
72  std::string sPaymentRequest;
73  if (!ser_action.ForRead() && paymentRequest.IsInitialized())
74  paymentRequest.SerializeToString(&sPaymentRequest);
75  std::string sAuthenticatedMerchant = authenticatedMerchant.toStdString();
76 
77  READWRITE(this->nVersion);
78  READWRITE(sAddress);
79  READWRITE(sLabel);
81  READWRITE(sMessage);
82  READWRITE(sPaymentRequest);
83  READWRITE(sAuthenticatedMerchant);
84 
85  if (ser_action.ForRead())
86  {
87  address = QString::fromStdString(sAddress);
88  label = QString::fromStdString(sLabel);
89  message = QString::fromStdString(sMessage);
90  if (!sPaymentRequest.empty())
91  paymentRequest.parse(QByteArray::fromRawData(sPaymentRequest.data(), sPaymentRequest.size()));
92  authenticatedMerchant = QString::fromStdString(sAuthenticatedMerchant);
93  }
94  }
95 };
96 
98 class WalletModel : public QObject
99 {
100  Q_OBJECT
101 
102 public:
103  explicit WalletModel(const PlatformStyle *platformStyle, CWallet *wallet, OptionsModel *optionsModel, QObject *parent = 0);
104  ~WalletModel();
105 
106  enum StatusCode // Returned by sendCoins
107  {
108  OK,
114  TransactionCreationFailed, // Error returned when wallet is still locked
118  };
119 
121  {
122  Unencrypted, // !wallet->IsCrypted()
123  Locked, // wallet->IsCrypted() && wallet->IsLocked()
124  Unlocked // wallet->IsCrypted() && !wallet->IsLocked()
125  };
126 
131 
132  CAmount getBalance(const CCoinControl *coinControl = NULL) const;
134  CAmount getImmatureBalance() const;
135  bool haveWatchOnly() const;
136  CAmount getWatchBalance() const;
140 
141  // Check address for validity
142  bool validateAddress(const QString &address);
143 
144  // Return status record for SendCoins, contains error id + information
146  {
147  SendCoinsReturn(StatusCode _status = OK, QString _reasonCommitFailed = "")
148  : status(_status),
149  reasonCommitFailed(_reasonCommitFailed)
150  {
151  }
154  };
155 
156  // prepare transaction for getting txfee before sending coins
157  SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const CCoinControl *coinControl = NULL);
158 
159  // Send coins to a list of recipients
161 
162  // Wallet encryption
163  bool setWalletEncrypted(bool encrypted, const SecureString &passphrase);
164  // Passphrase only needed when unlocking
165  bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
166  bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
167  // Wallet backup
168  bool backupWallet(const QString &filename);
169 
170  // RAI object for unlocking wallet, returned by requestUnlock()
172  {
173  public:
175  ~UnlockContext();
176 
177  bool isValid() const { return valid; }
178 
179  // Copy operator and constructor transfer the context
180  UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
181  UnlockContext& operator=(const UnlockContext& rhs) { CopyFrom(rhs); return *this; }
182  private:
184  bool valid;
185  mutable bool relock; // mutable, as it can be set to false by copying
186 
187  void CopyFrom(const UnlockContext& rhs);
188  };
189 
191 
192  bool getPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
193  bool havePrivKey(const CKeyID &address) const;
194  bool getPrivKey(const CKeyID &address, CKey& vchPrivKeyOut) const;
195  void getOutputs(const std::vector<COutPoint>& vOutpoints, std::vector<COutput>& vOutputs);
196  bool isSpent(const COutPoint& outpoint) const;
197  void listCoins(std::map<QString, std::vector<COutput> >& mapCoins) const;
198 
199  bool isLockedCoin(uint256 hash, unsigned int n) const;
200  void lockCoin(COutPoint& output);
201  void unlockCoin(COutPoint& output);
202  void listLockedCoins(std::vector<COutPoint>& vOutpts);
203 
204  void loadReceiveRequests(std::vector<std::string>& vReceiveRequests);
205  bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest);
206 
207  bool transactionCanBeAbandoned(uint256 hash) const;
208  bool abandonTransaction(uint256 hash) const;
209 
210  static bool isWalletEnabled();
211 
212  bool hdEnabled() const;
213 
214  int getDefaultConfirmTarget() const;
215 
216 private:
220 
221  // Wallet has an options model for wallet-specific options
222  // (transaction fee, for example)
224 
228 
229  // Cache some values to be able to detect changes
238 
239  QTimer *pollTimer;
240 
241  void subscribeToCoreSignals();
243  void checkBalanceChanged();
244 
245 Q_SIGNALS:
246  // Signal that balance in wallet changed
247  void balanceChanged(const CAmount& balance, const CAmount& unconfirmedBalance, const CAmount& immatureBalance,
248  const CAmount& watchOnlyBalance, const CAmount& watchUnconfBalance, const CAmount& watchImmatureBalance);
249 
250  // Encryption status of wallet changed
251  void encryptionStatusChanged(int status);
252 
253  // Signal emitted when wallet needs to be unlocked
254  // It is valid behaviour for listeners to keep the wallet locked after this signal;
255  // this means that the unlocking failed or was cancelled.
257 
258  // Fired when a message should be reported to the user
259  void message(const QString &title, const QString &message, unsigned int style);
260 
261  // Coins sent: from wallet, to recipient, in (serialized) transaction:
262  void coinsSent(CWallet* wallet, SendCoinsRecipient recipient, QByteArray transaction);
263 
264  // Show progress dialog e.g. for rescan
265  void showProgress(const QString &title, int nProgress);
266 
267  // Watch-only address added
268  void notifyWatchonlyChanged(bool fHaveWatchonly);
269 
270 public Q_SLOTS:
271  /* Wallet status might have changed */
272  void updateStatus();
273  /* New transaction, or transaction changed status */
274  void updateTransaction();
275  /* New, updated or removed address book entry */
276  void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
277  /* Watch-only added */
278  void updateWatchOnlyFlag(bool fHaveWatchonly);
279  /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
280  void pollBalanceChanged();
281 };
282 
283 #endif // BITCOIN_QT_WALLETMODEL_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:15
Qt model of the address book in the core.
Coin Control Features.
Definition: coincontrol.h:12
An encapsulated private key.
Definition: key.h:36
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:30
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
An encapsulated public key.
Definition: pubkey.h:40
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,...
Definition: wallet.h:493
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:23
bool parse(const QByteArray &data)
bool SerializeToString(std::string *output) const
Model for list of recently generated payment requests / bitcoin: URIs.
PaymentRequestPlus paymentRequest
Definition: walletmodel.h:56
static const int CURRENT_VERSION
Definition: walletmodel.h:62
bool fSubtractFeeFromAmount
Definition: walletmodel.h:60
SendCoinsRecipient(const QString &addr, const QString &_label, const CAmount &_amount, const QString &_message)
Definition: walletmodel.h:41
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletmodel.h:68
QString authenticatedMerchant
Definition: walletmodel.h:58
UI model for the transaction table of a wallet.
UnlockContext(WalletModel *wallet, bool valid, bool relock)
void CopyFrom(const UnlockContext &rhs)
UnlockContext & operator=(const UnlockContext &rhs)
Definition: walletmodel.h:181
UnlockContext(const UnlockContext &obj)
Definition: walletmodel.h:180
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:99
CAmount cachedWatchUnconfBalance
Definition: walletmodel.h:234
CAmount cachedImmatureBalance
Definition: walletmodel.h:232
OptionsModel * optionsModel
Definition: walletmodel.h:223
bool validateAddress(const QString &address)
AddressTableModel * addressTableModel
Definition: walletmodel.h:225
void unlockCoin(COutPoint &output)
void loadReceiveRequests(std::vector< std::string > &vReceiveRequests)
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:236
bool hdEnabled() const
CAmount cachedBalance
Definition: walletmodel.h:230
WalletModel(const PlatformStyle *platformStyle, CWallet *wallet, OptionsModel *optionsModel, QObject *parent=0)
Definition: walletmodel.cpp:34
void listLockedCoins(std::vector< COutPoint > &vOutpts)
bool transactionCanBeAbandoned(uint256 hash) const
bool getPrivKey(const CKeyID &address, CKey &vchPrivKeyOut) const
bool setWalletEncrypted(bool encrypted, const SecureString &passphrase)
CAmount getBalance(const CCoinControl *coinControl=NULL) const
Definition: walletmodel.cpp:62
void encryptionStatusChanged(int status)
CAmount getUnconfirmedBalance() const
Definition: walletmodel.cpp:79
void pollBalanceChanged()
void getOutputs(const std::vector< COutPoint > &vOutpoints, std::vector< COutput > &vOutputs)
SendCoinsReturn sendCoins(WalletModelTransaction &transaction)
void balanceChanged(const CAmount &balance, const CAmount &unconfirmedBalance, const CAmount &immatureBalance, const CAmount &watchOnlyBalance, const CAmount &watchUnconfBalance, const CAmount &watchImmatureBalance)
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:227
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:226
void notifyWatchonlyChanged(bool fHaveWatchonly)
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
CAmount getWatchUnconfirmedBalance() const
Definition: walletmodel.cpp:99
bool havePrivKey(const CKeyID &address) const
CAmount cachedWatchImmatureBalance
Definition: walletmodel.h:235
void showProgress(const QString &title, int nProgress)
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
CAmount getWatchBalance() const
Definition: walletmodel.cpp:94
void message(const QString &title, const QString &message, unsigned int style)
CAmount cachedWatchOnlyBalance
Definition: walletmodel.h:233
bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest)
void updateStatus()
AddressTableModel * getAddressTableModel()
SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const CCoinControl *coinControl=NULL)
CAmount cachedUnconfirmedBalance
Definition: walletmodel.h:231
OptionsModel * getOptionsModel()
void lockCoin(COutPoint &output)
CAmount getWatchImmatureBalance() const
bool abandonTransaction(uint256 hash) const
bool backupWallet(const QString &filename)
EncryptionStatus getEncryptionStatus() const
RecentRequestsTableModel * getRecentRequestsTableModel()
bool fForceCheckBalanceChanged
Definition: walletmodel.h:219
bool haveWatchOnly() const
Definition: walletmodel.cpp:89
CWallet * wallet
Definition: walletmodel.h:217
bool getPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
bool isLockedCoin(uint256 hash, unsigned int n) const
CAmount getImmatureBalance() const
Definition: walletmodel.cpp:84
QTimer * pollTimer
Definition: walletmodel.h:239
void unsubscribeFromCoreSignals()
bool isSpent(const COutPoint &outpoint) const
void requireUnlock()
void updateTransaction()
void coinsSent(CWallet *wallet, SendCoinsRecipient recipient, QByteArray transaction)
void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status)
bool fHaveWatchOnly
Definition: walletmodel.h:218
void checkBalanceChanged()
void updateWatchOnlyFlag(bool fHaveWatchonly)
void listCoins(std::map< QString, std::vector< COutput > > &mapCoins) const
UnlockContext requestUnlock()
int cachedNumBlocks
Definition: walletmodel.h:237
static bool isWalletEnabled()
@ AmountWithFeeExceedsBalance
Definition: walletmodel.h:112
@ TransactionCreationFailed
Definition: walletmodel.h:114
@ AmountExceedsBalance
Definition: walletmodel.h:111
@ TransactionCommitFailed
Definition: walletmodel.h:115
@ PaymentRequestExpired
Definition: walletmodel.h:117
int getDefaultConfirmTarget() const
void subscribeToCoreSignals()
TransactionTableModel * getTransactionTableModel()
Data model for a walletmodel transaction.
256-bit opaque blob.
Definition: uint256.h:123
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:56
#define READWRITE(obj)
Definition: serialize.h:151
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:147