Bitcoin Core  24.99.0
P2P Digital Currency
walletdb.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 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 #include <wallet/walletdb.h>
7 
8 #include <fs.h>
9 #include <key_io.h>
10 #include <protocol.h>
11 #include <serialize.h>
12 #include <sync.h>
13 #include <util/bip32.h>
14 #include <util/system.h>
15 #include <util/time.h>
16 #include <util/translation.h>
17 #ifdef USE_BDB
18 #include <wallet/bdb.h>
19 #endif
20 #ifdef USE_SQLITE
21 #include <wallet/sqlite.h>
22 #endif
23 #include <wallet/wallet.h>
24 
25 #include <atomic>
26 #include <optional>
27 #include <string>
28 
29 namespace wallet {
30 namespace DBKeys {
31 const std::string ACENTRY{"acentry"};
32 const std::string ACTIVEEXTERNALSPK{"activeexternalspk"};
33 const std::string ACTIVEINTERNALSPK{"activeinternalspk"};
34 const std::string BESTBLOCK_NOMERKLE{"bestblock_nomerkle"};
35 const std::string BESTBLOCK{"bestblock"};
36 const std::string CRYPTED_KEY{"ckey"};
37 const std::string CSCRIPT{"cscript"};
38 const std::string DEFAULTKEY{"defaultkey"};
39 const std::string DESTDATA{"destdata"};
40 const std::string FLAGS{"flags"};
41 const std::string HDCHAIN{"hdchain"};
42 const std::string KEYMETA{"keymeta"};
43 const std::string KEY{"key"};
44 const std::string LOCKED_UTXO{"lockedutxo"};
45 const std::string MASTER_KEY{"mkey"};
46 const std::string MINVERSION{"minversion"};
47 const std::string NAME{"name"};
48 const std::string OLD_KEY{"wkey"};
49 const std::string ORDERPOSNEXT{"orderposnext"};
50 const std::string POOL{"pool"};
51 const std::string PURPOSE{"purpose"};
52 const std::string SETTINGS{"settings"};
53 const std::string TX{"tx"};
54 const std::string VERSION{"version"};
55 const std::string WALLETDESCRIPTOR{"walletdescriptor"};
56 const std::string WALLETDESCRIPTORCACHE{"walletdescriptorcache"};
57 const std::string WALLETDESCRIPTORLHCACHE{"walletdescriptorlhcache"};
58 const std::string WALLETDESCRIPTORCKEY{"walletdescriptorckey"};
59 const std::string WALLETDESCRIPTORKEY{"walletdescriptorkey"};
60 const std::string WATCHMETA{"watchmeta"};
61 const std::string WATCHS{"watchs"};
62 const std::unordered_set<std::string> LEGACY_TYPES{CRYPTED_KEY, CSCRIPT, DEFAULTKEY, HDCHAIN, KEYMETA, KEY, OLD_KEY, POOL, WATCHMETA, WATCHS};
63 } // namespace DBKeys
64 
65 //
66 // WalletBatch
67 //
68 
69 bool WalletBatch::WriteName(const std::string& strAddress, const std::string& strName)
70 {
71  return WriteIC(std::make_pair(DBKeys::NAME, strAddress), strName);
72 }
73 
74 bool WalletBatch::EraseName(const std::string& strAddress)
75 {
76  // This should only be used for sending addresses, never for receiving addresses,
77  // receiving addresses must always have an address book entry if they're not change return.
78  return EraseIC(std::make_pair(DBKeys::NAME, strAddress));
79 }
80 
81 bool WalletBatch::WritePurpose(const std::string& strAddress, const std::string& strPurpose)
82 {
83  return WriteIC(std::make_pair(DBKeys::PURPOSE, strAddress), strPurpose);
84 }
85 
86 bool WalletBatch::ErasePurpose(const std::string& strAddress)
87 {
88  return EraseIC(std::make_pair(DBKeys::PURPOSE, strAddress));
89 }
90 
92 {
93  return WriteIC(std::make_pair(DBKeys::TX, wtx.GetHash()), wtx);
94 }
95 
97 {
98  return EraseIC(std::make_pair(DBKeys::TX, hash));
99 }
100 
101 bool WalletBatch::WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, const bool overwrite)
102 {
103  return WriteIC(std::make_pair(DBKeys::KEYMETA, pubkey), meta, overwrite);
104 }
105 
106 bool WalletBatch::WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata& keyMeta)
107 {
108  if (!WriteKeyMetadata(keyMeta, vchPubKey, false)) {
109  return false;
110  }
111 
112  // hash pubkey/privkey to accelerate wallet load
113  std::vector<unsigned char> vchKey;
114  vchKey.reserve(vchPubKey.size() + vchPrivKey.size());
115  vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end());
116  vchKey.insert(vchKey.end(), vchPrivKey.begin(), vchPrivKey.end());
117 
118  return WriteIC(std::make_pair(DBKeys::KEY, vchPubKey), std::make_pair(vchPrivKey, Hash(vchKey)), false);
119 }
120 
121 bool WalletBatch::WriteCryptedKey(const CPubKey& vchPubKey,
122  const std::vector<unsigned char>& vchCryptedSecret,
123  const CKeyMetadata &keyMeta)
124 {
125  if (!WriteKeyMetadata(keyMeta, vchPubKey, true)) {
126  return false;
127  }
128 
129  // Compute a checksum of the encrypted key
130  uint256 checksum = Hash(vchCryptedSecret);
131 
132  const auto key = std::make_pair(DBKeys::CRYPTED_KEY, vchPubKey);
133  if (!WriteIC(key, std::make_pair(vchCryptedSecret, checksum), false)) {
134  // It may already exist, so try writing just the checksum
135  std::vector<unsigned char> val;
136  if (!m_batch->Read(key, val)) {
137  return false;
138  }
139  if (!WriteIC(key, std::make_pair(val, checksum), true)) {
140  return false;
141  }
142  }
143  EraseIC(std::make_pair(DBKeys::KEY, vchPubKey));
144  return true;
145 }
146 
147 bool WalletBatch::WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
148 {
149  return WriteIC(std::make_pair(DBKeys::MASTER_KEY, nID), kMasterKey, true);
150 }
151 
152 bool WalletBatch::WriteCScript(const uint160& hash, const CScript& redeemScript)
153 {
154  return WriteIC(std::make_pair(DBKeys::CSCRIPT, hash), redeemScript, false);
155 }
156 
157 bool WalletBatch::WriteWatchOnly(const CScript &dest, const CKeyMetadata& keyMeta)
158 {
159  if (!WriteIC(std::make_pair(DBKeys::WATCHMETA, dest), keyMeta)) {
160  return false;
161  }
162  return WriteIC(std::make_pair(DBKeys::WATCHS, dest), uint8_t{'1'});
163 }
164 
166 {
167  if (!EraseIC(std::make_pair(DBKeys::WATCHMETA, dest))) {
168  return false;
169  }
170  return EraseIC(std::make_pair(DBKeys::WATCHS, dest));
171 }
172 
174 {
175  WriteIC(DBKeys::BESTBLOCK, CBlockLocator()); // Write empty block locator so versions that require a merkle branch automatically rescan
176  return WriteIC(DBKeys::BESTBLOCK_NOMERKLE, locator);
177 }
178 
180 {
181  if (m_batch->Read(DBKeys::BESTBLOCK, locator) && !locator.vHave.empty()) return true;
182  return m_batch->Read(DBKeys::BESTBLOCK_NOMERKLE, locator);
183 }
184 
185 bool WalletBatch::WriteOrderPosNext(int64_t nOrderPosNext)
186 {
187  return WriteIC(DBKeys::ORDERPOSNEXT, nOrderPosNext);
188 }
189 
190 bool WalletBatch::ReadPool(int64_t nPool, CKeyPool& keypool)
191 {
192  return m_batch->Read(std::make_pair(DBKeys::POOL, nPool), keypool);
193 }
194 
195 bool WalletBatch::WritePool(int64_t nPool, const CKeyPool& keypool)
196 {
197  return WriteIC(std::make_pair(DBKeys::POOL, nPool), keypool);
198 }
199 
200 bool WalletBatch::ErasePool(int64_t nPool)
201 {
202  return EraseIC(std::make_pair(DBKeys::POOL, nPool));
203 }
204 
206 {
207  return WriteIC(DBKeys::MINVERSION, nVersion);
208 }
209 
210 bool WalletBatch::WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bool internal)
211 {
212  std::string key = internal ? DBKeys::ACTIVEINTERNALSPK : DBKeys::ACTIVEEXTERNALSPK;
213  return WriteIC(make_pair(key, type), id);
214 }
215 
216 bool WalletBatch::EraseActiveScriptPubKeyMan(uint8_t type, bool internal)
217 {
218  const std::string key{internal ? DBKeys::ACTIVEINTERNALSPK : DBKeys::ACTIVEEXTERNALSPK};
219  return EraseIC(make_pair(key, type));
220 }
221 
222 bool WalletBatch::WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey)
223 {
224  // hash pubkey/privkey to accelerate wallet load
225  std::vector<unsigned char> key;
226  key.reserve(pubkey.size() + privkey.size());
227  key.insert(key.end(), pubkey.begin(), pubkey.end());
228  key.insert(key.end(), privkey.begin(), privkey.end());
229 
230  return WriteIC(std::make_pair(DBKeys::WALLETDESCRIPTORKEY, std::make_pair(desc_id, pubkey)), std::make_pair(privkey, Hash(key)), false);
231 }
232 
233 bool WalletBatch::WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret)
234 {
235  if (!WriteIC(std::make_pair(DBKeys::WALLETDESCRIPTORCKEY, std::make_pair(desc_id, pubkey)), secret, false)) {
236  return false;
237  }
238  EraseIC(std::make_pair(DBKeys::WALLETDESCRIPTORKEY, std::make_pair(desc_id, pubkey)));
239  return true;
240 }
241 
242 bool WalletBatch::WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor)
243 {
244  return WriteIC(make_pair(DBKeys::WALLETDESCRIPTOR, desc_id), descriptor);
245 }
246 
247 bool WalletBatch::WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index)
248 {
249  std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
250  xpub.Encode(ser_xpub.data());
251  return WriteIC(std::make_pair(std::make_pair(DBKeys::WALLETDESCRIPTORCACHE, desc_id), std::make_pair(key_exp_index, der_index)), ser_xpub);
252 }
253 
254 bool WalletBatch::WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index)
255 {
256  std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
257  xpub.Encode(ser_xpub.data());
258  return WriteIC(std::make_pair(std::make_pair(DBKeys::WALLETDESCRIPTORCACHE, desc_id), key_exp_index), ser_xpub);
259 }
260 
261 bool WalletBatch::WriteDescriptorLastHardenedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index)
262 {
263  std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
264  xpub.Encode(ser_xpub.data());
265  return WriteIC(std::make_pair(std::make_pair(DBKeys::WALLETDESCRIPTORLHCACHE, desc_id), key_exp_index), ser_xpub);
266 }
267 
269 {
270  for (const auto& parent_xpub_pair : cache.GetCachedParentExtPubKeys()) {
271  if (!WriteDescriptorParentCache(parent_xpub_pair.second, desc_id, parent_xpub_pair.first)) {
272  return false;
273  }
274  }
275  for (const auto& derived_xpub_map_pair : cache.GetCachedDerivedExtPubKeys()) {
276  for (const auto& derived_xpub_pair : derived_xpub_map_pair.second) {
277  if (!WriteDescriptorDerivedCache(derived_xpub_pair.second, desc_id, derived_xpub_map_pair.first, derived_xpub_pair.first)) {
278  return false;
279  }
280  }
281  }
282  for (const auto& lh_xpub_pair : cache.GetCachedLastHardenedExtPubKeys()) {
283  if (!WriteDescriptorLastHardenedCache(lh_xpub_pair.second, desc_id, lh_xpub_pair.first)) {
284  return false;
285  }
286  }
287  return true;
288 }
289 
291 {
292  return WriteIC(std::make_pair(DBKeys::LOCKED_UTXO, std::make_pair(output.hash, output.n)), uint8_t{'1'});
293 }
294 
296 {
297  return EraseIC(std::make_pair(DBKeys::LOCKED_UTXO, std::make_pair(output.hash, output.n)));
298 }
299 
301 public:
302  unsigned int nKeys{0};
303  unsigned int nCKeys{0};
304  unsigned int nWatchKeys{0};
305  unsigned int nKeyMeta{0};
306  unsigned int m_unknown_records{0};
307  bool fIsEncrypted{false};
308  bool fAnyUnordered{false};
309  std::vector<uint256> vWalletUpgrade;
310  std::map<OutputType, uint256> m_active_external_spks;
311  std::map<OutputType, uint256> m_active_internal_spks;
312  std::map<uint256, DescriptorCache> m_descriptor_caches;
313  std::map<std::pair<uint256, CKeyID>, CKey> m_descriptor_keys;
314  std::map<std::pair<uint256, CKeyID>, std::pair<CPubKey, std::vector<unsigned char>>> m_descriptor_crypt_keys;
315  std::map<uint160, CHDChain> m_hd_chains;
316  bool tx_corrupt{false};
317  bool descriptor_unknown{false};
318 
319  CWalletScanState() = default;
320 };
321 
322 static bool
323 ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
324  CWalletScanState &wss, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
325 {
326  try {
327  // Unserialize
328  // Taking advantage of the fact that pair serialization
329  // is just the two items serialized one after the other
330  ssKey >> strType;
331  // If we have a filter, check if this matches the filter
332  if (filter_fn && !filter_fn(strType)) {
333  return true;
334  }
335  if (strType == DBKeys::NAME) {
336  std::string strAddress;
337  ssKey >> strAddress;
338  std::string label;
339  ssValue >> label;
340  pwallet->m_address_book[DecodeDestination(strAddress)].SetLabel(label);
341  } else if (strType == DBKeys::PURPOSE) {
342  std::string strAddress;
343  ssKey >> strAddress;
344  ssValue >> pwallet->m_address_book[DecodeDestination(strAddress)].purpose;
345  } else if (strType == DBKeys::TX) {
346  uint256 hash;
347  ssKey >> hash;
348  // LoadToWallet call below creates a new CWalletTx that fill_wtx
349  // callback fills with transaction metadata.
350  auto fill_wtx = [&](CWalletTx& wtx, bool new_tx) {
351  if(!new_tx) {
352  // There's some corruption here since the tx we just tried to load was already in the wallet.
353  // We don't consider this type of corruption critical, and can fix it by removing tx data and
354  // rescanning.
355  wss.tx_corrupt = true;
356  return false;
357  }
358  ssValue >> wtx;
359  if (wtx.GetHash() != hash)
360  return false;
361 
362  // Undo serialize changes in 31600
363  if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
364  {
365  if (!ssValue.empty())
366  {
367  uint8_t fTmp;
368  uint8_t fUnused;
369  std::string unused_string;
370  ssValue >> fTmp >> fUnused >> unused_string;
371  strErr = strprintf("LoadWallet() upgrading tx ver=%d %d %s",
372  wtx.fTimeReceivedIsTxTime, fTmp, hash.ToString());
373  wtx.fTimeReceivedIsTxTime = fTmp;
374  }
375  else
376  {
377  strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString());
378  wtx.fTimeReceivedIsTxTime = 0;
379  }
380  wss.vWalletUpgrade.push_back(hash);
381  }
382 
383  if (wtx.nOrderPos == -1)
384  wss.fAnyUnordered = true;
385 
386  return true;
387  };
388  if (!pwallet->LoadToWallet(hash, fill_wtx)) {
389  return false;
390  }
391  } else if (strType == DBKeys::WATCHS) {
392  wss.nWatchKeys++;
393  CScript script;
394  ssKey >> script;
395  uint8_t fYes;
396  ssValue >> fYes;
397  if (fYes == '1') {
399  }
400  } else if (strType == DBKeys::KEY) {
401  CPubKey vchPubKey;
402  ssKey >> vchPubKey;
403  if (!vchPubKey.IsValid())
404  {
405  strErr = "Error reading wallet database: CPubKey corrupt";
406  return false;
407  }
408  CKey key;
409  CPrivKey pkey;
410  uint256 hash;
411 
412  wss.nKeys++;
413  ssValue >> pkey;
414 
415  // Old wallets store keys as DBKeys::KEY [pubkey] => [privkey]
416  // ... which was slow for wallets with lots of keys, because the public key is re-derived from the private key
417  // using EC operations as a checksum.
418  // Newer wallets store keys as DBKeys::KEY [pubkey] => [privkey][hash(pubkey,privkey)], which is much faster while
419  // remaining backwards-compatible.
420  try
421  {
422  ssValue >> hash;
423  }
424  catch (const std::ios_base::failure&) {}
425 
426  bool fSkipCheck = false;
427 
428  if (!hash.IsNull())
429  {
430  // hash pubkey/privkey to accelerate wallet load
431  std::vector<unsigned char> vchKey;
432  vchKey.reserve(vchPubKey.size() + pkey.size());
433  vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end());
434  vchKey.insert(vchKey.end(), pkey.begin(), pkey.end());
435 
436  if (Hash(vchKey) != hash)
437  {
438  strErr = "Error reading wallet database: CPubKey/CPrivKey corrupt";
439  return false;
440  }
441 
442  fSkipCheck = true;
443  }
444 
445  if (!key.Load(pkey, vchPubKey, fSkipCheck))
446  {
447  strErr = "Error reading wallet database: CPrivKey corrupt";
448  return false;
449  }
450  if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadKey(key, vchPubKey))
451  {
452  strErr = "Error reading wallet database: LegacyScriptPubKeyMan::LoadKey failed";
453  return false;
454  }
455  } else if (strType == DBKeys::MASTER_KEY) {
456  // Master encryption key is loaded into only the wallet and not any of the ScriptPubKeyMans.
457  unsigned int nID;
458  ssKey >> nID;
459  CMasterKey kMasterKey;
460  ssValue >> kMasterKey;
461  if(pwallet->mapMasterKeys.count(nID) != 0)
462  {
463  strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
464  return false;
465  }
466  pwallet->mapMasterKeys[nID] = kMasterKey;
467  if (pwallet->nMasterKeyMaxID < nID)
468  pwallet->nMasterKeyMaxID = nID;
469  } else if (strType == DBKeys::CRYPTED_KEY) {
470  CPubKey vchPubKey;
471  ssKey >> vchPubKey;
472  if (!vchPubKey.IsValid())
473  {
474  strErr = "Error reading wallet database: CPubKey corrupt";
475  return false;
476  }
477  std::vector<unsigned char> vchPrivKey;
478  ssValue >> vchPrivKey;
479 
480  // Get the checksum and check it
481  bool checksum_valid = false;
482  if (!ssValue.eof()) {
483  uint256 checksum;
484  ssValue >> checksum;
485  if ((checksum_valid = Hash(vchPrivKey) != checksum)) {
486  strErr = "Error reading wallet database: Encrypted key corrupt";
487  return false;
488  }
489  }
490 
491  wss.nCKeys++;
492 
493  if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCryptedKey(vchPubKey, vchPrivKey, checksum_valid))
494  {
495  strErr = "Error reading wallet database: LegacyScriptPubKeyMan::LoadCryptedKey failed";
496  return false;
497  }
498  wss.fIsEncrypted = true;
499  } else if (strType == DBKeys::KEYMETA) {
500  CPubKey vchPubKey;
501  ssKey >> vchPubKey;
502  CKeyMetadata keyMeta;
503  ssValue >> keyMeta;
504  wss.nKeyMeta++;
505  pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadKeyMetadata(vchPubKey.GetID(), keyMeta);
506 
507  // Extract some CHDChain info from this metadata if it has any
508  if (keyMeta.nVersion >= CKeyMetadata::VERSION_WITH_HDDATA && !keyMeta.hd_seed_id.IsNull() && keyMeta.hdKeypath.size() > 0) {
509  // Get the path from the key origin or from the path string
510  // Not applicable when path is "s" or "m" as those indicate a seed
511  // See https://github.com/bitcoin/bitcoin/pull/12924
512  bool internal = false;
513  uint32_t index = 0;
514  if (keyMeta.hdKeypath != "s" && keyMeta.hdKeypath != "m") {
515  std::vector<uint32_t> path;
516  if (keyMeta.has_key_origin) {
517  // We have a key origin, so pull it from its path vector
518  path = keyMeta.key_origin.path;
519  } else {
520  // No key origin, have to parse the string
521  if (!ParseHDKeypath(keyMeta.hdKeypath, path)) {
522  strErr = "Error reading wallet database: keymeta with invalid HD keypath";
523  return false;
524  }
525  }
526 
527  // Extract the index and internal from the path
528  // Path string is m/0'/k'/i'
529  // Path vector is [0', k', i'] (but as ints OR'd with the hardened bit
530  // k == 0 for external, 1 for internal. i is the index
531  if (path.size() != 3) {
532  strErr = "Error reading wallet database: keymeta found with unexpected path";
533  return false;
534  }
535  if (path[0] != 0x80000000) {
536  strErr = strprintf("Unexpected path index of 0x%08x (expected 0x80000000) for the element at index 0", path[0]);
537  return false;
538  }
539  if (path[1] != 0x80000000 && path[1] != (1 | 0x80000000)) {
540  strErr = strprintf("Unexpected path index of 0x%08x (expected 0x80000000 or 0x80000001) for the element at index 1", path[1]);
541  return false;
542  }
543  if ((path[2] & 0x80000000) == 0) {
544  strErr = strprintf("Unexpected path index of 0x%08x (expected to be greater than or equal to 0x80000000)", path[2]);
545  return false;
546  }
547  internal = path[1] == (1 | 0x80000000);
548  index = path[2] & ~0x80000000;
549  }
550 
551  // Insert a new CHDChain, or get the one that already exists
552  auto ins = wss.m_hd_chains.emplace(keyMeta.hd_seed_id, CHDChain());
553  CHDChain& chain = ins.first->second;
554  if (ins.second) {
555  // For new chains, we want to default to VERSION_HD_BASE until we see an internal
557  chain.seed_id = keyMeta.hd_seed_id;
558  }
559  if (internal) {
561  chain.nInternalChainCounter = std::max(chain.nInternalChainCounter, index + 1);
562  } else {
563  chain.nExternalChainCounter = std::max(chain.nExternalChainCounter, index + 1);
564  }
565  }
566  } else if (strType == DBKeys::WATCHMETA) {
567  CScript script;
568  ssKey >> script;
569  CKeyMetadata keyMeta;
570  ssValue >> keyMeta;
571  wss.nKeyMeta++;
572  pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadScriptMetadata(CScriptID(script), keyMeta);
573  } else if (strType == DBKeys::DEFAULTKEY) {
574  // We don't want or need the default key, but if there is one set,
575  // we want to make sure that it is valid so that we can detect corruption
576  CPubKey vchPubKey;
577  ssValue >> vchPubKey;
578  if (!vchPubKey.IsValid()) {
579  strErr = "Error reading wallet database: Default Key corrupt";
580  return false;
581  }
582  } else if (strType == DBKeys::POOL) {
583  int64_t nIndex;
584  ssKey >> nIndex;
585  CKeyPool keypool;
586  ssValue >> keypool;
587 
588  pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadKeyPool(nIndex, keypool);
589  } else if (strType == DBKeys::CSCRIPT) {
590  uint160 hash;
591  ssKey >> hash;
592  CScript script;
593  ssValue >> script;
594  if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCScript(script))
595  {
596  strErr = "Error reading wallet database: LegacyScriptPubKeyMan::LoadCScript failed";
597  return false;
598  }
599  } else if (strType == DBKeys::ORDERPOSNEXT) {
600  ssValue >> pwallet->nOrderPosNext;
601  } else if (strType == DBKeys::DESTDATA) {
602  std::string strAddress, strKey, strValue;
603  ssKey >> strAddress;
604  ssKey >> strKey;
605  ssValue >> strValue;
606  pwallet->LoadDestData(DecodeDestination(strAddress), strKey, strValue);
607  } else if (strType == DBKeys::HDCHAIN) {
608  CHDChain chain;
609  ssValue >> chain;
611  } else if (strType == DBKeys::OLD_KEY) {
612  strErr = "Found unsupported 'wkey' record, try loading with version 0.18";
613  return false;
614  } else if (strType == DBKeys::ACTIVEEXTERNALSPK || strType == DBKeys::ACTIVEINTERNALSPK) {
615  uint8_t type;
616  ssKey >> type;
617  uint256 id;
618  ssValue >> id;
619 
620  bool internal = strType == DBKeys::ACTIVEINTERNALSPK;
621  auto& spk_mans = internal ? wss.m_active_internal_spks : wss.m_active_external_spks;
622  if (spk_mans.count(static_cast<OutputType>(type)) > 0) {
623  strErr = "Multiple ScriptPubKeyMans specified for a single type";
624  return false;
625  }
626  spk_mans[static_cast<OutputType>(type)] = id;
627  } else if (strType == DBKeys::WALLETDESCRIPTOR) {
628  uint256 id;
629  ssKey >> id;
630  WalletDescriptor desc;
631  try {
632  ssValue >> desc;
633  } catch (const std::ios_base::failure& e) {
634  strErr = e.what();
635  wss.descriptor_unknown = true;
636  return false;
637  }
638  if (wss.m_descriptor_caches.count(id) == 0) {
640  }
641  pwallet->LoadDescriptorScriptPubKeyMan(id, desc);
642  } else if (strType == DBKeys::WALLETDESCRIPTORCACHE) {
643  bool parent = true;
644  uint256 desc_id;
645  uint32_t key_exp_index;
646  uint32_t der_index;
647  ssKey >> desc_id;
648  ssKey >> key_exp_index;
649 
650  // if the der_index exists, it's a derived xpub
651  try
652  {
653  ssKey >> der_index;
654  parent = false;
655  }
656  catch (...) {}
657 
658  std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
659  ssValue >> ser_xpub;
660  CExtPubKey xpub;
661  xpub.Decode(ser_xpub.data());
662  if (parent) {
663  wss.m_descriptor_caches[desc_id].CacheParentExtPubKey(key_exp_index, xpub);
664  } else {
665  wss.m_descriptor_caches[desc_id].CacheDerivedExtPubKey(key_exp_index, der_index, xpub);
666  }
667  } else if (strType == DBKeys::WALLETDESCRIPTORLHCACHE) {
668  uint256 desc_id;
669  uint32_t key_exp_index;
670  ssKey >> desc_id;
671  ssKey >> key_exp_index;
672 
673  std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
674  ssValue >> ser_xpub;
675  CExtPubKey xpub;
676  xpub.Decode(ser_xpub.data());
677  wss.m_descriptor_caches[desc_id].CacheLastHardenedExtPubKey(key_exp_index, xpub);
678  } else if (strType == DBKeys::WALLETDESCRIPTORKEY) {
679  uint256 desc_id;
680  CPubKey pubkey;
681  ssKey >> desc_id;
682  ssKey >> pubkey;
683  if (!pubkey.IsValid())
684  {
685  strErr = "Error reading wallet database: CPubKey corrupt";
686  return false;
687  }
688  CKey key;
689  CPrivKey pkey;
690  uint256 hash;
691 
692  wss.nKeys++;
693  ssValue >> pkey;
694  ssValue >> hash;
695 
696  // hash pubkey/privkey to accelerate wallet load
697  std::vector<unsigned char> to_hash;
698  to_hash.reserve(pubkey.size() + pkey.size());
699  to_hash.insert(to_hash.end(), pubkey.begin(), pubkey.end());
700  to_hash.insert(to_hash.end(), pkey.begin(), pkey.end());
701 
702  if (Hash(to_hash) != hash)
703  {
704  strErr = "Error reading wallet database: CPubKey/CPrivKey corrupt";
705  return false;
706  }
707 
708  if (!key.Load(pkey, pubkey, true))
709  {
710  strErr = "Error reading wallet database: CPrivKey corrupt";
711  return false;
712  }
713  wss.m_descriptor_keys.insert(std::make_pair(std::make_pair(desc_id, pubkey.GetID()), key));
714  } else if (strType == DBKeys::WALLETDESCRIPTORCKEY) {
715  uint256 desc_id;
716  CPubKey pubkey;
717  ssKey >> desc_id;
718  ssKey >> pubkey;
719  if (!pubkey.IsValid())
720  {
721  strErr = "Error reading wallet database: CPubKey corrupt";
722  return false;
723  }
724  std::vector<unsigned char> privkey;
725  ssValue >> privkey;
726  wss.nCKeys++;
727 
728  wss.m_descriptor_crypt_keys.insert(std::make_pair(std::make_pair(desc_id, pubkey.GetID()), std::make_pair(pubkey, privkey)));
729  wss.fIsEncrypted = true;
730  } else if (strType == DBKeys::LOCKED_UTXO) {
731  uint256 hash;
732  uint32_t n;
733  ssKey >> hash;
734  ssKey >> n;
735  pwallet->LockCoin(COutPoint(hash, n));
736  } else if (strType != DBKeys::BESTBLOCK && strType != DBKeys::BESTBLOCK_NOMERKLE &&
737  strType != DBKeys::MINVERSION && strType != DBKeys::ACENTRY &&
738  strType != DBKeys::VERSION && strType != DBKeys::SETTINGS &&
739  strType != DBKeys::FLAGS) {
740  wss.m_unknown_records++;
741  }
742  } catch (const std::exception& e) {
743  if (strErr.empty()) {
744  strErr = e.what();
745  }
746  return false;
747  } catch (...) {
748  if (strErr.empty()) {
749  strErr = "Caught unknown exception in ReadKeyValue";
750  }
751  return false;
752  }
753  return true;
754 }
755 
756 bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn)
757 {
758  CWalletScanState dummy_wss;
759  LOCK(pwallet->cs_wallet);
760  return ReadKeyValue(pwallet, ssKey, ssValue, dummy_wss, strType, strErr, filter_fn);
761 }
762 
763 bool WalletBatch::IsKeyType(const std::string& strType)
764 {
765  return (strType == DBKeys::KEY ||
766  strType == DBKeys::MASTER_KEY || strType == DBKeys::CRYPTED_KEY);
767 }
768 
770 {
771  CWalletScanState wss;
772  bool fNoncriticalErrors = false;
773  bool rescan_required = false;
774  DBErrors result = DBErrors::LOAD_OK;
775 
776  LOCK(pwallet->cs_wallet);
777 
778  // Last client version to open this wallet
779  int last_client = CLIENT_VERSION;
780  bool has_last_client = m_batch->Read(DBKeys::VERSION, last_client);
781  pwallet->WalletLogPrintf("Wallet file version = %d, last client version = %d\n", pwallet->GetVersion(), last_client);
782 
783  try {
784  int nMinVersion = 0;
785  if (m_batch->Read(DBKeys::MINVERSION, nMinVersion)) {
786  if (nMinVersion > FEATURE_LATEST)
787  return DBErrors::TOO_NEW;
788  pwallet->LoadMinVersion(nMinVersion);
789  }
790 
791  // Load wallet flags, so they are known when processing other records.
792  // The FLAGS key is absent during wallet creation.
793  uint64_t flags;
794  if (m_batch->Read(DBKeys::FLAGS, flags)) {
795  if (!pwallet->LoadWalletFlags(flags)) {
796  pwallet->WalletLogPrintf("Error reading wallet database: Unknown non-tolerable wallet flags found\n");
797  return DBErrors::CORRUPT;
798  }
799  }
800 
801 #ifndef ENABLE_EXTERNAL_SIGNER
803  pwallet->WalletLogPrintf("Error: External signer wallet being loaded without external signer support compiled\n");
805  }
806 #endif
807 
808  // Get cursor
809  if (!m_batch->StartCursor())
810  {
811  pwallet->WalletLogPrintf("Error getting wallet database cursor\n");
812  return DBErrors::CORRUPT;
813  }
814 
815  while (true)
816  {
817  // Read next record
820  bool complete;
821  bool ret = m_batch->ReadAtCursor(ssKey, ssValue, complete);
822  if (complete) {
823  break;
824  }
825  else if (!ret)
826  {
827  m_batch->CloseCursor();
828  pwallet->WalletLogPrintf("Error reading next record from wallet database\n");
829  return DBErrors::CORRUPT;
830  }
831 
832  // Try to be tolerant of single corrupt records:
833  std::string strType, strErr;
834  if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr))
835  {
836  // losing keys is considered a catastrophic error, anything else
837  // we assume the user can live with:
838  if (IsKeyType(strType) || strType == DBKeys::DEFAULTKEY) {
839  result = DBErrors::CORRUPT;
840  } else if (strType == DBKeys::FLAGS) {
841  // reading the wallet flags can only fail if unknown flags are present
842  result = DBErrors::TOO_NEW;
843  } else if (wss.tx_corrupt) {
844  pwallet->WalletLogPrintf("Error: Corrupt transaction found. This can be fixed by removing transactions from wallet and rescanning.\n");
845  // Set tx_corrupt back to false so that the error is only printed once (per corrupt tx)
846  wss.tx_corrupt = false;
847  result = DBErrors::CORRUPT;
848  } else if (wss.descriptor_unknown) {
849  strErr = strprintf("Error: Unrecognized descriptor found in wallet %s. ", pwallet->GetName());
850  strErr += (last_client > CLIENT_VERSION) ? "The wallet might had been created on a newer version. " :
851  "The database might be corrupted or the software version is not compatible with one of your wallet descriptors. ";
852  strErr += "Please try running the latest software version";
853  pwallet->WalletLogPrintf("%s\n", strErr);
855  } else {
856  // Leave other errors alone, if we try to fix them we might make things worse.
857  fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
858  if (strType == DBKeys::TX)
859  // Rescan if there is a bad transaction record:
860  rescan_required = true;
861  }
862  }
863  if (!strErr.empty())
864  pwallet->WalletLogPrintf("%s\n", strErr);
865  }
866  } catch (...) {
867  result = DBErrors::CORRUPT;
868  }
869  m_batch->CloseCursor();
870 
871  // Set the active ScriptPubKeyMans
872  for (auto spk_man_pair : wss.m_active_external_spks) {
873  pwallet->LoadActiveScriptPubKeyMan(spk_man_pair.second, spk_man_pair.first, /*internal=*/false);
874  }
875  for (auto spk_man_pair : wss.m_active_internal_spks) {
876  pwallet->LoadActiveScriptPubKeyMan(spk_man_pair.second, spk_man_pair.first, /*internal=*/true);
877  }
878 
879  // Set the descriptor caches
880  for (const auto& desc_cache_pair : wss.m_descriptor_caches) {
881  auto spk_man = pwallet->GetScriptPubKeyMan(desc_cache_pair.first);
882  assert(spk_man);
883  ((DescriptorScriptPubKeyMan*)spk_man)->SetCache(desc_cache_pair.second);
884  }
885 
886  // Set the descriptor keys
887  for (const auto& desc_key_pair : wss.m_descriptor_keys) {
888  auto spk_man = pwallet->GetScriptPubKeyMan(desc_key_pair.first.first);
889  ((DescriptorScriptPubKeyMan*)spk_man)->AddKey(desc_key_pair.first.second, desc_key_pair.second);
890  }
891  for (const auto& desc_key_pair : wss.m_descriptor_crypt_keys) {
892  auto spk_man = pwallet->GetScriptPubKeyMan(desc_key_pair.first.first);
893  ((DescriptorScriptPubKeyMan*)spk_man)->AddCryptedKey(desc_key_pair.first.second, desc_key_pair.second.first, desc_key_pair.second.second);
894  }
895 
896  if (rescan_required && result == DBErrors::LOAD_OK) {
897  result = DBErrors::NEED_RESCAN;
898  } else if (fNoncriticalErrors && result == DBErrors::LOAD_OK) {
900  }
901 
902  // Any wallet corruption at all: skip any rewriting or
903  // upgrading, we don't want to make it worse.
904  if (result != DBErrors::LOAD_OK)
905  return result;
906 
907  pwallet->WalletLogPrintf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total. Unknown wallet records: %u\n",
908  wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys, wss.m_unknown_records);
909 
910  // nTimeFirstKey is only reliable if all keys have metadata
911  if (pwallet->IsLegacy() && (wss.nKeys + wss.nCKeys + wss.nWatchKeys) != wss.nKeyMeta) {
912  auto spk_man = pwallet->GetOrCreateLegacyScriptPubKeyMan();
913  if (spk_man) {
914  LOCK(spk_man->cs_KeyStore);
915  spk_man->UpdateTimeFirstKey(1);
916  }
917  }
918 
919  for (const uint256& hash : wss.vWalletUpgrade)
920  WriteTx(pwallet->mapWallet.at(hash));
921 
922  // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
923  if (wss.fIsEncrypted && (last_client == 40000 || last_client == 50000))
924  return DBErrors::NEED_REWRITE;
925 
926  if (!has_last_client || last_client != CLIENT_VERSION) // Update
928 
929  if (wss.fAnyUnordered)
930  result = pwallet->ReorderTransactions();
931 
932  // Upgrade all of the wallet keymetadata to have the hd master key id
933  // This operation is not atomic, but if it fails, updated entries are still backwards compatible with older software
934  try {
935  pwallet->UpgradeKeyMetadata();
936  } catch (...) {
937  result = DBErrors::CORRUPT;
938  }
939 
940  // Upgrade all of the descriptor caches to cache the last hardened xpub
941  // This operation is not atomic, but if it fails, only new entries are added so it is backwards compatible
942  try {
943  pwallet->UpgradeDescriptorCache();
944  } catch (...) {
945  result = DBErrors::CORRUPT;
946  }
947 
948  // Set the inactive chain
949  if (wss.m_hd_chains.size() > 0) {
950  LegacyScriptPubKeyMan* legacy_spkm = pwallet->GetLegacyScriptPubKeyMan();
951  if (!legacy_spkm) {
952  pwallet->WalletLogPrintf("Inactive HD Chains found but no Legacy ScriptPubKeyMan\n");
953  return DBErrors::CORRUPT;
954  }
955  for (const auto& chain_pair : wss.m_hd_chains) {
956  if (chain_pair.first != pwallet->GetLegacyScriptPubKeyMan()->GetHDChain().seed_id) {
957  pwallet->GetLegacyScriptPubKeyMan()->AddInactiveHDChain(chain_pair.second);
958  }
959  }
960  }
961 
962  return result;
963 }
964 
965 DBErrors WalletBatch::FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWalletTx>& vWtx)
966 {
967  DBErrors result = DBErrors::LOAD_OK;
968 
969  try {
970  int nMinVersion = 0;
971  if (m_batch->Read(DBKeys::MINVERSION, nMinVersion)) {
972  if (nMinVersion > FEATURE_LATEST)
973  return DBErrors::TOO_NEW;
974  }
975 
976  // Get cursor
977  if (!m_batch->StartCursor())
978  {
979  LogPrintf("Error getting wallet database cursor\n");
980  return DBErrors::CORRUPT;
981  }
982 
983  while (true)
984  {
985  // Read next record
988  bool complete;
989  bool ret = m_batch->ReadAtCursor(ssKey, ssValue, complete);
990  if (complete) {
991  break;
992  } else if (!ret) {
993  m_batch->CloseCursor();
994  LogPrintf("Error reading next record from wallet database\n");
995  return DBErrors::CORRUPT;
996  }
997 
998  std::string strType;
999  ssKey >> strType;
1000  if (strType == DBKeys::TX) {
1001  uint256 hash;
1002  ssKey >> hash;
1003  vTxHash.push_back(hash);
1004  vWtx.emplace_back(/*tx=*/nullptr, TxStateInactive{});
1005  ssValue >> vWtx.back();
1006  }
1007  }
1008  } catch (...) {
1009  result = DBErrors::CORRUPT;
1010  }
1011  m_batch->CloseCursor();
1012 
1013  return result;
1014 }
1015 
1016 DBErrors WalletBatch::ZapSelectTx(std::vector<uint256>& vTxHashIn, std::vector<uint256>& vTxHashOut)
1017 {
1018  // build list of wallet TXs and hashes
1019  std::vector<uint256> vTxHash;
1020  std::list<CWalletTx> vWtx;
1021  DBErrors err = FindWalletTx(vTxHash, vWtx);
1022  if (err != DBErrors::LOAD_OK) {
1023  return err;
1024  }
1025 
1026  std::sort(vTxHash.begin(), vTxHash.end());
1027  std::sort(vTxHashIn.begin(), vTxHashIn.end());
1028 
1029  // erase each matching wallet TX
1030  bool delerror = false;
1031  std::vector<uint256>::iterator it = vTxHashIn.begin();
1032  for (const uint256& hash : vTxHash) {
1033  while (it < vTxHashIn.end() && (*it) < hash) {
1034  it++;
1035  }
1036  if (it == vTxHashIn.end()) {
1037  break;
1038  }
1039  else if ((*it) == hash) {
1040  if(!EraseTx(hash)) {
1041  LogPrint(BCLog::WALLETDB, "Transaction was found for deletion but returned database error: %s\n", hash.GetHex());
1042  delerror = true;
1043  }
1044  vTxHashOut.push_back(hash);
1045  }
1046  }
1047 
1048  if (delerror) {
1049  return DBErrors::CORRUPT;
1050  }
1051  return DBErrors::LOAD_OK;
1052 }
1053 
1055 {
1056  static std::atomic<bool> fOneThread(false);
1057  if (fOneThread.exchange(true)) {
1058  return;
1059  }
1060 
1061  for (const std::shared_ptr<CWallet>& pwallet : GetWallets(context)) {
1062  WalletDatabase& dbh = pwallet->GetDatabase();
1063 
1064  unsigned int nUpdateCounter = dbh.nUpdateCounter;
1065 
1066  if (dbh.nLastSeen != nUpdateCounter) {
1067  dbh.nLastSeen = nUpdateCounter;
1068  dbh.nLastWalletUpdate = GetTime();
1069  }
1070 
1071  if (dbh.nLastFlushed != nUpdateCounter && GetTime() - dbh.nLastWalletUpdate >= 2) {
1072  if (dbh.PeriodicFlush()) {
1073  dbh.nLastFlushed = nUpdateCounter;
1074  }
1075  }
1076  }
1077 
1078  fOneThread = false;
1079 }
1080 
1081 bool WalletBatch::WriteDestData(const std::string &address, const std::string &key, const std::string &value)
1082 {
1083  return WriteIC(std::make_pair(DBKeys::DESTDATA, std::make_pair(address, key)), value);
1084 }
1085 
1086 bool WalletBatch::EraseDestData(const std::string &address, const std::string &key)
1087 {
1088  return EraseIC(std::make_pair(DBKeys::DESTDATA, std::make_pair(address, key)));
1089 }
1090 
1091 
1093 {
1094  return WriteIC(DBKeys::HDCHAIN, chain);
1095 }
1096 
1098 {
1099  return WriteIC(DBKeys::FLAGS, flags);
1100 }
1101 
1102 bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
1103 {
1104  // Get cursor
1105  if (!m_batch->StartCursor())
1106  {
1107  return false;
1108  }
1109 
1110  // Iterate the DB and look for any records that have the type prefixes
1111  while (true)
1112  {
1113  // Read next record
1116  bool complete;
1117  bool ret = m_batch->ReadAtCursor(key, value, complete);
1118  if (complete) {
1119  break;
1120  }
1121  else if (!ret)
1122  {
1123  m_batch->CloseCursor();
1124  return false;
1125  }
1126 
1127  // Make a copy of key to avoid data being deleted by the following read of the type
1128  Span<const unsigned char> key_data = MakeUCharSpan(key);
1129 
1130  std::string type;
1131  key >> type;
1132 
1133  if (types.count(type) > 0) {
1134  m_batch->Erase(key_data);
1135  }
1136  }
1137  m_batch->CloseCursor();
1138  return true;
1139 }
1140 
1142 {
1143  return m_batch->TxnBegin();
1144 }
1145 
1147 {
1148  return m_batch->TxnCommit();
1149 }
1150 
1152 {
1153  return m_batch->TxnAbort();
1154 }
1155 
1156 std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
1157 {
1158  bool exists;
1159  try {
1160  exists = fs::symlink_status(path).type() != fs::file_type::not_found;
1161  } catch (const fs::filesystem_error& e) {
1162  error = Untranslated(strprintf("Failed to access database path '%s': %s", fs::PathToString(path), fsbridge::get_filesystem_error_message(e)));
1164  return nullptr;
1165  }
1166 
1167  std::optional<DatabaseFormat> format;
1168  if (exists) {
1169  if (IsBDBFile(BDBDataFile(path))) {
1171  }
1172  if (IsSQLiteFile(SQLiteDataFile(path))) {
1173  if (format) {
1174  error = Untranslated(strprintf("Failed to load database path '%s'. Data is in ambiguous format.", fs::PathToString(path)));
1176  return nullptr;
1177  }
1179  }
1180  } else if (options.require_existing) {
1181  error = Untranslated(strprintf("Failed to load database path '%s'. Path does not exist.", fs::PathToString(path)));
1183  return nullptr;
1184  }
1185 
1186  if (!format && options.require_existing) {
1187  error = Untranslated(strprintf("Failed to load database path '%s'. Data is not in recognized format.", fs::PathToString(path)));
1189  return nullptr;
1190  }
1191 
1192  if (format && options.require_create) {
1193  error = Untranslated(strprintf("Failed to create database path '%s'. Database already exists.", fs::PathToString(path)));
1195  return nullptr;
1196  }
1197 
1198  // A db already exists so format is set, but options also specifies the format, so make sure they agree
1199  if (format && options.require_format && format != options.require_format) {
1200  error = Untranslated(strprintf("Failed to load database path '%s'. Data is not in required format.", fs::PathToString(path)));
1202  return nullptr;
1203  }
1204 
1205  // Format is not set when a db doesn't already exist, so use the format specified by the options if it is set.
1206  if (!format && options.require_format) format = options.require_format;
1207 
1208  // If the format is not specified or detected, choose the default format based on what is available. We prefer BDB over SQLite for now.
1209  if (!format) {
1210 #ifdef USE_SQLITE
1212 #endif
1213 #ifdef USE_BDB
1215 #endif
1216  }
1217 
1218  if (format == DatabaseFormat::SQLITE) {
1219 #ifdef USE_SQLITE
1220  return MakeSQLiteDatabase(path, options, status, error);
1221 #endif
1222  error = Untranslated(strprintf("Failed to open database path '%s'. Build does not support SQLite database format.", fs::PathToString(path)));
1224  return nullptr;
1225  }
1226 
1227 #ifdef USE_BDB
1228  return MakeBerkeleyDatabase(path, options, status, error);
1229 #endif
1230  error = Untranslated(strprintf("Failed to open database path '%s'. Build does not support Berkeley DB database format.", fs::PathToString(path)));
1232  return nullptr;
1233 }
1234 
1236 std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase()
1237 {
1238  return std::make_unique<DummyDatabase>();
1239 }
1240 
1242 std::unique_ptr<WalletDatabase> CreateMockWalletDatabase(DatabaseOptions& options)
1243 {
1244 
1245  std::optional<DatabaseFormat> format;
1246  if (options.require_format) format = options.require_format;
1247  if (!format) {
1248 #ifdef USE_BDB
1250 #endif
1251 #ifdef USE_SQLITE
1253 #endif
1254  }
1255 
1256  if (format == DatabaseFormat::SQLITE) {
1257 #ifdef USE_SQLITE
1258  return std::make_unique<SQLiteDatabase>(":memory:", "", options, true);
1259 #endif
1260  assert(false);
1261  }
1262 
1263 #ifdef USE_BDB
1264  return std::make_unique<BerkeleyDatabase>(std::make_shared<BerkeleyEnvironment>(), "", options);
1265 #endif
1266  assert(false);
1267 }
1268 
1269 std::unique_ptr<WalletDatabase> CreateMockWalletDatabase()
1270 {
1271  DatabaseOptions options;
1272  return CreateMockWalletDatabase(options);
1273 }
1274 } // namespace wallet
bool ParseHDKeypath(const std::string &keypath_str, std::vector< uint32_t > &keypath)
Parse an HD keypaths like "m/7/0'/2000".
Definition: bip32.cpp:13
int ret
int flags
Definition: bitcoin-tx.cpp:525
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:186
bool empty() const
Definition: streams.h:238
bool eof() const
Definition: streams.h:271
An encapsulated private key.
Definition: key.h:27
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:303
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:35
uint32_t n
Definition: transaction.h:38
uint256 hash
Definition: transaction.h:37
An encapsulated public key.
Definition: pubkey.h:34
const unsigned char * end() const
Definition: pubkey.h:115
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:164
bool IsValid() const
Definition: pubkey.h:189
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:112
const unsigned char * begin() const
Definition: pubkey.h:114
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:27
Cache for single descriptor's derived extended pubkeys.
Definition: descriptor.h:19
const ExtPubKeyMap GetCachedLastHardenedExtPubKeys() const
Retrieve all cached last hardened xpubs.
const ExtPubKeyMap GetCachedParentExtPubKeys() const
Retrieve all cached parent xpubs.
const std::unordered_map< uint32_t, ExtPubKeyMap > GetCachedDerivedExtPubKeys() const
Retrieve all cached derived xpubs.
std::string ToString() const
Definition: uint256.cpp:64
bool IsNull() const
Definition: uint256.h:34
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
160-bit opaque blob.
Definition: uint256.h:108
256-bit opaque blob.
Definition: uint256.h:119
uint32_t nInternalChainCounter
Definition: walletdb.h:98
static const int VERSION_HD_BASE
Definition: walletdb.h:103
uint32_t nExternalChainCounter
Definition: walletdb.h:97
static const int VERSION_HD_CHAIN_SPLIT
Definition: walletdb.h:104
CKeyID seed_id
seed hash160
Definition: walletdb.h:99
std::string hdKeypath
Definition: walletdb.h:141
bool has_key_origin
Whether the key_origin is useful.
Definition: walletdb.h:144
KeyOriginInfo key_origin
Definition: walletdb.h:143
static const int VERSION_WITH_HDDATA
Definition: walletdb.h:136
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:236
const std::string & GetName() const
Get a name for this wallet for logging/debugging purposes.
Definition: wallet.h:366
void LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Adds a destination data tuple to the store, without saving it to disk.
Definition: wallet.cpp:2718
LegacyScriptPubKeyMan * GetLegacyScriptPubKeyMan() const
Get the LegacyScriptPubKeyMan which is used for all types, internal, and external.
Definition: wallet.cpp:3385
void LoadActiveScriptPubKeyMan(uint256 id, OutputType type, bool internal)
Loads an active ScriptPubKeyMan for the specified type and internal.
Definition: wallet.cpp:3527
unsigned int nMasterKeyMaxID
Definition: wallet.h:370
void LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor &desc)
Instantiate a descriptor ScriptPubKeyMan from the WalletDescriptor and load it.
Definition: wallet.cpp:3435
bool IsLegacy() const
Determine if we are a legacy wallet.
Definition: wallet.cpp:3564
bool LoadMinVersion(int nVersion) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.h:477
int GetVersion() const
get the current wallet format (the oldest client version guaranteed to understand this wallet)
Definition: wallet.h:712
MasterKeyMap mapMasterKeys
Definition: wallet.h:369
void WalletLogPrintf(std::string fmt, Params... parameters) const
Prepends the wallet name in logging output to ease debugging in multi-wallet use cases.
Definition: wallet.h:825
ScriptPubKeyMan * GetScriptPubKeyMan(const OutputType &type, bool internal) const
Get the ScriptPubKeyMan for the given OutputType and internal/external chain.
Definition: wallet.cpp:3327
RecursiveMutex cs_wallet
Main wallet lock.
Definition: wallet.h:356
LegacyScriptPubKeyMan * GetOrCreateLegacyScriptPubKeyMan()
Definition: wallet.cpp:3397
std::map< uint160, CHDChain > m_hd_chains
Definition: walletdb.cpp:315
unsigned int m_unknown_records
Definition: walletdb.cpp:306
std::map< uint256, DescriptorCache > m_descriptor_caches
Definition: walletdb.cpp:312
unsigned int nWatchKeys
Definition: walletdb.cpp:304
std::vector< uint256 > vWalletUpgrade
Definition: walletdb.cpp:309
std::map< OutputType, uint256 > m_active_internal_spks
Definition: walletdb.cpp:311
std::map< std::pair< uint256, CKeyID >, CKey > m_descriptor_keys
Definition: walletdb.cpp:313
std::map< OutputType, uint256 > m_active_external_spks
Definition: walletdb.cpp:310
std::map< std::pair< uint256, CKeyID >, std::pair< CPubKey, std::vector< unsigned char > > > m_descriptor_crypt_keys
Definition: walletdb.cpp:314
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:138
const uint256 & GetHash() const
Definition: transaction.h:298
void AddInactiveHDChain(const CHDChain &chain)
bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret, bool checksum_valid)
Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
void LoadKeyMetadata(const CKeyID &keyID, const CKeyMetadata &metadata)
Load metadata (used by LoadWallet)
const CHDChain & GetHDChain() const
void LoadScriptMetadata(const CScriptID &script_id, const CKeyMetadata &metadata)
bool LoadKey(const CKey &key, const CPubKey &pubkey)
Adds a key to the store, without saving it to disk (used by LoadWallet)
bool LoadWatchOnly(const CScript &dest)
Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool)
Load a keypool entry.
void LoadHDChain(const CHDChain &chain)
Load a HD chain model (used by LoadWallet)
bool LoadCScript(const CScript &redeemScript)
Adds a CScript to the store.
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:769
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:1092
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:763
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:294
bool EraseRecords(const std::unordered_set< std::string > &types)
Delete records of the given types.
Definition: walletdb.cpp:1102
bool WriteWalletFlags(const uint64_t flags)
Definition: walletdb.cpp:1097
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:191
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition: walletdb.cpp:185
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:204
DBErrors FindWalletTx(std::vector< uint256 > &vTxHash, std::list< CWalletTx > &vWtx)
Definition: walletdb.cpp:965
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
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:1081
bool WriteDescriptorDerivedCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index, uint32_t der_index)
Definition: walletdb.cpp:247
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:1086
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
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:1016
bool EraseWatchOnly(const CScript &script)
Definition: walletdb.cpp:165
An instance of this class represents one database.
Definition: db.h:106
virtual bool PeriodicFlush()=0
int64_t nLastWalletUpdate
Definition: db.h:153
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:150
unsigned int nLastFlushed
Definition: db.h:152
unsigned int nLastSeen
Definition: db.h:151
Descriptor with some wallet metadata.
Definition: walletutil.h:77
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
bool LockCoin(const COutPoint &output, WalletBatch *batch=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2518
bool LoadToWallet(const uint256 &hash, const UpdateWalletTxFn &fill_wtx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:1055
bool IsWalletFlagSet(uint64_t flag) const override
check if a certain wallet flag is set
Definition: wallet.cpp:1519
bool LoadWalletFlags(uint64_t flags)
Loads the flags into the wallet.
Definition: wallet.cpp:1524
void UpgradeKeyMetadata() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Upgrade stored CKeyMetadata objects to store key origin info as KeyOriginInfo.
Definition: wallet.cpp:436
DBErrors ReorderTransactions()
Definition: wallet.cpp:797
void UpgradeDescriptorCache() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Upgrade DescriptorCaches.
Definition: wallet.cpp:451
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:74
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
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
Definition: key_io.cpp:281
#define LogPrint(category,...)
Definition: logging.h:243
#define LogPrintf(...)
Definition: logging.h:234
@ WALLETDB
Definition: logging.h:46
static bool exists(const path &p)
Definition: fs.h:88
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:150
std::string get_filesystem_error_message(const fs::filesystem_error &e)
Definition: fs.cpp:138
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1062
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 WALLETDESCRIPTORLHCACHE
Definition: walletdb.cpp:57
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 WALLETDESCRIPTORCACHE
Definition: walletdb.cpp:56
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< BerkeleyDatabase > MakeBerkeleyDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Return object giving access to Berkeley database at specified path.
Definition: bdb.cpp:829
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: walletdb.cpp:1156
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase(DatabaseOptions &options)
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1242
static bool ReadKeyValue(CWallet *pwallet, CDataStream &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:323
std::vector< std::shared_ptr< CWallet > > GetWallets(WalletContext &context)
Definition: wallet.cpp:146
void MaybeCompactWalletDB(WalletContext &context)
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:1054
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:545
fs::path SQLiteDataFile(const fs::path &path)
Definition: db.cpp:78
std::function< bool(const std::string &)> KeyFilterFn
Callback for filtering key types to deserialize in ReadKeyValue.
Definition: walletdb.h:302
bool IsBDBFile(const fs::path &path)
Definition: db.cpp:83
fs::path BDBDataFile(const fs::path &wallet_path)
Definition: db.cpp:64
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1236
@ FEATURE_LATEST
Definition: walletutil.h:30
bool IsSQLiteFile(const fs::path &path)
Definition: db.cpp:108
@ WALLET_FLAG_EXTERNAL_SIGNER
Indicates that the wallet needs an external signer.
Definition: walletutil.h:69
DatabaseStatus
Definition: db.h:219
WalletContext context
OutputType
Definition: outputtype.h:17
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:19
@ SER_DISK
Definition: serialize.h:132
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) unsigned char member types only.
Definition: span.h:285
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
std::vector< uint256 > vHave
Definition: block.h:122
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: pubkey.cpp:337
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: pubkey.cpp:346
std::vector< uint32_t > path
Definition: keyorigin.h:14
Bilingual messages:
Definition: translation.h:18
bool require_existing
Definition: db.h:206
std::optional< DatabaseFormat > require_format
Definition: db.h:208
State of transaction not confirmed or conflicting with a known block and not in the mempool.
Definition: transaction.h:48
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:35
#define LOCK(cs)
Definition: sync.h:261
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
int64_t GetTime()
Definition: time.cpp:117
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:48
assert(!tx.IsCoinBase())