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