Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
walletmodel.h
Go to the documentation of this file.
1 #ifndef WALLETMODEL_H
2 #define WALLETMODEL_H
3 
4 #include <QObject>
5 #include <vector>
6 #include <map>
7 
8 #pragma warning(disable:4717) //bogus warning from MS
9 
10 #include "allocators.h" /* for SecureString */
11 
12 class OptionsModel;
13 class AddressTableModel;
15 class CWallet;
16 class CKeyID;
17 class CPubKey;
18 class COutput;
19 class COutPoint;
20 class uint256;
21 class CCoinControl;
22 
23 QT_BEGIN_NAMESPACE
24 class QTimer;
25 QT_END_NAMESPACE
26 
28 {
29 public:
30  QString address;
31  QString label;
32  qint64 amount;
33 };
34 
36 class WalletModel : public QObject
37 {
38  Q_OBJECT
39 
40 public:
41  explicit WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent = 0);
42  ~WalletModel();
43 
44  enum StatusCode // Returned by sendCoins
45  {
46  OK,
52  TransactionCreationFailed, // Error returned when wallet is still locked
55  };
56 
58  {
59  Unencrypted, // !wallet->IsCrypted()
60  Locked, // wallet->IsCrypted() && wallet->IsLocked()
61  Unlocked // wallet->IsCrypted() && !wallet->IsLocked()
62  };
63 
67 
68  qint64 getBalance(const CCoinControl *coinControl=NULL) const;
69  qint64 getUnconfirmedBalance() const;
70  qint64 getImmatureBalance() const;
71  int getNumTransactions() const;
73 
74  // Check address for validity
75  bool validateAddress(const QString &address);
76 
77  // Return status record for SendCoins, contains error id + information
79  {
81  qint64 fee=0,
82  QString hex=QString()):
83  status(status), fee(fee), hex(hex) {}
85  qint64 fee; // is used in case status is "AmountWithFeeExceedsBalance"
86  QString hex; // is filled with the transaction hash if status is "OK"
87  };
88 
89  // Send coins to a list of recipients
90  SendCoinsReturn sendCoins(const QList<SendCoinsRecipient> &recipients, const CCoinControl *coinControl=NULL);
91 
92  bool importPrivateKey(QString privKey);
93  // Wallet encryption
94  bool setWalletEncrypted(bool encrypted, const SecureString &passphrase);
95  // Passphrase only needed when unlocking
96  bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
97  bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
98  // Wallet backup
99  bool backupWallet(const QString &filename);
100 
101  // RAI object for unlocking wallet, returned by requestUnlock()
103  {
104  public:
105  UnlockContext(WalletModel *wallet, bool valid, bool relock);
106  ~UnlockContext();
107 
108  bool isValid() const { return valid; }
109 
110  // Copy operator and constructor transfer the context
111  UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
112  UnlockContext& operator=(const UnlockContext& rhs) { CopyFrom(rhs); return *this; }
113  private:
115  bool valid;
116  mutable bool relock; // mutable, as it can be set to false by copying
117 
118  void CopyFrom(const UnlockContext& rhs);
119  };
120 
122 
123  bool getPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
124  void getOutputs(const std::vector<COutPoint>& vOutpoints, std::vector<COutput>& vOutputs);
125  void listCoins(std::map<QString, std::vector<COutput> >& mapCoins) const;
126 
127  bool isLockedCoin(uint256 hash, unsigned int n) const;
128  void lockCoin(COutPoint& output);
129  void unlockCoin(COutPoint& output);
130  void listLockedCoins(std::vector<COutPoint>& vOutpts);
131 
132 private:
134 
135  // Wallet has an options model for wallet-specific options
136  // (transaction fee, for example)
138 
141 
142  // Cache some values to be able to detect changes
149 
150  QTimer *pollTimer;
151 
152  void subscribeToCoreSignals();
154  void checkBalanceChanged();
155 
156 signals:
157  // Signal that balance in wallet changed
158  void balanceChanged(qint64 balance, qint64 unconfirmedBalance, qint64 immatureBalance);
159 
160  // Number of transactions in wallet changed
161  void numTransactionsChanged(int count);
162 
163  // Encryption status of wallet changed
164  void encryptionStatusChanged(int status);
165 
166  // Signal emitted when wallet needs to be unlocked
167  // It is valid behaviour for listeners to keep the wallet locked after this signal;
168  // this means that the unlocking failed or was cancelled.
169  void requireUnlock();
170 
171  // Asynchronous message notification
172  void message(const QString &title, const QString &message, unsigned int style);
173 
174 public slots:
175  /* Wallet status might have changed */
176  void updateStatus();
177  /* New transaction, or transaction changed status */
178  void updateTransaction(const QString &hash, int status);
179  /* New, updated or removed address book entry */
180  void updateAddressBook(const QString &address, const QString &label, bool isMine, int status);
181  /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
182  void pollBalanceChanged();
183 };
184 
185 #endif // WALLETMODEL_H
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:140
void getOutputs(const std::vector< COutPoint > &vOutpoints, std::vector< COutput > &vOutputs)
void lockCoin(COutPoint &output)
UnlockContext & operator=(const UnlockContext &rhs)
Definition: walletmodel.h:112
SendCoinsReturn sendCoins(const QList< SendCoinsRecipient > &recipients, const CCoinControl *coinControl=NULL)
qint64 getImmatureBalance() const
Definition: walletmodel.cpp:60
qint64 cachedImmatureBalance
Definition: walletmodel.h:145
qint64 cachedNumTransactions
Definition: walletmodel.h:146
qint64 cachedUnconfirmedBalance
Definition: walletmodel.h:144
UnlockContext requestUnlock()
void unsubscribeFromCoreSignals()
bool isLockedCoin(uint256 hash, unsigned int n) const
bool backupWallet(const QString &filename)
SendCoinsReturn(StatusCode status=Aborted, qint64 fee=0, QString hex=QString())
Definition: walletmodel.h:80
AddressTableModel * getAddressTableModel()
Coin Control Features.
Definition: coincontrol.h:5
void updateStatus()
Definition: walletmodel.cpp:77
UnlockContext(WalletModel *wallet, bool valid, bool relock)
void balanceChanged(qint64 balance, qint64 unconfirmedBalance, qint64 immatureBalance)
WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent=0)
Definition: walletmodel.cpp:15
void checkBalanceChanged()
Definition: walletmodel.cpp:95
CWallet * wallet
Definition: walletmodel.h:133
void numTransactionsChanged(int count)
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: allocators.h:269
qint64 getUnconfirmedBalance() const
Definition: walletmodel.cpp:55
An encapsulated public key.
Definition: key.h:40
bool getPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
void updateAddressBook(const QString &address, const QString &label, bool isMine, int status)
OptionsModel * optionsModel
Definition: walletmodel.h:137
TransactionTableModel * getTransactionTableModel()
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:147
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: main.h:278
void CopyFrom(const UnlockContext &rhs)
UI model for the transaction table of a wallet.
Qt model of the address book in the core.
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
UnlockContext(const UnlockContext &obj)
Definition: walletmodel.h:111
void encryptionStatusChanged(int status)
QTimer * pollTimer
Definition: walletmodel.h:150
EncryptionStatus getEncryptionStatus() const
bool validateAddress(const QString &address)
256-bit unsigned integer
Definition: uint256.h:537
int cachedNumBlocks
Definition: walletmodel.h:148
void requireUnlock()
bool importPrivateKey(QString privKey)
void listLockedCoins(std::vector< COutPoint > &vOutpts)
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:12
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:36
bool setWalletEncrypted(bool encrypted, const SecureString &passphrase)
void unlockCoin(COutPoint &output)
void message(const QString &title, const QString &message, unsigned int style)
A reference to a CKey: the Hash160 of its serialized public key.
Definition: key.h:24
qint64 getBalance(const CCoinControl *coinControl=NULL) const
Definition: walletmodel.cpp:39
void updateTransaction(const QString &hash, int status)
int getNumTransactions() const
Definition: walletmodel.cpp:65
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:69
AddressTableModel * addressTableModel
Definition: walletmodel.h:139
qint64 cachedBalance
Definition: walletmodel.h:143
void listCoins(std::map< QString, std::vector< COutput > > &mapCoins) const
uint32_t hash
Definition: cache.cc:34
void pollBalanceChanged()
Definition: walletmodel.cpp:85
OptionsModel * getOptionsModel()
void subscribeToCoreSignals()