LCOV - code coverage report
Current view: top level - src/wallet/rpc - util.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 0.0 % 80 0
Test Date: 2025-07-10 04:10:27 Functions: 0.0 % 9 0
Branches: 0.0 % 113 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2011-present 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 <wallet/rpc/util.h>
       6                 :             : 
       7                 :             : #include <common/url.h>
       8                 :             : #include <rpc/util.h>
       9                 :             : #include <util/any.h>
      10                 :             : #include <util/translation.h>
      11                 :             : #include <wallet/context.h>
      12                 :             : #include <wallet/wallet.h>
      13                 :             : 
      14                 :             : #include <string_view>
      15                 :             : #include <univalue.h>
      16                 :             : 
      17                 :             : namespace wallet {
      18                 :             : static const std::string WALLET_ENDPOINT_BASE = "/wallet/";
      19                 :             : const std::string HELP_REQUIRING_PASSPHRASE{"\nRequires wallet passphrase to be set with walletpassphrase call if wallet is encrypted.\n"};
      20                 :             : 
      21                 :           0 : bool GetAvoidReuseFlag(const CWallet& wallet, const UniValue& param) {
      22                 :           0 :     bool can_avoid_reuse = wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE);
      23         [ #  # ]:           0 :     bool avoid_reuse = param.isNull() ? can_avoid_reuse : param.get_bool();
      24                 :             : 
      25         [ #  # ]:           0 :     if (avoid_reuse && !can_avoid_reuse) {
      26   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_WALLET_ERROR, "wallet does not have the \"avoid reuse\" feature enabled");
      27                 :             :     }
      28                 :             : 
      29                 :           0 :     return avoid_reuse;
      30                 :             : }
      31                 :             : 
      32                 :           0 : bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string& wallet_name)
      33                 :             : {
      34         [ #  # ]:           0 :     if (request.URI.starts_with(WALLET_ENDPOINT_BASE)) {
      35                 :             :         // wallet endpoint was used
      36                 :           0 :         wallet_name = UrlDecode(std::string_view{request.URI}.substr(WALLET_ENDPOINT_BASE.size()));
      37                 :           0 :         return true;
      38                 :             :     }
      39                 :             :     return false;
      40                 :             : }
      41                 :             : 
      42                 :           0 : std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
      43                 :             : {
      44                 :           0 :     CHECK_NONFATAL(request.mode == JSONRPCRequest::EXECUTE);
      45                 :           0 :     WalletContext& context = EnsureWalletContext(request.context);
      46                 :             : 
      47         [ #  # ]:           0 :     std::string wallet_name;
      48   [ #  #  #  # ]:           0 :     if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
      49         [ #  # ]:           0 :         std::shared_ptr<CWallet> pwallet = GetWallet(context, wallet_name);
      50   [ #  #  #  #  :           0 :         if (!pwallet) throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
                   #  # ]
      51                 :             :         return pwallet;
      52                 :           0 :     }
      53                 :             : 
      54                 :           0 :     size_t count{0};
      55         [ #  # ]:           0 :     auto wallet = GetDefaultWallet(context, count);
      56   [ #  #  #  # ]:           0 :     if (wallet) return wallet;
      57                 :             : 
      58         [ #  # ]:           0 :     if (count == 0) {
      59         [ #  # ]:           0 :         throw JSONRPCError(
      60   [ #  #  #  # ]:           0 :             RPC_WALLET_NOT_FOUND, "No wallet is loaded. Load a wallet using loadwallet or create a new one with createwallet. (Note: A default wallet is no longer automatically created)");
      61                 :             :     }
      62         [ #  # ]:           0 :     throw JSONRPCError(RPC_WALLET_NOT_SPECIFIED,
      63   [ #  #  #  # ]:           0 :         "Multiple wallets are loaded. Please select which wallet to use by requesting the RPC through the /wallet/<walletname> URI path.");
      64                 :           0 : }
      65                 :             : 
      66                 :           0 : void EnsureWalletIsUnlocked(const CWallet& wallet)
      67                 :             : {
      68         [ #  # ]:           0 :     if (wallet.IsLocked()) {
      69   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please enter the wallet passphrase with walletpassphrase first.");
      70                 :             :     }
      71                 :           0 : }
      72                 :             : 
      73                 :           0 : WalletContext& EnsureWalletContext(const std::any& context)
      74                 :             : {
      75                 :           0 :     auto wallet_context = util::AnyPtr<WalletContext>(context);
      76         [ #  # ]:           0 :     if (!wallet_context) {
      77   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet context not found");
      78                 :             :     }
      79                 :           0 :     return *wallet_context;
      80                 :             : }
      81                 :             : 
      82                 :           0 : std::string LabelFromValue(const UniValue& value)
      83                 :             : {
      84   [ #  #  #  # ]:           0 :     static const std::string empty_string;
      85         [ #  # ]:           0 :     if (value.isNull()) return empty_string;
      86                 :             : 
      87                 :           0 :     const std::string& label{value.get_str()};
      88         [ #  # ]:           0 :     if (label == "*")
      89   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, "Invalid label name");
      90                 :           0 :     return label;
      91                 :             : }
      92                 :             : 
      93                 :           0 : void PushParentDescriptors(const CWallet& wallet, const CScript& script_pubkey, UniValue& entry)
      94                 :             : {
      95                 :           0 :     UniValue parent_descs(UniValue::VARR);
      96   [ #  #  #  # ]:           0 :     for (const auto& desc: wallet.GetWalletDescriptors(script_pubkey)) {
      97                 :           0 :         std::string desc_str;
      98                 :           0 :         FlatSigningProvider dummy_provider;
      99   [ #  #  #  #  :           0 :         if (!CHECK_NONFATAL(desc.descriptor->ToNormalizedString(dummy_provider, desc_str, &desc.cache))) continue;
                   #  # ]
     100   [ #  #  #  # ]:           0 :         parent_descs.push_back(desc_str);
     101                 :           0 :     }
     102   [ #  #  #  # ]:           0 :     entry.pushKV("parent_descs", std::move(parent_descs));
     103                 :           0 : }
     104                 :             : 
     105                 :           0 : void HandleWalletError(const std::shared_ptr<CWallet> wallet, DatabaseStatus& status, bilingual_str& error)
     106                 :             : {
     107         [ #  # ]:           0 :     if (!wallet) {
     108                 :             :         // Map bad format to not found, since bad format is returned when the
     109                 :             :         // wallet directory exists, but doesn't contain a data file.
     110                 :           0 :         RPCErrorCode code = RPC_WALLET_ERROR;
     111   [ #  #  #  #  :           0 :         switch (status) {
                      # ]
     112                 :           0 :             case DatabaseStatus::FAILED_NOT_FOUND:
     113                 :           0 :             case DatabaseStatus::FAILED_BAD_FORMAT:
     114                 :           0 :             case DatabaseStatus::FAILED_LEGACY_DISABLED:
     115                 :           0 :                 code = RPC_WALLET_NOT_FOUND;
     116                 :           0 :                 break;
     117                 :           0 :             case DatabaseStatus::FAILED_ALREADY_LOADED:
     118                 :           0 :                 code = RPC_WALLET_ALREADY_LOADED;
     119                 :           0 :                 break;
     120                 :           0 :             case DatabaseStatus::FAILED_ALREADY_EXISTS:
     121                 :           0 :                 code = RPC_WALLET_ALREADY_EXISTS;
     122                 :           0 :                 break;
     123                 :           0 :             case DatabaseStatus::FAILED_INVALID_BACKUP_FILE:
     124                 :           0 :                 code = RPC_INVALID_PARAMETER;
     125                 :           0 :                 break;
     126                 :             :             default: // RPC_WALLET_ERROR is returned for all other cases.
     127                 :             :                 break;
     128                 :             :         }
     129         [ #  # ]:           0 :         throw JSONRPCError(code, error.original);
     130                 :             :     }
     131                 :           0 : }
     132                 :             : 
     133                 :           0 : void AppendLastProcessedBlock(UniValue& entry, const CWallet& wallet)
     134                 :             : {
     135                 :           0 :     AssertLockHeld(wallet.cs_wallet);
     136                 :           0 :     UniValue lastprocessedblock{UniValue::VOBJ};
     137   [ #  #  #  #  :           0 :     lastprocessedblock.pushKV("hash", wallet.GetLastBlockHash().GetHex());
             #  #  #  # ]
     138   [ #  #  #  #  :           0 :     lastprocessedblock.pushKV("height", wallet.GetLastBlockHeight());
                   #  # ]
     139   [ #  #  #  # ]:           0 :     entry.pushKV("lastprocessedblock", std::move(lastprocessedblock));
     140                 :           0 : }
     141                 :             : 
     142                 :             : } // namespace wallet
        

Generated by: LCOV version 2.0-1