Fabcoin Core  0.16.2
P2P Digital Currency
walletmodel.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2017 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 FABCOIN_QT_WALLETMODEL_H
6 #define FABCOIN_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 class TokenItemModel;
26 class ContractTableModel;
27 
28 class CCoinControl;
29 class CKeyID;
30 class COutPoint;
31 class COutput;
32 class CPubKey;
33 class CWallet;
34 class CTokenInfo;
35 class CTokenTx;
36 class uint256;
37 
38 QT_BEGIN_NAMESPACE
39 class QTimer;
40 QT_END_NAMESPACE
41 
43 {
44 public:
46  explicit SendCoinsRecipient(const QString &addr, const QString &_label, const CAmount& _amount, const QString &_message):
47  address(addr), label(_label), amount(_amount), message(_message), fSubtractFeeFromAmount(false), nVersion(SendCoinsRecipient::CURRENT_VERSION) {}
48 
49  // If from an unauthenticated payment request, this is used for storing
50  // the addresses, e.g. address-A<br />address-B<br />address-C.
51  // Info: As we don't need to process addresses in here when using
52  // payment requests, we can abuse it for displaying an address list.
53  // Todo: This is a hack, should be replaced with a cleaner solution!
54  QString address;
55  QString label;
57  // If from a payment request, this is used for storing the memo
58  QString message;
59 
60  // If from a payment request, paymentRequest.IsInitialized() will be true
62  // Empty if no authentication or invalid signature/cert/etc.
64 
65  bool fSubtractFeeFromAmount; // memory only
66 
67  static const int CURRENT_VERSION = 1;
68  int nVersion;
69 
71 
72  template <typename Stream, typename Operation>
73  inline void SerializationOp(Stream& s, Operation ser_action) {
74  std::string sAddress = address.toStdString();
75  std::string sLabel = label.toStdString();
76  std::string sMessage = message.toStdString();
77  std::string sPaymentRequest;
78  if (!ser_action.ForRead() && paymentRequest.IsInitialized())
79  paymentRequest.SerializeToString(&sPaymentRequest);
80  std::string sAuthenticatedMerchant = authenticatedMerchant.toStdString();
81 
82  READWRITE(this->nVersion);
83  READWRITE(sAddress);
84  READWRITE(sLabel);
85  READWRITE(amount);
86  READWRITE(sMessage);
87  READWRITE(sPaymentRequest);
88  READWRITE(sAuthenticatedMerchant);
89 
90  if (ser_action.ForRead())
91  {
92  address = QString::fromStdString(sAddress);
93  label = QString::fromStdString(sLabel);
94  message = QString::fromStdString(sMessage);
95  if (!sPaymentRequest.empty())
96  paymentRequest.parse(QByteArray::fromRawData(sPaymentRequest.data(), sPaymentRequest.size()));
97  authenticatedMerchant = QString::fromStdString(sAuthenticatedMerchant);
98  }
99  }
100 };
101 
103 class WalletModel : public QObject
104 {
105  Q_OBJECT
106 
107 public:
108  explicit WalletModel(const PlatformStyle *platformStyle, CWallet *wallet, OptionsModel *optionsModel, QObject *parent = 0);
109  ~WalletModel();
110 
111  enum StatusCode // Returned by sendCoins
112  {
113  OK,
119  TransactionCreationFailed, // Error returned when wallet is still locked
122  PaymentRequestExpired
123  };
124 
126  {
127  Unencrypted, // !wallet->IsCrypted()
128  Locked, // wallet->IsCrypted() && wallet->IsLocked()
129  Unlocked // wallet->IsCrypted() && !wallet->IsLocked()
130  };
131 
132  OptionsModel *getOptionsModel();
133  AddressTableModel *getAddressTableModel();
134  ContractTableModel *getContractTableModel();
135  TransactionTableModel *getTransactionTableModel();
136  RecentRequestsTableModel *getRecentRequestsTableModel();
137  TokenItemModel *getTokenItemModel();
138  TokenTransactionTableModel *getTokenTransactionTableModel();
139 
140  CAmount getBalance(const CCoinControl *coinControl = nullptr) const;
141  CAmount getUnconfirmedBalance() const;
142  CAmount getImmatureBalance() const;
143  CAmount getStake() const;
144  bool haveWatchOnly() const;
145  CAmount getWatchBalance() const;
146  CAmount getWatchUnconfirmedBalance() const;
147  CAmount getWatchImmatureBalance() const;
148  CAmount getWatchStake() const;
149  EncryptionStatus getEncryptionStatus() const;
150 
151  // Check address for validity
152  bool validateAddress(const QString &address);
153 
154  // Return status record for SendCoins, contains error id + information
156  {
157  SendCoinsReturn(StatusCode _status = OK, QString _reasonCommitFailed = "")
158  : status(_status),
159  reasonCommitFailed(_reasonCommitFailed)
160  {
161  }
164  };
165 
166  // prepare transaction for getting txfee before sending coins
167  SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const CCoinControl& coinControl);
168 
169  // Send coins to a list of recipients
170  SendCoinsReturn sendCoins(WalletModelTransaction &transaction);
171 
172  // Wallet encryption
173  bool setWalletEncrypted(bool encrypted, const SecureString &passphrase);
174  // Passphrase only needed when unlocking
175  bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
176  bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
177  // Wallet backup
178  bool backupWallet(const QString &filename);
179  // Has wallet backup
180  bool hasWalletBackup();
181  // Restore backup
182  bool restoreWallet(const QString &filename, const QString &param);
183 
184  // RAI object for unlocking wallet, returned by requestUnlock()
186  {
187  public:
188  UnlockContext(WalletModel *wallet, bool valid, bool relock);
189  ~UnlockContext();
190 
191  bool isValid() const { return valid; }
192 
193  // Copy operator and constructor transfer the context
194  UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
195  UnlockContext& operator=(const UnlockContext& rhs) { CopyFrom(rhs); return *this; }
196  private:
198  bool valid;
199  mutable bool relock; // mutable, as it can be set to false by copying
201 
202  void CopyFrom(const UnlockContext& rhs);
203  };
204 
205  UnlockContext requestUnlock();
206 
207  bool getPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
208  bool IsSpendable(const CTxDestination& dest) const;
209  bool getPrivKey(const CKeyID &address, CKey& vchPrivKeyOut) const;
210  void getOutputs(const std::vector<COutPoint>& vOutpoints, std::vector<COutput>& vOutputs);
211  bool isSpent(const COutPoint& outpoint) const;
212  bool isUnspentAddress(const std::string& address) const;
213  void listCoins(std::map<QString, std::vector<COutput> >& mapCoins) const;
214 
215  bool isLockedCoin(uint256 hash, unsigned int n) const;
216  void lockCoin(COutPoint& output);
217  void unlockCoin(COutPoint& output);
218  void listLockedCoins(std::vector<COutPoint>& vOutpts);
219 
220  void loadReceiveRequests(std::vector<std::string>& vReceiveRequests);
221  bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest);
222 
223  bool transactionCanBeAbandoned(uint256 hash) const;
224  bool abandonTransaction(uint256 hash) const;
225 
226  bool transactionCanBeBumped(uint256 hash) const;
227  bool bumpFee(uint256 hash);
228 
229  static bool isWalletEnabled();
230 
231  bool hdEnabled() const;
232 
233  int getDefaultConfirmTarget() const;
234 
235  bool getDefaultWalletRbf() const;
236 
237  bool addTokenEntry(const CTokenInfo& token);
238 
239  bool addTokenTxEntry(const CTokenTx& tokenTx, bool fFlushOnClose=true);
240 
241  bool existTokenEntry(const CTokenInfo& token);
242 
243  bool removeTokenEntry(const std::string& sHash);
244 
245  QString getRestorePath();
246  QString getRestoreParam();
247 
248  std::vector<CTokenInfo> getInvalidTokens();
249 
250  bool isMineAddress(const std::string &strAddress);
251 
252 private:
256 
257  // Wallet has an options model for wallet-specific options
258  // (transaction fee, for example)
260 
267 
268  // Cache some values to be able to detect changes
279 
280  QTimer *pollTimer;
281 
282  QString restorePath;
283  QString restoreParam;
284 
285  void subscribeToCoreSignals();
286  void unsubscribeFromCoreSignals();
287  void checkBalanceChanged();
288  void checkTokenBalanceChanged();
289 
290 Q_SIGNALS:
291  // Signal that balance in wallet changed
292  void balanceChanged(const CAmount& balance, const CAmount& unconfirmedBalance, const CAmount& immatureBalance, const CAmount& stake,
293  const CAmount& watchOnlyBalance, const CAmount& watchUnconfBalance, const CAmount& watchImmatureBalance, const CAmount& watchOnlyStake);
294 
295  // Encryption status of wallet changed
296  void encryptionStatusChanged(int status);
297 
298  // Signal emitted when wallet needs to be unlocked
299  // It is valid behaviour for listeners to keep the wallet locked after this signal;
300  // this means that the unlocking failed or was cancelled.
301  void requireUnlock();
302 
303  // Fired when a message should be reported to the user
304  void message(const QString &title, const QString &message, unsigned int style);
305 
306  // Coins sent: from wallet, to recipient, in (serialized) transaction:
307  void coinsSent(CWallet* wallet, SendCoinsRecipient recipient, QByteArray transaction);
308 
309  // Show progress dialog e.g. for rescan
310  void showProgress(const QString &title, int nProgress);
311 
312  // Watch-only address added
313  void notifyWatchonlyChanged(bool fHaveWatchonly);
314 
315 public Q_SLOTS:
316  /* Wallet status might have changed */
317  void updateStatus();
318  /* New transaction, or transaction changed status */
319  void updateTransaction();
320  /* New, updated or removed address book entry */
321  void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
322  /* Watch-only added */
323  void updateWatchOnlyFlag(bool fHaveWatchonly);
324  /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
325  void pollBalanceChanged();
326  /* New, updated or removed contract book entry */
327  void updateContractBook(const QString &address, const QString &label, const QString &abi, int status);
328 };
329 
330 #endif // FABCOIN_QT_WALLETMODEL_H
Model for list of recently generated payment requests / fabcoin: URIs.
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:263
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:79
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:264
PaymentRequestPlus paymentRequest
Definition: walletmodel.h:61
bool IsInitialized() const
UnlockContext & operator=(const UnlockContext &rhs)
Definition: walletmodel.h:195
#define READWRITE(obj)
Definition: serialize.h:179
struct evm_uint256be balance(struct evm_env *env, struct evm_uint160be address)
Definition: capi.c:7
TokenTransactionTableModel * tokenTransactionTableModel
Definition: walletmodel.h:266
static const int CURRENT_VERSION
Definition: walletmodel.h:67
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:56
ContractTableModel * contractTableModel
Definition: walletmodel.h:262
SendCoinsRecipient(const QString &addr, const QString &_label, const CAmount &_amount, const QString &_message)
Definition: walletmodel.h:46
Coin Control Features.
Definition: coincontrol.h:16
QString restoreParam
Definition: walletmodel.h:283
int64_t CAmount
Amount in lius (Can be negative)
Definition: amount.h:15
bool SerializeToString(std::string *output) const
CWallet * wallet
Definition: walletmodel.h:253
CAmount cachedImmatureBalance
Definition: walletmodel.h:271
QString restorePath
Definition: walletmodel.h:282
An encapsulated public key.
Definition: pubkey.h:39
UI model for the transaction table of a wallet.
TokenItemModel * tokenItemModel
Definition: walletmodel.h:265
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:157
CAmount cachedBalance
Definition: walletmodel.h:269
OptionsModel * optionsModel
Definition: walletmodel.h:259
CAmount cachedWatchOnlyBalance
Definition: walletmodel.h:273
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:277
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletmodel.h:73
UI model for the transaction table of a wallet.
bool parse(const QByteArray &data)
Qt model of the address book in the core.
UnlockContext(const UnlockContext &obj)
Definition: walletmodel.h:194
QTimer * pollTimer
Definition: walletmodel.h:280
Qt model of the contract book in the core.
CAmount cachedStake
Definition: walletmodel.h:272
CAmount cachedWatchOnlyStake
Definition: walletmodel.h:276
256-bit opaque blob.
Definition: uint256.h:132
int cachedNumBlocks
Definition: walletmodel.h:278
CAmount cachedWatchUnconfBalance
Definition: walletmodel.h:274
bool fForceCheckBalanceChanged
Definition: walletmodel.h:255
CAmount cachedWatchImmatureBalance
Definition: walletmodel.h:275
Interface from Qt to configuration data structure for Fabcoin client.
Definition: optionsmodel.h:22
Interface to Fabcoin wallet from Qt view code.
Definition: walletmodel.h:103
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
CAmount cachedUnconfirmedBalance
Definition: walletmodel.h:270
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:672
Data model for a walletmodel transaction.
AddressTableModel * addressTableModel
Definition: walletmodel.h:261
bool fSubtractFeeFromAmount
Definition: walletmodel.h:65
An encapsulated private key.
Definition: key.h:35
bool fHaveWatchOnly
Definition: walletmodel.h:254
QString authenticatedMerchant
Definition: walletmodel.h:63