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