Bitcoin Core 29.99.0
P2P Digital Currency
walletmodel.h
Go to the documentation of this file.
1// Copyright (c) 2011-2022 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 {
64 TransactionCreationFailed, // Error returned when wallet is still locked
66 };
67
69 {
70 NoKeys, // wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)
71 Unencrypted, // !wallet->IsCrypted()
72 Locked, // wallet->IsCrypted() && wallet->IsLocked()
73 Unlocked // wallet->IsCrypted() && !wallet->IsLocked()
74 };
75
80
82
83 // Check address for validity
84 bool validateAddress(const QString& address) const;
85
86 // Return status record for SendCoins, contains error id + information
88 {
89 SendCoinsReturn(StatusCode _status = OK, QString _reasonCommitFailed = "")
90 : status(_status),
91 reasonCommitFailed(_reasonCommitFailed)
92 {
93 }
96 };
97
98 // prepare transaction for getting txfee before sending coins
100
101 // Send coins to a list of recipients
102 void sendCoins(WalletModelTransaction& transaction);
103
104 // Wallet encryption
105 bool setWalletEncrypted(const SecureString& passphrase);
106 // Passphrase only needed when unlocking
107 bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
108 bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
109
110 // RAII object for unlocking wallet, returned by requestUnlock()
112 {
113 public:
116
117 bool isValid() const { return valid; }
118
119 // Disable unused copy/move constructors/assignments explicitly.
120 UnlockContext(const UnlockContext&) = delete;
124
125 private:
127 const bool valid;
128 const bool relock;
129 };
130
132
133 bool bumpFee(Txid hash, Txid& new_hash);
134 void displayAddress(std::string sAddress) const;
135
136 static bool isWalletEnabled();
137
138 interfaces::Node& node() const { return m_node; }
139 interfaces::Wallet& wallet() const { return *m_wallet; }
141 void setClientModel(ClientModel* client_model);
142
143 QString getWalletName() const;
144 QString getDisplayName() const;
145
146 bool isMultiwallet() const;
147
148 void refresh(bool pk_hash_only = false);
149
151
152 // Retrieve the cached wallet balance
154
155 // If coin control has selected outputs, searches the total amount inside the wallet.
156 // Otherwise, uses the wallet's cached available balance.
158
159private:
160 std::unique_ptr<interfaces::Wallet> m_wallet;
161 std::unique_ptr<interfaces::Handler> m_handler_unload;
162 std::unique_ptr<interfaces::Handler> m_handler_status_changed;
163 std::unique_ptr<interfaces::Handler> m_handler_address_book_changed;
164 std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
165 std::unique_ptr<interfaces::Handler> m_handler_show_progress;
166 std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
169
171
172 // Wallet has an options model for wallet-specific options
173 // (transaction fee, for example)
175
179
180 // Cache some values to be able to detect changes
183 QTimer* timer;
184
185 // Block hash denoting when the last balance update was done.
187
190 void checkBalanceChanged(const interfaces::WalletBalances& new_balances);
191
192Q_SIGNALS:
193 // Signal that balance in wallet changed
195
196 // Encryption status of wallet changed
198
199 // Signal emitted when wallet needs to be unlocked
200 // It is valid behaviour for listeners to keep the wallet locked after this signal;
201 // this means that the unlocking failed or was cancelled.
203
204 // Fired when a message should be reported to the user
205 void message(const QString &title, const QString &message, unsigned int style);
206
207 // Coins sent: from wallet, to recipient, in (serialized) transaction:
208 void coinsSent(WalletModel* wallet, SendCoinsRecipient recipient, QByteArray transaction);
209
210 // Show progress dialog e.g. for rescan
211 void showProgress(const QString &title, int nProgress);
212
213 // Signal that wallet is about to be removed
214 void unload();
215
216 // Notify that there are now keys in the keypool
218
220
221public Q_SLOTS:
222 /* Starts a timer to periodically update the balance */
223 void startPollBalance();
224
225 /* Wallet status might have changed */
226 void updateStatus();
227 /* New transaction, or transaction changed status */
228 void updateTransaction();
229 /* New, updated or removed address book entry */
230 void updateAddressBook(const QString &address, const QString &label, bool isMine, wallet::AddressPurpose purpose, int status);
231 /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
232 void pollBalanceChanged();
233};
234
235#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:24
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
An encapsulated public key.
Definition: pubkey.h:34
Model for Bitcoin network client.
Definition: clientmodel.h:57
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:43
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:174
AddressTableModel * addressTableModel
Definition: walletmodel.h:176
RecentRequestsTableModel * getRecentRequestsTableModel() const
EncryptionStatus cachedEncryptionStatus
Definition: walletmodel.h:182
void refresh(bool pk_hash_only=false)
uint256 m_cached_last_update_tip
Definition: walletmodel.h:186
ClientModel * m_client_model
Definition: walletmodel.h:167
bool bumpFee(Txid hash, Txid &new_hash)
interfaces::Node & m_node
Definition: walletmodel.h:168
ClientModel & clientModel() const
Definition: walletmodel.h:140
interfaces::Node & node() const
Definition: walletmodel.h:138
std::unique_ptr< interfaces::Handler > m_handler_transaction_changed
Definition: walletmodel.h:164
void startPollBalance()
Definition: walletmodel.cpp:62
void pollBalanceChanged()
Definition: walletmodel.cpp:91
AddressTableModel * getAddressTableModel() const
RecentRequestsTableModel * recentRequestsTableModel
Definition: walletmodel.h:178
SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const wallet::CCoinControl &coinControl)
TransactionTableModel * transactionTableModel
Definition: walletmodel.h:177
bool setWalletEncrypted(const SecureString &passphrase)
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
void showProgress(const QString &title, int nProgress)
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
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:76
void updateStatus()
Definition: walletmodel.cpp:82
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:166
std::unique_ptr< interfaces::Handler > m_handler_unload
Definition: walletmodel.h:161
void displayAddress(std::string sAddress) const
EncryptionStatus getEncryptionStatus() const
TransactionTableModel * getTransactionTableModel() const
interfaces::Wallet & wallet() const
Definition: walletmodel.h:139
std::unique_ptr< interfaces::Handler > m_handler_status_changed
Definition: walletmodel.h:162
interfaces::WalletBalances m_cached_balances
Definition: walletmodel.h:181
bool fForceCheckBalanceChanged
Definition: walletmodel.h:170
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:183
WalletModel(std::unique_ptr< interfaces::Wallet > wallet, ClientModel &client_model, const PlatformStyle *platformStyle, QObject *parent=nullptr)
Definition: walletmodel.cpp:42
std::unique_ptr< interfaces::Handler > m_handler_address_book_changed
Definition: walletmodel.h:163
void encryptionStatusChanged()
std::unique_ptr< interfaces::Wallet > m_wallet
Definition: walletmodel.h:160
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:165
void unload()
@ AmountWithFeeExceedsBalance
Definition: walletmodel.h:62
@ TransactionCreationFailed
Definition: walletmodel.h:64
@ AmountExceedsBalance
Definition: walletmodel.h:61
@ DuplicateAddress
Definition: walletmodel.h:63
void subscribeToCoreSignals()
Data model for a walletmodel transaction.
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:71
Interface for accessing a wallet.
Definition: wallet.h:67
256-bit opaque blob.
Definition: uint256.h:196
Coin Control Features.
Definition: coincontrol.h:81
AddressPurpose
Address purpose field that has been been stored with wallet sending and receiving addresses since BIP...
Definition: types.h:61
OutputType
Definition: outputtype.h:17
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:58
SendCoinsReturn(StatusCode _status=OK, QString _reasonCommitFailed="")
Definition: walletmodel.h:89
Collection of wallet balances.
Definition: wallet.h:371