walletmodel.h 7.02 KB
Newer Older
1
2
3
4
// Copyright (c) 2011-2013 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

5
6
7
#ifndef WALLETMODEL_H
#define WALLETMODEL_H

8
9
#include "paymentrequestplus.h"
#include "walletmodeltransaction.h"
10

11
#include "allocators.h" /* for SecureString */
12

Cozz Lovan's avatar
Cozz Lovan committed
13
#include <map>
14
15
16
#include <vector>

#include <QObject>
17

18
class AddressTableModel;
19
class OptionsModel;
20
class TransactionTableModel;
21
class RecentRequestsTableModel;
22
class WalletModelTransaction;
23
24

class CCoinControl;
Cozz Lovan's avatar
Cozz Lovan committed
25
26
class CKeyID;
class COutPoint;
27
28
class COutput;
class CPubKey;
29
class CWallet;
30
class uint256;
31

32
33
34
35
QT_BEGIN_NAMESPACE
class QTimer;
QT_END_NAMESPACE

36
class SendCoinsRecipient
37
{
38
public:
39
40
41
    explicit SendCoinsRecipient() : amount(0) { }
    explicit SendCoinsRecipient(const QString &addr, const QString &label, quint64 amount, const QString &message):
        address(addr), label(label), amount(amount), message(message) {}
42

43
44
45
46
47
    // If from an insecure payment request, this is used for storing
    // the addresses, e.g. address-A<br />address-B<br />address-C.
    // Info: As we don't need to process addresses in here when using
    // payment requests, we can abuse it for displaying an address list.
    // Todo: This is a hack, should be replaced with a cleaner solution!
48
49
50
    QString address;
    QString label;
    qint64 amount;
51
    // If from a payment request, this is used for storing the memo
52
    QString message;
53
54
55

    // If from a payment request, paymentRequest.IsInitialized() will be true
    PaymentRequestPlus paymentRequest;
56
57
    // Empty if no authentication or invalid signature/cert/etc.
    QString authenticatedMerchant;
58
59
};

60
/** Interface to Bitcoin wallet from Qt view code. */
61
62
63
class WalletModel : public QObject
{
    Q_OBJECT
64

65
public:
66
    explicit WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent = 0);
67
    ~WalletModel();
68

69
    enum StatusCode // Returned by sendCoins
70
71
72
73
74
75
    {
        OK,
        InvalidAmount,
        InvalidAddress,
        AmountExceedsBalance,
        AmountWithFeeExceedsBalance,
76
        DuplicateAddress,
77
        TransactionCreationFailed, // Error returned when wallet is still locked
78
        TransactionCommitFailed,
79
        Aborted
80
81
    };

82
83
84
85
86
87
88
    enum EncryptionStatus
    {
        Unencrypted,  // !wallet->IsCrypted()
        Locked,       // wallet->IsCrypted() && wallet->IsLocked()
        Unlocked      // wallet->IsCrypted() && !wallet->IsLocked()
    };

89
90
91
    OptionsModel *getOptionsModel();
    AddressTableModel *getAddressTableModel();
    TransactionTableModel *getTransactionTableModel();
92
    RecentRequestsTableModel *getRecentRequestsTableModel();
93

94
    qint64 getBalance(const CCoinControl *coinControl = NULL) const;
95
    qint64 getUnconfirmedBalance() const;
96
    qint64 getImmatureBalance() const;
97
    int getNumTransactions() const;
98
99
    EncryptionStatus getEncryptionStatus() const;

100
101
102
    // Check address for validity
    bool validateAddress(const QString &address);

103
    // Return status record for SendCoins, contains error id + information
104
105
    struct SendCoinsReturn
    {
106
        SendCoinsReturn(StatusCode status = Aborted):
107
            status(status) {}
108
109
110
        StatusCode status;
    };

111
    // prepare transaction for getting txfee before sending coins
112
    SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const CCoinControl *coinControl = NULL);
113

114
    // Send coins to a list of recipients
115
    SendCoinsReturn sendCoins(WalletModelTransaction &transaction);
116
117

    // Wallet encryption
118
    bool setWalletEncrypted(bool encrypted, const SecureString &passphrase);
119
    // Passphrase only needed when unlocking
120
121
    bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
    bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
sje397's avatar
sje397 committed
122
123
    // Wallet backup
    bool backupWallet(const QString &filename);
124
125
126
127
128
129
130
131
132
133

    // RAI object for unlocking wallet, returned by requestUnlock()
    class UnlockContext
    {
    public:
        UnlockContext(WalletModel *wallet, bool valid, bool relock);
        ~UnlockContext();

        bool isValid() const { return valid; }

134
135
136
        // Copy operator and constructor transfer the context
        UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
        UnlockContext& operator=(const UnlockContext& rhs) { CopyFrom(rhs); return *this; }
137
138
139
140
141
142
143
144
145
146
    private:
        WalletModel *wallet;
        bool valid;
        mutable bool relock; // mutable, as it can be set to false by copying

        void CopyFrom(const UnlockContext& rhs);
    };

    UnlockContext requestUnlock();

Cozz Lovan's avatar
Cozz Lovan committed
147
148
149
150
151
152
153
154
155
    bool getPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
    void getOutputs(const std::vector<COutPoint>& vOutpoints, std::vector<COutput>& vOutputs);
    void listCoins(std::map<QString, std::vector<COutput> >& mapCoins) const;

    bool isLockedCoin(uint256 hash, unsigned int n) const;
    void lockCoin(COutPoint& output);
    void unlockCoin(COutPoint& output);
    void listLockedCoins(std::vector<COutPoint>& vOutpts);

156
157
158
159
160
161
162
163
164
private:
    CWallet *wallet;

    // Wallet has an options model for wallet-specific options
    // (transaction fee, for example)
    OptionsModel *optionsModel;

    AddressTableModel *addressTableModel;
    TransactionTableModel *transactionTableModel;
165
    RecentRequestsTableModel *recentRequestsTableModel;
166

167
    // Cache some values to be able to detect changes
168
169
    qint64 cachedBalance;
    qint64 cachedUnconfirmedBalance;
170
    qint64 cachedImmatureBalance;
171
    qint64 cachedNumTransactions;
172
    EncryptionStatus cachedEncryptionStatus;
173
174
175
    int cachedNumBlocks;

    QTimer *pollTimer;
176

177
178
    void subscribeToCoreSignals();
    void unsubscribeFromCoreSignals();
179
180
    void checkBalanceChanged();

181
signals:
182
    // Signal that balance in wallet changed
183
    void balanceChanged(qint64 balance, 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
192
193

    // Signal emitted when wallet needs to be unlocked
    // It is valid behaviour for listeners to keep the wallet locked after this signal;
    // this means that the unlocking failed or was cancelled.
194
    void requireUnlock();
195

196
    // Fired when a message should be reported to the user
197
    void message(const QString &title, const QString &message, unsigned int style);
198

199
200
201
    // Coins sent: from wallet, to recipient, in (serialized) transaction:
    void coinsSent(CWallet* wallet, SendCoinsRecipient recipient, QByteArray transaction);

202
public slots:
203
204
205
206
207
    /* Wallet status might have changed */
    void updateStatus();
    /* New transaction, or transaction changed status */
    void updateTransaction(const QString &hash, int status);
    /* New, updated or removed address book entry */
208
    void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
209
210
    /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
    void pollBalanceChanged();
211
212
213
};

#endif // WALLETMODEL_H