PageRenderTime 120ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/src/qt/walletmodel.h

https://bitbucket.org/r_development82/nxton-core
C Header | 298 lines | 204 code | 52 blank | 42 comment | 4 complexity | 6a430ba7a8404c4657dd2ab62a0cf3c5 MD5 | raw file
Possible License(s): MIT, 0BSD, GPL-3.0, BSD-3-Clause
  1. // Copyright (c) 2011-2014 The Bitcoin developers
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef BITCOIN_QT_WALLETMODEL_H
  5. #define BITCOIN_QT_WALLETMODEL_H
  6. #include "paymentrequestplus.h"
  7. #include "walletmodeltransaction.h"
  8. #include "allocators.h" /* for SecureString */
  9. #include "swiftnode.h"
  10. #include "wallet.h"
  11. #include <map>
  12. #include <vector>
  13. #include <QObject>
  14. class AddressTableModel;
  15. class OptionsModel;
  16. class RecentRequestsTableModel;
  17. class TransactionTableModel;
  18. class WalletModelTransaction;
  19. class CCoinControl;
  20. class CKeyID;
  21. class COutPoint;
  22. class COutput;
  23. class CPubKey;
  24. class CWallet;
  25. class uint256;
  26. QT_BEGIN_NAMESPACE
  27. class QTimer;
  28. QT_END_NAMESPACE
  29. class SendCoinsRecipient
  30. {
  31. public:
  32. explicit SendCoinsRecipient() : amount(0), nVersion(SendCoinsRecipient::CURRENT_VERSION) {}
  33. explicit SendCoinsRecipient(const QString& addr, const QString& label, const CAmount& amount, const QString& message) : address(addr), label(label), amount(amount), message(message), nVersion(SendCoinsRecipient::CURRENT_VERSION) {}
  34. // If from an insecure payment request, this is used for storing
  35. // the addresses, e.g. address-A<br />address-B<br />address-C.
  36. // Info: As we don't need to process addresses in here when using
  37. // payment requests, we can abuse it for displaying an address list.
  38. // Todo: This is a hack, should be replaced with a cleaner solution!
  39. QString address;
  40. QString label;
  41. AvailableCoinsType inputType;
  42. bool useSwiftNode;
  43. CAmount amount;
  44. // If from a payment request, this is used for storing the memo
  45. QString message;
  46. // If from a payment request, paymentRequest.IsInitialized() will be true
  47. PaymentRequestPlus paymentRequest;
  48. // Empty if no authentication or invalid signature/cert/etc.
  49. QString authenticatedMerchant;
  50. static const int CURRENT_VERSION = 1;
  51. int nVersion;
  52. ADD_SERIALIZE_METHODS;
  53. template <typename Stream, typename Operation>
  54. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
  55. {
  56. std::string sAddress = address.toStdString();
  57. std::string sLabel = label.toStdString();
  58. std::string sMessage = message.toStdString();
  59. std::string sPaymentRequest;
  60. if (!ser_action.ForRead() && paymentRequest.IsInitialized())
  61. paymentRequest.SerializeToString(&sPaymentRequest);
  62. std::string sAuthenticatedMerchant = authenticatedMerchant.toStdString();
  63. READWRITE(this->nVersion);
  64. nVersion = this->nVersion;
  65. READWRITE(sAddress);
  66. READWRITE(sLabel);
  67. READWRITE(amount);
  68. READWRITE(sMessage);
  69. READWRITE(sPaymentRequest);
  70. READWRITE(sAuthenticatedMerchant);
  71. if (ser_action.ForRead()) {
  72. address = QString::fromStdString(sAddress);
  73. label = QString::fromStdString(sLabel);
  74. message = QString::fromStdString(sMessage);
  75. if (!sPaymentRequest.empty())
  76. paymentRequest.parse(QByteArray::fromRawData(sPaymentRequest.data(), sPaymentRequest.size()));
  77. authenticatedMerchant = QString::fromStdString(sAuthenticatedMerchant);
  78. }
  79. }
  80. };
  81. /** Interface to Bitcoin wallet from Qt view code. */
  82. class WalletModel : public QObject
  83. {
  84. Q_OBJECT
  85. public:
  86. explicit WalletModel(CWallet* wallet, OptionsModel* optionsModel, QObject* parent = 0);
  87. ~WalletModel();
  88. enum StatusCode // Returned by sendCoins
  89. {
  90. OK,
  91. InvalidAmount,
  92. InvalidAddress,
  93. AmountExceedsBalance,
  94. AmountWithFeeExceedsBalance,
  95. DuplicateAddress,
  96. TransactionCreationFailed, // Error returned when wallet is still locked
  97. TransactionCommitFailed,
  98. AnonymizeOnlyUnlocked,
  99. InsaneFee
  100. };
  101. enum EncryptionStatus {
  102. Unencrypted, // !wallet->IsCrypted()
  103. Locked, // wallet->IsCrypted() && wallet->IsLocked()
  104. Unlocked, // wallet->IsCrypted() && !wallet->IsLocked()
  105. UnlockedForAnonymizationOnly // wallet->IsCrypted() && !wallet->IsLocked() && wallet->fWalletUnlockAnonymizeOnly
  106. };
  107. OptionsModel* getOptionsModel();
  108. AddressTableModel* getAddressTableModel();
  109. TransactionTableModel* getTransactionTableModel();
  110. RecentRequestsTableModel* getRecentRequestsTableModel();
  111. CAmount getBalance(const CCoinControl* coinControl = NULL) const;
  112. CAmount getUnconfirmedBalance() const;
  113. CAmount getImmatureBalance() const;
  114. CAmount getLockedBalance() const;
  115. CAmount getZerocoinBalance() const;
  116. CAmount getUnconfirmedZerocoinBalance() const;
  117. CAmount getImmatureZerocoinBalance() const;
  118. bool haveWatchOnly() const;
  119. CAmount getWatchBalance() const;
  120. CAmount getWatchUnconfirmedBalance() const;
  121. CAmount getWatchImmatureBalance() const;
  122. EncryptionStatus getEncryptionStatus() const;
  123. CKey generateNewKey() const; //for temporary paper wallet key generation
  124. bool setAddressBook(const CTxDestination& address, const string& strName, const string& strPurpose);
  125. void encryptKey(const CKey key, const std::string& pwd, const std::string& slt, std::vector<unsigned char>& crypted);
  126. void decryptKey(const std::vector<unsigned char>& crypted, const std::string& slt, const std::string& pwd, CKey& key);
  127. void emitBalanceChanged(); // Force update of UI-elements even when no values have changed
  128. // Check address for validity
  129. bool validateAddress(const QString& address);
  130. // Return status record for SendCoins, contains error id + information
  131. struct SendCoinsReturn {
  132. SendCoinsReturn(StatusCode status = OK) : status(status) {}
  133. StatusCode status;
  134. };
  135. // prepare transaction for getting txfee before sending coins
  136. SendCoinsReturn prepareTransaction(WalletModelTransaction& transaction, const CCoinControl* coinControl = NULL);
  137. // Send coins to a list of recipients
  138. SendCoinsReturn sendCoins(WalletModelTransaction& transaction);
  139. // Wallet encryption
  140. bool setWalletEncrypted(bool encrypted, const SecureString& passphrase);
  141. // Passphrase only needed when unlocking
  142. bool setWalletLocked(bool locked, const SecureString& passPhrase = SecureString(), bool anonymizeOnly = false);
  143. bool changePassphrase(const SecureString& oldPass, const SecureString& newPass);
  144. // Is wallet unlocked for anonymization only?
  145. bool isAnonymizeOnlyUnlocked();
  146. // Wallet backup
  147. bool backupWallet(const QString& filename);
  148. // RAI object for unlocking wallet, returned by requestUnlock()
  149. class UnlockContext
  150. {
  151. public:
  152. UnlockContext(bool valid, bool relock);
  153. ~UnlockContext();
  154. bool isValid() const { return valid; }
  155. // Copy operator and constructor transfer the context
  156. UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
  157. UnlockContext& operator=(const UnlockContext& rhs)
  158. {
  159. CopyFrom(rhs);
  160. return *this;
  161. }
  162. private:
  163. bool valid;
  164. mutable bool relock; // mutable, as it can be set to false by copying
  165. void CopyFrom(const UnlockContext& rhs);
  166. };
  167. UnlockContext requestUnlock(bool relock = false);
  168. bool getPubKey(const CKeyID& address, CPubKey& vchPubKeyOut) const;
  169. bool isMine(CBitcoinAddress address);
  170. void getOutputs(const std::vector<COutPoint>& vOutpoints, std::vector<COutput>& vOutputs);
  171. bool isSpent(const COutPoint& outpoint) const;
  172. void listCoins(std::map<QString, std::vector<COutput> >& mapCoins) const;
  173. bool isLockedCoin(uint256 hash, unsigned int n) const;
  174. void lockCoin(COutPoint& output);
  175. void unlockCoin(COutPoint& output);
  176. void listLockedCoins(std::vector<COutPoint>& vOutpts);
  177. void listZerocoinMints(std::list<CZerocoinMint>& listMints, bool fUnusedOnly = false, bool fMaturedOnly = false, bool fUpdateStatus = false);
  178. void loadReceiveRequests(std::vector<std::string>& vReceiveRequests);
  179. bool saveReceiveRequest(const std::string& sAddress, const int64_t nId, const std::string& sRequest);
  180. private:
  181. CWallet* wallet;
  182. bool fHaveWatchOnly;
  183. bool fHaveMultiSig;
  184. bool fForceCheckBalanceChanged;
  185. // Wallet has an options model for wallet-specific options
  186. // (transaction fee, for example)
  187. OptionsModel* optionsModel;
  188. AddressTableModel* addressTableModel;
  189. TransactionTableModel* transactionTableModel;
  190. RecentRequestsTableModel* recentRequestsTableModel;
  191. // Cache some values to be able to detect changes
  192. CAmount cachedBalance;
  193. CAmount cachedUnconfirmedBalance;
  194. CAmount cachedImmatureBalance;
  195. CAmount cachedZerocoinBalance;
  196. CAmount cachedUnconfirmedZerocoinBalance;
  197. CAmount cachedImmatureZerocoinBalance;
  198. CAmount cachedWatchOnlyBalance;
  199. CAmount cachedWatchUnconfBalance;
  200. CAmount cachedWatchImmatureBalance;
  201. EncryptionStatus cachedEncryptionStatus;
  202. int cachedNumBlocks;
  203. int cachedTxLocks;
  204. int cachedZeromintPercentage;
  205. QTimer* pollTimer;
  206. void subscribeToCoreSignals();
  207. void unsubscribeFromCoreSignals();
  208. void checkBalanceChanged();
  209. signals:
  210. // Signal that balance in wallet changed
  211. void balanceChanged(const CAmount& balance, const CAmount& unconfirmedBalance, const CAmount& immatureBalance,
  212. const CAmount& zerocoinBalance, const CAmount& unconfirmedZerocoinBalance, const CAmount& immatureZerocoinBalance,
  213. const CAmount& watchOnlyBalance, const CAmount& watchUnconfBalance, const CAmount& watchImmatureBalance);
  214. // Encryption status of wallet changed
  215. void encryptionStatusChanged(int status);
  216. // Signal emitted when wallet needs to be unlocked
  217. // It is valid behaviour for listeners to keep the wallet locked after this signal;
  218. // this means that the unlocking failed or was cancelled.
  219. void requireUnlock();
  220. // Fired when a message should be reported to the user
  221. void message(const QString& title, const QString& message, unsigned int style);
  222. // Coins sent: from wallet, to recipient, in (serialized) transaction:
  223. void coinsSent(CWallet* wallet, SendCoinsRecipient recipient, QByteArray transaction);
  224. // Show progress dialog e.g. for rescan
  225. void showProgress(const QString& title, int nProgress);
  226. // Watch-only address added
  227. void notifyWatchonlyChanged(bool fHaveWatchonly);
  228. // MultiSig address added
  229. void notifyMultiSigChanged(bool fHaveMultiSig);
  230. public slots:
  231. /* Wallet status might have changed */
  232. void updateStatus();
  233. /* New transaction, or transaction changed status */
  234. void updateTransaction();
  235. /* New, updated or removed address book entry */
  236. void updateAddressBook(const QString& address, const QString& label, bool isMine, const QString& purpose, int status);
  237. /* Zerocoin update */
  238. void updateAddressBook(const QString &pubCoin, const QString &isUsed, int status);
  239. /* Watch-only added */
  240. void updateWatchOnlyFlag(bool fHaveWatchonly);
  241. /* MultiSig added */
  242. void updateMultiSigFlag(bool fHaveMultiSig);
  243. /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
  244. void pollBalanceChanged();
  245. };
  246. #endif // BITCOIN_QT_WALLETMODEL_H