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