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 <vector>
18
19class CScript;
20class uint160;
21class uint256;
22struct CBlockLocator;
23
24namespace wallet {
25class CMasterKey;
26class CWallet;
27class CWalletTx;
28struct WalletContext;
29
30// Logs information about the database, including available engines, features, and other capabilities
31void LogDBInfo();
32
43enum class DBErrors : int
44{
45 LOAD_OK = 0,
46 NEED_RESCAN = 1,
49 TOO_NEW = 5,
51 LOAD_FAIL = 7,
53 LEGACY_WALLET = 9,
54 CORRUPT = 10,
55};
56
57namespace DBKeys {
58extern const std::string ACENTRY;
59extern const std::string ACTIVEEXTERNALSPK;
60extern const std::string ACTIVEINTERNALSPK;
61extern const std::string BESTBLOCK;
62extern const std::string BESTBLOCK_NOMERKLE;
63extern const std::string CRYPTED_KEY;
64extern const std::string CSCRIPT;
65extern const std::string DEFAULTKEY;
66extern const std::string DESTDATA;
67extern const std::string FLAGS;
68extern const std::string HDCHAIN;
69extern const std::string KEY;
70extern const std::string KEYMETA;
71extern const std::string LOCKED_UTXO;
72extern const std::string MASTER_KEY;
73extern const std::string MINVERSION;
74extern const std::string NAME;
75extern const std::string OLD_KEY;
76extern const std::string ORDERPOSNEXT;
77extern const std::string POOL;
78extern const std::string PURPOSE;
79extern const std::string SETTINGS;
80extern const std::string TX;
81extern const std::string VERSION;
82extern const std::string WALLETDESCRIPTOR;
83extern const std::string WALLETDESCRIPTORCKEY;
84extern const std::string WALLETDESCRIPTORKEY;
85extern const std::string WATCHMETA;
86extern const std::string WATCHS;
87
88// Keys in this set pertain only to the legacy wallet (LegacyScriptPubKeyMan) and are removed during migration from legacy to descriptors.
89extern const std::unordered_set<std::string> LEGACY_TYPES;
90} // namespace DBKeys
91
92/* simple HD chain data model */
94{
95public:
99 int64_t m_next_external_index{0}; // Next index in the keypool to be used. Memory only.
100 int64_t m_next_internal_index{0}; // Next index in the keypool to be used. Memory only.
101
102 static const int VERSION_HD_BASE = 1;
103 static const int VERSION_HD_CHAIN_SPLIT = 2;
106
108
110 {
111 READWRITE(obj.nVersion, obj.nExternalChainCounter, obj.seed_id);
112 if (obj.nVersion >= VERSION_HD_CHAIN_SPLIT) {
113 READWRITE(obj.nInternalChainCounter);
114 }
115 }
116
117 void SetNull()
118 {
123 }
124
125 bool operator==(const CHDChain& chain) const
126 {
127 return seed_id == chain.seed_id;
128 }
129};
130
132{
133public:
134 static const int VERSION_BASIC=1;
135 static const int VERSION_WITH_HDDATA=10;
136 static const int VERSION_WITH_KEY_ORIGIN = 12;
139 int64_t nCreateTime; // 0 means unknown
140 std::string hdKeypath; //optional HD/bip32 keypath. Still used to determine whether a key is a seed. Also kept for backwards compatibility
141 CKeyID hd_seed_id; //id of the HD seed used to derive this key
142 KeyOriginInfo key_origin; // Key origin info with path and fingerprint
143 bool has_key_origin = false;
144
146 {
147 SetNull();
148 }
149 explicit CKeyMetadata(int64_t nCreateTime_)
150 {
151 SetNull();
152 nCreateTime = nCreateTime_;
153 }
154
156 {
157 READWRITE(obj.nVersion, obj.nCreateTime);
158 if (obj.nVersion >= VERSION_WITH_HDDATA) {
159 READWRITE(obj.hdKeypath, obj.hd_seed_id);
160 }
161 if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN)
162 {
163 READWRITE(obj.key_origin);
164 READWRITE(obj.has_key_origin);
165 }
166 }
167
168 void SetNull()
169 {
171 nCreateTime = 0;
172 hdKeypath.clear();
175 has_key_origin = false;
176 }
177};
178
180{
181 std::function<void()> on_commit, on_abort;
182};
183
192{
193private:
194 template <typename K, typename T>
195 bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
196 {
197 if (!m_batch->Write(key, value, fOverwrite)) {
198 return false;
199 }
200 return true;
201 }
202
203 template <typename K>
204 bool EraseIC(const K& key)
205 {
206 if (!m_batch->Erase(key)) {
207 return false;
208 }
209 return true;
210 }
211
212public:
213 explicit WalletBatch(WalletDatabase &database) :
214 m_batch(database.MakeBatch())
215 {
216 }
217 WalletBatch(const WalletBatch&) = delete;
219
220 bool WriteName(const std::string& strAddress, const std::string& strName);
221 bool EraseName(const std::string& strAddress);
222
223 bool WritePurpose(const std::string& strAddress, const std::string& purpose);
224 bool ErasePurpose(const std::string& strAddress);
225
226 bool WriteTx(const CWalletTx& wtx);
227 bool EraseTx(Txid hash);
228
229 bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, bool overwrite);
230 bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
231 bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
232 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
233 bool EraseMasterKey(unsigned int id);
234
235 bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
236 bool EraseWatchOnly(const CScript &script);
237
238 bool WriteBestBlock(const CBlockLocator& locator);
239 bool ReadBestBlock(CBlockLocator& locator);
240
241 // Returns true if wallet stores encryption keys
242 bool IsEncrypted();
243
244 bool WriteOrderPosNext(int64_t nOrderPosNext);
245
246 bool WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey);
247 bool WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret);
248 bool WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor);
249 bool WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index);
250 bool WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
251 bool WriteDescriptorLastHardenedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
252 bool WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache);
253
254 bool WriteLockedUTXO(const COutPoint& output);
255 bool EraseLockedUTXO(const COutPoint& output);
256
257 bool WriteAddressPreviouslySpent(const CTxDestination& dest, bool previously_spent);
258 bool WriteAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& receive_request);
259 bool EraseAddressReceiveRequest(const CTxDestination& dest, const std::string& id);
260 bool EraseAddressData(const CTxDestination& dest);
261
262 bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bool internal);
263 bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal);
264
265 DBErrors LoadWallet(CWallet* pwallet);
266
268 bool WriteVersion(int client_version) { return m_batch->Write(DBKeys::VERSION, CLIENT_VERSION); }
269
271 bool EraseRecords(const std::unordered_set<std::string>& types);
272
273 bool WriteWalletFlags(uint64_t flags);
275 bool TxnBegin();
277 bool TxnCommit();
279 bool TxnAbort();
280 bool HasActiveTxn() { return m_batch->HasActiveTxn(); }
281
283 void RegisterTxnListener(const DbTxnListener& l);
284
285private:
286 std::unique_ptr<DatabaseBatch> m_batch;
287
288 // External functions listening to the current db txn outcome.
289 // Listeners are cleared at the end of the transaction.
290 std::vector<DbTxnListener> m_txn_listeners;
291};
292
305bool RunWithinTxn(WalletDatabase& database, std::string_view process_desc, const std::function<bool(WalletBatch&)>& func);
306
307bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
308bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
309bool LoadEncryptionKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
310bool LoadHDChain(CWallet* pwallet, DataStream& ssValue, std::string& strErr);
311
315} // namespace wallet
316
317#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:529
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:405
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:133
Cache for single descriptor's derived extended pubkeys.
Definition: descriptor.h:19
constexpr void SetNull()
Definition: uint256.h:55
160-bit opaque blob.
Definition: uint256.h:183
256-bit opaque blob.
Definition: uint256.h:195
SERIALIZE_METHODS(CHDChain, obj)
Definition: walletdb.h:109
void SetNull()
Definition: walletdb.h:117
uint32_t nInternalChainCounter
Definition: walletdb.h:97
static const int VERSION_HD_BASE
Definition: walletdb.h:102
uint32_t nExternalChainCounter
Definition: walletdb.h:96
int64_t m_next_external_index
Definition: walletdb.h:99
static const int VERSION_HD_CHAIN_SPLIT
Definition: walletdb.h:103
CKeyID seed_id
seed hash160
Definition: walletdb.h:98
bool operator==(const CHDChain &chain) const
Definition: walletdb.h:125
int64_t m_next_internal_index
Definition: walletdb.h:100
static const int CURRENT_VERSION
Definition: walletdb.h:104
static const int VERSION_WITH_KEY_ORIGIN
Definition: walletdb.h:136
SERIALIZE_METHODS(CKeyMetadata, obj)
Definition: walletdb.h:155
std::string hdKeypath
Definition: walletdb.h:140
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:149
bool has_key_origin
Whether the key_origin is useful.
Definition: walletdb.h:143
KeyOriginInfo key_origin
Definition: walletdb.h:142
static const int VERSION_BASIC
Definition: walletdb.h:134
static const int VERSION_WITH_HDDATA
Definition: walletdb.h:135
static const int CURRENT_VERSION
Definition: walletdb.h:137
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:310
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:192
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:234
bool TxnAbort()
Abort current transaction.
Definition: walletdb.cpp:1273
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:1097
bool WriteBestBlock(const CBlockLocator &locator)
Definition: walletdb.cpp:177
void RegisterTxnListener(const DbTxnListener &l)
Registers db txn callback functions.
Definition: walletdb.cpp:1286
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:290
bool WriteWalletFlags(uint64_t flags)
Definition: walletdb.cpp:1243
bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta)
Definition: walletdb.cpp:161
bool TxnBegin()
Begin a new transaction.
Definition: walletdb.cpp:1255
bool WriteAddressPreviouslySpent(const CTxDestination &dest, bool previously_spent)
Definition: walletdb.cpp:1220
bool EraseAddressReceiveRequest(const CTxDestination &dest, const std::string &id)
Definition: walletdb.cpp:1231
bool TxnCommit()
Commit current transaction.
Definition: walletdb.cpp:1260
bool WriteName(const std::string &strAddress, const std::string &strName)
Definition: walletdb.cpp:76
WalletBatch(WalletDatabase &database)
Definition: walletdb.h:213
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:286
bool EraseRecords(const std::unordered_set< std::string > &types)
Delete records of the given types.
Definition: walletdb.cpp:1248
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:195
bool EraseAddressData(const CTxDestination &dest)
Definition: walletdb.cpp:1236
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:204
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:268
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:1226
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:24
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:1191
bool LoadCryptedKey(CWallet *pwallet, DataStream &ssKey, DataStream &ssValue, std::string &strErr)
Definition: walletdb.cpp:355
DBErrors
Overview of wallet database classes:
Definition: walletdb.h:44
@ 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:181
std::function< void()> on_abort
Definition: walletdb.h:181