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