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