Bitcoin Core  24.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 
18 class CScript;
19 class uint160;
20 class uint256;
21 struct CBlockLocator;
22 
23 namespace wallet {
24 class CKeyPool;
25 class CMasterKey;
26 class CWallet;
27 class CWalletTx;
28 struct WalletContext;
29 
42 static const bool DEFAULT_FLUSHWALLET = true;
43 
45 enum class DBErrors
46 {
47  LOAD_OK,
48  CORRUPT,
50  TOO_NEW,
52  LOAD_FAIL,
57 };
58 
59 namespace DBKeys {
60 extern const std::string ACENTRY;
61 extern const std::string ACTIVEEXTERNALSPK;
62 extern const std::string ACTIVEINTERNALSPK;
63 extern const std::string BESTBLOCK;
64 extern const std::string BESTBLOCK_NOMERKLE;
65 extern const std::string CRYPTED_KEY;
66 extern const std::string CSCRIPT;
67 extern const std::string DEFAULTKEY;
68 extern const std::string DESTDATA;
69 extern const std::string FLAGS;
70 extern const std::string HDCHAIN;
71 extern const std::string KEY;
72 extern const std::string KEYMETA;
73 extern const std::string LOCKED_UTXO;
74 extern const std::string MASTER_KEY;
75 extern const std::string MINVERSION;
76 extern const std::string NAME;
77 extern const std::string OLD_KEY;
78 extern const std::string ORDERPOSNEXT;
79 extern const std::string POOL;
80 extern const std::string PURPOSE;
81 extern const std::string SETTINGS;
82 extern const std::string TX;
83 extern const std::string VERSION;
84 extern const std::string WALLETDESCRIPTOR;
85 extern const std::string WALLETDESCRIPTORCKEY;
86 extern const std::string WALLETDESCRIPTORKEY;
87 extern const std::string WATCHMETA;
88 extern const std::string WATCHS;
89 
90 // Keys in this set pertain only to the legacy wallet (LegacyScriptPubKeyMan) and are removed during migration from legacy to descriptors.
91 extern const std::unordered_set<std::string> LEGACY_TYPES;
92 } // namespace DBKeys
93 
94 /* simple HD chain data model */
95 class CHDChain
96 {
97 public:
101  int64_t m_next_external_index{0}; // Next index in the keypool to be used. Memory only.
102  int64_t m_next_internal_index{0}; // Next index in the keypool to be used. Memory only.
103 
104  static const int VERSION_HD_BASE = 1;
105  static const int VERSION_HD_CHAIN_SPLIT = 2;
107  int nVersion;
108 
109  CHDChain() { SetNull(); }
110 
112  {
113  READWRITE(obj.nVersion, obj.nExternalChainCounter, obj.seed_id);
114  if (obj.nVersion >= VERSION_HD_CHAIN_SPLIT) {
115  READWRITE(obj.nInternalChainCounter);
116  }
117  }
118 
119  void SetNull()
120  {
124  seed_id.SetNull();
125  }
126 
127  bool operator==(const CHDChain& chain) const
128  {
129  return seed_id == chain.seed_id;
130  }
131 };
132 
134 {
135 public:
136  static const int VERSION_BASIC=1;
137  static const int VERSION_WITH_HDDATA=10;
138  static const int VERSION_WITH_KEY_ORIGIN = 12;
140  int nVersion;
141  int64_t nCreateTime; // 0 means unknown
142  std::string hdKeypath; //optional HD/bip32 keypath. Still used to determine whether a key is a seed. Also kept for backwards compatibility
143  CKeyID hd_seed_id; //id of the HD seed used to derive this key
144  KeyOriginInfo key_origin; // Key origin info with path and fingerprint
145  bool has_key_origin = false;
146 
148  {
149  SetNull();
150  }
151  explicit CKeyMetadata(int64_t nCreateTime_)
152  {
153  SetNull();
154  nCreateTime = nCreateTime_;
155  }
156 
158  {
159  READWRITE(obj.nVersion, obj.nCreateTime);
160  if (obj.nVersion >= VERSION_WITH_HDDATA) {
161  READWRITE(obj.hdKeypath, obj.hd_seed_id);
162  }
163  if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN)
164  {
165  READWRITE(obj.key_origin);
166  READWRITE(obj.has_key_origin);
167  }
168  }
169 
170  void SetNull()
171  {
173  nCreateTime = 0;
174  hdKeypath.clear();
176  key_origin.clear();
177  has_key_origin = false;
178  }
179 };
180 
189 {
190 private:
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  }
198  if (m_database.nUpdateCounter % 1000 == 0) {
199  m_batch->Flush();
200  }
201  return true;
202  }
203 
204  template <typename K>
205  bool EraseIC(const K& key)
206  {
207  if (!m_batch->Erase(key)) {
208  return false;
209  }
211  if (m_database.nUpdateCounter % 1000 == 0) {
212  m_batch->Flush();
213  }
214  return true;
215  }
216 
217 public:
218  explicit WalletBatch(WalletDatabase &database, bool _fFlushOnClose = true) :
219  m_batch(database.MakeBatch(_fFlushOnClose)),
220  m_database(database)
221  {
222  }
223  WalletBatch(const WalletBatch&) = delete;
224  WalletBatch& operator=(const WalletBatch&) = delete;
225 
226  bool WriteName(const std::string& strAddress, const std::string& strName);
227  bool EraseName(const std::string& strAddress);
228 
229  bool WritePurpose(const std::string& strAddress, const std::string& purpose);
230  bool ErasePurpose(const std::string& strAddress);
231 
232  bool WriteTx(const CWalletTx& wtx);
233  bool EraseTx(uint256 hash);
234 
235  bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, const bool overwrite);
236  bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
237  bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
238  bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
239 
240  bool WriteCScript(const uint160& hash, const CScript& redeemScript);
241 
242  bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
243  bool EraseWatchOnly(const CScript &script);
244 
245  bool WriteBestBlock(const CBlockLocator& locator);
246  bool ReadBestBlock(CBlockLocator& locator);
247 
248  bool WriteOrderPosNext(int64_t nOrderPosNext);
249 
250  bool ReadPool(int64_t nPool, CKeyPool& keypool);
251  bool WritePool(int64_t nPool, const CKeyPool& keypool);
252  bool ErasePool(int64_t nPool);
253 
254  bool WriteMinVersion(int nVersion);
255 
256  bool WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey);
257  bool WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret);
258  bool WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor);
259  bool WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index);
260  bool WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
261  bool WriteDescriptorLastHardenedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
262  bool WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache);
263 
264  bool WriteLockedUTXO(const COutPoint& output);
265  bool EraseLockedUTXO(const COutPoint& output);
266 
268  bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
270  bool EraseDestData(const std::string &address, const std::string &key);
271 
272  bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bool internal);
273  bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal);
274 
275  DBErrors LoadWallet(CWallet* pwallet);
276  DBErrors FindWalletTxHashes(std::vector<uint256>& tx_hashes);
277  DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
278  /* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
279  static bool IsKeyType(const std::string& strType);
280 
282  bool WriteHDChain(const CHDChain& chain);
283 
285  bool EraseRecords(const std::unordered_set<std::string>& types);
286 
287  bool WriteWalletFlags(const uint64_t flags);
289  bool TxnBegin();
291  bool TxnCommit();
293  bool TxnAbort();
294 private:
295  std::unique_ptr<DatabaseBatch> m_batch;
297 };
298 
301 
303 using KeyFilterFn = std::function<bool(const std::string&)>;
304 
306 bool ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr);
307 
309 std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase();
310 
312 std::unique_ptr<WalletDatabase> CreateMockWalletDatabase(DatabaseOptions& options);
313 std::unique_ptr<WalletDatabase> CreateMockWalletDatabase();
314 } // namespace wallet
315 
316 #endif // BITCOIN_WALLET_WALLETDB_H
int flags
Definition: bitcoin-tx.cpp:526
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:36
An encapsulated public key.
Definition: pubkey.h:34
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:186
Cache for single descriptor's derived extended pubkeys.
Definition: descriptor.h:19
constexpr void SetNull()
Definition: uint256.h:48
160-bit opaque blob.
Definition: uint256.h:94
256-bit opaque blob.
Definition: uint256.h:105
SERIALIZE_METHODS(CHDChain, obj)
Definition: walletdb.h:111
void SetNull()
Definition: walletdb.h:119
uint32_t nInternalChainCounter
Definition: walletdb.h:99
static const int VERSION_HD_BASE
Definition: walletdb.h:104
uint32_t nExternalChainCounter
Definition: walletdb.h:98
int64_t m_next_external_index
Definition: walletdb.h:101
static const int VERSION_HD_CHAIN_SPLIT
Definition: walletdb.h:105
CKeyID seed_id
seed hash160
Definition: walletdb.h:100
bool operator==(const CHDChain &chain) const
Definition: walletdb.h:127
int64_t m_next_internal_index
Definition: walletdb.h:102
static const int CURRENT_VERSION
Definition: walletdb.h:106
static const int VERSION_WITH_KEY_ORIGIN
Definition: walletdb.h:138
SERIALIZE_METHODS(CKeyMetadata, obj)
Definition: walletdb.h:157
std::string hdKeypath
Definition: walletdb.h:142
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:151
bool has_key_origin
Whether the key_origin is useful.
Definition: walletdb.h:145
KeyOriginInfo key_origin
Definition: walletdb.h:144
static const int VERSION_BASIC
Definition: walletdb.h:136
static const int VERSION_WITH_HDDATA
Definition: walletdb.h:137
static const int CURRENT_VERSION
Definition: walletdb.h:139
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:35
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:237
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:138
Access to the wallet database.
Definition: walletdb.h:189
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:242
bool TxnAbort()
Abort current transaction.
Definition: walletdb.cpp:1151
bool WriteDescriptorParentCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index)
Definition: walletdb.cpp:254
bool EraseName(const std::string &strAddress)
Definition: walletdb.cpp:74
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:775
bool WriteBestBlock(const CBlockLocator &locator)
Definition: walletdb.cpp:173
bool ReadBestBlock(CBlockLocator &locator)
Definition: walletdb.cpp:179
bool WriteDescriptorCacheItems(const uint256 &desc_id, const DescriptorCache &cache)
Definition: walletdb.cpp:268
bool EraseTx(uint256 hash)
Definition: walletdb.cpp:96
bool WriteHDChain(const CHDChain &chain)
write the hdchain model (external chain child index counter)
Definition: walletdb.cpp:1096
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition: walletdb.cpp:147
bool WriteMinVersion(int nVersion)
Definition: walletdb.cpp:205
bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta)
Definition: walletdb.cpp:157
bool TxnBegin()
Begin a new transaction.
Definition: walletdb.cpp:1141
static bool IsKeyType(const std::string &strType)
Definition: walletdb.cpp:769
bool TxnCommit()
Commit current transaction.
Definition: walletdb.cpp:1146
bool WriteName(const std::string &strAddress, const std::string &strName)
Definition: walletdb.cpp:69
bool WritePurpose(const std::string &strAddress, const std::string &purpose)
Definition: walletdb.cpp:81
std::unique_ptr< DatabaseBatch > m_batch
Definition: walletdb.h:295
bool EraseRecords(const std::unordered_set< std::string > &types)
Delete records of the given types.
Definition: walletdb.cpp:1106
WalletBatch(const WalletBatch &)=delete
bool WriteWalletFlags(const uint64_t flags)
Definition: walletdb.cpp:1101
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:261
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:192
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition: walletdb.cpp:185
WalletDatabase & m_database
Definition: walletdb.h:296
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 ReadPool(int64_t nPool, CKeyPool &keypool)
Definition: walletdb.cpp:190
bool EraseIC(const K &key)
Definition: walletdb.h:205
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:295
WalletBatch(WalletDatabase &database, bool _fFlushOnClose=true)
Definition: walletdb.h:218
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:1085
bool WriteDescriptorDerivedCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index, uint32_t der_index)
Definition: walletdb.cpp:247
WalletBatch & operator=(const WalletBatch &)=delete
bool WriteCryptedDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey, const std::vector< unsigned char > &secret)
Definition: walletdb.cpp:233
bool WriteLockedUTXO(const COutPoint &output)
Definition: walletdb.cpp:290
bool EraseDestData(const std::string &address, const std::string &key)
Erase destination data tuple from wallet database.
Definition: walletdb.cpp:1090
bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id, bool internal)
Definition: walletdb.cpp:210
bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal)
Definition: walletdb.cpp:216
bool WritePool(int64_t nPool, const CKeyPool &keypool)
Definition: walletdb.cpp:195
DBErrors FindWalletTxHashes(std::vector< uint256 > &tx_hashes)
Definition: walletdb.cpp:974
bool WriteDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey, const CPrivKey &privkey)
Definition: walletdb.cpp:222
bool WriteCScript(const uint160 &hash, const CScript &redeemScript)
Definition: walletdb.cpp:152
bool ErasePool(int64_t nPool)
Definition: walletdb.cpp:200
DBErrors ZapSelectTx(std::vector< uint256 > &vHashIn, std::vector< uint256 > &vHashOut)
Definition: walletdb.cpp:1021
bool EraseWatchOnly(const CScript &script)
Definition: walletdb.cpp:165
An instance of this class represents one database.
Definition: db.h:123
virtual void IncrementUpdateCounter()=0
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:167
Descriptor with some wallet metadata.
Definition: walletutil.h:77
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
Definition: node.h:39
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:46
@ EXTERNAL_SIGNER_SUPPORT_REQUIRED
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase(DatabaseOptions &options)
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1242
void MaybeCompactWalletDB(WalletContext &context)
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:1058
std::function< bool(const std::string &)> KeyFilterFn
Callback for filtering key types to deserialize in ReadKeyValue.
Definition: walletdb.h:303
static bool ReadKeyValue(CWallet *pwallet, DataStream &ssKey, CDataStream &ssValue, CWalletScanState &wss, std::string &strType, std::string &strErr, const KeyFilterFn &filter_fn=nullptr) EXCLUSIVE_LOCKS_REQUIRED(pwallet -> cs_wallet)
Definition: walletdb.cpp:324
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1236
static const bool DEFAULT_FLUSHWALLET
Overview of wallet database classes:
Definition: walletdb.h:42
WalletContext context
#define READWRITE(...)
Definition: serialize.h:140
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:121
void clear()
Definition: keyorigin.h:42
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:35