LCOV - code coverage report
Current view: top level - src/wallet - wallettool.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 0.0 % 137 0
Test Date: 2024-08-28 04:44:32 Functions: 0.0 % 5 0
Branches: 0.0 % 307 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2016-2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <config/bitcoin-config.h> // IWYU pragma: keep
       6                 :             : 
       7                 :             : #include <wallet/wallettool.h>
       8                 :             : 
       9                 :             : #include <common/args.h>
      10                 :             : #include <util/fs.h>
      11                 :             : #include <util/translation.h>
      12                 :             : #include <wallet/dump.h>
      13                 :             : #include <wallet/salvage.h>
      14                 :             : #include <wallet/wallet.h>
      15                 :             : #include <wallet/walletutil.h>
      16                 :             : 
      17                 :             : namespace wallet {
      18                 :             : namespace WalletTool {
      19                 :             : 
      20                 :             : // The standard wallet deleter function blocks on the validation interface
      21                 :             : // queue, which doesn't exist for the bitcoin-wallet. Define our own
      22                 :             : // deleter here.
      23                 :           0 : static void WalletToolReleaseWallet(CWallet* wallet)
      24                 :             : {
      25                 :           0 :     wallet->WalletLogPrintf("Releasing wallet\n");
      26                 :           0 :     wallet->Close();
      27         [ #  # ]:           0 :     delete wallet;
      28                 :           0 : }
      29                 :             : 
      30                 :           0 : static void WalletCreate(CWallet* wallet_instance, uint64_t wallet_creation_flags)
      31                 :             : {
      32                 :           0 :     LOCK(wallet_instance->cs_wallet);
      33                 :             : 
      34         [ #  # ]:           0 :     wallet_instance->SetMinVersion(FEATURE_LATEST);
      35         [ #  # ]:           0 :     wallet_instance->InitWalletFlags(wallet_creation_flags);
      36                 :             : 
      37   [ #  #  #  # ]:           0 :     if (!wallet_instance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
      38         [ #  # ]:           0 :         auto spk_man = wallet_instance->GetOrCreateLegacyScriptPubKeyMan();
      39         [ #  # ]:           0 :         spk_man->SetupGeneration(false);
      40                 :             :     } else {
      41         [ #  # ]:           0 :         wallet_instance->SetupDescriptorScriptPubKeyMans();
      42                 :             :     }
      43                 :             : 
      44         [ #  # ]:           0 :     tfm::format(std::cout, "Topping up keypool...\n");
      45         [ #  # ]:           0 :     wallet_instance->TopUpKeyPool();
      46                 :           0 : }
      47                 :             : 
      48                 :           0 : static std::shared_ptr<CWallet> MakeWallet(const std::string& name, const fs::path& path, DatabaseOptions options)
      49                 :             : {
      50                 :           0 :     DatabaseStatus status;
      51         [ #  # ]:           0 :     bilingual_str error;
      52         [ #  # ]:           0 :     std::unique_ptr<WalletDatabase> database = MakeDatabase(path, options, status, error);
      53         [ #  # ]:           0 :     if (!database) {
      54         [ #  # ]:           0 :         tfm::format(std::cerr, "%s\n", error.original);
      55                 :           0 :         return nullptr;
      56                 :             :     }
      57                 :             : 
      58                 :             :     // dummy chain interface
      59   [ #  #  #  #  :           0 :     std::shared_ptr<CWallet> wallet_instance{new CWallet(/*chain=*/nullptr, name, std::move(database)), WalletToolReleaseWallet};
             #  #  #  # ]
      60                 :           0 :     DBErrors load_wallet_ret;
      61                 :           0 :     try {
      62         [ #  # ]:           0 :         load_wallet_ret = wallet_instance->LoadWallet();
      63         [ -  - ]:           0 :     } catch (const std::runtime_error&) {
      64         [ -  - ]:           0 :         tfm::format(std::cerr, "Error loading %s. Is wallet being used by another process?\n", name);
      65                 :           0 :         return nullptr;
      66                 :           0 :     }
      67                 :             : 
      68   [ #  #  #  #  :           0 :     if (load_wallet_ret != DBErrors::LOAD_OK) {
                #  #  # ]
      69                 :             :         if (load_wallet_ret == DBErrors::CORRUPT) {
      70         [ #  # ]:           0 :             tfm::format(std::cerr, "Error loading %s: Wallet corrupted", name);
      71                 :           0 :             return nullptr;
      72                 :             :         } else if (load_wallet_ret == DBErrors::NONCRITICAL_ERROR) {
      73         [ #  # ]:           0 :             tfm::format(std::cerr, "Error reading %s! All keys read correctly, but transaction data"
      74                 :             :                             " or address book entries might be missing or incorrect.",
      75                 :             :                 name);
      76                 :             :         } else if (load_wallet_ret == DBErrors::TOO_NEW) {
      77         [ #  # ]:           0 :             tfm::format(std::cerr, "Error loading %s: Wallet requires newer version of %s",
      78                 :             :                 name, PACKAGE_NAME);
      79                 :           0 :             return nullptr;
      80                 :             :         } else if (load_wallet_ret == DBErrors::NEED_REWRITE) {
      81         [ #  # ]:           0 :             tfm::format(std::cerr, "Wallet needed to be rewritten: restart %s to complete", PACKAGE_NAME);
      82                 :           0 :             return nullptr;
      83                 :             :         } else if (load_wallet_ret == DBErrors::NEED_RESCAN) {
      84         [ #  # ]:           0 :             tfm::format(std::cerr, "Error reading %s! Some transaction data might be missing or"
      85                 :             :                            " incorrect. Wallet requires a rescan.",
      86                 :             :                 name);
      87                 :             :         } else {
      88         [ #  # ]:           0 :             tfm::format(std::cerr, "Error loading %s", name);
      89                 :           0 :             return nullptr;
      90                 :             :         }
      91                 :             :     }
      92                 :             : 
      93   [ #  #  #  # ]:           0 :     if (options.require_create) WalletCreate(wallet_instance.get(), options.create_flags);
      94                 :             : 
      95                 :           0 :     return wallet_instance;
      96                 :           0 : }
      97                 :             : 
      98                 :           0 : static void WalletShowInfo(CWallet* wallet_instance)
      99                 :             : {
     100                 :           0 :     LOCK(wallet_instance->cs_wallet);
     101                 :             : 
     102         [ #  # ]:           0 :     tfm::format(std::cout, "Wallet info\n===========\n");
     103         [ #  # ]:           0 :     tfm::format(std::cout, "Name: %s\n", wallet_instance->GetName());
     104   [ #  #  #  # ]:           0 :     tfm::format(std::cout, "Format: %s\n", wallet_instance->GetDatabase().Format());
     105   [ #  #  #  #  :           0 :     tfm::format(std::cout, "Descriptors: %s\n", wallet_instance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS) ? "yes" : "no");
                   #  # ]
     106   [ #  #  #  #  :           0 :     tfm::format(std::cout, "Encrypted: %s\n", wallet_instance->IsCrypted() ? "yes" : "no");
                   #  # ]
     107   [ #  #  #  #  :           0 :     tfm::format(std::cout, "HD (hd seed available): %s\n", wallet_instance->IsHDEnabled() ? "yes" : "no");
                   #  # ]
     108   [ #  #  #  # ]:           0 :     tfm::format(std::cout, "Keypool Size: %u\n", wallet_instance->GetKeyPoolSize());
     109         [ #  # ]:           0 :     tfm::format(std::cout, "Transactions: %zu\n", wallet_instance->mapWallet.size());
     110         [ #  # ]:           0 :     tfm::format(std::cout, "Address Book: %zu\n", wallet_instance->m_address_book.size());
     111                 :           0 : }
     112                 :             : 
     113                 :           0 : bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command)
     114                 :             : {
     115   [ #  #  #  #  :           0 :     if (args.IsArgSet("-format") && command != "createfromdump") {
             #  #  #  # ]
     116                 :           0 :         tfm::format(std::cerr, "The -format option can only be used with the \"createfromdump\" command.\n");
     117                 :           0 :         return false;
     118                 :             :     }
     119   [ #  #  #  #  :           0 :     if (args.IsArgSet("-dumpfile") && command != "dump" && command != "createfromdump") {
          #  #  #  #  #  
                      # ]
     120                 :           0 :         tfm::format(std::cerr, "The -dumpfile option can only be used with the \"dump\" and \"createfromdump\" commands.\n");
     121                 :           0 :         return false;
     122                 :             :     }
     123   [ #  #  #  #  :           0 :     if (args.IsArgSet("-descriptors") && command != "create") {
             #  #  #  # ]
     124                 :           0 :         tfm::format(std::cerr, "The -descriptors option can only be used with the 'create' command.\n");
     125                 :           0 :         return false;
     126                 :             :     }
     127   [ #  #  #  #  :           0 :     if (args.IsArgSet("-legacy") && command != "create") {
             #  #  #  # ]
     128                 :           0 :         tfm::format(std::cerr, "The -legacy option can only be used with the 'create' command.\n");
     129                 :           0 :         return false;
     130                 :             :     }
     131   [ #  #  #  #  :           0 :     if (command == "create" && !args.IsArgSet("-wallet")) {
             #  #  #  # ]
     132                 :           0 :         tfm::format(std::cerr, "Wallet name must be provided when creating a new wallet.\n");
     133                 :           0 :         return false;
     134                 :             :     }
     135   [ #  #  #  # ]:           0 :     const std::string name = args.GetArg("-wallet", "");
     136   [ #  #  #  #  :           0 :     const fs::path path = fsbridge::AbsPathJoin(GetWalletDir(), fs::PathFromString(name));
                   #  # ]
     137                 :             : 
     138         [ #  # ]:           0 :     if (command == "create") {
     139         [ #  # ]:           0 :         DatabaseOptions options;
     140         [ #  # ]:           0 :         ReadDatabaseArgs(args, options);
     141                 :           0 :         options.require_create = true;
     142                 :             :         // If -legacy is set, use it. Otherwise default to false.
     143   [ #  #  #  # ]:           0 :         bool make_legacy = args.GetBoolArg("-legacy", false);
     144                 :             :         // If neither -legacy nor -descriptors is set, default to true. If -descriptors is set, use its value.
     145   [ #  #  #  #  :           0 :         bool make_descriptors = (!args.IsArgSet("-descriptors") && !args.IsArgSet("-legacy")) || (args.IsArgSet("-descriptors") && args.GetBoolArg("-descriptors", true));
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     146         [ #  # ]:           0 :         if (make_legacy && make_descriptors) {
     147         [ #  # ]:           0 :             tfm::format(std::cerr, "Only one of -legacy or -descriptors can be set to true, not both\n");
     148                 :             :             return false;
     149                 :             :         }
     150         [ #  # ]:           0 :         if (!make_legacy && !make_descriptors) {
     151         [ #  # ]:           0 :             tfm::format(std::cerr, "One of -legacy or -descriptors must be set to true (or omitted)\n");
     152                 :             :             return false;
     153                 :             :         }
     154         [ #  # ]:           0 :         if (make_descriptors) {
     155                 :           0 :             options.create_flags |= WALLET_FLAG_DESCRIPTORS;
     156                 :           0 :             options.require_format = DatabaseFormat::SQLITE;
     157                 :             :         }
     158                 :             : 
     159   [ #  #  #  # ]:           0 :         const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
     160         [ #  # ]:           0 :         if (wallet_instance) {
     161         [ #  # ]:           0 :             WalletShowInfo(wallet_instance.get());
     162         [ #  # ]:           0 :             wallet_instance->Close();
     163                 :             :         }
     164         [ #  # ]:           0 :     } else if (command == "info") {
     165         [ #  # ]:           0 :         DatabaseOptions options;
     166         [ #  # ]:           0 :         ReadDatabaseArgs(args, options);
     167                 :           0 :         options.require_existing = true;
     168   [ #  #  #  # ]:           0 :         const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
     169   [ #  #  #  # ]:           0 :         if (!wallet_instance) return false;
     170         [ #  # ]:           0 :         WalletShowInfo(wallet_instance.get());
     171         [ #  # ]:           0 :         wallet_instance->Close();
     172         [ #  # ]:           0 :     } else if (command == "salvage") {
     173                 :             : #ifdef USE_BDB
     174         [ #  # ]:           0 :         bilingual_str error;
     175                 :           0 :         std::vector<bilingual_str> warnings;
     176         [ #  # ]:           0 :         bool ret = RecoverDatabaseFile(args, path, error, warnings);
     177         [ #  # ]:           0 :         if (!ret) {
     178         [ #  # ]:           0 :             for (const auto& warning : warnings) {
     179         [ #  # ]:           0 :                 tfm::format(std::cerr, "%s\n", warning.original);
     180                 :             :             }
     181         [ #  # ]:           0 :             if (!error.empty()) {
     182         [ #  # ]:           0 :                 tfm::format(std::cerr, "%s\n", error.original);
     183                 :             :             }
     184                 :             :         }
     185                 :           0 :         return ret;
     186                 :             : #else
     187                 :             :         tfm::format(std::cerr, "Salvage command is not available as BDB support is not compiled");
     188                 :             :         return false;
     189                 :             : #endif
     190         [ #  # ]:           0 :     } else if (command == "dump") {
     191         [ #  # ]:           0 :         DatabaseOptions options;
     192         [ #  # ]:           0 :         ReadDatabaseArgs(args, options);
     193                 :           0 :         options.require_existing = true;
     194                 :           0 :         DatabaseStatus status;
     195                 :             : 
     196   [ #  #  #  #  :           0 :         if (args.GetBoolArg("-withinternalbdb", false) && IsBDBFile(BDBDataFile(path))) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     197                 :           0 :             options.require_format = DatabaseFormat::BERKELEY_RO;
     198                 :             :         }
     199                 :             : 
     200         [ #  # ]:           0 :         bilingual_str error;
     201         [ #  # ]:           0 :         std::unique_ptr<WalletDatabase> database = MakeDatabase(path, options, status, error);
     202         [ #  # ]:           0 :         if (!database) {
     203         [ #  # ]:           0 :             tfm::format(std::cerr, "%s\n", error.original);
     204                 :             :             return false;
     205                 :             :         }
     206                 :             : 
     207         [ #  # ]:           0 :         bool ret = DumpWallet(args, *database, error);
     208   [ #  #  #  # ]:           0 :         if (!ret && !error.empty()) {
     209         [ #  # ]:           0 :             tfm::format(std::cerr, "%s\n", error.original);
     210                 :             :             return ret;
     211                 :             :         }
     212         [ #  # ]:           0 :         tfm::format(std::cout, "The dumpfile may contain private keys. To ensure the safety of your Bitcoin, do not share the dumpfile.\n");
     213                 :             :         return ret;
     214         [ #  # ]:           0 :     } else if (command == "createfromdump") {
     215         [ #  # ]:           0 :         bilingual_str error;
     216                 :           0 :         std::vector<bilingual_str> warnings;
     217         [ #  # ]:           0 :         bool ret = CreateFromDump(args, name, path, error, warnings);
     218         [ #  # ]:           0 :         for (const auto& warning : warnings) {
     219         [ #  # ]:           0 :             tfm::format(std::cout, "%s\n", warning.original);
     220                 :             :         }
     221   [ #  #  #  # ]:           0 :         if (!ret && !error.empty()) {
     222         [ #  # ]:           0 :             tfm::format(std::cerr, "%s\n", error.original);
     223                 :             :         }
     224                 :           0 :         return ret;
     225                 :           0 :     } else {
     226         [ #  # ]:           0 :         tfm::format(std::cerr, "Invalid command: %s\n", command);
     227                 :             :         return false;
     228                 :             :     }
     229                 :             : 
     230                 :             :     return true;
     231                 :           0 : }
     232                 :             : } // namespace WalletTool
     233                 :             : } // namespace wallet
        

Generated by: LCOV version 2.0-1