Bitcoin Core 22.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-2020 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 <script/sign.h>
10#include <wallet/db.h>
11#include <wallet/walletutil.h>
12#include <key.h>
13
14#include <stdint.h>
15#include <string>
16#include <vector>
17
30static const bool DEFAULT_FLUSHWALLET = true;
31
32struct CBlockLocator;
33struct WalletContext;
34class CKeyPool;
35class CMasterKey;
36class CScript;
37class CWallet;
38class CWalletTx;
39class uint160;
40class uint256;
41
43enum class DBErrors
44{
45 LOAD_OK,
46 CORRUPT,
48 TOO_NEW,
52};
53
54namespace DBKeys {
55extern const std::string ACENTRY;
56extern const std::string ACTIVEEXTERNALSPK;
57extern const std::string ACTIVEINTERNALSPK;
58extern const std::string BESTBLOCK;
59extern const std::string BESTBLOCK_NOMERKLE;
60extern const std::string CRYPTED_KEY;
61extern const std::string CSCRIPT;
62extern const std::string DEFAULTKEY;
63extern const std::string DESTDATA;
64extern const std::string FLAGS;
65extern const std::string HDCHAIN;
66extern const std::string KEY;
67extern const std::string KEYMETA;
68extern const std::string LOCKED_UTXO;
69extern const std::string MASTER_KEY;
70extern const std::string MINVERSION;
71extern const std::string NAME;
72extern const std::string OLD_KEY;
73extern const std::string ORDERPOSNEXT;
74extern const std::string POOL;
75extern const std::string PURPOSE;
76extern const std::string SETTINGS;
77extern const std::string TX;
78extern const std::string VERSION;
79extern const std::string WALLETDESCRIPTOR;
80extern const std::string WALLETDESCRIPTORCKEY;
81extern const std::string WALLETDESCRIPTORKEY;
82extern const std::string WATCHMETA;
83extern const std::string WATCHS;
84} // namespace DBKeys
85
86/* simple HD chain data model */
88{
89public:
93
94 static const int VERSION_HD_BASE = 1;
95 static const int VERSION_HD_CHAIN_SPLIT = 2;
98
100
102 {
103 READWRITE(obj.nVersion, obj.nExternalChainCounter, obj.seed_id);
104 if (obj.nVersion >= VERSION_HD_CHAIN_SPLIT) {
105 READWRITE(obj.nInternalChainCounter);
106 }
107 }
108
109 void SetNull()
110 {
115 }
116
117 bool operator==(const CHDChain& chain) const
118 {
119 return seed_id == chain.seed_id;
120 }
121};
122
124{
125public:
126 static const int VERSION_BASIC=1;
127 static const int VERSION_WITH_HDDATA=10;
128 static const int VERSION_WITH_KEY_ORIGIN = 12;
131 int64_t nCreateTime; // 0 means unknown
132 std::string hdKeypath; //optional HD/bip32 keypath. Still used to determine whether a key is a seed. Also kept for backwards compatibility
133 CKeyID hd_seed_id; //id of the HD seed used to derive this key
134 KeyOriginInfo key_origin; // Key origin info with path and fingerprint
135 bool has_key_origin = false;
136
138 {
139 SetNull();
140 }
141 explicit CKeyMetadata(int64_t nCreateTime_)
142 {
143 SetNull();
144 nCreateTime = nCreateTime_;
145 }
146
148 {
149 READWRITE(obj.nVersion, obj.nCreateTime);
150 if (obj.nVersion >= VERSION_WITH_HDDATA) {
151 READWRITE(obj.hdKeypath, obj.hd_seed_id);
152 }
153 if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN)
154 {
155 READWRITE(obj.key_origin);
156 READWRITE(obj.has_key_origin);
157 }
158 }
159
160 void SetNull()
161 {
163 nCreateTime = 0;
164 hdKeypath.clear();
167 has_key_origin = false;
168 }
169};
170
179{
180private:
181 template <typename K, typename T>
182 bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
183 {
184 if (!m_batch->Write(key, value, fOverwrite)) {
185 return false;
186 }
188 if (m_database.nUpdateCounter % 1000 == 0) {
189 m_batch->Flush();
190 }
191 return true;
192 }
193
194 template <typename K>
195 bool EraseIC(const K& key)
196 {
197 if (!m_batch->Erase(key)) {
198 return false;
199 }
201 if (m_database.nUpdateCounter % 1000 == 0) {
202 m_batch->Flush();
203 }
204 return true;
205 }
206
207public:
208 explicit WalletBatch(WalletDatabase &database, bool _fFlushOnClose = true) :
209 m_batch(database.MakeBatch(_fFlushOnClose)),
210 m_database(database)
211 {
212 }
213 WalletBatch(const WalletBatch&) = delete;
215
216 bool WriteName(const std::string& strAddress, const std::string& strName);
217 bool EraseName(const std::string& strAddress);
218
219 bool WritePurpose(const std::string& strAddress, const std::string& purpose);
220 bool ErasePurpose(const std::string& strAddress);
221
222 bool WriteTx(const CWalletTx& wtx);
223 bool EraseTx(uint256 hash);
224
225 bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, const bool overwrite);
226 bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
227 bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
228 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
229
230 bool WriteCScript(const uint160& hash, const CScript& redeemScript);
231
232 bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
233 bool EraseWatchOnly(const CScript &script);
234
235 bool WriteBestBlock(const CBlockLocator& locator);
236 bool ReadBestBlock(CBlockLocator& locator);
237
238 bool WriteOrderPosNext(int64_t nOrderPosNext);
239
240 bool ReadPool(int64_t nPool, CKeyPool& keypool);
241 bool WritePool(int64_t nPool, const CKeyPool& keypool);
242 bool ErasePool(int64_t nPool);
243
244 bool WriteMinVersion(int nVersion);
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
258 bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
260 bool EraseDestData(const std::string &address, const std::string &key);
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 DBErrors FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWalletTx>& vWtx);
267 DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
268 /* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
269 static bool IsKeyType(const std::string& strType);
270
272 bool WriteHDChain(const CHDChain& chain);
273
274 bool WriteWalletFlags(const uint64_t flags);
276 bool TxnBegin();
278 bool TxnCommit();
280 bool TxnAbort();
281private:
282 std::unique_ptr<DatabaseBatch> m_batch;
284};
285
288
290using KeyFilterFn = std::function<bool(const std::string&)>;
291
293bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr);
294
296std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase();
297
299std::unique_ptr<WalletDatabase> CreateMockWalletDatabase();
300
301#endif // BITCOIN_WALLET_WALLETDB_H
int flags
Definition: bitcoin-tx.cpp:525
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:205
static const int VERSION_HD_BASE
Definition: walletdb.h:94
SERIALIZE_METHODS(CHDChain, obj)
Definition: walletdb.h:101
uint32_t nInternalChainCounter
Definition: walletdb.h:91
CHDChain()
Definition: walletdb.h:99
bool operator==(const CHDChain &chain) const
Definition: walletdb.h:117
CKeyID seed_id
seed hash160
Definition: walletdb.h:92
void SetNull()
Definition: walletdb.h:109
static const int VERSION_HD_CHAIN_SPLIT
Definition: walletdb.h:95
int nVersion
Definition: walletdb.h:97
static const int CURRENT_VERSION
Definition: walletdb.h:96
uint32_t nExternalChainCounter
Definition: walletdb.h:90
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:23
static const int VERSION_WITH_KEY_ORIGIN
Definition: walletdb.h:128
KeyOriginInfo key_origin
Definition: walletdb.h:134
bool has_key_origin
Whether the key_origin is useful.
Definition: walletdb.h:135
static const int VERSION_BASIC
Definition: walletdb.h:126
int nVersion
Definition: walletdb.h:130
static const int CURRENT_VERSION
Definition: walletdb.h:129
void SetNull()
Definition: walletdb.h:160
std::string hdKeypath
Definition: walletdb.h:132
SERIALIZE_METHODS(CKeyMetadata, obj)
Definition: walletdb.h:147
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:141
int64_t nCreateTime
Definition: walletdb.h:131
CKeyID hd_seed_id
Definition: walletdb.h:133
static const int VERSION_WITH_HDDATA
Definition: walletdb.h:127
A key from a CWallet's keypool.
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key.
Definition: crypter.h:34
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:27
An encapsulated public key.
Definition: pubkey.h:33
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:229
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:47
Cache for single descriptor's derived extended pubkeys.
Definition: descriptor.h:19
Access to the wallet database.
Definition: walletdb.h:179
bool TxnCommit()
Commit current transaction.
Definition: walletdb.cpp:1093
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:240
bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id, bool internal)
Definition: walletdb.cpp:208
bool WriteDescriptorDerivedCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index, uint32_t der_index)
Definition: walletdb.cpp:245
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition: walletdb.cpp:145
bool ErasePool(int64_t nPool)
Definition: walletdb.cpp:198
WalletBatch(const WalletBatch &)=delete
bool WriteName(const std::string &strAddress, const std::string &strName)
Definition: walletdb.cpp:67
bool WriteMinVersion(int nVersion)
Definition: walletdb.cpp:203
bool WriteDescriptorParentCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index)
Definition: walletdb.cpp:252
static bool IsKeyType(const std::string &strType)
Definition: walletdb.cpp:755
bool ErasePurpose(const std::string &strAddress)
Definition: walletdb.cpp:84
bool WriteCScript(const uint160 &hash, const CScript &redeemScript)
Definition: walletdb.cpp:150
bool EraseLockedUTXO(const COutPoint &output)
Definition: walletdb.cpp:293
bool EraseWatchOnly(const CScript &script)
Definition: walletdb.cpp:163
bool WriteCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:119
bool WriteDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey, const CPrivKey &privkey)
Definition: walletdb.cpp:220
bool ReadPool(int64_t nPool, CKeyPool &keypool)
Definition: walletdb.cpp:188
bool WriteWalletFlags(const uint64_t flags)
Definition: walletdb.cpp:1083
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:182
bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:104
bool EraseDestData(const std::string &address, const std::string &key)
Erase destination data tuple from wallet database.
Definition: walletdb.cpp:1072
std::unique_ptr< DatabaseBatch > m_batch
Definition: walletdb.h:282
bool WriteHDChain(const CHDChain &chain)
write the hdchain model (external chain child index counter)
Definition: walletdb.cpp:1078
bool WriteCryptedDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey, const std::vector< unsigned char > &secret)
Definition: walletdb.cpp:231
bool ReadBestBlock(CBlockLocator &locator)
Definition: walletdb.cpp:177
bool WriteDescriptorCacheItems(const uint256 &desc_id, const DescriptorCache &cache)
Definition: walletdb.cpp:266
bool WriteKeyMetadata(const CKeyMetadata &meta, const CPubKey &pubkey, const bool overwrite)
Definition: walletdb.cpp:99
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition: walletdb.cpp:183
bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal)
Definition: walletdb.cpp:214
bool WriteTx(const CWalletTx &wtx)
Definition: walletdb.cpp:89
bool EraseIC(const K &key)
Definition: walletdb.h:195
bool TxnBegin()
Begin a new transaction.
Definition: walletdb.cpp:1088
bool TxnAbort()
Abort current transaction.
Definition: walletdb.cpp:1098
DBErrors FindWalletTx(std::vector< uint256 > &vTxHash, std::list< CWalletTx > &vWtx)
Definition: walletdb.cpp:951
bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta)
Definition: walletdb.cpp:155
bool WritePool(int64_t nPool, const CKeyPool &keypool)
Definition: walletdb.cpp:193
bool WriteBestBlock(const CBlockLocator &locator)
Definition: walletdb.cpp:171
DBErrors ZapSelectTx(std::vector< uint256 > &vHashIn, std::vector< uint256 > &vHashOut)
Definition: walletdb.cpp:1002
bool EraseTx(uint256 hash)
Definition: walletdb.cpp:94
bool WritePurpose(const std::string &strAddress, const std::string &purpose)
Definition: walletdb.cpp:79
bool WriteDestData(const std::string &address, const std::string &key, const std::string &value)
Write destination data key,value tuple to database.
Definition: walletdb.cpp:1067
bool EraseName(const std::string &strAddress)
Definition: walletdb.cpp:72
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:761
bool WriteLockedUTXO(const COutPoint &output)
Definition: walletdb.cpp:288
bool WriteDescriptorLastHardenedCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index)
Definition: walletdb.cpp:259
WalletBatch(WalletDatabase &database, bool _fFlushOnClose=true)
Definition: walletdb.h:208
WalletBatch & operator=(const WalletBatch &)=delete
WalletDatabase & m_database
Definition: walletdb.h:283
An instance of this class represents one database.
Definition: db.h:104
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:148
virtual void IncrementUpdateCounter()=0
Descriptor with some wallet metadata.
Definition: walletutil.h:76
void SetNull()
Definition: uint256.h:39
160-bit opaque blob.
Definition: uint256.h:113
256-bit opaque blob.
Definition: uint256.h:124
#define T(expected, seed, data)
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
CPrivKey is a serialized private key, with all parameters included (SIZE bytes)
Definition: key.h:23
const std::string ORDERPOSNEXT
Definition: walletdb.cpp:48
const std::string DEFAULTKEY
Definition: walletdb.cpp:37
const std::string CSCRIPT
Definition: walletdb.cpp:36
const std::string TX
Definition: walletdb.cpp:52
const std::string BESTBLOCK_NOMERKLE
Definition: walletdb.cpp:33
const std::string DESTDATA
Definition: walletdb.cpp:38
const std::string WALLETDESCRIPTORKEY
Definition: walletdb.cpp:58
const std::string SETTINGS
Definition: walletdb.cpp:51
const std::string POOL
Definition: walletdb.cpp:49
const std::string WALLETDESCRIPTORCKEY
Definition: walletdb.cpp:57
const std::string ACTIVEEXTERNALSPK
Definition: walletdb.cpp:31
const std::string NAME
Definition: walletdb.cpp:46
const std::string MINVERSION
Definition: walletdb.cpp:45
const std::string HDCHAIN
Definition: walletdb.cpp:40
const std::string WALLETDESCRIPTOR
Definition: walletdb.cpp:54
const std::string VERSION
Definition: walletdb.cpp:53
const std::string KEY
Definition: walletdb.cpp:42
const std::string KEYMETA
Definition: walletdb.cpp:41
const std::string LOCKED_UTXO
Definition: walletdb.cpp:43
const std::string WATCHMETA
Definition: walletdb.cpp:59
const std::string ACENTRY
Definition: walletdb.cpp:30
const std::string MASTER_KEY
Definition: walletdb.cpp:44
const std::string ACTIVEINTERNALSPK
Definition: walletdb.cpp:32
const std::string FLAGS
Definition: walletdb.cpp:39
const std::string WATCHS
Definition: walletdb.cpp:60
const std::string PURPOSE
Definition: walletdb.cpp:50
const std::string OLD_KEY
Definition: walletdb.cpp:47
const std::string CRYPTED_KEY
Definition: walletdb.cpp:35
const std::string BESTBLOCK
Definition: walletdb.cpp:34
#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:115
void clear()
Definition: keyorigin.h:23
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:34
bool ReadKeyValue(CWallet *pwallet, CDataStream &ssKey, CDataStream &ssValue, std::string &strType, std::string &strErr, const KeyFilterFn &filter_fn=nullptr)
Unserialize a given Key-Value pair and load it into the wallet.
Definition: walletdb.cpp:748
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:44
@ NONCRITICAL_ERROR
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1183
static const bool DEFAULT_FLUSHWALLET
Overview of wallet database classes:
Definition: walletdb.h:30
void MaybeCompactWalletDB(WalletContext &context)
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:1040
std::function< bool(const std::string &)> KeyFilterFn
Callback for filtering key types to deserialize in ReadKeyValue.
Definition: walletdb.h:290
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase()
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1189