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