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) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#ifndef BITCOIN_WALLET_WALLET_H7#define BITCOIN_WALLET_WALLET_H9#include <addresstype.h>10#include <consensus/amount.h>11#include <interfaces/chain.h>12#include <interfaces/handler.h>13#include <kernel/cs_main.h>14#include <node/types.h>15#include <outputtype.h>16#include <policy/feerate.h>17#include <primitives/transaction.h>18#include <primitives/transaction_identifier.h>19#include <script/interpreter.h>20#include <script/script.h>21#include <support/allocators/secure.h>22#include <sync.h>23#include <tinyformat.h>24#include <uint256.h>25#include <util/btcsignals.h>26#include <util/expected.h>27#include <util/fs.h>28#include <util/hasher.h>29#include <util/log.h>30#include <util/result.h>31#include <util/string.h>32#include <util/time.h>33#include <util/ui_change_type.h>34#include <wallet/crypter.h>35#include <wallet/db.h>36#include <wallet/scriptpubkeyman.h>37#include <wallet/transaction.h>38#include <wallet/types.h>39#include <wallet/walletutil.h>41#include <atomic>42#include <cassert>43#include <cstddef>44#include <cstdint>45#include <functional>46#include <limits>47#include <map>48#include <memory>49#include <optional>50#include <set>51#include <string>52#include <unordered_map>53#include <utility>54#include <vector>56class CKey;57class CKeyID;58class CPubKey;59class Coin;60class SigningProvider;61enum class MemPoolRemovalReason;62enum class SigningResult;63namespace common {64enum class PSBTError;65} // namespace common66namespace interfaces {67class Wallet;68}69namespace wallet {70class CWallet;71class WalletBatch;72enum class DBErrors : int;73} // namespace wallet74struct CBlockLocator;75struct CExtKey;76struct FlatSigningProvider;77struct KeyOriginInfo;78class PartiallySignedTransaction;79struct SignatureData;81using LoadWalletFn = std::function<void(std::unique_ptr<interfaces::Wallet> wallet)>;83struct bilingual_str;85namespace wallet {86struct WalletContext;88//! Explicitly delete the wallet.89//! Blocks the current thread until the wallet is destructed.90void WaitForDeleteWallet(std::shared_ptr<CWallet>&& wallet);92bool AddWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet);93bool RemoveWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet, std::optional<bool> load_on_start, std::vector<bilingual_str>& warnings);94bool RemoveWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet, std::optional<bool> load_on_start);95std::vector<std::shared_ptr<CWallet>> GetWallets(WalletContext& context);96std::shared_ptr<CWallet> GetDefaultWallet(WalletContext& context, size_t& count);97std::shared_ptr<CWallet> GetWallet(WalletContext& context, const std::string& name);98std::shared_ptr<CWallet> LoadWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);99std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);100std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const fs::path& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings, bool load_after_restore = true, bool allow_unnamed = false);101std::unique_ptr<interfaces::Handler> HandleLoadWallet(WalletContext& context, LoadWalletFn load_wallet);102void NotifyWalletLoaded(WalletContext& context, const std::shared_ptr<CWallet>& wallet);103std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);105//! -fallbackfee default106inline constexpr CAmount DEFAULT_FALLBACK_FEE = 0;107//! -discardfee default108inline constexpr CAmount DEFAULT_DISCARD_FEE{10'000};109//! -mintxfee default110inline constexpr CAmount DEFAULT_TRANSACTION_MINFEE = 1000;111//! -consolidatefeerate default112inline constexpr CAmount DEFAULT_CONSOLIDATE_FEERATE{10'000}; // 10 sat/vbyte113/**114* maximum fee increase allowed to do partial spend avoidance, even for nodes with this feature disabled by default115*116* A value of -1 disables this feature completely.117* A value of 0 (current default) means to attempt to do partial spend avoidance, and use its results if the fees remain *unchanged*118* A value > 0 means to do partial spend avoidance if the fee difference against a regular coin selection instance is in the range [0..value].119*/120inline constexpr CAmount DEFAULT_MAX_AVOIDPARTIALSPEND_FEE = 0;121//! discourage APS fee higher than this amount122inline constexpr CAmount HIGH_APS_FEE{COIN / 10000};123//! minimum recommended increment for replacement txs124inline constexpr CAmount WALLET_INCREMENTAL_RELAY_FEE = 5000;125//! Default for -spendzeroconfchange126inline constexpr bool DEFAULT_SPEND_ZEROCONF_CHANGE = true;127//! Default for -walletrejectlongchains128inline constexpr bool DEFAULT_WALLET_REJECT_LONG_CHAINS{true};129//! -txconfirmtarget default130inline constexpr unsigned int DEFAULT_TX_CONFIRM_TARGET = 6;131//! -walletrbf default132inline constexpr bool DEFAULT_WALLET_RBF = true;133inline constexpr bool DEFAULT_WALLETBROADCAST = true;134inline constexpr bool DEFAULT_DISABLE_WALLET = false;135inline constexpr bool DEFAULT_WALLETCROSSCHAIN = false;136//! -maxtxfee default137inline constexpr CAmount DEFAULT_TRANSACTION_MAXFEE{COIN / 10};138//! Discourage users to set fees higher than this amount (in satoshis) per kB139inline constexpr CAmount HIGH_TX_FEE_PER_KB{COIN / 100};140//! -maxtxfee will warn if called with a higher fee than this amount (in satoshis)141inline constexpr CAmount HIGH_MAX_TX_FEE{100 * HIGH_TX_FEE_PER_KB};142//! Pre-calculated constants for input size estimation in *virtual size*143inline constexpr size_t DUMMY_NESTED_P2WPKH_INPUT_SIZE = 91;145class CCoinControl;147//! Default for -addresstype148inline constexpr OutputType DEFAULT_ADDRESS_TYPE{OutputType::BECH32};150inline constexpr uint64_t KNOWN_WALLET_FLAGS =151WALLET_FLAG_AVOID_REUSE152| WALLET_FLAG_BLANK_WALLET153| WALLET_FLAG_KEY_ORIGIN_METADATA154| WALLET_FLAG_LAST_HARDENED_XPUB_CACHED155| WALLET_FLAG_DISABLE_PRIVATE_KEYS156| WALLET_FLAG_DESCRIPTORS157| WALLET_FLAG_EXTERNAL_SIGNER;159inline constexpr uint64_t MUTABLE_WALLET_FLAGS =160WALLET_FLAG_AVOID_REUSE;162static const std::map<WalletFlags, std::string> WALLET_FLAG_TO_STRING{163{WALLET_FLAG_AVOID_REUSE, "avoid_reuse"},164{WALLET_FLAG_BLANK_WALLET, "blank"},165{WALLET_FLAG_KEY_ORIGIN_METADATA, "key_origin_metadata"},166{WALLET_FLAG_LAST_HARDENED_XPUB_CACHED, "last_hardened_xpub_cached"},167{WALLET_FLAG_DISABLE_PRIVATE_KEYS, "disable_private_keys"},168{WALLET_FLAG_DESCRIPTORS, "descriptor_wallet"},169{WALLET_FLAG_EXTERNAL_SIGNER, "external_signer"}170};172static const std::map<std::string, WalletFlags> STRING_TO_WALLET_FLAG{173{WALLET_FLAG_TO_STRING.at(WALLET_FLAG_AVOID_REUSE), WALLET_FLAG_AVOID_REUSE},174{WALLET_FLAG_TO_STRING.at(WALLET_FLAG_BLANK_WALLET), WALLET_FLAG_BLANK_WALLET},175{WALLET_FLAG_TO_STRING.at(WALLET_FLAG_KEY_ORIGIN_METADATA), WALLET_FLAG_KEY_ORIGIN_METADATA},176{WALLET_FLAG_TO_STRING.at(WALLET_FLAG_LAST_HARDENED_XPUB_CACHED), WALLET_FLAG_LAST_HARDENED_XPUB_CACHED},177{WALLET_FLAG_TO_STRING.at(WALLET_FLAG_DISABLE_PRIVATE_KEYS), WALLET_FLAG_DISABLE_PRIVATE_KEYS},178{WALLET_FLAG_TO_STRING.at(WALLET_FLAG_DESCRIPTORS), WALLET_FLAG_DESCRIPTORS},179{WALLET_FLAG_TO_STRING.at(WALLET_FLAG_EXTERNAL_SIGNER), WALLET_FLAG_EXTERNAL_SIGNER}180};182/** A wrapper to reserve an address from a wallet183*184* ReserveDestination is used to reserve an address.185* It is currently only used inside of CreateTransaction.186*187* Instantiating a ReserveDestination does not reserve an address. To do so,188* GetReservedDestination() needs to be called on the object. Once an address has been189* reserved, call KeepDestination() on the ReserveDestination object to make sure it is not190* returned. Call ReturnDestination() to return the address so it can be reused (for191* example, if the address was used in a new transaction192* and that transaction was not completed and needed to be aborted).193*194* If an address is reserved and KeepDestination() is not called, then the address will be195* returned when the ReserveDestination goes out of scope.196*/197class ReserveDestination198{199protected:200//! The wallet to reserve from201const CWallet* const pwallet;202//! The ScriptPubKeyMan to reserve from. Based on type when GetReservedDestination is called203ScriptPubKeyMan* m_spk_man{nullptr};204OutputType const type;205//! The index of the address's key in the keypool206int64_t nIndex{-1};207//! The destination208CTxDestination address;209//! Whether this is from the internal (change output) keypool210bool fInternal{false};212public:213//! Construct a ReserveDestination object. This does NOT reserve an address yet214explicit ReserveDestination(CWallet* pwallet, OutputType type)215: pwallet(pwallet)216, type(type) { }218ReserveDestination(const ReserveDestination&) = delete;219ReserveDestination& operator=(const ReserveDestination&) = delete;221//! Destructor. If a key has been reserved and not KeepKey'ed, it will be returned to the keypool222~ReserveDestination()223{224ReturnDestination();225}227//! Reserve an address228util::Result<CTxDestination> GetReservedDestination(bool internal);229//! Return reserved address230void ReturnDestination();231//! Keep the address. Do not return its key to the keypool when this object goes out of scope232void KeepDestination();233};235/**236* Address book data.237*/238struct CAddressBookData239{240/**241* Address label which is always nullopt for change addresses. For sending242* and receiving addresses, it will be set to an arbitrary label string243* provided by the user, or to "", which is the default label. The presence244* or absence of a label is used to distinguish change addresses from245* non-change addresses by wallet transaction listing and fee bumping code.246*/247std::optional<std::string> label;249/**250* Address purpose which was originally recorded for payment protocol
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.