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