Bitcoin Core 29.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-2022 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
18class CScript;
19class uint160;
20class uint256;
21struct CBlockLocator;
22
23namespace wallet {
24class CMasterKey;
25class CWallet;
26class CWalletTx;
27struct WalletContext;
28
39enum class DBErrors : int
40{
41 LOAD_OK = 0,
42 NEED_RESCAN = 1,
43 NEED_REWRITE = 2,
46 TOO_NEW = 5,
48 LOAD_FAIL = 7,
50 LEGACY_WALLET = 9,
51 CORRUPT = 10,
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
85// Keys in this set pertain only to the legacy wallet (LegacyScriptPubKeyMan) and are removed during migration from legacy to descriptors.
86extern const std::unordered_set<std::string> LEGACY_TYPES;
87} // namespace DBKeys
88
89/* simple HD chain data model */
91{
92public:
96 int64_t m_next_external_index{0}; // Next index in the keypool to be used. Memory only.
97 int64_t m_next_internal_index{0}; // Next index in the keypool to be used. Memory only.
98
99 static const int VERSION_HD_BASE = 1;
100 static const int VERSION_HD_CHAIN_SPLIT = 2;
103
105
107 {
108 READWRITE(obj.nVersion, obj.nExternalChainCounter, obj.seed_id);
109 if (obj.nVersion >= VERSION_HD_CHAIN_SPLIT) {
110 READWRITE(obj.nInternalChainCounter);
111 }
112 }
113
114 void SetNull()
115 {
120 }
121
122 bool operator==(const CHDChain& chain) const
123 {
124 return seed_id == chain.seed_id;
125 }
126};
127
129{
130public:
131 static const int VERSION_BASIC=1;
132 static const int VERSION_WITH_HDDATA=10;
133 static const int VERSION_WITH_KEY_ORIGIN = 12;
136 int64_t nCreateTime; // 0 means unknown
137 std::string hdKeypath; //optional HD/bip32 keypath. Still used to determine whether a key is a seed. Also kept for backwards compatibility
138 CKeyID hd_seed_id; //id of the HD seed used to derive this key
139 KeyOriginInfo key_origin; // Key origin info with path and fingerprint
140 bool has_key_origin = false;
141
143 {
144 SetNull();
145 }
146 explicit CKeyMetadata(int64_t nCreateTime_)
147 {
148 SetNull();
149 nCreateTime = nCreateTime_;
150 }
151
153 {
154 READWRITE(obj.nVersion, obj.nCreateTime);
155 if (obj.nVersion >= VERSION_WITH_HDDATA) {
156 READWRITE(obj.hdKeypath, obj.hd_seed_id);
157 }
158 if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN)
159 {
160 READWRITE(obj.key_origin);
161 READWRITE(obj.has_key_origin);
162 }
163 }
164
165 void SetNull()
166 {
168 nCreateTime = 0;
169 hdKeypath.clear();
172 has_key_origin = false;
173 }
174};
175
177{
178 std::function<void()> on_commit, on_abort;
179};
180
189{
190private:
191 template <typename K, typename T>
192 bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
193 {
194 if (!m_batch->Write(key, value, fOverwrite)) {
195 return false;
196 }
197 return true;
198 }
199
200 template <typename K>
201 bool EraseIC(const K& key)
202 {
203 if (!m_batch->Erase(key)) {
204 return false;
205 }
206 return true;
207 }
208
209public:
210 explicit WalletBatch(WalletDatabase &database) :
211 m_batch(database.MakeBatch())
212 {
213 }
214 WalletBatch(const WalletBatch&) = delete;
216
217 bool WriteName(const std::string& strAddress, const std::string& strName);
218 bool EraseName(const std::string& strAddress);
219
220 bool WritePurpose(const std::string& strAddress, const std::string& purpose);
221 bool ErasePurpose(const std::string& strAddress);
222
223 bool WriteTx(const CWalletTx& wtx);
224 bool EraseTx(uint256 hash);
225
226 bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, const bool overwrite);
227 bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
228 bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
229 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
230 bool EraseMasterKey(unsigned int id);
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 // Returns true if wallet stores encryption keys
239 bool IsEncrypted();
240
241 bool WriteOrderPosNext(int64_t nOrderPosNext);
242
243 bool WriteMinVersion(int nVersion);
244
245 bool WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey);
246 bool WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret);
247 bool WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor);
248 bool WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index);
249 bool WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
250 bool WriteDescriptorLastHardenedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
251 bool WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache);
252
253 bool WriteLockedUTXO(const COutPoint& output);
254 bool EraseLockedUTXO(const COutPoint& output);
255
256 bool WriteAddressPreviouslySpent(const CTxDestination& dest, bool previously_spent);
257 bool WriteAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& receive_request);
258 bool EraseAddressReceiveRequest(const CTxDestination& dest, const std::string& id);
259 bool EraseAddressData(const CTxDestination& dest);
260
261 bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bool internal);
262 bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal);
263
264 DBErrors LoadWallet(CWallet* pwallet);
265
267 bool EraseRecords(const std::unordered_set<std::string>& types);
268
269 bool WriteWalletFlags(const uint64_t flags);
271 bool TxnBegin();
273 bool TxnCommit();
275 bool TxnAbort();
276 bool HasActiveTxn() { return m_batch->HasActiveTxn(); }
277
279 void RegisterTxnListener(const DbTxnListener& l);
280
281private:
282 std::unique_ptr<DatabaseBatch> m_batch;
283
284 // External functions listening to the current db txn outcome.
285 // Listeners are cleared at the end of the transaction.
286 std::vector<DbTxnListener> m_txn_listeners;
287};
288
301bool RunWithinTxn(WalletDatabase& database, std::string_view process_desc, const std::function<bool(WalletBatch&)>& func);
302
303bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
304bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
305bool LoadEncryptionKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
306bool LoadHDChain(CWallet* pwallet, DataStream& ssValue, std::string& strErr);
307
311} // namespace wallet
312
313#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:536
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:415
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
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:184
256-bit opaque blob.
Definition: uint256.h:196
SERIALIZE_METHODS(CHDChain, obj)
Definition: walletdb.h:106
void SetNull()
Definition: walletdb.h:114
uint32_t nInternalChainCounter
Definition: walletdb.h:94
static const int VERSION_HD_BASE
Definition: walletdb.h:99
uint32_t nExternalChainCounter
Definition: walletdb.h:93
int64_t m_next_external_index
Definition: walletdb.h:96
static const int VERSION_HD_CHAIN_SPLIT
Definition: walletdb.h:100
CKeyID seed_id
seed hash160
Definition: walletdb.h:95
bool operator==(const CHDChain &chain) const
Definition: walletdb.h:122
int64_t m_next_internal_index
Definition: walletdb.h:97
static const int CURRENT_VERSION
Definition: walletdb.h:101
static const int VERSION_WITH_KEY_ORIGIN
Definition: walletdb.h:133
SERIALIZE_METHODS(CKeyMetadata, obj)
Definition: walletdb.h:152
std::string hdKeypath
Definition: walletdb.h:137
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:146
bool has_key_origin
Whether the key_origin is useful.
Definition: walletdb.h:140
KeyOriginInfo key_origin
Definition: walletdb.h:139
static const int VERSION_BASIC
Definition: walletdb.h:131
static const int VERSION_WITH_HDDATA
Definition: walletdb.h:132
static const int CURRENT_VERSION
Definition: walletdb.h:134
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:300
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:177
RAII class that provides access to a WalletDatabase.
Definition: db.h:51
Access to the wallet database.
Definition: walletdb.h:189
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:238
bool TxnAbort()
Abort current transaction.
Definition: walletdb.cpp:1323
bool WriteDescriptorParentCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index)
Definition: walletdb.cpp:250
bool EraseName(const std::string &strAddress)
Definition: walletdb.cpp:74
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:1147
bool WriteBestBlock(const CBlockLocator &locator)
Definition: walletdb.cpp:173
void RegisterTxnListener(const DbTxnListener &l)
Registers db txn callback functions.
Definition: walletdb.cpp:1336
bool ReadBestBlock(CBlockLocator &locator)
Definition: walletdb.cpp:179
bool WriteDescriptorCacheItems(const uint256 &desc_id, const DescriptorCache &cache)
Definition: walletdb.cpp:264
bool EraseTx(uint256 hash)
Definition: walletdb.cpp:96
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition: walletdb.cpp:147
std::vector< DbTxnListener > m_txn_listeners
Definition: walletdb.h:286
bool WriteMinVersion(int nVersion)
Definition: walletdb.cpp:201
bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta)
Definition: walletdb.cpp:157
bool TxnBegin()
Begin a new transaction.
Definition: walletdb.cpp:1305
bool WriteAddressPreviouslySpent(const CTxDestination &dest, bool previously_spent)
Definition: walletdb.cpp:1270
bool EraseAddressReceiveRequest(const CTxDestination &dest, const std::string &id)
Definition: walletdb.cpp:1281
bool TxnCommit()
Commit current transaction.
Definition: walletdb.cpp:1310
bool WriteName(const std::string &strAddress, const std::string &strName)
Definition: walletdb.cpp:69
WalletBatch(WalletDatabase &database)
Definition: walletdb.h:210
bool WritePurpose(const std::string &strAddress, const std::string &purpose)
Definition: walletdb.cpp:81
WalletBatch & operator=(const WalletBatch &)=delete
std::unique_ptr< DatabaseBatch > m_batch
Definition: walletdb.h:282
bool EraseRecords(const std::unordered_set< std::string > &types)
Delete records of the given types.
Definition: walletdb.cpp:1298
WalletBatch(const WalletBatch &)=delete
bool WriteWalletFlags(const uint64_t flags)
Definition: walletdb.cpp:1293
bool WriteKeyMetadata(const CKeyMetadata &meta, const CPubKey &pubkey, const bool overwrite)
Definition: walletdb.cpp:101
bool WriteDescriptorLastHardenedCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index)
Definition: walletdb.cpp:257
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:192
bool EraseAddressData(const CTxDestination &dest)
Definition: walletdb.cpp:1286
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition: walletdb.cpp:196
bool WriteTx(const CWalletTx &wtx)
Definition: walletdb.cpp:91
bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:106
bool EraseIC(const K &key)
Definition: walletdb.h:201
bool WriteCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:121
bool ErasePurpose(const std::string &strAddress)
Definition: walletdb.cpp:86
bool EraseLockedUTXO(const COutPoint &output)
Definition: walletdb.cpp:291
bool WriteDescriptorDerivedCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index, uint32_t der_index)
Definition: walletdb.cpp:243
bool WriteCryptedDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey, const std::vector< unsigned char > &secret)
Definition: walletdb.cpp:229
bool WriteLockedUTXO(const COutPoint &output)
Definition: walletdb.cpp:286
bool EraseMasterKey(unsigned int id)
Definition: walletdb.cpp:152
bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id, bool internal)
Definition: walletdb.cpp:206
bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal)
Definition: walletdb.cpp:212
bool WriteDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey, const CPrivKey &privkey)
Definition: walletdb.cpp:218
bool WriteAddressReceiveRequest(const CTxDestination &dest, const std::string &id, const std::string &receive_request)
Definition: walletdb.cpp:1276
bool EraseWatchOnly(const CScript &script)
Definition: walletdb.cpp:165
An instance of this class represents one database.
Definition: db.h:130
Descriptor with some wallet metadata.
Definition: walletutil.h:85
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 NAME
Definition: walletdb.cpp:47
const std::string BESTBLOCK
Definition: walletdb.cpp:35
const std::string WALLETDESCRIPTORCKEY
Definition: walletdb.cpp:58
const std::string WATCHS
Definition: walletdb.cpp:61
const std::string POOL
Definition: walletdb.cpp:50
const std::string MINVERSION
Definition: walletdb.cpp:46
const std::string WATCHMETA
Definition: walletdb.cpp:60
const std::string DEFAULTKEY
Definition: walletdb.cpp:38
const std::string OLD_KEY
Definition: walletdb.cpp:48
const std::string WALLETDESCRIPTORKEY
Definition: walletdb.cpp:59
const std::string ACENTRY
Definition: walletdb.cpp:31
const std::string ACTIVEEXTERNALSPK
Definition: walletdb.cpp:32
const std::string TX
Definition: walletdb.cpp:53
const std::string KEY
Definition: walletdb.cpp:43
const std::string CRYPTED_KEY
Definition: walletdb.cpp:36
const std::string DESTDATA
Definition: walletdb.cpp:39
const std::string CSCRIPT
Definition: walletdb.cpp:37
const std::unordered_set< std::string > LEGACY_TYPES
Definition: walletdb.cpp:62
const std::string SETTINGS
Definition: walletdb.cpp:52
const std::string BESTBLOCK_NOMERKLE
Definition: walletdb.cpp:34
const std::string LOCKED_UTXO
Definition: walletdb.cpp:44
const std::string ACTIVEINTERNALSPK
Definition: walletdb.cpp:33
const std::string HDCHAIN
Definition: walletdb.cpp:41
const std::string ORDERPOSNEXT
Definition: walletdb.cpp:49
const std::string FLAGS
Definition: walletdb.cpp:40
const std::string VERSION
Definition: walletdb.cpp:54
const std::string MASTER_KEY
Definition: walletdb.cpp:45
const std::string KEYMETA
Definition: walletdb.cpp:42
const std::string PURPOSE
Definition: walletdb.cpp:51
const std::string WALLETDESCRIPTOR
Definition: walletdb.cpp:55
bool LoadKey(CWallet *pwallet, DataStream &ssKey, DataStream &ssValue, std::string &strErr)
Definition: walletdb.cpp:296
static bool RunWithinTxn(WalletBatch &batch, std::string_view process_desc, const std::function< bool(WalletBatch &)> &func)
Definition: walletdb.cpp:1241
bool LoadCryptedKey(CWallet *pwallet, DataStream &ssKey, DataStream &ssValue, std::string &strErr)
Definition: walletdb.cpp:362
DBErrors
Overview of wallet database classes:
Definition: walletdb.h:40
@ EXTERNAL_SIGNER_SUPPORT_REQUIRED
bool LoadEncryptionKey(CWallet *pwallet, DataStream &ssKey, DataStream &ssValue, std::string &strErr)
Definition: walletdb.cpp:401
bool HasLegacyRecords(CWallet &wallet)
Returns true if there are any DBKeys::LEGACY_TYPES record in the wallet db.
Definition: walletdb.cpp:525
bool LoadHDChain(CWallet *pwallet, DataStream &ssValue, std::string &strErr)
Definition: walletdb.cpp:428
#define READWRITE(...)
Definition: serialize.h:156
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:124
void clear()
Definition: keyorigin.h:42
std::function< void()> on_commit
Definition: walletdb.h:178
std::function< void()> on_abort
Definition: walletdb.h:178