Inside Bitcoin
Opening the selected file…
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_WALLETDB_H7#define BITCOIN_WALLET_WALLETDB_H9#include <key.h>10#include <primitives/transaction.h>11#include <primitives/transaction_identifier.h>12#include <script/sign.h>13#include <wallet/db.h>14#include <wallet/walletutil.h>16#include <cstdint>17#include <string>18#include <unordered_set>19#include <vector>21class CScript;22class uint160;23class uint256;24struct CBlockLocator;26namespace wallet {27class CMasterKey;28class CWallet;29class CWalletTx;30struct WalletContext;32// Logs information about the database, including available engines, features, and other capabilities33void LogDBInfo();35/**36* Overview of wallet database classes:37*38* - WalletBatch is an abstract modifier object for the wallet database, and encapsulates a database39* batch update as well as methods to act on the database. It should be agnostic to the database implementation.40*/42/** Error statuses for the wallet database.43* Values are in order of severity. When multiple errors occur, the most severe (highest value) will be returned.44*/45enum class DBErrors : int46{47LOAD_OK = 0,48NEED_RESCAN = 1,49EXTERNAL_SIGNER_SUPPORT_REQUIRED = 3,50NONCRITICAL_ERROR = 4,51TOO_NEW = 5,52UNKNOWN_DESCRIPTOR = 6,53LOAD_FAIL = 7,54UNEXPECTED_LEGACY_ENTRY = 8,55LEGACY_WALLET = 9,56CORRUPT = 10,57};59namespace DBKeys {60extern const std::string ACENTRY;61extern const std::string ACTIVEEXTERNALSPK;62extern const std::string ACTIVEINTERNALSPK;63extern const std::string BESTBLOCK;64extern const std::string BESTBLOCK_NOMERKLE;65extern const std::string CRYPTED_KEY;66extern const std::string CSCRIPT;67extern const std::string DEFAULTKEY;68extern const std::string DESTDATA;69extern const std::string FLAGS;70extern const std::string HDCHAIN;71extern const std::string KEY;72extern const std::string KEYMETA;73extern const std::string LOCKED_UTXO;74extern const std::string MASTER_KEY;75extern const std::string MINVERSION;76extern const std::string NAME;77extern const std::string OLD_KEY;78extern const std::string ORDERPOSNEXT;79extern const std::string POOL;80extern const std::string PURPOSE;81extern const std::string SETTINGS;82extern const std::string TX;83extern const std::string WTX_VARIANT;84extern const std::string VERSION;85extern const std::string WALLETDESCRIPTOR;86extern const std::string WALLETDESCRIPTORCKEY;87extern const std::string WALLETDESCRIPTORKEY;88extern const std::string WATCHMETA;89extern const std::string WATCHS;91// Keys in this set pertain only to legacy wallets and are removed during migration to descriptors.92extern const std::unordered_set<std::string> LEGACY_TYPES;93} // namespace DBKeys95/* simple HD chain data model */96class CHDChain97{98public:99uint32_t nExternalChainCounter;100uint32_t nInternalChainCounter;101CKeyID seed_id; //!< seed hash160103static constexpr int VERSION_HD_BASE{1};104static constexpr int VERSION_HD_CHAIN_SPLIT{2};105static constexpr int CURRENT_VERSION{VERSION_HD_CHAIN_SPLIT};106int nVersion;108CHDChain() { SetNull(); }110SERIALIZE_METHODS(CHDChain, obj)111{112READWRITE(obj.nVersion, obj.nExternalChainCounter, obj.seed_id);113if (obj.nVersion >= VERSION_HD_CHAIN_SPLIT) {114READWRITE(obj.nInternalChainCounter);115}116}118void SetNull()119{120nVersion = CHDChain::CURRENT_VERSION;121nExternalChainCounter = 0;122nInternalChainCounter = 0;123seed_id.SetNull();124}126bool operator==(const CHDChain& chain) const127{128return seed_id == chain.seed_id;129}130bool operator<(const CHDChain& chain) const131{132return seed_id < chain.seed_id;133}134};136class CKeyMetadata137{138public:139static constexpr int VERSION_BASIC{1};140static constexpr int VERSION_WITH_HDDATA{10};141static constexpr int VERSION_WITH_KEY_ORIGIN{12};142static constexpr int CURRENT_VERSION{VERSION_WITH_KEY_ORIGIN};143int nVersion;144int64_t nCreateTime; // 0 means unknown145std::string hdKeypath; //optional HD/bip32 keypath. Still used to determine whether a key is a seed. Also kept for backwards compatibility146CKeyID hd_seed_id; //id of the HD seed used to derive this key147KeyOriginInfo key_origin; // Key origin info with path and fingerprint148bool has_key_origin = false; //!< Whether the key_origin is useful150CKeyMetadata()151{152SetNull();153}154explicit CKeyMetadata(int64_t nCreateTime_)155{156SetNull();157nCreateTime = nCreateTime_;158}160SERIALIZE_METHODS(CKeyMetadata, obj)161{162READWRITE(obj.nVersion, obj.nCreateTime);163if (obj.nVersion >= VERSION_WITH_HDDATA) {164READWRITE(obj.hdKeypath, obj.hd_seed_id);165}166if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN)167{168READWRITE(obj.key_origin);169READWRITE(obj.has_key_origin);170}171}173void SetNull()174{175nVersion = CKeyMetadata::CURRENT_VERSION;176nCreateTime = 0;177hdKeypath.clear();178hd_seed_id.SetNull();179key_origin.clear();180has_key_origin = false;181}182};184struct DbTxnListener185{186std::function<void()> on_commit, on_abort;187};189/** Access to the wallet database.190* Opens the database and provides read and write access to it. Each read and write is its own transaction.191* Multiple operation transactions can be started using TxnBegin() and committed using TxnCommit()192* Otherwise the transaction will be committed when the object goes out of scope.193* Optionally (on by default) it will flush to disk on close.194* Every 1000 writes will automatically trigger a flush to disk.195*/196class WalletBatch197{198private:199template <typename K, typename T>200bool WriteIC(const K& key, const T& value, bool fOverwrite = true)201{202if (!m_batch->Write(key, value, fOverwrite)) {203return false;204}205return true;206}208template <typename K>209bool EraseIC(const K& key)210{211if (!m_batch->Erase(key)) {212return false;213}214return true;215}217public:218explicit WalletBatch(WalletDatabase &database) :219m_batch(database.MakeBatch())220{221}222WalletBatch(const WalletBatch&) = delete;223WalletBatch& operator=(const WalletBatch&) = delete;225bool WriteName(const std::string& strAddress, const std::string& strName);226bool EraseName(const std::string& strAddress);228bool WritePurpose(const std::string& strAddress, const std::string& purpose);229bool ErasePurpose(const std::string& strAddress);231// Write a CWalletTx and all variant witness txs (single tx record and multiple wtxvariant records)232bool WriteFullTx(const CWalletTx& wtx);233bool EraseTx(Txid hash);234// Write a single witness variant of CWalletTx (single wtxvariant record)235bool WriteWtxVariant(const Txid& txid, const CTransactionRef& tx);236// Write only the canonical witness tx and all of the tx metadata (single tx record)237bool WriteTxMetadata(const CWalletTx& wtx);239bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, bool overwrite);240bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);241bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);242bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);243bool EraseMasterKey(unsigned int id);245bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);247bool WriteBestBlock(const CBlockLocator& locator);248bool ReadBestBlock(CBlockLocator& locator);250// Returns true if wallet stores encryption keys
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.