Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2011-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_QT_WALLETMODEL_H6#define BITCOIN_QT_WALLETMODEL_H8#include <key.h>10#include <qt/walletmodeltransaction.h>12#include <interfaces/wallet.h>13#include <primitives/transaction_identifier.h>14#include <support/allocators/secure.h>16#include <vector>18#include <QObject>20enum class OutputType;22class AddressTableModel;23class ClientModel;24class OptionsModel;25class PlatformStyle;26class RecentRequestsTableModel;27class SendCoinsRecipient;28class TransactionTableModel;29class WalletModelTransaction;31class CKeyID;32class COutPoint;33class CPubKey;34class uint256;36namespace interfaces {37class Node;38} // namespace interfaces39namespace wallet {40class CCoinControl;41} // namespace wallet43QT_BEGIN_NAMESPACE44class QTimer;45QT_END_NAMESPACE47/** Interface to Bitcoin wallet from Qt view code. */48class WalletModel : public QObject49{50Q_OBJECT52public:53explicit WalletModel(std::unique_ptr<interfaces::Wallet> wallet, ClientModel& client_model, const PlatformStyle *platformStyle, QObject *parent = nullptr);54~WalletModel();56enum StatusCode // Returned by sendCoins57{58OK,59InvalidAmount,60InvalidAddress,61AmountExceedsBalance,62DuplicateAddress,63TransactionCreationFailed, // Error returned when wallet is still locked64AbsurdFee65};67enum EncryptionStatus68{69NoKeys, // wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)70Unencrypted, // !wallet->HasEncryptionKeys()71Locked, // wallet->HasEncryptionKeys() && wallet->IsLocked()72Unlocked // wallet->HasEncryptionKeys() && !wallet->IsLocked()73};75OptionsModel* getOptionsModel() const;76AddressTableModel* getAddressTableModel() const;77TransactionTableModel* getTransactionTableModel() const;78RecentRequestsTableModel* getRecentRequestsTableModel() const;80EncryptionStatus getEncryptionStatus() const;82// Check address for validity83bool validateAddress(const QString& address) const;85// Return status record for SendCoins, contains error id + information86struct SendCoinsReturn87{88SendCoinsReturn(StatusCode _status = OK, QString _reasonCommitFailed = "")89: status(_status),90reasonCommitFailed(_reasonCommitFailed)91{92}93StatusCode status;94QString reasonCommitFailed;95};97// prepare transaction for getting txfee before sending coins98SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const wallet::CCoinControl& coinControl);100// Send coins to a list of recipients101void sendCoins(WalletModelTransaction& transaction);103// Wallet encryption104bool setWalletEncrypted(const SecureString& passphrase);105// Passphrase only needed when unlocking106bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());107bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);109// RAII object for unlocking wallet, returned by requestUnlock()110class UnlockContext111{112public:113UnlockContext(WalletModel *wallet, bool valid, bool relock);114~UnlockContext();116bool isValid() const { return valid; }118// Disable unused copy/move constructors/assignments explicitly.119UnlockContext(const UnlockContext&) = delete;120UnlockContext(UnlockContext&&) = delete;121UnlockContext& operator=(const UnlockContext&) = delete;122UnlockContext& operator=(UnlockContext&&) = delete;124private:125WalletModel *wallet;126const bool valid;127const bool relock;128};130UnlockContext requestUnlock();132bool bumpFee(Txid hash, Txid& new_hash);133void displayAddress(std::string sAddress) const;135static bool isWalletEnabled();137interfaces::Node& node() const { return m_node; }138interfaces::Wallet& wallet() const { return *m_wallet; }139ClientModel& clientModel() const { return *m_client_model; }140void setClientModel(ClientModel* client_model);142QString getWalletName() const;143QString getDisplayName() const;145bool isMultiwallet() const;147void refresh(bool pk_hash_only = false);149uint256 getLastBlockProcessed() const;151// Retrieve the cached wallet balance152interfaces::WalletBalances getCachedBalance() const;154// If coin control has selected outputs, searches the total amount inside the wallet.155// Otherwise, uses the wallet's cached available balance.156CAmount getAvailableBalance(const wallet::CCoinControl* control);158private:159std::unique_ptr<interfaces::Wallet> m_wallet;160std::unique_ptr<interfaces::Handler> m_handler_unload;161std::unique_ptr<interfaces::Handler> m_handler_status_changed;162std::unique_ptr<interfaces::Handler> m_handler_address_book_changed;163std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;164std::unique_ptr<interfaces::Handler> m_handler_show_progress;165std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;166ClientModel* m_client_model;167interfaces::Node& m_node;169bool fForceCheckBalanceChanged{false};171// Wallet has an options model for wallet-specific options172// (transaction fee, for example)173OptionsModel *optionsModel;175AddressTableModel* addressTableModel{nullptr};176TransactionTableModel* transactionTableModel{nullptr};177RecentRequestsTableModel* recentRequestsTableModel{nullptr};179// Cache some values to be able to detect changes180interfaces::WalletBalances m_cached_balances;181EncryptionStatus cachedEncryptionStatus{Unencrypted};182QTimer* timer;184// Block hash denoting when the last balance update was done.185uint256 m_cached_last_update_tip{};187void subscribeToCoreSignals();188void unsubscribeFromCoreSignals();189void checkBalanceChanged(const interfaces::WalletBalances& new_balances);191Q_SIGNALS:192// Signal that balance in wallet changed193void balanceChanged(const interfaces::WalletBalances& balances);195// Encryption status of wallet changed196void encryptionStatusChanged();198// Signal emitted when wallet needs to be unlocked199// It is valid behaviour for listeners to keep the wallet locked after this signal;200// this means that the unlocking failed or was cancelled.201void requireUnlock();203// Fired when a message should be reported to the user204void message(const QString &title, const QString &message, unsigned int style);206// Coins sent: from wallet, to recipient, in (serialized) transaction:207void coinsSent(WalletModel* wallet, SendCoinsRecipient recipient, QByteArray transaction);209// Show progress dialog e.g. for rescan210void showProgress(const QString &title, int nProgress);212// Signal that wallet is about to be removed213void unload();215// Notify that there are now keys in the keypool216void canGetAddressesChanged();218void timerTimeout();220public Q_SLOTS:221/* Starts a timer to periodically update the balance */222void startPollBalance();224/* Wallet status might have changed */225void updateStatus();226/* New transaction, or transaction changed status */227void updateTransaction();228/* New, updated or removed address book entry */229void updateAddressBook(const QString &address, const QString &label, bool isMine, wallet::AddressPurpose purpose, int status);230/* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */231void pollBalanceChanged();232};234#endif // BITCOIN_QT_WALLETMODEL_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.