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