Bitcoin Core 32.99.0
P2P Digital Currency
walletmodel.h
Go to the documentation of this file.
1// Copyright (c) 2011-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_QT_WALLETMODEL_H
6#define BITCOIN_QT_WALLETMODEL_H
7
8#include <key.h>
9
11
12#include <interfaces/wallet.h>
15
16#include <vector>
17
18#include <QObject>
19
20enum class OutputType;
21
23class ClientModel;
24class OptionsModel;
25class PlatformStyle;
30
31class CKeyID;
32class COutPoint;
33class CPubKey;
34class uint256;
35
36namespace interfaces {
37class Node;
38} // namespace interfaces
39namespace wallet {
40class CCoinControl;
41} // namespace wallet
42
43QT_BEGIN_NAMESPACE
44class QTimer;
45QT_END_NAMESPACE
46
48class WalletModel : public QObject
49{
50 Q_OBJECT
51
52public:
53 explicit WalletModel(std::unique_ptr<interfaces::Wallet> wallet, ClientModel& client_model, const PlatformStyle *platformStyle, QObject *parent = nullptr);
55
56 enum StatusCode // Returned by sendCoins
57 {
63 TransactionCreationFailed, // Error returned when wallet is still locked
65 };
66
68 {
69 NoKeys, // wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)
70 Unencrypted, // !wallet->HasEncryptionKeys()
71 Locked, // wallet->HasEncryptionKeys() && wallet->IsLocked()
72 Unlocked // wallet->HasEncryptionKeys() && !wallet->IsLocked()
73 };
74
79
81
82 // Check address for validity
83 bool validateAddress(const QString& address) const;
84
85 // Return status record for SendCoins, contains error id + information
87 {
88 SendCoinsReturn(StatusCode _status = OK, QString _reasonCommitFailed = "")
89 : status(_status),
90 reasonCommitFailed(_reasonCommitFailed)
91 {
92 }
95 };
96
97 // prepare transaction for getting txfee before sending coins
99
100 // Send coins to a list of recipients
101 void sendCoins(WalletModelTransaction& transaction);
102
103 // Wallet encryption
104 bool setWalletEncrypted(const SecureString& passphrase);
106
107 // RAII object for unlocking wallet, returned by requestUnlock()
109 {
110 public:
113
114 bool isValid() const { return valid; }
115
116 // Disable unused copy/move constructors/assignments explicitly.
117 UnlockContext(const UnlockContext&) = delete;
121
122 private:
124 const bool valid;
125 const bool relock;
126 };
127
129
130 bool bumpFee(Txid hash, Txid& new_hash);
131 void displayAddress(std::string sAddress) const;
132
133 static bool isWalletEnabled();
134
135 interfaces::Node& node() const { return m_node; }
136 interfaces::Wallet& wallet() const { return *m_wallet; }
138 void setClientModel(ClientModel* client_model);
139
140 QString getWalletName() const;
141 QString getDisplayName() const;
142
143 bool isMultiwallet() const;
144
145 void refresh(bool pk_hash_only = false);
146
148
149 // Retrieve the cached wallet balance
151
152 // If coin control has selected outputs, searches the total amount inside the wallet.
153 // Otherwise, uses the wallet's cached available balance.
155
156private:
157 std::unique_ptr<interfaces::Wallet> m_wallet;
158 std::unique_ptr<interfaces::Handler> m_handler_unload;
159 std::unique_ptr<interfaces::Handler> m_handler_status_changed;
160 std::unique_ptr<interfaces::Handler> m_handler_address_book_changed;
161 std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
162 std::unique_ptr<interfaces::Handler> m_handler_show_progress;
163 std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
166
168
169 // Wallet has an options model for wallet-specific options
170 // (transaction fee, for example)
172
176
177 // Cache some values to be able to detect changes
180 QTimer* timer;
181
182 // Block hash denoting when the last balance update was done.
184
187 void checkBalanceChanged(const interfaces::WalletBalances& new_balances);
188
189Q_SIGNALS:
190 // Signal that balance in wallet changed
192
193 // Encryption status of wallet changed
195
196 // Signal emitted when wallet needs to be unlocked
197 // It is valid behaviour for listeners to keep the wallet locked after this signal;
198 // this means that the unlocking failed or was cancelled.
200
201 // Fired when a message should be reported to the user
202 void message(const QString &title, const QString &message, unsigned int style);
203
204 // Coins sent: from wallet, to recipient, in (serialized) transaction:
205 void coinsSent(WalletModel* wallet, SendCoinsRecipient recipient, QByteArray transaction);
206
207 // Show progress dialog e.g. for rescan
208 void showProgress(const QString &title, int nProgress);
209
210 // Signal that wallet is about to be removed
211 void unload();
212
213 // Notify that there are now keys in the keypool
215
217
218public Q_SLOTS:
219 /* Starts a timer to periodically update the balance */
220 void startPollBalance();
221
222 /* Wallet status might have changed */
223 void updateStatus();
224 /* New transaction, or transaction changed status */
225 void updateTransaction();
226 /* New, updated or removed address book entry */
227 void updateAddressBook(const QString &address, const QString &label, bool isMine, wallet::AddressPurpose purpose, int status);
228 /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
229 void pollBalanceChanged();
230};
231
232#endif // BITCOIN_QT_WALLETMODEL_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Qt model of the address book in the core.
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:30
An encapsulated public key.
Definition: pubkey.h:43
Model for Bitcoin network client.
Definition: clientmodel.h:57
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:44
Model for list of recently generated payment requests / bitcoin: URIs.
UI model for the transaction table of a wallet.
UnlockContext(WalletModel *wallet, bool valid, bool relock)
UnlockContext(UnlockContext &&)=delete
UnlockContext(const UnlockContext &)=delete
UnlockContext & operator=(const UnlockContext &)=delete
UnlockContext & operator=(UnlockContext &&)=delete
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:49
OptionsModel * optionsModel
Definition: walletmodel.h:171
AddressTableModel * addressTableModel
Definition: walletmodel.h:173
RecentRequestsTableModel * getRecentRequestsTableModel() const
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:179
void refresh(bool pk_hash_only=false)
uint256 m_cached_last_update_tip
Definition: walletmodel.h:183
ClientModel * m_client_model
Definition: walletmodel.h:164
bool bumpFee(Txid hash, Txid &new_hash)
interfaces::Node & m_node
Definition: walletmodel.h:165
ClientModel & clientModel() const
Definition: walletmodel.h:137
interfaces::Node & node() const
Definition: walletmodel.h:135
std::unique_ptr< interfaces::Handler > m_handler_transaction_changed
Definition: walletmodel.h:161
void startPollBalance()
Definition: walletmodel.cpp:63
void pollBalanceChanged()
Definition: walletmodel.cpp:92
AddressTableModel * getAddressTableModel() const
util::Expected< void, wallet::WalletError > changePassphrase(const SecureString &oldPass, const SecureString &newPass)
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:175
SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const wallet::CCoinControl &coinControl)
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:174
bool setWalletEncrypted(const SecureString &passphrase)
void showProgress(const QString &title, int nProgress)
void updateAddressBook(const QString &address, const QString &label, bool isMine, wallet::AddressPurpose purpose, int status)
void message(const QString &title, const QString &message, unsigned int style)
bool validateAddress(const QString &address) const
void sendCoins(WalletModelTransaction &transaction)
void setClientModel(ClientModel *client_model)
Definition: walletmodel.cpp:77
void updateStatus()
Definition: walletmodel.cpp:83
CAmount getAvailableBalance(const wallet::CCoinControl *control)
bool isMultiwallet() const
void timerTimeout()
std::unique_ptr< interfaces::Handler > m_handler_can_get_addrs_changed
Definition: walletmodel.h:163
std::unique_ptr< interfaces::Handler > m_handler_unload
Definition: walletmodel.h:158
void displayAddress(std::string sAddress) const
EncryptionStatus getEncryptionStatus() const
TransactionTableModel * getTransactionTableModel() const
interfaces::Wallet & wallet() const
Definition: walletmodel.h:136
std::unique_ptr< interfaces::Handler > m_handler_status_changed
Definition: walletmodel.h:159
interfaces::WalletBalances m_cached_balances
Definition: walletmodel.h:178
bool fForceCheckBalanceChanged
Definition: walletmodel.h:167
void coinsSent(WalletModel *wallet, SendCoinsRecipient recipient, QByteArray transaction)
QString getDisplayName() const
OptionsModel * getOptionsModel() const
void checkBalanceChanged(const interfaces::WalletBalances &new_balances)
void unsubscribeFromCoreSignals()
void requireUnlock()
void updateTransaction()
uint256 getLastBlockProcessed() const
void canGetAddressesChanged()
QTimer * timer
Definition: walletmodel.h:180
WalletModel(std::unique_ptr< interfaces::Wallet > wallet, ClientModel &client_model, const PlatformStyle *platformStyle, QObject *parent=nullptr)
Definition: walletmodel.cpp:43
std::unique_ptr< interfaces::Handler > m_handler_address_book_changed
Definition: walletmodel.h:160
void encryptionStatusChanged()
std::unique_ptr< interfaces::Wallet > m_wallet
Definition: walletmodel.h:157
UnlockContext requestUnlock()
void balanceChanged(const interfaces::WalletBalances &balances)
static bool isWalletEnabled()
interfaces::WalletBalances getCachedBalance() const
QString getWalletName() const
std::unique_ptr< interfaces::Handler > m_handler_show_progress
Definition: walletmodel.h:162
void unload()
@ TransactionCreationFailed
Definition: walletmodel.h:63
@ AmountExceedsBalance
Definition: walletmodel.h:61
@ DuplicateAddress
Definition: walletmodel.h:62
void subscribeToCoreSignals()
Data model for a walletmodel transaction.
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:66
Interface for accessing a wallet.
Definition: wallet.h:65
256-bit opaque blob.
Definition: uint256.h:196
The util::Expected class provides a standard way for low-level functions to return either error value...
Definition: expected.h:44
Coin Control Features.
Definition: coincontrol.h:83
AddressPurpose
Address purpose field that has been been stored with wallet sending and receiving addresses since BIP...
Definition: types.h:44
OutputType
Definition: outputtype.h:18
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:53
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:88
Collection of wallet balances.
Definition: wallet.h:377