Bitcoin ABC  0.24.7
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/consensus.h>
12 #include <consensus/validation.h>
13 #include <fs.h>
14 #include <interfaces/wallet.h>
15 #include <key.h>
16 #include <key_io.h>
17 #include <policy/mempool.h>
18 #include <policy/policy.h>
19 #include <primitives/transaction.h>
20 #include <random.h>
21 #include <script/descriptor.h>
22 #include <script/script.h>
23 #include <script/sighashtype.h>
24 #include <script/sign.h>
25 #include <script/signingprovider.h>
26 #include <txmempool.h>
27 #include <univalue.h>
28 #include <util/bip32.h>
29 #include <util/check.h>
30 #include <util/error.h>
31 #include <util/moneystr.h>
32 #include <util/string.h>
33 #include <util/translation.h>
34 #include <wallet/coincontrol.h>
35 #include <wallet/fees.h>
36 
37 #include <boost/algorithm/string/replace.hpp>
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  return true;
115 }
116 
117 bool RemoveWallet(const std::shared_ptr<CWallet> &wallet,
118  std::optional<bool> load_on_start,
119  std::vector<bilingual_str> &warnings) {
120  assert(wallet);
121 
122  interfaces::Chain &chain = wallet->chain();
123  std::string name = wallet->GetName();
124 
125  // Unregister with the validation interface which also drops shared ponters.
126  wallet->m_chain_notifications_handler.reset();
127  LOCK(cs_wallets);
128  std::vector<std::shared_ptr<CWallet>>::iterator i =
129  std::find(vpwallets.begin(), vpwallets.end(), wallet);
130  if (i == vpwallets.end()) {
131  return false;
132  }
133  vpwallets.erase(i);
134 
135  // Write the wallet setting
136  UpdateWalletSetting(chain, name, load_on_start, warnings);
137 
138  return true;
139 }
140 
141 bool RemoveWallet(const std::shared_ptr<CWallet> &wallet,
142  std::optional<bool> load_on_start) {
143  std::vector<bilingual_str> warnings;
144  return RemoveWallet(wallet, load_on_start, warnings);
145 }
146 
147 std::vector<std::shared_ptr<CWallet>> GetWallets() {
148  LOCK(cs_wallets);
149  return vpwallets;
150 }
151 
152 std::shared_ptr<CWallet> GetWallet(const std::string &name) {
153  LOCK(cs_wallets);
154  for (const std::shared_ptr<CWallet> &wallet : vpwallets) {
155  if (wallet->GetName() == name) {
156  return wallet;
157  }
158  }
159  return nullptr;
160 }
161 
162 std::unique_ptr<interfaces::Handler>
164  LOCK(cs_wallets);
165  auto it = g_load_wallet_fns.emplace(g_load_wallet_fns.end(),
166  std::move(load_wallet));
167  return interfaces::MakeHandler([it] {
168  LOCK(cs_wallets);
169  g_load_wallet_fns.erase(it);
170  });
171 }
172 
175 static std::condition_variable g_wallet_release_cv;
176 static std::set<std::string>
177  g_loading_wallet_set GUARDED_BY(g_loading_wallet_mutex);
178 static std::set<std::string>
179  g_unloading_wallet_set GUARDED_BY(g_wallet_release_mutex);
180 
181 // Custom deleter for shared_ptr<CWallet>.
182 static void ReleaseWallet(CWallet *wallet) {
183  const std::string name = wallet->GetName();
184  wallet->WalletLogPrintf("Releasing wallet\n");
185  wallet->Flush();
186  delete wallet;
187  // Wallet is now released, notify UnloadWallet, if any.
188  {
190  if (g_unloading_wallet_set.erase(name) == 0) {
191  // UnloadWallet was not called for this wallet, all done.
192  return;
193  }
194  }
195  g_wallet_release_cv.notify_all();
196 }
197 
198 void UnloadWallet(std::shared_ptr<CWallet> &&wallet) {
199  // Mark wallet for unloading.
200  const std::string name = wallet->GetName();
201  {
203  auto it = g_unloading_wallet_set.insert(name);
204  assert(it.second);
205  }
206  // The wallet can be in use so it's not possible to explicitly unload here.
207  // Notify the unload intent so that all remaining shared pointers are
208  // released.
209  wallet->NotifyUnload();
210 
211  // Time to ditch our shared_ptr and wait for ReleaseWallet call.
212  wallet.reset();
213  {
215  while (g_unloading_wallet_set.count(name) == 1) {
216  g_wallet_release_cv.wait(lock);
217  }
218  }
219 }
220 
221 static const size_t OUTPUT_GROUP_MAX_ENTRIES = 10;
222 
223 namespace {
224 std::shared_ptr<CWallet>
225 LoadWalletInternal(interfaces::Chain &chain, const std::string &name,
226  std::optional<bool> load_on_start,
227  const DatabaseOptions &options, DatabaseStatus &status,
228  bilingual_str &error, std::vector<bilingual_str> &warnings) {
229  try {
230  std::unique_ptr<WalletDatabase> database =
231  MakeWalletDatabase(name, options, status, error);
232  if (!database) {
233  error = Untranslated("Wallet file verification failed.") +
234  Untranslated(" ") + error;
235  return nullptr;
236  }
237 
238  std::shared_ptr<CWallet> wallet =
239  CWallet::Create(chain, name, std::move(database),
240  options.create_flags, error, warnings);
241  if (!wallet) {
242  error = Untranslated("Wallet loading failed.") + Untranslated(" ") +
243  error;
245  return nullptr;
246  }
247  AddWallet(wallet);
248  wallet->postInitProcess();
249 
250  // Write the wallet setting
251  UpdateWalletSetting(chain, name, load_on_start, warnings);
252 
253  return wallet;
254  } catch (const std::runtime_error &e) {
255  error = Untranslated(e.what());
257  return nullptr;
258  }
259 }
260 } // namespace
261 
262 std::shared_ptr<CWallet>
263 LoadWallet(interfaces::Chain &chain, const std::string &name,
264  std::optional<bool> load_on_start, const DatabaseOptions &options,
266  std::vector<bilingual_str> &warnings) {
267  auto result = WITH_LOCK(g_loading_wallet_mutex,
268  return g_loading_wallet_set.insert(name));
269  if (!result.second) {
270  error = Untranslated("Wallet already being loading.");
272  return nullptr;
273  }
274  auto wallet = LoadWalletInternal(chain, name, load_on_start, options,
275  status, error, warnings);
276  WITH_LOCK(g_loading_wallet_mutex, g_loading_wallet_set.erase(result.first));
277  return wallet;
278 }
279 
280 std::shared_ptr<CWallet>
281 CreateWallet(interfaces::Chain &chain, const std::string &name,
282  std::optional<bool> load_on_start, const DatabaseOptions &options,
284  std::vector<bilingual_str> &warnings) {
285  uint64_t wallet_creation_flags = options.create_flags;
286  const SecureString &passphrase = options.create_passphrase;
287 
288  // Indicate that the wallet is actually supposed to be blank and not just
289  // blank to make it encrypted
290  bool create_blank = (wallet_creation_flags & WALLET_FLAG_BLANK_WALLET);
291 
292  // Born encrypted wallets need to be created blank first.
293  if (!passphrase.empty()) {
294  wallet_creation_flags |= WALLET_FLAG_BLANK_WALLET;
295  }
296 
297  // Wallet::Verify will check if we're trying to create a wallet with a
298  // duplicate name.
299  std::unique_ptr<WalletDatabase> database =
300  MakeWalletDatabase(name, options, status, error);
301  if (!database) {
302  error = Untranslated("Wallet file verification failed.") +
303  Untranslated(" ") + error;
305  return nullptr;
306  }
307 
308  // Do not allow a passphrase when private keys are disabled
309  if (!passphrase.empty() &&
310  (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
312  "Passphrase provided but private keys are disabled. A passphrase "
313  "is only used to encrypt private keys, so cannot be used for "
314  "wallets with private keys disabled.");
316  return nullptr;
317  }
318 
319  // Make the wallet
320  std::shared_ptr<CWallet> wallet =
321  CWallet::Create(chain, name, std::move(database), wallet_creation_flags,
322  error, warnings);
323  if (!wallet) {
324  error =
325  Untranslated("Wallet creation failed.") + Untranslated(" ") + error;
327  return nullptr;
328  }
329 
330  // Encrypt the wallet
331  if (!passphrase.empty() &&
332  !(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
333  if (!wallet->EncryptWallet(passphrase)) {
334  error =
335  Untranslated("Error: Wallet created but failed to encrypt.");
337  return nullptr;
338  }
339  if (!create_blank) {
340  // Unlock the wallet
341  if (!wallet->Unlock(passphrase)) {
343  "Error: Wallet was encrypted but could not be unlocked");
345  return nullptr;
346  }
347 
348  // Set a seed for the wallet
349  {
350  LOCK(wallet->cs_wallet);
351  if (wallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
352  wallet->SetupDescriptorScriptPubKeyMans();
353  } else {
354  for (auto spk_man : wallet->GetActiveScriptPubKeyMans()) {
355  if (!spk_man->SetupGeneration()) {
356  error =
357  Untranslated("Unable to generate initial keys");
359  return nullptr;
360  }
361  }
362  }
363  }
364 
365  // Relock the wallet
366  wallet->Lock();
367  }
368  }
369  AddWallet(wallet);
370  wallet->postInitProcess();
371 
372  // Write the wallet settings
373  UpdateWalletSetting(chain, name, load_on_start, warnings);
374 
375  status = DatabaseStatus::SUCCESS;
376  return wallet;
377 }
378 
384 std::string COutput::ToString() const {
385  return strprintf("COutput(%s, %d, %d) [%s]", tx->GetId().ToString(), i,
386  nDepth, FormatMoney(tx->tx->vout[i].nValue));
387 }
388 
390  // Get CChainParams from interfaces::Chain, unless wallet doesn't have a
391  // chain (i.e. bitcoin-wallet), in which case return global Params()
392  return m_chain ? m_chain->params() : Params();
393 }
394 
395 const CWalletTx *CWallet::GetWalletTx(const TxId &txid) const {
397  std::map<TxId, CWalletTx>::const_iterator it = mapWallet.find(txid);
398  if (it == mapWallet.end()) {
399  return nullptr;
400  }
401 
402  return &(it->second);
403 }
404 
407  return;
408  }
409 
410  auto spk_man = GetLegacyScriptPubKeyMan();
411  if (!spk_man) {
412  return;
413  }
414 
415  spk_man->UpgradeKeyMetadata();
416  SetWalletFlag(WALLET_FLAG_KEY_ORIGIN_METADATA);
417 }
418 
419 bool CWallet::Unlock(const SecureString &strWalletPassphrase,
420  bool accept_no_keys) {
421  CCrypter crypter;
422  CKeyingMaterial _vMasterKey;
423 
424  {
425  LOCK(cs_wallet);
426  for (const MasterKeyMap::value_type &pMasterKey : mapMasterKeys) {
427  if (!crypter.SetKeyFromPassphrase(
428  strWalletPassphrase, pMasterKey.second.vchSalt,
429  pMasterKey.second.nDeriveIterations,
430  pMasterKey.second.nDerivationMethod)) {
431  return false;
432  }
433  if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey,
434  _vMasterKey)) {
435  // try another master key
436  continue;
437  }
438  if (Unlock(_vMasterKey, accept_no_keys)) {
439  // Now that we've unlocked, upgrade the key metadata
441  return true;
442  }
443  }
444  }
445 
446  return false;
447 }
448 
450  const SecureString &strOldWalletPassphrase,
451  const SecureString &strNewWalletPassphrase) {
452  bool fWasLocked = IsLocked();
453 
454  LOCK(cs_wallet);
455  Lock();
456 
457  CCrypter crypter;
458  CKeyingMaterial _vMasterKey;
459  for (MasterKeyMap::value_type &pMasterKey : mapMasterKeys) {
460  if (!crypter.SetKeyFromPassphrase(
461  strOldWalletPassphrase, pMasterKey.second.vchSalt,
462  pMasterKey.second.nDeriveIterations,
463  pMasterKey.second.nDerivationMethod)) {
464  return false;
465  }
466 
467  if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, _vMasterKey)) {
468  return false;
469  }
470 
471  if (Unlock(_vMasterKey)) {
472  int64_t nStartTime = GetTimeMillis();
473  crypter.SetKeyFromPassphrase(strNewWalletPassphrase,
474  pMasterKey.second.vchSalt,
475  pMasterKey.second.nDeriveIterations,
476  pMasterKey.second.nDerivationMethod);
477  pMasterKey.second.nDeriveIterations = static_cast<unsigned int>(
478  pMasterKey.second.nDeriveIterations *
479  (100 / ((double)(GetTimeMillis() - nStartTime))));
480 
481  nStartTime = GetTimeMillis();
482  crypter.SetKeyFromPassphrase(strNewWalletPassphrase,
483  pMasterKey.second.vchSalt,
484  pMasterKey.second.nDeriveIterations,
485  pMasterKey.second.nDerivationMethod);
486  pMasterKey.second.nDeriveIterations =
487  (pMasterKey.second.nDeriveIterations +
488  static_cast<unsigned int>(
489  pMasterKey.second.nDeriveIterations * 100 /
490  double(GetTimeMillis() - nStartTime))) /
491  2;
492 
493  if (pMasterKey.second.nDeriveIterations < 25000) {
494  pMasterKey.second.nDeriveIterations = 25000;
495  }
496 
498  "Wallet passphrase changed to an nDeriveIterations of %i\n",
499  pMasterKey.second.nDeriveIterations);
500 
501  if (!crypter.SetKeyFromPassphrase(
502  strNewWalletPassphrase, pMasterKey.second.vchSalt,
503  pMasterKey.second.nDeriveIterations,
504  pMasterKey.second.nDerivationMethod)) {
505  return false;
506  }
507 
508  if (!crypter.Encrypt(_vMasterKey,
509  pMasterKey.second.vchCryptedKey)) {
510  return false;
511  }
512 
513  WalletBatch(*database).WriteMasterKey(pMasterKey.first,
514  pMasterKey.second);
515  if (fWasLocked) {
516  Lock();
517  }
518 
519  return true;
520  }
521  }
522 
523  return false;
524 }
525 
527  WalletBatch batch(*database);
528  batch.WriteBestBlock(loc);
529 }
530 
531 void CWallet::SetMinVersion(enum WalletFeature nVersion, WalletBatch *batch_in,
532  bool fExplicit) {
533  LOCK(cs_wallet);
534  if (nWalletVersion >= nVersion) {
535  return;
536  }
537 
538  // When doing an explicit upgrade, if we pass the max version permitted,
539  // upgrade all the way.
540  if (fExplicit && nVersion > nWalletMaxVersion) {
541  nVersion = FEATURE_LATEST;
542  }
543 
544  nWalletVersion = nVersion;
545 
546  if (nVersion > nWalletMaxVersion) {
547  nWalletMaxVersion = nVersion;
548  }
549 
550  WalletBatch *batch = batch_in ? batch_in : new WalletBatch(*database);
551  if (nWalletVersion > 40000) {
552  batch->WriteMinVersion(nWalletVersion);
553  }
554  if (!batch_in) {
555  delete batch;
556  }
557 }
558 
559 bool CWallet::SetMaxVersion(int nVersion) {
560  LOCK(cs_wallet);
561 
562  // Cannot downgrade below current version
563  if (nWalletVersion > nVersion) {
564  return false;
565  }
566 
567  nWalletMaxVersion = nVersion;
568 
569  return true;
570 }
571 
572 std::set<TxId> CWallet::GetConflicts(const TxId &txid) const {
573  std::set<TxId> result;
575 
576  std::map<TxId, CWalletTx>::const_iterator it = mapWallet.find(txid);
577  if (it == mapWallet.end()) {
578  return result;
579  }
580 
581  const CWalletTx &wtx = it->second;
582 
583  std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range;
584 
585  for (const CTxIn &txin : wtx.tx->vin) {
586  if (mapTxSpends.count(txin.prevout) <= 1) {
587  // No conflict if zero or one spends.
588  continue;
589  }
590 
591  range = mapTxSpends.equal_range(txin.prevout);
592  for (TxSpends::const_iterator _it = range.first; _it != range.second;
593  ++_it) {
594  result.insert(_it->second);
595  }
596  }
597 
598  return result;
599 }
600 
601 bool CWallet::HasWalletSpend(const TxId &txid) const {
603  auto iter = mapTxSpends.lower_bound(COutPoint(txid, 0));
604  return (iter != mapTxSpends.end() && iter->first.GetTxId() == txid);
605 }
606 
608  database->Flush();
609 }
610 
612  database->Close();
613 }
614 
616  std::pair<TxSpends::iterator, TxSpends::iterator> range) {
617  // We want all the wallet transactions in range to have the same metadata as
618  // the oldest (smallest nOrderPos).
619  // So: find smallest nOrderPos:
620 
621  int nMinOrderPos = std::numeric_limits<int>::max();
622  const CWalletTx *copyFrom = nullptr;
623  for (TxSpends::iterator it = range.first; it != range.second; ++it) {
624  const CWalletTx *wtx = &mapWallet.at(it->second);
625  if (wtx->nOrderPos < nMinOrderPos) {
626  nMinOrderPos = wtx->nOrderPos;
627  copyFrom = wtx;
628  }
629  }
630 
631  if (!copyFrom) {
632  return;
633  }
634 
635  // Now copy data from copyFrom to rest:
636  for (TxSpends::iterator it = range.first; it != range.second; ++it) {
637  const TxId &txid = it->second;
638  CWalletTx *copyTo = &mapWallet.at(txid);
639  if (copyFrom == copyTo) {
640  continue;
641  }
642 
643  assert(
644  copyFrom &&
645  "Oldest wallet transaction in range assumed to have been found.");
646 
647  if (!copyFrom->IsEquivalentTo(*copyTo)) {
648  continue;
649  }
650 
651  copyTo->mapValue = copyFrom->mapValue;
652  copyTo->vOrderForm = copyFrom->vOrderForm;
653  // fTimeReceivedIsTxTime not copied on purpose nTimeReceived not copied
654  // on purpose.
655  copyTo->nTimeSmart = copyFrom->nTimeSmart;
656  copyTo->fFromMe = copyFrom->fFromMe;
657  // nOrderPos not copied on purpose cached members not copied on purpose.
658  }
659 }
660 
664 bool CWallet::IsSpent(const COutPoint &outpoint) const {
665  std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range =
666  mapTxSpends.equal_range(outpoint);
667 
668  for (TxSpends::const_iterator it = range.first; it != range.second; ++it) {
669  const TxId &wtxid = it->second;
670  std::map<TxId, CWalletTx>::const_iterator mit = mapWallet.find(wtxid);
671  if (mit != mapWallet.end()) {
672  int depth = mit->second.GetDepthInMainChain();
673  if (depth > 0 || (depth == 0 && !mit->second.isAbandoned())) {
674  // Spent
675  return true;
676  }
677  }
678  }
679 
680  return false;
681 }
682 
683 void CWallet::AddToSpends(const COutPoint &outpoint, const TxId &wtxid) {
684  mapTxSpends.insert(std::make_pair(outpoint, wtxid));
685 
686  setLockedCoins.erase(outpoint);
687 
688  std::pair<TxSpends::iterator, TxSpends::iterator> range;
689  range = mapTxSpends.equal_range(outpoint);
690  SyncMetaData(range);
691 }
692 
693 void CWallet::AddToSpends(const TxId &wtxid) {
694  auto it = mapWallet.find(wtxid);
695  assert(it != mapWallet.end());
696  CWalletTx &thisTx = it->second;
697  // Coinbases don't spend anything!
698  if (thisTx.IsCoinBase()) {
699  return;
700  }
701 
702  for (const CTxIn &txin : thisTx.tx->vin) {
703  AddToSpends(txin.prevout, wtxid);
704  }
705 }
706 
707 bool CWallet::EncryptWallet(const SecureString &strWalletPassphrase) {
708  if (IsCrypted()) {
709  return false;
710  }
711 
712  CKeyingMaterial _vMasterKey;
713 
714  _vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
715  GetStrongRandBytes(&_vMasterKey[0], WALLET_CRYPTO_KEY_SIZE);
716 
717  CMasterKey kMasterKey;
718 
719  kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
721 
722  CCrypter crypter;
723  int64_t nStartTime = GetTimeMillis();
724  crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000,
725  kMasterKey.nDerivationMethod);
726  kMasterKey.nDeriveIterations = static_cast<unsigned int>(
727  2500000 / double(GetTimeMillis() - nStartTime));
728 
729  nStartTime = GetTimeMillis();
730  crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt,
731  kMasterKey.nDeriveIterations,
732  kMasterKey.nDerivationMethod);
733  kMasterKey.nDeriveIterations =
734  (kMasterKey.nDeriveIterations +
735  static_cast<unsigned int>(kMasterKey.nDeriveIterations * 100 /
736  double(GetTimeMillis() - nStartTime))) /
737  2;
738 
739  if (kMasterKey.nDeriveIterations < 25000) {
740  kMasterKey.nDeriveIterations = 25000;
741  }
742 
743  WalletLogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n",
744  kMasterKey.nDeriveIterations);
745 
746  if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt,
747  kMasterKey.nDeriveIterations,
748  kMasterKey.nDerivationMethod)) {
749  return false;
750  }
751 
752  if (!crypter.Encrypt(_vMasterKey, kMasterKey.vchCryptedKey)) {
753  return false;
754  }
755 
756  {
757  LOCK(cs_wallet);
758  mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
759  WalletBatch *encrypted_batch = new WalletBatch(*database);
760  if (!encrypted_batch->TxnBegin()) {
761  delete encrypted_batch;
762  encrypted_batch = nullptr;
763  return false;
764  }
765  encrypted_batch->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
766 
767  for (const auto &spk_man_pair : m_spk_managers) {
768  auto spk_man = spk_man_pair.second.get();
769  if (!spk_man->Encrypt(_vMasterKey, encrypted_batch)) {
770  encrypted_batch->TxnAbort();
771  delete encrypted_batch;
772  encrypted_batch = nullptr;
773  // We now probably have half of our keys encrypted in memory,
774  // and half not... die and let the user reload the unencrypted
775  // wallet.
776  assert(false);
777  }
778  }
779 
780  // Encryption was introduced in version 0.4.0
781  SetMinVersion(FEATURE_WALLETCRYPT, encrypted_batch, true);
782 
783  if (!encrypted_batch->TxnCommit()) {
784  delete encrypted_batch;
785  encrypted_batch = nullptr;
786  // We now have keys encrypted in memory, but not on disk...
787  // die to avoid confusion and let the user reload the unencrypted
788  // wallet.
789  assert(false);
790  }
791 
792  delete encrypted_batch;
793  encrypted_batch = nullptr;
794 
795  Lock();
796  Unlock(strWalletPassphrase);
797 
798  // If we are using descriptors, make new descriptors with a new seed
802  } else if (auto spk_man = GetLegacyScriptPubKeyMan()) {
803  // if we are using HD, replace the HD seed with a new one
804  if (spk_man->IsHDEnabled()) {
805  if (!spk_man->SetupGeneration(true)) {
806  return false;
807  }
808  }
809  }
810  Lock();
811 
812  // Need to completely rewrite the wallet file; if we don't, bdb might
813  // keep bits of the unencrypted private key in slack space in the
814  // database file.
815  database->Rewrite();
816 
817  // BDB seems to have a bad habit of writing old data into
818  // slack space in .dat files; that is bad if the old data is
819  // unencrypted private keys. So:
820  database->ReloadDbEnv();
821  }
822 
823  NotifyStatusChanged(this);
824  return true;
825 }
826 
828  LOCK(cs_wallet);
829  WalletBatch batch(*database);
830 
831  // Old wallets didn't have any defined order for transactions. Probably a
832  // bad idea to change the output of this.
833 
834  // First: get all CWalletTx into a sorted-by-time
835  // multimap.
836  TxItems txByTime;
837 
838  for (auto &entry : mapWallet) {
839  CWalletTx *wtx = &entry.second;
840  txByTime.insert(std::make_pair(wtx->nTimeReceived, wtx));
841  }
842 
843  nOrderPosNext = 0;
844  std::vector<int64_t> nOrderPosOffsets;
845  for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it) {
846  CWalletTx *const pwtx = (*it).second;
847  int64_t &nOrderPos = pwtx->nOrderPos;
848 
849  if (nOrderPos == -1) {
850  nOrderPos = nOrderPosNext++;
851  nOrderPosOffsets.push_back(nOrderPos);
852 
853  if (!batch.WriteTx(*pwtx)) {
854  return DBErrors::LOAD_FAIL;
855  }
856  } else {
857  int64_t nOrderPosOff = 0;
858  for (const int64_t &nOffsetStart : nOrderPosOffsets) {
859  if (nOrderPos >= nOffsetStart) {
860  ++nOrderPosOff;
861  }
862  }
863 
864  nOrderPos += nOrderPosOff;
865  nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
866 
867  if (!nOrderPosOff) {
868  continue;
869  }
870 
871  // Since we're changing the order, write it back.
872  if (!batch.WriteTx(*pwtx)) {
873  return DBErrors::LOAD_FAIL;
874  }
875  }
876  }
877 
878  batch.WriteOrderPosNext(nOrderPosNext);
879 
880  return DBErrors::LOAD_OK;
881 }
882 
885  int64_t nRet = nOrderPosNext++;
886  if (batch) {
887  batch->WriteOrderPosNext(nOrderPosNext);
888  } else {
889  WalletBatch(*database).WriteOrderPosNext(nOrderPosNext);
890  }
891 
892  return nRet;
893 }
894 
896  LOCK(cs_wallet);
897  for (std::pair<const TxId, CWalletTx> &item : mapWallet) {
898  item.second.MarkDirty();
899  }
900 }
901 
902 void CWallet::SetSpentKeyState(WalletBatch &batch, const TxId &txid,
903  unsigned int n, bool used,
904  std::set<CTxDestination> &tx_destinations) {
906  const CWalletTx *srctx = GetWalletTx(txid);
907  if (!srctx) {
908  return;
909  }
910 
911  CTxDestination dst;
912  if (ExtractDestination(srctx->tx->vout[n].scriptPubKey, dst)) {
913  if (IsMine(dst)) {
914  if (used && !GetDestData(dst, "used", nullptr)) {
915  // p for "present", opposite of absent (null)
916  if (AddDestData(batch, dst, "used", "p")) {
917  tx_destinations.insert(dst);
918  }
919  } else if (!used && GetDestData(dst, "used", nullptr)) {
920  EraseDestData(batch, dst, "used");
921  }
922  }
923  }
924 }
925 
926 bool CWallet::IsSpentKey(const TxId &txid, unsigned int n) const {
928  const CWalletTx *srctx = GetWalletTx(txid);
929  if (srctx) {
930  assert(srctx->tx->vout.size() > n);
931  CTxDestination dest;
932  if (!ExtractDestination(srctx->tx->vout[n].scriptPubKey, dest)) {
933  return false;
934  }
935  if (GetDestData(dest, "used", nullptr)) {
936  return true;
937  }
938  if (IsLegacy()) {
940  assert(spk_man != nullptr);
941  for (const auto &keyid :
942  GetAffectedKeys(srctx->tx->vout[n].scriptPubKey, *spk_man)) {
943  PKHash pkh_dest(keyid);
944  if (GetDestData(pkh_dest, "used", nullptr)) {
945  return true;
946  }
947  }
948  }
949  }
950  return false;
951 }
952 
954  const CWalletTx::Confirmation &confirm,
955  const UpdateWalletTxFn &update_wtx,
956  bool fFlushOnClose) {
957  LOCK(cs_wallet);
958 
959  WalletBatch batch(*database, fFlushOnClose);
960 
961  const TxId &txid = tx->GetId();
962 
964  // Mark used destinations
965  std::set<CTxDestination> tx_destinations;
966 
967  for (const CTxIn &txin : tx->vin) {
968  const COutPoint &op = txin.prevout;
969  SetSpentKeyState(batch, op.GetTxId(), op.GetN(), true,
970  tx_destinations);
971  }
972 
973  MarkDestinationsDirty(tx_destinations);
974  }
975 
976  // Inserts only if not already there, returns tx inserted or tx found.
977  auto ret =
978  mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(txid),
979  std::forward_as_tuple(this, tx));
980  CWalletTx &wtx = (*ret.first).second;
981  bool fInsertedNew = ret.second;
982  bool fUpdated = update_wtx && update_wtx(wtx, fInsertedNew);
983  if (fInsertedNew) {
984  wtx.m_confirm = confirm;
986  wtx.nOrderPos = IncOrderPosNext(&batch);
987  wtx.m_it_wtxOrdered =
988  wtxOrdered.insert(std::make_pair(wtx.nOrderPos, &wtx));
989  wtx.nTimeSmart = ComputeTimeSmart(wtx);
990  AddToSpends(txid);
991  }
992 
993  if (!fInsertedNew) {
994  if (confirm.status != wtx.m_confirm.status) {
995  wtx.m_confirm.status = confirm.status;
996  wtx.m_confirm.nIndex = confirm.nIndex;
997  wtx.m_confirm.hashBlock = confirm.hashBlock;
998  wtx.m_confirm.block_height = confirm.block_height;
999  fUpdated = true;
1000  } else {
1001  assert(wtx.m_confirm.nIndex == confirm.nIndex);
1002  assert(wtx.m_confirm.hashBlock == confirm.hashBlock);
1003  assert(wtx.m_confirm.block_height == confirm.block_height);
1004  }
1005  }
1006 
1008  WalletLogPrintf("AddToWallet %s %s%s\n", txid.ToString(),
1009  (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
1010 
1011  // Write to disk
1012  if ((fInsertedNew || fUpdated) && !batch.WriteTx(wtx)) {
1013  return nullptr;
1014  }
1015 
1016  // Break debit/credit balance caches:
1017  wtx.MarkDirty();
1018 
1019  // Notify UI of new or updated transaction.
1020  NotifyTransactionChanged(this, txid, fInsertedNew ? CT_NEW : CT_UPDATED);
1021 
1022 #if defined(HAVE_SYSTEM)
1023  // Notify an external script when a wallet transaction comes in or is
1024  // updated.
1025  std::string strCmd = gArgs.GetArg("-walletnotify", "");
1026 
1027  if (!strCmd.empty()) {
1028  boost::replace_all(strCmd, "%s", txid.GetHex());
1029 #ifndef WIN32
1030  // Substituting the wallet name isn't currently supported on windows
1031  // because windows shell escaping has not been implemented yet:
1032  // https://github.com/bitcoin/bitcoin/pull/13339#issuecomment-537384875
1033  // A few ways it could be implemented in the future are described in:
1034  // https://github.com/bitcoin/bitcoin/pull/13339#issuecomment-461288094
1035  boost::replace_all(strCmd, "%w", ShellEscape(GetName()));
1036 #endif
1037 
1038  std::thread t(runCommand, strCmd);
1039  // Thread runs free.
1040  t.detach();
1041  }
1042 #endif
1043 
1044  return &wtx;
1045 }
1046 
1047 bool CWallet::LoadToWallet(const TxId &txid, const UpdateWalletTxFn &fill_wtx) {
1048  const auto &ins =
1049  mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(txid),
1050  std::forward_as_tuple(this, nullptr));
1051  CWalletTx &wtx = ins.first->second;
1052  if (!fill_wtx(wtx, ins.second)) {
1053  return false;
1054  }
1055  // If wallet doesn't have a chain (e.g wallet-tool), don't bother to update
1056  // txn.
1057  if (HaveChain()) {
1058  std::optional<int> block_height =
1060  if (block_height) {
1061  // Update cached block height variable since it not stored in the
1062  // serialized transaction.
1063  wtx.m_confirm.block_height = *block_height;
1064  } else if (wtx.isConflicted() || wtx.isConfirmed()) {
1065  // If tx block (or conflicting block) was reorged out of chain
1066  // while the wallet was shutdown, change tx status to UNCONFIRMED
1067  // and reset block height, hash, and index. ABANDONED tx don't have
1068  // associated blocks and don't need to be updated. The case where a
1069  // transaction was reorged out while online and then reconfirmed
1070  // while offline is covered by the rescan logic.
1071  wtx.setUnconfirmed();
1072  wtx.m_confirm.hashBlock = BlockHash();
1073  wtx.m_confirm.block_height = 0;
1074  wtx.m_confirm.nIndex = 0;
1075  }
1076  }
1077  if (/* insertion took place */ ins.second) {
1078  wtx.m_it_wtxOrdered =
1079  wtxOrdered.insert(std::make_pair(wtx.nOrderPos, &wtx));
1080  }
1081  AddToSpends(txid);
1082  for (const CTxIn &txin : wtx.tx->vin) {
1083  auto it = mapWallet.find(txin.prevout.GetTxId());
1084  if (it != mapWallet.end()) {
1085  CWalletTx &prevtx = it->second;
1086  if (prevtx.isConflicted()) {
1088  prevtx.m_confirm.block_height, wtx.GetId());
1089  }
1090  }
1091  }
1092  return true;
1093 }
1094 
1096  CWalletTx::Confirmation confirm,
1097  bool fUpdate) {
1098  const CTransaction &tx = *ptx;
1100 
1101  if (!confirm.hashBlock.IsNull()) {
1102  for (const CTxIn &txin : tx.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 != tx.GetId()) {
1108  "Transaction %s (in block %s) conflicts with wallet "
1109  "transaction %s (both spend %s:%i)\n",
1110  tx.GetId().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(tx.GetId()) != 0;
1123  if (fExisted && !fUpdate) {
1124  return false;
1125  }
1126  if (fExisted || IsMine(tx) || IsFromMe(tx)) {
1135  // loop though all outputs
1136  for (const CTxOut &txout : tx.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(MakeTransactionRef(tx), 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() && wtx->GetDepthInMainChain() == 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  CWalletTx &origtx = it->second;
1179  if (origtx.GetDepthInMainChain() != 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 = wtx.GetDepthInMainChain();
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 = wtx.GetDepthInMainChain();
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  // chainActive.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 isminetype CWallet::IsMine(const CTxIn &txin) const {
1400  std::map<TxId, CWalletTx>::const_iterator mi =
1401  mapWallet.find(txin.prevout.GetTxId());
1402  if (mi != mapWallet.end()) {
1403  const CWalletTx &prev = (*mi).second;
1404  if (txin.prevout.GetN() < prev.tx->vout.size()) {
1405  return IsMine(prev.tx->vout[txin.prevout.GetN()]);
1406  }
1407  }
1408 
1409  return ISMINE_NO;
1410 }
1411 
1412 // Note that this function doesn't distinguish between a 0-valued input, and a
1413 // not-"is mine" (according to the filter) input.
1414 Amount CWallet::GetDebit(const CTxIn &txin, const isminefilter &filter) const {
1415  LOCK(cs_wallet);
1416  std::map<TxId, CWalletTx>::const_iterator mi =
1417  mapWallet.find(txin.prevout.GetTxId());
1418  if (mi != mapWallet.end()) {
1419  const CWalletTx &prev = (*mi).second;
1420  if (txin.prevout.GetN() < prev.tx->vout.size()) {
1421  if (IsMine(prev.tx->vout[txin.prevout.GetN()]) & filter) {
1422  return prev.tx->vout[txin.prevout.GetN()].nValue;
1423  }
1424  }
1425  }
1426 
1427  return Amount::zero();
1428 }
1429 
1430 isminetype CWallet::IsMine(const CTxOut &txout) const {
1432  return IsMine(txout.scriptPubKey);
1433 }
1434 
1437  return IsMine(GetScriptForDestination(dest));
1438 }
1439 
1440 isminetype CWallet::IsMine(const CScript &script) const {
1442  isminetype result = ISMINE_NO;
1443  for (const auto &spk_man_pair : m_spk_managers) {
1444  result = std::max(result, spk_man_pair.second->IsMine(script));
1445  }
1446  return result;
1447 }
1448 
1450  const isminefilter &filter) const {
1451  if (!MoneyRange(txout.nValue)) {
1452  throw std::runtime_error(std::string(__func__) +
1453  ": value out of range");
1454  }
1455  LOCK(cs_wallet);
1456  return (IsMine(txout) & filter) ? txout.nValue : Amount::zero();
1457 }
1458 
1459 bool CWallet::IsChange(const CTxOut &txout) const {
1460  return IsChange(txout.scriptPubKey);
1461 }
1462 
1463 bool CWallet::IsChange(const CScript &script) const {
1464  // TODO: fix handling of 'change' outputs. The assumption is that any
1465  // payment to a script that is ours, but is not in the address book is
1466  // change. That assumption is likely to break when we implement
1467  // multisignature wallets that return change back into a
1468  // multi-signature-protected address; a better way of identifying which
1469  // outputs are 'the send' and which are 'the change' will need to be
1470  // implemented (maybe extend CWalletTx to remember which output, if any, was
1471  // change).
1473  if (IsMine(script)) {
1474  CTxDestination address;
1475  if (!ExtractDestination(script, address)) {
1476  return true;
1477  }
1478  if (!FindAddressBookEntry(address)) {
1479  return true;
1480  }
1481  }
1482 
1483  return false;
1484 }
1485 
1486 Amount CWallet::GetChange(const CTxOut &txout) const {
1488  if (!MoneyRange(txout.nValue)) {
1489  throw std::runtime_error(std::string(__func__) +
1490  ": value out of range");
1491  }
1492 
1493  return (IsChange(txout) ? txout.nValue : Amount::zero());
1494 }
1495 
1496 bool CWallet::IsMine(const CTransaction &tx) const {
1498  for (const CTxOut &txout : tx.vout) {
1499  if (IsMine(txout)) {
1500  return true;
1501  }
1502  }
1503 
1504  return false;
1505 }
1506 
1507 bool CWallet::IsFromMe(const CTransaction &tx) const {
1508  return GetDebit(tx, ISMINE_ALL) > Amount::zero();
1509 }
1510 
1512  const isminefilter &filter) const {
1513  Amount nDebit = Amount::zero();
1514  for (const CTxIn &txin : tx.vin) {
1515  nDebit += GetDebit(txin, filter);
1516  if (!MoneyRange(nDebit)) {
1517  throw std::runtime_error(std::string(__func__) +
1518  ": value out of range");
1519  }
1520  }
1521 
1522  return nDebit;
1523 }
1524 
1526  const isminefilter &filter) const {
1527  LOCK(cs_wallet);
1528 
1529  for (const CTxIn &txin : tx.vin) {
1530  auto mi = mapWallet.find(txin.prevout.GetTxId());
1531  if (mi == mapWallet.end()) {
1532  // Any unknown inputs can't be from us.
1533  return false;
1534  }
1535 
1536  const CWalletTx &prev = (*mi).second;
1537 
1538  if (txin.prevout.GetN() >= prev.tx->vout.size()) {
1539  // Invalid input!
1540  return false;
1541  }
1542 
1543  if (!(IsMine(prev.tx->vout[txin.prevout.GetN()]) & filter)) {
1544  return false;
1545  }
1546  }
1547 
1548  return true;
1549 }
1550 
1552  const isminefilter &filter) const {
1553  Amount nCredit = Amount::zero();
1554  for (const CTxOut &txout : tx.vout) {
1555  nCredit += GetCredit(txout, filter);
1556  if (!MoneyRange(nCredit)) {
1557  throw std::runtime_error(std::string(__func__) +
1558  ": value out of range");
1559  }
1560  }
1561 
1562  return nCredit;
1563 }
1564 
1566  LOCK(cs_wallet);
1567  Amount nChange = Amount::zero();
1568  for (const CTxOut &txout : tx.vout) {
1569  nChange += GetChange(txout);
1570  if (!MoneyRange(nChange)) {
1571  throw std::runtime_error(std::string(__func__) +
1572  ": value out of range");
1573  }
1574  }
1575 
1576  return nChange;
1577 }
1578 
1579 bool CWallet::IsHDEnabled() const {
1580  // All Active ScriptPubKeyMans must be HD for this to be true
1581  bool result = true;
1582  for (const auto &spk_man : GetActiveScriptPubKeyMans()) {
1583  result &= spk_man->IsHDEnabled();
1584  }
1585  return result;
1586 }
1587 
1588 bool CWallet::CanGetAddresses(bool internal) const {
1589  LOCK(cs_wallet);
1590  if (m_spk_managers.empty()) {
1591  return false;
1592  }
1593  for (OutputType t : OUTPUT_TYPES) {
1594  auto spk_man = GetScriptPubKeyMan(t, internal);
1595  if (spk_man && spk_man->CanGetAddresses(internal)) {
1596  return true;
1597  }
1598  }
1599  return false;
1600 }
1601 
1602 void CWallet::SetWalletFlag(uint64_t flags) {
1603  LOCK(cs_wallet);
1604  m_wallet_flags |= flags;
1605  if (!WalletBatch(*database).WriteWalletFlags(m_wallet_flags)) {
1606  throw std::runtime_error(std::string(__func__) +
1607  ": writing wallet flags failed");
1608  }
1609 }
1610 
1611 void CWallet::UnsetWalletFlag(uint64_t flag) {
1612  WalletBatch batch(*database);
1613  UnsetWalletFlagWithDB(batch, flag);
1614 }
1615 
1616 void CWallet::UnsetWalletFlagWithDB(WalletBatch &batch, uint64_t flag) {
1617  LOCK(cs_wallet);
1618  m_wallet_flags &= ~flag;
1619  if (!batch.WriteWalletFlags(m_wallet_flags)) {
1620  throw std::runtime_error(std::string(__func__) +
1621  ": writing wallet flags failed");
1622  }
1623 }
1624 
1627 }
1628 
1629 bool CWallet::IsWalletFlagSet(uint64_t flag) const {
1630  return (m_wallet_flags & flag);
1631 }
1632 
1634  LOCK(cs_wallet);
1635  if (((flags & KNOWN_WALLET_FLAGS) >> 32) ^ (flags >> 32)) {
1636  // contains unknown non-tolerable wallet flags
1637  return false;
1638  }
1640 
1641  return true;
1642 }
1643 
1645  LOCK(cs_wallet);
1646  // We should never be writing unknown non-tolerable wallet flags
1647  assert(((flags & KNOWN_WALLET_FLAGS) >> 32) == (flags >> 32));
1648  if (!WalletBatch(*database).WriteWalletFlags(flags)) {
1649  throw std::runtime_error(std::string(__func__) +
1650  ": writing wallet flags failed");
1651  }
1652 
1653  return LoadWalletFlags(flags);
1654 }
1655 
1656 int64_t CWalletTx::GetTxTime() const {
1657  int64_t n = nTimeSmart;
1658  return n ? n : nTimeReceived;
1659 }
1660 
1661 // Helper for producing a max-sized low-S low-R signature (eg 71 bytes)
1662 // or a max-sized low-S signature (e.g. 72 bytes) if use_max_sig is true
1663 bool CWallet::DummySignInput(CTxIn &tx_in, const CTxOut &txout,
1664  bool use_max_sig) const {
1665  // Fill in dummy signatures for fee calculation.
1666  const CScript &scriptPubKey = txout.scriptPubKey;
1667  SignatureData sigdata;
1668 
1669  std::unique_ptr<SigningProvider> provider =
1670  GetSolvingProvider(scriptPubKey);
1671  if (!provider) {
1672  // We don't know about this scriptpbuKey;
1673  return false;
1674  }
1675 
1676  if (!ProduceSignature(*provider,
1677  use_max_sig ? DUMMY_MAXIMUM_SIGNATURE_CREATOR
1679  scriptPubKey, sigdata)) {
1680  return false;
1681  }
1682 
1683  UpdateInput(tx_in, sigdata);
1684  return true;
1685 }
1686 
1687 // Helper for producing a bunch of max-sized low-S low-R signatures (eg 71
1688 // bytes)
1690  const std::vector<CTxOut> &txouts,
1691  bool use_max_sig) const {
1692  // Fill in dummy signatures for fee calculation.
1693  int nIn = 0;
1694  for (const auto &txout : txouts) {
1695  if (!DummySignInput(txNew.vin[nIn], txout, use_max_sig)) {
1696  return false;
1697  }
1698 
1699  nIn++;
1700  }
1701  return true;
1702 }
1703 
1704 bool CWallet::ImportScripts(const std::set<CScript> scripts,
1705  int64_t timestamp) {
1706  auto spk_man = GetLegacyScriptPubKeyMan();
1707  if (!spk_man) {
1708  return false;
1709  }
1710  LOCK(spk_man->cs_KeyStore);
1711  return spk_man->ImportScripts(scripts, timestamp);
1712 }
1713 
1714 bool CWallet::ImportPrivKeys(const std::map<CKeyID, CKey> &privkey_map,
1715  const int64_t timestamp) {
1716  auto spk_man = GetLegacyScriptPubKeyMan();
1717  if (!spk_man) {
1718  return false;
1719  }
1720  LOCK(spk_man->cs_KeyStore);
1721  return spk_man->ImportPrivKeys(privkey_map, timestamp);
1722 }
1723 
1725  const std::vector<CKeyID> &ordered_pubkeys,
1726  const std::map<CKeyID, CPubKey> &pubkey_map,
1727  const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> &key_origins,
1728  const bool add_keypool, const bool internal, const int64_t timestamp) {
1729  auto spk_man = GetLegacyScriptPubKeyMan();
1730  if (!spk_man) {
1731  return false;
1732  }
1733  LOCK(spk_man->cs_KeyStore);
1734  return spk_man->ImportPubKeys(ordered_pubkeys, pubkey_map, key_origins,
1735  add_keypool, internal, timestamp);
1736 }
1737 
1738 bool CWallet::ImportScriptPubKeys(const std::string &label,
1739  const std::set<CScript> &script_pub_keys,
1740  const bool have_solving_data,
1741  const bool apply_label,
1742  const int64_t timestamp) {
1743  auto spk_man = GetLegacyScriptPubKeyMan();
1744  if (!spk_man) {
1745  return false;
1746  }
1747  LOCK(spk_man->cs_KeyStore);
1748  if (!spk_man->ImportScriptPubKeys(script_pub_keys, have_solving_data,
1749  timestamp)) {
1750  return false;
1751  }
1752  if (apply_label) {
1753  WalletBatch batch(*database);
1754  for (const CScript &script : script_pub_keys) {
1755  CTxDestination dest;
1756  ExtractDestination(script, dest);
1757  if (IsValidDestination(dest)) {
1758  SetAddressBookWithDB(batch, dest, label, "receive");
1759  }
1760  }
1761  }
1762  return true;
1763 }
1764 
1766  const CWallet *wallet, bool use_max_sig) {
1767  std::vector<CTxOut> txouts;
1768  for (auto &input : tx.vin) {
1769  const auto mi = wallet->mapWallet.find(input.prevout.GetTxId());
1770  // Can not estimate size without knowing the input details
1771  if (mi == wallet->mapWallet.end()) {
1772  return -1;
1773  }
1774  assert(input.prevout.GetN() < mi->second.tx->vout.size());
1775  txouts.emplace_back(mi->second.tx->vout[input.prevout.GetN()]);
1776  }
1777  return CalculateMaximumSignedTxSize(tx, wallet, txouts, use_max_sig);
1778 }
1779 
1780 // txouts needs to be in the order of tx.vin
1782  const CWallet *wallet,
1783  const std::vector<CTxOut> &txouts,
1784  bool use_max_sig) {
1785  CMutableTransaction txNew(tx);
1786  if (!wallet->DummySignTx(txNew, txouts, use_max_sig)) {
1787  return -1;
1788  }
1789  return GetSerializeSize(txNew, PROTOCOL_VERSION);
1790 }
1791 
1792 int CalculateMaximumSignedInputSize(const CTxOut &txout, const CWallet *wallet,
1793  bool use_max_sig) {
1794  CMutableTransaction txn;
1795  txn.vin.push_back(CTxIn(COutPoint()));
1796  if (!wallet->DummySignInput(txn.vin[0], txout, use_max_sig)) {
1797  return -1;
1798  }
1799  return GetSerializeSize(txn.vin[0], PROTOCOL_VERSION);
1800 }
1801 
1802 void CWalletTx::GetAmounts(std::list<COutputEntry> &listReceived,
1803  std::list<COutputEntry> &listSent, Amount &nFee,
1804  const isminefilter &filter) const {
1805  nFee = Amount::zero();
1806  listReceived.clear();
1807  listSent.clear();
1808 
1809  // Compute fee:
1810  Amount nDebit = GetDebit(filter);
1811  // debit>0 means we signed/sent this transaction.
1812  if (nDebit > Amount::zero()) {
1813  Amount nValueOut = tx->GetValueOut();
1814  nFee = (nDebit - nValueOut);
1815  }
1816 
1818  // Sent/received.
1819  for (unsigned int i = 0; i < tx->vout.size(); ++i) {
1820  const CTxOut &txout = tx->vout[i];
1821  isminetype fIsMine = pwallet->IsMine(txout);
1822  // Only need to handle txouts if AT LEAST one of these is true:
1823  // 1) they debit from us (sent)
1824  // 2) the output is to us (received)
1825  if (nDebit > Amount::zero()) {
1826  // Don't report 'change' txouts
1827  if (pwallet->IsChange(txout)) {
1828  continue;
1829  }
1830  } else if (!(fIsMine & filter)) {
1831  continue;
1832  }
1833 
1834  // In either case, we need to get the destination address.
1835  CTxDestination address;
1836 
1837  if (!ExtractDestination(txout.scriptPubKey, address) &&
1838  !txout.scriptPubKey.IsUnspendable()) {
1839  pwallet->WalletLogPrintf("CWalletTx::GetAmounts: Unknown "
1840  "transaction type found, txid %s\n",
1841  this->GetId().ToString());
1842  address = CNoDestination();
1843  }
1844 
1845  COutputEntry output = {address, txout.nValue, (int)i};
1846 
1847  // If we are debited by the transaction, add the output as a "sent"
1848  // entry.
1849  if (nDebit > Amount::zero()) {
1850  listSent.push_back(output);
1851  }
1852 
1853  // If we are receiving the output, add it as a "received" entry.
1854  if (fIsMine & filter) {
1855  listReceived.push_back(output);
1856  }
1857  }
1858 }
1859 
1868 int64_t CWallet::RescanFromTime(int64_t startTime,
1869  const WalletRescanReserver &reserver,
1870  bool update) {
1871  // Find starting block. May be null if nCreateTime is greater than the
1872  // highest blockchain timestamp, in which case there is nothing that needs
1873  // to be scanned.
1874  int start_height = 0;
1875  BlockHash start_block;
1876  bool start = chain().findFirstBlockWithTimeAndHeight(
1877  startTime - TIMESTAMP_WINDOW, 0,
1878  FoundBlock().hash(start_block).height(start_height));
1879  WalletLogPrintf("%s: Rescanning last %i blocks\n", __func__,
1880  start ? WITH_LOCK(cs_wallet, return GetLastBlockHeight()) -
1881  start_height + 1
1882  : 0);
1883 
1884  if (start) {
1885  // TODO: this should take into account failure by ScanResult::USER_ABORT
1887  start_block, start_height, {} /* max_height */, reserver, update);
1888  if (result.status == ScanResult::FAILURE) {
1889  int64_t time_max;
1890  CHECK_NONFATAL(chain().findBlock(result.last_failed_block,
1891  FoundBlock().maxTime(time_max)));
1892  return time_max + TIMESTAMP_WINDOW + 1;
1893  }
1894  }
1895  return startTime;
1896 }
1897 
1920  const BlockHash &start_block, int start_height,
1921  std::optional<int> max_height, const WalletRescanReserver &reserver,
1922  bool fUpdate) {
1923  int64_t nNow = GetTime();
1924  int64_t start_time = GetTimeMillis();
1925 
1926  assert(reserver.isReserved());
1927 
1928  BlockHash block_hash = start_block;
1929  ScanResult result;
1930 
1931  WalletLogPrintf("Rescan started from block %s...\n",
1932  start_block.ToString());
1933 
1934  fAbortRescan = false;
1935  // Show rescan progress in GUI as dialog or on splashscreen, if -rescan on
1936  // startup.
1937  ShowProgress(
1938  strprintf("%s " + _("Rescanning...").translated, GetDisplayName()), 0);
1939  BlockHash tip_hash = WITH_LOCK(cs_wallet, return GetLastBlockHash());
1940  BlockHash end_hash = tip_hash;
1941  if (max_height) {
1942  chain().findAncestorByHeight(tip_hash, *max_height,
1943  FoundBlock().hash(end_hash));
1944  }
1945  double progress_begin = chain().guessVerificationProgress(block_hash);
1946  double progress_end = chain().guessVerificationProgress(end_hash);
1947  double progress_current = progress_begin;
1948  int block_height = start_height;
1949  while (!fAbortRescan && !chain().shutdownRequested()) {
1950  if (progress_end - progress_begin > 0.0) {
1951  m_scanning_progress = (progress_current - progress_begin) /
1952  (progress_end - progress_begin);
1953  } else {
1954  // avoid divide-by-zero for single block scan range (i.e. start and
1955  // stop hashes are equal)
1956  m_scanning_progress = 0;
1957  }
1958  if (block_height % 100 == 0 && progress_end - progress_begin > 0.0) {
1959  ShowProgress(
1960  strprintf("%s " + _("Rescanning...").translated,
1961  GetDisplayName()),
1962  std::max(1, std::min(99, (int)(m_scanning_progress * 100))));
1963  }
1964  if (GetTime() >= nNow + 60) {
1965  nNow = GetTime();
1966  WalletLogPrintf("Still rescanning. At block %d. Progress=%f\n",
1967  block_height, progress_current);
1968  }
1969 
1970  CBlock block;
1971  bool next_block;
1972  BlockHash next_block_hash;
1973  bool reorg = false;
1974  if (chain().findBlock(block_hash, FoundBlock().data(block)) &&
1975  !block.IsNull()) {
1976  LOCK(cs_wallet);
1977  next_block = chain().findNextBlock(
1978  block_hash, block_height, FoundBlock().hash(next_block_hash),
1979  &reorg);
1980  if (reorg) {
1981  // Abort scan if current block is no longer active, to prevent
1982  // marking transactions as coming from the wrong block.
1983  // TODO: This should return success instead of failure, see
1984  // https://github.com/bitcoin/bitcoin/pull/14711#issuecomment-458342518
1985  result.last_failed_block = block_hash;
1986  result.status = ScanResult::FAILURE;
1987  break;
1988  }
1989  for (size_t posInBlock = 0; posInBlock < block.vtx.size();
1990  ++posInBlock) {
1991  CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED,
1992  block_height, block_hash,
1993  posInBlock);
1994  SyncTransaction(block.vtx[posInBlock],
1995  {CWalletTx::Status::CONFIRMED, block_height,
1996  block_hash, int(posInBlock)},
1997  fUpdate);
1998  }
1999  // scan succeeded, record block as most recent successfully
2000  // scanned
2001  result.last_scanned_block = block_hash;
2002  result.last_scanned_height = block_height;
2003  } else {
2004  // could not scan block, keep scanning but record this block as
2005  // the most recent failure
2006  result.last_failed_block = block_hash;
2007  result.status = ScanResult::FAILURE;
2008  next_block = chain().findNextBlock(
2009  block_hash, block_height, FoundBlock().hash(next_block_hash),
2010  &reorg);
2011  }
2012  if (max_height && block_height >= *max_height) {
2013  break;
2014  }
2015  {
2016  if (!next_block || reorg) {
2017  // break successfully when rescan has reached the tip, or
2018  // previous block is no longer on the chain due to a reorg
2019  break;
2020  }
2021 
2022  // increment block and verification progress
2023  block_hash = next_block_hash;
2024  ++block_height;
2025  progress_current = chain().guessVerificationProgress(block_hash);
2026 
2027  // handle updated tip hash
2028  const BlockHash prev_tip_hash = tip_hash;
2029  tip_hash = WITH_LOCK(cs_wallet, return GetLastBlockHash());
2030  if (!max_height && prev_tip_hash != tip_hash) {
2031  // in case the tip has changed, update progress max
2032  progress_end = chain().guessVerificationProgress(tip_hash);
2033  }
2034  }
2035  }
2036 
2037  // Hide progress dialog in GUI.
2038  ShowProgress(
2039  strprintf("%s " + _("Rescanning...").translated, GetDisplayName()),
2040  100);
2041  if (block_height && fAbortRescan) {
2042  WalletLogPrintf("Rescan aborted at block %d. Progress=%f\n",
2043  block_height, progress_current);
2044  result.status = ScanResult::USER_ABORT;
2045  } else if (block_height && chain().shutdownRequested()) {
2047  "Rescan interrupted by shutdown request at block %d. Progress=%f\n",
2048  block_height, progress_current);
2049  result.status = ScanResult::USER_ABORT;
2050  } else {
2051  WalletLogPrintf("Rescan completed in %15dms\n",
2052  GetTimeMillis() - start_time);
2053  }
2054  return result;
2055 }
2056 
2058  // If transactions aren't being broadcasted, don't let them into local
2059  // mempool either.
2060  if (!fBroadcastTransactions) {
2061  return;
2062  }
2063 
2064  std::map<int64_t, CWalletTx *> mapSorted;
2065 
2066  // Sort pending wallet transactions based on their initial wallet insertion
2067  // order.
2068  for (std::pair<const TxId, CWalletTx> &item : mapWallet) {
2069  const TxId &wtxid = item.first;
2070  CWalletTx &wtx = item.second;
2071  assert(wtx.GetId() == wtxid);
2072 
2073  int nDepth = wtx.GetDepthInMainChain();
2074 
2075  if (!wtx.IsCoinBase() && (nDepth == 0 && !wtx.isAbandoned())) {
2076  mapSorted.insert(std::make_pair(wtx.nOrderPos, &wtx));
2077  }
2078  }
2079 
2080  // Try to add wallet transactions to memory pool.
2081  for (const std::pair<const int64_t, CWalletTx *> &item : mapSorted) {
2082  CWalletTx &wtx = *(item.second);
2083  std::string unused_err_string;
2084  wtx.SubmitMemoryPoolAndRelay(unused_err_string, false);
2085  }
2086 }
2087 
2088 bool CWalletTx::SubmitMemoryPoolAndRelay(std::string &err_string, bool relay) {
2089  // Can't relay if wallet is not broadcasting
2091  return false;
2092  }
2093  // Don't relay abandoned transactions
2094  if (isAbandoned()) {
2095  return false;
2096  }
2097  // Don't try to submit coinbase transactions. These would fail anyway but
2098  // would cause log spam.
2099  if (IsCoinBase()) {
2100  return false;
2101  }
2102  // Don't try to submit conflicted or confirmed transactions.
2103  if (GetDepthInMainChain() != 0) {
2104  return false;
2105  }
2106 
2107  // Submit transaction to mempool for relay
2108  pwallet->WalletLogPrintf("Submitting wtx %s to mempool for relay\n",
2109  GetId().ToString());
2110  // We must set fInMempool here - while it will be re-set to true by the
2111  // entered-mempool callback, if we did not there would be a race where a
2112  // user could call sendmoney in a loop and hit spurious out of funds errors
2113  // because we think that this newly generated transaction's change is
2114  // unavailable as we're not yet aware that it is in the mempool.
2115  //
2116  // Irrespective of the failure reason, un-marking fInMempool
2117  // out-of-order is incorrect - it should be unmarked when
2118  // TransactionRemovedFromMempool fires.
2119  bool ret = pwallet->chain().broadcastTransaction(
2120  GetConfig(), tx, pwallet->m_default_max_tx_fee, relay, err_string);
2121  fInMempool |= ret;
2122  return ret;
2123 }
2124 
2125 std::set<TxId> CWalletTx::GetConflicts() const {
2126  std::set<TxId> result;
2127  if (pwallet != nullptr) {
2128  const TxId &txid = GetId();
2129  result = pwallet->GetConflicts(txid);
2130  result.erase(txid);
2131  }
2132 
2133  return result;
2134 }
2135 
2137  bool recalculate) const {
2138  auto &amount = m_amounts[type];
2139  if (recalculate || !amount.m_cached[filter]) {
2140  amount.Set(filter, type == DEBIT ? pwallet->GetDebit(*tx, filter)
2141  : pwallet->GetCredit(*tx, filter));
2142  m_is_cache_empty = false;
2143  }
2144  return amount.m_value[filter];
2145 }
2146 
2148  if (tx->vin.empty()) {
2149  return Amount::zero();
2150  }
2151 
2152  Amount debit = Amount::zero();
2153  if (filter & ISMINE_SPENDABLE) {
2155  }
2156  if (filter & ISMINE_WATCH_ONLY) {
2158  }
2159 
2160  return debit;
2161 }
2162 
2164  // Must wait until coinbase is safely deep enough in the chain before
2165  // valuing it.
2166  if (IsImmatureCoinBase()) {
2167  return Amount::zero();
2168  }
2169 
2170  Amount credit = Amount::zero();
2171  if (filter & ISMINE_SPENDABLE) {
2172  // GetBalance can assume transactions in mapWallet won't change.
2174  }
2175 
2176  if (filter & ISMINE_WATCH_ONLY) {
2178  }
2179 
2180  return credit;
2181 }
2182 
2183 Amount CWalletTx::GetImmatureCredit(bool fUseCache) const {
2184  if (IsImmatureCoinBase() && IsInMainChain()) {
2185  return GetCachableAmount(IMMATURE_CREDIT, ISMINE_SPENDABLE, !fUseCache);
2186  }
2187 
2188  return Amount::zero();
2189 }
2190 
2192  const isminefilter &filter) const {
2193  if (pwallet == nullptr) {
2194  return Amount::zero();
2195  }
2196 
2197  // Avoid caching ismine for NO or ALL cases (could remove this check and
2198  // simplify in the future).
2199  bool allow_cache =
2200  (filter & ISMINE_ALL) && (filter & ISMINE_ALL) != ISMINE_ALL;
2201 
2202  // Must wait until coinbase is safely deep enough in the chain before
2203  // valuing it.
2204  if (IsImmatureCoinBase()) {
2205  return Amount::zero();
2206  }
2207 
2208  if (fUseCache && allow_cache &&
2209  m_amounts[AVAILABLE_CREDIT].m_cached[filter]) {
2210  return m_amounts[AVAILABLE_CREDIT].m_value[filter];
2211  }
2212 
2213  bool allow_used_addresses =
2214  (filter & ISMINE_USED) ||
2216  Amount nCredit = Amount::zero();
2217  const TxId &txid = GetId();
2218  for (uint32_t i = 0; i < tx->vout.size(); i++) {
2219  if (!pwallet->IsSpent(COutPoint(txid, i)) &&
2220  (allow_used_addresses || !pwallet->IsSpentKey(txid, i))) {
2221  const CTxOut &txout = tx->vout[i];
2222  nCredit += pwallet->GetCredit(txout, filter);
2223  if (!MoneyRange(nCredit)) {
2224  throw std::runtime_error(std::string(__func__) +
2225  " : value out of range");
2226  }
2227  }
2228  }
2229 
2230  if (allow_cache) {
2231  m_amounts[AVAILABLE_CREDIT].Set(filter, nCredit);
2232  m_is_cache_empty = false;
2233  }
2234 
2235  return nCredit;
2236 }
2237 
2238 Amount CWalletTx::GetImmatureWatchOnlyCredit(const bool fUseCache) const {
2239  if (IsImmatureCoinBase() && IsInMainChain()) {
2241  !fUseCache);
2242  }
2243 
2244  return Amount::zero();
2245 }
2246 
2248  if (fChangeCached) {
2249  return nChangeCached;
2250  }
2251 
2253  fChangeCached = true;
2254  return nChangeCached;
2255 }
2256 
2257 bool CWalletTx::InMempool() const {
2258  return fInMempool;
2259 }
2260 
2261 bool CWalletTx::IsTrusted() const {
2262  std::set<TxId> trusted_parents;
2264  return pwallet->IsTrusted(*this, trusted_parents);
2265 }
2266 
2268  std::set<TxId> &trusted_parents) const {
2270  // Quick answer in most cases
2271  TxValidationState state;
2272  if (!chain().contextualCheckTransactionForCurrentBlock(*wtx.tx, state)) {
2273  return false;
2274  }
2275 
2276  int nDepth = wtx.GetDepthInMainChain();
2277  if (nDepth >= 1) {
2278  return true;
2279  }
2280 
2281  if (nDepth < 0) {
2282  return false;
2283  }
2284 
2285  // using wtx's cached debit
2287  return false;
2288  }
2289 
2290  // Don't trust unconfirmed transactions from us unless they are in the
2291  // mempool.
2292  if (!wtx.InMempool()) {
2293  return false;
2294  }
2295 
2296  // Trusted if all inputs are from us and are in the mempool:
2297  for (const CTxIn &txin : wtx.tx->vin) {
2298  // Transactions not sent by us: not trusted
2299  const CWalletTx *parent = GetWalletTx(txin.prevout.GetTxId());
2300  if (parent == nullptr) {
2301  return false;
2302  }
2303 
2304  const CTxOut &parentOut = parent->tx->vout[txin.prevout.GetN()];
2305  // Check that this specific input being spent is trusted
2306  if (IsMine(parentOut) != ISMINE_SPENDABLE) {
2307  return false;
2308  }
2309  // If we've already trusted this parent, continue
2310  if (trusted_parents.count(parent->GetId())) {
2311  continue;
2312  }
2313  // Recurse to check that the parent is also trusted
2314  if (!IsTrusted(*parent, trusted_parents)) {
2315  return false;
2316  }
2317  trusted_parents.insert(parent->GetId());
2318  }
2319 
2320  return true;
2321 }
2322 
2323 bool CWalletTx::IsEquivalentTo(const CWalletTx &_tx) const {
2324  CMutableTransaction tx1{*this->tx};
2325  CMutableTransaction tx2{*_tx.tx};
2326  for (auto &txin : tx1.vin) {
2327  txin.scriptSig = CScript();
2328  }
2329 
2330  for (auto &txin : tx2.vin) {
2331  txin.scriptSig = CScript();
2332  }
2333 
2334  return CTransaction(tx1) == CTransaction(tx2);
2335 }
2336 
2337 // Rebroadcast transactions from the wallet. We do this on a random timer
2338 // to slightly obfuscate which transactions come from our wallet.
2339 //
2340 // Ideally, we'd only resend transactions that we think should have been
2341 // mined in the most recent block. Any transaction that wasn't in the top
2342 // blockweight of transactions in the mempool shouldn't have been mined,
2343 // and so is probably just sitting in the mempool waiting to be confirmed.
2344 // Rebroadcasting does nothing to speed up confirmation and only damages
2345 // privacy.
2347  // During reindex, importing and IBD, old wallet transactions become
2348  // unconfirmed. Don't resend them as that would spam other nodes.
2349  if (!chain().isReadyToBroadcast()) {
2350  return;
2351  }
2352 
2353  // Do this infrequently and randomly to avoid giving away that these are our
2354  // transactions.
2356  return;
2357  }
2358 
2359  bool fFirst = (nNextResend == 0);
2360  // resend 12-36 hours from now, ~1 day on average.
2361  nNextResend = GetTime() + (12 * 60 * 60) + GetRand(24 * 60 * 60);
2362  if (fFirst) {
2363  return;
2364  }
2365 
2366  int submitted_tx_count = 0;
2367 
2368  { // cs_wallet scope
2369  LOCK(cs_wallet);
2370 
2371  // Relay transactions
2372  for (std::pair<const TxId, CWalletTx> &item : mapWallet) {
2373  CWalletTx &wtx = item.second;
2374  // Attempt to rebroadcast all txes more than 5 minutes older than
2375  // the last block. SubmitMemoryPoolAndRelay() will not rebroadcast
2376  // any confirmed or conflicting txs.
2377  if (wtx.nTimeReceived > m_best_block_time - 5 * 60) {
2378  continue;
2379  }
2380  std::string unused_err_string;
2381  if (wtx.SubmitMemoryPoolAndRelay(unused_err_string, true)) {
2382  ++submitted_tx_count;
2383  }
2384  }
2385  } // cs_wallet
2386 
2387  if (submitted_tx_count > 0) {
2388  WalletLogPrintf("%s: resubmit %u unconfirmed transactions\n", __func__,
2389  submitted_tx_count);
2390  }
2391 }
2392  // end of mapWallet
2394 
2396  for (const std::shared_ptr<CWallet> &pwallet : GetWallets()) {
2397  pwallet->ResendWalletTransactions();
2398  }
2399 }
2400 
2407  bool avoid_reuse) const {
2408  Balance ret;
2409  isminefilter reuse_filter = avoid_reuse ? ISMINE_NO : ISMINE_USED;
2410  LOCK(cs_wallet);
2411  std::set<TxId> trusted_parents;
2412  for (const auto &entry : mapWallet) {
2413  const CWalletTx &wtx = entry.second;
2414  const bool is_trusted{IsTrusted(wtx, trusted_parents)};
2415  const int tx_depth{wtx.GetDepthInMainChain()};
2416  const Amount tx_credit_mine{wtx.GetAvailableCredit(
2417  /* fUseCache */ true, ISMINE_SPENDABLE | reuse_filter)};
2418  const Amount tx_credit_watchonly{wtx.GetAvailableCredit(
2419  /* fUseCache */ true, ISMINE_WATCH_ONLY | reuse_filter)};
2420  if (is_trusted && tx_depth >= min_depth) {
2421  ret.m_mine_trusted += tx_credit_mine;
2422  ret.m_watchonly_trusted += tx_credit_watchonly;
2423  }
2424  if (!is_trusted && tx_depth == 0 && wtx.InMempool()) {
2425  ret.m_mine_untrusted_pending += tx_credit_mine;
2426  ret.m_watchonly_untrusted_pending += tx_credit_watchonly;
2427  }
2428  ret.m_mine_immature += wtx.GetImmatureCredit();
2430  }
2431  return ret;
2432 }
2433 
2435  LOCK(cs_wallet);
2436 
2438  std::vector<COutput> vCoins;
2439  AvailableCoins(vCoins, true, coinControl);
2440  for (const COutput &out : vCoins) {
2441  if (out.fSpendable) {
2442  balance += out.tx->tx->vout[out.i].nValue;
2443  }
2444  }
2445  return balance;
2446 }
2447 
2448 void CWallet::AvailableCoins(std::vector<COutput> &vCoins, bool fOnlySafe,
2449  const CCoinControl *coinControl,
2450  const Amount nMinimumAmount,
2451  const Amount nMaximumAmount,
2452  const Amount nMinimumSumAmount,
2453  const uint64_t nMaximumCount) const {
2455 
2456  vCoins.clear();
2457  Amount nTotal = Amount::zero();
2458  // Either the WALLET_FLAG_AVOID_REUSE flag is not set (in which case we
2459  // always allow), or we default to avoiding, and only in the case where a
2460  // coin control object is provided, and has the avoid address reuse flag set
2461  // to false, do we allow already used addresses
2462  bool allow_used_addresses =
2464  (coinControl && !coinControl->m_avoid_address_reuse);
2465  const int min_depth = {coinControl ? coinControl->m_min_depth
2466  : DEFAULT_MIN_DEPTH};
2467  const int max_depth = {coinControl ? coinControl->m_max_depth
2468  : DEFAULT_MAX_DEPTH};
2469 
2470  std::set<TxId> trusted_parents;
2471  for (const auto &entry : mapWallet) {
2472  const TxId &wtxid = entry.first;
2473  const CWalletTx &wtx = entry.second;
2474 
2475  TxValidationState state;
2477  state)) {
2478  continue;
2479  }
2480 
2481  if (wtx.IsImmatureCoinBase()) {
2482  continue;
2483  }
2484 
2485  int nDepth = wtx.GetDepthInMainChain();
2486  if (nDepth < 0) {
2487  continue;
2488  }
2489 
2490  // We should not consider coins which aren't at least in our mempool.
2491  // It's possible for these to be conflicted via ancestors which we may
2492  // never be able to detect.
2493  if (nDepth == 0 && !wtx.InMempool()) {
2494  continue;
2495  }
2496 
2497  bool safeTx = IsTrusted(wtx, trusted_parents);
2498 
2499  // Bitcoin-ABC: Removed check that prevents consideration of coins from
2500  // transactions that are replacing other transactions. This check based
2501  // on wtx.mapValue.count("replaces_txid") which was not being set
2502  // anywhere.
2503 
2504  // Similarly, we should not consider coins from transactions that have
2505  // been replaced. In the example above, we would want to prevent
2506  // creation of a transaction A' spending an output of A, because if
2507  // transaction B were initially confirmed, conflicting with A and A', we
2508  // wouldn't want to the user to create a transaction D intending to
2509  // replace A', but potentially resulting in a scenario where A, A', and
2510  // D could all be accepted (instead of just B and D, or just A and A'
2511  // like the user would want).
2512 
2513  // Bitcoin-ABC: retained this check as 'replaced_by_txid' is still set
2514  // in the wallet code.
2515  if (nDepth == 0 && wtx.mapValue.count("replaced_by_txid")) {
2516  safeTx = false;
2517  }
2518 
2519  if (fOnlySafe && !safeTx) {
2520  continue;
2521  }
2522 
2523  if (nDepth < min_depth || nDepth > max_depth) {
2524  continue;
2525  }
2526 
2527  for (uint32_t i = 0; i < wtx.tx->vout.size(); i++) {
2528  // Only consider selected coins if add_inputs is false
2529  if (coinControl && !coinControl->m_add_inputs &&
2530  !coinControl->IsSelected(COutPoint(entry.first, i))) {
2531  continue;
2532  }
2533 
2534  if (wtx.tx->vout[i].nValue < nMinimumAmount ||
2535  wtx.tx->vout[i].nValue > nMaximumAmount) {
2536  continue;
2537  }
2538 
2539  const COutPoint outpoint(wtxid, i);
2540 
2541  if (coinControl && coinControl->HasSelected() &&
2542  !coinControl->fAllowOtherInputs &&
2543  !coinControl->IsSelected(outpoint)) {
2544  continue;
2545  }
2546 
2547  if (IsLockedCoin(outpoint)) {
2548  continue;
2549  }
2550 
2551  if (IsSpent(outpoint)) {
2552  continue;
2553  }
2554 
2555  isminetype mine = IsMine(wtx.tx->vout[i]);
2556 
2557  if (mine == ISMINE_NO) {
2558  continue;
2559  }
2560 
2561  if (!allow_used_addresses && IsSpentKey(wtxid, i)) {
2562  continue;
2563  }
2564 
2565  std::unique_ptr<SigningProvider> provider =
2566  GetSolvingProvider(wtx.tx->vout[i].scriptPubKey);
2567 
2568  bool solvable =
2569  provider ? IsSolvable(*provider, wtx.tx->vout[i].scriptPubKey)
2570  : false;
2571  bool spendable =
2572  ((mine & ISMINE_SPENDABLE) != ISMINE_NO) ||
2573  (((mine & ISMINE_WATCH_ONLY) != ISMINE_NO) &&
2574  (coinControl && coinControl->fAllowWatchOnly && solvable));
2575 
2576  vCoins.push_back(
2577  COutput(&wtx, i, nDepth, spendable, solvable, safeTx,
2578  (coinControl && coinControl->fAllowWatchOnly)));
2579 
2580  // Checks the sum amount of all UTXO's.
2581  if (nMinimumSumAmount != MAX_MONEY) {
2582  nTotal += wtx.tx->vout[i].nValue;
2583 
2584  if (nTotal >= nMinimumSumAmount) {
2585  return;
2586  }
2587  }
2588 
2589  // Checks the maximum number of UTXO's.
2590  if (nMaximumCount > 0 && vCoins.size() >= nMaximumCount) {
2591  return;
2592  }
2593  }
2594  }
2595 }
2596 
2597 std::map<CTxDestination, std::vector<COutput>> CWallet::ListCoins() const {
2599 
2600  std::map<CTxDestination, std::vector<COutput>> result;
2601  std::vector<COutput> availableCoins;
2602 
2603  AvailableCoins(availableCoins);
2604 
2605  for (const auto &coin : availableCoins) {
2606  CTxDestination address;
2607  if ((coin.fSpendable ||
2609  coin.fSolvable)) &&
2611  FindNonChangeParentOutput(*coin.tx->tx, coin.i).scriptPubKey,
2612  address)) {
2613  result[address].emplace_back(std::move(coin));
2614  }
2615  }
2616 
2617  std::vector<COutPoint> lockedCoins;
2618  ListLockedCoins(lockedCoins);
2619  // Include watch-only for LegacyScriptPubKeyMan wallets without private keys
2620  const bool include_watch_only =
2623  const isminetype is_mine_filter =
2624  include_watch_only ? ISMINE_WATCH_ONLY : ISMINE_SPENDABLE;
2625  for (const auto &output : lockedCoins) {
2626  auto it = mapWallet.find(output.GetTxId());
2627  if (it != mapWallet.end()) {
2628  int depth = it->second.GetDepthInMainChain();
2629  if (depth >= 0 && output.GetN() < it->second.tx->vout.size() &&
2630  IsMine(it->second.tx->vout[output.GetN()]) == is_mine_filter) {
2631  CTxDestination address;
2632  if (ExtractDestination(
2633  FindNonChangeParentOutput(*it->second.tx, output.GetN())
2634  .scriptPubKey,
2635  address)) {
2636  result[address].emplace_back(
2637  &it->second, output.GetN(), depth, true /* spendable */,
2638  true /* solvable */, false /* safe */);
2639  }
2640  }
2641  }
2642  }
2643 
2644  return result;
2645 }
2646 
2648  int output) const {
2650  const CTransaction *ptx = &tx;
2651  int n = output;
2652  while (IsChange(ptx->vout[n]) && ptx->vin.size() > 0) {
2653  const COutPoint &prevout = ptx->vin[0].prevout;
2654  auto it = mapWallet.find(prevout.GetTxId());
2655  if (it == mapWallet.end() ||
2656  it->second.tx->vout.size() <= prevout.GetN() ||
2657  !IsMine(it->second.tx->vout[prevout.GetN()])) {
2658  break;
2659  }
2660  ptx = it->second.tx.get();
2661  n = prevout.GetN();
2662  }
2663  return ptx->vout[n];
2664 }
2665 
2667  const Amount nTargetValue, const CoinEligibilityFilter &eligibility_filter,
2668  std::vector<OutputGroup> groups, std::set<CInputCoin> &setCoinsRet,
2670  bool &bnb_used) const {
2671  setCoinsRet.clear();
2672  nValueRet = Amount::zero();
2673 
2674  std::vector<OutputGroup> utxo_pool;
2676  // Get long term estimate
2677  CCoinControl temp;
2678  temp.m_confirm_target = 1008;
2679  CFeeRate long_term_feerate = GetMinimumFeeRate(*this, temp);
2680 
2681  // Calculate cost of change
2682  Amount cost_of_change = chain().relayDustFee().GetFee(
2686 
2687  // Filter by the min conf specs and add to utxo_pool and calculate
2688  // effective value
2689  for (OutputGroup &group : groups) {
2690  if (!group.EligibleForSpending(eligibility_filter)) {
2691  continue;
2692  }
2693 
2695  // Set the effective feerate to 0 as we don't want to use the
2696  // effective value since the fees will be deducted from the
2697  // output
2698  group.SetFees(CFeeRate(Amount::zero()) /* effective_feerate */,
2699  long_term_feerate);
2700  } else {
2701  group.SetFees(coin_selection_params.effective_fee,
2702  long_term_feerate);
2703  }
2704 
2705  OutputGroup pos_group = group.GetPositiveOnlyGroup();
2706  if (pos_group.effective_value > Amount::zero()) {
2707  utxo_pool.push_back(pos_group);
2708  }
2709  }
2710  // Calculate the fees for things that aren't inputs
2713  bnb_used = true;
2714  return SelectCoinsBnB(utxo_pool, nTargetValue, cost_of_change,
2715  setCoinsRet, nValueRet, not_input_fees);
2716  } else {
2717  // Filter by the min conf specs and add to utxo_pool
2718  for (const OutputGroup &group : groups) {
2719  if (!group.EligibleForSpending(eligibility_filter)) {
2720  continue;
2721  }
2722  utxo_pool.push_back(group);
2723  }
2724  bnb_used = false;
2725  return KnapsackSolver(nTargetValue, utxo_pool, setCoinsRet, nValueRet);
2726  }
2727 }
2728 
2729 bool CWallet::SelectCoins(const std::vector<COutput> &vAvailableCoins,
2730  const Amount nTargetValue,
2731  std::set<CInputCoin> &setCoinsRet, Amount &nValueRet,
2732  const CCoinControl &coin_control,
2734  bool &bnb_used) const {
2735  std::vector<COutput> vCoins(vAvailableCoins);
2736  Amount value_to_select = nTargetValue;
2737 
2738  // Default to bnb was not used. If we use it, we set it later
2739  bnb_used = false;
2740 
2741  // coin control -> return all selected outputs (we want all selected to go
2742  // into the transaction for sure)
2743  if (coin_control.HasSelected() && !coin_control.fAllowOtherInputs) {
2744  for (const COutput &out : vCoins) {
2745  if (!out.fSpendable) {
2746  continue;
2747  }
2748 
2749  nValueRet += out.tx->tx->vout[out.i].nValue;
2750  setCoinsRet.insert(out.GetInputCoin());
2751  }
2752 
2753  return (nValueRet >= nTargetValue);
2754  }
2755 
2756  // Calculate value from preset inputs and store them.
2757  std::set<CInputCoin> setPresetCoins;
2758  Amount nValueFromPresetInputs = Amount::zero();
2759 
2760  std::vector<COutPoint> vPresetInputs;
2761  coin_control.ListSelected(vPresetInputs);
2762 
2763  for (const COutPoint &outpoint : vPresetInputs) {
2764  std::map<TxId, CWalletTx>::const_iterator it =
2765  mapWallet.find(outpoint.GetTxId());
2766  if (it != mapWallet.end()) {
2767  const CWalletTx &wtx = it->second;
2768  // Clearly invalid input, fail
2769  if (wtx.tx->vout.size() <= outpoint.GetN()) {
2770  return false;
2771  }
2772  // Just to calculate the marginal byte size
2773  CInputCoin coin(wtx.tx, outpoint.GetN(),
2774  wtx.GetSpendSize(outpoint.GetN(), false));
2775  nValueFromPresetInputs += coin.txout.nValue;
2776  if (coin.m_input_bytes <= 0) {
2777  // Not solvable, can't estimate size for fee
2778  return false;
2779  }
2780  coin.effective_value =
2781  coin.txout.nValue -
2784  value_to_select -= coin.effective_value;
2785  } else {
2786  value_to_select -= coin.txout.nValue;
2787  }
2788  setPresetCoins.insert(coin);
2789  } else {
2790  return false; // TODO: Allow non-wallet inputs
2791  }
2792  }
2793 
2794  // Remove preset inputs from vCoins
2795  for (std::vector<COutput>::iterator it = vCoins.begin();
2796  it != vCoins.end() && coin_control.HasSelected();) {
2797  if (setPresetCoins.count(it->GetInputCoin())) {
2798  it = vCoins.erase(it);
2799  } else {
2800  ++it;
2801  }
2802  }
2803 
2804  size_t max_ancestors{0};
2805  size_t max_descendants{0};
2806  chain().getPackageLimits(max_ancestors, max_descendants);
2807  bool fRejectLongChains = gArgs.GetBoolArg(
2808  "-walletrejectlongchains", DEFAULT_WALLET_REJECT_LONG_CHAINS);
2809 
2810  // form groups from remaining coins; note that preset coins will not
2811  // automatically have their associated (same address) coins included
2812  if (coin_control.m_avoid_partial_spends &&
2813  vCoins.size() > OUTPUT_GROUP_MAX_ENTRIES) {
2814  // Cases where we have 11+ outputs all pointing to the same destination
2815  // may result in privacy leaks as they will potentially be
2816  // deterministically sorted. We solve that by explicitly shuffling the
2817  // outputs before processing
2818  Shuffle(vCoins.begin(), vCoins.end(), FastRandomContext());
2819  }
2820 
2821  std::vector<OutputGroup> groups = GroupOutputs(
2822  vCoins, !coin_control.m_avoid_partial_spends, max_ancestors);
2823 
2824  bool res =
2825  value_to_select <= Amount::zero() ||
2826  SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(1, 6, 0),
2827  groups, setCoinsRet, nValueRet,
2828  coin_selection_params, bnb_used) ||
2829  SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(1, 1, 0),
2830  groups, setCoinsRet, nValueRet,
2831  coin_selection_params, bnb_used) ||
2833  SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(0, 1, 2),
2834  groups, setCoinsRet, nValueRet,
2835  coin_selection_params, bnb_used)) ||
2838  value_to_select,
2839  CoinEligibilityFilter(0, 1, std::min((size_t)4, max_ancestors / 3),
2840  std::min((size_t)4, max_descendants / 3)),
2841  groups, setCoinsRet, nValueRet, coin_selection_params,
2842  bnb_used)) ||
2844  SelectCoinsMinConf(value_to_select,
2845  CoinEligibilityFilter(0, 1, max_ancestors / 2,
2846  max_descendants / 2),
2847  groups, setCoinsRet, nValueRet,
2848  coin_selection_params, bnb_used)) ||
2850  SelectCoinsMinConf(value_to_select,
2851  CoinEligibilityFilter(0, 1, max_ancestors - 1,
2852  max_descendants - 1),
2853  groups, setCoinsRet, nValueRet,
2854  coin_selection_params, bnb_used)) ||
2855  (m_spend_zero_conf_change && !fRejectLongChains &&
2857  value_to_select,
2858  CoinEligibilityFilter(0, 1, std::numeric_limits<uint64_t>::max()),
2859  groups, setCoinsRet, nValueRet, coin_selection_params, bnb_used));
2860 
2861  // Because SelectCoinsMinConf clears the setCoinsRet, we now add the
2862  // possible inputs to the coinset.
2863  util::insert(setCoinsRet, setPresetCoins);
2864 
2865  // Add preset inputs to the total value selected.
2866  nValueRet += nValueFromPresetInputs;
2867 
2868  return res;
2869 }
2870 
2873 
2874  // Build coins map
2875  std::map<COutPoint, Coin> coins;
2876  for (auto &input : tx.vin) {
2877  auto mi = mapWallet.find(input.prevout.GetTxId());
2878  if (mi == mapWallet.end() ||
2879  input.prevout.GetN() >= mi->second.tx->vout.size()) {
2880  return false;
2881  }
2882  const CWalletTx &wtx = mi->second;
2883  coins[input.prevout] =
2884  Coin(wtx.tx->vout[input.prevout.GetN()], wtx.m_confirm.block_height,
2885  wtx.IsCoinBase());
2886  }
2887  std::map<int, std::string> input_errors;
2888  return SignTransaction(tx, coins, SigHashType().withForkId(), input_errors);
2889 }
2890 
2892  const std::map<COutPoint, Coin> &coins,
2893  SigHashType sighash,
2894  std::map<int, std::string> &input_errors) const {
2895  // Try to sign with all ScriptPubKeyMans
2896  for (ScriptPubKeyMan *spk_man : GetAllScriptPubKeyMans()) {
2897  // spk_man->SignTransaction will return true if the transaction is
2898  // complete, so we can exit early and return true if that happens
2899  if (spk_man->SignTransaction(tx, coins, sighash, input_errors)) {
2900  return true;
2901  }
2902  }
2903 
2904  // At this point, one input was not fully signed otherwise we would have
2905  // exited already
2906 
2907  // When there are no available providers for the remaining inputs, use the
2908  // legacy provider so we can get proper error messages.
2909  auto legacy_spk_man = GetLegacyScriptPubKeyMan();
2910  if (legacy_spk_man &&
2911  legacy_spk_man->SignTransaction(tx, coins, sighash, input_errors)) {
2912  return true;
2913  }
2914 
2915  return false;
2916 }
2917 
2919  bool &complete, SigHashType sighash_type,
2920  bool sign, bool bip32derivs) const {
2921  LOCK(cs_wallet);
2922  // Get all of the previous transactions
2923  for (size_t i = 0; i < psbtx.tx->vin.size(); ++i) {
2924  const CTxIn &txin = psbtx.tx->vin[i];
2925  PSBTInput &input = psbtx.inputs.at(i);
2926 
2927  if (PSBTInputSigned(input)) {
2928  continue;
2929  }
2930 
2931  // If we have no utxo, grab it from the wallet.
2932  if (input.utxo.IsNull()) {
2933  const TxId &txid = txin.prevout.GetTxId();
2934  const auto it = mapWallet.find(txid);
2935  if (it != mapWallet.end()) {
2936  const CWalletTx &wtx = it->second;
2937  CTxOut utxo = wtx.tx->vout[txin.prevout.GetN()];
2938  // Update UTXOs from the wallet.
2939  input.utxo = utxo;
2940  }
2941  }
2942  }
2943 
2944  // Fill in information from ScriptPubKeyMans
2945  for (ScriptPubKeyMan *spk_man : GetAllScriptPubKeyMans()) {
2946  TransactionError res =
2947  spk_man->FillPSBT(psbtx, sighash_type, sign, bip32derivs);
2948  if (res != TransactionError::OK) {
2949  return res;
2950  }
2951  }
2952 
2953  // Complete if every input is now signed
2954  complete = true;
2955  for (const auto &input : psbtx.inputs) {
2956  complete &= PSBTInputSigned(input);
2957  }
2958 
2959  return TransactionError::OK;
2960 }
2961 
2962 SigningResult CWallet::SignMessage(const std::string &message,
2963  const PKHash &pkhash,
2964  std::string &str_sig) const {
2965  SignatureData sigdata;
2966  CScript script_pub_key = GetScriptForDestination(pkhash);
2967  for (const auto &spk_man_pair : m_spk_managers) {
2968  if (spk_man_pair.second->CanProvide(script_pub_key, sigdata)) {
2969  return spk_man_pair.second->SignMessage(message, pkhash, str_sig);
2970  }
2971  }
2973 }
2974 
2976  int &nChangePosInOut, bilingual_str &error,
2977  bool lockUnspents,
2978  const std::set<int> &setSubtractFeeFromOutputs,
2979  CCoinControl coinControl) {
2980  std::vector<CRecipient> vecSend;
2981 
2982  // Turn the txout set into a CRecipient vector.
2983  for (size_t idx = 0; idx < tx.vout.size(); idx++) {
2984  const CTxOut &txOut = tx.vout[idx];
2985  CRecipient recipient = {txOut.scriptPubKey, txOut.nValue,
2986  setSubtractFeeFromOutputs.count(idx) == 1};
2987  vecSend.push_back(recipient);
2988  }
2989 
2990  coinControl.fAllowOtherInputs = true;
2991 
2992  for (const CTxIn &txin : tx.vin) {
2993  coinControl.Select(txin.prevout);
2994  }
2995 
2996  // Acquire the locks to prevent races to the new locked unspents between the
2997  // CreateTransaction call and LockCoin calls (when lockUnspents is true).
2998  LOCK(cs_wallet);
2999 
3000  CTransactionRef tx_new;
3001  if (!CreateTransaction(vecSend, tx_new, nFeeRet, nChangePosInOut, error,
3002  coinControl, false)) {
3003  return false;
3004  }
3005 
3006  if (nChangePosInOut != -1) {
3007  tx.vout.insert(tx.vout.begin() + nChangePosInOut,
3008  tx_new->vout[nChangePosInOut]);
3009  }
3010 
3011  // Copy output sizes from new transaction; they may have had the fee
3012  // subtracted from them.
3013  for (size_t idx = 0; idx < tx.vout.size(); idx++) {
3014  tx.vout[idx].nValue = tx_new->vout[idx].nValue;
3015  }
3016 
3017  // Add new txins (keeping original txin scriptSig/order)
3018  for (const CTxIn &txin : tx_new->vin) {
3019  if (!coinControl.IsSelected(txin.prevout)) {
3020  tx.vin.push_back(txin);
3021  }
3022  if (lockUnspents) {
3023  LockCoin(txin.prevout);
3024  }
3025  }
3026 
3027  return true;
3028 }
3029 
3031  const BlockHash &block_hash) {
3032  if (chain.isInitialBlockDownload()) {
3033  return false;
3034  }
3035 
3036  // in seconds
3037  constexpr int64_t MAX_ANTI_FEE_SNIPING_TIP_AGE = 8 * 60 * 60;
3038  int64_t block_time;
3039  CHECK_NONFATAL(chain.findBlock(block_hash, FoundBlock().time(block_time)));
3040  if (block_time < (GetTime() - MAX_ANTI_FEE_SNIPING_TIP_AGE)) {
3041  return false;
3042  }
3043  return true;
3044 }
3045 
3051  const BlockHash &block_hash,
3052  int block_height) {
3053  uint32_t locktime;
3054  // Discourage fee sniping.
3055  //
3056  // For a large miner the value of the transactions in the best block and
3057  // the mempool can exceed the cost of deliberately attempting to mine two
3058  // blocks to orphan the current best block. By setting nLockTime such that
3059  // only the next block can include the transaction, we discourage this
3060  // practice as the height restricted and limited blocksize gives miners
3061  // considering fee sniping fewer options for pulling off this attack.
3062  //
3063  // A simple way to think about this is from the wallet's point of view we
3064  // always want the blockchain to move forward. By setting nLockTime this
3065  // way we're basically making the statement that we only want this
3066  // transaction to appear in the next block; we don't want to potentially
3067  // encourage reorgs by allowing transactions to appear at lower heights
3068  // than the next block in forks of the best chain.
3069  //
3070  // Of course, the subsidy is high enough, and transaction volume low
3071  // enough, that fee sniping isn't a problem yet, but by implementing a fix
3072  // now we ensure code won't be written that makes assumptions about
3073  // nLockTime that preclude a fix later.
3074  if (IsCurrentForAntiFeeSniping(chain, block_hash)) {
3075  locktime = block_height;
3076 
3077  // Secondly occasionally randomly pick a nLockTime even further back, so
3078  // that transactions that are delayed after signing for whatever reason,
3079  // e.g. high-latency mix networks and some CoinJoin implementations,
3080  // have better privacy.
3081  if (GetRandInt(10) == 0) {
3082  locktime = std::max(0, int(locktime) - GetRandInt(100));
3083  }
3084  } else {
3085  // If our chain is lagging behind, we can't discourage fee sniping nor
3086  // help the privacy of high-latency transactions. To avoid leaking a
3087  // potentially unique "nLockTime fingerprint", set nLockTime to a
3088  // constant.
3089  locktime = 0;
3090  }
3091  assert(locktime < LOCKTIME_THRESHOLD);
3092  return locktime;
3093 }
3094 
3095 OutputType
3096 CWallet::TransactionChangeType(const std::optional<OutputType> &change_type,
3097  const std::vector<CRecipient> &vecSend) {
3098  // If -changetype is specified, always use that change type.
3099  if (change_type) {
3100  return *change_type;
3101  }
3102 
3103  // if m_default_address_type is legacy, use legacy address as change.
3105  return OutputType::LEGACY;
3106  }
3107 
3108  // else use m_default_address_type for change
3109  return m_default_address_type;
3110 }
3111 
3112 bool CWallet::CreateTransactionInternal(const std::vector<CRecipient> &vecSend,
3113  CTransactionRef &tx, Amount &nFeeRet,
3114  int &nChangePosInOut,
3116  const CCoinControl &coin_control,
3117  bool sign) {
3118  Amount nValue = Amount::zero();
3119  const OutputType change_type = TransactionChangeType(
3120  coin_control.m_change_type ? *coin_control.m_change_type
3122  vecSend);
3123  ReserveDestination reservedest(this, change_type);
3124  int nChangePosRequest = nChangePosInOut;
3125  unsigned int nSubtractFeeFromAmount = 0;
3126  for (const auto &recipient : vecSend) {
3127  if (nValue < Amount::zero() || recipient.nAmount < Amount::zero()) {
3128  error = _("Transaction amounts must not be negative");
3129  return false;
3130  }
3131 
3132  nValue += recipient.nAmount;
3133 
3134  if (recipient.fSubtractFeeFromAmount) {
3135  nSubtractFeeFromAmount++;
3136  }
3137  }
3138 
3139  if (vecSend.empty()) {
3140  error = _("Transaction must have at least one recipient");
3141  return false;
3142  }
3143 
3144  CMutableTransaction txNew;
3145 
3146  {
3147  std::set<CInputCoin> setCoins;
3148  LOCK(cs_wallet);
3151  std::vector<COutput> vAvailableCoins;
3152  AvailableCoins(vAvailableCoins, true, &coin_control);
3153  // Parameters for coin selection, init with dummy
3155 
3156  // Create change script that will be used if we need change
3157  // TODO: pass in scriptChange instead of reservedest so
3158  // change transaction isn't always pay-to-bitcoin-address
3159  CScript scriptChange;
3160 
3161  // coin control: send change to custom address
3162  if (!boost::get<CNoDestination>(&coin_control.destChange)) {
3163  scriptChange = GetScriptForDestination(coin_control.destChange);
3164 
3165  // no coin control: send change to newly generated address
3166  } else {
3167  // Note: We use a new key here to keep it from being obvious
3168  // which side is the change.
3169  // The drawback is that by not reusing a previous key, the
3170  // change may be lost if a backup is restored, if the backup
3171  // doesn't have the new private key for the change. If we
3172  // reused the old key, it would be possible to add code to look
3173  // for and rediscover unknown transactions that were written
3174  // with keys of ours to recover post-backup change.
3175 
3176  // Reserve a new key pair from key pool. If it fails, provide a
3177  // dummy destination in case we don't need change.
3178  CTxDestination dest;
3179  if (!reservedest.GetReservedDestination(dest, true)) {
3180  error = _("Transaction needs a change address, but we can't "
3181  "generate it. Please call keypoolrefill first.");
3182  }
3183 
3184  scriptChange = GetScriptForDestination(dest);
3185  // A valid destination implies a change script (and
3186  // vice-versa). An empty change script will abort later, if the
3187  // change keypool ran out, but change is required.
3188  CHECK_NONFATAL(IsValidDestination(dest) != scriptChange.empty());
3189  }
3190  CTxOut change_prototype_txout(Amount::zero(), scriptChange);
3192  GetSerializeSize(change_prototype_txout);
3193 
3194  // Get the fee rate to use effective values in coin selection
3195  CFeeRate nFeeRateNeeded = GetMinimumFeeRate(*this, coin_control);
3196  // Do not, ever, assume that it's fine to change the fee rate if the
3197  // user has explicitly provided one
3198  if (coin_control.m_feerate &&
3199  nFeeRateNeeded > *coin_control.m_feerate) {
3200  error = strprintf(_("Fee rate (%s) is lower than the minimum fee "
3201  "rate setting (%s)"),
3202  coin_control.m_feerate->ToString(),
3203  nFeeRateNeeded.ToString());
3204  return false;
3205  }
3206 
3207  nFeeRet = Amount::zero();
3208  bool pick_new_inputs = true;
3209  Amount nValueIn = Amount::zero();
3210 
3211  // BnB selector is the only selector used when this is true.
3212  // That should only happen on the first pass through the loop.
3214  // If we are doing subtract fee from recipient, don't use effective
3215  // values
3217  nSubtractFeeFromAmount != 0;
3218  // Start with no fee and loop until there is enough fee
3219  while (true) {
3220  nChangePosInOut = nChangePosRequest;
3221  txNew.vin.clear();
3222  txNew.vout.clear();
3223  bool fFirst = true;
3224 
3225  Amount nValueToSelect = nValue;
3226  if (nSubtractFeeFromAmount == 0) {
3227  nValueToSelect += nFeeRet;
3228  }
3229 
3230  // vouts to the payees
3232  // Static size overhead + outputs vsize. 4 nVersion, 4
3233  // nLocktime, 1 input count, 1 output count
3235  }
3236  // vouts to the payees
3237  for (const auto &recipient : vecSend) {
3238  CTxOut txout(recipient.nAmount, recipient.scriptPubKey);
3239 
3240  if (recipient.fSubtractFeeFromAmount) {
3241  assert(nSubtractFeeFromAmount != 0);
3242  // Subtract fee equally from each selected recipient.
3243  txout.nValue -= nFeeRet / int(nSubtractFeeFromAmount);
3244 
3245  // First receiver pays the remainder not divisible by output
3246  // count.
3247  if (fFirst) {
3248  fFirst = false;
3249  txout.nValue -= nFeeRet % int(nSubtractFeeFromAmount);
3250  }
3251  }
3252 
3253  // Include the fee cost for outputs. Note this is only used for
3254  // BnB right now
3258  }
3259 
3260  if (IsDust(txout, chain().relayDustFee())) {
3261  if (recipient.fSubtractFeeFromAmount &&
3262  nFeeRet > Amount::zero()) {
3263  if (txout.nValue < Amount::zero()) {
3264  error = _("The transaction amount is too small to "
3265  "pay the fee");
3266  } else {
3267  error = _("The transaction amount is too small to "
3268  "send after the fee has been deducted");
3269  }
3270  } else {
3271  error = _("Transaction amount too small");
3272  }
3273 
3274  return false;
3275  }
3276 
3277  txNew.vout.push_back(txout);
3278  }
3279 
3280  // Choose coins to use
3281  bool bnb_used = false;
3282  if (pick_new_inputs) {
3283  nValueIn = Amount::zero();
3284  setCoins.clear();
3285  int change_spend_size = CalculateMaximumSignedInputSize(
3286  change_prototype_txout, this);
3287  // If the wallet doesn't know how to sign change output, assume
3288  // p2pkh as lower-bound to allow BnB to do it's thing
3289  if (change_spend_size == -1) {
3292  } else {
3294  size_t(change_spend_size);
3295  }
3296  coin_selection_params.effective_fee = nFeeRateNeeded;
3297  if (!SelectCoins(vAvailableCoins, nValueToSelect, setCoins,
3298  nValueIn, coin_control, coin_selection_params,
3299  bnb_used)) {
3300  // If BnB was used, it was the first pass. No longer the
3301  // first pass and continue loop with knapsack.
3302  if (bnb_used) {
3304  continue;
3305  } else {
3306  error = _("Insufficient funds");
3307  return false;
3308  }
3309  }
3310  } else {
3311  bnb_used = false;
3312  }
3313 
3314  const Amount nChange = nValueIn - nValueToSelect;
3315  if (nChange > Amount::zero()) {
3316  // Fill a vout to ourself.
3317  CTxOut newTxOut(nChange, scriptChange);
3318 
3319  // Never create dust outputs; if we would, just add the dust to
3320  // the fee.
3321  // The nChange when BnB is used is always going to go to fees.
3322  if (IsDust(newTxOut, chain().relayDustFee()) || bnb_used) {
3323  nChangePosInOut = -1;
3324  nFeeRet += nChange;
3325  } else {
3326  if (nChangePosInOut == -1) {
3327  // Insert change txn at random position:
3328  nChangePosInOut = GetRandInt(txNew.vout.size() + 1);
3329  } else if ((unsigned int)nChangePosInOut >
3330  txNew.vout.size()) {
3331  error = _("Change index out of range");
3332  return false;
3333  }
3334 
3335  std::vector<CTxOut>::iterator position =
3336  txNew.vout.begin() + nChangePosInOut;
3337  txNew.vout.insert(position, newTxOut);
3338  }
3339  } else {
3340  nChangePosInOut = -1;
3341  }
3342 
3343  // Dummy fill vin for maximum size estimation
3344  //
3345  for (const auto &coin : setCoins) {
3346  txNew.vin.push_back(CTxIn(coin.outpoint, CScript()));
3347  }
3348 
3349  CTransaction txNewConst(txNew);
3350  int nBytes = CalculateMaximumSignedTxSize(
3351  txNewConst, this, coin_control.fAllowWatchOnly);
3352  if (nBytes < 0) {
3353  error = _("Signing transaction failed");
3354  return false;
3355  }
3356 
3357  Amount nFeeNeeded = GetMinimumFee(*this, nBytes, coin_control);
3358 
3359  if (nFeeRet >= nFeeNeeded) {
3360  // Reduce fee to only the needed amount if possible. This
3361  // prevents potential overpayment in fees if the coins selected
3362  // to meet nFeeNeeded result in a transaction that requires less
3363  // fee than the prior iteration.
3364 
3365  // If we have no change and a big enough excess fee, then try to
3366  // construct transaction again only without picking new inputs.
3367  // We now know we only need the smaller fee (because of reduced
3368  // tx size) and so we should add a change output. Only try this
3369  // once.
3370  if (nChangePosInOut == -1 && nSubtractFeeFromAmount == 0 &&
3371  pick_new_inputs) {
3372  // Add 2 as a buffer in case increasing # of outputs changes
3373  // compact size
3374  unsigned int tx_size_with_change =
3376  Amount fee_needed_with_change =
3377  GetMinimumFee(*this, tx_size_with_change, coin_control);
3378  Amount minimum_value_for_change = GetDustThreshold(
3379  change_prototype_txout, chain().relayDustFee());
3380  if (nFeeRet >=
3381  fee_needed_with_change + minimum_value_for_change) {
3382  pick_new_inputs = false;
3383  nFeeRet = fee_needed_with_change;
3384  continue;
3385  }
3386  }
3387 
3388  // If we have change output already, just increase it
3389  if (nFeeRet > nFeeNeeded && nChangePosInOut != -1 &&
3390  nSubtractFeeFromAmount == 0) {
3391  Amount extraFeePaid = nFeeRet - nFeeNeeded;
3392  std::vector<CTxOut>::iterator change_position =
3393  txNew.vout.begin() + nChangePosInOut;
3394  change_position->nValue += extraFeePaid;
3395  nFeeRet -= extraFeePaid;
3396  }
3397 
3398  // Done, enough fee included.
3399  break;
3400  } else if (!pick_new_inputs) {
3401  // This shouldn't happen, we should have had enough excess fee
3402  // to pay for the new output and still meet nFeeNeeded.
3403  // Or we should have just subtracted fee from recipients and
3404  // nFeeNeeded should not have changed.
3405  error = _("Transaction fee and change calculation failed");
3406  return false;
3407  }
3408 
3409  // Try to reduce change to include necessary fee.
3410  if (nChangePosInOut != -1 && nSubtractFeeFromAmount == 0) {
3411  Amount additionalFeeNeeded = nFeeNeeded - nFeeRet;
3412  std::vector<CTxOut>::iterator change_position =
3413  txNew.vout.begin() + nChangePosInOut;
3414  // Only reduce change if remaining amount is still a large
3415  // enough output.
3416  if (change_position->nValue >=
3417  MIN_FINAL_CHANGE + additionalFeeNeeded) {
3418  change_position->nValue -= additionalFeeNeeded;
3419  nFeeRet += additionalFeeNeeded;
3420  // Done, able to increase fee from change.
3421  break;
3422  }
3423  }
3424 
3425  // If subtracting fee from recipients, we now know what fee we
3426  // need to subtract, we have no reason to reselect inputs.
3427  if (nSubtractFeeFromAmount > 0) {
3428  pick_new_inputs = false;
3429  }
3430 
3431  // Include more fee and try again.
3432  nFeeRet = nFeeNeeded;
3434  continue;
3435  }
3436 
3437  // Give up if change keypool ran out and change is required
3438  if (scriptChange.empty() && nChangePosInOut != -1) {
3439  return false;
3440  }
3441 
3442  // Shuffle selected coins and fill in final vin
3443  txNew.vin.clear();
3444  std::vector<CInputCoin> selected_coins(setCoins.begin(),
3445  setCoins.end());
3446  Shuffle(selected_coins.begin(), selected_coins.end(),
3447  FastRandomContext());
3448 
3449  // Note how the sequence number is set to non-maxint so that
3450  // the nLockTime set above actually works.
3451  for (const auto &coin : selected_coins) {
3452  txNew.vin.push_back(
3453  CTxIn(coin.outpoint, CScript(),
3454  std::numeric_limits<uint32_t>::max() - 1));
3455  }
3456 
3457  if (sign && !SignTransaction(txNew)) {
3458  error = _("Signing transaction failed");
3459  return false;
3460  }
3461 
3462  // Return the constructed transaction data.
3463  tx = MakeTransactionRef(std::move(txNew));
3464 
3465  // Limit size.
3466  if (tx->GetTotalSize() > MAX_STANDARD_TX_SIZE) {
3467  error = _("Transaction too large");
3468  return false;
3469  }
3470  }
3471 
3472  if (nFeeRet > m_default_max_tx_fee) {
3474  return false;
3475  }
3476 
3477  if (gArgs.GetBoolArg("-walletrejectlongchains",
3479  // Lastly, ensure this tx will pass the mempool's chain limits
3480  if (!chain().checkChainLimits(tx)) {
3481  error = _("Transaction has too long of a mempool chain");
3482  return false;
3483  }
3484  }
3485 
3486  // Before we return success, we assume any change key will be used to
3487  // prevent accidental re-use.
3488  reservedest.KeepDestination();
3489 
3490  return true;
3491 }
3492 
3493 bool CWallet::CreateTransaction(const std::vector<CRecipient> &vecSend,
3494  CTransactionRef &tx, Amount &nFeeRet,
3495  int &nChangePosInOut, bilingual_str &error,
3496  const CCoinControl &coin_control, bool sign) {
3497  int nChangePosIn = nChangePosInOut;
3498  CTransactionRef tx2 = tx;
3499  bool res = CreateTransactionInternal(vecSend, tx, nFeeRet, nChangePosInOut,
3500  error, coin_control, sign);
3501  // try with avoidpartialspends unless it's enabled already
3502  if (res &&
3503  nFeeRet >
3504  Amount::zero() /* 0 means non-functional fee rate estimation */
3505  && m_max_aps_fee > (-1 * SATOSHI) &&
3506  !coin_control.m_avoid_partial_spends) {
3507  CCoinControl tmp_cc = coin_control;
3508  tmp_cc.m_avoid_partial_spends = true;
3509  Amount nFeeRet2;
3510  int nChangePosInOut2 = nChangePosIn;
3511  // fired and forgotten; if an error occurs, we discard the results
3512  bilingual_str error2;
3513  if (CreateTransactionInternal(vecSend, tx2, nFeeRet2, nChangePosInOut2,
3514  error2, tmp_cc, sign)) {
3515  // if fee of this alternative one is within the range of the max
3516  // fee, we use this one
3517  const bool use_aps = nFeeRet2 <= nFeeRet + m_max_aps_fee;
3519  "Fee non-grouped = %lld, grouped = %lld, using %s\n", nFeeRet,
3520  nFeeRet2, use_aps ? "grouped" : "non-grouped");
3521  if (use_aps) {
3522  tx = tx2;
3523  nFeeRet = nFeeRet2;
3524  nChangePosInOut = nChangePosInOut2;
3525  }
3526  }
3527  }
3528  return res;
3529 }
3530 
3532  CTransactionRef tx, mapValue_t mapValue,
3533  std::vector<std::pair<std::string, std::string>> orderForm) {
3534  LOCK(cs_wallet);
3535 
3536  WalletLogPrintfToBeContinued("CommitTransaction:\n%s", tx->ToString());
3537 
3538  // Add tx to wallet, because if it has change it's also ours, otherwise just
3539  // for transaction history.
3540  AddToWallet(tx, {}, [&](CWalletTx &wtx, bool new_tx) {
3541  CHECK_NONFATAL(wtx.mapValue.empty());
3542  CHECK_NONFATAL(wtx.vOrderForm.empty());
3543  wtx.mapValue = std::move(mapValue);
3544  wtx.vOrderForm = std::move(orderForm);
3545  wtx.fTimeReceivedIsTxTime = true;
3546  wtx.fFromMe = true;
3547  return true;
3548  });
3549 
3550  // Notify that old coins are spent.
3551  for (const CTxIn &txin : tx->vin) {
3552  CWalletTx &coin = mapWallet.at(txin.prevout.GetTxId());
3553  coin.MarkDirty();
3554  NotifyTransactionChanged(this, coin.GetId(), CT_UPDATED);
3555  }
3556 
3557  // Get the inserted-CWalletTx from mapWallet so that the
3558  // fInMempool flag is cached properly
3559  CWalletTx &wtx = mapWallet.at(tx->GetId());
3560 
3561  if (!fBroadcastTransactions) {
3562  // Don't submit tx to the mempool
3563  return;
3564  }
3565 
3566  std::string err_string;
3567  if (!wtx.SubmitMemoryPoolAndRelay(err_string, true)) {
3568  WalletLogPrintf("CommitTransaction(): Transaction cannot be broadcast "
3569  "immediately, %s\n",
3570  err_string);
3571  // TODO: if we expect the failure to be long term or permanent, instead
3572  // delete wtx from the wallet and return failure.
3573  }
3574 }
3575 
3576 DBErrors CWallet::LoadWallet(bool &fFirstRunRet) {
3577  LOCK(cs_wallet);
3578 
3579  fFirstRunRet = false;
3580  DBErrors nLoadWalletRet = WalletBatch(*database).LoadWallet(this);
3581  if (nLoadWalletRet == DBErrors::NEED_REWRITE) {
3582  if (database->Rewrite("\x04pool")) {
3583  for (const auto &spk_man_pair : m_spk_managers) {
3584  spk_man_pair.second->RewriteDB();
3585  }
3586  }
3587  }
3588 
3589  // This wallet is in its first run if there are no ScriptPubKeyMans and it
3590  // isn't blank or no privkeys
3591  fFirstRunRet = m_spk_managers.empty() &&
3594  if (fFirstRunRet) {
3595  assert(m_external_spk_managers.empty());
3596  assert(m_internal_spk_managers.empty());
3597  }
3598 
3599  if (nLoadWalletRet != DBErrors::LOAD_OK) {
3600  return nLoadWalletRet;
3601  }
3602 
3603  return DBErrors::LOAD_OK;
3604 }
3605 
3606 DBErrors CWallet::ZapSelectTx(std::vector<TxId> &txIdsIn,
3607  std::vector<TxId> &txIdsOut) {
3609  DBErrors nZapSelectTxRet =
3610  WalletBatch(*database).ZapSelectTx(txIdsIn, txIdsOut);
3611  for (const TxId &txid : txIdsOut) {
3612  const auto &it = mapWallet.find(txid);
3613  wtxOrdered.erase(it->second.m_it_wtxOrdered);
3614  for (const auto &txin : it->second.tx->vin) {
3615  mapTxSpends.erase(txin.prevout);
3616  }
3617  mapWallet.erase(it);
3618  NotifyTransactionChanged(this, txid, CT_DELETED);
3619  }
3620 
3621  if (nZapSelectTxRet == DBErrors::NEED_REWRITE) {
3622  if (database->Rewrite("\x04pool")) {
3623  for (const auto &spk_man_pair : m_spk_managers) {
3624  spk_man_pair.second->RewriteDB();
3625  }
3626  }
3627  }
3628 
3629  if (nZapSelectTxRet != DBErrors::LOAD_OK) {
3630  return nZapSelectTxRet;
3631  }
3632 
3633  MarkDirty();
3634 
3635  return DBErrors::LOAD_OK;
3636 }
3637 
3639  const CTxDestination &address,
3640  const std::string &strName,
3641  const std::string &strPurpose) {
3642  bool fUpdated = false;
3643  bool is_mine;
3644  {
3645  LOCK(cs_wallet);
3646  std::map<CTxDestination, CAddressBookData>::iterator mi =
3647  m_address_book.find(address);
3648  fUpdated = (mi != m_address_book.end() && !mi->second.IsChange());
3649  m_address_book[address].SetLabel(strName);
3650  // Update purpose only if requested.
3651  if (!strPurpose.empty()) {
3652  m_address_book[address].purpose = strPurpose;
3653  }
3654  is_mine = IsMine(address) != ISMINE_NO;
3655  }
3656 
3657  NotifyAddressBookChanged(this, address, strName, is_mine, strPurpose,
3658  (fUpdated ? CT_UPDATED : CT_NEW));
3659  if (!strPurpose.empty() && !batch.WritePurpose(address, strPurpose)) {
3660  return false;
3661  }
3662  return batch.WriteName(address, strName);
3663 }
3664 
3666  const std::string &strName,
3667  const std::string &strPurpose) {
3668  WalletBatch batch(*database);
3669  return SetAddressBookWithDB(batch, address, strName, strPurpose);
3670 }
3671 
3673  bool is_mine;
3674  WalletBatch batch(*database);
3675  {
3676  LOCK(cs_wallet);
3677  // If we want to delete receiving addresses, we need to take care that
3678  // DestData "used" (and possibly newer DestData) gets preserved (and the
3679  // "deleted" address transformed into a change entry instead of actually
3680  // being deleted)
3681  // NOTE: This isn't a problem for sending addresses because they never
3682  // have any DestData yet! When adding new DestData, it should be
3683  // considered here whether to retain or delete it (or move it?).
3684  if (IsMine(address)) {
3686  "%s called with IsMine address, NOT SUPPORTED. Please "
3687  "report this bug! %s\n",
3688  __func__, PACKAGE_BUGREPORT);
3689  return false;
3690  }
3691  // Delete destdata tuples associated with address
3692  for (const std::pair<const std::string, std::string> &item :
3693  m_address_book[address].destdata) {
3694  batch.EraseDestData(address, item.first);
3695  }
3696  m_address_book.erase(address);
3697  is_mine = IsMine(address) != ISMINE_NO;
3698  }
3699 
3700  NotifyAddressBookChanged(this, address, "", is_mine, "", CT_DELETED);
3701 
3702  batch.ErasePurpose(address);
3703  return batch.EraseName(address);
3704 }
3705 
3708 
3709  unsigned int count = 0;
3710  for (auto spk_man : GetActiveScriptPubKeyMans()) {
3711  count += spk_man->KeypoolCountExternalKeys();
3712  }
3713 
3714  return count;
3715 }
3716 
3717 unsigned int CWallet::GetKeyPoolSize() const {
3719 
3720  unsigned int count = 0;
3721  for (auto spk_man : GetActiveScriptPubKeyMans()) {
3722  count += spk_man->GetKeyPoolSize();
3723  }
3724  return count;
3725 }
3726 
3727 bool CWallet::TopUpKeyPool(unsigned int kpSize) {
3728  LOCK(cs_wallet);
3729  bool res = true;
3730  for (auto spk_man : GetActiveScriptPubKeyMans()) {
3731  res &= spk_man->TopUp(kpSize);
3732  }
3733  return res;
3734 }
3735 
3736 bool CWallet::GetNewDestination(const OutputType type, const std::string label,
3737  CTxDestination &dest, std::string &error) {
3738  LOCK(cs_wallet);
3739  error.clear();
3740  bool result = false;
3741  auto spk_man = GetScriptPubKeyMan(type, false /* internal */);
3742  if (spk_man) {
3743  spk_man->TopUp();
3744  result = spk_man->GetNewDestination(type, dest, error);
3745  } else {
3746  error = strprintf("Error: No %s addresses available.",
3747  FormatOutputType(type));
3748  }
3749  if (result) {
3750  SetAddressBook(dest, label, "receive");
3751  }
3752 
3753  return result;
3754 }
3755 
3757  CTxDestination &dest,
3758  std::string &error) {
3759  LOCK(cs_wallet);
3760  error.clear();
3761 
3762  ReserveDestination reservedest(this, type);
3763  if (!reservedest.GetReservedDestination(dest, true)) {
3764  error = _("Error: Keypool ran out, please call keypoolrefill first")
3765  .translated;
3766  return false;
3767  }
3768 
3769  reservedest.KeepDestination();
3770  return true;
3771 }
3772 
3774  LOCK(cs_wallet);
3775  int64_t oldestKey = std::numeric_limits<int64_t>::max();
3776  for (const auto &spk_man_pair : m_spk_managers) {
3777  oldestKey =
3778  std::min(oldestKey, spk_man_pair.second->GetOldestKeyPoolTime());
3779  }
3780  return oldestKey;
3781 }
3782 
3784  const std::set<CTxDestination> &destinations) {
3785  for (auto &entry : mapWallet) {
3786  CWalletTx &wtx = entry.second;
3787  if (wtx.m_is_cache_empty) {
3788  continue;
3789  }
3790 
3791  for (size_t i = 0; i < wtx.tx->vout.size(); i++) {
3792  CTxDestination dst;
3793 
3794  if (ExtractDestination(wtx.tx->vout[i].scriptPubKey, dst) &&
3795  destinations.count(dst)) {
3796  wtx.MarkDirty();
3797  break;
3798  }
3799  }
3800  }
3801 }
3802 
3803 std::map<CTxDestination, Amount> CWallet::GetAddressBalances() const {
3804  std::map<CTxDestination, Amount> balances;
3805 
3806  LOCK(cs_wallet);
3807  std::set<TxId> trusted_parents;
3808  for (const auto &walletEntry : mapWallet) {
3809  const CWalletTx &wtx = walletEntry.second;
3810 
3811  if (!IsTrusted(wtx, trusted_parents)) {
3812  continue;
3813  }
3814 
3815  if (wtx.IsImmatureCoinBase()) {
3816  continue;
3817  }
3818 
3819  int nDepth = wtx.GetDepthInMainChain();
3820  if (nDepth < (wtx.IsFromMe(ISMINE_ALL) ? 0 : 1)) {
3821  continue;
3822  }
3823 
3824  for (uint32_t i = 0; i < wtx.tx->vout.size(); i++) {
3825  CTxDestination addr;
3826  if (!IsMine(wtx.tx->vout[i])) {
3827  continue;
3828  }
3829 
3830  if (!ExtractDestination(wtx.tx->vout[i].scriptPubKey, addr)) {
3831  continue;
3832  }
3833 
3834  Amount n = IsSpent(COutPoint(walletEntry.first, i))
3835  ? Amount::zero()
3836  : wtx.tx->vout[i].nValue;
3837  balances[addr] += n;
3838  }
3839  }
3840 
3841  return balances;
3842 }
3843 
3844 std::set<std::set<CTxDestination>> CWallet::GetAddressGroupings() const {
3846  std::set<std::set<CTxDestination>> groupings;
3847  std::set<CTxDestination> grouping;
3848 
3849  for (const auto &walletEntry : mapWallet) {
3850  const CWalletTx &wtx = walletEntry.second;
3851 
3852  if (wtx.tx->vin.size() > 0) {
3853  bool any_mine = false;
3854  // Group all input addresses with each other.
3855  for (const auto &txin : wtx.tx->vin) {
3856  CTxDestination address;
3857  // If this input isn't mine, ignore it.
3858  if (!IsMine(txin)) {
3859  continue;
3860  }
3861 
3862  if (!ExtractDestination(mapWallet.at(txin.prevout.GetTxId())
3863  .tx->vout[txin.prevout.GetN()]
3864  .scriptPubKey,
3865  address)) {
3866  continue;
3867  }
3868 
3869  grouping.insert(address);
3870  any_mine = true;
3871  }
3872 
3873  // Group change with input addresses.
3874  if (any_mine) {
3875  for (const auto &txout : wtx.tx->vout) {
3876  if (IsChange(txout)) {
3877  CTxDestination txoutAddr;
3878  if (!ExtractDestination(txout.scriptPubKey,
3879  txoutAddr)) {
3880  continue;
3881  }
3882 
3883  grouping.insert(txoutAddr);
3884  }
3885  }
3886  }
3887 
3888  if (grouping.size() > 0) {
3889  groupings.insert(grouping);
3890  grouping.clear();
3891  }
3892  }
3893 
3894  // Group lone addrs by themselves.
3895  for (const auto &txout : wtx.tx->vout) {
3896  if (IsMine(txout)) {
3897  CTxDestination address;
3898  if (!ExtractDestination(txout.scriptPubKey, address)) {
3899  continue;
3900  }
3901 
3902  grouping.insert(address);
3903  groupings.insert(grouping);
3904  grouping.clear();
3905  }
3906  }
3907  }
3908 
3909  // A set of pointers to groups of addresses.
3910  std::set<std::set<CTxDestination> *> uniqueGroupings;
3911  // Map addresses to the unique group containing it.
3912  std::map<CTxDestination, std::set<CTxDestination> *> setmap;
3913  for (std::set<CTxDestination> _grouping : groupings) {
3914  // Make a set of all the groups hit by this new group.
3915  std::set<std::set<CTxDestination> *> hits;
3916  std::map<CTxDestination, std::set<CTxDestination> *>::iterator it;
3917  for (const CTxDestination &address : _grouping) {
3918  if ((it = setmap.find(address)) != setmap.end()) {
3919  hits.insert((*it).second);
3920  }
3921  }
3922 
3923  // Merge all hit groups into a new single group and delete old groups.
3924  std::set<CTxDestination> *merged =
3925  new std::set<CTxDestination>(_grouping);
3926  for (std::set<CTxDestination> *hit : hits) {
3927  merged->insert(hit->begin(), hit->end());
3928  uniqueGroupings.erase(hit);
3929  delete hit;
3930  }
3931  uniqueGroupings.insert(merged);
3932 
3933  // Update setmap.
3934  for (const CTxDestination &element : *merged) {
3935  setmap[element] = merged;
3936  }
3937  }
3938 
3939  std::set<std::set<CTxDestination>> ret;
3940  for (const std::set<CTxDestination> *uniqueGrouping : uniqueGroupings) {
3941  ret.insert(*uniqueGrouping);
3942  delete uniqueGrouping;
3943  }
3944 
3945  return ret;
3946 }
3947 
3948 std::set<CTxDestination>
3949 CWallet::GetLabelAddresses(const std::string &label) const {
3950  LOCK(cs_wallet);
3951  std::set<CTxDestination> result;
3952  for (const std::pair<const CTxDestination, CAddressBookData> &item :
3953  m_address_book) {
3954  if (item.second.IsChange()) {
3955  continue;
3956  }
3957  const CTxDestination &address = item.first;
3958  const std::string &strName = item.second.GetLabel();
3959  if (strName == label) {
3960  result.insert(address);
3961  }
3962  }
3963 
3964  return result;
3965 }
3966 
3968  bool internal) {
3969  m_spk_man = pwallet->GetScriptPubKeyMan(type, internal);
3970  if (!m_spk_man) {
3971  return false;
3972  }
3973 
3974  if (nIndex == -1) {
3975  m_spk_man->TopUp();
3976 
3977  CKeyPool keypool;
3979  keypool)) {
3980  return false;
3981  }
3982  fInternal = keypool.fInternal;
3983  }
3984  dest = address;
3985  return true;
3986 }
3987 
3989  if (nIndex != -1) {
3991  }
3992 
3993  nIndex = -1;
3994  address = CNoDestination();
3995 }
3996 
3998  if (nIndex != -1) {
4000  }
4001  nIndex = -1;
4002  address = CNoDestination();
4003 }
4004 
4005 void CWallet::LockCoin(const COutPoint &output) {
4007  setLockedCoins.insert(output);
4008 }
4009 
4010 void CWallet::UnlockCoin(const COutPoint &output) {
4012  setLockedCoins.erase(output);
4013 }
4014 
4017  setLockedCoins.clear();
4018 }
4019 
4020 bool CWallet::IsLockedCoin(const COutPoint &outpoint) const {
4022 
4023  return setLockedCoins.count(outpoint) > 0;
4024 }
4025 
4026 void CWallet::ListLockedCoins(std::vector<COutPoint> &vOutpts) const {
4028  for (COutPoint outpoint : setLockedCoins) {
4029  vOutpts.push_back(outpoint);
4030  }
4031 }
4032  // end of Actions
4034 
4035 void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const {
4037  mapKeyBirth.clear();
4038 
4040  assert(spk_man != nullptr);
4041  LOCK(spk_man->cs_KeyStore);
4042 
4043  // Get birth times for keys with metadata.
4044  for (const auto &entry : spk_man->mapKeyMetadata) {
4045  if (entry.second.nCreateTime) {
4046  mapKeyBirth[entry.first] = entry.second.nCreateTime;
4047  }
4048  }
4049 
4050  // map in which we'll infer heights of other keys
4051  std::map<CKeyID, const CWalletTx::Confirmation *> mapKeyFirstBlock;
4052  CWalletTx::Confirmation max_confirm;
4053  // the tip can be reorganized; use a 144-block safety margin
4054  max_confirm.block_height =
4055  GetLastBlockHeight() > 144 ? GetLastBlockHeight() - 144 : 0;
4056  CHECK_NONFATAL(chain().findAncestorByHeight(
4057  GetLastBlockHash(), max_confirm.block_height,
4058  FoundBlock().hash(max_confirm.hashBlock)));
4059  for (const CKeyID &keyid : spk_man->GetKeys()) {
4060  if (mapKeyBirth.count(keyid) == 0) {
4061  mapKeyFirstBlock[keyid] = &max_confirm;
4062  }
4063  }
4064 
4065  // If there are no such keys, we're done.
4066  if (mapKeyFirstBlock.empty()) {
4067  return;
4068  }
4069 
4070  // Find first block that affects those keys, if there are any left.
4071  for (const auto &entry : mapWallet) {
4072  // iterate over all wallet transactions...
4073  const CWalletTx &wtx = entry.second;
4074  if (wtx.m_confirm.status == CWalletTx::CONFIRMED) {
4075  // ... which are already in a block
4076  for (const CTxOut &txout : wtx.tx->vout) {
4077  // Iterate over all their outputs...
4078  for (const auto &keyid :
4079  GetAffectedKeys(txout.scriptPubKey, *spk_man)) {
4080  // ... and all their affected keys.
4081  auto rit = mapKeyFirstBlock.find(keyid);
4082  if (rit != mapKeyFirstBlock.end() &&
4083  wtx.m_confirm.block_height <
4084  rit->second->block_height) {
4085  rit->second = &wtx.m_confirm;
4086  }
4087  }
4088  }
4089  }
4090  }
4091 
4092  // Extract block timestamps for those keys.
4093  for (const auto &entry : mapKeyFirstBlock) {
4094  int64_t block_time;
4095  CHECK_NONFATAL(chain().findBlock(entry.second->hashBlock,
4096  FoundBlock().time(block_time)));
4097  // block times can be 2h off
4098  mapKeyBirth[entry.first] = block_time - TIMESTAMP_WINDOW;
4099  }
4100 }
4101 
4123 unsigned int CWallet::ComputeTimeSmart(const CWalletTx &wtx) const {
4124  unsigned int nTimeSmart = wtx.nTimeReceived;
4125  if (!wtx.isUnconfirmed() && !wtx.isAbandoned()) {
4126  int64_t blocktime;
4127  if (chain().findBlock(wtx.m_confirm.hashBlock,
4128  FoundBlock().time(blocktime))) {
4129  int64_t latestNow = wtx.nTimeReceived;
4130  int64_t latestEntry = 0;
4131 
4132  // Tolerate times up to the last timestamp in the wallet not more
4133  // than 5 minutes into the future
4134  int64_t latestTolerated = latestNow + 300;
4135  const TxItems &txOrdered = wtxOrdered;
4136  for (auto it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) {
4137  CWalletTx *const pwtx = it->second;
4138  if (pwtx == &wtx) {
4139  continue;
4140  }
4141  int64_t nSmartTime;
4142  nSmartTime = pwtx->nTimeSmart;
4143  if (!nSmartTime) {
4144  nSmartTime = pwtx->nTimeReceived;
4145  }
4146  if (nSmartTime <= latestTolerated) {
4147  latestEntry = nSmartTime;
4148  if (nSmartTime > latestNow) {
4149  latestNow = nSmartTime;
4150  }
4151  break;
4152  }
4153  }
4154 
4155  nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
4156  } else {
4157  WalletLogPrintf("%s: found %s in block %s not in index\n", __func__,
4158  wtx.GetId().ToString(),
4159  wtx.m_confirm.hashBlock.ToString());
4160  }
4161  }
4162  return nTimeSmart;
4163 }
4164 
4166  const std::string &key, const std::string &value) {
4167  if (boost::get<CNoDestination>(&dest)) {
4168  return false;
4169  }
4170 
4171  m_address_book[dest].destdata.insert(std::make_pair(key, value));
4172  return batch.WriteDestData(dest, key, value);
4173 }
4174 
4176  const std::string &key) {
4177  if (!m_address_book[dest].destdata.erase(key)) {
4178  return false;
4179  }
4180 
4181  return batch.EraseDestData(dest, key);
4182 }
4183 
4184 void CWallet::LoadDestData(const CTxDestination &dest, const std::string &key,
4185  const std::string &value) {
4186  m_address_book[dest].destdata.insert(std::make_pair(key, value));
4187 }
4188 
4189 bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key,
4190  std::string *value) const {
4191  std::map<CTxDestination, CAddressBookData>::const_iterator i =
4192  m_address_book.find(dest);
4193  if (i != m_address_book.end()) {
4194  CAddressBookData::StringMap::const_iterator j =
4195  i->second.destdata.find(key);
4196  if (j != i->second.destdata.end()) {
4197  if (value) {
4198  *value = j->second;
4199  }
4200 
4201  return true;
4202  }
4203  }
4204  return false;
4205 }
4206 
4207 std::vector<std::string>
4208 CWallet::GetDestValues(const std::string &prefix) const {
4209  std::vector<std::string> values;
4210  for (const auto &address : m_address_book) {
4211  for (const auto &data : address.second.destdata) {
4212  if (!data.first.compare(0, prefix.size(), prefix)) {
4213  values.emplace_back(data.second);
4214  }
4215  }
4216  }
4217  return values;
4218 }
4219 
4220 std::unique_ptr<WalletDatabase>
4221 MakeWalletDatabase(const std::string &name, const DatabaseOptions &options,
4222  DatabaseStatus &status, bilingual_str &error_string) {
4223  // Do some checking on wallet path. It should be either a:
4224  //
4225  // 1. Path where a directory can be created.
4226  // 2. Path to an existing directory.
4227  // 3. Path to a symlink to a directory.
4228  // 4. For backwards compatibility, the name of a data file in -walletdir.
4229  const fs::path &wallet_path = fs::absolute(name, GetWalletDir());
4230  fs::file_type path_type = fs::symlink_status(wallet_path).type();
4231  if (!(path_type == fs::file_not_found || path_type == fs::directory_file ||
4232  (path_type == fs::symlink_file && fs::is_directory(wallet_path)) ||
4233  (path_type == fs::regular_file &&
4234  fs::path(name).filename() == name))) {
4235  error_string = Untranslated(
4236  strprintf("Invalid -wallet path '%s'. -wallet path should point to "
4237  "a directory where wallet.dat and "
4238  "database/log.?????????? files can be stored, a location "
4239  "where such a directory could be created, "
4240  "or (for backwards compatibility) the name of an "
4241  "existing data file in -walletdir (%s)",
4242  name, GetWalletDir()));
4244  return nullptr;
4245  }
4246  return MakeDatabase(wallet_path, options, status, error_string);
4247 }
4248 
4249 std::shared_ptr<CWallet>
4250 CWallet::Create(interfaces::Chain &chain, const std::string &name,
4251  std::unique_ptr<WalletDatabase> database,
4252  uint64_t wallet_creation_flags, bilingual_str &error,
4253  std::vector<bilingual_str> &warnings) {
4254  const std::string &walletFile = database->Filename();
4255 
4256  chain.initMessage(_("Loading wallet...").translated);
4257 
4258  int64_t nStart = GetTimeMillis();
4259  bool fFirstRun = true;
4260  // TODO: Can't use std::make_shared because we need a custom deleter but
4261  // should be possible to use std::allocate_shared.
4262  std::shared_ptr<CWallet> walletInstance(
4263  new CWallet(&chain, name, std::move(database)), ReleaseWallet);
4264  DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
4265  if (nLoadWalletRet != DBErrors::LOAD_OK) {
4266  if (nLoadWalletRet == DBErrors::CORRUPT) {
4267  error =
4268  strprintf(_("Error loading %s: Wallet corrupted"), walletFile);
4269  return nullptr;
4270  }
4271 
4272  if (nLoadWalletRet == DBErrors::NONCRITICAL_ERROR) {
4273  warnings.push_back(
4274  strprintf(_("Error reading %s! All keys read correctly, but "
4275  "transaction data or address book entries might be "
4276  "missing or incorrect."),
4277  walletFile));
4278  } else if (nLoadWalletRet == DBErrors::TOO_NEW) {
4279  error = strprintf(
4280  _("Error loading %s: Wallet requires newer version of %s"),
4281  walletFile, PACKAGE_NAME);
4282  return nullptr;
4283  } else if (nLoadWalletRet == DBErrors::NEED_REWRITE) {
4284  error = strprintf(
4285  _("Wallet needed to be rewritten: restart %s to complete"),
4286  PACKAGE_NAME);
4287  return nullptr;
4288  } else {
4289  error = strprintf(_("Error loading %s"), walletFile);
4290  return nullptr;
4291  }
4292  }
4293 
4294  if (fFirstRun) {
4295  // Ensure this wallet.dat can only be opened by clients supporting
4296  // HD with chain split and expects no default key.
4297  walletInstance->SetMinVersion(FEATURE_LATEST);
4298 
4299  walletInstance->AddWalletFlags(wallet_creation_flags);
4300 
4301  // Only create LegacyScriptPubKeyMan when not descriptor wallet
4302  if (!walletInstance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
4303  walletInstance->SetupLegacyScriptPubKeyMan();
4304  }
4305 
4306  if (!(wallet_creation_flags &
4308  LOCK(walletInstance->cs_wallet);
4309  if (walletInstance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
4310  walletInstance->SetupDescriptorScriptPubKeyMans();
4311  // SetupDescriptorScriptPubKeyMans already calls SetupGeneration
4312  // for us so we don't need to call SetupGeneration separately
4313  } else {
4314  // Legacy wallets need SetupGeneration here.
4315  for (auto spk_man :
4316  walletInstance->GetActiveScriptPubKeyMans()) {
4317  if (!spk_man->SetupGeneration()) {
4318  error = _("Unable to generate initial keys");
4319  return nullptr;
4320  }
4321  }
4322  }
4323  }
4324 
4325  walletInstance->chainStateFlushed(chain.getTipLocator());
4326  } else if (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS) {
4327  // Make it impossible to disable private keys after creation
4328  error = strprintf(_("Error loading %s: Private keys can only be "
4329  "disabled during creation"),
4330  walletFile);
4331  return nullptr;
4332  } else if (walletInstance->IsWalletFlagSet(
4334  for (auto spk_man : walletInstance->GetActiveScriptPubKeyMans()) {
4335  if (spk_man->HavePrivateKeys()) {
4336  warnings.push_back(
4337  strprintf(_("Warning: Private keys detected in wallet {%s} "
4338  "with disabled private keys"),
4339  walletFile));
4340  }
4341  }
4342  }
4343 
4344  if (gArgs.IsArgSet("-mintxfee")) {
4345  Amount n = Amount::zero();
4346  if (!ParseMoney(gArgs.GetArg("-mintxfee", ""), n) ||
4347  n == Amount::zero()) {
4348  error = AmountErrMsg("mintxfee", gArgs.GetArg("-mintxfee", ""));
4349  return nullptr;
4350  }
4351  if (n > HIGH_TX_FEE_PER_KB) {
4352  warnings.push_back(AmountHighWarn("-mintxfee") + Untranslated(" ") +
4353  _("This is the minimum transaction fee you pay "
4354  "on every transaction."));
4355  }
4356  walletInstance->m_min_fee = CFeeRate(n);
4357  }
4358 
4359  if (gArgs.IsArgSet("-maxapsfee")) {
4360  const std::string max_aps_fee{gArgs.GetArg("-maxapsfee", "")};
4361  Amount n = Amount::zero();
4362  if (max_aps_fee == "-1") {
4363  n = -1 * SATOSHI;
4364  } else if (!ParseMoney(max_aps_fee, n)) {
4365  error = AmountErrMsg("maxapsfee", max_aps_fee);
4366  return nullptr;
4367  }
4368  if (n > HIGH_APS_FEE) {
4369  warnings.push_back(
4370  AmountHighWarn("-maxapsfee") + Untranslated(" ") +
4371  _("This is the maximum transaction fee you pay (in addition to"
4372  " the normal fee) to prioritize partial spend avoidance over"
4373  " regular coin selection."));
4374  }
4375  walletInstance->m_max_aps_fee = n;
4376  }
4377 
4378  if (gArgs.IsArgSet("-fallbackfee")) {
4379  Amount nFeePerK = Amount::zero();
4380  if (!ParseMoney(gArgs.GetArg("-fallbackfee", ""), nFeePerK)) {
4381  error =
4382  strprintf(_("Invalid amount for -fallbackfee=<amount>: '%s'"),
4383  gArgs.GetArg("-fallbackfee", ""));
4384  return nullptr;
4385  }
4386  if (nFeePerK > HIGH_TX_FEE_PER_KB) {
4387  warnings.push_back(AmountHighWarn("-fallbackfee") +
4388  Untranslated(" ") +
4389  _("This is the transaction fee you may pay when "
4390  "fee estimates are not available."));
4391  }
4392  walletInstance->m_fallback_fee = CFeeRate(nFeePerK);
4393  }
4394  // Disable fallback fee in case value was set to 0, enable if non-null value
4395  walletInstance->m_allow_fallback_fee =
4396  walletInstance->m_fallback_fee.GetFeePerK() != Amount::zero();
4397 
4398  if (gArgs.IsArgSet("-paytxfee")) {
4399  Amount nFeePerK = Amount::zero();
4400  if (!ParseMoney(gArgs.GetArg("-paytxfee", ""), nFeePerK)) {
4401  error = AmountErrMsg("paytxfee", gArgs.GetArg("-paytxfee", ""));
4402  return nullptr;
4403  }
4404  if (nFeePerK > HIGH_TX_FEE_PER_KB) {
4405  warnings.push_back(AmountHighWarn("-paytxfee") + Untranslated(" ") +
4406  _("This is the transaction fee you will pay if "
4407  "you send a transaction."));
4408  }
4409  walletInstance->m_pay_tx_fee = CFeeRate(nFeePerK, 1000);
4410  if (walletInstance->m_pay_tx_fee < chain.relayMinFee()) {
4411  error = strprintf(_("Invalid amount for -paytxfee=<amount>: '%s' "
4412  "(must be at least %s)"),
4413  gArgs.GetArg("-paytxfee", ""),
4414  chain.relayMinFee().ToString());
4415  return nullptr;
4416  }
4417  }
4418 
4419  if (gArgs.IsArgSet("-maxtxfee")) {
4420  Amount nMaxFee = Amount::zero();
4421  if (!ParseMoney(gArgs.GetArg("-maxtxfee", ""), nMaxFee)) {
4422  error = AmountErrMsg("maxtxfee", gArgs.GetArg("-maxtxfee", ""));
4423  return nullptr;
4424  }
4425  if (nMaxFee > HIGH_MAX_TX_FEE) {
4426  warnings.push_back(_("-maxtxfee is set very high! Fees this large "
4427  "could be paid on a single transaction."));
4428  }
4429  if (CFeeRate(nMaxFee, 1000) < chain.relayMinFee()) {
4430  error = strprintf(
4431  _("Invalid amount for -maxtxfee=<amount>: '%s' (must be at "
4432  "least the minrelay fee of %s to prevent stuck "
4433  "transactions)"),
4434  gArgs.GetArg("-maxtxfee", ""), chain.relayMinFee().ToString());
4435  return nullptr;
4436  }
4437  walletInstance->m_default_max_tx_fee = nMaxFee;
4438  }
4439 
4441  warnings.push_back(
4442  AmountHighWarn("-minrelaytxfee") + Untranslated(" ") +
4443  _("The wallet will avoid paying less than the minimum relay fee."));
4444  }
4445 
4446  walletInstance->m_spend_zero_conf_change =
4447  gArgs.GetBoolArg("-spendzeroconfchange", DEFAULT_SPEND_ZEROCONF_CHANGE);
4448 
4449  walletInstance->m_default_address_type = DEFAULT_ADDRESS_TYPE;
4450 
4451  walletInstance->WalletLogPrintf("Wallet completed loading in %15dms\n",
4452  GetTimeMillis() - nStart);
4453 
4454  // Try to top up keypool. No-op if the wallet is locked.
4455  walletInstance->TopUpKeyPool();
4456 
4457  LOCK(walletInstance->cs_wallet);
4458 
4459  // Register wallet with validationinterface. It's done before rescan to
4460  // avoid missing block connections between end of rescan and validation
4461  // subscribing. Because of wallet lock being hold, block connection
4462  // notifications are going to be pending on the validation-side until lock
4463  // release. It's likely to have block processing duplicata (if rescan block
4464  // range overlaps with notification one) but we guarantee at least than
4465  // wallet state is correct after notifications delivery. This is temporary
4466  // until rescan and notifications delivery are unified under same interface.
4467  walletInstance->m_chain_notifications_handler =
4468  walletInstance->chain().handleNotifications(walletInstance);
4469 
4470  int rescan_height = 0;
4471  if (!gArgs.GetBoolArg("-rescan", false)) {
4472  WalletBatch batch(*walletInstance->database);
4473  CBlockLocator locator;
4474  if (batch.ReadBestBlock(locator)) {
4475  if (const std::optional<int> fork_height =
4476  chain.findLocatorFork(locator)) {
4477  rescan_height = *fork_height;
4478  }
4479  }
4480  }
4481 
4482  const std::optional<int> tip_height = chain.getHeight();
4483  if (tip_height) {
4484  walletInstance->m_last_block_processed =
4485  chain.getBlockHash(*tip_height);
4486  walletInstance->m_last_block_processed_height = *tip_height;
4487  } else {
4488  walletInstance->m_last_block_processed.SetNull();
4489  walletInstance->m_last_block_processed_height = -1;
4490  }
4491 
4492  if (tip_height && *tip_height != rescan_height) {
4493  // We can't rescan beyond non-pruned blocks, stop and throw an error.
4494  // This might happen if a user uses an old wallet within a pruned node
4495  // or if they ran -disablewallet for a longer time, then decided to
4496  // re-enable
4497  if (chain.havePruned()) {
4498  // Exit early and print an error.
4499  // If a block is pruned after this check, we will load the wallet,
4500  // but fail the rescan with a generic error.
4501  int block_height = *tip_height;
4502  while (block_height > 0 &&
4503  chain.haveBlockOnDisk(block_height - 1) &&
4504  rescan_height != block_height) {
4505  --block_height;
4506  }
4507 
4508  if (rescan_height != block_height) {
4509  error = _("Prune: last wallet synchronisation goes beyond "
4510  "pruned data. You need to -reindex (download the "
4511  "whole blockchain again in case of pruned node)");
4512  return nullptr;
4513  }
4514  }
4515 
4516  chain.initMessage(_("Rescanning...").translated);
4517  walletInstance->WalletLogPrintf(
4518  "Rescanning last %i blocks (from block %i)...\n",
4519  *tip_height - rescan_height, rescan_height);
4520 
4521  // No need to read and scan block if block was created before our wallet
4522  // birthday (as adjusted for block time variability)
4523  std::optional<int64_t> time_first_key;
4524  for (auto spk_man : walletInstance->GetAllScriptPubKeyMans()) {
4525  int64_t time = spk_man->GetTimeFirstKey();
4526  if (!time_first_key || time < *time_first_key) {
4527  time_first_key = time;
4528  }
4529  }
4530  if (time_first_key) {
4531  if (std::optional<int> first_block =
4533  *time_first_key - TIMESTAMP_WINDOW, rescan_height,
4534  nullptr)) {
4535  rescan_height = *first_block;
4536  }
4537  }
4538 
4539  {
4540  WalletRescanReserver reserver(*walletInstance);
4541  if (!reserver.reserve() ||
4543  walletInstance
4544  ->ScanForWalletTransactions(
4545  chain.getBlockHash(rescan_height), rescan_height,
4546  {} /* max height */, reserver, true /* update */)
4547  .status)) {
4548  error = _("Failed to rescan the wallet during initialization");
4549  return nullptr;
4550  }
4551  }
4552  walletInstance->chainStateFlushed(chain.getTipLocator());
4553  walletInstance->database->IncrementUpdateCounter();
4554  }
4555 
4556  {
4557  LOCK(cs_wallets);
4558  for (auto &load_wallet : g_load_wallet_fns) {
4559  load_wallet(interfaces::MakeWallet(walletInstance));
4560  }
4561  }
4562 
4563  walletInstance->SetBroadcastTransactions(
4564  gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
4565 
4566  walletInstance->WalletLogPrintf("setKeyPool.size() = %u\n",
4567  walletInstance->GetKeyPoolSize());
4568  walletInstance->WalletLogPrintf("mapWallet.size() = %u\n",
4569  walletInstance->mapWallet.size());
4570  walletInstance->WalletLogPrintf("m_address_book.size() = %u\n",
4571  walletInstance->m_address_book.size());
4572 
4573  return walletInstance;
4574 }
4575 
4576 const CAddressBookData *
4578  bool allow_change) const {
4579  const auto &address_book_it = m_address_book.find(dest);
4580  if (address_book_it == m_address_book.end()) {
4581  return nullptr;
4582  }
4583  if ((!allow_change) && address_book_it->second.IsChange()) {
4584  return nullptr;
4585  }
4586  return &address_book_it->second;
4587 }
4588 
4590  std::vector<bilingual_str> &warnings) {
4591  int prev_version = GetVersion();
4592  int nMaxVersion = version;
4593  // The -upgradewallet without argument case
4594  if (nMaxVersion == 0) {
4595  WalletLogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
4596  nMaxVersion = FEATURE_LATEST;
4597  // permanently upgrade the wallet immediately
4599  } else {
4600  WalletLogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
4601  }
4602 
4603  if (nMaxVersion < GetVersion()) {
4604  error = _("Cannot downgrade wallet");
4605  return false;
4606  }
4607 
4608  SetMaxVersion(nMaxVersion);
4609 
4610  LOCK(cs_wallet);
4611 
4612  // Do not upgrade versions to any version between HD_SPLIT and
4613  // FEATURE_PRE_SPLIT_KEYPOOL unless already supporting HD_SPLIT
4614  int max_version = GetVersion();
4616  max_version >= FEATURE_HD_SPLIT &&
4617  max_version < FEATURE_PRE_SPLIT_KEYPOOL) {
4618  error = _("Cannot upgrade a non HD split wallet without upgrading to "
4619  "support pre split keypool. Please use version 200300 or no "
4620  "version specified.");
4621  return false;
4622  }
4623 
4624  for (auto spk_man : GetActiveScriptPubKeyMans()) {
4625  if (!spk_man->Upgrade(prev_version, error)) {
4626  return false;
4627  }
4628  }
4629 
4630  return true;
4631 }
4632 
4634  LOCK(cs_wallet);
4635 
4636  // Add wallet transactions that aren't already in a block to mempool.
4637  // Do this here as mempool requires genesis block to be loaded.
4639 
4640  // Update wallet transactions with current mempool transactions.
4642 }
4643 
4644 bool CWallet::BackupWallet(const std::string &strDest) const {
4645  return database->Backup(strDest);
4646 }
4647 
4649  nTime = GetTime();
4650  fInternal = false;
4651  m_pre_split = false;
4652 }
4653 
4654 CKeyPool::CKeyPool(const CPubKey &vchPubKeyIn, bool internalIn) {
4655  nTime = GetTime();
4656  vchPubKey = vchPubKeyIn;
4657  fInternal = internalIn;
4658  m_pre_split = false;
4659 }
4660 
4662  assert(pwallet != nullptr);
4664  if (isUnconfirmed() || isAbandoned()) {
4665  return 0;
4666  }
4667 
4668  return (pwallet->GetLastBlockHeight() - m_confirm.block_height + 1) *
4669  (isConflicted() ? -1 : 1);
4670 }
4671 
4673  if (!IsCoinBase()) {
4674  return 0;
4675  }
4676 
4677  int chain_depth = GetDepthInMainChain();
4678  // coinbase tx should not be conflicted
4679  assert(chain_depth >= 0);
4680  return std::max(0, (COINBASE_MATURITY + 1) - chain_depth);
4681 }
4682 
4684  // note GetBlocksToMaturity is 0 for non-coinbase tx
4685  return GetBlocksToMaturity() > 0;
4686 }
4687 
4688 std::vector<OutputGroup>
4689 CWallet::GroupOutputs(const std::vector<COutput> &outputs, bool single_coin,
4690  const size_t max_ancestors) const {
4691  std::vector<OutputGroup> groups;
4692  std::map<CTxDestination, OutputGroup> gmap;
4693  std::set<CTxDestination> full_groups;
4694 
4695  for (const auto &output : outputs) {
4696  if (output.fSpendable) {
4697  CTxDestination dst;
4698  CInputCoin input_coin = output.GetInputCoin();
4699 
4700  size_t ancestors, descendants;
4701  chain().getTransactionAncestry(output.tx->GetId(), ancestors,
4702  descendants);
4703  if (!single_coin &&
4704  ExtractDestination(output.tx->tx->vout[output.i].scriptPubKey,
4705  dst)) {
4706  auto it = gmap.find(dst);
4707  if (it != gmap.end()) {
4708  // Limit output groups to no more than
4709  // OUTPUT_GROUP_MAX_ENTRIES number of entries, to protect
4710  // against inadvertently creating a too-large transaction
4711  // when using -avoidpartialspends to prevent breaking
4712  // consensus or surprising users with a very high amount of
4713  // fees.
4714  if (it->second.m_outputs.size() >=
4716  groups.push_back(it->second);
4717  it->second = OutputGroup{};
4718  full_groups.insert(dst);
4719  }
4720  it->second.Insert(input_coin, output.nDepth,
4721  output.tx->IsFromMe(ISMINE_ALL),
4722  ancestors, descendants);
4723  } else {
4724  gmap[dst].Insert(input_coin, output.nDepth,
4725  output.tx->IsFromMe(ISMINE_ALL), ancestors,
4726  descendants);
4727  }
4728  } else {
4729  groups.emplace_back(input_coin, output.nDepth,
4730  output.tx->IsFromMe(ISMINE_ALL), ancestors,
4731  descendants);
4732  }
4733  }
4734  }
4735  if (!single_coin) {
4736  for (auto &it : gmap) {
4737  auto &group = it.second;
4738  if (full_groups.count(it.first) > 0) {
4739  // Make this unattractive as we want coin selection to avoid it
4740  // if possible
4741  group.m_ancestors = max_ancestors - 1;
4742  }
4743  groups.push_back(group);
4744  }
4745  }
4746  return groups;
4747 }
4748 
4749 bool CWallet::IsCrypted() const {
4750  return HasEncryptionKeys();
4751 }
4752 
4753 bool CWallet::IsLocked() const {
4754  if (!IsCrypted()) {
4755  return false;
4756  }
4757  LOCK(cs_wallet);
4758  return vMasterKey.empty();
4759 }
4760 
4762  if (!IsCrypted()) {
4763  return false;
4764  }
4765 
4766  {
4767  LOCK(cs_wallet);
4768  vMasterKey.clear();
4769  }
4770 
4771  NotifyStatusChanged(this);
4772  return true;
4773 }
4774 
4775 bool CWallet::Unlock(const CKeyingMaterial &vMasterKeyIn, bool accept_no_keys) {
4776  {
4777  LOCK(cs_wallet);
4778  for (const auto &spk_man_pair : m_spk_managers) {
4779  if (!spk_man_pair.second->CheckDecryptionKey(vMasterKeyIn,
4780  accept_no_keys)) {
4781  return false;
4782  }
4783  }
4784  vMasterKey = vMasterKeyIn;
4785  }
4786  NotifyStatusChanged(this);
4787  return true;
4788 }
4789 
4790 std::set<ScriptPubKeyMan *> CWallet::GetActiveScriptPubKeyMans() const {
4791  std::set<ScriptPubKeyMan *> spk_mans;
4792  for (bool internal : {false, true}) {
4793  for (OutputType t : OUTPUT_TYPES) {
4794  auto spk_man = GetScriptPubKeyMan(t, internal);
4795  if (spk_man) {
4796  spk_mans.insert(spk_man);
4797  }
4798  }
4799  }
4800  return spk_mans;
4801 }
4802 
4803 std::set<ScriptPubKeyMan *> CWallet::GetAllScriptPubKeyMans() const {
4804  std::set<ScriptPubKeyMan *> spk_mans;
4805  for (const auto &spk_man_pair : m_spk_managers) {
4806  spk_mans.insert(spk_man_pair.second.get());
4807  }
4808  return spk_mans;
4809 }
4810 
4812  bool internal) const {
4813  const std::map<OutputType, ScriptPubKeyMan *> &spk_managers =
4815  std::map<OutputType, ScriptPubKeyMan *>::const_iterator it =
4816  spk_managers.find(type);
4817  if (it == spk_managers.end()) {
4819  "%s scriptPubKey Manager for output type %d does not exist\n",
4820  internal ? "Internal" : "External", static_cast<int>(type));
4821  return nullptr;
4822  }
4823  return it->second;
4824 }
4825 
4826 std::set<ScriptPubKeyMan *>
4828  SignatureData &sigdata) const {
4829  std::set<ScriptPubKeyMan *> spk_mans;
4830  for (const auto &spk_man_pair : m_spk_managers) {
4831  if (spk_man_pair.second->CanProvide(script, sigdata)) {
4832  spk_mans.insert(spk_man_pair.second.get());
4833  }
4834  }
4835  return spk_mans;
4836 }
4837 
4839  SignatureData sigdata;
4840  for (const auto &spk_man_pair : m_spk_managers) {
4841  if (spk_man_pair.second->CanProvide(script, sigdata)) {
4842  return spk_man_pair.second.get();
4843  }
4844  }
4845  return nullptr;
4846 }
4847 
4849  if (m_spk_managers.count(id) > 0) {
4850  return m_spk_managers.at(id).get();
4851  }
4852  return nullptr;
4853 }
4854 
4855 std::unique_ptr<SigningProvider>
4856 CWallet::GetSolvingProvider(const CScript &script) const {
4857  SignatureData sigdata;
4858  return GetSolvingProvider(script, sigdata);
4859 }
4860 
4861 std::unique_ptr<SigningProvider>
4863  SignatureData &sigdata) const {
4864  for (const auto &spk_man_pair : m_spk_managers) {
4865  if (spk_man_pair.second->CanProvide(script, sigdata)) {
4866  return spk_man_pair.second->GetSolvingProvider(script);
4867  }
4868  }
4869  return nullptr;
4870 }
4871 
4874  return nullptr;
4875  }
4876  // Legacy wallets only have one ScriptPubKeyMan which is a
4877  // LegacyScriptPubKeyMan. Everything in m_internal_spk_managers and
4878  // m_external_spk_managers point to the same legacyScriptPubKeyMan.
4880  if (it == m_internal_spk_managers.end()) {
4881  return nullptr;
4882  }
4883  return dynamic_cast<LegacyScriptPubKeyMan *>(it->second);
4884 }
4885 
4888  return GetLegacyScriptPubKeyMan();
4889 }
4890 
4892  if (!m_internal_spk_managers.empty() || !m_external_spk_managers.empty() ||
4894  return;
4895  }
4896 
4897  auto spk_manager =
4898  std::unique_ptr<ScriptPubKeyMan>(new LegacyScriptPubKeyMan(*this));
4899  for (const auto &type : OUTPUT_TYPES) {
4900  m_internal_spk_managers[type] = spk_manager.get();
4901  m_external_spk_managers[type] = spk_manager.get();
4902  }
4903  m_spk_managers[spk_manager->GetID()] = std::move(spk_manager);
4904 }
4905 
4907  return vMasterKey;
4908 }
4909 
4911  return !mapMasterKeys.empty();
4912 }
4913 
4915  for (const auto &spk_man : GetActiveScriptPubKeyMans()) {
4916  spk_man->NotifyWatchonlyChanged.connect(NotifyWatchonlyChanged);
4917  spk_man->NotifyCanGetAddressesChanged.connect(
4919  }
4920 }
4921 
4923  WalletDescriptor &desc) {
4924  auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(
4925  new DescriptorScriptPubKeyMan(*this, desc));
4926  m_spk_managers[id] = std::move(spk_manager);
4927 }
4928 
4931 
4932  // Make a seed
4933  CKey seed_key;
4934  seed_key.MakeNewKey(true);
4935  CPubKey seed = seed_key.GetPubKey();
4936  assert(seed_key.VerifyPubKey(seed));
4937 
4938  // Get the extended key
4939  CExtKey master_key;
4940  master_key.SetSeed(seed_key.begin(), seed_key.size());
4941 
4942  for (bool internal : {false, true}) {
4943  for (OutputType t : OUTPUT_TYPES) {
4944  auto spk_manager =
4945  std::make_unique<DescriptorScriptPubKeyMan>(*this, internal);
4946  if (IsCrypted()) {
4947  if (IsLocked()) {
4948  throw std::runtime_error(
4949  std::string(__func__) +
4950  ": Wallet is locked, cannot setup new descriptors");
4951  }
4952  if (!spk_manager->CheckDecryptionKey(vMasterKey) &&
4953  !spk_manager->Encrypt(vMasterKey, nullptr)) {
4954  throw std::runtime_error(
4955  std::string(__func__) +
4956  ": Could not encrypt new descriptors");
4957  }
4958  }
4959  spk_manager->SetupDescriptorGeneration(master_key, t);
4960  uint256 id = spk_manager->GetID();
4961  m_spk_managers[id] = std::move(spk_manager);
4962  AddActiveScriptPubKeyMan(id, t, internal);
4963  }
4964  }
4965 }
4966 
4968  bool internal) {
4969  WalletBatch batch(*database);
4970  if (!batch.WriteActiveScriptPubKeyMan(static_cast<uint8_t>(type), id,
4971  internal)) {
4972  throw std::runtime_error(std::string(__func__) +
4973  ": writing active ScriptPubKeyMan id failed");
4974  }
4975  LoadActiveScriptPubKeyMan(id, type, internal);
4976 }
4977 
4979  bool internal) {
4981  "Setting spkMan to active: id = %s, type = %d, internal = %d\n",
4982  id.ToString(), static_cast<int>(type), static_cast<int>(internal));
4983  auto &spk_mans =
4985  auto spk_man = m_spk_managers.at(id).get();
4986  spk_man->SetInternal(internal);
4987  spk_mans[type] = spk_man;
4988 
4990 }
4991 
4992 bool CWallet::IsLegacy() const {
4993  if (m_internal_spk_managers.count(OutputType::LEGACY) == 0) {
4994  return false;
4995  }
4996  auto spk_man = dynamic_cast<LegacyScriptPubKeyMan *>(
4998  return spk_man != nullptr;
4999 }
5000 
5003  for (auto &spk_man_pair : m_spk_managers) {
5004  // Try to downcast to DescriptorScriptPubKeyMan then check if the
5005  // descriptors match
5006  DescriptorScriptPubKeyMan *spk_manager =
5007  dynamic_cast<DescriptorScriptPubKeyMan *>(
5008  spk_man_pair.second.get());
5009  if (spk_manager != nullptr && spk_manager->HasWalletDescriptor(desc)) {
5010  return spk_manager;
5011  }
5012  }
5013 
5014  return nullptr;
5015 }
5016 
5019  const FlatSigningProvider &signing_provider,
5020  const std::string &label) {
5023  "Cannot add WalletDescriptor to a non-descriptor wallet\n");
5024  return nullptr;
5025  }
5026 
5027  LOCK(cs_wallet);
5028  auto new_spk_man = std::make_unique<DescriptorScriptPubKeyMan>(*this, desc);
5029 
5030  // If we already have this descriptor, remove it from the maps but add the
5031  // existing cache to desc
5032  auto old_spk_man = GetDescriptorScriptPubKeyMan(desc);
5033  if (old_spk_man) {
5034  WalletLogPrintf("Update existing descriptor: %s\n",
5035  desc.descriptor->ToString());
5036 
5037  {
5038  LOCK(old_spk_man->cs_desc_man);
5039  new_spk_man->SetCache(old_spk_man->GetWalletDescriptor().cache);
5040  }
5041 
5042  // Remove from maps of active spkMans
5043  auto old_spk_man_id = old_spk_man->GetID();
5044  for (bool internal : {false, true}) {
5045  for (OutputType t : OUTPUT_TYPES) {
5046  auto active_spk_man = GetScriptPubKeyMan(t, internal);
5047  if (active_spk_man &&
5048  active_spk_man->GetID() == old_spk_man_id) {
5049  if (internal) {
5050  m_internal_spk_managers.erase(t);
5051  } else {
5052  m_external_spk_managers.erase(t);
5053  }
5054  break;
5055  }
5056  }
5057  }
5058  m_spk_managers.erase(old_spk_man_id);
5059  }
5060 
5061  // Add the private keys to the descriptor
5062  for (const auto &entry : signing_provider.keys) {
5063  const CKey &key = entry.second;
5064  new_spk_man->AddDescriptorKey(key, key.GetPubKey());
5065  }
5066 
5067  // Top up key pool, the manager will generate new scriptPubKeys internally
5068  new_spk_man->TopUp();
5069 
5070  // Apply the label if necessary
5071  // Note: we disable labels for ranged descriptors
5072  if (!desc.descriptor->IsRange()) {
5073  auto script_pub_keys = new_spk_man->GetScriptPubKeys();
5074  if (script_pub_keys.empty()) {
5076  "Could not generate scriptPubKeys (cache is empty)\n");
5077  return nullptr;
5078  }
5079 
5080  CTxDestination dest;
5081  if (ExtractDestination(script_pub_keys.at(0), dest)) {
5082  SetAddressBook(dest, label, "receive");
5083  }
5084  }
5085 
5086  // Save the descriptor to memory
5087  auto ret = new_spk_man.get();
5088  m_spk_managers[new_spk_man->GetID()] = std::move(new_spk_man);
5089 
5090  // Save the descriptor to DB
5091  ret->WriteDescriptor();
5092 
5093  return ret;
5094 }
CWallet::CWallet
CWallet(interfaces::Chain *chain, const std::string &name, std::unique_ptr< WalletDatabase > _database)
Construct wallet with specified name and database implementation.
Definition: wallet.h:850
CWallet::GetKeyPoolSize
unsigned int GetKeyPoolSize() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:3717
HandleLoadWallet
std::unique_ptr< interfaces::Handler > HandleLoadWallet(LoadWalletFn load_wallet)
Definition: wallet.cpp:163
DatabaseOptions
Definition: db.h:221
GetSerializeSize
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1182
CTxIn
An input of a transaction.
Definition: transaction.h:61
HIGH_MAX_TX_FEE
constexpr Amount HIGH_MAX_TX_FEE
-maxtxfee will warn if called with a higher fee than this amount (in satoshis)
Definition: wallet.h:113
CCoinControl::Select
void Select(const COutPoint &output)
Definition: coincontrol.h:58
CCoinControl::fAllowOtherInputs
bool fAllowOtherInputs
If false, allows unselected inputs, but requires all selected inputs be used.
Definition: coincontrol.h:30
CWallet::WalletLogPrintf
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:1464
policy.h
CWallet::AddDestData
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:4165
CWallet::ComputeTimeSmart
unsigned int ComputeTimeSmart(const CWalletTx &wtx) const
Compute smart timestamp for a transaction being added to the wallet.
Definition: wallet.cpp:4123
CWallet::ConnectScriptPubKeyManNotifiers
void ConnectScriptPubKeyManNotifiers()
Connect the signals from ScriptPubKeyMans to the signals in CWallet.
Definition: wallet.cpp:4914
ReserveDestination::fInternal
bool fInternal
Whether this is from the internal (change output) keypool.
Definition: wallet.h:174
CTransaction::vin
const std::vector< CTxIn > vin
Definition: transaction.h:210
CWallet::m_external_spk_managers
std::map< OutputType, ScriptPubKeyMan * > m_external_spk_managers
Definition: wallet.h:806
CoinSelectionParams::change_output_size
size_t change_output_size
Definition: wallet.h:655
DatabaseStatus::FAILED_BAD_PATH
@ FAILED_BAD_PATH
CWallet::GetActiveScriptPubKeyMans
std::set< ScriptPubKeyMan * > GetActiveScriptPubKeyMans() const
Returns all unique ScriptPubKeyMans in m_internal_spk_managers and m_external_spk_managers.
Definition: wallet.cpp:4790
GetLocktimeForNewTransaction
static uint32_t GetLocktimeForNewTransaction(interfaces::Chain &chain, const BlockHash &block_hash, int block_height)
Return a height-based locktime for new transactions (uses the height of the current chain tip unless ...
Definition: wallet.cpp:3050
MoneyRange
bool MoneyRange(const Amount nValue)
Definition: amount.h:176
CWallet::ScanResult::USER_ABORT
@ USER_ABORT
Definition: wallet.h:1071
CWalletTx::GetBlocksToMaturity
int GetBlocksToMaturity() const
Definition: wallet.cpp:4672
bip32.h
CCrypter::Decrypt
bool Decrypt(const std::vector< uint8_t > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
Definition: crypter.cpp:100
CTxOut::nValue
Amount nValue
Definition: transaction.h:132
CWallet::UnlockCoin
void UnlockCoin(const COutPoint &output) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:4010
CWallet::FillPSBT
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:2918
CWallet::nMasterKeyMaxID
unsigned int nMasterKeyMaxID
Definition: wallet.h:847
ArgsManager::GetBoolArg
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:517
interfaces::Chain::updateRwSetting
virtual bool updateRwSetting(const std::string &name, const util::SettingsValue &value)=0
Write a setting to <datadir>/settings.json.
WALLET_CRYPTO_KEY_SIZE
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:12
IsCurrentForAntiFeeSniping
static bool IsCurrentForAntiFeeSniping(interfaces::Chain &chain, const BlockHash &block_hash)
Definition: wallet.cpp:3030
CoinSelectionParams::effective_fee
CFeeRate effective_fee
Definition: wallet.h:657
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:55
CWalletTx::IsEquivalentTo
bool IsEquivalentTo(const CWalletTx &tx) const
Definition: wallet.cpp:2323
OutputType
OutputType
Definition: outputtype.h:17
ToString
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:69
DUMMY_P2PKH_INPUT_SIZE
static constexpr size_t DUMMY_P2PKH_INPUT_SIZE
Pre-calculated constants for input size estimation.
Definition: wallet.h:115
count
static int count
Definition: tests.c:41
CWallet::ListCoins
std::map< CTxDestination, std::vector< COutput > > ListCoins() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Return list of available coins and locked coins grouped by non-change output address.
Definition: wallet.cpp:2597
CWallet::GetAddressGroupings
std::set< std::set< CTxDestination > > GetAddressGroupings() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:3844
FEATURE_HD_SPLIT
@ FEATURE_HD_SPLIT
Definition: walletutil.h:28
CWallet::GetChainParams
const CChainParams & GetChainParams() const override
Definition: wallet.cpp:389
CWallet::ResendWalletTransactions
void ResendWalletTransactions()
Definition: wallet.cpp:2346
MakeTransactionRef
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:320
LOCKTIME_THRESHOLD
static const unsigned int LOCKTIME_THRESHOLD
Definition: script.h:39
check.h
CWalletTx::setConflicted
void setConflicted()
Definition: wallet.h:577
CWalletTx::GetId
TxId GetId() const
Definition: wallet.h:586
CWallet::GetSolvingProvider
std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const
Get the SigningProvider for a script.
Definition: wallet.cpp:4856
CWalletTx::GetCachableAmount
Amount GetCachableAmount(AmountType type, const isminefilter &filter, bool recalculate=false) const
Definition: wallet.cpp:2136
wallet.h
CHECK_NONFATAL
#define CHECK_NONFATAL(condition)
Throw a NonFatalCheckError when the condition evaluates to false.
Definition: check.h:34
OutputGroup
Definition: coinselection.h:80
CoinSelectionParams::use_bnb
bool use_bnb
Definition: wallet.h:654
HIGH_TX_FEE_PER_KB
constexpr Amount HIGH_TX_FEE_PER_KB
Discourage users to set fees higher than this amount (in satoshis) per kB.
Definition: wallet.h:110
interfaces::MakeWallet
std::unique_ptr< Wallet > MakeWallet(const std::shared_ptr< CWallet > &wallet)
Return implementation of Wallet interface.
Definition: dummywallet.cpp:44
CreateWallet
std::shared_ptr< CWallet > CreateWallet(interfaces::Chain &chain, const std::string &name, std::optional< bool > load_on_start, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error, std::vector< bilingual_str > &warnings)
Definition: wallet.cpp:281
CKey::MakeNewKey
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:183
CWallet::GetAvailableBalance
Amount GetAvailableBalance(const CCoinControl *coinControl=nullptr) const
Definition: wallet.cpp:2434
CKeyPool
A key from a CWallet's keypool.
Definition: scriptpubkeyman.h:106
CWallet::blockDisconnected
void blockDisconnected(const CBlock &block, int height) override
Definition: wallet.cpp:1366
COINBASE_MATURITY
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule).
Definition: consensus.h:32
isminefilter
uint8_t isminefilter
Definition: wallet.h:41
CBlockHeader::IsNull
bool IsNull() const
Definition: block.h:48
CWallet::SetMinVersion
void SetMinVersion(enum WalletFeature, WalletBatch *batch_in=nullptr, bool fExplicit=false) override
signify that a particular wallet feature is now used.
Definition: wallet.cpp:531
fs.h
CWallet::HasWalletSpend
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:601
CWallet::FundTransaction
bool FundTransaction(CMutableTransaction &tx, Amount &nFeeRet, int &nChangePosInOut, bilingual_str &error, bool lockUnspents, const std::set< int > &setSubtractFeeFromOutputs, CCoinControl coinControl)
Insert additional inputs into the transaction by calling CreateTransaction();.
Definition: wallet.cpp:2975
sighashtype.h
DBErrors::NEED_REWRITE
@ NEED_REWRITE
CCoinControl::ListSelected
void ListSelected(std::vector< COutPoint > &vOutpoints) const
Definition: coincontrol.h:64
CWallet::IsFromMe
bool IsFromMe(const CTransaction &tx) const
should probably be renamed to IsRelevantToMe
Definition: wallet.cpp:1507
CWallet::NotifyStatusChanged
boost::signals2::signal< void(CWallet *wallet)> NotifyStatusChanged
Wallet status (encrypted, locked) changed.
Definition: wallet.h:1368
CWallet::ReacceptWalletTransactions
void ReacceptWalletTransactions() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2057
flags
int flags
Definition: bitcoin-tx.cpp:532
FormatOutputType
const std::string & FormatOutputType(OutputType type)
Definition: outputtype.cpp:27
key_io.h
interfaces::Chain::findNextBlock
virtual bool findNextBlock(const BlockHash &block_hash, int block_height, const FoundBlock &next={}, bool *reorg=nullptr)=0
Find next block if block is part of current chain.
CKey::begin
const uint8_t * begin() const
Definition: key.h:90
CWalletTx::fTimeReceivedIsTxTime
unsigned int fTimeReceivedIsTxTime
Definition: wallet.h:318
CWallet::TxItems
std::multimap< int64_t, CWalletTx * > TxItems
Definition: wallet.h:871
CCoinControl::m_avoid_partial_spends
bool m_avoid_partial_spends
Avoid partial use of funds sent to a given address.
Definition: coincontrol.h:40
ISMINE_ALL
@ ISMINE_ALL
Definition: ismine.h:23
CWallet::GetOldestKeyPoolTime
int64_t GetOldestKeyPoolTime() const
Definition: wallet.cpp:3773
CWalletTx::AVAILABLE_CREDIT
@ AVAILABLE_CREDIT
Definition: wallet.h:346
DUMMY_SIGNATURE_CREATOR
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
Definition: sign.cpp:418
DatabaseStatus::FAILED_VERIFY
@ FAILED_VERIFY
OutputType::LEGACY
@ LEGACY
CWallet::m_wallet_flags
std::atomic< uint64_t > m_wallet_flags
Definition: wallet.h:763
moneystr.h
CWallet::GetLastBlockHeight
int GetLastBlockHeight() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Get last block processed height.
Definition: wallet.h:1520
DatabaseOptions::create_passphrase
SecureString create_passphrase
Definition: db.h:225
CWallet::GetBalance
Balance GetBalance(int min_depth=0, bool avoid_reuse=true) const
Definition: wallet.cpp:2406
transaction.h
WalletBatch::LoadWallet
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:768
CWallet::IsWalletFlagSet
bool IsWalletFlagSet(uint64_t flag) const override
Check if a certain wallet flag is set.
Definition: wallet.cpp:1629
GetDustThreshold
Amount GetDustThreshold(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:14
DescriptorScriptPubKeyMan::HasWalletDescriptor
bool HasWalletDescriptor(const WalletDescriptor &desc) const
Definition: scriptpubkeyman.cpp:2322
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:243
CWallet::AddWalletDescriptor
ScriptPubKeyMan * AddWalletDescriptor(WalletDescriptor &desc, const FlatSigningProvider &signing_provider, const std::string &label)
Add a descriptor to the wallet, return a ScriptPubKeyMan & associated output type.
Definition: wallet.cpp:5018
CWallet::CanSupportFeature
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:902
string.h
CWalletTx::mapValue
mapValue_t mapValue
Key/value map with information about the transaction.
Definition: wallet.h:316
CWallet::LoadDescriptorScriptPubKeyMan
void LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor &desc)
Instantiate a descriptor ScriptPubKeyMan from the WalletDescriptor and load it.
Definition: wallet.cpp:4922
ISMINE_WATCH_ONLY
@ ISMINE_WATCH_ONLY
Definition: ismine.h:20
CWallet::IsChange
bool IsChange(const CTxOut &txout) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:1459
vCoins
static std::vector< COutput > vCoins
Definition: coinselector_tests.cpp:35
CWallet::WalletLogPrintfToBeContinued
void WalletLogPrintfToBeContinued(std::string fmt, Params... parameters) const
Definition: wallet.h:1469
bilingual_str
Bilingual messages:
Definition: translation.h:17
interfaces::Chain::isInitialBlockDownload
virtual bool isInitialBlockDownload()=0
Check if in IBD.
DEFAULT_WALLET_REJECT_LONG_CHAINS
static const bool DEFAULT_WALLET_REJECT_LONG_CHAINS
Default for -walletrejectlongchains.
Definition: wallet.h:104
CMasterKey::vchCryptedKey
std::vector< uint8_t > vchCryptedKey
Definition: crypter.h:33
RemoveWallet
bool RemoveWallet(const std::shared_ptr< CWallet > &wallet, std::optional< bool > load_on_start, std::vector< bilingual_str > &warnings)
Definition: wallet.cpp:117
CWallet::ScanResult::last_scanned_height
std::optional< int > last_scanned_height
Definition: wallet.h:1077
CWalletTx::IsImmatureCoinBase
bool IsImmatureCoinBase() const
Definition: wallet.cpp:4683
CCoinControl
Coin Control Features.
Definition: coincontrol.h:21
ArgsManager::IsArgSet
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:400
CWallet::GetLastBlockHash
BlockHash GetLastBlockHash() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.h:1525
ReleaseWallet
static void ReleaseWallet(CWallet *wallet)
Definition: wallet.cpp:182
FlatSigningProvider::keys
std::map< CKeyID, CKey > keys
Definition: signingprovider.h:62
validation.h
CWallet::SetMaxVersion
bool SetMaxVersion(int nVersion)
change which version we're allowed to upgrade to (note that this does not immediately imply upgrading...
Definition: wallet.cpp:559
PartiallySignedTransaction::inputs
std::vector< PSBTInput > inputs
Definition: psbt.h:337
CWallet::GetEncryptionKey
const CKeyingMaterial & GetEncryptionKey() const override
Definition: wallet.cpp:4906
CWallet::chain
interfaces::Chain & chain() const
Interface for accessing chain state.
Definition: wallet.h:890
ReserveDestination::address
CTxDestination address
The destination.
Definition: wallet.h:172
CKeyingMaterial
std::vector< uint8_t, secure_allocator< uint8_t > > CKeyingMaterial
Definition: crypter.h:57
CWalletTx::setUnconfirmed
void setUnconfirmed()
Definition: wallet.h:581
CT_DELETED
@ CT_DELETED
Definition: ui_change_type.h:9
CChainParams
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:47
CWallet::m_scanning_progress
std::atomic< double > m_scanning_progress
Definition: wallet.h:690
CWallet::IsCrypted
bool IsCrypted() const
Definition: wallet.cpp:4749
base_blob::SetNull
void SetNull()
Definition: uint256.h:39
WalletBatch::WriteMasterKey
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition: walletdb.cpp:154
CWalletTx::AmountType
AmountType
Definition: wallet.h:342
CWallet::UpdateWalletTxFn
std::function< bool(CWalletTx &wtx, bool new_tx)> UpdateWalletTxFn
Callback for updating transaction metadata in mapWallet.
Definition: wallet.h:1054
CWallet::FindNonChangeParentOutput
const CTxOut & FindNonChangeParentOutput(const CTransaction &tx, int output) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Find non-change parent output.
Definition: wallet.cpp:2647
ReserveDestination::pwallet
const CWallet *const pwallet
The wallet to reserve from.
Definition: wallet.h:164
CWalletTx::GetDepthInMainChain
int GetDepthInMainChain() const NO_THREAD_SAFETY_ANALYSIS
Return depth of transaction in blockchain: <0 : conflicts with a transaction this deep in the blockch...
Definition: wallet.cpp:4661
OUTPUT_GROUP_MAX_ENTRIES
static const size_t OUTPUT_GROUP_MAX_ENTRIES
Definition: wallet.cpp:221
GetTime
int64_t GetTime()
DEPRECATED Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:27
CWallet::IsSpentKey
bool IsSpentKey(const TxId &txid, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:926
interfaces::Chain::findFirstBlockWithTimeAndHeight
virtual std::optional< int > findFirstBlockWithTimeAndHeight(int64_t time, int height, BlockHash *hash)=0
Return height of the first block in the chain with timestamp equal or greater than the given time and...
MakeWalletDatabase
std::unique_ptr< WalletDatabase > MakeWalletDatabase(const std::string &name, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error_string)
Definition: wallet.cpp:4221
CWallet::NotifyCanGetAddressesChanged
boost::signals2::signal< void()> NotifyCanGetAddressesChanged
Keypool has new keys.
Definition: wallet.h:1362
WITH_LOCK
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:272
CWallet::NotifyWatchonlyChanged
boost::signals2::signal< void(bool fHaveWatchOnly)> NotifyWatchonlyChanged
Watch-only address added.
Definition: wallet.h:1359
DBErrors::NONCRITICAL_ERROR
@ NONCRITICAL_ERROR
AnnotatedMixin< std::recursive_mutex >
CWallet::ShowProgress
boost::signals2::signal< void(const std::string &title, int nProgress)> ShowProgress
Show progress e.g.
Definition: wallet.h:1356
CT_UPDATED
@ CT_UPDATED
Definition: ui_change_type.h:9
ScriptPubKeyMan::KeepDestination
virtual void KeepDestination(int64_t index, const OutputType &type)
Definition: scriptpubkeyman.h:203
ReserveDestination::nIndex
int64_t nIndex
The index of the address's key in the keypool.
Definition: wallet.h:170
CWallet::IsTrusted
bool IsTrusted(const CWalletTx &wtx, std::set< TxId > &trusted_parents) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2267
cs_wallets
static RecursiveMutex cs_wallets
Definition: wallet.cpp:48
FEATURE_WALLETCRYPT
@ FEATURE_WALLETCRYPT
Definition: walletutil.h:20
CWallet::EraseDestData
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:4175
CWallet::TransactionCanBeAbandoned
bool TransactionCanBeAbandoned(const TxId &txid) const
Return whether transaction can be abandoned.
Definition: wallet.cpp:1151
CWalletTx::isConfirmed
bool isConfirmed() const
Definition: wallet.h:582
CMasterKey
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key.
Definition: crypter.h:31
CWalletTx::m_confirm
Confirmation m_confirm
Definition: wallet.h:413
interfaces::Chain::getHeight
virtual std::optional< int > getHeight()=0
Get current chain height, not including genesis block (returns 0 if chain only contains genesis block...
CCoinControl::IsSelected
bool IsSelected(const COutPoint &output) const
Definition: coincontrol.h:54
LegacyScriptPubKeyMan::GetKeys
std::set< CKeyID > GetKeys() const override
Definition: scriptpubkeyman.cpp:1610
WalletRescanReserver::reserve
bool reserve()
Definition: wallet.h:1596
CWallet::GroupOutputs
std::vector< OutputGroup > GroupOutputs(const std::vector< COutput > &outputs, bool single_coin, const size_t max_ancestors) const
Definition: wallet.cpp:4689
CCoinControl::m_feerate
std::optional< CFeeRate > m_feerate
Override the wallet's m_pay_tx_fee if set.
Definition: coincontrol.h:36
interfaces::Chain::getBlockHash
virtual BlockHash getBlockHash(int height)=0
Get block hash. Height must be valid or this function will abort.
CWallet::AddToWallet
CWalletTx * AddToWallet(CTransactionRef tx, const CWalletTx::Confirmation &confirm, const UpdateWalletTxFn &update_wtx=nullptr, bool fFlushOnClose=true)
Definition: wallet.cpp:953
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
CWallet::GetBroadcastTransactions
bool GetBroadcastTransactions() const
Inquire whether this wallet broadcasts transactions.
Definition: wallet.h:1371
ReserveDestination
A wrapper to reserve an address from a wallet.
Definition: wallet.h:161
CWalletTx::m_is_cache_empty
bool m_is_cache_empty
This flag is true if all m_amounts caches are empty.
Definition: wallet.h:358
LegacyScriptPubKeyMan
Definition: scriptpubkeyman.h:320
interfaces::Chain::guessVerificationProgress
virtual double guessVerificationProgress(const BlockHash &block_hash)=0
Estimate fraction of total transactions verified if blocks up to the specified block hash are verifie...
CKeyPool::nTime
int64_t nTime
The time at which the key was generated. Set in AddKeypoolPubKeyWithDB.
Definition: scriptpubkeyman.h:109
CoinEligibilityFilter
Definition: coinselection.h:64
chainparams.h
CWallet::blockConnected
void blockConnected(const CBlock &block, int height) override
Definition: wallet.cpp:1351
CCoinControl::HasSelected
bool HasSelected() const
Definition: coincontrol.h:52
CWalletTx::GetSpendSize
int GetSpendSize(unsigned int out, bool use_max_sig=false) const
Definition: wallet.h:511
WalletBatch::WriteDestData
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
CWallet::ScanForWalletTransactions
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:1919
CWallet::SelectCoinsMinConf
bool SelectCoinsMinConf(const Amount nTargetValue, const CoinEligibilityFilter &eligibility_filter, std::vector< OutputGroup > groups, std::set< CInputCoin > &setCoinsRet, Amount &nValueRet, const CoinSelectionParams &coin_selection_params, bool &bnb_used) const
Shuffle and select coins until nTargetValue is reached while avoiding small change; This method is st...
Definition: wallet.cpp:2666
CWalletTx::Confirmation::block_height
int block_height
Definition: wallet.h:405
interfaces::Chain::getRwSetting
virtual util::SettingsValue getRwSetting(const std::string &name)=0
Return <datadir>/settings.json setting value.
GetRand
uint64_t GetRand(uint64_t nMax) noexcept
Generate a uniform random integer in the range [0..range).
Definition: random.cpp:650
GetAffectedKeys
std::vector< CKeyID > GetAffectedKeys(const CScript &spk, const SigningProvider &provider)
Definition: scriptpubkeyman.cpp:1447
CFeeRate
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
CWallet::GetNewChangeDestination
bool GetNewChangeDestination(const OutputType type, CTxDestination &dest, std::string &error)
Definition: wallet.cpp:3756
CWallet::UnsetBlankWalletFlag
void UnsetBlankWalletFlag(WalletBatch &batch) override
Unset the blank wallet flag and saves it to disk.
Definition: wallet.cpp:1625
CWallet::IsAllFromMe
bool IsAllFromMe(const CTransaction &tx, const isminefilter &filter) const
Returns whether all of the inputs match the filter.
Definition: wallet.cpp:1525
WalletBatch::WriteActiveScriptPubKeyMan
bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id, bool internal)
Definition: walletdb.cpp:213
SigningResult
SigningResult
Definition: message.h:47
CWalletTx::nChangeCached
Amount nChangeCached
Definition: wallet.h:361
CWalletTx::nTimeSmart
unsigned int nTimeSmart
Stable timestamp that never changes, and reflects the order a transaction was added to the wallet.
Definition: wallet.h:330
CCoinControl::m_confirm_target
std::optional< unsigned int > m_confirm_target
Override the default confirmation target if set.
Definition: coincontrol.h:38
UniValue
Definition: univalue.h:23
CWallet::SignMessage
SigningResult SignMessage(const std::string &message, const PKHash &pkhash, std::string &str_sig) const
Definition: wallet.cpp:2962
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:194
CWalletTx::IsTrusted
bool IsTrusted() const
Definition: wallet.cpp:2261
WalletBatch::TxnCommit
bool TxnCommit()
Commit current transaction.
Definition: walletdb.cpp:1106
CWallet::Balance
Definition: wallet.h:1095
balance
static Amount balance
Definition: coinselector_tests.cpp:37
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
AmountErrMsg
bilingual_str AmountErrMsg(const std::string &optname, const std::string &strValue)
Definition: error.cpp:51
txmempool.h
ShellEscape
std::string ShellEscape(const std::string &arg)
Definition: system.cpp:1279
CWallet::GetVersion
int GetVersion() const
get the current wallet format (the oldest client version guaranteed to understand this wallet)
Definition: wallet.h:1313
prefix
const char * prefix
Definition: rest.cpp:772
mempool.h
CCoinControl::m_change_type
std::optional< OutputType > m_change_type
Override the default change type if set, ignored if destChange is set.
Definition: coincontrol.h:25
CWallet::SetAddressBook
bool SetAddressBook(const CTxDestination &address, const std::string &strName, const std::string &purpose)
Definition: wallet.cpp:3665
AmountHighWarn
bilingual_str AmountHighWarn(const std::string &optname)
Definition: error.cpp:47
LoadWalletFn
std::function< void(std::unique_ptr< interfaces::Wallet > wallet)> LoadWalletFn
Definition: wallet.h:46
CWallet::GetDisplayName
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:1453
DescriptorScriptPubKeyMan
Definition: scriptpubkeyman.h:657
TxValidationState
Definition: validation.h:137
CWallet::MarkDirty
void MarkDirty()
Definition: wallet.cpp:895
FEATURE_PRE_SPLIT_KEYPOOL
@ FEATURE_PRE_SPLIT_KEYPOOL
Definition: walletutil.h:34
CWallet::LoadToWallet
bool LoadToWallet(const TxId &txid, const UpdateWalletTxFn &fill_wtx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:1047
signingprovider.h
CWallet::DelAddressBook
bool DelAddressBook(const CTxDestination &address)
Definition: wallet.cpp:3672
SATOSHI
static constexpr Amount SATOSHI
Definition: amount.h:153
CFeeRate::GetFeePerK
Amount GetFeePerK() const
Return the fee in satoshis for a size of 1000 bytes.
Definition: feerate.h:54
CTxOut::IsNull
bool IsNull() const
Definition: transaction.h:147
fees.h
DatabaseStatus::FAILED_ENCRYPT
@ FAILED_ENCRYPT
TransactionError
TransactionError
Definition: error.h:22
interfaces::Chain::havePruned
virtual bool havePruned()=0
Check if any block has been pruned.
interfaces::Chain
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:108
CWallet::IsLocked
bool IsLocked() const override
Definition: wallet.cpp:4753
SecureString
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:56
CWallet::Flush
void Flush()
Flush wallet (bitdb flush)
Definition: wallet.cpp:607
CWalletTx::nTimeReceived
unsigned int nTimeReceived
time received by this node
Definition: wallet.h:320
CWallet::CreateTransactionInternal
bool CreateTransactionInternal(const std::vector< CRecipient > &vecSend, CTransactionRef &tx, Amount &nFeeRet, int &nChangePosInOut, bilingual_str &error, const CCoinControl &coin_control, bool sign)
Definition: wallet.cpp:3112
CCoinControl::fAllowWatchOnly
bool fAllowWatchOnly
Includes watch only addresses which are solvable.
Definition: coincontrol.h:32
random.h
COutput::ToString
std::string ToString() const
Definition: wallet.cpp:384
GetRandInt
int GetRandInt(int nMax) noexcept
Definition: random.cpp:654
COutPoint::GetTxId
const TxId & GetTxId() const
Definition: transaction.h:37
CWalletTx::isAbandoned
bool isAbandoned() const
Definition: wallet.h:565
CWallet::UpgradeKeyMetadata
void UpgradeKeyMetadata() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Upgrade stored CKeyMetadata objects to store key origin info as KeyOriginInfo.
Definition: wallet.cpp:405
isminetype
isminetype
IsMine() return codes.
Definition: ismine.h:18
CWalletTx::m_it_wtxOrdered
std::multimap< int64_t, CWalletTx * >::const_iterator m_it_wtxOrdered
Definition: wallet.h:339
CWalletTx::Confirmation::status
Status status
Definition: wallet.h:404
CKeyPool::m_pre_split
bool m_pre_split
Whether this key was generated for a keypool before the wallet was upgraded to HD-split.
Definition: scriptpubkeyman.h:117
CWallet::Balance::m_watchonly_untrusted_pending
Amount m_watchonly_untrusted_pending
Definition: wallet.h:1103
CWallet::IsLockedCoin
bool IsLockedCoin(const COutPoint &outpoint) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:4020
DUMMY_MAXIMUM_SIGNATURE_CREATOR
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
Definition: sign.cpp:420
CWallet::LoadWalletFlags
bool LoadWalletFlags(uint64_t flags)
Loads the flags into the wallet.
Definition: wallet.cpp:1633
Untranslated
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36
SignatureData
Definition: sign.h:66
AssertLockNotHeld
#define AssertLockNotHeld(cs)
Definition: sync.h:92
CWalletTx::GetDebit
Amount GetDebit(const isminefilter &filter) const
filter decides which addresses will count towards the debit
Definition: wallet.cpp:2147
IsValidDestination
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:263
CWallet::MarkConflicted
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
CWallet::CommitTransaction
void CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector< std::pair< std::string, std::string >> orderForm)
Submit the transaction to the node's mempool and then relay to peers.
Definition: wallet.cpp:3531
CWalletTx::nOrderPos
int64_t nOrderPos
position in ordered transaction list
Definition: wallet.h:338
CachableAmount::Set
void Set(isminefilter filter, Amount value)
Definition: ismine.h:39
CTxOut
An output of a transaction.
Definition: transaction.h:130
CTransaction::GetId
const TxId GetId() const
Definition: transaction.h:244
WalletBatch::WriteOrderPosNext
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition: walletdb.cpp:193
CExtKey
Definition: key.h:164
CWallet::m_best_block_time
std::atomic< int64_t > m_best_block_time
Definition: wallet.h:705
Coin
A UTXO entry.
Definition: coins.h:27
CWallet::ImportScriptPubKeys
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:1738
CWallet::Balance::m_mine_trusted
Amount m_mine_trusted
Trusted, at depth=GetBalance.min_depth or more.
Definition: wallet.h:1097
CCoinControl::m_avoid_address_reuse
bool m_avoid_address_reuse
Forbids inclusion of dirty (previously used) addresses.
Definition: coincontrol.h:42
CWallet::ImportPrivKeys
bool ImportPrivKeys(const std::map< CKeyID, CKey > &privkey_map, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:1714
CWalletTx::fChangeCached
bool fChangeCached
Definition: wallet.h:359
GetMinimumFeeRate
CFeeRate GetMinimumFeeRate(const CWallet &wallet, const CCoinControl &coin_control)
Estimate the minimum fee rate considering user set parameters and the required fee.
Definition: fees.cpp:26
GetMinimumFee
Amount GetMinimumFee(const CWallet &wallet, unsigned int nTxBytes, const CCoinControl &coin_