We need 20 additional confirmations
[novacoin.git] / src / qt / walletmodel.h
1 #ifndef WALLETMODEL_H
2 #define WALLETMODEL_H
3
4 #include <QObject>
5 #include <vector>
6 #include <map>
7
8 #include "allocators.h" /* for SecureString */
9
10 class OptionsModel;
11 class AddressTableModel;
12 class TransactionTableModel;
13 class MintingTableModel;
14 class CWallet;
15 class CKeyID;
16 class CPubKey;
17 class COutput;
18 class COutPoint;
19 class uint256;
20 class CCoinControl;
21
22 QT_BEGIN_NAMESPACE
23 class QTimer;
24 QT_END_NAMESPACE
25
26 class SendCoinsRecipient
27 {
28 public:
29     QString address;
30     QString label;
31     qint64 amount;
32 };
33
34 /** Interface to Bitcoin wallet from Qt view code. */
35 class WalletModel : public QObject
36 {
37     Q_OBJECT
38
39 public:
40     explicit WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent = 0);
41     ~WalletModel();
42
43     enum StatusCode // Returned by sendCoins
44     {
45         OK,
46         InvalidAmount,
47         InvalidAddress,
48         AmountExceedsBalance,
49         AmountWithFeeExceedsBalance,
50         DuplicateAddress,
51         TransactionCreationFailed, // Error returned when wallet is still locked
52         TransactionCommitFailed,
53         Aborted
54     };
55
56     enum EncryptionStatus
57     {
58         Unencrypted,  // !wallet->IsCrypted()
59         Locked,       // wallet->IsCrypted() && wallet->IsLocked()
60         Unlocked      // wallet->IsCrypted() && !wallet->IsLocked()
61     };
62
63     OptionsModel *getOptionsModel();
64     AddressTableModel *getAddressTableModel();
65     MintingTableModel *getMintingTableModel();
66     TransactionTableModel *getTransactionTableModel();
67
68     bool haveWatchOnly() const;
69     qint64 getBalance() const;
70     qint64 getBalanceWatchOnly() const;
71     qint64 getStake() const;
72     qint64 getUnconfirmedBalance() const;
73     qint64 getImmatureBalance() const;
74     int getNumTransactions() const;
75     EncryptionStatus getEncryptionStatus() const;
76
77     // Check address for validity
78     bool validateAddress(const QString &address);
79
80     // Return status record for SendCoins, contains error id + information
81     struct SendCoinsReturn
82     {
83         SendCoinsReturn(StatusCode status=Aborted,
84                          qint64 fee=0,
85                          QString hex=QString()):
86             status(status), fee(fee), hex(hex) {}
87         StatusCode status;
88         qint64 fee; // is used in case status is "AmountWithFeeExceedsBalance"
89         QString hex; // is filled with the transaction hash if status is "OK"
90     };
91
92     // Send coins to a list of recipients
93     SendCoinsReturn sendCoins(const QList<SendCoinsRecipient> &recipients, const CCoinControl *coinControl=NULL);
94
95     // Wallet encryption
96     bool setWalletEncrypted(bool encrypted, const SecureString &passphrase);
97     // Passphrase only needed when unlocking
98     bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
99     bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
100     // Wallet backup
101     bool backupWallet(const QString &filename);
102
103     bool dumpWallet(const QString &filename);
104     bool importWallet(const QString &filename);
105
106     void getStakeStats(float &nKernelsRate, float &nCoinDaysRate);
107     void getStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& nWeight);
108
109     // RAI object for unlocking wallet, returned by requestUnlock()
110     class UnlockContext
111     {
112     public:
113         UnlockContext(WalletModel *wallet, bool valid, bool relock, bool mintflag);
114         ~UnlockContext();
115
116         bool isValid() const { return valid; }
117
118         // Copy operator and constructor transfer the context
119         UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
120         UnlockContext& operator=(const UnlockContext& rhs) { CopyFrom(rhs); return *this; }
121     private:
122         WalletModel *wallet;
123         bool valid;
124         mutable bool relock; // mutable, as it can be set to false by copying
125         bool mintflag;
126
127         void CopyFrom(const UnlockContext& rhs);
128     };
129
130     UnlockContext requestUnlock();
131
132     bool getPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
133     void getOutputs(const std::vector<COutPoint>& vOutpoints, std::vector<COutput>& vOutputs);
134     void listCoins(std::map<QString, std::vector<COutput> >& mapCoins) const;
135     bool isLockedCoin(uint256 hash, unsigned int n) const;
136     void lockCoin(COutPoint& output);
137     void unlockCoin(COutPoint& output);
138     void listLockedCoins(std::vector<COutPoint>& vOutpts);
139     void clearOrphans();
140     CWallet* getWallet();
141
142 private:
143     CWallet *wallet;
144     bool fHaveWatchOnly;
145
146     // Wallet has an options model for wallet-specific options
147     // (transaction fee, for example)
148     OptionsModel *optionsModel;
149
150     AddressTableModel *addressTableModel;
151     MintingTableModel *mintingTableModel;
152     TransactionTableModel *transactionTableModel;
153
154     // Cache some values to be able to detect changes
155     qint64 cachedBalance;
156     qint64 cachedStake;
157     qint64 cachedUnconfirmedBalance;
158     qint64 cachedImmatureBalance;
159     qint64 cachedNumTransactions;
160     EncryptionStatus cachedEncryptionStatus;
161     int cachedNumBlocks;
162
163     QTimer *pollTimer;
164
165     void subscribeToCoreSignals();
166     void unsubscribeFromCoreSignals();
167     void checkBalanceChanged();
168
169 public slots:
170     /* Wallet status might have changed */
171     void updateStatus();
172     /* New transaction, or transaction changed status */
173     void updateTransaction(const QString &hash, int status);
174     /* New, updated or removed address book entry */
175     void updateAddressBook(const QString &address, const QString &label, bool isMine, int status);
176     /* Watchonly added */
177     void updateWatchOnlyFlag(bool fHaveWatchonly);
178     /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
179     void pollBalanceChanged();
180
181 signals:
182     // Signal that balance in wallet changed
183     void balanceChanged(qint64 total, qint64 watchOnly, qint64 stake, qint64 unconfirmedBalance, qint64 immatureBalance);
184
185     // Number of transactions in wallet changed
186     void numTransactionsChanged(int count);
187
188     // Encryption status of wallet changed
189     void encryptionStatusChanged(int status);
190
191     // Signal emitted when wallet needs to be unlocked
192     // It is valid behaviour for listeners to keep the wallet locked after this signal;
193     // this means that the unlocking failed or was cancelled.
194     void requireUnlock();
195
196     // Asynchronous error notification
197     void error(const QString &title, const QString &message, bool modal);
198
199     // Watch-only address added
200     void notifyWatchonlyChanged(bool fHaveWatchonly);
201 };
202
203
204 #endif // WALLETMODEL_H