Bitcoin Core 31.99.0
P2P Digital Currency
walletdb.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-present The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef BITCOIN_WALLET_WALLETDB_H
7#define BITCOIN_WALLET_WALLETDB_H
8
9#include <key.h>
11#include <script/sign.h>
12#include <wallet/db.h>
13#include <wallet/walletutil.h>
14
15#include <cstdint>
16#include <string>
17#include <unordered_set>
18#include <vector>
19
20class CScript;
21class uint160;
22class uint256;
23struct CBlockLocator;
24
25namespace wallet {
26class CMasterKey;
27class CWallet;
28class CWalletTx;
29struct WalletContext;
30
31// Logs information about the database, including available engines, features, and other capabilities
32void LogDBInfo();
33
44enum class DBErrors : int
45{
46 LOAD_OK = 0,
47 NEED_RESCAN = 1,
50 TOO_NEW = 5,
52 LOAD_FAIL = 7,
54 LEGACY_WALLET = 9,
55 CORRUPT = 10,
56};
57
58namespace DBKeys {
59extern const std::string ACENTRY;
60extern const std::string ACTIVEEXTERNALSPK;
61extern const std::string ACTIVEINTERNALSPK;
62extern const std::string BESTBLOCK;
63extern const std::string BESTBLOCK_NOMERKLE;
64extern const std::string CRYPTED_KEY;
65extern const std::string CSCRIPT;
66extern const std::string DEFAULTKEY;
67extern const std::string DESTDATA;
68extern const std::string FLAGS;
69extern const std::string HDCHAIN;
70extern const std::string KEY;
71extern const std::string KEYMETA;
72extern const std::string LOCKED_UTXO;
73extern const std::string MASTER_KEY;
74extern const std::string MINVERSION;
75extern const std::string NAME;
76extern const std::string OLD_KEY;
77extern const std::string ORDERPOSNEXT;
78extern const std::string POOL;
79extern const std::string PURPOSE;
80extern const std::string SETTINGS;
81extern const std::string TX;
82extern const std::string VERSION;
83extern const std::string WALLETDESCRIPTOR;
84extern const std::string WALLETDESCRIPTORCKEY;
85extern const std::string WALLETDESCRIPTORKEY;
86extern const std::string WATCHMETA;
87extern const std::string WATCHS;
88
89// Keys in this set pertain only to the legacy wallet (LegacyScriptPubKeyMan) and are removed during migration from legacy to descriptors.
90extern const std::unordered_set<std::string> LEGACY_TYPES;
91} // namespace DBKeys
92
93/* simple HD chain data model */
95{
96public:
100 int64_t m_next_external_index{0}; // Next index in the keypool to be used. Memory only.
101 int64_t m_next_internal_index{0}; // Next index in the keypool to be used. Memory only.
102
103 static const int VERSION_HD_BASE = 1;
104 static const int VERSION_HD_CHAIN_SPLIT = 2;
107
109
111 {
112 READWRITE(obj.nVersion, obj.nExternalChainCounter, obj.seed_id);
113 if (obj.nVersion >= VERSION_HD_CHAIN_SPLIT) {
114 READWRITE(obj.nInternalChainCounter);
115 }
116 }
117
118 void SetNull()
119 {
124 }
125
126 bool operator==(const CHDChain& chain) const
127 {
128 return seed_id == chain.seed_id;
129 }
130 bool operator<(const CHDChain& chain) const
131 {
132 return seed_id < chain.seed_id;
133 }
134};
135
137{
138public:
139 static const int VERSION_BASIC=1;
140 static const int VERSION_WITH_HDDATA=10;
141 static const int VERSION_WITH_KEY_ORIGIN = 12;
144 int64_t nCreateTime; // 0 means unknown
145 std::string hdKeypath; //optional HD/bip32 keypath. Still used to determine whether a key is a seed. Also kept for backwards compatibility
146 CKeyID hd_seed_id; //id of the HD seed used to derive this key
147 KeyOriginInfo key_origin; // Key origin info with path and fingerprint
148 bool has_key_origin = false;
149
151 {
152 SetNull();
153 }
154 explicit CKeyMetadata(int64_t nCreateTime_)
155 {
156 SetNull();
157 nCreateTime = nCreateTime_;
158 }
159
161 {
162 READWRITE(obj.nVersion, obj.nCreateTime);
163 if (obj.nVersion >= VERSION_WITH_HDDATA) {
164 READWRITE(obj.hdKeypath, obj.hd_seed_id);
165 }
166 if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN)
167 {
168 READWRITE(obj.key_origin);
169 READWRITE(obj.has_key_origin);
170 }
171 }
172
173 void SetNull()
174 {
176 nCreateTime = 0;
177 hdKeypath.clear();
180 has_key_origin = false;
181 }
182};
183
185{
186 std::function<void()> on_commit, on_abort;
187};
188
197{
198private:
199 template <typename K, typename T>
200 bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
201 {
202 if (!m_batch->Write(key, value, fOverwrite)) {
203 return false;
204 }
205 return true;
206 }
207
208 template <typename K>
209 bool EraseIC(const K& key)
210 {
211 if (!m_batch->Erase(key)) {
212 return false;
213 }
214 return true;
215 }
216
217public:
218 explicit WalletBatch(WalletDatabase &database) :
219 m_batch(database.MakeBatch())
220 {
221 }
222 WalletBatch(const WalletBatch&) = delete;
224
225 bool WriteName(const std::string& strAddress, const std::string& strName);
226 bool EraseName(const std::string& strAddress);
227
228 bool WritePurpose(const std::string& strAddress, const std::string& purpose);
229 bool ErasePurpose(const std::string& strAddress);
230
231 bool WriteTx(const CWalletTx& wtx);
232 bool EraseTx(Txid hash);
233
234 bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, bool overwrite);
235 bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
236 bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
237 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
238 bool EraseMasterKey(unsigned int id);
239
240 bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
241 bool EraseWatchOnly(const CScript &script);
242
243 bool WriteBestBlock(const CBlockLocator& locator);
244 bool ReadBestBlock(CBlockLocator& locator);
245
246 // Returns true if wallet stores encryption keys
247 bool IsEncrypted();
248
249 bool WriteOrderPosNext(int64_t nOrderPosNext);
250
251 bool WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey);
252 bool WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret);
253 bool WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor);
254 bool WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index);
255 bool WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
256 bool WriteDescriptorLastHardenedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
257 bool WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache);
258
259 bool WriteLockedUTXO(const COutPoint& output);
260 bool EraseLockedUTXO(const COutPoint& output);
261
262 bool WriteAddressPreviouslySpent(const CTxDestination& dest, bool previously_spent);
263 bool WriteAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& receive_request);
264 bool EraseAddressReceiveRequest(const CTxDestination& dest, const std::string& id);
265 bool EraseAddressData(const CTxDestination& dest);
266
267 bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bool internal);
268 bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal);
269
270 DBErrors LoadWallet(CWallet* pwallet);
271
273 bool WriteVersion(int client_version) { return m_batch->Write(DBKeys::VERSION, CLIENT_VERSION); }
274
276 bool EraseRecords(const std::unordered_set<std::string>& types);
277
278 bool WriteWalletFlags(uint64_t flags);
280 bool TxnBegin();
282 bool TxnCommit();
284 bool TxnAbort();
285 bool HasActiveTxn() { return m_batch->HasActiveTxn(); }
286
288 void RegisterTxnListener(const DbTxnListener& l);
289
290private:
291 std::unique_ptr<DatabaseBatch> m_batch;
292
293 // External functions listening to the current db txn outcome.
294 // Listeners are cleared at the end of the transaction.
295 std::vector<DbTxnListener> m_txn_listeners;
296};
297
310bool RunWithinTxn(WalletDatabase& database, std::string_view process_desc, const std::function<bool(WalletBatch&)>& func);
311
312bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
313bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
314bool LoadEncryptionKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
315bool LoadHDChain(CWallet* pwallet, DataStream& ssValue, std::string& strErr);
316
320} // namespace wallet
321
322#endif // BITCOIN_WALLET_WALLETDB_H
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
int flags
Definition: bitcoin-tx.cpp:530
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
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:165
Cache for single descriptor's derived extended pubkeys.
Definition: descriptor.h:29
constexpr void SetNull()
Definition: uint256.h:56
160-bit opaque blob.
Definition: uint256.h:184
256-bit opaque blob.
Definition: uint256.h:196
SERIALIZE_METHODS(CHDChain, obj)
Definition: walletdb.h:110
void SetNull()
Definition: walletdb.h:118
uint32_t nInternalChainCounter
Definition: walletdb.h:98
static const int VERSION_HD_BASE
Definition: walletdb.h:103
uint32_t nExternalChainCounter
Definition: walletdb.h:97
int64_t m_next_external_index
Definition: walletdb.h:100
static const int VERSION_HD_CHAIN_SPLIT
Definition: walletdb.h:104
bool operator<(const CHDChain &chain) const
Definition: walletdb.h:130
CKeyID seed_id
seed hash160
Definition: walletdb.h:99
bool operator==(const CHDChain &chain) const
Definition: walletdb.h:126
int64_t m_next_internal_index
Definition: walletdb.h:101
static const int CURRENT_VERSION
Definition: walletdb.h:105
static const int VERSION_WITH_KEY_ORIGIN
Definition: walletdb.h:141
SERIALIZE_METHODS(CKeyMetadata, obj)
Definition: walletdb.h:160
std::string hdKeypath
Definition: walletdb.h:145
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:154
bool has_key_origin
Whether the key_origin is useful.
Definition: walletdb.h:148
KeyOriginInfo key_origin
Definition: walletdb.h:147
static const int VERSION_BASIC
Definition: walletdb.h:139
static const int VERSION_WITH_HDDATA
Definition: walletdb.h:140
static const int CURRENT_VERSION
Definition: walletdb.h:142
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key.
Definition: crypter.h:35
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:309
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:195
RAII class that provides access to a WalletDatabase.
Definition: db.h:51
Access to the wallet database.
Definition: walletdb.h:197
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:234
bool TxnAbort()
Abort current transaction.
Definition: walletdb.cpp:1278
bool WriteDescriptorParentCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index)
Definition: walletdb.cpp:246
bool EraseName(const std::string &strAddress)
Definition: walletdb.cpp:81
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:1102
bool WriteBestBlock(const CBlockLocator &locator)
Definition: walletdb.cpp:177
void RegisterTxnListener(const DbTxnListener &l)
Registers db txn callback functions.
Definition: walletdb.cpp:1291
bool ReadBestBlock(CBlockLocator &locator)
Definition: walletdb.cpp:183
bool WriteDescriptorCacheItems(const uint256 &desc_id, const DescriptorCache &cache)
Definition: walletdb.cpp:260
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition: walletdb.cpp:151
std::vector< DbTxnListener > m_txn_listeners
Definition: walletdb.h:295
bool WriteWalletFlags(uint64_t flags)
Definition: walletdb.cpp:1248
bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta)
Definition: walletdb.cpp:161
bool TxnBegin()
Begin a new transaction.
Definition: walletdb.cpp:1260
bool WriteAddressPreviouslySpent(const CTxDestination &dest, bool previously_spent)
Definition: walletdb.cpp:1225
bool EraseAddressReceiveRequest(const CTxDestination &dest, const std::string &id)
Definition: walletdb.cpp:1236
bool TxnCommit()
Commit current transaction.
Definition: walletdb.cpp:1265
bool WriteName(const std::string &strAddress, const std::string &strName)
Definition: walletdb.cpp:76
WalletBatch(WalletDatabase &database)
Definition: walletdb.h:218
bool WritePurpose(const std::string &strAddress, const std::string &purpose)
Definition: walletdb.cpp:88
WalletBatch & operator=(const WalletBatch &)=delete
std::unique_ptr< DatabaseBatch > m_batch
Definition: walletdb.h:291
bool EraseRecords(const std::unordered_set< std::string > &types)
Delete records of the given types.
Definition: walletdb.cpp:1253
WalletBatch(const WalletBatch &)=delete
bool WriteDescriptorLastHardenedCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index)
Definition: walletdb.cpp:253
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:200
bool EraseAddressData(const CTxDestination &dest)
Definition: walletdb.cpp:1241
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition: walletdb.cpp:200
bool WriteTx(const CWalletTx &wtx)
Definition: walletdb.cpp:98
bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:113
bool EraseIC(const K &key)
Definition: walletdb.h:209
bool WriteCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:125
bool ErasePurpose(const std::string &strAddress)
Definition: walletdb.cpp:93
bool EraseLockedUTXO(const COutPoint &output)
Definition: walletdb.cpp:287
bool WriteDescriptorDerivedCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index, uint32_t der_index)
Definition: walletdb.cpp:239
bool WriteCryptedDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey, const std::vector< unsigned char > &secret)
Definition: walletdb.cpp:225
bool WriteLockedUTXO(const COutPoint &output)
Definition: walletdb.cpp:282
bool EraseMasterKey(unsigned int id)
Definition: walletdb.cpp:156
bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id, bool internal)
Definition: walletdb.cpp:205
bool WriteVersion(int client_version)
Write the given client_version.
Definition: walletdb.h:273
bool EraseTx(Txid hash)
Definition: walletdb.cpp:103
bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal)
Definition: walletdb.cpp:211
bool WriteKeyMetadata(const CKeyMetadata &meta, const CPubKey &pubkey, bool overwrite)
Definition: walletdb.cpp:108
bool WriteDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey, const CPrivKey &privkey)
Definition: walletdb.cpp:217
bool WriteAddressReceiveRequest(const CTxDestination &dest, const std::string &id, const std::string &receive_request)
Definition: walletdb.cpp:1231
bool EraseWatchOnly(const CScript &script)
Definition: walletdb.cpp:169
An instance of this class represents one database.
Definition: db.h:130
Descriptor with some wallet metadata.
Definition: walletutil.h:64
static const int CLIENT_VERSION
Definition: clientversion.h:26
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
CPrivKey is a serialized private key, with all parameters included (SIZE bytes)
Definition: key.h:25
const std::string NAME
Definition: walletdb.cpp:48
const std::string BESTBLOCK
Definition: walletdb.cpp:36
const std::string WALLETDESCRIPTORCKEY
Definition: walletdb.cpp:59
const std::string WATCHS
Definition: walletdb.cpp:62
const std::string POOL
Definition: walletdb.cpp:51
const std::string MINVERSION
Definition: walletdb.cpp:47
const std::string WATCHMETA
Definition: walletdb.cpp:61
const std::string DEFAULTKEY
Definition: walletdb.cpp:39
const std::string OLD_KEY
Definition: walletdb.cpp:49
const std::string WALLETDESCRIPTORKEY
Definition: walletdb.cpp:60
const std::string ACENTRY
Definition: walletdb.cpp:32
const std::string ACTIVEEXTERNALSPK
Definition: walletdb.cpp:33
const std::string TX
Definition: walletdb.cpp:54
const std::string KEY
Definition: walletdb.cpp:44
const std::string CRYPTED_KEY
Definition: walletdb.cpp:37
const std::string DESTDATA
Definition: walletdb.cpp:40
const std::string CSCRIPT
Definition: walletdb.cpp:38
const std::unordered_set< std::string > LEGACY_TYPES
Definition: walletdb.cpp:63
const std::string SETTINGS
Definition: walletdb.cpp:53
const std::string BESTBLOCK_NOMERKLE
Definition: walletdb.cpp:35
const std::string LOCKED_UTXO
Definition: walletdb.cpp:45
const std::string ACTIVEINTERNALSPK
Definition: walletdb.cpp:34
const std::string HDCHAIN
Definition: walletdb.cpp:42
const std::string ORDERPOSNEXT
Definition: walletdb.cpp:50
const std::string FLAGS
Definition: walletdb.cpp:41
const std::string VERSION
Definition: walletdb.cpp:55
const std::string MASTER_KEY
Definition: walletdb.cpp:46
const std::string KEYMETA
Definition: walletdb.cpp:43
const std::string PURPOSE
Definition: walletdb.cpp:52
const std::string WALLETDESCRIPTOR
Definition: walletdb.cpp:56
bool LoadKey(CWallet *pwallet, DataStream &ssKey, DataStream &ssValue, std::string &strErr)
Definition: walletdb.cpp:292
static bool RunWithinTxn(WalletBatch &batch, std::string_view process_desc, const std::function< bool(WalletBatch &)> &func)
Definition: walletdb.cpp:1196
bool LoadCryptedKey(CWallet *pwallet, DataStream &ssKey, DataStream &ssValue, std::string &strErr)
Definition: walletdb.cpp:355
DBErrors
Overview of wallet database classes:
Definition: walletdb.h:45
@ EXTERNAL_SIGNER_SUPPORT_REQUIRED
bool LoadEncryptionKey(CWallet *pwallet, DataStream &ssKey, DataStream &ssValue, std::string &strErr)
Definition: walletdb.cpp:394
bool HasLegacyRecords(CWallet &wallet)
Returns true if there are any DBKeys::LEGACY_TYPES record in the wallet db.
Definition: walletdb.cpp:506
void LogDBInfo()
Definition: walletdb.cpp:66
bool LoadHDChain(CWallet *pwallet, DataStream &ssValue, std::string &strErr)
Definition: walletdb.cpp:421
#define READWRITE(...)
Definition: serialize.h:147
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:117
void clear()
Definition: keyorigin.h:42
std::function< void()> on_commit
Definition: walletdb.h:186
std::function< void()> on_abort
Definition: walletdb.h:186