Merge #11676: contrib/init: Update openrc-run filename
[bitcoinplatinum.git] / src / qt / walletmodel.h
blob05733f82727a6be6d695685e71297e5deb98c6a4
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.
5 #ifndef BITCOIN_QT_WALLETMODEL_H
6 #define BITCOIN_QT_WALLETMODEL_H
8 #include "paymentrequestplus.h"
9 #include "walletmodeltransaction.h"
11 #include "support/allocators/secure.h"
13 #include <map>
14 #include <vector>
16 #include <QObject>
18 class AddressTableModel;
19 class OptionsModel;
20 class PlatformStyle;
21 class RecentRequestsTableModel;
22 class TransactionTableModel;
23 class WalletModelTransaction;
25 class CCoinControl;
26 class CKeyID;
27 class COutPoint;
28 class COutput;
29 class CPubKey;
30 class CWallet;
31 class uint256;
33 QT_BEGIN_NAMESPACE
34 class QTimer;
35 QT_END_NAMESPACE
37 class SendCoinsRecipient
39 public:
40 explicit SendCoinsRecipient() : amount(0), fSubtractFeeFromAmount(false), nVersion(SendCoinsRecipient::CURRENT_VERSION) { }
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) {}
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;
51 CAmount amount;
52 // If from a payment request, this is used for storing the memo
53 QString message;
55 // If from a payment request, paymentRequest.IsInitialized() will be true
56 PaymentRequestPlus paymentRequest;
57 // Empty if no authentication or invalid signature/cert/etc.
58 QString authenticatedMerchant;
60 bool fSubtractFeeFromAmount; // memory only
62 static const int CURRENT_VERSION = 1;
63 int nVersion;
65 ADD_SERIALIZE_METHODS;
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();
77 READWRITE(this->nVersion);
78 READWRITE(sAddress);
79 READWRITE(sLabel);
80 READWRITE(amount);
81 READWRITE(sMessage);
82 READWRITE(sPaymentRequest);
83 READWRITE(sAuthenticatedMerchant);
85 if (ser_action.ForRead())
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);
97 /** Interface to Bitcoin wallet from Qt view code. */
98 class WalletModel : public QObject
100 Q_OBJECT
102 public:
103 explicit WalletModel(const PlatformStyle *platformStyle, CWallet *wallet, OptionsModel *optionsModel, QObject *parent = 0);
104 ~WalletModel();
106 enum StatusCode // Returned by sendCoins
109 InvalidAmount,
110 InvalidAddress,
111 AmountExceedsBalance,
112 AmountWithFeeExceedsBalance,
113 DuplicateAddress,
114 TransactionCreationFailed, // Error returned when wallet is still locked
115 TransactionCommitFailed,
116 AbsurdFee,
117 PaymentRequestExpired
120 enum EncryptionStatus
122 Unencrypted, // !wallet->IsCrypted()
123 Locked, // wallet->IsCrypted() && wallet->IsLocked()
124 Unlocked // wallet->IsCrypted() && !wallet->IsLocked()
127 OptionsModel *getOptionsModel();
128 AddressTableModel *getAddressTableModel();
129 TransactionTableModel *getTransactionTableModel();
130 RecentRequestsTableModel *getRecentRequestsTableModel();
132 CAmount getBalance(const CCoinControl *coinControl = nullptr) const;
133 CAmount getUnconfirmedBalance() const;
134 CAmount getImmatureBalance() const;
135 bool haveWatchOnly() const;
136 CAmount getWatchBalance() const;
137 CAmount getWatchUnconfirmedBalance() const;
138 CAmount getWatchImmatureBalance() const;
139 EncryptionStatus getEncryptionStatus() const;
141 // Check address for validity
142 bool validateAddress(const QString &address);
144 // Return status record for SendCoins, contains error id + information
145 struct SendCoinsReturn
147 SendCoinsReturn(StatusCode _status = OK, QString _reasonCommitFailed = "")
148 : status(_status),
149 reasonCommitFailed(_reasonCommitFailed)
152 StatusCode status;
153 QString reasonCommitFailed;
156 // prepare transaction for getting txfee before sending coins
157 SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const CCoinControl& coinControl);
159 // Send coins to a list of recipients
160 SendCoinsReturn sendCoins(WalletModelTransaction &transaction);
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);
170 // RAI object for unlocking wallet, returned by requestUnlock()
171 class UnlockContext
173 public:
174 UnlockContext(WalletModel *wallet, bool valid, bool relock);
175 ~UnlockContext();
177 bool isValid() const { return valid; }
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:
183 WalletModel *wallet;
184 bool valid;
185 mutable bool relock; // mutable, as it can be set to false by copying
187 void CopyFrom(const UnlockContext& rhs);
190 UnlockContext requestUnlock();
192 bool getPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
193 bool IsSpendable(const CTxDestination& dest) 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;
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);
204 void loadReceiveRequests(std::vector<std::string>& vReceiveRequests);
205 bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest);
207 bool transactionCanBeAbandoned(uint256 hash) const;
208 bool abandonTransaction(uint256 hash) const;
210 bool transactionCanBeBumped(uint256 hash) const;
211 bool bumpFee(uint256 hash);
213 static bool isWalletEnabled();
215 bool hdEnabled() const;
217 int getDefaultConfirmTarget() const;
219 bool getDefaultWalletRbf() const;
221 private:
222 CWallet *wallet;
223 bool fHaveWatchOnly;
224 bool fForceCheckBalanceChanged;
226 // Wallet has an options model for wallet-specific options
227 // (transaction fee, for example)
228 OptionsModel *optionsModel;
230 AddressTableModel *addressTableModel;
231 TransactionTableModel *transactionTableModel;
232 RecentRequestsTableModel *recentRequestsTableModel;
234 // Cache some values to be able to detect changes
235 CAmount cachedBalance;
236 CAmount cachedUnconfirmedBalance;
237 CAmount cachedImmatureBalance;
238 CAmount cachedWatchOnlyBalance;
239 CAmount cachedWatchUnconfBalance;
240 CAmount cachedWatchImmatureBalance;
241 EncryptionStatus cachedEncryptionStatus;
242 int cachedNumBlocks;
244 QTimer *pollTimer;
246 void subscribeToCoreSignals();
247 void unsubscribeFromCoreSignals();
248 void checkBalanceChanged();
250 Q_SIGNALS:
251 // Signal that balance in wallet changed
252 void balanceChanged(const CAmount& balance, const CAmount& unconfirmedBalance, const CAmount& immatureBalance,
253 const CAmount& watchOnlyBalance, const CAmount& watchUnconfBalance, const CAmount& watchImmatureBalance);
255 // Encryption status of wallet changed
256 void encryptionStatusChanged(int status);
258 // Signal emitted when wallet needs to be unlocked
259 // It is valid behaviour for listeners to keep the wallet locked after this signal;
260 // this means that the unlocking failed or was cancelled.
261 void requireUnlock();
263 // Fired when a message should be reported to the user
264 void message(const QString &title, const QString &message, unsigned int style);
266 // Coins sent: from wallet, to recipient, in (serialized) transaction:
267 void coinsSent(CWallet* wallet, SendCoinsRecipient recipient, QByteArray transaction);
269 // Show progress dialog e.g. for rescan
270 void showProgress(const QString &title, int nProgress);
272 // Watch-only address added
273 void notifyWatchonlyChanged(bool fHaveWatchonly);
275 public Q_SLOTS:
276 /* Wallet status might have changed */
277 void updateStatus();
278 /* New transaction, or transaction changed status */
279 void updateTransaction();
280 /* New, updated or removed address book entry */
281 void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
282 /* Watch-only added */
283 void updateWatchOnlyFlag(bool fHaveWatchonly);
284 /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
285 void pollBalanceChanged();
288 #endif // BITCOIN_QT_WALLETMODEL_H