Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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