Anoncoin  0.9.4
P2P Digital Currency
walletmodel.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2014 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef WALLETMODEL_H
7 #define WALLETMODEL_H
8 
9 #include "paymentrequestplus.h"
10 #include "walletmodeltransaction.h"
11 
12 #include "allocators.h" /* for SecureString */
13 
14 #include <map>
15 #include <vector>
16 
17 #include <QObject>
18 
19 class AddressTableModel;
20 class OptionsModel;
24 
25 class CCoinControl;
26 class CKeyID;
27 class COutPoint;
28 class COutput;
29 class CPubKey;
30 class CWallet;
31 class uint256;
32 
33 QT_BEGIN_NAMESPACE
34 class QTimer;
35 QT_END_NAMESPACE
36 
38 {
39 public:
41  explicit SendCoinsRecipient(const QString &addr, const QString &label, quint64 amount, const QString &message):
42  address(addr), label(label), amount(amount), message(message), nVersion(SendCoinsRecipient::CURRENT_VERSION) {}
43 
44  // If from an insecure 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  qint64 amount;
52  // If from a payment request, this is used for storing the memo
53  QString message;
54 
55  // If from a payment request, paymentRequest.IsInitialized() will be true
57  // Empty if no authentication or invalid signature/cert/etc.
59 
60  static const int CURRENT_VERSION = 1;
61  int nVersion;
62 
64  (
65  SendCoinsRecipient* pthis = const_cast<SendCoinsRecipient*>(this);
66 
67  std::string sAddress = pthis->address.toStdString();
68  std::string sLabel = pthis->label.toStdString();
69  std::string sMessage = pthis->message.toStdString();
70  std::string sPaymentRequest;
71  if (!fRead && pthis->paymentRequest.IsInitialized())
72  pthis->paymentRequest.SerializeToString(&sPaymentRequest);
73  std::string sAuthenticatedMerchant = pthis->authenticatedMerchant.toStdString();
74 
75  READWRITE(pthis->nVersion);
76  nVersion = pthis->nVersion;
77  READWRITE(sAddress);
78  READWRITE(sLabel);
79  READWRITE(amount);
80  READWRITE(sMessage);
81  READWRITE(sPaymentRequest);
82  READWRITE(sAuthenticatedMerchant);
83 
84  if (fRead)
85  {
86  pthis->address = QString::fromStdString(sAddress);
87  pthis->label = QString::fromStdString(sLabel);
88  pthis->message = QString::fromStdString(sMessage);
89  if (!sPaymentRequest.empty())
90  pthis->paymentRequest.parse(QByteArray::fromRawData(sPaymentRequest.data(), sPaymentRequest.size()));
91  pthis->authenticatedMerchant = QString::fromStdString(sAuthenticatedMerchant);
92  }
93  )
94 };
95 
97 class WalletModel : public QObject
98 {
99  Q_OBJECT
100 
101 public:
102  explicit WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent = 0);
103  ~WalletModel();
104 
105  enum StatusCode // Returned by sendCoins
106  {
107  OK,
113  TransactionCreationFailed, // Error returned when wallet is still locked
114  TransactionCommitFailed
115  };
116 
118  {
119  Unencrypted, // !wallet->IsCrypted()
120  Locked, // wallet->IsCrypted() && wallet->IsLocked()
121  Unlocked // wallet->IsCrypted() && !wallet->IsLocked()
122  };
123 
124  OptionsModel *getOptionsModel();
125  AddressTableModel *getAddressTableModel();
126  TransactionTableModel *getTransactionTableModel();
127  RecentRequestsTableModel *getRecentRequestsTableModel();
128 
129  qint64 getBalance(const CCoinControl *coinControl = NULL) const;
130  qint64 getUnconfirmedBalance() const;
131  qint64 getImmatureBalance() const;
132  bool haveWatchOnly() const;
133  qint64 getWatchBalance() const;
134  qint64 getWatchUnconfirmedBalance() const;
135  qint64 getWatchImmatureBalance() const;
136  EncryptionStatus getEncryptionStatus() const;
137  bool processingQueuedTransactions() { return fProcessingQueuedTransactions; }
138 
139  // Check address for validity
140  bool validateAddress(const QString &address);
141 
142  // Return status record for SendCoins, contains error id + information
144  {
146  status(status) {}
148  };
149 
150  // prepare transaction for getting txfee before sending coins
151  SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const CCoinControl *coinControl = NULL);
152 
153  // Send coins to a list of recipients
154  SendCoinsReturn sendCoins(WalletModelTransaction &transaction);
155 
156  // Wallet encryption
157  bool setWalletEncrypted(bool encrypted, const SecureString &passphrase);
158  // Passphrase only needed when unlocking
159  bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
160  bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
161  // Wallet backup
162  bool backupWallet(const QString &filename);
163 
164  // RAI object for unlocking wallet, returned by requestUnlock()
166  {
167  public:
168  UnlockContext(WalletModel *wallet, bool valid, bool relock);
169  ~UnlockContext();
170 
171  bool isValid() const { return valid; }
172 
173  // Copy operator and constructor transfer the context
174  UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
175  UnlockContext& operator=(const UnlockContext& rhs) { CopyFrom(rhs); return *this; }
176  private:
178  bool valid;
179  mutable bool relock; // mutable, as it can be set to false by copying
180 
181  void CopyFrom(const UnlockContext& rhs);
182  };
183 
184  UnlockContext requestUnlock();
185 
186  bool getPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
187  void getOutputs(const std::vector<COutPoint>& vOutpoints, std::vector<COutput>& vOutputs);
188  bool isSpent(const COutPoint& outpoint) const;
189  void listCoins(std::map<QString, std::vector<COutput> >& mapCoins) const;
190 
191  bool isLockedCoin(uint256 hash, unsigned int n) const;
192  void lockCoin(COutPoint& output);
193  void unlockCoin(COutPoint& output);
194  void listLockedCoins(std::vector<COutPoint>& vOutpts);
195 
196  void loadReceiveRequests(std::vector<std::string>& vReceiveRequests);
197  bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest);
198 
199 private:
204 
205  // Wallet has an options model for wallet-specific options
206  // (transaction fee, for example)
208 
212 
213  // Cache some values to be able to detect changes
222 
223  QTimer *pollTimer;
224 
225  void subscribeToCoreSignals();
226  void unsubscribeFromCoreSignals();
227  void checkBalanceChanged();
228 
229 signals:
230  // Signal that balance in wallet changed
231  void balanceChanged(qint64 balance, qint64 unconfirmedBalance, qint64 immatureBalance,
232  qint64 watchOnlyBalance, qint64 watchUnconfBalance, qint64 watchImmatureBalance);
233 
234  // Encryption status of wallet changed
235  void encryptionStatusChanged(int status);
236 
237  // Signal emitted when wallet needs to be unlocked
238  // It is valid behaviour for listeners to keep the wallet locked after this signal;
239  // this means that the unlocking failed or was cancelled.
240  void requireUnlock();
241 
242  // Fired when a message should be reported to the user
243  void message(const QString &title, const QString &message, unsigned int style);
244 
245  // Coins sent: from wallet, to recipient, in (serialized) transaction:
246  void coinsSent(CWallet* wallet, SendCoinsRecipient recipient, QByteArray transaction);
247 
248  // Show progress dialog e.g. for rescan
249  void showProgress(const QString &title, int nProgress);
250 
251  // Watch-only address added
252  void notifyWatchonlyChanged(bool fHaveWatchonly);
253 
254 public slots:
255  /* Wallet status might have changed */
256  void updateStatus();
257  /* New transaction, or transaction changed status */
258  void updateTransaction(const QString &hash, int status);
259  /* New, updated or removed address book entry */
260  void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
261  /* Watch-only added */
262  void updateWatchOnlyFlag(bool fHaveWatchonly);
263  /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
264  void pollBalanceChanged();
265  /* Needed to update fProcessingQueuedTransactions through a QueuedConnection */
266  void setProcessingQueuedTransactions(bool value) { fProcessingQueuedTransactions = value; }
267 };
268 
269 #endif // WALLETMODEL_H
bool processingQueuedTransactions()
Definition: walletmodel.h:137
Model for list of recently generated payment requests / anoncoin: URIs.
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:210
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:211
PaymentRequestPlus paymentRequest
Definition: walletmodel.h:56
UnlockContext & operator=(const UnlockContext &rhs)
Definition: walletmodel.h:175
void setProcessingQueuedTransactions(bool value)
Definition: walletmodel.h:266
#define READWRITE(obj)
Definition: serialize.h:101
qint64 cachedImmatureBalance
Definition: walletmodel.h:216
static const int CURRENT_VERSION
Definition: walletmodel.h:60
qint64 cachedUnconfirmedBalance
Definition: walletmodel.h:215
#define IMPLEMENT_SERIALIZE(statements)
Definition: serialize.h:63
bool fProcessingQueuedTransactions
Definition: walletmodel.h:201
Coin Control Features.
Definition: coincontrol.h:12
CWallet * wallet
Definition: walletmodel.h:200
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: allocators.h:254
An encapsulated public key.
Definition: key.h:43
OptionsModel * optionsModel
Definition: walletmodel.h:207
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:220
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: core.h:24
UI model for the transaction table of a wallet.
Qt model of the address book in the core.
UnlockContext(const UnlockContext &obj)
Definition: walletmodel.h:174
QTimer * pollTimer
Definition: walletmodel.h:223
qint64 cachedWatchOnlyBalance
Definition: walletmodel.h:217
256-bit unsigned integer
Definition: uint256.h:532
int cachedNumBlocks
Definition: walletmodel.h:221
bool fForceCheckBalanceChanged
Definition: walletmodel.h:203
qint64 cachedWatchUnconfBalance
Definition: walletmodel.h:218
Interface from Qt to configuration data structure for Anoncoin client.
Definition: optionsmodel.h:27
SendCoinsRecipient(const QString &addr, const QString &label, quint64 amount, const QString &message)
Definition: walletmodel.h:41
Interface to Anoncoin wallet from Qt view code.
Definition: walletmodel.h:97
SendCoinsReturn(StatusCode status=OK)
Definition: walletmodel.h:145
A reference to a CKey: the Hash160 of its serialized public key.
Definition: key.h:27
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:101
Data model for a walletmodel transaction.
qint64 cachedWatchImmatureBalance
Definition: walletmodel.h:219
AddressTableModel * addressTableModel
Definition: walletmodel.h:209
qint64 cachedBalance
Definition: walletmodel.h:214
bool fHaveWatchOnly
Definition: walletmodel.h:202
QString authenticatedMerchant
Definition: walletmodel.h:58