Bitcoin ABC  0.26.3
P2P Digital Currency
wallet.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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/wallet.h>
7 
8 #include <chain.h>
9 #include <chainparams.h>
10 #include <config.h>
11 #include <consensus/amount.h>
12 #include <consensus/consensus.h>
13 #include <consensus/validation.h>
14 #include <fs.h>
15 #include <interfaces/wallet.h>
16 #include <key.h>
17 #include <key_io.h>
18 #include <policy/policy.h>
19 #include <primitives/transaction.h>
20 #include <random.h>
21 #include <script/descriptor.h>
22 #include <script/script.h>
23 #include <script/sighashtype.h>
24 #include <script/sign.h>
25 #include <script/signingprovider.h>
26 #include <support/cleanse.h>
27 #include <txmempool.h>
28 #include <univalue.h>
29 #include <util/bip32.h>
30 #include <util/check.h>
31 #include <util/error.h>
32 #include <util/moneystr.h>
33 #include <util/string.h>
34 #include <util/translation.h>
35 #include <wallet/coincontrol.h>
36 #include <wallet/fees.h>
37 
38 #include <variant>
39 
41 
42 const std::map<uint64_t, std::string> WALLET_FLAG_CAVEATS{
44  "You need to rescan the blockchain in order to correctly mark used "
45  "destinations in the past. Until this is done, some destinations may "
46  "be considered unused, even if the opposite is the case."},
47 };
48 
50 static std::vector<std::shared_ptr<CWallet>> vpwallets GUARDED_BY(cs_wallets);
51 static std::list<LoadWalletFn> g_load_wallet_fns GUARDED_BY(cs_wallets);
52 
54  const std::string &wallet_name) {
55  util::SettingsValue setting_value = chain.getRwSetting("wallet");
56  if (!setting_value.isArray()) {
57  setting_value.setArray();
58  }
59  for (const util::SettingsValue &value : setting_value.getValues()) {
60  if (value.isStr() && value.get_str() == wallet_name) {
61  return true;
62  }
63  }
64  setting_value.push_back(wallet_name);
65  return chain.updateRwSetting("wallet", setting_value);
66 }
67 
69  const std::string &wallet_name) {
70  util::SettingsValue setting_value = chain.getRwSetting("wallet");
71  if (!setting_value.isArray()) {
72  return true;
73  }
75  for (const util::SettingsValue &value : setting_value.getValues()) {
76  if (!value.isStr() || value.get_str() != wallet_name) {
77  new_value.push_back(value);
78  }
79  }
80  if (new_value.size() == setting_value.size()) {
81  return true;
82  }
83  return chain.updateRwSetting("wallet", new_value);
84 }
85 
87  const std::string &wallet_name,
88  std::optional<bool> load_on_startup,
89  std::vector<bilingual_str> &warnings) {
90  if (!load_on_startup) {
91  return;
92  }
93  if (load_on_startup.value() && !AddWalletSetting(chain, wallet_name)) {
94  warnings.emplace_back(
95  Untranslated("Wallet load on startup setting could not be updated, "
96  "so wallet may not be loaded next node startup."));
97  } else if (!load_on_startup.value() &&
98  !RemoveWalletSetting(chain, wallet_name)) {
99  warnings.emplace_back(
100  Untranslated("Wallet load on startup setting could not be updated, "
101  "so wallet may still be loaded next node startup."));
102  }
103 }
104 
105 bool AddWallet(const std::shared_ptr<CWallet> &wallet) {
106  LOCK(cs_wallets);
107  assert(wallet);
108  std::vector<std::shared_ptr<CWallet>>::const_iterator i =
109  std::find(vpwallets.begin(), vpwallets.end(), wallet);
110  if (i != vpwallets.end()) {
111  return false;
112  }
113  vpwallets.push_back(wallet);
114  wallet->ConnectScriptPubKeyManNotifiers();
115  wallet->NotifyCanGetAddressesChanged();
116  return true;
117 }
118 
119 bool RemoveWallet(const std::shared_ptr<CWallet> &wallet,
120  std::optional<bool> load_on_start,
121  std::vector<bilingual_str> &warnings) {
122  assert(wallet);
123 
124  interfaces::Chain &chain = wallet->chain();
125  std::string name = wallet->GetName();
126 
127  // Unregister with the validation interface which also drops shared ponters.
128  wallet->m_chain_notifications_handler.reset();
129  LOCK(cs_wallets);
130  std::vector<std::shared_ptr<CWallet>>::iterator i =
131  std::find(vpwallets.begin(), vpwallets.end(), wallet);
132  if (i == vpwallets.end()) {
133  return false;
134  }
135  vpwallets.erase(i);
136 
137  // Write the wallet setting
138  UpdateWalletSetting(chain, name, load_on_start, warnings);
139 
140  return true;
141 }
142 
143 bool RemoveWallet(const std::shared_ptr<CWallet> &wallet,
144  std::optional<bool> load_on_start) {
145  std::vector<bilingual_str> warnings;
146  return RemoveWallet(wallet, load_on_start, warnings);
147 }
148 
149 std::vector<std::shared_ptr<CWallet>> GetWallets() {
150  LOCK(cs_wallets);
151  return vpwallets;
152 }
153 
154 std::shared_ptr<CWallet> GetWallet(const std::string &name) {
155  LOCK(cs_wallets);
156  for (const std::shared_ptr<CWallet> &wallet : vpwallets) {
157  if (wallet->GetName() == name) {
158  return wallet;
159  }
160  }
161  return nullptr;
162 }
163 
164 std::unique_ptr<interfaces::Handler>
166  LOCK(cs_wallets);
167  auto it = g_load_wallet_fns.emplace(g_load_wallet_fns.end(),
168  std::move(load_wallet));
169  return interfaces::MakeHandler([it] {
170  LOCK(cs_wallets);
171  g_load_wallet_fns.erase(it);
172  });
173 }
174 
177 static std::condition_variable g_wallet_release_cv;
178 static std::set<std::string>
179  g_loading_wallet_set GUARDED_BY(g_loading_wallet_mutex);
180 static std::set<std::string>
181  g_unloading_wallet_set GUARDED_BY(g_wallet_release_mutex);
182 
183 // Custom deleter for shared_ptr<CWallet>.
184 static void ReleaseWallet(CWallet *wallet) {
185  const std::string name = wallet->GetName();
186  wallet->WalletLogPrintf("Releasing wallet\n");
187  wallet->Flush();
188  delete wallet;
189  // Wallet is now released, notify UnloadWallet, if any.
190  {
192  if (g_unloading_wallet_set.erase(name) == 0) {
193  // UnloadWallet was not called for this wallet, all done.
194  return;
195  }
196  }
197  g_wallet_release_cv.notify_all();
198 }
199 
200 void UnloadWallet(std::shared_ptr<CWallet> &&wallet) {
201  // Mark wallet for unloading.
202  const std::string name = wallet->GetName();
203  {
205  auto it = g_unloading_wallet_set.insert(name);
206  assert(it.second);
207  }
208  // The wallet can be in use so it's not possible to explicitly unload here.
209  // Notify the unload intent so that all remaining shared pointers are
210  // released.
211  wallet->NotifyUnload();
212 
213  // Time to ditch our shared_ptr and wait for ReleaseWallet call.
214  wallet.reset();
215  {
217  while (g_unloading_wallet_set.count(name) == 1) {
218  g_wallet_release_cv.wait(lock);
219  }
220  }
221 }
222 
223 namespace {
224 std::shared_ptr<CWallet>
225 LoadWalletInternal(interfaces::Chain &chain, const std::string &name,
226  std::optional<bool> load_on_start,
227  const DatabaseOptions &options, DatabaseStatus &status,
228  bilingual_str &error, std::vector<bilingual_str> &warnings) {
229  try {
230  std::unique_ptr<WalletDatabase> database =
231  MakeWalletDatabase(name, options, status, error);
232  if (!database) {
233  error = Untranslated("Wallet file verification failed.") +
234  Untranslated(" ") + error;
235  return nullptr;
236  }
237 
238  std::shared_ptr<CWallet> wallet =
239  CWallet::Create(chain, name, std::move(database),
240  options.create_flags, error, warnings);
241  if (!wallet) {
242  error = Untranslated("Wallet loading failed.") + Untranslated(" ") +
243  error;
245  return nullptr;
246  }
247  AddWallet(wallet);
248  wallet->postInitProcess();
249 
250  // Write the wallet setting
251  UpdateWalletSetting(chain, name, load_on_start, warnings);
252 
253  return wallet;
254  } catch (const std::runtime_error &e) {
255  error = Untranslated(e.what());
257  return nullptr;
258  }
259 }
260 } // namespace
261 
262 std::shared_ptr<CWallet>
263 LoadWallet(interfaces::Chain &chain, const std::string &name,
264  std::optional<bool> load_on_start, const DatabaseOptions &options,
266  std::vector<bilingual_str> &warnings) {
267  auto result = WITH_LOCK(g_loading_wallet_mutex,
268  return g_loading_wallet_set.insert(name));
269  if (!result.second) {
270  error = Untranslated("Wallet already being loading.");
272  return nullptr;
273  }
274  auto wallet = LoadWalletInternal(chain, name, load_on_start, options,
275  status, error, warnings);
276  WITH_LOCK(g_loading_wallet_mutex, g_loading_wallet_set.erase(result.first));
277  return wallet;
278 }
279 
280 std::shared_ptr<CWallet>
281 CreateWallet(interfaces::Chain &chain, const std::string &name,
282  std::optional<bool> load_on_start, const DatabaseOptions &options,
284  std::vector<bilingual_str> &warnings) {
285  uint64_t wallet_creation_flags = options.create_flags;
286  const SecureString &passphrase = options.create_passphrase;
287 
288  // Indicate that the wallet is actually supposed to be blank and not just
289  // blank to make it encrypted
290  bool create_blank = (wallet_creation_flags & WALLET_FLAG_BLANK_WALLET);
291 
292  // Born encrypted wallets need to be created blank first.
293  if (!passphrase.empty()) {
294  wallet_creation_flags |= WALLET_FLAG_BLANK_WALLET;
295  }
296 
297  // Wallet::Verify will check if we're trying to create a wallet with a
298  // duplicate name.
299  std::unique_ptr<WalletDatabase> database =
300  MakeWalletDatabase(name, options, status, error);
301  if (!database) {
302  error = Untranslated("Wallet file verification failed.") +
303  Untranslated(" ") + error;
305  return nullptr;
306  }
307 
308  // Do not allow a passphrase when private keys are disabled
309  if (!passphrase.empty() &&
310  (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
312  "Passphrase provided but private keys are disabled. A passphrase "
313  "is only used to encrypt private keys, so cannot be used for "
314  "wallets with private keys disabled.");
316  return nullptr;
317  }
318 
319  // Make the wallet
320  std::shared_ptr<CWallet> wallet =
321  CWallet::Create(chain, name, std::move(database), wallet_creation_flags,
322  error, warnings);
323  if (!wallet) {
324  error =
325  Untranslated("Wallet creation failed.") + Untranslated(" ") + error;
327  return nullptr;
328  }
329 
330  // Encrypt the wallet
331  if (!passphrase.empty() &&
332  !(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
333  if (!wallet->EncryptWallet(passphrase)) {
334  error =
335  Untranslated("Error: Wallet created but failed to encrypt.");
337  return nullptr;
338  }
339  if (!create_blank) {
340  // Unlock the wallet
341  if (!wallet->Unlock(passphrase)) {
343  "Error: Wallet was encrypted but could not be unlocked");
345  return nullptr;
346  }
347 
348  // Set a seed for the wallet
349  {
350  LOCK(wallet->cs_wallet);
351  if (wallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
352  wallet->SetupDescriptorScriptPubKeyMans();
353  } else {
354  for (auto spk_man : wallet->GetActiveScriptPubKeyMans()) {
355  if (!spk_man->SetupGeneration()) {
356  error =
357  Untranslated("Unable to generate initial keys");
359  return nullptr;
360  }
361  }
362  }
363  }
364 
365  // Relock the wallet
366  wallet->Lock();
367  }
368  }
369  AddWallet(wallet);
370  wallet->postInitProcess();
371 
372  // Write the wallet settings
373  UpdateWalletSetting(chain, name, load_on_start, warnings);
374 
375  status = DatabaseStatus::SUCCESS;
376  return wallet;
377 }
378 
385  // Get CChainParams from interfaces::Chain, unless wallet doesn't have a
386  // chain (i.e. bitcoin-wallet), in which case return global Params()
387  return m_chain ? m_chain->params() : Params();
388 }
389 
390 const CWalletTx *CWallet::GetWalletTx(const TxId &txid) const {
392  std::map<TxId, CWalletTx>::const_iterator it = mapWallet.find(txid);
393  if (it == mapWallet.end()) {
394  return nullptr;
395  }
396 
397  return &(it->second);
398 }
399 
402  return;
403  }
404 
405  auto spk_man = GetLegacyScriptPubKeyMan();
406  if (!spk_man) {
407  return;
408  }
409 
410  spk_man->UpgradeKeyMetadata();
411  SetWalletFlag(WALLET_FLAG_KEY_ORIGIN_METADATA);
412 }
413 
414 bool CWallet::Unlock(const SecureString &strWalletPassphrase,
415  bool accept_no_keys) {
416  CCrypter crypter;
417  CKeyingMaterial _vMasterKey;
418 
419  {
420  LOCK(cs_wallet);
421  for (const MasterKeyMap::value_type &pMasterKey : mapMasterKeys) {
422  if (!crypter.SetKeyFromPassphrase(
423  strWalletPassphrase, pMasterKey.second.vchSalt,
424  pMasterKey.second.nDeriveIterations,
425  pMasterKey.second.nDerivationMethod)) {
426  return false;
427  }
428  if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey,
429  _vMasterKey)) {
430  // try another master key
431  continue;
432  }
433  if (Unlock(_vMasterKey, accept_no_keys)) {
434  // Now that we've unlocked, upgrade the key metadata
436  return true;
437  }
438  }
439  }
440 
441  return false;
442 }
443 
445  const SecureString &strOldWalletPassphrase,
446  const SecureString &strNewWalletPassphrase) {
447  bool fWasLocked = IsLocked();
448 
449  LOCK(cs_wallet);
450  Lock();
451 
452  CCrypter crypter;
453  CKeyingMaterial _vMasterKey;
454  for (MasterKeyMap::value_type &pMasterKey : mapMasterKeys) {
455  if (!crypter.SetKeyFromPassphrase(
456  strOldWalletPassphrase, pMasterKey.second.vchSalt,
457  pMasterKey.second.nDeriveIterations,
458  pMasterKey.second.nDerivationMethod)) {
459  return false;
460  }
461 
462  if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, _vMasterKey)) {
463  return false;
464  }
465 
466  if (Unlock(_vMasterKey)) {
467  int64_t nStartTime = GetTimeMillis();
468  crypter.SetKeyFromPassphrase(strNewWalletPassphrase,
469  pMasterKey.second.vchSalt,
470  pMasterKey.second.nDeriveIterations,
471  pMasterKey.second.nDerivationMethod);
472  pMasterKey.second.nDeriveIterations = static_cast<unsigned int>(
473  pMasterKey.second.nDeriveIterations *
474  (100 / ((double)(GetTimeMillis() - nStartTime))));
475 
476  nStartTime = GetTimeMillis();
477  crypter.SetKeyFromPassphrase(strNewWalletPassphrase,
478  pMasterKey.second.vchSalt,
479  pMasterKey.second.nDeriveIterations,
480  pMasterKey.second.nDerivationMethod);
481  pMasterKey.second.nDeriveIterations =
482  (pMasterKey.second.nDeriveIterations +
483  static_cast<unsigned int>(
484  pMasterKey.second.nDeriveIterations * 100 /
485  double(GetTimeMillis() - nStartTime))) /
486  2;
487 
488  if (pMasterKey.second.nDeriveIterations < 25000) {
489  pMasterKey.second.nDeriveIterations = 25000;
490  }
491 
493  "Wallet passphrase changed to an nDeriveIterations of %i\n",
494  pMasterKey.second.nDeriveIterations);
495 
496  if (!crypter.SetKeyFromPassphrase(
497  strNewWalletPassphrase, pMasterKey.second.vchSalt,
498  pMasterKey.second.nDeriveIterations,
499  pMasterKey.second.nDerivationMethod)) {
500  return false;
501  }
502 
503  if (!crypter.Encrypt(_vMasterKey,
504  pMasterKey.second.vchCryptedKey)) {
505  return false;
506  }
507 
508  WalletBatch(*database).WriteMasterKey(pMasterKey.first,
509  pMasterKey.second);
510  if (fWasLocked) {
511  Lock();
512  }
513 
514  return true;
515  }
516  }
517 
518  return false;
519 }
520 
522  WalletBatch batch(*database);
523  batch.WriteBestBlock(loc);
524 }
525 
526 void CWallet::SetMinVersion(enum WalletFeature nVersion, WalletBatch *batch_in,
527  bool fExplicit) {
528  LOCK(cs_wallet);
529  if (nWalletVersion >= nVersion) {
530  return;
531  }
532 
533  // When doing an explicit upgrade, if we pass the max version permitted,
534  // upgrade all the way.
535  if (fExplicit && nVersion > nWalletMaxVersion) {
536  nVersion = FEATURE_LATEST;
537  }
538 
539  nWalletVersion = nVersion;
540 
541  if (nVersion > nWalletMaxVersion) {
542  nWalletMaxVersion = nVersion;
543  }
544 
545  WalletBatch *batch = batch_in ? batch_in : new WalletBatch(*database);
546  if (nWalletVersion > 40000) {
547  batch->WriteMinVersion(nWalletVersion);
548  }
549  if (!batch_in) {
550  delete batch;
551  }
552 }
553 
554 bool CWallet::SetMaxVersion(int nVersion) {
555  LOCK(cs_wallet);
556 
557  // Cannot downgrade below current version
558  if (nWalletVersion > nVersion) {
559  return false;
560  }
561 
562  nWalletMaxVersion = nVersion;
563 
564  return true;
565 }
566 
567 std::set<TxId> CWallet::GetConflicts(const TxId &txid) const {
568  std::set<TxId> result;
570 
571  std::map<TxId, CWalletTx>::const_iterator it = mapWallet.find(txid);
572  if (it == mapWallet.end()) {
573  return result;
574  }
575 
576  const CWalletTx &wtx = it->second;
577 
578  std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range;
579 
580  for (const CTxIn &txin : wtx.tx->vin) {
581  if (mapTxSpends.count(txin.prevout) <= 1) {
582  // No conflict if zero or one spends.
583  continue;
584  }
585 
586  range = mapTxSpends.equal_range(txin.prevout);
587  for (TxSpends::const_iterator _it = range.first; _it != range.second;
588  ++_it) {
589  result.insert(_it->second);
590  }
591  }
592 
593  return result;
594 }
595 
596 bool CWallet::HasWalletSpend(const TxId &txid) const {
598  auto iter = mapTxSpends.lower_bound(COutPoint(txid, 0));
599  return (iter != mapTxSpends.end() && iter->first.GetTxId() == txid);
600 }
601 
603  database->Flush();
604 }
605 
607  database->Close();
608 }
609 
611  std::pair<TxSpends::iterator, TxSpends::iterator> range) {
612  // We want all the wallet transactions in range to have the same metadata as
613  // the oldest (smallest nOrderPos).
614  // So: find smallest nOrderPos:
615 
616  int nMinOrderPos = std::numeric_limits<int>::max();
617  const CWalletTx *copyFrom = nullptr;
618  for (TxSpends::iterator it = range.first; it != range.second; ++it) {
619  const CWalletTx *wtx = &mapWallet.at(it->second);
620  if (wtx->nOrderPos < nMinOrderPos) {
621  nMinOrderPos = wtx->nOrderPos;
622  copyFrom = wtx;
623  }
624  }
625 
626  if (!copyFrom) {
627  return;
628  }
629 
630  // Now copy data from copyFrom to rest:
631  for (TxSpends::iterator it = range.first; it != range.second; ++it) {
632  const TxId &txid = it->second;
633  CWalletTx *copyTo = &mapWallet.at(txid);
634  if (copyFrom == copyTo) {
635  continue;
636  }
637 
638  assert(
639  copyFrom &&
640  "Oldest wallet transaction in range assumed to have been found.");
641 
642  if (!copyFrom->IsEquivalentTo(*copyTo)) {
643  continue;
644  }
645 
646  copyTo->mapValue = copyFrom->mapValue;
647  copyTo->vOrderForm = copyFrom->vOrderForm;
648  // fTimeReceivedIsTxTime not copied on purpose nTimeReceived not copied
649  // on purpose.
650  copyTo->nTimeSmart = copyFrom->nTimeSmart;
651  copyTo->fFromMe = copyFrom->fFromMe;
652  // nOrderPos not copied on purpose cached members not copied on purpose.
653  }
654 }
655 
659 bool CWallet::IsSpent(const COutPoint &outpoint) const {
661 
662  std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range =
663  mapTxSpends.equal_range(outpoint);
664 
665  for (TxSpends::const_iterator it = range.first; it != range.second; ++it) {
666  const TxId &wtxid = it->second;
667  std::map<TxId, CWalletTx>::const_iterator mit = mapWallet.find(wtxid);
668  if (mit != mapWallet.end()) {
669  int depth = GetTxDepthInMainChain(mit->second);
670  if (depth > 0 || (depth == 0 && !mit->second.isAbandoned())) {
671  // Spent
672  return true;
673  }
674  }
675  }
676 
677  return false;
678 }
679 
680 void CWallet::AddToSpends(const COutPoint &outpoint, const TxId &wtxid) {
681  mapTxSpends.insert(std::make_pair(outpoint, wtxid));
682 
683  setLockedCoins.erase(outpoint);
684 
685  std::pair<TxSpends::iterator, TxSpends::iterator> range;
686  range = mapTxSpends.equal_range(outpoint);
687  SyncMetaData(range);
688 }
689 
690 void CWallet::AddToSpends(const TxId &wtxid) {
691  auto it = mapWallet.find(wtxid);
692  assert(it != mapWallet.end());
693  const CWalletTx &thisTx = it->second;
694  // Coinbases don't spend anything!
695  if (thisTx.IsCoinBase()) {
696  return;
697  }
698 
699  for (const CTxIn &txin : thisTx.tx->vin) {
700  AddToSpends(txin.prevout, wtxid);
701  }
702 }
703 
704 bool CWallet::EncryptWallet(const SecureString &strWalletPassphrase) {
705  if (IsCrypted()) {
706  return false;
707  }
708 
709  CKeyingMaterial _vMasterKey;
710 
711  _vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
712  GetStrongRandBytes(_vMasterKey);
713 
714  CMasterKey kMasterKey;
715 
716  kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
717  GetStrongRandBytes(kMasterKey.vchSalt);
718 
719  CCrypter crypter;
720  int64_t nStartTime = GetTimeMillis();
721  crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000,
722  kMasterKey.nDerivationMethod);
723  kMasterKey.nDeriveIterations = static_cast<unsigned int>(
724  2500000 / double(GetTimeMillis() - nStartTime));
725 
726  nStartTime = GetTimeMillis();
727  crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt,
728  kMasterKey.nDeriveIterations,
729  kMasterKey.nDerivationMethod);
730  kMasterKey.nDeriveIterations =
731  (kMasterKey.nDeriveIterations +
732  static_cast<unsigned int>(kMasterKey.nDeriveIterations * 100 /
733  double(GetTimeMillis() - nStartTime))) /
734  2;
735 
736  if (kMasterKey.nDeriveIterations < 25000) {
737  kMasterKey.nDeriveIterations = 25000;
738  }
739 
740  WalletLogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n",
741  kMasterKey.nDeriveIterations);
742 
743  if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt,
744  kMasterKey.nDeriveIterations,
745  kMasterKey.nDerivationMethod)) {
746  return false;
747  }
748 
749  if (!crypter.Encrypt(_vMasterKey, kMasterKey.vchCryptedKey)) {
750  return false;
751  }
752 
753  {
754  LOCK(cs_wallet);
755  mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
756  WalletBatch *encrypted_batch = new WalletBatch(*database);
757  if (!encrypted_batch->TxnBegin()) {
758  delete encrypted_batch;
759  encrypted_batch = nullptr;
760  return false;
761  }
762  encrypted_batch->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
763 
764  for (const auto &spk_man_pair : m_spk_managers) {
765  auto spk_man = spk_man_pair.second.get();
766  if (!spk_man->Encrypt(_vMasterKey, encrypted_batch)) {
767  encrypted_batch->TxnAbort();
768  delete encrypted_batch;
769  encrypted_batch = nullptr;
770  // We now probably have half of our keys encrypted in memory,
771  // and half not... die and let the user reload the unencrypted
772  // wallet.
773  assert(false);
774  }
775  }
776 
777  // Encryption was introduced in version 0.4.0
778  SetMinVersion(FEATURE_WALLETCRYPT, encrypted_batch, true);
779 
780  if (!encrypted_batch->TxnCommit()) {
781  delete encrypted_batch;
782  encrypted_batch = nullptr;
783  // We now have keys encrypted in memory, but not on disk...
784  // die to avoid confusion and let the user reload the unencrypted
785  // wallet.
786  assert(false);
787  }
788 
789  delete encrypted_batch;
790  encrypted_batch = nullptr;
791 
792  Lock();
793  Unlock(strWalletPassphrase);
794 
795  // If we are using descriptors, make new descriptors with a new seed
799  } else if (auto spk_man = GetLegacyScriptPubKeyMan()) {
800  // if we are using HD, replace the HD seed with a new one
801  if (spk_man->IsHDEnabled()) {
802  if (!spk_man->SetupGeneration(true)) {
803  return false;
804  }
805  }
806  }
807  Lock();
808 
809  // Need to completely rewrite the wallet file; if we don't, bdb might
810  // keep bits of the unencrypted private key in slack space in the
811  // database file.
812  database->Rewrite();
813 
814  // BDB seems to have a bad habit of writing old data into
815  // slack space in .dat files; that is bad if the old data is
816  // unencrypted private keys. So:
817  database->ReloadDbEnv();
818  }
819 
820  NotifyStatusChanged(this);
821  return true;
822 }
823 
825  LOCK(cs_wallet);
826  WalletBatch batch(*database);
827 
828  // Old wallets didn't have any defined order for transactions. Probably a
829  // bad idea to change the output of this.
830 
831  // First: get all CWalletTx into a sorted-by-time
832  // multimap.
833  TxItems txByTime;
834 
835  for (auto &entry : mapWallet) {
836  CWalletTx *wtx = &entry.second;
837  txByTime.insert(std::make_pair(wtx->nTimeReceived, wtx));
838  }
839 
840  nOrderPosNext = 0;
841  std::vector<int64_t> nOrderPosOffsets;
842  for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it) {
843  CWalletTx *const pwtx = (*it).second;
844  int64_t &nOrderPos = pwtx->nOrderPos;
845 
846  if (nOrderPos == -1) {
847  nOrderPos = nOrderPosNext++;
848  nOrderPosOffsets.push_back(nOrderPos);
849 
850  if (!batch.WriteTx(*pwtx)) {
851  return DBErrors::LOAD_FAIL;
852  }
853  } else {
854  int64_t nOrderPosOff = 0;
855  for (const int64_t &nOffsetStart : nOrderPosOffsets) {
856  if (nOrderPos >= nOffsetStart) {
857  ++nOrderPosOff;
858  }
859  }
860 
861  nOrderPos += nOrderPosOff;
862  nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
863 
864  if (!nOrderPosOff) {
865  continue;
866  }
867 
868  // Since we're changing the order, write it back.
869  if (!batch.WriteTx(*pwtx)) {
870  return DBErrors::LOAD_FAIL;
871  }
872  }
873  }
874 
875  batch.WriteOrderPosNext(nOrderPosNext);
876 
877  return DBErrors::LOAD_OK;
878 }
879 
882  int64_t nRet = nOrderPosNext++;
883  if (batch) {
884  batch->WriteOrderPosNext(nOrderPosNext);
885  } else {
886  WalletBatch(*database).WriteOrderPosNext(nOrderPosNext);
887  }
888 
889  return nRet;
890 }
891 
893  LOCK(cs_wallet);
894  for (std::pair<const TxId, CWalletTx> &item : mapWallet) {
895  item.second.MarkDirty();
896  }
897 }
898 
899 void CWallet::SetSpentKeyState(WalletBatch &batch, const TxId &txid,
900  unsigned int n, bool used,
901  std::set<CTxDestination> &tx_destinations) {
903  const CWalletTx *srctx = GetWalletTx(txid);
904  if (!srctx) {
905  return;
906  }
907 
908  CTxDestination dst;
909  if (ExtractDestination(srctx->tx->vout[n].scriptPubKey, dst)) {
910  if (IsMine(dst)) {
911  if (used && !GetDestData(dst, "used", nullptr)) {
912  // p for "present", opposite of absent (null)
913  if (AddDestData(batch, dst, "used", "p")) {
914  tx_destinations.insert(dst);
915  }
916  } else if (!used && GetDestData(dst, "used", nullptr)) {
917  EraseDestData(batch, dst, "used");
918  }
919  }
920  }
921 }
922 
923 bool CWallet::IsSpentKey(const TxId &txid, unsigned int n) const {
925  const CWalletTx *srctx = GetWalletTx(txid);
926  if (srctx) {
927  assert(srctx->tx->vout.size() > n);
928  CTxDestination dest;
929  if (!ExtractDestination(srctx->tx->vout[n].scriptPubKey, dest)) {
930  return false;
931  }
932  if (GetDestData(dest, "used", nullptr)) {
933  return true;
934  }
935  if (IsLegacy()) {
937  assert(spk_man != nullptr);
938  for (const auto &keyid :
939  GetAffectedKeys(srctx->tx->vout[n].scriptPubKey, *spk_man)) {
940  PKHash pkh_dest(keyid);
941  if (GetDestData(pkh_dest, "used", nullptr)) {
942  return true;
943  }
944  }
945  }
946  }
947  return false;
948 }
949 
951  const CWalletTx::Confirmation &confirm,
952  const UpdateWalletTxFn &update_wtx,
953  bool fFlushOnClose) {
954  LOCK(cs_wallet);
955 
956  WalletBatch batch(*database, fFlushOnClose);
957 
958  const TxId &txid = tx->GetId();
959 
961  // Mark used destinations
962  std::set<CTxDestination> tx_destinations;
963 
964  for (const CTxIn &txin : tx->vin) {
965  const COutPoint &op = txin.prevout;
966  SetSpentKeyState(batch, op.GetTxId(), op.GetN(), true,
967  tx_destinations);
968  }
969 
970  MarkDestinationsDirty(tx_destinations);
971  }
972 
973  // Inserts only if not already there, returns tx inserted or tx found.
974  auto ret =
975  mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(txid),
976  std::forward_as_tuple(tx));
977  CWalletTx &wtx = (*ret.first).second;
978  bool fInsertedNew = ret.second;
979  bool fUpdated = update_wtx && update_wtx(wtx, fInsertedNew);
980  if (fInsertedNew) {
981  wtx.m_confirm = confirm;
982  wtx.nTimeReceived = GetTime();
983  wtx.nOrderPos = IncOrderPosNext(&batch);
984  wtx.m_it_wtxOrdered =
985  wtxOrdered.insert(std::make_pair(wtx.nOrderPos, &wtx));
986  wtx.nTimeSmart = ComputeTimeSmart(wtx);
987  AddToSpends(txid);
988  }
989 
990  if (!fInsertedNew) {
991  if (confirm.status != wtx.m_confirm.status) {
992  wtx.m_confirm.status = confirm.status;
993  wtx.m_confirm.nIndex = confirm.nIndex;
994  wtx.m_confirm.hashBlock = confirm.hashBlock;
995  wtx.m_confirm.block_height = confirm.block_height;
996  fUpdated = true;
997  } else {
998  assert(wtx.m_confirm.nIndex == confirm.nIndex);
999  assert(wtx.m_confirm.hashBlock == confirm.hashBlock);
1000  assert(wtx.m_confirm.block_height == confirm.block_height);
1001  }
1002  }
1003 
1005  WalletLogPrintf("AddToWallet %s %s%s\n", txid.ToString(),
1006  (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
1007 
1008  // Write to disk
1009  if ((fInsertedNew || fUpdated) && !batch.WriteTx(wtx)) {
1010  return nullptr;
1011  }
1012 
1013  // Break debit/credit balance caches:
1014  wtx.MarkDirty();
1015 
1016  // Notify UI of new or updated transaction.
1017  NotifyTransactionChanged(this, txid, fInsertedNew ? CT_NEW : CT_UPDATED);
1018 
1019 #if defined(HAVE_SYSTEM)
1020  // Notify an external script when a wallet transaction comes in or is
1021  // updated.
1022  std::string strCmd = gArgs.GetArg("-walletnotify", "");
1023 
1024  if (!strCmd.empty()) {
1025  ReplaceAll(strCmd, "%s", txid.GetHex());
1026 #ifndef WIN32
1027  // Substituting the wallet name isn't currently supported on windows
1028  // because windows shell escaping has not been implemented yet:
1029  // https://github.com/bitcoin/bitcoin/pull/13339#issuecomment-537384875
1030  // A few ways it could be implemented in the future are described in:
1031  // https://github.com/bitcoin/bitcoin/pull/13339#issuecomment-461288094
1032  ReplaceAll(strCmd, "%w", ShellEscape(GetName()));
1033 #endif
1034 
1035  std::thread t(runCommand, strCmd);
1036  // Thread runs free.
1037  t.detach();
1038  }
1039 #endif
1040 
1041  return &wtx;
1042 }
1043 
1044 bool CWallet::LoadToWallet(const TxId &txid, const UpdateWalletTxFn &fill_wtx) {
1045  const auto &ins =
1046  mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(txid),
1047  std::forward_as_tuple(nullptr));
1048  CWalletTx &wtx = ins.first->second;
1049  if (!fill_wtx(wtx, ins.second)) {
1050  return false;
1051  }
1052  // If wallet doesn't have a chain (e.g wallet-tool), don't bother to update
1053  // txn.
1054  if (HaveChain()) {
1055  bool active;
1056  int height;
1057  if (chain().findBlock(
1058  wtx.m_confirm.hashBlock,
1059  FoundBlock().inActiveChain(active).height(height)) &&
1060  active) {
1061  // Update cached block height variable since it not stored in the
1062  // serialized transaction.
1063  wtx.m_confirm.block_height = height;
1064  } else if (wtx.isConflicted() || wtx.isConfirmed()) {
1065  // If tx block (or conflicting block) was reorged out of chain
1066  // while the wallet was shutdown, change tx status to UNCONFIRMED
1067  // and reset block height, hash, and index. ABANDONED tx don't have
1068  // associated blocks and don't need to be updated. The case where a
1069  // transaction was reorged out while online and then reconfirmed
1070  // while offline is covered by the rescan logic.
1071  wtx.setUnconfirmed();
1072  wtx.m_confirm.hashBlock = BlockHash();
1073  wtx.m_confirm.block_height = 0;
1074  wtx.m_confirm.nIndex = 0;
1075  }
1076  }
1077  if (/* insertion took place */ ins.second) {
1078  wtx.m_it_wtxOrdered =
1079  wtxOrdered.insert(std::make_pair(wtx.nOrderPos, &wtx));
1080  }
1081  AddToSpends(txid);
1082  for (const CTxIn &txin : wtx.tx->vin) {
1083  auto it = mapWallet.find(txin.prevout.GetTxId());
1084  if (it != mapWallet.end()) {
1085  CWalletTx &prevtx = it->second;
1086  if (prevtx.isConflicted()) {
1088  prevtx.m_confirm.block_height, wtx.GetId());
1089  }
1090  }
1091  }
1092  return true;
1093 }
1094 
1096  CWalletTx::Confirmation confirm,
1097  bool fUpdate) {
1099 
1100  const TxId &txid = ptx->GetId();
1101 
1102  if (!confirm.hashBlock.IsNull()) {
1103  for (const CTxIn &txin : ptx->vin) {
1104  std::pair<TxSpends::const_iterator, TxSpends::const_iterator>
1105  range = mapTxSpends.equal_range(txin.prevout);
1106  while (range.first != range.second) {
1107  if (range.first->second != txid) {
1109  "Transaction %s (in block %s) conflicts with wallet "
1110  "transaction %s (both spend %s:%i)\n",
1111  txid.ToString(), confirm.hashBlock.ToString(),
1112  range.first->second.ToString(),
1113  range.first->first.GetTxId().ToString(),
1114  range.first->first.GetN());
1115  MarkConflicted(confirm.hashBlock, confirm.block_height,
1116  range.first->second);
1117  }
1118  range.first++;
1119  }
1120  }
1121  }
1122 
1123  bool fExisted = mapWallet.count(txid) != 0;
1124  if (fExisted && !fUpdate) {
1125  return false;
1126  }
1127  if (fExisted || IsMine(*ptx) || IsFromMe(*ptx)) {
1136  // loop though all outputs
1137  for (const CTxOut &txout : ptx->vout) {
1138  for (const auto &spk_man_pair : m_spk_managers) {
1139  spk_man_pair.second->MarkUnusedAddresses(txout.scriptPubKey);
1140  }
1141  }
1142 
1143  // Block disconnection override an abandoned tx as unconfirmed
1144  // which means user may have to call abandontransaction again
1145  return AddToWallet(ptx, confirm,
1146  /* update_wtx= */ nullptr,
1147  /* fFlushOnClose= */ false);
1148  }
1149  return false;
1150 }
1151 
1152 bool CWallet::TransactionCanBeAbandoned(const TxId &txid) const {
1153  LOCK(cs_wallet);
1154  const CWalletTx *wtx = GetWalletTx(txid);
1155  return wtx && !wtx->isAbandoned() && GetTxDepthInMainChain(*wtx) == 0 &&
1156  !wtx->InMempool();
1157 }
1158 
1160  for (const CTxIn &txin : tx->vin) {
1161  auto it = mapWallet.find(txin.prevout.GetTxId());
1162  if (it != mapWallet.end()) {
1163  it->second.MarkDirty();
1164  }
1165  }
1166 }
1167 
1169  LOCK(cs_wallet);
1170 
1171  WalletBatch batch(*database);
1172 
1173  std::set<TxId> todo;
1174  std::set<TxId> done;
1175 
1176  // Can't mark abandoned if confirmed or in mempool
1177  auto it = mapWallet.find(txid);
1178  assert(it != mapWallet.end());
1179  const CWalletTx &origtx = it->second;
1180  if (GetTxDepthInMainChain(origtx) != 0 || origtx.InMempool()) {
1181  return false;
1182  }
1183 
1184  todo.insert(txid);
1185 
1186  while (!todo.empty()) {
1187  const TxId now = *todo.begin();
1188  todo.erase(now);
1189  done.insert(now);
1190  it = mapWallet.find(now);
1191  assert(it != mapWallet.end());
1192  CWalletTx &wtx = it->second;
1193  int currentconfirm = GetTxDepthInMainChain(wtx);
1194  // If the orig tx was not in block, none of its spends can be.
1195  assert(currentconfirm <= 0);
1196  // If (currentconfirm < 0) {Tx and spends are already conflicted, no
1197  // need to abandon}
1198  if (currentconfirm == 0 && !wtx.isAbandoned()) {
1199  // If the orig tx was not in block/mempool, none of its spends can
1200  // be in mempool.
1201  assert(!wtx.InMempool());
1202  wtx.setAbandoned();
1203  wtx.MarkDirty();
1204  batch.WriteTx(wtx);
1206  // Iterate over all its outputs, and mark transactions in the wallet
1207  // that spend them abandoned too.
1208  TxSpends::const_iterator iter =
1209  mapTxSpends.lower_bound(COutPoint(now, 0));
1210  while (iter != mapTxSpends.end() && iter->first.GetTxId() == now) {
1211  if (!done.count(iter->second)) {
1212  todo.insert(iter->second);
1213  }
1214  iter++;
1215  }
1216 
1217  // If a transaction changes 'conflicted' state, that changes the
1218  // balance available of the outputs it spends. So force those to be
1219  // recomputed.
1220  MarkInputsDirty(wtx.tx);
1221  }
1222  }
1223 
1224  return true;
1225 }
1226 
1227 void CWallet::MarkConflicted(const BlockHash &hashBlock, int conflicting_height,
1228  const TxId &txid) {
1229  LOCK(cs_wallet);
1230 
1231  int conflictconfirms =
1232  (m_last_block_processed_height - conflicting_height + 1) * -1;
1233 
1234  // If number of conflict confirms cannot be determined, this means that the
1235  // block is still unknown or not yet part of the main chain, for example
1236  // when loading the wallet during a reindex. Do nothing in that case.
1237  if (conflictconfirms >= 0) {
1238  return;
1239  }
1240 
1241  // Do not flush the wallet here for performance reasons.
1242  WalletBatch batch(*database, false);
1243 
1244  std::set<TxId> todo;
1245  std::set<TxId> done;
1246 
1247  todo.insert(txid);
1248 
1249  while (!todo.empty()) {
1250  const TxId now = *todo.begin();
1251  todo.erase(now);
1252  done.insert(now);
1253  auto it = mapWallet.find(now);
1254  assert(it != mapWallet.end());
1255  CWalletTx &wtx = it->second;
1256  int currentconfirm = GetTxDepthInMainChain(wtx);
1257  if (conflictconfirms < currentconfirm) {
1258  // Block is 'more conflicted' than current confirm; update.
1259  // Mark transaction as conflicted with this block.
1260  wtx.m_confirm.nIndex = 0;
1261  wtx.m_confirm.hashBlock = hashBlock;
1262  wtx.m_confirm.block_height = conflicting_height;
1263  wtx.setConflicted();
1264  wtx.MarkDirty();
1265  batch.WriteTx(wtx);
1266  // Iterate over all its outputs, and mark transactions in the wallet
1267  // that spend them conflicted too.
1268  TxSpends::const_iterator iter =
1269  mapTxSpends.lower_bound(COutPoint(now, 0));
1270  while (iter != mapTxSpends.end() && iter->first.GetTxId() == now) {
1271  if (!done.count(iter->second)) {
1272  todo.insert(iter->second);
1273  }
1274  iter++;
1275  }
1276  // If a transaction changes 'conflicted' state, that changes the
1277  // balance available of the outputs it spends. So force those to be
1278  // recomputed.
1279  MarkInputsDirty(wtx.tx);
1280  }
1281  }
1282 }
1283 
1285  CWalletTx::Confirmation confirm, bool update_tx) {
1286  if (!AddToWalletIfInvolvingMe(ptx, confirm, update_tx)) {
1287  // Not one of ours
1288  return;
1289  }
1290 
1291  // If a transaction changes 'conflicted' state, that changes the balance
1292  // available of the outputs it spends. So force those to be
1293  // recomputed, also:
1294  MarkInputsDirty(ptx);
1295 }
1296 
1298  uint64_t mempool_sequence) {
1299  LOCK(cs_wallet);
1300 
1301  SyncTransaction(tx, {CWalletTx::Status::UNCONFIRMED, /* block_height */ 0,
1302  BlockHash(), /* nIndex */ 0});
1303 
1304  auto it = mapWallet.find(tx->GetId());
1305  if (it != mapWallet.end()) {
1306  it->second.fInMempool = true;
1307  }
1308 }
1309 
1311  MemPoolRemovalReason reason,
1312  uint64_t mempool_sequence) {
1313  LOCK(cs_wallet);
1314  auto it = mapWallet.find(tx->GetId());
1315  if (it != mapWallet.end()) {
1316  it->second.fInMempool = false;
1317  }
1318  // Handle transactions that were removed from the mempool because they
1319  // conflict with transactions in a newly connected block.
1320  if (reason == MemPoolRemovalReason::CONFLICT) {
1321  // Call SyncNotifications, so external -walletnotify notifications will
1322  // be triggered for these transactions. Set Status::UNCONFIRMED instead
1323  // of Status::CONFLICTED for a few reasons:
1324  //
1325  // 1. The transactionRemovedFromMempool callback does not currently
1326  // provide the conflicting block's hash and height, and for backwards
1327  // compatibility reasons it may not be not safe to store conflicted
1328  // wallet transactions with a null block hash. See
1329  // https://github.com/bitcoin/bitcoin/pull/18600#discussion_r420195993.
1330  // 2. For most of these transactions, the wallet's internal conflict
1331  // detection in the blockConnected handler will subsequently call
1332  // MarkConflicted and update them with CONFLICTED status anyway. This
1333  // applies to any wallet transaction that has inputs spent in the
1334  // block, or that has ancestors in the wallet with inputs spent by
1335  // the block.
1336  // 3. Longstanding behavior since the sync implementation in
1337  // https://github.com/bitcoin/bitcoin/pull/9371 and the prior sync
1338  // implementation before that was to mark these transactions
1339  // unconfirmed rather than conflicted.
1340  //
1341  // Nothing described above should be seen as an unchangeable requirement
1342  // when improving this code in the future. The wallet's heuristics for
1343  // distinguishing between conflicted and unconfirmed transactions are
1344  // imperfect, and could be improved in general, see
1345  // https://github.com/bitcoin-core/bitcoin-devwiki/wiki/Wallet-Transaction-Conflict-Tracking
1346  SyncTransaction(tx,
1347  {CWalletTx::Status::UNCONFIRMED, /* block height */ 0,
1348  BlockHash(), /* index */ 0});
1349  }
1350 }
1351 
1352 void CWallet::blockConnected(const CBlock &block, int height) {
1353  const BlockHash &block_hash = block.GetHash();
1354  LOCK(cs_wallet);
1355 
1356  m_last_block_processed_height = height;
1357  m_last_block_processed = block_hash;
1358  for (size_t index = 0; index < block.vtx.size(); index++) {
1359  SyncTransaction(block.vtx[index], {CWalletTx::Status::CONFIRMED, height,
1360  block_hash, int(index)});
1361  transactionRemovedFromMempool(block.vtx[index],
1363  0 /* mempool_sequence */);
1364  }
1365 }
1366 
1367 void CWallet::blockDisconnected(const CBlock &block, int height) {
1368  LOCK(cs_wallet);
1369 
1370  // At block disconnection, this will change an abandoned transaction to
1371  // be unconfirmed, whether or not the transaction is added back to the
1372  // mempool. User may have to call abandontransaction again. It may be
1373  // addressed in the future with a stickier abandoned state or even removing
1374  // abandontransaction call.
1375  m_last_block_processed_height = height - 1;
1376  m_last_block_processed = block.hashPrevBlock;
1377  for (const CTransactionRef &ptx : block.vtx) {
1378  SyncTransaction(ptx,
1379  {CWalletTx::Status::UNCONFIRMED, /* block_height */ 0,
1380  BlockHash(), /* nIndex */ 0});
1381  }
1382 }
1383 
1386 }
1387 
1388 void CWallet::BlockUntilSyncedToCurrentChain() const {
1390  // Skip the queue-draining stuff if we know we're caught up with
1391  // chain().Tip(), otherwise put a callback in the validation interface
1392  // queue and wait for the queue to drain enough to execute it (indicating we
1393  // are caught up at least with the time we entered this function).
1394  const BlockHash last_block_hash =
1395  WITH_LOCK(cs_wallet, return m_last_block_processed);
1396  chain().waitForNotificationsIfTipChanged(last_block_hash);
1397 }
1398 
1399 // Note that this function doesn't distinguish between a 0-valued input, and a
1400 // not-"is mine" (according to the filter) input.
1401 Amount CWallet::GetDebit(const CTxIn &txin, const isminefilter &filter) const {
1402  LOCK(cs_wallet);
1403  std::map<TxId, CWalletTx>::const_iterator mi =
1404  mapWallet.find(txin.prevout.GetTxId());
1405  if (mi != mapWallet.end()) {
1406  const CWalletTx &prev = (*mi).second;
1407  if (txin.prevout.GetN() < prev.tx->vout.size()) {
1408  if (IsMine(prev.tx->vout[txin.prevout.GetN()]) & filter) {
1409  return prev.tx->vout[txin.prevout.GetN()].nValue;
1410  }
1411  }
1412  }
1413 
1414  return Amount::zero();
1415 }
1416 
1417 isminetype CWallet::IsMine(const CTxOut &txout) const {
1419  return IsMine(txout.scriptPubKey);
1420 }
1421 
1424  return IsMine(GetScriptForDestination(dest));
1425 }
1426 
1427 isminetype CWallet::IsMine(const CScript &script) const {
1429  isminetype result = ISMINE_NO;
1430  for (const auto &spk_man_pair : m_spk_managers) {
1431  result = std::max(result, spk_man_pair.second->IsMine(script));
1432  }
1433  return result;
1434 }
1435 
1436 bool CWallet::IsMine(const CTransaction &tx) const {
1438  for (const CTxOut &txout : tx.vout) {
1439  if (IsMine(txout)) {
1440  return true;
1441  }
1442  }
1443 
1444  return false;
1445 }
1446 
1447 bool CWallet::IsFromMe(const CTransaction &tx) const {
1448  return GetDebit(tx, ISMINE_ALL) > Amount::zero();
1449 }
1450 
1452  const isminefilter &filter) const {
1453  Amount nDebit = Amount::zero();
1454  for (const CTxIn &txin : tx.vin) {
1455  nDebit += GetDebit(txin, filter);
1456  if (!MoneyRange(nDebit)) {
1457  throw std::runtime_error(std::string(__func__) +
1458  ": value out of range");
1459  }
1460  }
1461 
1462  return nDebit;
1463 }
1464 
1465 bool CWallet::IsHDEnabled() const {
1466  // All Active ScriptPubKeyMans must be HD for this to be true
1467  bool result = true;
1468  for (const auto &spk_man : GetActiveScriptPubKeyMans()) {
1469  result &= spk_man->IsHDEnabled();
1470  }
1471  return result;
1472 }
1473 
1474 bool CWallet::CanGetAddresses(bool internal) const {
1475  LOCK(cs_wallet);
1476  if (m_spk_managers.empty()) {
1477  return false;
1478  }
1479  for (OutputType t : OUTPUT_TYPES) {
1480  auto spk_man = GetScriptPubKeyMan(t, internal);
1481  if (spk_man && spk_man->CanGetAddresses(internal)) {
1482  return true;
1483  }
1484  }
1485  return false;
1486 }
1487 
1488 void CWallet::SetWalletFlag(uint64_t flags) {
1489  LOCK(cs_wallet);
1490  m_wallet_flags |= flags;
1491  if (!WalletBatch(*database).WriteWalletFlags(m_wallet_flags)) {
1492  throw std::runtime_error(std::string(__func__) +
1493  ": writing wallet flags failed");
1494  }
1495 }
1496 
1497 void CWallet::UnsetWalletFlag(uint64_t flag) {
1498  WalletBatch batch(*database);
1499  UnsetWalletFlagWithDB(batch, flag);
1500 }
1501 
1502 void CWallet::UnsetWalletFlagWithDB(WalletBatch &batch, uint64_t flag) {
1503  LOCK(cs_wallet);
1504  m_wallet_flags &= ~flag;
1505  if (!batch.WriteWalletFlags(m_wallet_flags)) {
1506  throw std::runtime_error(std::string(__func__) +
1507  ": writing wallet flags failed");
1508  }
1509 }
1510 
1513 }
1514 
1515 bool CWallet::IsWalletFlagSet(uint64_t flag) const {
1516  return (m_wallet_flags & flag);
1517 }
1518 
1520  LOCK(cs_wallet);
1521  if (((flags & KNOWN_WALLET_FLAGS) >> 32) ^ (flags >> 32)) {
1522  // contains unknown non-tolerable wallet flags
1523  return false;
1524  }
1526 
1527  return true;
1528 }
1529 
1531  LOCK(cs_wallet);
1532  // We should never be writing unknown non-tolerable wallet flags
1533  assert(((flags & KNOWN_WALLET_FLAGS) >> 32) == (flags >> 32));
1534  if (!WalletBatch(*database).WriteWalletFlags(flags)) {
1535  throw std::runtime_error(std::string(__func__) +
1536  ": writing wallet flags failed");
1537  }
1538 
1539  return LoadWalletFlags(flags);
1540 }
1541 
1542 // Helper for producing a max-sized low-S low-R signature (eg 71 bytes)
1543 // or a max-sized low-S signature (e.g. 72 bytes) if use_max_sig is true
1544 bool CWallet::DummySignInput(CTxIn &tx_in, const CTxOut &txout,
1545  bool use_max_sig) const {
1546  // Fill in dummy signatures for fee calculation.
1547  const CScript &scriptPubKey = txout.scriptPubKey;
1548  SignatureData sigdata;
1549 
1550  std::unique_ptr<SigningProvider> provider =
1551  GetSolvingProvider(scriptPubKey);
1552  if (!provider) {
1553  // We don't know about this scriptpbuKey;
1554  return false;
1555  }
1556 
1557  if (!ProduceSignature(*provider,
1558  use_max_sig ? DUMMY_MAXIMUM_SIGNATURE_CREATOR
1560  scriptPubKey, sigdata)) {
1561  return false;
1562  }
1563 
1564  UpdateInput(tx_in, sigdata);
1565  return true;
1566 }
1567 
1568 // Helper for producing a bunch of max-sized low-S low-R signatures (eg 71
1569 // bytes)
1571  const std::vector<CTxOut> &txouts,
1572  bool use_max_sig) const {
1573  // Fill in dummy signatures for fee calculation.
1574  int nIn = 0;
1575  for (const auto &txout : txouts) {
1576  if (!DummySignInput(txNew.vin[nIn], txout, use_max_sig)) {
1577  return false;
1578  }
1579 
1580  nIn++;
1581  }
1582  return true;
1583 }
1584 
1585 bool CWallet::ImportScripts(const std::set<CScript> scripts,
1586  int64_t timestamp) {
1587  auto spk_man = GetLegacyScriptPubKeyMan();
1588  if (!spk_man) {
1589  return false;
1590  }
1591  LOCK(spk_man->cs_KeyStore);
1592  return spk_man->ImportScripts(scripts, timestamp);
1593 }
1594 
1595 bool CWallet::ImportPrivKeys(const std::map<CKeyID, CKey> &privkey_map,
1596  const int64_t timestamp) {
1597  auto spk_man = GetLegacyScriptPubKeyMan();
1598  if (!spk_man) {
1599  return false;
1600  }
1601  LOCK(spk_man->cs_KeyStore);
1602  return spk_man->ImportPrivKeys(privkey_map, timestamp);
1603 }
1604 
1606  const std::vector<CKeyID> &ordered_pubkeys,
1607  const std::map<CKeyID, CPubKey> &pubkey_map,
1608  const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> &key_origins,
1609  const bool add_keypool, const bool internal, const int64_t timestamp) {
1610  auto spk_man = GetLegacyScriptPubKeyMan();
1611  if (!spk_man) {
1612  return false;
1613  }
1614  LOCK(spk_man->cs_KeyStore);
1615  return spk_man->ImportPubKeys(ordered_pubkeys, pubkey_map, key_origins,
1616  add_keypool, internal, timestamp);
1617 }
1618 
1619 bool CWallet::ImportScriptPubKeys(const std::string &label,
1620  const std::set<CScript> &script_pub_keys,
1621  const bool have_solving_data,
1622  const bool apply_label,
1623  const int64_t timestamp) {
1624  auto spk_man = GetLegacyScriptPubKeyMan();
1625  if (!spk_man) {
1626  return false;
1627  }
1628  LOCK(spk_man->cs_KeyStore);
1629  if (!spk_man->ImportScriptPubKeys(script_pub_keys, have_solving_data,
1630  timestamp)) {
1631  return false;
1632  }
1633  if (apply_label) {
1634  WalletBatch batch(*database);
1635  for (const CScript &script : script_pub_keys) {
1636  CTxDestination dest;
1637  ExtractDestination(script, dest);
1638  if (IsValidDestination(dest)) {
1639  SetAddressBookWithDB(batch, dest, label, "receive");
1640  }
1641  }
1642  }
1643  return true;
1644 }
1645 
1654 int64_t CWallet::RescanFromTime(int64_t startTime,
1655  const WalletRescanReserver &reserver,
1656  bool update) {
1657  // Find starting block. May be null if nCreateTime is greater than the
1658  // highest blockchain timestamp, in which case there is nothing that needs
1659  // to be scanned.
1660  int start_height = 0;
1661  BlockHash start_block;
1662  bool start = chain().findFirstBlockWithTimeAndHeight(
1663  startTime - TIMESTAMP_WINDOW, 0,
1664  FoundBlock().hash(start_block).height(start_height));
1665  WalletLogPrintf("%s: Rescanning last %i blocks\n", __func__,
1666  start ? WITH_LOCK(cs_wallet, return GetLastBlockHeight()) -
1667  start_height + 1
1668  : 0);
1669 
1670  if (start) {
1671  // TODO: this should take into account failure by ScanResult::USER_ABORT
1673  start_block, start_height, {} /* max_height */, reserver, update);
1674  if (result.status == ScanResult::FAILURE) {
1675  int64_t time_max;
1676  CHECK_NONFATAL(chain().findBlock(result.last_failed_block,
1677  FoundBlock().maxTime(time_max)));
1678  return time_max + TIMESTAMP_WINDOW + 1;
1679  }
1680  }
1681  return startTime;
1682 }
1683 
1706  const BlockHash &start_block, int start_height,
1707  std::optional<int> max_height, const WalletRescanReserver &reserver,
1708  bool fUpdate) {
1709  int64_t nNow = GetTime();
1710  int64_t start_time = GetTimeMillis();
1711 
1712  assert(reserver.isReserved());
1713 
1714  BlockHash block_hash = start_block;
1715  ScanResult result;
1716 
1717  WalletLogPrintf("Rescan started from block %s...\n",
1718  start_block.ToString());
1719 
1720  fAbortRescan = false;
1721  // Show rescan progress in GUI as dialog or on splashscreen, if -rescan on
1722  // startup.
1723  ShowProgress(
1724  strprintf("%s " + _("Rescanning...").translated, GetDisplayName()), 0);
1725  BlockHash tip_hash = WITH_LOCK(cs_wallet, return GetLastBlockHash());
1726  BlockHash end_hash = tip_hash;
1727  if (max_height) {
1728  chain().findAncestorByHeight(tip_hash, *max_height,
1729  FoundBlock().hash(end_hash));
1730  }
1731  double progress_begin = chain().guessVerificationProgress(block_hash);
1732  double progress_end = chain().guessVerificationProgress(end_hash);
1733  double progress_current = progress_begin;
1734  int block_height = start_height;
1735  while (!fAbortRescan && !chain().shutdownRequested()) {
1736  if (progress_end - progress_begin > 0.0) {
1737  m_scanning_progress = (progress_current - progress_begin) /
1738  (progress_end - progress_begin);
1739  } else {
1740  // avoid divide-by-zero for single block scan range (i.e. start and
1741  // stop hashes are equal)
1742  m_scanning_progress = 0;
1743  }
1744  if (block_height % 100 == 0 && progress_end - progress_begin > 0.0) {
1745  ShowProgress(
1746  strprintf("%s " + _("Rescanning...").translated,
1747  GetDisplayName()),
1748  std::max(1, std::min(99, (int)(m_scanning_progress * 100))));
1749  }
1750  if (GetTime() >= nNow + 60) {
1751  nNow = GetTime();
1752  WalletLogPrintf("Still rescanning. At block %d. Progress=%f\n",
1753  block_height, progress_current);
1754  }
1755 
1756  // Read block data
1757  CBlock block;
1758  chain().findBlock(block_hash, FoundBlock().data(block));
1759 
1760  // Find next block separately from reading data above, because reading
1761  // is slow and there might be a reorg while it is read.
1762  bool block_still_active = false;
1763  bool next_block = false;
1764  BlockHash next_block_hash;
1765  chain().findBlock(block_hash,
1766  FoundBlock()
1767  .inActiveChain(block_still_active)
1768  .nextBlock(FoundBlock()
1769  .inActiveChain(next_block)
1770  .hash(next_block_hash)));
1771 
1772  if (!block.IsNull()) {
1773  LOCK(cs_wallet);
1774  if (!block_still_active) {
1775  // Abort scan if current block is no longer active, to prevent
1776  // marking transactions as coming from the wrong block.
1777  result.last_failed_block = block_hash;
1778  result.status = ScanResult::FAILURE;
1779  break;
1780  }
1781  for (size_t posInBlock = 0; posInBlock < block.vtx.size();
1782  ++posInBlock) {
1783  CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED,
1784  block_height, block_hash,
1785  posInBlock);
1786  SyncTransaction(block.vtx[posInBlock],
1787  {CWalletTx::Status::CONFIRMED, block_height,
1788  block_hash, int(posInBlock)},
1789  fUpdate);
1790  }
1791  // scan succeeded, record block as most recent successfully
1792  // scanned
1793  result.last_scanned_block = block_hash;
1794  result.last_scanned_height = block_height;
1795  } else {
1796  // could not scan block, keep scanning but record this block as
1797  // the most recent failure
1798  result.last_failed_block = block_hash;
1799  result.status = ScanResult::FAILURE;
1800  }
1801  if (max_height && block_height >= *max_height) {
1802  break;
1803  }
1804  {
1805  if (!next_block) {
1806  // break successfully when rescan has reached the tip, or
1807  // previous block is no longer on the chain due to a reorg
1808  break;
1809  }
1810 
1811  // increment block and verification progress
1812  block_hash = next_block_hash;
1813  ++block_height;
1814  progress_current = chain().guessVerificationProgress(block_hash);
1815 
1816  // handle updated tip hash
1817  const BlockHash prev_tip_hash = tip_hash;
1818  tip_hash = WITH_LOCK(cs_wallet, return GetLastBlockHash());
1819  if (!max_height && prev_tip_hash != tip_hash) {
1820  // in case the tip has changed, update progress max
1821  progress_end = chain().guessVerificationProgress(tip_hash);
1822  }
1823  }
1824  }
1825 
1826  // Hide progress dialog in GUI.
1827  ShowProgress(
1828  strprintf("%s " + _("Rescanning...").translated, GetDisplayName()),
1829  100);
1830  if (block_height && fAbortRescan) {
1831  WalletLogPrintf("Rescan aborted at block %d. Progress=%f\n",
1832  block_height, progress_current);
1833  result.status = ScanResult::USER_ABORT;
1834  } else if (block_height && chain().shutdownRequested()) {
1836  "Rescan interrupted by shutdown request at block %d. Progress=%f\n",
1837  block_height, progress_current);
1838  result.status = ScanResult::USER_ABORT;
1839  } else {
1840  WalletLogPrintf("Rescan completed in %15dms\n",
1841  GetTimeMillis() - start_time);
1842  }
1843  return result;
1844 }
1845 
1848 
1849  // If transactions aren't being broadcasted, don't let them into local
1850  // mempool either.
1851  if (!fBroadcastTransactions) {
1852  return;
1853  }
1854 
1855  std::map<int64_t, CWalletTx *> mapSorted;
1856 
1857  // Sort pending wallet transactions based on their initial wallet insertion
1858  // order.
1859  for (std::pair<const TxId, CWalletTx> &item : mapWallet) {
1860  const TxId &wtxid = item.first;
1861  CWalletTx &wtx = item.second;
1862  assert(wtx.GetId() == wtxid);
1863 
1864  int nDepth = GetTxDepthInMainChain(wtx);
1865 
1866  if (!wtx.IsCoinBase() && (nDepth == 0 && !wtx.isAbandoned())) {
1867  mapSorted.insert(std::make_pair(wtx.nOrderPos, &wtx));
1868  }
1869  }
1870 
1871  // Try to add wallet transactions to memory pool.
1872  for (const std::pair<const int64_t, CWalletTx *> &item : mapSorted) {
1873  CWalletTx &wtx = *(item.second);
1874  std::string unused_err_string;
1875  SubmitTxMemoryPoolAndRelay(wtx, unused_err_string, false);
1876  }
1877 }
1878 
1880  std::string &err_string,
1881  bool relay) const {
1883 
1884  // Can't relay if wallet is not broadcasting
1885  if (!GetBroadcastTransactions()) {
1886  return false;
1887  }
1888  // Don't relay abandoned transactions
1889  if (wtx.isAbandoned()) {
1890  return false;
1891  }
1892  // Don't try to submit coinbase transactions. These would fail anyway but
1893  // would cause log spam.
1894  if (wtx.IsCoinBase()) {
1895  return false;
1896  }
1897  // Don't try to submit conflicted or confirmed transactions.
1898  if (GetTxDepthInMainChain(wtx) != 0) {
1899  return false;
1900  }
1901 
1902  // Submit transaction to mempool for relay
1903  WalletLogPrintf("Submitting wtx %s to mempool for relay\n",
1904  wtx.GetId().ToString());
1905  // We must set fInMempool here - while it will be re-set to true by the
1906  // entered-mempool callback, if we did not there would be a race where a
1907  // user could call sendmoney in a loop and hit spurious out of funds errors
1908  // because we think that this newly generated transaction's change is
1909  // unavailable as we're not yet aware that it is in the mempool.
1910  //
1911  // Irrespective of the failure reason, un-marking fInMempool
1912  // out-of-order is incorrect - it should be unmarked when
1913  // TransactionRemovedFromMempool fires.
1914  bool ret = chain().broadcastTransaction(
1915  GetConfig(), wtx.tx, m_default_max_tx_fee, relay, err_string);
1916  wtx.fInMempool |= ret;
1917  return ret;
1918 }
1919 
1920 std::set<TxId> CWallet::GetTxConflicts(const CWalletTx &wtx) const {
1922 
1923  std::set<TxId> result;
1924  const TxId &txid = wtx.GetId();
1925  result = GetConflicts(txid);
1926  result.erase(txid);
1927 
1928  return result;
1929 }
1930 
1931 // Rebroadcast transactions from the wallet. We do this on a random timer
1932 // to slightly obfuscate which transactions come from our wallet.
1933 //
1934 // Ideally, we'd only resend transactions that we think should have been
1935 // mined in the most recent block. Any transaction that wasn't in the top
1936 // blockweight of transactions in the mempool shouldn't have been mined,
1937 // and so is probably just sitting in the mempool waiting to be confirmed.
1938 // Rebroadcasting does nothing to speed up confirmation and only damages
1939 // privacy.
1941  // During reindex, importing and IBD, old wallet transactions become
1942  // unconfirmed. Don't resend them as that would spam other nodes.
1943  if (!chain().isReadyToBroadcast()) {
1944  return;
1945  }
1946 
1947  // Do this infrequently and randomly to avoid giving away that these are our
1948  // transactions.
1950  return;
1951  }
1952 
1953  bool fFirst = (nNextResend == 0);
1954  // resend 12-36 hours from now, ~1 day on average.
1955  nNextResend = GetTime() + (12 * 60 * 60) + GetRand(24 * 60 * 60);
1956  if (fFirst) {
1957  return;
1958  }
1959 
1960  int submitted_tx_count = 0;
1961 
1962  { // cs_wallet scope
1963  LOCK(cs_wallet);
1964 
1965  // Relay transactions
1966  for (std::pair<const TxId, CWalletTx> &item : mapWallet) {
1967  CWalletTx &wtx = item.second;
1968  // Attempt to rebroadcast all txes more than 5 minutes older than
1969  // the last block. SubmitTxMemoryPoolAndRelay() will not rebroadcast
1970  // any confirmed or conflicting txs.
1971  if (wtx.nTimeReceived > m_best_block_time - 5 * 60) {
1972  continue;
1973  }
1974  std::string unused_err_string;
1975  if (SubmitTxMemoryPoolAndRelay(wtx, unused_err_string, true)) {
1976  ++submitted_tx_count;
1977  }
1978  }
1979  } // cs_wallet
1980 
1981  if (submitted_tx_count > 0) {
1982  WalletLogPrintf("%s: resubmit %u unconfirmed transactions\n", __func__,
1983  submitted_tx_count);
1984  }
1985 }
1986  // end of mapWallet
1988 
1990  for (const std::shared_ptr<CWallet> &pwallet : GetWallets()) {
1991  pwallet->ResendWalletTransactions();
1992  }
1993 }
1994 
2003 
2004  // Build coins map
2005  std::map<COutPoint, Coin> coins;
2006  for (auto &input : tx.vin) {
2007  auto mi = mapWallet.find(input.prevout.GetTxId());
2008  if (mi == mapWallet.end() ||
2009  input.prevout.GetN() >= mi->second.tx->vout.size()) {
2010  return false;
2011  }
2012  const CWalletTx &wtx = mi->second;
2013  coins[input.prevout] =
2014  Coin(wtx.tx->vout[input.prevout.GetN()], wtx.m_confirm.block_height,
2015  wtx.IsCoinBase());
2016  }
2017  std::map<int, std::string> input_errors;
2018  return SignTransaction(tx, coins, SigHashType().withForkId(), input_errors);
2019 }
2020 
2022  const std::map<COutPoint, Coin> &coins,
2023  SigHashType sighash,
2024  std::map<int, std::string> &input_errors) const {
2025  // Try to sign with all ScriptPubKeyMans
2026  for (ScriptPubKeyMan *spk_man : GetAllScriptPubKeyMans()) {
2027  // spk_man->SignTransaction will return true if the transaction is
2028  // complete, so we can exit early and return true if that happens
2029  if (spk_man->SignTransaction(tx, coins, sighash, input_errors)) {
2030  return true;
2031  }
2032  }
2033 
2034  // At this point, one input was not fully signed otherwise we would have
2035  // exited already
2036 
2037  // When there are no available providers for the remaining inputs, use the
2038  // legacy provider so we can get proper error messages.
2039  auto legacy_spk_man = GetLegacyScriptPubKeyMan();
2040  if (legacy_spk_man &&
2041  legacy_spk_man->SignTransaction(tx, coins, sighash, input_errors)) {
2042  return true;
2043  }
2044 
2045  return false;
2046 }
2047 
2049  bool &complete, SigHashType sighash_type,
2050  bool sign, bool bip32derivs) const {
2051  LOCK(cs_wallet);
2052  // Get all of the previous transactions
2053  for (size_t i = 0; i < psbtx.tx->vin.size(); ++i) {
2054  const CTxIn &txin = psbtx.tx->vin[i];
2055  PSBTInput &input = psbtx.inputs.at(i);
2056 
2057  if (PSBTInputSigned(input)) {
2058  continue;
2059  }
2060 
2061  // If we have no utxo, grab it from the wallet.
2062  if (input.utxo.IsNull()) {
2063  const TxId &txid = txin.prevout.GetTxId();
2064  const auto it = mapWallet.find(txid);
2065  if (it != mapWallet.end()) {
2066  const CWalletTx &wtx = it->second;
2067  CTxOut utxo = wtx.tx->vout[txin.prevout.GetN()];
2068  // Update UTXOs from the wallet.
2069  input.utxo = utxo;
2070  }
2071  }
2072  }
2073 
2074  // Fill in information from ScriptPubKeyMans
2075  for (ScriptPubKeyMan *spk_man : GetAllScriptPubKeyMans()) {
2076  TransactionError res =
2077  spk_man->FillPSBT(psbtx, sighash_type, sign, bip32derivs);
2078  if (res != TransactionError::OK) {
2079  return res;
2080  }
2081  }
2082 
2083  // Complete if every input is now signed
2084  complete = true;
2085  for (const auto &input : psbtx.inputs) {
2086  complete &= PSBTInputSigned(input);
2087  }
2088 
2089  return TransactionError::OK;
2090 }
2091 
2092 SigningResult CWallet::SignMessage(const std::string &message,
2093  const PKHash &pkhash,
2094  std::string &str_sig) const {
2095  SignatureData sigdata;
2096  CScript script_pub_key = GetScriptForDestination(pkhash);
2097  for (const auto &spk_man_pair : m_spk_managers) {
2098  if (spk_man_pair.second->CanProvide(script_pub_key, sigdata)) {
2099  return spk_man_pair.second->SignMessage(message, pkhash, str_sig);
2100  }
2101  }
2103 }
2104 
2105 OutputType
2106 CWallet::TransactionChangeType(const std::optional<OutputType> &change_type,
2107  const std::vector<CRecipient> &vecSend) const {
2108  // If -changetype is specified, always use that change type.
2109  if (change_type) {
2110  return *change_type;
2111  }
2112 
2113  // if m_default_address_type is legacy, use legacy address as change.
2115  return OutputType::LEGACY;
2116  }
2117 
2118  // else use m_default_address_type for change
2119  return m_default_address_type;
2120 }
2121 
2123  CTransactionRef tx, mapValue_t mapValue,
2124  std::vector<std::pair<std::string, std::string>> orderForm,
2125  bool broadcast) {
2126  LOCK(cs_wallet);
2127 
2128  WalletLogPrintfToBeContinued("CommitTransaction:\n%s", tx->ToString());
2129 
2130  // Add tx to wallet, because if it has change it's also ours, otherwise just
2131  // for transaction history.
2132  AddToWallet(tx, {}, [&](CWalletTx &wtx, bool new_tx) {
2133  CHECK_NONFATAL(wtx.mapValue.empty());
2134  CHECK_NONFATAL(wtx.vOrderForm.empty());
2135  wtx.mapValue = std::move(mapValue);
2136  wtx.vOrderForm = std::move(orderForm);
2137  wtx.fTimeReceivedIsTxTime = true;
2138  wtx.fFromMe = true;
2139  return true;
2140  });
2141 
2142  // Notify that old coins are spent.
2143  for (const CTxIn &txin : tx->vin) {
2144  CWalletTx &coin = mapWallet.at(txin.prevout.GetTxId());
2145  coin.MarkDirty();
2146  NotifyTransactionChanged(this, coin.GetId(), CT_UPDATED);
2147  }
2148 
2149  // Get the inserted-CWalletTx from mapWallet so that the
2150  // fInMempool flag is cached properly
2151  CWalletTx &wtx = mapWallet.at(tx->GetId());
2152 
2153  if (!broadcast || !fBroadcastTransactions) {
2154  // Don't submit tx to the mempool if the flag is unset for this single
2155  // transaction, or if the wallet doesn't broadcast transactions at all.
2156  return;
2157  }
2158 
2159  std::string err_string;
2160  if (!SubmitTxMemoryPoolAndRelay(wtx, err_string, true)) {
2161  WalletLogPrintf("CommitTransaction(): Transaction cannot be broadcast "
2162  "immediately, %s\n",
2163  err_string);
2164  // TODO: if we expect the failure to be long term or permanent, instead
2165  // delete wtx from the wallet and return failure.
2166  }
2167 }
2168 
2169 DBErrors CWallet::LoadWallet(bool &fFirstRunRet) {
2170  LOCK(cs_wallet);
2171 
2172  fFirstRunRet = false;
2173  DBErrors nLoadWalletRet = WalletBatch(*database).LoadWallet(this);
2174  if (nLoadWalletRet == DBErrors::NEED_REWRITE) {
2175  if (database->Rewrite("\x04pool")) {
2176  for (const auto &spk_man_pair : m_spk_managers) {
2177  spk_man_pair.second->RewriteDB();
2178  }
2179  }
2180  }
2181 
2182  // This wallet is in its first run if there are no ScriptPubKeyMans and it
2183  // isn't blank or no privkeys
2184  fFirstRunRet = m_spk_managers.empty() &&
2187  if (fFirstRunRet) {
2190  }
2191 
2192  if (nLoadWalletRet != DBErrors::LOAD_OK) {
2193  return nLoadWalletRet;
2194  }
2195 
2196  return DBErrors::LOAD_OK;
2197 }
2198 
2199 DBErrors CWallet::ZapSelectTx(std::vector<TxId> &txIdsIn,
2200  std::vector<TxId> &txIdsOut) {
2202  DBErrors nZapSelectTxRet =
2203  WalletBatch(*database).ZapSelectTx(txIdsIn, txIdsOut);
2204  for (const TxId &txid : txIdsOut) {
2205  const auto &it = mapWallet.find(txid);
2206  wtxOrdered.erase(it->second.m_it_wtxOrdered);
2207  for (const auto &txin : it->second.tx->vin) {
2208  mapTxSpends.erase(txin.prevout);
2209  }
2210  mapWallet.erase(it);
2211  NotifyTransactionChanged(this, txid, CT_DELETED);
2212  }
2213 
2214  if (nZapSelectTxRet == DBErrors::NEED_REWRITE) {
2215  if (database->Rewrite("\x04pool")) {
2216  for (const auto &spk_man_pair : m_spk_managers) {
2217  spk_man_pair.second->RewriteDB();
2218  }
2219  }
2220  }
2221 
2222  if (nZapSelectTxRet != DBErrors::LOAD_OK) {
2223  return nZapSelectTxRet;
2224  }
2225 
2226  MarkDirty();
2227 
2228  return DBErrors::LOAD_OK;
2229 }
2230 
2232  const CTxDestination &address,
2233  const std::string &strName,
2234  const std::string &strPurpose) {
2235  bool fUpdated = false;
2236  bool is_mine;
2237  {
2238  LOCK(cs_wallet);
2239  std::map<CTxDestination, CAddressBookData>::iterator mi =
2240  m_address_book.find(address);
2241  fUpdated = (mi != m_address_book.end() && !mi->second.IsChange());
2242  m_address_book[address].SetLabel(strName);
2243  // Update purpose only if requested.
2244  if (!strPurpose.empty()) {
2245  m_address_book[address].purpose = strPurpose;
2246  }
2247  is_mine = IsMine(address) != ISMINE_NO;
2248  }
2249 
2250  NotifyAddressBookChanged(this, address, strName, is_mine, strPurpose,
2251  (fUpdated ? CT_UPDATED : CT_NEW));
2252  if (!strPurpose.empty() && !batch.WritePurpose(address, strPurpose)) {
2253  return false;
2254  }
2255  return batch.WriteName(address, strName);
2256 }
2257 
2259  const std::string &strName,
2260  const std::string &strPurpose) {
2261  WalletBatch batch(*database);
2262  return SetAddressBookWithDB(batch, address, strName, strPurpose);
2263 }
2264 
2266  bool is_mine;
2267  WalletBatch batch(*database);
2268  {
2269  LOCK(cs_wallet);
2270  // If we want to delete receiving addresses, we need to take care that
2271  // DestData "used" (and possibly newer DestData) gets preserved (and the
2272  // "deleted" address transformed into a change entry instead of actually
2273  // being deleted)
2274  // NOTE: This isn't a problem for sending addresses because they never
2275  // have any DestData yet! When adding new DestData, it should be
2276  // considered here whether to retain or delete it (or move it?).
2277  if (IsMine(address)) {
2279  "%s called with IsMine address, NOT SUPPORTED. Please "
2280  "report this bug! %s\n",
2281  __func__, PACKAGE_BUGREPORT);
2282  return false;
2283  }
2284  // Delete destdata tuples associated with address
2285  for (const std::pair<const std::string, std::string> &item :
2286  m_address_book[address].destdata) {
2287  batch.EraseDestData(address, item.first);
2288  }
2289  m_address_book.erase(address);
2290  is_mine = IsMine(address) != ISMINE_NO;
2291  }
2292 
2293  NotifyAddressBookChanged(this, address, "", is_mine, "", CT_DELETED);
2294 
2295  batch.ErasePurpose(address);
2296  return batch.EraseName(address);
2297 }
2298 
2301 
2302  unsigned int count = 0;
2303  for (auto spk_man : GetActiveScriptPubKeyMans()) {
2304  count += spk_man->KeypoolCountExternalKeys();
2305  }
2306 
2307  return count;
2308 }
2309 
2310 unsigned int CWallet::GetKeyPoolSize() const {
2312 
2313  unsigned int count = 0;
2314  for (auto spk_man : GetActiveScriptPubKeyMans()) {
2315  count += spk_man->GetKeyPoolSize();
2316  }
2317  return count;
2318 }
2319 
2320 bool CWallet::TopUpKeyPool(unsigned int kpSize) {
2321  LOCK(cs_wallet);
2322  bool res = true;
2323  for (auto spk_man : GetActiveScriptPubKeyMans()) {
2324  res &= spk_man->TopUp(kpSize);
2325  }
2326  return res;
2327 }
2328 
2329 bool CWallet::GetNewDestination(const OutputType type, const std::string label,
2330  CTxDestination &dest, std::string &error) {
2331  LOCK(cs_wallet);
2332  error.clear();
2333  bool result = false;
2334  auto spk_man = GetScriptPubKeyMan(type, false /* internal */);
2335  if (spk_man) {
2336  spk_man->TopUp();
2337  result = spk_man->GetNewDestination(type, dest, error);
2338  } else {
2339  error = strprintf("Error: No %s addresses available.",
2340  FormatOutputType(type));
2341  }
2342  if (result) {
2343  SetAddressBook(dest, label, "receive");
2344  }
2345 
2346  return result;
2347 }
2348 
2350  CTxDestination &dest,
2351  std::string &error) {
2352  LOCK(cs_wallet);
2353  error.clear();
2354 
2355  ReserveDestination reservedest(this, type);
2356  if (!reservedest.GetReservedDestination(dest, true)) {
2357  error = _("Error: Keypool ran out, please call keypoolrefill first")
2358  .translated;
2359  return false;
2360  }
2361 
2362  reservedest.KeepDestination();
2363  return true;
2364 }
2365 
2367  LOCK(cs_wallet);
2368  int64_t oldestKey = std::numeric_limits<int64_t>::max();
2369  for (const auto &spk_man_pair : m_spk_managers) {
2370  oldestKey =
2371  std::min(oldestKey, spk_man_pair.second->GetOldestKeyPoolTime());
2372  }
2373  return oldestKey;
2374 }
2375 
2377  const std::set<CTxDestination> &destinations) {
2378  for (auto &entry : mapWallet) {
2379  CWalletTx &wtx = entry.second;
2380  if (wtx.m_is_cache_empty) {
2381  continue;
2382  }
2383 
2384  for (size_t i = 0; i < wtx.tx->vout.size(); i++) {
2385  CTxDestination dst;
2386 
2387  if (ExtractDestination(wtx.tx->vout[i].scriptPubKey, dst) &&
2388  destinations.count(dst)) {
2389  wtx.MarkDirty();
2390  break;
2391  }
2392  }
2393  }
2394 }
2395 
2396 std::set<CTxDestination>
2397 CWallet::GetLabelAddresses(const std::string &label) const {
2399  std::set<CTxDestination> result;
2400  for (const std::pair<const CTxDestination, CAddressBookData> &item :
2401  m_address_book) {
2402  if (item.second.IsChange()) {
2403  continue;
2404  }
2405  const CTxDestination &address = item.first;
2406  const std::string &strName = item.second.GetLabel();
2407  if (strName == label) {
2408  result.insert(address);
2409  }
2410  }
2411 
2412  return result;
2413 }
2414 
2416  bool internal) {
2417  m_spk_man = pwallet->GetScriptPubKeyMan(type, internal);
2418  if (!m_spk_man) {
2419  return false;
2420  }
2421 
2422  if (nIndex == -1) {
2423  m_spk_man->TopUp();
2424 
2425  CKeyPool keypool;
2427  keypool)) {
2428  return false;
2429  }
2430  fInternal = keypool.fInternal;
2431  }
2432  dest = address;
2433  return true;
2434 }
2435 
2437  if (nIndex != -1) {
2439  }
2440 
2441  nIndex = -1;
2442  address = CNoDestination();
2443 }
2444 
2446  if (nIndex != -1) {
2448  }
2449  nIndex = -1;
2450  address = CNoDestination();
2451 }
2452 
2453 void CWallet::LockCoin(const COutPoint &output) {
2455  setLockedCoins.insert(output);
2456 }
2457 
2458 void CWallet::UnlockCoin(const COutPoint &output) {
2460  setLockedCoins.erase(output);
2461 }
2462 
2465  setLockedCoins.clear();
2466 }
2467 
2468 bool CWallet::IsLockedCoin(const COutPoint &outpoint) const {
2470 
2471  return setLockedCoins.count(outpoint) > 0;
2472 }
2473 
2474 void CWallet::ListLockedCoins(std::vector<COutPoint> &vOutpts) const {
2476  for (COutPoint outpoint : setLockedCoins) {
2477  vOutpts.push_back(outpoint);
2478  }
2479 }
2480  // end of Actions
2482 
2483 void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const {
2485  mapKeyBirth.clear();
2486 
2487  // map in which we'll infer heights of other keys
2488  std::map<CKeyID, const CWalletTx::Confirmation *> mapKeyFirstBlock;
2489  CWalletTx::Confirmation max_confirm;
2490  // the tip can be reorganized; use a 144-block safety margin
2491  max_confirm.block_height =
2492  GetLastBlockHeight() > 144 ? GetLastBlockHeight() - 144 : 0;
2493  CHECK_NONFATAL(chain().findAncestorByHeight(
2494  GetLastBlockHash(), max_confirm.block_height,
2495  FoundBlock().hash(max_confirm.hashBlock)));
2496 
2497  {
2499  assert(spk_man != nullptr);
2500  LOCK(spk_man->cs_KeyStore);
2501 
2502  // Get birth times for keys with metadata.
2503  for (const auto &entry : spk_man->mapKeyMetadata) {
2504  if (entry.second.nCreateTime) {
2505  mapKeyBirth[entry.first] = entry.second.nCreateTime;
2506  }
2507  }
2508 
2509  // Prepare to infer birth heights for keys without metadata.
2510  for (const CKeyID &keyid : spk_man->GetKeys()) {
2511  if (mapKeyBirth.count(keyid) == 0) {
2512  mapKeyFirstBlock[keyid] = &max_confirm;
2513  }
2514  }
2515 
2516  // If there are no such keys, we're done.
2517  if (mapKeyFirstBlock.empty()) {
2518  return;
2519  }
2520 
2521  // Find first block that affects those keys, if there are any left.
2522  for (const auto &entry : mapWallet) {
2523  // iterate over all wallet transactions...
2524  const CWalletTx &wtx = entry.second;
2525  if (wtx.m_confirm.status == CWalletTx::CONFIRMED) {
2526  // ... which are already in a block
2527  for (const CTxOut &txout : wtx.tx->vout) {
2528  // Iterate over all their outputs...
2529  for (const auto &keyid :
2530  GetAffectedKeys(txout.scriptPubKey, *spk_man)) {
2531  // ... and all their affected keys.
2532  auto rit = mapKeyFirstBlock.find(keyid);
2533  if (rit != mapKeyFirstBlock.end() &&
2534  wtx.m_confirm.block_height <
2535  rit->second->block_height) {
2536  rit->second = &wtx.m_confirm;
2537  }
2538  }
2539  }
2540  }
2541  }
2542  }
2543 
2544  // Extract block timestamps for those keys.
2545  for (const auto &entry : mapKeyFirstBlock) {
2546  int64_t block_time;
2547  CHECK_NONFATAL(chain().findBlock(entry.second->hashBlock,
2548  FoundBlock().time(block_time)));
2549  // block times can be 2h off
2550  mapKeyBirth[entry.first] = block_time - TIMESTAMP_WINDOW;
2551  }
2552 }
2553 
2575 unsigned int CWallet::ComputeTimeSmart(const CWalletTx &wtx) const {
2576  unsigned int nTimeSmart = wtx.nTimeReceived;
2577  if (!wtx.isUnconfirmed() && !wtx.isAbandoned()) {
2578  int64_t blocktime;
2579  if (chain().findBlock(wtx.m_confirm.hashBlock,
2580  FoundBlock().time(blocktime))) {
2581  int64_t latestNow = wtx.nTimeReceived;
2582  int64_t latestEntry = 0;
2583 
2584  // Tolerate times up to the last timestamp in the wallet not more
2585  // than 5 minutes into the future
2586  int64_t latestTolerated = latestNow + 300;
2587  const TxItems &txOrdered = wtxOrdered;
2588  for (auto it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) {
2589  CWalletTx *const pwtx = it->second;
2590  if (pwtx == &wtx) {
2591  continue;
2592  }
2593  int64_t nSmartTime;
2594  nSmartTime = pwtx->nTimeSmart;
2595  if (!nSmartTime) {
2596  nSmartTime = pwtx->nTimeReceived;
2597  }
2598  if (nSmartTime <= latestTolerated) {
2599  latestEntry = nSmartTime;
2600  if (nSmartTime > latestNow) {
2601  latestNow = nSmartTime;
2602  }
2603  break;
2604  }
2605  }
2606 
2607  nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
2608  } else {
2609  WalletLogPrintf("%s: found %s in block %s not in index\n", __func__,
2610  wtx.GetId().ToString(),
2611  wtx.m_confirm.hashBlock.ToString());
2612  }
2613  }
2614  return nTimeSmart;
2615 }
2616 
2618  const std::string &key, const std::string &value) {
2619  if (std::get_if<CNoDestination>(&dest)) {
2620  return false;
2621  }
2622 
2623  m_address_book[dest].destdata.insert(std::make_pair(key, value));
2624  return batch.WriteDestData(dest, key, value);
2625 }
2626 
2628  const std::string &key) {
2629  if (!m_address_book[dest].destdata.erase(key)) {
2630  return false;
2631  }
2632 
2633  return batch.EraseDestData(dest, key);
2634 }
2635 
2636 void CWallet::LoadDestData(const CTxDestination &dest, const std::string &key,
2637  const std::string &value) {
2638  m_address_book[dest].destdata.insert(std::make_pair(key, value));
2639 }
2640 
2641 bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key,
2642  std::string *value) const {
2643  std::map<CTxDestination, CAddressBookData>::const_iterator i =
2644  m_address_book.find(dest);
2645  if (i != m_address_book.end()) {
2646  CAddressBookData::StringMap::const_iterator j =
2647  i->second.destdata.find(key);
2648  if (j != i->second.destdata.end()) {
2649  if (value) {
2650  *value = j->second;
2651  }
2652 
2653  return true;
2654  }
2655  }
2656  return false;
2657 }
2658 
2659 std::vector<std::string>
2660 CWallet::GetDestValues(const std::string &prefix) const {
2661  std::vector<std::string> values;
2662  for (const auto &address : m_address_book) {
2663  for (const auto &data : address.second.destdata) {
2664  if (!data.first.compare(0, prefix.size(), prefix)) {
2665  values.emplace_back(data.second);
2666  }
2667  }
2668  }
2669  return values;
2670 }
2671 
2672 std::unique_ptr<WalletDatabase>
2673 MakeWalletDatabase(const std::string &name, const DatabaseOptions &options,
2674  DatabaseStatus &status, bilingual_str &error_string) {
2675  // Do some checking on wallet path. It should be either a:
2676  //
2677  // 1. Path where a directory can be created.
2678  // 2. Path to an existing directory.
2679  // 3. Path to a symlink to a directory.
2680  // 4. For backwards compatibility, the name of a data file in -walletdir.
2681  const fs::path wallet_path =
2683  fs::file_type path_type = fs::symlink_status(wallet_path).type();
2684  if (!(path_type == fs::file_type::not_found ||
2685  path_type == fs::file_type::directory ||
2686  (path_type == fs::file_type::symlink &&
2687  fs::is_directory(wallet_path)) ||
2688  (path_type == fs::file_type::regular &&
2689  fs::PathFromString(name).filename() == fs::PathFromString(name)))) {
2690  error_string = Untranslated(
2691  strprintf("Invalid -wallet path '%s'. -wallet path should point to "
2692  "a directory where wallet.dat and "
2693  "database/log.?????????? files can be stored, a location "
2694  "where such a directory could be created, "
2695  "or (for backwards compatibility) the name of an "
2696  "existing data file in -walletdir (%s)",
2699  return nullptr;
2700  }
2701  return MakeDatabase(wallet_path, options, status, error_string);
2702 }
2703 
2704 std::shared_ptr<CWallet>
2705 CWallet::Create(interfaces::Chain &chain, const std::string &name,
2706  std::unique_ptr<WalletDatabase> database,
2707  uint64_t wallet_creation_flags, bilingual_str &error,
2708  std::vector<bilingual_str> &warnings) {
2709  const std::string &walletFile = database->Filename();
2710 
2711  chain.initMessage(_("Loading wallet...").translated);
2712 
2713  int64_t nStart = GetTimeMillis();
2714  bool fFirstRun = true;
2715  // TODO: Can't use std::make_shared because we need a custom deleter but
2716  // should be possible to use std::allocate_shared.
2717  std::shared_ptr<CWallet> walletInstance(
2718  new CWallet(&chain, name, std::move(database)), ReleaseWallet);
2719  DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
2720  if (nLoadWalletRet != DBErrors::LOAD_OK) {
2721  if (nLoadWalletRet == DBErrors::CORRUPT) {
2722  error =
2723  strprintf(_("Error loading %s: Wallet corrupted"), walletFile);
2724  return nullptr;
2725  }
2726 
2727  if (nLoadWalletRet == DBErrors::NONCRITICAL_ERROR) {
2728  warnings.push_back(
2729  strprintf(_("Error reading %s! All keys read correctly, but "
2730  "transaction data or address book entries might be "
2731  "missing or incorrect."),
2732  walletFile));
2733  } else if (nLoadWalletRet == DBErrors::TOO_NEW) {
2734  error = strprintf(
2735  _("Error loading %s: Wallet requires newer version of %s"),
2736  walletFile, PACKAGE_NAME);
2737  return nullptr;
2738  } else if (nLoadWalletRet == DBErrors::NEED_REWRITE) {
2739  error = strprintf(
2740  _("Wallet needed to be rewritten: restart %s to complete"),
2741  PACKAGE_NAME);
2742  return nullptr;
2743  } else {
2744  error = strprintf(_("Error loading %s"), walletFile);
2745  return nullptr;
2746  }
2747  }
2748 
2749  if (fFirstRun) {
2750  // Ensure this wallet.dat can only be opened by clients supporting
2751  // HD with chain split and expects no default key.
2752  walletInstance->SetMinVersion(FEATURE_LATEST);
2753 
2754  walletInstance->AddWalletFlags(wallet_creation_flags);
2755 
2756  // Only create LegacyScriptPubKeyMan when not descriptor wallet
2757  if (!walletInstance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
2758  walletInstance->SetupLegacyScriptPubKeyMan();
2759  }
2760 
2761  if (!(wallet_creation_flags &
2763  LOCK(walletInstance->cs_wallet);
2764  if (walletInstance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
2765  walletInstance->SetupDescriptorScriptPubKeyMans();
2766  // SetupDescriptorScriptPubKeyMans already calls SetupGeneration
2767  // for us so we don't need to call SetupGeneration separately
2768  } else {
2769  // Legacy wallets need SetupGeneration here.
2770  for (auto spk_man :
2771  walletInstance->GetActiveScriptPubKeyMans()) {
2772  if (!spk_man->SetupGeneration()) {
2773  error = _("Unable to generate initial keys");
2774  return nullptr;
2775  }
2776  }
2777  }
2778  }
2779 
2780  walletInstance->chainStateFlushed(chain.getTipLocator());
2781  } else if (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS) {
2782  // Make it impossible to disable private keys after creation
2783  error = strprintf(_("Error loading %s: Private keys can only be "
2784  "disabled during creation"),
2785  walletFile);
2786  return nullptr;
2787  } else if (walletInstance->IsWalletFlagSet(
2789  for (auto spk_man : walletInstance->GetActiveScriptPubKeyMans()) {
2790  if (spk_man->HavePrivateKeys()) {
2791  warnings.push_back(
2792  strprintf(_("Warning: Private keys detected in wallet {%s} "
2793  "with disabled private keys"),
2794  walletFile));
2795  }
2796  }
2797  }
2798 
2799  if (gArgs.IsArgSet("-mintxfee")) {
2800  Amount n = Amount::zero();
2801  if (!ParseMoney(gArgs.GetArg("-mintxfee", ""), n) ||
2802  n == Amount::zero()) {
2803  error = AmountErrMsg("mintxfee", gArgs.GetArg("-mintxfee", ""));
2804  return nullptr;
2805  }
2806  if (n > HIGH_TX_FEE_PER_KB) {
2807  warnings.push_back(AmountHighWarn("-mintxfee") + Untranslated(" ") +
2808  _("This is the minimum transaction fee you pay "
2809  "on every transaction."));
2810  }
2811  walletInstance->m_min_fee = CFeeRate(n);
2812  }
2813 
2814  if (gArgs.IsArgSet("-maxapsfee")) {
2815  const std::string max_aps_fee{gArgs.GetArg("-maxapsfee", "")};
2816  Amount n = Amount::zero();
2817  if (max_aps_fee == "-1") {
2818  n = -1 * SATOSHI;
2819  } else if (!ParseMoney(max_aps_fee, n)) {
2820  error = AmountErrMsg("maxapsfee", max_aps_fee);
2821  return nullptr;
2822  }
2823  if (n > HIGH_APS_FEE) {
2824  warnings.push_back(
2825  AmountHighWarn("-maxapsfee") + Untranslated(" ") +
2826  _("This is the maximum transaction fee you pay (in addition to"
2827  " the normal fee) to prioritize partial spend avoidance over"
2828  " regular coin selection."));
2829  }
2830  walletInstance->m_max_aps_fee = n;
2831  }
2832 
2833  if (gArgs.IsArgSet("-fallbackfee")) {
2834  Amount nFeePerK = Amount::zero();
2835  if (!ParseMoney(gArgs.GetArg("-fallbackfee", ""), nFeePerK)) {
2836  error =
2837  strprintf(_("Invalid amount for -fallbackfee=<amount>: '%s'"),
2838  gArgs.GetArg("-fallbackfee", ""));
2839  return nullptr;
2840  }
2841  if (nFeePerK > HIGH_TX_FEE_PER_KB) {
2842  warnings.push_back(AmountHighWarn("-fallbackfee") +
2843  Untranslated(" ") +
2844  _("This is the transaction fee you may pay when "
2845  "fee estimates are not available."));
2846  }
2847  walletInstance->m_fallback_fee = CFeeRate(nFeePerK);
2848  }
2849  // Disable fallback fee in case value was set to 0, enable if non-null value
2850  walletInstance->m_allow_fallback_fee =
2851  walletInstance->m_fallback_fee.GetFeePerK() != Amount::zero();
2852 
2853  if (gArgs.IsArgSet("-paytxfee")) {
2854  Amount nFeePerK = Amount::zero();
2855  if (!ParseMoney(gArgs.GetArg("-paytxfee", ""), nFeePerK)) {
2856  error = AmountErrMsg("paytxfee", gArgs.GetArg("-paytxfee", ""));
2857  return nullptr;
2858  }
2859  if (nFeePerK > HIGH_TX_FEE_PER_KB) {
2860  warnings.push_back(AmountHighWarn("-paytxfee") + Untranslated(" ") +
2861  _("This is the transaction fee you will pay if "
2862  "you send a transaction."));
2863  }
2864  walletInstance->m_pay_tx_fee = CFeeRate(nFeePerK, 1000);
2865  if (walletInstance->m_pay_tx_fee < chain.relayMinFee()) {
2866  error = strprintf(_("Invalid amount for -paytxfee=<amount>: '%s' "
2867  "(must be at least %s)"),
2868  gArgs.GetArg("-paytxfee", ""),
2869  chain.relayMinFee().ToString());
2870  return nullptr;
2871  }
2872  }
2873 
2874  if (gArgs.IsArgSet("-maxtxfee")) {
2875  Amount nMaxFee = Amount::zero();
2876  if (!ParseMoney(gArgs.GetArg("-maxtxfee", ""), nMaxFee)) {
2877  error = AmountErrMsg("maxtxfee", gArgs.GetArg("-maxtxfee", ""));
2878  return nullptr;
2879  }
2880  if (nMaxFee > HIGH_MAX_TX_FEE) {
2881  warnings.push_back(_("-maxtxfee is set very high! Fees this large "
2882  "could be paid on a single transaction."));
2883  }
2884  if (CFeeRate(nMaxFee, 1000) < chain.relayMinFee()) {
2885  error = strprintf(
2886  _("Invalid amount for -maxtxfee=<amount>: '%s' (must be at "
2887  "least the minrelay fee of %s to prevent stuck "
2888  "transactions)"),
2889  gArgs.GetArg("-maxtxfee", ""), chain.relayMinFee().ToString());
2890  return nullptr;
2891  }
2892  walletInstance->m_default_max_tx_fee = nMaxFee;
2893  }
2894 
2896  warnings.push_back(
2897  AmountHighWarn("-minrelaytxfee") + Untranslated(" ") +
2898  _("The wallet will avoid paying less than the minimum relay fee."));
2899  }
2900 
2901  walletInstance->m_spend_zero_conf_change =
2902  gArgs.GetBoolArg("-spendzeroconfchange", DEFAULT_SPEND_ZEROCONF_CHANGE);
2903 
2904  walletInstance->m_default_address_type = DEFAULT_ADDRESS_TYPE;
2905 
2906  walletInstance->WalletLogPrintf("Wallet completed loading in %15dms\n",
2907  GetTimeMillis() - nStart);
2908 
2909  // Try to top up keypool. No-op if the wallet is locked.
2910  walletInstance->TopUpKeyPool();
2911 
2912  LOCK(walletInstance->cs_wallet);
2913 
2914  // Register wallet with validationinterface. It's done before rescan to
2915  // avoid missing block connections between end of rescan and validation
2916  // subscribing. Because of wallet lock being hold, block connection
2917  // notifications are going to be pending on the validation-side until lock
2918  // release. It's likely to have block processing duplicata (if rescan block
2919  // range overlaps with notification one) but we guarantee at least than
2920  // wallet state is correct after notifications delivery. This is temporary
2921  // until rescan and notifications delivery are unified under same interface.
2922  walletInstance->m_chain_notifications_handler =
2923  walletInstance->chain().handleNotifications(walletInstance);
2924 
2925  int rescan_height = 0;
2926  if (!gArgs.GetBoolArg("-rescan", false)) {
2927  WalletBatch batch(*walletInstance->database);
2928  CBlockLocator locator;
2929  if (batch.ReadBestBlock(locator)) {
2930  if (const std::optional<int> fork_height =
2931  chain.findLocatorFork(locator)) {
2932  rescan_height = *fork_height;
2933  }
2934  }
2935  }
2936 
2937  const std::optional<int> tip_height = chain.getHeight();
2938  if (tip_height) {
2939  walletInstance->m_last_block_processed =
2940  chain.getBlockHash(*tip_height);
2941  walletInstance->m_last_block_processed_height = *tip_height;
2942  } else {
2943  walletInstance->m_last_block_processed.SetNull();
2944  walletInstance->m_last_block_processed_height = -1;
2945  }
2946 
2947  if (tip_height && *tip_height != rescan_height) {
2948  // We can't rescan beyond non-pruned blocks, stop and throw an error.
2949  // This might happen if a user uses an old wallet within a pruned node
2950  // or if they ran -disablewallet for a longer time, then decided to
2951  // re-enable
2952  if (chain.havePruned()) {
2953  // Exit early and print an error.
2954  // If a block is pruned after this check, we will load the wallet,
2955  // but fail the rescan with a generic error.
2956  int block_height = *tip_height;
2957  while (block_height > 0 &&
2958  chain.haveBlockOnDisk(block_height - 1) &&
2959  rescan_height != block_height) {
2960  --block_height;
2961  }
2962 
2963  if (rescan_height != block_height) {
2964  error = _("Prune: last wallet synchronisation goes beyond "
2965  "pruned data. You need to -reindex (download the "
2966  "whole blockchain again in case of pruned node)");
2967  return nullptr;
2968  }
2969  }
2970 
2971  chain.initMessage(_("Rescanning...").translated);
2972  walletInstance->WalletLogPrintf(
2973  "Rescanning last %i blocks (from block %i)...\n",
2974  *tip_height - rescan_height, rescan_height);
2975 
2976  // No need to read and scan block if block was created before our wallet
2977  // birthday (as adjusted for block time variability)
2978  std::optional<int64_t> time_first_key;
2979  for (auto spk_man : walletInstance->GetAllScriptPubKeyMans()) {
2980  int64_t time = spk_man->GetTimeFirstKey();
2981  if (!time_first_key || time < *time_first_key) {
2982  time_first_key = time;
2983  }
2984  }
2985  if (time_first_key) {
2987  *time_first_key - TIMESTAMP_WINDOW, rescan_height,
2988  FoundBlock().height(rescan_height));
2989  }
2990 
2991  {
2992  WalletRescanReserver reserver(*walletInstance);
2993  if (!reserver.reserve() ||
2995  walletInstance
2996  ->ScanForWalletTransactions(
2997  chain.getBlockHash(rescan_height), rescan_height,
2998  {} /* max height */, reserver, true /* update */)
2999  .status)) {
3000  error = _("Failed to rescan the wallet during initialization");
3001  return nullptr;
3002  }
3003  }
3004  walletInstance->chainStateFlushed(chain.getTipLocator());
3005  walletInstance->database->IncrementUpdateCounter();
3006  }
3007 
3008  {
3009  LOCK(cs_wallets);
3010  for (auto &load_wallet : g_load_wallet_fns) {
3011  load_wallet(interfaces::MakeWallet(walletInstance));
3012  }
3013  }
3014 
3015  walletInstance->SetBroadcastTransactions(
3016  gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
3017 
3018  walletInstance->WalletLogPrintf("setKeyPool.size() = %u\n",
3019  walletInstance->GetKeyPoolSize());
3020  walletInstance->WalletLogPrintf("mapWallet.size() = %u\n",
3021  walletInstance->mapWallet.size());
3022  walletInstance->WalletLogPrintf("m_address_book.size() = %u\n",
3023  walletInstance->m_address_book.size());
3024 
3025  return walletInstance;
3026 }
3027 
3028 const CAddressBookData *
3030  bool allow_change) const {
3031  const auto &address_book_it = m_address_book.find(dest);
3032  if (address_book_it == m_address_book.end()) {
3033  return nullptr;
3034  }
3035  if ((!allow_change) && address_book_it->second.IsChange()) {
3036  return nullptr;
3037  }
3038  return &address_book_it->second;
3039 }
3040 
3042  int prev_version = GetVersion();
3043  int nMaxVersion = version;
3044  // The -upgradewallet without argument case
3045  if (nMaxVersion == 0) {
3046  WalletLogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
3047  nMaxVersion = FEATURE_LATEST;
3048  // permanently upgrade the wallet immediately
3050  } else {
3051  WalletLogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
3052  }
3053 
3054  if (nMaxVersion < GetVersion()) {
3055  error = _("Cannot downgrade wallet");
3056  return false;
3057  }
3058 
3059  SetMaxVersion(nMaxVersion);
3060 
3061  LOCK(cs_wallet);
3062 
3063  // Do not upgrade versions to any version between HD_SPLIT and
3064  // FEATURE_PRE_SPLIT_KEYPOOL unless already supporting HD_SPLIT
3065  int max_version = GetVersion();
3067  max_version >= FEATURE_HD_SPLIT &&
3068  max_version < FEATURE_PRE_SPLIT_KEYPOOL) {
3069  error = _("Cannot upgrade a non HD split wallet without upgrading to "
3070  "support pre split keypool. Please use version 200300 or no "
3071  "version specified.");
3072  return false;
3073  }
3074 
3075  for (auto spk_man : GetActiveScriptPubKeyMans()) {
3076  if (!spk_man->Upgrade(prev_version, error)) {
3077  return false;
3078  }
3079  }
3080 
3081  return true;
3082 }
3083 
3085  LOCK(cs_wallet);
3086 
3087  // Add wallet transactions that aren't already in a block to mempool.
3088  // Do this here as mempool requires genesis block to be loaded.
3090 
3091  // Update wallet transactions with current mempool transactions.
3093 }
3094 
3095 bool CWallet::BackupWallet(const std::string &strDest) const {
3096  return database->Backup(strDest);
3097 }
3098 
3100  nTime = GetTime();
3101  fInternal = false;
3102  m_pre_split = false;
3103 }
3104 
3105 CKeyPool::CKeyPool(const CPubKey &vchPubKeyIn, bool internalIn) {
3106  nTime = GetTime();
3107  vchPubKey = vchPubKeyIn;
3108  fInternal = internalIn;
3109  m_pre_split = false;
3110 }
3111 
3114  if (wtx.isUnconfirmed() || wtx.isAbandoned()) {
3115  return 0;
3116  }
3117 
3118  return (GetLastBlockHeight() - wtx.m_confirm.block_height + 1) *
3119  (wtx.isConflicted() ? -1 : 1);
3120 }
3121 
3124 
3125  if (!wtx.IsCoinBase()) {
3126  return 0;
3127  }
3128  int chain_depth = GetTxDepthInMainChain(wtx);
3129  // coinbase tx should not be conflicted
3130  assert(chain_depth >= 0);
3131  return std::max(0, (COINBASE_MATURITY + 1) - chain_depth);
3132 }
3133 
3136 
3137  // note GetBlocksToMaturity is 0 for non-coinbase tx
3138  return GetTxBlocksToMaturity(wtx) > 0;
3139 }
3140 
3141 bool CWallet::IsCrypted() const {
3142  return HasEncryptionKeys();
3143 }
3144 
3145 bool CWallet::IsLocked() const {
3146  if (!IsCrypted()) {
3147  return false;
3148  }
3149  LOCK(cs_wallet);
3150  return vMasterKey.empty();
3151 }
3152 
3154  if (!IsCrypted()) {
3155  return false;
3156  }
3157 
3158  {
3159  LOCK(cs_wallet);
3160  if (!vMasterKey.empty()) {
3161  memory_cleanse(vMasterKey.data(),
3162  vMasterKey.size() *
3163  sizeof(decltype(vMasterKey)::value_type));
3164  vMasterKey.clear();
3165  }
3166  }
3167 
3168  NotifyStatusChanged(this);
3169  return true;
3170 }
3171 
3172 bool CWallet::Unlock(const CKeyingMaterial &vMasterKeyIn, bool accept_no_keys) {
3173  {
3174  LOCK(cs_wallet);
3175  for (const auto &spk_man_pair : m_spk_managers) {
3176  if (!spk_man_pair.second->CheckDecryptionKey(vMasterKeyIn,
3177  accept_no_keys)) {
3178  return false;
3179  }
3180  }
3181  vMasterKey = vMasterKeyIn;
3182  }
3183  NotifyStatusChanged(this);
3184  return true;
3185 }
3186 
3187 std::set<ScriptPubKeyMan *> CWallet::GetActiveScriptPubKeyMans() const {
3188  std::set<ScriptPubKeyMan *> spk_mans;
3189  for (bool internal : {false, true}) {
3190  for (OutputType t : OUTPUT_TYPES) {
3191  auto spk_man = GetScriptPubKeyMan(t, internal);
3192  if (spk_man) {
3193  spk_mans.insert(spk_man);
3194  }
3195  }
3196  }
3197  return spk_mans;
3198 }
3199 
3200 std::set<ScriptPubKeyMan *> CWallet::GetAllScriptPubKeyMans() const {
3201  std::set<ScriptPubKeyMan *> spk_mans;
3202  for (const auto &spk_man_pair : m_spk_managers) {
3203  spk_mans.insert(spk_man_pair.second.get());
3204  }
3205  return spk_mans;
3206 }
3207 
3209  bool internal) const {
3210  const std::map<OutputType, ScriptPubKeyMan *> &spk_managers =
3212  std::map<OutputType, ScriptPubKeyMan *>::const_iterator it =
3213  spk_managers.find(type);
3214  if (it == spk_managers.end()) {
3216  "%s scriptPubKey Manager for output type %d does not exist\n",
3217  internal ? "Internal" : "External", static_cast<int>(type));
3218  return nullptr;
3219  }
3220  return it->second;
3221 }
3222 
3223 std::set<ScriptPubKeyMan *>
3225  SignatureData &sigdata) const {
3226  std::set<ScriptPubKeyMan *> spk_mans;
3227  for (const auto &spk_man_pair : m_spk_managers) {
3228  if (spk_man_pair.second->CanProvide(script, sigdata)) {
3229  spk_mans.insert(spk_man_pair.second.get());
3230  }
3231  }
3232  return spk_mans;
3233 }
3234 
3236  SignatureData sigdata;
3237  for (const auto &spk_man_pair : m_spk_managers) {
3238  if (spk_man_pair.second->CanProvide(script, sigdata)) {
3239  return spk_man_pair.second.get();
3240  }
3241  }
3242  return nullptr;
3243 }
3244 
3246  if (m_spk_managers.count(id) > 0) {
3247  return m_spk_managers.at(id).get();
3248  }
3249  return nullptr;
3250 }
3251 
3252 std::unique_ptr<SigningProvider>
3253 CWallet::GetSolvingProvider(const CScript &script) const {
3254  SignatureData sigdata;
3255  return GetSolvingProvider(script, sigdata);
3256 }
3257 
3258 std::unique_ptr<SigningProvider>
3260  SignatureData &sigdata) const {
3261  for (const auto &spk_man_pair : m_spk_managers) {
3262  if (spk_man_pair.second->CanProvide(script, sigdata)) {
3263  return spk_man_pair.second->GetSolvingProvider(script);
3264  }
3265  }
3266  return nullptr;
3267 }
3268 
3271  return nullptr;
3272  }
3273  // Legacy wallets only have one ScriptPubKeyMan which is a
3274  // LegacyScriptPubKeyMan. Everything in m_internal_spk_managers and
3275  // m_external_spk_managers point to the same legacyScriptPubKeyMan.
3277  if (it == m_internal_spk_managers.end()) {
3278  return nullptr;
3279  }
3280  return dynamic_cast<LegacyScriptPubKeyMan *>(it->second);
3281 }
3282 
3285  return GetLegacyScriptPubKeyMan();
3286 }
3287 
3289  if (!m_internal_spk_managers.empty() || !m_external_spk_managers.empty() ||
3291  return;
3292  }
3293 
3294  auto spk_manager =
3295  std::unique_ptr<ScriptPubKeyMan>(new LegacyScriptPubKeyMan(*this));
3296  for (const auto &type : OUTPUT_TYPES) {
3297  m_internal_spk_managers[type] = spk_manager.get();
3298  m_external_spk_managers[type] = spk_manager.get();
3299  }
3300  m_spk_managers[spk_manager->GetID()] = std::move(spk_manager);
3301 }
3302 
3304  return vMasterKey;
3305 }
3306 
3308  return !mapMasterKeys.empty();
3309 }
3310 
3312  for (const auto &spk_man : GetActiveScriptPubKeyMans()) {
3313  spk_man->NotifyWatchonlyChanged.connect(NotifyWatchonlyChanged);
3314  spk_man->NotifyCanGetAddressesChanged.connect(
3316  }
3317 }
3318 
3320  WalletDescriptor &desc) {
3321  auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(
3322  new DescriptorScriptPubKeyMan(*this, desc));
3323  m_spk_managers[id] = std::move(spk_manager);
3324 }
3325 
3328 
3329  // Make a seed
3330  CKey seed_key;
3331  seed_key.MakeNewKey(true);
3332  CPubKey seed = seed_key.GetPubKey();
3333  assert(seed_key.VerifyPubKey(seed));
3334 
3335  // Get the extended key
3336  CExtKey master_key;
3337  master_key.SetSeed(seed_key.begin(), seed_key.size());
3338 
3339  for (bool internal : {false, true}) {
3340  for (OutputType t : OUTPUT_TYPES) {
3341  auto spk_manager =
3342  std::make_unique<DescriptorScriptPubKeyMan>(*this, internal);
3343  if (IsCrypted()) {
3344  if (IsLocked()) {
3345  throw std::runtime_error(
3346  std::string(__func__) +
3347  ": Wallet is locked, cannot setup new descriptors");
3348  }
3349  if (!spk_manager->CheckDecryptionKey(vMasterKey) &&
3350  !spk_manager->Encrypt(vMasterKey, nullptr)) {
3351  throw std::runtime_error(
3352  std::string(__func__) +
3353  ": Could not encrypt new descriptors");
3354  }
3355  }
3356  spk_manager->SetupDescriptorGeneration(master_key, t);
3357  uint256 id = spk_manager->GetID();
3358  m_spk_managers[id] = std::move(spk_manager);
3359  AddActiveScriptPubKeyMan(id, t, internal);
3360  }
3361  }
3362 }
3363 
3365  bool internal) {
3366  WalletBatch batch(*database);
3367  if (!batch.WriteActiveScriptPubKeyMan(static_cast<uint8_t>(type), id,
3368  internal)) {
3369  throw std::runtime_error(std::string(__func__) +
3370  ": writing active ScriptPubKeyMan id failed");
3371  }
3372  LoadActiveScriptPubKeyMan(id, type, internal);
3373 }
3374 
3376  bool internal) {
3377  // Activating ScriptPubKeyManager for a given output and change type is
3378  // incompatible with legacy wallets.
3379  // Legacy wallets have only one ScriptPubKeyManager and it's active for all
3380  // output and change types.
3382 
3384  "Setting spkMan to active: id = %s, type = %d, internal = %d\n",
3385  id.ToString(), static_cast<int>(type), static_cast<int>(internal));
3386  auto &spk_mans =
3388  auto &spk_mans_other =
3390  auto spk_man = m_spk_managers.at(id).get();
3391  spk_man->SetInternal(internal);
3392  spk_mans[type] = spk_man;
3393 
3394  const auto it = spk_mans_other.find(type);
3395  if (it != spk_mans_other.end() && it->second == spk_man) {
3396  spk_mans_other.erase(type);
3397  }
3398 
3400 }
3401 
3403  bool internal) {
3404  auto spk_man = GetScriptPubKeyMan(type, internal);
3405  if (spk_man != nullptr && spk_man->GetID() == id) {
3407  "Deactivate spkMan: id = %s, type = %d, internal = %d\n",
3408  id.ToString(), static_cast<int>(type), static_cast<int>(internal));
3409  WalletBatch batch(GetDatabase());
3410  if (!batch.EraseActiveScriptPubKeyMan(static_cast<uint8_t>(type),
3411  internal)) {
3412  throw std::runtime_error(
3413  std::string(__func__) +
3414  ": erasing active ScriptPubKeyMan id failed");
3415  }
3416 
3417  auto &spk_mans =
3419  spk_mans.erase(type);
3420  }
3421 
3423 }
3424 
3425 bool CWallet::IsLegacy() const {
3426  if (m_internal_spk_managers.count(OutputType::LEGACY) == 0) {
3427  return false;
3428  }
3429  auto spk_man = dynamic_cast<LegacyScriptPubKeyMan *>(
3431  return spk_man != nullptr;
3432 }
3433 
3436  for (auto &spk_man_pair : m_spk_managers) {
3437  // Try to downcast to DescriptorScriptPubKeyMan then check if the
3438  // descriptors match
3439  DescriptorScriptPubKeyMan *spk_manager =
3440  dynamic_cast<DescriptorScriptPubKeyMan *>(
3441  spk_man_pair.second.get());
3442  if (spk_manager != nullptr && spk_manager->HasWalletDescriptor(desc)) {
3443  return spk_manager;
3444  }
3445  }
3446 
3447  return nullptr;
3448 }
3449 
3452  const FlatSigningProvider &signing_provider,
3453  const std::string &label, bool internal) {
3455 
3458  "Cannot add WalletDescriptor to a non-descriptor wallet\n");
3459  return nullptr;
3460  }
3461 
3462  auto spk_man = GetDescriptorScriptPubKeyMan(desc);
3463  if (spk_man) {
3464  WalletLogPrintf("Update existing descriptor: %s\n",
3465  desc.descriptor->ToString());
3466  spk_man->UpdateWalletDescriptor(desc);
3467  } else {
3468  auto new_spk_man =
3469  std::make_unique<DescriptorScriptPubKeyMan>(*this, desc);
3470  spk_man = new_spk_man.get();
3471 
3472  // Save the descriptor to memory
3473  m_spk_managers[new_spk_man->GetID()] = std::move(new_spk_man);
3474  }
3475 
3476  // Add the private keys to the descriptor
3477  for (const auto &entry : signing_provider.keys) {
3478  const CKey &key = entry.second;
3479  spk_man->AddDescriptorKey(key, key.GetPubKey());
3480  }
3481 
3482  // Top up key pool, the manager will generate new scriptPubKeys internally
3483  if (!spk_man->TopUp()) {
3484  WalletLogPrintf("Could not top up scriptPubKeys\n");
3485  return nullptr;
3486  }
3487 
3488  // Apply the label if necessary
3489  // Note: we disable labels for ranged descriptors
3490  if (!desc.descriptor->IsRange()) {
3491  auto script_pub_keys = spk_man->GetScriptPubKeys();
3492  if (script_pub_keys.empty()) {
3494  "Could not generate scriptPubKeys (cache is empty)\n");
3495  return nullptr;
3496  }
3497 
3498  CTxDestination dest;
3499  if (!internal && ExtractDestination(script_pub_keys.at(0), dest)) {
3500  SetAddressBook(dest, label, "receive");
3501  }
3502  }
3503 
3504  // Save the descriptor to DB
3505  spk_man->WriteDescriptor();
3506 
3507  return spk_man;
3508 }
bool MoneyRange(const Amount nValue)
Definition: amount.h:166
static constexpr Amount SATOSHI
Definition: amount.h:143
int flags
Definition: bitcoin-tx.cpp:533
static constexpr int64_t TIMESTAMP_WINDOW
Timestamp window used as a grace period by code that compares external timestamps (such as timestamps...
Definition: chain.h:37
const CChainParams & Params()
Return the currently selected parameters.
#define CHECK_NONFATAL(condition)
Identity function.
Definition: check.h:53
#define Assert(val)
Identity function.
Definition: check.h:84
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:490
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:603
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:665
Address book data.
Definition: wallet.h:198
BlockHash GetHash() const
Definition: block.cpp:11
BlockHash hashPrevBlock
Definition: block.h:27
bool IsNull() const
Definition: block.h:49
Definition: block.h:60
std::vector< CTransactionRef > vtx
Definition: block.h:63
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:74
Encryption/decryption context with key information.
Definition: crypter.h:64
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< uint8_t > &vchCiphertext) const
Definition: crypter.cpp:79
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< uint8_t > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
Definition: crypter.cpp:41
bool Decrypt(const std::vector< uint8_t > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
Definition: crypter.cpp:100
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
std::string ToString() const
Definition: feerate.cpp:57
Amount GetFeePerK() const
Return the fee in satoshis for a size of 1000 bytes.
Definition: feerate.h:54
An encapsulated secp256k1 private key.
Definition: key.h:28
const uint8_t * begin() const
Definition: key.h:90
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:89
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:183
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:210
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:302
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
A key from a CWallet's keypool.
bool fInternal
Whether this keypool entry is in the internal keypool (for change outputs)
CPubKey vchPubKey
The public key.
int64_t nTime
The time at which the key was generated. Set in AddKeypoolPubKeyWithDB.
CKeyPool()
Definition: wallet.cpp:3099
bool m_pre_split
Whether this key was generated for a keypool before the wallet was upgraded to HD-split.
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key.
Definition: crypter.h:31
std::vector< uint8_t > vchSalt
Definition: crypter.h:34
unsigned int nDerivationMethod
0 = EVP_sha512() 1 = scrypt()
Definition: crypter.h:37
unsigned int nDeriveIterations
Definition: crypter.h:38
std::vector< uint8_t > vchCryptedKey
Definition: crypter.h:33
A mutable version of CTransaction.
Definition: transaction.h:274
std::vector< CTxIn > vin
Definition: transaction.h:276
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
uint32_t GetN() const
Definition: transaction.h:36
const TxId & GetTxId() const
Definition: transaction.h:35
An encapsulated public key.
Definition: pubkey.h:31
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
const std::vector< CTxOut > vout
Definition: transaction.h:207
const std::vector< CTxIn > vin
Definition: transaction.h:206
An input of a transaction.
Definition: transaction.h:59
COutPoint prevout
Definition: transaction.h:61
An output of a transaction.
Definition: transaction.h:128
CScript scriptPubKey
Definition: transaction.h:131
bool IsNull() const
Definition: transaction.h:145
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:253
std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const
Get the SigningProvider for a script.
Definition: wallet.cpp:3253
std::atomic< int64_t > m_best_block_time
Definition: wallet.h:279
bool Lock()
Definition: wallet.cpp:3153
BlockHash GetLastBlockHash() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.h:1025
std::set< ScriptPubKeyMan * > GetScriptPubKeyMans(const CScript &script, SignatureData &sigdata) const
Get all of the ScriptPubKeyMans for a script given additional information in sigdata (populated by e....
Definition: wallet.cpp:3224
bool HaveChain() const
Interface to assert chain access.
Definition: wallet.h:424
int GetTxBlocksToMaturity(const CWalletTx &wtx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:3122
bool DummySignTx(CMutableTransaction &txNew, const std::set< CTxOut > &txouts, bool use_max_sig=false) const
Definition: wallet.h:695
void ConnectScriptPubKeyManNotifiers()
Connect the signals from ScriptPubKeyMans to the signals in CWallet.
Definition: wallet.cpp:3311
void AddActiveScriptPubKeyMan(uint256 id, OutputType type, bool internal)
Adds the active ScriptPubKeyMan for the specified type and internal.
Definition: wallet.cpp:3364
void SetupLegacyScriptPubKeyMan()
Make a LegacyScriptPubKeyMan and set it for all types, internal, and external.
Definition: wallet.cpp:3288
bool AddDestData(WalletBatch &batch, const CTxDestination &dest, const std::string &key, const std::string &value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Adds a destination data tuple to the store, and saves it to disk When adding new fields,...
Definition: wallet.cpp:2617
boost::signals2::signal< void()> NotifyCanGetAddressesChanged
Keypool has new keys.
Definition: wallet.h:863
const std::string GetDisplayName() const override
Returns a bracketed wallet name for displaying in logs, will return [default wallet] if the wallet ha...
Definition: wallet.h:954
MasterKeyMap mapMasterKeys
Definition: wallet.h:403
TxItems wtxOrdered
Definition: wallet.h:429
const std::string & GetName() const
Get a name for this wallet for logging/debugging purposes.
Definition: wallet.h:400
int GetTxDepthInMainChain(const CWalletTx &wtx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Return depth of transaction in blockchain: <0 : conflicts with a transaction this deep in the blockch...
Definition: wallet.cpp:3112
bool IsTxImmatureCoinBase(const CWalletTx &wtx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:3134
boost::signals2::signal< void(const std::string &title, int nProgress)> ShowProgress
Show progress e.g.
Definition: wallet.h:857
RecursiveMutex cs_wallet
Definition: wallet.h:388
bool Unlock(const CKeyingMaterial &vMasterKeyIn, bool accept_no_keys=false)
Definition: wallet.cpp:3172
bool GetBroadcastTransactions() const
Inquire whether this wallet broadcasts transactions.
Definition: wallet.h:872
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:965
void SetupDescriptorScriptPubKeyMans() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Create new DescriptorScriptPubKeyMans and add them to the wallet.
Definition: wallet.cpp:3326
interfaces::Chain * m_chain
Interface for accessing chain state.
Definition: wallet.h:350
LegacyScriptPubKeyMan * GetOrCreateLegacyScriptPubKeyMan()
Definition: wallet.cpp:3283
std::map< OutputType, ScriptPubKeyMan * > m_external_spk_managers
Definition: wallet.h:375
WalletDatabase & GetDatabase() override
Definition: wallet.h:395
void DeactivateScriptPubKeyMan(const uint256 &id, OutputType type, bool internal)
Remove specified ScriptPubKeyMan from set of active SPK managers.
Definition: wallet.cpp:3402
bool GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Look up a destination data tuple in the store, return true if found false otherwise.
Definition: wallet.cpp:2641
bool IsLegacy() const
Determine if we are a legacy wallet.
Definition: wallet.cpp:3425
interfaces::Chain & chain() const
Interface for accessing chain state.
Definition: wallet.h:447
std::atomic< bool > fAbortRescan
Definition: wallet.h:260
std::map< uint256, std::unique_ptr< ScriptPubKeyMan > > m_spk_managers
Definition: wallet.h:381
void LoadActiveScriptPubKeyMan(uint256 id, OutputType type, bool internal)
Loads an active ScriptPubKeyMan for the specified type and internal.
Definition: wallet.cpp:3375
static std::shared_ptr< CWallet > Create(interfaces::Chain &chain, const std::string &name, std::unique_ptr< WalletDatabase > database, uint64_t wallet_creation_flags, bilingual_str &error, std::vector< bilingual_str > &warnings)
Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error.
Definition: wallet.cpp:2705
boost::signals2::signal< void(CWallet *wallet, const CTxDestination &address, const std::string &label, bool isMine, const std::string &purpose, ChangeType status)> NotifyAddressBookChanged
Address book entry changed.
Definition: wallet.h:845
int GetLastBlockHeight() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Get last block processed height.
Definition: wallet.h:1020
boost::signals2::signal< void(CWallet *wallet)> NotifyStatusChanged
Wallet status (encrypted, locked) changed.
Definition: wallet.h:869
OutputType m_default_address_type
Definition: wallet.h:738
DescriptorScriptPubKeyMan * GetDescriptorScriptPubKeyMan(const WalletDescriptor &desc) const
Return the DescriptorScriptPubKeyMan for a WalletDescriptor if it is already in the wallet.
Definition: wallet.cpp:3435
void LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor &desc)
Instantiate a descriptor ScriptPubKeyMan from the WalletDescriptor and load it.
Definition: wallet.cpp:3319
LegacyScriptPubKeyMan * GetLegacyScriptPubKeyMan() const
Get the LegacyScriptPubKeyMan which is used for all types, internal, and external.
Definition: wallet.cpp:3269
std::atomic< uint64_t > m_wallet_flags
Definition: wallet.h:337
int64_t nNextResend
Definition: wallet.h:275
bool CanSupportFeature(enum WalletFeature wf) const override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
check whether we are allowed to upgrade (or already support) to the named feature
Definition: wallet.h:485
bool BackupWallet(const std::string &strDest) const
Definition: wallet.cpp:3095
unsigned int ComputeTimeSmart(const CWalletTx &wtx) const
Compute smart timestamp for a transaction being added to the wallet.
Definition: wallet.cpp:2575
void WalletLogPrintfToBeContinued(std::string fmt, Params... parameters) const
Definition: wallet.h:970
std::unique_ptr< WalletDatabase > database
Internal database handle.
Definition: wallet.h:356
ScriptPubKeyMan * AddWalletDescriptor(WalletDescriptor &desc, const FlatSigningProvider &signing_provider, const std::string &label, bool internal) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Add a descriptor to the wallet, return a ScriptPubKeyMan & associated output type.
Definition: wallet.cpp:3451
std::set< ScriptPubKeyMan * > GetActiveScriptPubKeyMans() const
Returns all unique ScriptPubKeyMans in m_internal_spk_managers and m_external_spk_managers.
Definition: wallet.cpp:3187
std::vector< std::string > GetDestValues(const std::string &prefix) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Get all destination values matching a prefix.
Definition: wallet.cpp:2660
boost::signals2::signal< void(bool fHaveWatchOnly)> NotifyWatchonlyChanged
Watch-only address added.
Definition: wallet.h:860
bool IsLocked() const override
Definition: wallet.cpp:3145
std::map< OutputType, ScriptPubKeyMan * > m_internal_spk_managers
Definition: wallet.h:376
std::atomic< double > m_scanning_progress
Definition: wallet.h:264
int GetVersion() const
get the current wallet format (the oldest client version guaranteed to understand this wallet)
Definition: wallet.h:814
void GetKeyBirthTimes(std::map< CKeyID, int64_t > &mapKeyBirth) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2483
bool EraseDestData(WalletBatch &batch, const CTxDestination &dest, const std::string &key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Erases a destination data tuple in the store and on disk.
Definition: wallet.cpp:2627
boost::signals2::signal< void(CWallet *wallet, const TxId &txid, ChangeType status)> NotifyTransactionChanged
Wallet transaction added, removed or updated.
Definition: wallet.h:853
bool HasEncryptionKeys() const override
Definition: wallet.cpp:3307
CWallet(interfaces::Chain *chain, const std::string &name, std::unique_ptr< WalletDatabase > _database)
Construct wallet with specified name and database implementation.
Definition: wallet.h:407
Amount m_default_max_tx_fee
Absolute maximum transaction fee (in satoshis) used by default for the wallet.
Definition: wallet.h:750
bool UpgradeWallet(int version, bilingual_str &error)
Upgrade the wallet.
Definition: wallet.cpp:3041
bool fBroadcastTransactions
Definition: wallet.h:276
ScriptPubKeyMan * GetScriptPubKeyMan(const OutputType &type, bool internal) const
Get the ScriptPubKeyMan for the given OutputType and internal/external chain.
Definition: wallet.cpp:3208
bool IsCrypted() const
Definition: wallet.cpp:3141
std::set< ScriptPubKeyMan * > GetAllScriptPubKeyMans() const
Returns all unique ScriptPubKeyMans.
Definition: wallet.cpp:3200
std::multimap< int64_t, CWalletTx * > TxItems
Definition: wallet.h:428
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:2636
unsigned int nMasterKeyMaxID
Definition: wallet.h:404
std::function< bool(CWalletTx &wtx, bool new_tx)> UpdateWalletTxFn
Callback for updating transaction metadata in mapWallet.
Definition: wallet.h:594
void postInitProcess()
Wallet post-init setup Gives the wallet a chance to register repetitive tasks and complete post-init ...
Definition: wallet.cpp:3084
const CAddressBookData * FindAddressBookEntry(const CTxDestination &, bool allow_change=false) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:3029
const CKeyingMaterial & GetEncryptionKey() const override
Definition: wallet.cpp:3303
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:65
bool isAbandoned() const
Definition: transaction.h:279
mapValue_t mapValue
Key/value map with information about the transaction.
Definition: transaction.h:99
CTransactionRef tx
Definition: transaction.h:160
bool isUnconfirmed() const
Definition: transaction.h:292
void setConflicted()
Definition: transaction.h:291
unsigned int nTimeSmart
Stable timestamp that never changes, and reflects the order a transaction was added to the wallet.
Definition: transaction.h:113
std::multimap< int64_t, CWalletTx * >::const_iterator m_it_wtxOrdered
Definition: transaction.h:122
bool IsEquivalentTo(const CWalletTx &tx) const
Definition: transaction.cpp:7
bool isConflicted() const
Definition: transaction.h:288
Confirmation m_confirm
Definition: transaction.h:191
TxId GetId() const
Definition: transaction.h:300
std::vector< std::pair< std::string, std::string > > vOrderForm
Definition: transaction.h:100
bool fFromMe
From me flag is set to 1 for transactions that were created by the wallet on this bitcoin node,...
Definition: transaction.h:119
void setAbandoned()
Definition: transaction.h:282
void setUnconfirmed()
Definition: transaction.h:295
bool fInMempool
Definition: transaction.h:141
unsigned int fTimeReceivedIsTxTime
Definition: transaction.h:101
bool isConfirmed() const
Definition: transaction.h:296
void MarkDirty()
make sure balances are recalculated
Definition: transaction.h:263
bool m_is_cache_empty
This flag is true if all m_amounts caches are empty.
Definition: transaction.h:139
bool InMempool() const
Definition: transaction.cpp:21
bool IsCoinBase() const
Definition: transaction.h:301
unsigned int nTimeReceived
time received by this node
Definition: transaction.h:103
int64_t nOrderPos
position in ordered transaction list
Definition: transaction.h:121
A UTXO entry.
Definition: coins.h:27
bool HasWalletDescriptor(const WalletDescriptor &desc) const
RecursiveMutex cs_KeyStore
Different type to mark Mutex at global scope.
Definition: sync.h:144
std::set< CKeyID > GetKeys() const override
A wrapper to reserve an address from a wallet.
Definition: wallet.h:160
bool fInternal
Whether this is from the internal (change output) keypool.
Definition: wallet.h:173
OutputType const type
Definition: wallet.h:167
ScriptPubKeyMan * m_spk_man
The ScriptPubKeyMan to reserve from.
Definition: wallet.h:166
int64_t nIndex
The index of the address's key in the keypool.
Definition: wallet.h:169
CTxDestination address
The destination.
Definition: wallet.h:171
const CWallet *const pwallet
The wallet to reserve from.
Definition: wallet.h:163
A class implementing ScriptPubKeyMan manages some (or all) scriptPubKeys used in a wallet.
virtual bool TopUp(unsigned int size=0)
Fills internal address pool.
virtual bool GetReservedDestination(const OutputType type, bool internal, CTxDestination &address, int64_t &index, CKeyPool &keypool)
virtual void KeepDestination(int64_t index, const OutputType &type)
virtual void ReturnDestination(int64_t index, bool internal, const CTxDestination &addr)
Signature hash type wrapper class.
Definition: sighashtype.h:37
bool setArray()
Definition: univalue.cpp:94
bool isArray() const
Definition: univalue.h:95
@ VARR
Definition: univalue.h:27
size_t size() const
Definition: univalue.h:80
const std::vector< UniValue > & getValues() const
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
Access to the wallet database.
Definition: walletdb.h:175
bool TxnCommit()
Commit current transaction.
Definition: walletdb.cpp:1112
bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id, bool internal)
Definition: walletdb.cpp:213
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition: walletdb.cpp:154
bool WriteName(const CTxDestination &address, const std::string &strName)
Definition: walletdb.cpp:60
bool WritePurpose(const CTxDestination &address, const std::string &purpose)
Definition: walletdb.cpp:81
bool WriteMinVersion(int nVersion)
Definition: walletdb.cpp:209
bool ErasePurpose(const CTxDestination &address)
Definition: walletdb.cpp:91
bool EraseDestData(const CTxDestination &address, const std::string &key)
Erase destination data tuple from wallet database.
Definition: walletdb.cpp:1090
bool WriteWalletFlags(const uint64_t flags)
Definition: walletdb.cpp:1104
bool ReadBestBlock(CBlockLocator &locator)
Definition: walletdb.cpp:186
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition: walletdb.cpp:193
bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal)
Definition: walletdb.cpp:220
bool WriteTx(const CWalletTx &wtx)
Definition: walletdb.cpp:99
bool TxnBegin()
Begin a new transaction.
Definition: walletdb.cpp:1108
bool TxnAbort()
Abort current transaction.
Definition: walletdb.cpp:1116
bool EraseName(const CTxDestination &address)
Definition: walletdb.cpp:70
bool WriteBestBlock(const CBlockLocator &locator)
Definition: walletdb.cpp:179
DBErrors ZapSelectTx(std::vector< TxId > &txIdsIn, std::vector< TxId > &txIdsOut)
Definition: walletdb.cpp:1008
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:774
bool WriteDestData(const CTxDestination &address, const std::string &key, const std::string &value)
Write destination data key,value tuple to database.
Definition: walletdb.cpp:1077
Descriptor with some wallet metadata.
Definition: walletutil.h:80
std::shared_ptr< Descriptor > descriptor
Definition: walletutil.h:82
RAII object to check and reserve a wallet rescan.
Definition: wallet.h:1098
bool isReserved() const
Definition: wallet.h:1118
uint8_t * begin()
Definition: uint256.h:85
std::string ToString() const
Definition: uint256.h:80
void SetNull()
Definition: uint256.h:41
bool IsNull() const
Definition: uint256.h:32
std::string GetHex() const
Definition: uint256.cpp:16
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:123
virtual CBlockLocator getTipLocator()=0
Get locator for the current chain tip.
virtual BlockHash getBlockHash(int height)=0
Get block hash. Height must be valid or this function will abort.
virtual bool findBlock(const BlockHash &hash, const FoundBlock &block={})=0
Return whether node has the block and optionally return block metadata or contents.
virtual bool updateRwSetting(const std::string &name, const util::SettingsValue &value, bool write=true)=0
Write a setting to <datadir>/settings.json.
virtual bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock &block={})=0
Find first block in the chain with timestamp >= the given time and height >= than the given height,...
virtual bool broadcastTransaction(const Config &config, const CTransactionRef &tx, const Amount &max_tx_fee, bool relay, std::string &err_string)=0
Transaction is added to memory pool, if the transaction fee is below the amount specified by max_tx_f...
virtual util::SettingsValue getRwSetting(const std::string &name)=0
Return <datadir>/settings.json setting value.
virtual double guessVerificationProgress(const BlockHash &block_hash)=0
Estimate fraction of total transactions verified if blocks up to the specified block hash are verifie...
virtual bool havePruned()=0
Check if any block has been pruned.
virtual std::optional< int > getHeight()=0
Get current chain height, not including genesis block (returns 0 if chain only contains genesis block...
virtual std::optional< int > findLocatorFork(const CBlockLocator &locator)=0
Return height of the highest block on chain in common with the locator, which will either be the orig...
virtual bool findAncestorByHeight(const BlockHash &block_hash, int ancestor_height, const FoundBlock &ancestor_out={})=0
Find ancestor of block at specified height and optionally return ancestor information.
virtual void initMessage(const std::string &message)=0
Send init message.
virtual bool haveBlockOnDisk(int height)=0
Check that the block is available on disk (i.e.
virtual void requestMempoolTransactions(Notifications &notifications)=0
Synchronously send transactionAddedToMempool notifications about all current mempool transactions to ...
virtual void waitForNotificationsIfTipChanged(const BlockHash &old_tip)=0
Wait for pending notifications to be processed unless block hash points to the current chain tip.
virtual CFeeRate relayMinFee()=0
Relay current minimum fee (from -minrelaytxfee settings).
virtual const CChainParams & params() const =0
This Chain's parameters.
Helper for findBlock to selectively return pieces of block data.
Definition: chain.h:48
256-bit opaque blob.
Definition: uint256.h:129
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
Definition: cleanse.cpp:14
const Config & GetConfig()
Definition: config.cpp:34
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule).
Definition: consensus.h:32
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:13
std::vector< uint8_t, secure_allocator< uint8_t > > CKeyingMaterial
Definition: crypter.h:57
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:12
bilingual_str AmountHighWarn(const std::string &optname)
Definition: error.cpp:47
bilingual_str AmountErrMsg(const std::string &optname, const std::string &strValue)
Definition: error.cpp:51
TransactionError
Definition: error.h:22
void LockCoin(const COutPoint &output) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2453
void MarkDestinationsDirty(const std::set< CTxDestination > &destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Marks all outputs in each one of the destinations dirty, so their cache is reset and does not return ...
Definition: wallet.cpp:2376
size_t KeypoolCountExternalKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2299
void CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector< std::pair< std::string, std::string >> orderForm, bool broadcast=true)
Add the transaction to the wallet and maybe attempt to broadcast it.
Definition: wallet.cpp:2122
void KeepDestination()
Keep the address.
Definition: wallet.cpp:2436
void ListLockedCoins(std::vector< COutPoint > &vOutpts) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2474
unsigned int GetKeyPoolSize() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2310
std::set< CTxDestination > GetLabelAddresses(const std::string &label) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2397
bool IsLockedCoin(const COutPoint &outpoint) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2468
SigningResult SignMessage(const std::string &message, const PKHash &pkhash, std::string &str_sig) const
Definition: wallet.cpp:2092
void UnlockCoin(const COutPoint &output) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2458
bool SetAddressBook(const CTxDestination &address, const std::string &strName, const std::string &purpose)
Definition: wallet.cpp:2258
DBErrors LoadWallet(bool &fFirstRunRet)
Definition: wallet.cpp:2169
OutputType TransactionChangeType(const std::optional< OutputType > &change_type, const std::vector< CRecipient > &vecSend) const
Definition: wallet.cpp:2106
bool SignTransaction(CMutableTransaction &tx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2001
void ReturnDestination()
Return reserved address.
Definition: wallet.cpp:2445
bool GetNewChangeDestination(const OutputType type, CTxDestination &dest, std::string &error)
Definition: wallet.cpp:2349
void UnlockAllCoins() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2463
bool TopUpKeyPool(unsigned int kpSize=0)
Definition: wallet.cpp:2320
bool SetAddressBookWithDB(WalletBatch &batch, const CTxDestination &address, const std::string &strName, const std::string &strPurpose)
Definition: wallet.cpp:2231
TransactionError FillPSBT(PartiallySignedTransaction &psbtx, bool &complete, SigHashType sighash_type=SigHashType().withForkId(), bool sign=true, bool bip32derivs=true) const
Fills out a PSBT with information from the wallet.
Definition: wallet.cpp:2048
bool GetReservedDestination(CTxDestination &pubkey, bool internal)
Reserve an address.
Definition: wallet.cpp:2415
int64_t GetOldestKeyPoolTime() const
Definition: wallet.cpp:2366
bool DelAddressBook(const CTxDestination &address)
Definition: wallet.cpp:2265
bool GetNewDestination(const OutputType type, const std::string label, CTxDestination &dest, std::string &error)
Definition: wallet.cpp:2329
DBErrors ZapSelectTx(std::vector< TxId > &txIdsIn, std::vector< TxId > &txIdsOut) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2199
bool AddWalletFlags(uint64_t flags)
Overwrite all flags by the given uint64_t.
Definition: wallet.cpp:1530
void blockConnected(const CBlock &block, int height) override
Definition: wallet.cpp:1352
bool LoadToWallet(const TxId &txid, const UpdateWalletTxFn &fill_wtx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:1044
void MarkConflicted(const BlockHash &hashBlock, int conflicting_height, const TxId &txid)
Mark a transaction (and its in-wallet descendants) as conflicting with a particular block.
Definition: wallet.cpp:1227
void Flush()
Flush wallet (bitdb flush)
Definition: wallet.cpp:602
void UpgradeKeyMetadata() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Upgrade stored CKeyMetadata objects to store key origin info as KeyOriginInfo.
Definition: wallet.cpp:400
bool SetMaxVersion(int nVersion)
change which version we're allowed to upgrade to (note that this does not immediately imply upgrading...
Definition: wallet.cpp:554
bool ImportPubKeys(const std::vector< CKeyID > &ordered_pubkeys, const std::map< CKeyID, CPubKey > &pubkey_map, const std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo >> &key_origins, const bool add_keypool, const bool internal, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:1605
std::set< TxId > GetConflicts(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Get wallet transactions that conflict with given transaction (spend same outputs)
Definition: wallet.cpp:567
void MarkDirty()
Definition: wallet.cpp:892
bool SubmitTxMemoryPoolAndRelay(const CWalletTx &wtx, std::string &err_string, bool relay) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Pass this transaction to node for mempool insertion and relay to peers if flag set to true.
Definition: wallet.cpp:1879
void AddToSpends(const COutPoint &outpoint, const TxId &wtxid) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:680
void SyncTransaction(const CTransactionRef &tx, CWalletTx::Confirmation confirm, bool update_tx=true) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Used by TransactionAddedToMemorypool/BlockConnected/Disconnected/ScanForWalletTransactions.
Definition: wallet.cpp:1284
bool ImportScripts(const std::set< CScript > scripts, int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:1585
CWalletTx * AddToWallet(CTransactionRef tx, const CWalletTx::Confirmation &confirm, const UpdateWalletTxFn &update_wtx=nullptr, bool fFlushOnClose=true)
Definition: wallet.cpp:950
bool HasWalletSpend(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Check if a given transaction has any of its outputs spent by another transaction in the wallet.
Definition: wallet.cpp:596
bool ChangeWalletPassphrase(const SecureString &strOldWalletPassphrase, const SecureString &strNewWalletPassphrase)
Definition: wallet.cpp:444
bool IsFromMe(const CTransaction &tx) const
should probably be renamed to IsRelevantToMe
Definition: wallet.cpp:1447
bool ImportPrivKeys(const std::map< CKeyID, CKey > &privkey_map, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:1595
isminetype IsMine(const CTxDestination &dest) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:1422
bool LoadWalletFlags(uint64_t flags)
Loads the flags into the wallet.
Definition: wallet.cpp:1519
bool ImportScriptPubKeys(const std::string &label, const std::set< CScript > &script_pub_keys, const bool have_solving_data, const bool apply_label, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:1619
bool CanGetAddresses(bool internal=false) const
Returns true if the wallet can give out new addresses.
Definition: wallet.cpp:1474
ScanResult ScanForWalletTransactions(const BlockHash &start_block, int start_height, std::optional< int > max_height, const WalletRescanReserver &reserver, bool fUpdate)
Scan the block chain (starting in start_block) for transactions from or to us.
Definition: wallet.cpp:1705
bool IsSpentKey(const TxId &txid, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:923
bool TransactionCanBeAbandoned(const TxId &txid) const
Return whether transaction can be abandoned.
Definition: wallet.cpp:1152
const CChainParams & GetChainParams() const override
Definition: wallet.cpp:384
Amount GetDebit(const CTxIn &txin, const isminefilter &filter) const
Returns amount of debit if the input matches the filter, otherwise returns 0.
Definition: wallet.cpp:1401
void MarkInputsDirty(const CTransactionRef &tx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Mark a transaction's inputs dirty, thus forcing the outputs to be recomputed.
Definition: wallet.cpp:1159
bool AddToWalletIfInvolvingMe(const CTransactionRef &tx, CWalletTx::Confirmation confirm, bool fUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Add a transaction to the wallet, or update it.
Definition: wallet.cpp:1095
bool IsSpent(const COutPoint &outpoint) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Outpoint is spent if any non-conflicted transaction, spends it:
Definition: wallet.cpp:659
void ReacceptWalletTransactions() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:1846
bool IsHDEnabled() const
Definition: wallet.cpp:1465
void UnsetWalletFlagWithDB(WalletBatch &batch, uint64_t flag)
Unsets a wallet flag and saves it to disk.
Definition: wallet.cpp:1502
void SyncMetaData(std::pair< TxSpends::iterator, TxSpends::iterator >) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:610
bool EncryptWallet(const SecureString &strWalletPassphrase)
Definition: wallet.cpp:704
void updatedBlockTip() override
Definition: wallet.cpp:1384
void UnsetWalletFlag(uint64_t flag)
Unsets a single wallet flag.
Definition: wallet.cpp:1497
void transactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) override
Definition: wallet.cpp:1310
bool IsWalletFlagSet(uint64_t flag) const override
Check if a certain wallet flag is set.
Definition: wallet.cpp:1515
int64_t RescanFromTime(int64_t startTime, const WalletRescanReserver &reserver, bool update)
Scan active chain for relevant transactions after importing keys.
Definition: wallet.cpp:1654
bool AbandonTransaction(const TxId &txid)
Mark a transaction (and it in-wallet descendants) as abandoned so its inputs may be respent.
Definition: wallet.cpp:1168
void UnsetBlankWalletFlag(WalletBatch &batch) override
Unset the blank wallet flag and saves it to disk.
Definition: wallet.cpp:1511
void SetSpentKeyState(WalletBatch &batch, const TxId &txid, unsigned int n, bool used, std::set< CTxDestination > &tx_destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:899
void transactionAddedToMempool(const CTransactionRef &tx, uint64_t mempool_sequence) override
Definition: wallet.cpp:1297
DBErrors ReorderTransactions()
Definition: wallet.cpp:824
void blockDisconnected(const CBlock &block, int height) override
Definition: wallet.cpp:1367
void Close()
Close wallet database.
Definition: wallet.cpp:606
int64_t IncOrderPosNext(WalletBatch *batch=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Increment the next transaction order id.
Definition: wallet.cpp:880
const CWalletTx * GetWalletTx(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:390
void ResendWalletTransactions()
Definition: wallet.cpp:1940
void SetMinVersion(enum WalletFeature, WalletBatch *batch_in=nullptr, bool fExplicit=false) override
signify that a particular wallet feature is now used.
Definition: wallet.cpp:526
std::set< TxId > GetTxConflicts(const CWalletTx &wtx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:1920
bool DummySignInput(CTxIn &tx_in, const CTxOut &txout, bool use_max_sig=false) const
Definition: wallet.cpp:1544
void chainStateFlushed(const CBlockLocator &loc) override
Definition: wallet.cpp:521
uint8_t isminefilter
Definition: wallet.h:41
isminetype
IsMine() return codes.
Definition: ismine.h:18
@ ISMINE_ALL
Definition: ismine.h:23
@ ISMINE_NO
Definition: ismine.h:19
SigningResult
Definition: message.h:47
@ PRIVATE_KEY_NOT_AVAILABLE
bool ParseMoney(const std::string &money_string, Amount &nRet)
Parse an amount denoted in full coins.
Definition: moneystr.cpp:37
static auto quoted(const std::string &s)
Definition: fs.h:107
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:142
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:165
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition: fs.cpp:37
std::unique_ptr< Wallet > MakeWallet(const std::shared_ptr< CWallet > &wallet)
Return implementation of Wallet interface.
Definition: dummywallet.cpp:44
std::unique_ptr< Handler > MakeHandler(boost::signals2::connection connection)
Return handler wrapping a boost signal connection.
Definition: handler.cpp:48
const std::string & FormatOutputType(OutputType type)
Definition: outputtype.cpp:27
const std::array< OutputType, 1 > OUTPUT_TYPES
Definition: outputtype.cpp:17
OutputType
Definition: outputtype.h:16
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed.
Definition: psbt.cpp:160
void GetStrongRandBytes(Span< uint8_t > bytes) noexcept
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using i...
Definition: random.cpp:642
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition: random.h:85
const char * prefix
Definition: rest.cpp:819
const char * name
Definition: rest.cpp:48
std::vector< CKeyID > GetAffectedKeys(const CScript &spk, const SigningProvider &provider)
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:55
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
Definition: sign.cpp:198
void UpdateInput(CTxIn &input, const SignatureData &data)
Definition: sign.cpp:331
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
Definition: sign.cpp:421
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
Definition: sign.cpp:419
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:158
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:260
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:240
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:85
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
Definition: string.cpp:10
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:87
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:105
Definition: key.h:164
void SetSeed(const uint8_t *seed, unsigned int nSeedLen)
Definition: key.cpp:382
std::optional< int > last_scanned_height
Definition: wallet.h:617
BlockHash last_scanned_block
Hash and height of most recent block that was successfully scanned.
Definition: wallet.h:616
enum CWallet::ScanResult::@20 status
BlockHash last_failed_block
Hash of the most recent block that could not be scanned due to read errors or pruning.
Definition: wallet.h:623
Confirmation includes tx status and a triplet of {block height/block hash/tx index in block} at which...
Definition: transaction.h:181
uint64_t create_flags
Definition: db.h:224
SecureString create_passphrase
Definition: db.h:225
std::map< CKeyID, CKey > keys
A structure for PSBTs which contain per-input information.
Definition: psbt.h:44
CTxOut utxo
Definition: psbt.h:45
A version of CTransaction with the PSBT format.
Definition: psbt.h:334
std::vector< PSBTInput > inputs
Definition: psbt.h:336
std::optional< CMutableTransaction > tx
Definition: psbt.h:335
A TxId is the identifier of a transaction.
Definition: txid.h:14
Bilingual messages:
Definition: translation.h:17
std::string translated
Definition: translation.h:19
#define WAIT_LOCK(cs, name)
Definition: sync.h:317
#define AssertLockNotHeld(cs)
Definition: sync.h:163
#define LOCK(cs)
Definition: sync.h:306
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:357
ArgsManager gArgs
Definition: system.cpp:80
std::string ShellEscape(const std::string &arg)
Definition: system.cpp:1371
bool error(const char *fmt, const Args &...args)
Definition: system.h:45
static int count
Definition: tests.c:31
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:101
int64_t GetTime()
Definition: time.cpp:109
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:148
@ BLOCK
Removed for block.
@ CONFLICT
Removed for conflict with in-block transaction.
@ CT_UPDATED
Definition: ui_change_type.h:9
@ CT_DELETED
Definition: ui_change_type.h:9
@ CT_NEW
Definition: ui_change_type.h:9
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())
DatabaseStatus
Definition: db.h:229
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: walletdb.cpp:1120
std::map< std::string, std::string > mapValue_t
Definition: transaction.h:21
constexpr Amount HIGH_TX_FEE_PER_KB
Discourage users to set fees higher than this amount (in satoshis) per kB.
Definition: wallet.h:109
std::function< void(std::unique_ptr< interfaces::Wallet > wallet)> LoadWalletFn
Definition: wallet.h:47
constexpr OutputType DEFAULT_ADDRESS_TYPE
Default for -addresstype.
Definition: wallet.h:125
constexpr Amount HIGH_MAX_TX_FEE
-maxtxfee will warn if called with a higher fee than this amount (in satoshis)
Definition: wallet.h:112
static const bool DEFAULT_SPEND_ZEROCONF_CHANGE
Default for -spendzeroconfchange.
Definition: wallet.h:103
static constexpr uint64_t KNOWN_WALLET_FLAGS
Definition: wallet.h:127
static const bool DEFAULT_WALLETBROADCAST
Definition: wallet.h:104
constexpr Amount HIGH_APS_FEE
discourage APS fee higher than this amount
Definition: wallet.h:99
std::shared_ptr< CWallet > LoadWallet(interfaces::Chain &chain, const std::string &name, std::optional< bool > load_on_start, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error, std::vector< bilingual_str > &warnings)
Definition: wallet.cpp:263
std::unique_ptr< interfaces::Handler > HandleLoadWallet(LoadWalletFn load_wallet)
Definition: wallet.cpp:165
bool RemoveWallet(const std::shared_ptr< CWallet > &wallet, std::optional< bool > load_on_start, std::vector< bilingual_str > &warnings)
Definition: wallet.cpp:119
static void ReleaseWallet(CWallet *wallet)
Definition: wallet.cpp:184
const std::map< uint64_t, std::string > WALLET_FLAG_CAVEATS
Definition: wallet.cpp:42
void MaybeResendWalletTxs()
Called periodically by the schedule thread.
Definition: wallet.cpp:1989
static std::vector< std::shared_ptr< CWallet > > vpwallets GUARDED_BY(cs_wallets)
std::shared_ptr< CWallet > GetWallet(const std::string &name)
Definition: wallet.cpp:154
std::shared_ptr< CWallet > CreateWallet(interfaces::Chain &chain, const std::string &name, std::optional< bool > load_on_start, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error, std::vector< bilingual_str > &warnings)
Definition: wallet.cpp:281
void UnloadWallet(std::shared_ptr< CWallet > &&wallet)
Explicitly unload and delete the wallet.
Definition: wallet.cpp:200
static std::condition_variable g_wallet_release_cv
Definition: wallet.cpp:177
std::unique_ptr< WalletDatabase > MakeWalletDatabase(const std::string &name, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error_string)
Definition: wallet.cpp:2673
static GlobalMutex g_loading_wallet_mutex
Definition: wallet.cpp:175
RecursiveMutex cs_wallets
Definition: wallet.cpp:49
bool AddWalletSetting(interfaces::Chain &chain, const std::string &wallet_name)
Add wallet name to persistent configuration so it will be loaded on startup.
Definition: wallet.cpp:53
bool RemoveWalletSetting(interfaces::Chain &chain, const std::string &wallet_name)
Remove wallet name from persistent configuration so it will not be loaded on startup.
Definition: wallet.cpp:68
static void UpdateWalletSetting(interfaces::Chain &chain, const std::string &wallet_name, std::optional< bool > load_on_startup, std::vector< bilingual_str > &warnings)
Definition: wallet.cpp:86
std::vector< std::shared_ptr< CWallet > > GetWallets()
Definition: wallet.cpp:149
static GlobalMutex g_wallet_release_mutex
Definition: wallet.cpp:176
bool AddWallet(const std::shared_ptr< CWallet > &wallet)
Definition: wallet.cpp:105
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:45
@ NONCRITICAL_ERROR
fs::path GetWalletDir()
Get the path of the wallet directory.
Definition: walletutil.cpp:13
@ WALLET_FLAG_DISABLE_PRIVATE_KEYS
Definition: walletutil.h:55
@ WALLET_FLAG_AVOID_REUSE
Definition: walletutil.h:47
@ WALLET_FLAG_KEY_ORIGIN_METADATA
Definition: walletutil.h:51
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:70
@ WALLET_FLAG_BLANK_WALLET
Flag set when a wallet contains no HD seed and no private keys, scripts, addresses,...
Definition: walletutil.h:67
WalletFeature
(client) version numbers for particular wallet features
Definition: walletutil.h:14
@ FEATURE_HD_SPLIT
Definition: walletutil.h:28
@ FEATURE_WALLETCRYPT
Definition: walletutil.h:20
@ FEATURE_PRE_SPLIT_KEYPOOL
Definition: walletutil.h:34
@ FEATURE_LATEST
Definition: walletutil.h:36