LCOV - code coverage report
Current view: top level - src/wallet/rpc - coins.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 0.0 % 409 0
Test Date: 2025-07-13 04:09:07 Functions: 0.0 % 15 0
Branches: 0.0 % 1678 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2011-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 <core_io.h>
       6                 :             : #include <hash.h>
       7                 :             : #include <key_io.h>
       8                 :             : #include <rpc/util.h>
       9                 :             : #include <script/script.h>
      10                 :             : #include <util/moneystr.h>
      11                 :             : #include <wallet/coincontrol.h>
      12                 :             : #include <wallet/receive.h>
      13                 :             : #include <wallet/rpc/util.h>
      14                 :             : #include <wallet/spend.h>
      15                 :             : #include <wallet/wallet.h>
      16                 :             : 
      17                 :             : #include <univalue.h>
      18                 :             : 
      19                 :             : 
      20                 :             : namespace wallet {
      21                 :           0 : static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
      22                 :             : {
      23                 :           0 :     std::vector<CTxDestination> addresses;
      24         [ #  # ]:           0 :     if (by_label) {
      25                 :             :         // Get the set of addresses assigned to label
      26   [ #  #  #  #  :           0 :         addresses = wallet.ListAddrBookAddresses(CWallet::AddrBookFilter{LabelFromValue(params[0])});
                   #  # ]
      27   [ #  #  #  #  :           0 :         if (addresses.empty()) throw JSONRPCError(RPC_WALLET_ERROR, "Label not found in wallet");
                   #  # ]
      28                 :             :     } else {
      29                 :             :         // Get the address
      30   [ #  #  #  #  :           0 :         CTxDestination dest = DecodeDestination(params[0].get_str());
                   #  # ]
      31   [ #  #  #  # ]:           0 :         if (!IsValidDestination(dest)) {
      32   [ #  #  #  # ]:           0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
      33                 :             :         }
      34         [ #  # ]:           0 :         addresses.emplace_back(dest);
      35                 :           0 :     }
      36                 :             : 
      37                 :             :     // Filter by own scripts only
      38                 :           0 :     std::set<CScript> output_scripts;
      39         [ #  # ]:           0 :     for (const auto& address : addresses) {
      40         [ #  # ]:           0 :         auto output_script{GetScriptForDestination(address)};
      41   [ #  #  #  # ]:           0 :         if (wallet.IsMine(output_script)) {
      42         [ #  # ]:           0 :             output_scripts.insert(output_script);
      43                 :             :         }
      44                 :           0 :     }
      45                 :             : 
      46         [ #  # ]:           0 :     if (output_scripts.empty()) {
      47   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Address not found in wallet");
      48                 :             :     }
      49                 :             : 
      50                 :             :     // Minimum confirmations
      51                 :           0 :     int min_depth = 1;
      52   [ #  #  #  # ]:           0 :     if (!params[1].isNull())
      53   [ #  #  #  # ]:           0 :         min_depth = params[1].getInt<int>();
      54                 :             : 
      55   [ #  #  #  #  :           0 :     const bool include_immature_coinbase{params[2].isNull() ? false : params[2].get_bool()};
             #  #  #  # ]
      56                 :             : 
      57                 :             :     // Tally
      58                 :           0 :     CAmount amount = 0;
      59   [ #  #  #  # ]:           0 :     for (const auto& [_, wtx] : wallet.mapWallet) {
      60         [ #  # ]:           0 :         int depth{wallet.GetTxDepthInMainChain(wtx)};
      61                 :           0 :         if (depth < min_depth
      62                 :             :             // Coinbase with less than 1 confirmation is no longer in the main chain
      63   [ #  #  #  # ]:           0 :             || (wtx.IsCoinBase() && (depth < 1))
      64   [ #  #  #  #  :           0 :             || (wallet.IsTxImmatureCoinBase(wtx) && !include_immature_coinbase))
             #  #  #  # ]
      65                 :             :         {
      66                 :           0 :             continue;
      67                 :             :         }
      68                 :             : 
      69         [ #  # ]:           0 :         for (const CTxOut& txout : wtx.tx->vout) {
      70         [ #  # ]:           0 :             if (output_scripts.count(txout.scriptPubKey) > 0) {
      71                 :           0 :                 amount += txout.nValue;
      72                 :             :             }
      73                 :             :         }
      74                 :             :     }
      75                 :             : 
      76                 :           0 :     return amount;
      77                 :           0 : }
      78                 :             : 
      79                 :             : 
      80                 :           0 : RPCHelpMan getreceivedbyaddress()
      81                 :             : {
      82                 :           0 :     return RPCHelpMan{
      83                 :             :         "getreceivedbyaddress",
      84                 :             :         "Returns the total amount received by the given address in transactions with at least minconf confirmations.\n",
      85                 :             :                 {
      86         [ #  # ]:           0 :                     {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address for transactions."},
      87         [ #  # ]:           0 :                     {"minconf", RPCArg::Type::NUM, RPCArg::Default{1}, "Only include transactions confirmed at least this many times."},
      88         [ #  # ]:           0 :                     {"include_immature_coinbase", RPCArg::Type::BOOL, RPCArg::Default{false}, "Include immature coinbase transactions."},
      89                 :             :                 },
      90                 :           0 :                 RPCResult{
      91         [ #  # ]:           0 :                     RPCResult::Type::STR_AMOUNT, "amount", "The total amount in " + CURRENCY_UNIT + " received at this address."
      92   [ #  #  #  # ]:           0 :                 },
      93                 :           0 :                 RPCExamples{
      94                 :             :             "\nThe amount from transactions with at least 1 confirmation\n"
      95   [ #  #  #  #  :           0 :             + HelpExampleCli("getreceivedbyaddress", "\"" + EXAMPLE_ADDRESS[0] + "\"") +
             #  #  #  # ]
      96                 :           0 :             "\nThe amount including unconfirmed transactions, zero confirmations\n"
      97   [ #  #  #  #  :           0 :             + HelpExampleCli("getreceivedbyaddress", "\"" + EXAMPLE_ADDRESS[0] + "\" 0") +
             #  #  #  # ]
      98                 :           0 :             "\nThe amount with at least 6 confirmations\n"
      99   [ #  #  #  #  :           0 :             + HelpExampleCli("getreceivedbyaddress", "\"" + EXAMPLE_ADDRESS[0] + "\" 6") +
             #  #  #  # ]
     100                 :           0 :             "\nThe amount with at least 6 confirmations including immature coinbase outputs\n"
     101   [ #  #  #  #  :           0 :             + HelpExampleCli("getreceivedbyaddress", "\"" + EXAMPLE_ADDRESS[0] + "\" 6 true") +
             #  #  #  # ]
     102                 :           0 :             "\nAs a JSON-RPC call\n"
     103   [ #  #  #  #  :           0 :             + HelpExampleRpc("getreceivedbyaddress", "\"" + EXAMPLE_ADDRESS[0] + "\", 6")
             #  #  #  # ]
     104         [ #  # ]:           0 :                 },
     105                 :           0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     106                 :             : {
     107         [ #  # ]:           0 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
     108         [ #  # ]:           0 :     if (!pwallet) return UniValue::VNULL;
     109                 :             : 
     110                 :             :     // Make sure the results are valid at least up to the most recent block
     111                 :             :     // the user could have gotten from another RPC command prior to now
     112         [ #  # ]:           0 :     pwallet->BlockUntilSyncedToCurrentChain();
     113                 :             : 
     114         [ #  # ]:           0 :     LOCK(pwallet->cs_wallet);
     115                 :             : 
     116   [ #  #  #  # ]:           0 :     return ValueFromAmount(GetReceived(*pwallet, request.params, /*by_label=*/false));
     117                 :           0 : },
     118   [ #  #  #  #  :           0 :     };
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     119   [ #  #  #  #  :           0 : }
          #  #  #  #  #  
                      # ]
     120                 :             : 
     121                 :             : 
     122                 :           0 : RPCHelpMan getreceivedbylabel()
     123                 :             : {
     124                 :           0 :     return RPCHelpMan{
     125                 :             :         "getreceivedbylabel",
     126                 :             :         "Returns the total amount received by addresses with <label> in transactions with at least [minconf] confirmations.\n",
     127                 :             :                 {
     128         [ #  # ]:           0 :                     {"label", RPCArg::Type::STR, RPCArg::Optional::NO, "The selected label, may be the default label using \"\"."},
     129         [ #  # ]:           0 :                     {"minconf", RPCArg::Type::NUM, RPCArg::Default{1}, "Only include transactions confirmed at least this many times."},
     130         [ #  # ]:           0 :                     {"include_immature_coinbase", RPCArg::Type::BOOL, RPCArg::Default{false}, "Include immature coinbase transactions."},
     131                 :             :                 },
     132                 :           0 :                 RPCResult{
     133         [ #  # ]:           0 :                     RPCResult::Type::STR_AMOUNT, "amount", "The total amount in " + CURRENCY_UNIT + " received for this label."
     134   [ #  #  #  # ]:           0 :                 },
     135                 :           0 :                 RPCExamples{
     136                 :             :             "\nAmount received by the default label with at least 1 confirmation\n"
     137   [ #  #  #  #  :           0 :             + HelpExampleCli("getreceivedbylabel", "\"\"") +
             #  #  #  # ]
     138                 :           0 :             "\nAmount received at the tabby label including unconfirmed amounts with zero confirmations\n"
     139   [ #  #  #  #  :           0 :             + HelpExampleCli("getreceivedbylabel", "\"tabby\" 0") +
             #  #  #  # ]
     140                 :           0 :             "\nThe amount with at least 6 confirmations\n"
     141   [ #  #  #  #  :           0 :             + HelpExampleCli("getreceivedbylabel", "\"tabby\" 6") +
             #  #  #  # ]
     142                 :           0 :             "\nThe amount with at least 6 confirmations including immature coinbase outputs\n"
     143   [ #  #  #  #  :           0 :             + HelpExampleCli("getreceivedbylabel", "\"tabby\" 6 true") +
             #  #  #  # ]
     144                 :           0 :             "\nAs a JSON-RPC call\n"
     145   [ #  #  #  #  :           0 :             + HelpExampleRpc("getreceivedbylabel", "\"tabby\", 6, true")
             #  #  #  # ]
     146         [ #  # ]:           0 :                 },
     147                 :           0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     148                 :             : {
     149         [ #  # ]:           0 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
     150         [ #  # ]:           0 :     if (!pwallet) return UniValue::VNULL;
     151                 :             : 
     152                 :             :     // Make sure the results are valid at least up to the most recent block
     153                 :             :     // the user could have gotten from another RPC command prior to now
     154         [ #  # ]:           0 :     pwallet->BlockUntilSyncedToCurrentChain();
     155                 :             : 
     156         [ #  # ]:           0 :     LOCK(pwallet->cs_wallet);
     157                 :             : 
     158   [ #  #  #  # ]:           0 :     return ValueFromAmount(GetReceived(*pwallet, request.params, /*by_label=*/true));
     159                 :           0 : },
     160   [ #  #  #  #  :           0 :     };
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     161   [ #  #  #  #  :           0 : }
          #  #  #  #  #  
                #  #  # ]
     162                 :             : 
     163                 :             : 
     164                 :           0 : RPCHelpMan getbalance()
     165                 :             : {
     166                 :           0 :     return RPCHelpMan{
     167                 :             :         "getbalance",
     168                 :             :         "Returns the total available balance.\n"
     169                 :             :                 "The available balance is what the wallet considers currently spendable, and is\n"
     170                 :             :                 "thus affected by options which limit spendability such as -spendzeroconfchange.\n",
     171                 :             :                 {
     172         [ #  # ]:           0 :                     {"dummy", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Remains for backward compatibility. Must be excluded or set to \"*\"."},
     173         [ #  # ]:           0 :                     {"minconf", RPCArg::Type::NUM, RPCArg::Default{0}, "Only include transactions confirmed at least this many times."},
     174         [ #  # ]:           0 :                     {"include_watchonly", RPCArg::Type::BOOL, RPCArg::Default{false}, "No longer used"},
     175         [ #  # ]:           0 :                     {"avoid_reuse", RPCArg::Type::BOOL, RPCArg::Default{true}, "(only available if avoid_reuse wallet flag is set) Do not include balance in dirty outputs; addresses are considered dirty if they have previously been used in a transaction."},
     176                 :             :                 },
     177                 :           0 :                 RPCResult{
     178         [ #  # ]:           0 :                     RPCResult::Type::STR_AMOUNT, "amount", "The total amount in " + CURRENCY_UNIT + " received for this wallet."
     179   [ #  #  #  # ]:           0 :                 },
     180                 :           0 :                 RPCExamples{
     181                 :             :             "\nThe total amount in the wallet with 0 or more confirmations\n"
     182   [ #  #  #  #  :           0 :             + HelpExampleCli("getbalance", "") +
             #  #  #  # ]
     183                 :           0 :             "\nThe total amount in the wallet with at least 6 confirmations\n"
     184   [ #  #  #  #  :           0 :             + HelpExampleCli("getbalance", "\"*\" 6") +
             #  #  #  # ]
     185                 :           0 :             "\nAs a JSON-RPC call\n"
     186   [ #  #  #  #  :           0 :             + HelpExampleRpc("getbalance", "\"*\", 6")
             #  #  #  # ]
     187         [ #  # ]:           0 :                 },
     188                 :           0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     189                 :             : {
     190         [ #  # ]:           0 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
     191         [ #  # ]:           0 :     if (!pwallet) return UniValue::VNULL;
     192                 :             : 
     193                 :             :     // Make sure the results are valid at least up to the most recent block
     194                 :             :     // the user could have gotten from another RPC command prior to now
     195         [ #  # ]:           0 :     pwallet->BlockUntilSyncedToCurrentChain();
     196                 :             : 
     197         [ #  # ]:           0 :     LOCK(pwallet->cs_wallet);
     198                 :             : 
     199         [ #  # ]:           0 :     const auto dummy_value{self.MaybeArg<std::string>("dummy")};
     200   [ #  #  #  # ]:           0 :     if (dummy_value && *dummy_value != "*") {
     201   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_METHOD_DEPRECATED, "dummy first argument must be excluded or set to \"*\".");
     202                 :             :     }
     203                 :             : 
     204         [ #  # ]:           0 :     const auto min_depth{self.Arg<int>("minconf")};
     205                 :             : 
     206   [ #  #  #  # ]:           0 :     bool avoid_reuse = GetAvoidReuseFlag(*pwallet, request.params[3]);
     207                 :             : 
     208         [ #  # ]:           0 :     const auto bal = GetBalance(*pwallet, min_depth, avoid_reuse);
     209                 :             : 
     210         [ #  # ]:           0 :     return ValueFromAmount(bal.m_mine_trusted);
     211                 :           0 : },
     212   [ #  #  #  #  :           0 :     };
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     213   [ #  #  #  #  :           0 : }
          #  #  #  #  #  
                #  #  # ]
     214                 :             : 
     215                 :           0 : RPCHelpMan lockunspent()
     216                 :             : {
     217                 :           0 :     return RPCHelpMan{
     218                 :             :         "lockunspent",
     219                 :             :         "Updates list of temporarily unspendable outputs.\n"
     220                 :             :                 "Temporarily lock (unlock=false) or unlock (unlock=true) specified transaction outputs.\n"
     221                 :             :                 "If no transaction outputs are specified when unlocking then all current locked transaction outputs are unlocked.\n"
     222                 :             :                 "A locked transaction output will not be chosen by automatic coin selection, when spending bitcoins.\n"
     223                 :             :                 "Manually selected coins are automatically unlocked.\n"
     224                 :             :                 "Locks are stored in memory only, unless persistent=true, in which case they will be written to the\n"
     225                 :             :                 "wallet database and loaded on node start. Unwritten (persistent=false) locks are always cleared\n"
     226                 :             :                 "(by virtue of process exit) when a node stops or fails. Unlocking will clear both persistent and not.\n"
     227                 :             :                 "Also see the listunspent call\n",
     228                 :             :                 {
     229         [ #  # ]:           0 :                     {"unlock", RPCArg::Type::BOOL, RPCArg::Optional::NO, "Whether to unlock (true) or lock (false) the specified transactions"},
     230                 :           0 :                     {"transactions", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "The transaction outputs and within each, the txid (string) vout (numeric).",
     231                 :             :                         {
     232         [ #  # ]:           0 :                             {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
     233                 :             :                                 {
     234         [ #  # ]:           0 :                                     {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id"},
     235         [ #  # ]:           0 :                                     {"vout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The output number"},
     236                 :             :                                 },
     237                 :             :                             },
     238                 :             :                         },
     239                 :             :                     },
     240         [ #  # ]:           0 :                     {"persistent", RPCArg::Type::BOOL, RPCArg::Default{false}, "Whether to write/erase this lock in the wallet database, or keep the change in memory only. Ignored for unlocking."},
     241                 :             :                 },
     242                 :           0 :                 RPCResult{
     243                 :             :                     RPCResult::Type::BOOL, "", "Whether the command was successful or not"
     244   [ #  #  #  #  :           0 :                 },
                   #  # ]
     245                 :           0 :                 RPCExamples{
     246                 :             :             "\nList the unspent transactions\n"
     247   [ #  #  #  #  :           0 :             + HelpExampleCli("listunspent", "") +
             #  #  #  # ]
     248                 :           0 :             "\nLock an unspent transaction\n"
     249   [ #  #  #  #  :           0 :             + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
             #  #  #  # ]
     250                 :           0 :             "\nList the locked transactions\n"
     251   [ #  #  #  #  :           0 :             + HelpExampleCli("listlockunspent", "") +
             #  #  #  # ]
     252                 :           0 :             "\nUnlock the transaction again\n"
     253   [ #  #  #  #  :           0 :             + HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
             #  #  #  # ]
     254                 :           0 :             "\nLock the transaction persistently in the wallet database\n"
     255   [ #  #  #  #  :           0 :             + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\" true") +
             #  #  #  # ]
     256                 :           0 :             "\nAs a JSON-RPC call\n"
     257   [ #  #  #  #  :           0 :             + HelpExampleRpc("lockunspent", "false, \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"")
             #  #  #  # ]
     258         [ #  # ]:           0 :                 },
     259                 :           0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     260                 :             : {
     261                 :           0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
     262         [ #  # ]:           0 :     if (!pwallet) return UniValue::VNULL;
     263                 :             : 
     264                 :             :     // Make sure the results are valid at least up to the most recent block
     265                 :             :     // the user could have gotten from another RPC command prior to now
     266         [ #  # ]:           0 :     pwallet->BlockUntilSyncedToCurrentChain();
     267                 :             : 
     268         [ #  # ]:           0 :     LOCK(pwallet->cs_wallet);
     269                 :             : 
     270   [ #  #  #  # ]:           0 :     bool fUnlock = request.params[0].get_bool();
     271                 :             : 
     272   [ #  #  #  #  :           0 :     const bool persistent{request.params[2].isNull() ? false : request.params[2].get_bool()};
             #  #  #  # ]
     273                 :             : 
     274   [ #  #  #  # ]:           0 :     if (request.params[1].isNull()) {
     275         [ #  # ]:           0 :         if (fUnlock) {
     276   [ #  #  #  # ]:           0 :             if (!pwallet->UnlockAllCoins())
     277   [ #  #  #  # ]:           0 :                 throw JSONRPCError(RPC_WALLET_ERROR, "Unlocking coins failed");
     278                 :             :         }
     279         [ #  # ]:           0 :         return true;
     280                 :             :     }
     281                 :             : 
     282   [ #  #  #  # ]:           0 :     const UniValue& output_params = request.params[1].get_array();
     283                 :             : 
     284                 :             :     // Create and validate the COutPoints first.
     285                 :             : 
     286                 :           0 :     std::vector<COutPoint> outputs;
     287         [ #  # ]:           0 :     outputs.reserve(output_params.size());
     288                 :             : 
     289         [ #  # ]:           0 :     for (unsigned int idx = 0; idx < output_params.size(); idx++) {
     290   [ #  #  #  # ]:           0 :         const UniValue& o = output_params[idx].get_obj();
     291                 :             : 
     292   [ #  #  #  #  :           0 :         RPCTypeCheckObj(o,
                   #  # ]
     293                 :             :             {
     294         [ #  # ]:           0 :                 {"txid", UniValueType(UniValue::VSTR)},
     295         [ #  # ]:           0 :                 {"vout", UniValueType(UniValue::VNUM)},
     296                 :             :             });
     297                 :             : 
     298         [ #  # ]:           0 :         const Txid txid = Txid::FromUint256(ParseHashO(o, "txid"));
     299   [ #  #  #  # ]:           0 :         const int nOutput = o.find_value("vout").getInt<int>();
     300         [ #  # ]:           0 :         if (nOutput < 0) {
     301   [ #  #  #  # ]:           0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative");
     302                 :             :         }
     303                 :             : 
     304         [ #  # ]:           0 :         const COutPoint outpt(txid, nOutput);
     305                 :             : 
     306         [ #  # ]:           0 :         const auto it = pwallet->mapWallet.find(outpt.hash);
     307         [ #  # ]:           0 :         if (it == pwallet->mapWallet.end()) {
     308   [ #  #  #  # ]:           0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, unknown transaction");
     309                 :             :         }
     310                 :             : 
     311         [ #  # ]:           0 :         const CWalletTx& trans = it->second;
     312                 :             : 
     313         [ #  # ]:           0 :         if (outpt.n >= trans.tx->vout.size()) {
     314   [ #  #  #  # ]:           0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout index out of bounds");
     315                 :             :         }
     316                 :             : 
     317   [ #  #  #  # ]:           0 :         if (pwallet->IsSpent(outpt)) {
     318   [ #  #  #  # ]:           0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected unspent output");
     319                 :             :         }
     320                 :             : 
     321         [ #  # ]:           0 :         const bool is_locked = pwallet->IsLockedCoin(outpt);
     322                 :             : 
     323         [ #  # ]:           0 :         if (fUnlock && !is_locked) {
     324   [ #  #  #  # ]:           0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected locked output");
     325                 :             :         }
     326                 :             : 
     327   [ #  #  #  # ]:           0 :         if (!fUnlock && is_locked && !persistent) {
     328   [ #  #  #  # ]:           0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, output already locked");
     329                 :             :         }
     330                 :             : 
     331         [ #  # ]:           0 :         outputs.push_back(outpt);
     332                 :             :     }
     333                 :             : 
     334                 :           0 :     std::unique_ptr<WalletBatch> batch = nullptr;
     335                 :             :     // Unlock is always persistent
     336   [ #  #  #  # ]:           0 :     if (fUnlock || persistent) batch = std::make_unique<WalletBatch>(pwallet->GetDatabase());
     337                 :             : 
     338                 :             :     // Atomically set (un)locked status for the outputs.
     339         [ #  # ]:           0 :     for (const COutPoint& outpt : outputs) {
     340         [ #  # ]:           0 :         if (fUnlock) {
     341   [ #  #  #  #  :           0 :             if (!pwallet->UnlockCoin(outpt, batch.get())) throw JSONRPCError(RPC_WALLET_ERROR, "Unlocking coin failed");
             #  #  #  # ]
     342                 :             :         } else {
     343   [ #  #  #  #  :           0 :             if (!pwallet->LockCoin(outpt, batch.get())) throw JSONRPCError(RPC_WALLET_ERROR, "Locking coin failed");
             #  #  #  # ]
     344                 :             :         }
     345                 :             :     }
     346                 :             : 
     347         [ #  # ]:           0 :     return true;
     348   [ #  #  #  #  :           0 : },
             #  #  #  # ]
     349   [ #  #  #  #  :           0 :     };
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     350   [ #  #  #  #  :           0 : }
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     351                 :             : 
     352                 :           0 : RPCHelpMan listlockunspent()
     353                 :             : {
     354                 :           0 :     return RPCHelpMan{
     355                 :             :         "listlockunspent",
     356                 :             :         "Returns list of temporarily unspendable outputs.\n"
     357                 :             :                 "See the lockunspent call to lock and unlock transactions for spending.\n",
     358                 :             :                 {},
     359                 :           0 :                 RPCResult{
     360                 :             :                     RPCResult::Type::ARR, "", "",
     361                 :             :                     {
     362                 :             :                         {RPCResult::Type::OBJ, "", "",
     363                 :             :                         {
     364                 :             :                             {RPCResult::Type::STR_HEX, "txid", "The transaction id locked"},
     365                 :             :                             {RPCResult::Type::NUM, "vout", "The vout value"},
     366                 :             :                         }},
     367                 :             :                     }
     368   [ #  #  #  #  :           0 :                 },
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     369                 :           0 :                 RPCExamples{
     370                 :             :             "\nList the unspent transactions\n"
     371   [ #  #  #  #  :           0 :             + HelpExampleCli("listunspent", "") +
             #  #  #  # ]
     372                 :           0 :             "\nLock an unspent transaction\n"
     373   [ #  #  #  #  :           0 :             + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
             #  #  #  # ]
     374                 :           0 :             "\nList the locked transactions\n"
     375   [ #  #  #  #  :           0 :             + HelpExampleCli("listlockunspent", "") +
             #  #  #  # ]
     376                 :           0 :             "\nUnlock the transaction again\n"
     377   [ #  #  #  #  :           0 :             + HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
             #  #  #  # ]
     378                 :           0 :             "\nAs a JSON-RPC call\n"
     379   [ #  #  #  #  :           0 :             + HelpExampleRpc("listlockunspent", "")
             #  #  #  # ]
     380         [ #  # ]:           0 :                 },
     381                 :           0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     382                 :             : {
     383         [ #  # ]:           0 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
     384         [ #  # ]:           0 :     if (!pwallet) return UniValue::VNULL;
     385                 :             : 
     386         [ #  # ]:           0 :     LOCK(pwallet->cs_wallet);
     387                 :             : 
     388                 :           0 :     std::vector<COutPoint> vOutpts;
     389         [ #  # ]:           0 :     pwallet->ListLockedCoins(vOutpts);
     390                 :             : 
     391                 :           0 :     UniValue ret(UniValue::VARR);
     392                 :             : 
     393         [ #  # ]:           0 :     for (const COutPoint& outpt : vOutpts) {
     394                 :           0 :         UniValue o(UniValue::VOBJ);
     395                 :             : 
     396   [ #  #  #  #  :           0 :         o.pushKV("txid", outpt.hash.GetHex());
             #  #  #  # ]
     397   [ #  #  #  #  :           0 :         o.pushKV("vout", (int)outpt.n);
                   #  # ]
     398         [ #  # ]:           0 :         ret.push_back(std::move(o));
     399                 :           0 :     }
     400                 :             : 
     401                 :           0 :     return ret;
     402         [ #  # ]:           0 : },
     403   [ #  #  #  #  :           0 :     };
             #  #  #  # ]
     404   [ #  #  #  #  :           0 : }
          #  #  #  #  #  
                      # ]
     405                 :             : 
     406                 :           0 : RPCHelpMan getbalances()
     407                 :             : {
     408                 :           0 :     return RPCHelpMan{
     409                 :             :         "getbalances",
     410         [ #  # ]:           0 :         "Returns an object with all balances in " + CURRENCY_UNIT + ".\n",
     411                 :             :         {},
     412                 :           0 :         RPCResult{
     413                 :             :             RPCResult::Type::OBJ, "", "",
     414                 :             :             {
     415                 :             :                 {RPCResult::Type::OBJ, "mine", "balances from outputs that the wallet can sign",
     416                 :             :                 {
     417                 :             :                     {RPCResult::Type::STR_AMOUNT, "trusted", "trusted balance (outputs created by the wallet or confirmed outputs)"},
     418                 :             :                     {RPCResult::Type::STR_AMOUNT, "untrusted_pending", "untrusted pending balance (outputs created by others that are in the mempool)"},
     419                 :             :                     {RPCResult::Type::STR_AMOUNT, "immature", "balance from immature coinbase outputs"},
     420                 :             :                     {RPCResult::Type::STR_AMOUNT, "used", /*optional=*/true, "(only present if avoid_reuse is set) balance from coins sent to addresses that were previously spent from (potentially privacy violating)"},
     421                 :             :                 }},
     422                 :             :                 RESULT_LAST_PROCESSED_BLOCK,
     423                 :             :             }
     424   [ #  #  #  #  :           0 :             },
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     425                 :           0 :         RPCExamples{
     426   [ #  #  #  #  :           0 :             HelpExampleCli("getbalances", "") +
                   #  # ]
     427   [ #  #  #  #  :           0 :             HelpExampleRpc("getbalances", "")},
          #  #  #  #  #  
                      # ]
     428                 :           0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     429                 :             : {
     430         [ #  # ]:           0 :     const std::shared_ptr<const CWallet> rpc_wallet = GetWalletForJSONRPCRequest(request);
     431         [ #  # ]:           0 :     if (!rpc_wallet) return UniValue::VNULL;
     432         [ #  # ]:           0 :     const CWallet& wallet = *rpc_wallet;
     433                 :             : 
     434                 :             :     // Make sure the results are valid at least up to the most recent block
     435                 :             :     // the user could have gotten from another RPC command prior to now
     436         [ #  # ]:           0 :     wallet.BlockUntilSyncedToCurrentChain();
     437                 :             : 
     438         [ #  # ]:           0 :     LOCK(wallet.cs_wallet);
     439                 :             : 
     440         [ #  # ]:           0 :     const auto bal = GetBalance(wallet);
     441                 :           0 :     UniValue balances{UniValue::VOBJ};
     442                 :           0 :     {
     443                 :           0 :         UniValue balances_mine{UniValue::VOBJ};
     444   [ #  #  #  #  :           0 :         balances_mine.pushKV("trusted", ValueFromAmount(bal.m_mine_trusted));
                   #  # ]
     445   [ #  #  #  #  :           0 :         balances_mine.pushKV("untrusted_pending", ValueFromAmount(bal.m_mine_untrusted_pending));
                   #  # ]
     446   [ #  #  #  #  :           0 :         balances_mine.pushKV("immature", ValueFromAmount(bal.m_mine_immature));
                   #  # ]
     447   [ #  #  #  # ]:           0 :         if (wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE)) {
     448                 :             :             // If the AVOID_REUSE flag is set, bal has been set to just the un-reused address balance. Get
     449                 :             :             // the total balance, and then subtract bal to get the reused address balance.
     450         [ #  # ]:           0 :             const auto full_bal = GetBalance(wallet, 0, false);
     451   [ #  #  #  #  :           0 :             balances_mine.pushKV("used", ValueFromAmount(full_bal.m_mine_trusted + full_bal.m_mine_untrusted_pending - bal.m_mine_trusted - bal.m_mine_untrusted_pending));
                   #  # ]
     452                 :             :         }
     453   [ #  #  #  # ]:           0 :         balances.pushKV("mine", std::move(balances_mine));
     454                 :           0 :     }
     455         [ #  # ]:           0 :     AppendLastProcessedBlock(balances, wallet);
     456                 :           0 :     return balances;
     457         [ #  # ]:           0 : },
     458   [ #  #  #  #  :           0 :     };
                   #  # ]
     459   [ #  #  #  #  :           0 : }
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     460                 :             : 
     461                 :           0 : RPCHelpMan listunspent()
     462                 :             : {
     463                 :           0 :     return RPCHelpMan{
     464                 :             :         "listunspent",
     465                 :             :         "Returns array of unspent transaction outputs\n"
     466                 :             :                 "with between minconf and maxconf (inclusive) confirmations.\n"
     467                 :             :                 "Optionally filter to only include txouts paid to specified addresses.\n",
     468                 :             :                 {
     469         [ #  # ]:           0 :                     {"minconf", RPCArg::Type::NUM, RPCArg::Default{1}, "The minimum confirmations to filter"},
     470         [ #  # ]:           0 :                     {"maxconf", RPCArg::Type::NUM, RPCArg::Default{9999999}, "The maximum confirmations to filter"},
     471                 :           0 :                     {"addresses", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "The bitcoin addresses to filter",
     472                 :             :                         {
     473         [ #  # ]:           0 :                             {"address", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "bitcoin address"},
     474                 :             :                         },
     475                 :             :                     },
     476         [ #  # ]:           0 :                     {"include_unsafe", RPCArg::Type::BOOL, RPCArg::Default{true}, "Include outputs that are not safe to spend\n"
     477                 :             :                               "See description of \"safe\" attribute below."},
     478         [ #  # ]:           0 :                     {"query_options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "",
     479                 :             :                         {
     480   [ #  #  #  # ]:           0 :                             {"minimumAmount", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(0)}, "Minimum value of each UTXO in " + CURRENCY_UNIT + ""},
     481         [ #  # ]:           0 :                             {"maximumAmount", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"unlimited"}, "Maximum value of each UTXO in " + CURRENCY_UNIT + ""},
     482         [ #  # ]:           0 :                             {"maximumCount", RPCArg::Type::NUM, RPCArg::DefaultHint{"unlimited"}, "Maximum number of UTXOs"},
     483         [ #  # ]:           0 :                             {"minimumSumAmount", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"unlimited"}, "Minimum sum value of all UTXOs in " + CURRENCY_UNIT + ""},
     484         [ #  # ]:           0 :                             {"include_immature_coinbase", RPCArg::Type::BOOL, RPCArg::Default{false}, "Include immature coinbase UTXOs"}
     485                 :             :                         },
     486   [ #  #  #  # ]:           0 :                         RPCArgOptions{.oneline_description="query_options"}},
     487                 :             :                 },
     488                 :           0 :                 RPCResult{
     489                 :             :                     RPCResult::Type::ARR, "", "",
     490                 :             :                     {
     491                 :             :                         {RPCResult::Type::OBJ, "", "",
     492                 :             :                         {
     493                 :             :                             {RPCResult::Type::STR_HEX, "txid", "the transaction id"},
     494                 :             :                             {RPCResult::Type::NUM, "vout", "the vout value"},
     495                 :             :                             {RPCResult::Type::STR, "address", /*optional=*/true, "the bitcoin address"},
     496                 :             :                             {RPCResult::Type::STR, "label", /*optional=*/true, "The associated label, or \"\" for the default label"},
     497                 :             :                             {RPCResult::Type::STR, "scriptPubKey", "the output script"},
     498         [ #  # ]:           0 :                             {RPCResult::Type::STR_AMOUNT, "amount", "the transaction output amount in " + CURRENCY_UNIT},
     499                 :             :                             {RPCResult::Type::NUM, "confirmations", "The number of confirmations"},
     500                 :             :                             {RPCResult::Type::NUM, "ancestorcount", /*optional=*/true, "The number of in-mempool ancestor transactions, including this one (if transaction is in the mempool)"},
     501                 :             :                             {RPCResult::Type::NUM, "ancestorsize", /*optional=*/true, "The virtual transaction size of in-mempool ancestors, including this one (if transaction is in the mempool)"},
     502         [ #  # ]:           0 :                             {RPCResult::Type::STR_AMOUNT, "ancestorfees", /*optional=*/true, "The total fees of in-mempool ancestors (including this one) with fee deltas used for mining priority in " + CURRENCY_ATOM + " (if transaction is in the mempool)"},
     503                 :             :                             {RPCResult::Type::STR_HEX, "redeemScript", /*optional=*/true, "The redeem script if the output script is P2SH"},
     504                 :             :                             {RPCResult::Type::STR, "witnessScript", /*optional=*/true, "witness script if the output script is P2WSH or P2SH-P2WSH"},
     505                 :             :                             {RPCResult::Type::BOOL, "spendable", "Whether we have the private keys to spend this output"},
     506                 :             :                             {RPCResult::Type::BOOL, "solvable", "Whether we know how to spend this output, ignoring the lack of keys"},
     507                 :             :                             {RPCResult::Type::BOOL, "reused", /*optional=*/true, "(only present if avoid_reuse is set) Whether this output is reused/dirty (sent to an address that was previously spent from)"},
     508                 :             :                             {RPCResult::Type::STR, "desc", /*optional=*/true, "(only when solvable) A descriptor for spending this output"},
     509                 :             :                             {RPCResult::Type::ARR, "parent_descs", /*optional=*/false, "List of parent descriptors for the output script of this coin.", {
     510                 :             :                                 {RPCResult::Type::STR, "desc", "The descriptor string."},
     511                 :             :                             }},
     512                 :             :                             {RPCResult::Type::BOOL, "safe", "Whether this output is considered safe to spend. Unconfirmed transactions\n"
     513                 :             :                                                             "from outside keys and unconfirmed replacement transactions are considered unsafe\n"
     514                 :             :                                                             "and are not eligible for spending by fundrawtransaction and sendtoaddress."},
     515                 :             :                         }},
     516                 :             :                     }
     517   [ #  #  #  #  :           0 :                 },
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     518                 :           0 :                 RPCExamples{
     519   [ #  #  #  #  :           0 :                     HelpExampleCli("listunspent", "")
                   #  # ]
     520   [ #  #  #  #  :           0 :             + HelpExampleCli("listunspent", "6 9999999 \"[\\\"" + EXAMPLE_ADDRESS[0] + "\\\",\\\"" + EXAMPLE_ADDRESS[1] + "\\\"]\"")
          #  #  #  #  #  
                      # ]
     521   [ #  #  #  #  :           0 :             + HelpExampleRpc("listunspent", "6, 9999999 \"[\\\"" + EXAMPLE_ADDRESS[0] + "\\\",\\\"" + EXAMPLE_ADDRESS[1] + "\\\"]\"")
          #  #  #  #  #  
                      # ]
     522   [ #  #  #  #  :           0 :             + HelpExampleCli("listunspent", "6 9999999 '[]' true '{ \"minimumAmount\": 0.005 }'")
             #  #  #  # ]
     523   [ #  #  #  #  :           0 :             + HelpExampleRpc("listunspent", "6, 9999999, [] , true, { \"minimumAmount\": 0.005 } ")
             #  #  #  # ]
     524         [ #  # ]:           0 :                 },
     525                 :           0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     526                 :             : {
     527         [ #  # ]:           0 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
     528         [ #  # ]:           0 :     if (!pwallet) return UniValue::VNULL;
     529                 :             : 
     530                 :           0 :     int nMinDepth = 1;
     531   [ #  #  #  # ]:           0 :     if (!request.params[0].isNull()) {
     532   [ #  #  #  # ]:           0 :         nMinDepth = request.params[0].getInt<int>();
     533                 :             :     }
     534                 :             : 
     535                 :           0 :     int nMaxDepth = 9999999;
     536   [ #  #  #  # ]:           0 :     if (!request.params[1].isNull()) {
     537   [ #  #  #  # ]:           0 :         nMaxDepth = request.params[1].getInt<int>();
     538                 :             :     }
     539                 :             : 
     540         [ #  # ]:           0 :     std::set<CTxDestination> destinations;
     541   [ #  #  #  # ]:           0 :     if (!request.params[2].isNull()) {
     542   [ #  #  #  #  :           0 :         UniValue inputs = request.params[2].get_array();
                   #  # ]
     543         [ #  # ]:           0 :         for (unsigned int idx = 0; idx < inputs.size(); idx++) {
     544         [ #  # ]:           0 :             const UniValue& input = inputs[idx];
     545   [ #  #  #  # ]:           0 :             CTxDestination dest = DecodeDestination(input.get_str());
     546   [ #  #  #  # ]:           0 :             if (!IsValidDestination(dest)) {
     547   [ #  #  #  #  :           0 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ") + input.get_str());
             #  #  #  # ]
     548                 :             :             }
     549   [ #  #  #  # ]:           0 :             if (!destinations.insert(dest).second) {
     550   [ #  #  #  #  :           0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ") + input.get_str());
             #  #  #  # ]
     551                 :             :             }
     552                 :           0 :         }
     553                 :           0 :     }
     554                 :             : 
     555                 :           0 :     bool include_unsafe = true;
     556   [ #  #  #  # ]:           0 :     if (!request.params[3].isNull()) {
     557   [ #  #  #  # ]:           0 :         include_unsafe = request.params[3].get_bool();
     558                 :             :     }
     559                 :             : 
     560                 :           0 :     CoinFilterParams filter_coins;
     561                 :           0 :     filter_coins.min_amount = 0;
     562                 :             : 
     563   [ #  #  #  # ]:           0 :     if (!request.params[4].isNull()) {
     564   [ #  #  #  # ]:           0 :         const UniValue& options = request.params[4].get_obj();
     565                 :             : 
     566   [ #  #  #  #  :           0 :         RPCTypeCheckObj(options,
                   #  # ]
     567                 :             :             {
     568         [ #  # ]:           0 :                 {"minimumAmount", UniValueType()},
     569         [ #  # ]:           0 :                 {"maximumAmount", UniValueType()},
     570         [ #  # ]:           0 :                 {"minimumSumAmount", UniValueType()},
     571         [ #  # ]:           0 :                 {"maximumCount", UniValueType(UniValue::VNUM)},
     572         [ #  # ]:           0 :                 {"include_immature_coinbase", UniValueType(UniValue::VBOOL)}
     573                 :             :             },
     574                 :             :             true, true);
     575                 :             : 
     576   [ #  #  #  # ]:           0 :         if (options.exists("minimumAmount"))
     577   [ #  #  #  #  :           0 :             filter_coins.min_amount = AmountFromValue(options["minimumAmount"]);
                   #  # ]
     578                 :             : 
     579   [ #  #  #  # ]:           0 :         if (options.exists("maximumAmount"))
     580   [ #  #  #  #  :           0 :             filter_coins.max_amount = AmountFromValue(options["maximumAmount"]);
                   #  # ]
     581                 :             : 
     582   [ #  #  #  # ]:           0 :         if (options.exists("minimumSumAmount"))
     583   [ #  #  #  #  :           0 :             filter_coins.min_sum_amount = AmountFromValue(options["minimumSumAmount"]);
                   #  # ]
     584                 :             : 
     585   [ #  #  #  # ]:           0 :         if (options.exists("maximumCount"))
     586   [ #  #  #  #  :           0 :             filter_coins.max_count = options["maximumCount"].getInt<int64_t>();
                   #  # ]
     587                 :             : 
     588   [ #  #  #  # ]:           0 :         if (options.exists("include_immature_coinbase")) {
     589   [ #  #  #  #  :           0 :             filter_coins.include_immature_coinbase = options["include_immature_coinbase"].get_bool();
                   #  # ]
     590                 :             :         }
     591                 :             :     }
     592                 :             : 
     593                 :             :     // Make sure the results are valid at least up to the most recent block
     594                 :             :     // the user could have gotten from another RPC command prior to now
     595         [ #  # ]:           0 :     pwallet->BlockUntilSyncedToCurrentChain();
     596                 :             : 
     597                 :           0 :     UniValue results(UniValue::VARR);
     598                 :           0 :     std::vector<COutput> vecOutputs;
     599                 :           0 :     {
     600         [ #  # ]:           0 :         CCoinControl cctl;
     601                 :           0 :         cctl.m_avoid_address_reuse = false;
     602                 :           0 :         cctl.m_min_depth = nMinDepth;
     603                 :           0 :         cctl.m_max_depth = nMaxDepth;
     604                 :           0 :         cctl.m_include_unsafe_inputs = include_unsafe;
     605         [ #  # ]:           0 :         LOCK(pwallet->cs_wallet);
     606   [ #  #  #  #  :           0 :         vecOutputs = AvailableCoinsListUnspent(*pwallet, &cctl, filter_coins).All();
                   #  # ]
     607                 :           0 :     }
     608                 :             : 
     609         [ #  # ]:           0 :     LOCK(pwallet->cs_wallet);
     610                 :             : 
     611         [ #  # ]:           0 :     const bool avoid_reuse = pwallet->IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE);
     612                 :             : 
     613         [ #  # ]:           0 :     for (const COutput& out : vecOutputs) {
     614                 :           0 :         CTxDestination address;
     615                 :           0 :         const CScript& scriptPubKey = out.txout.scriptPubKey;
     616         [ #  # ]:           0 :         bool fValidAddress = ExtractDestination(scriptPubKey, address);
     617   [ #  #  #  #  :           0 :         bool reused = avoid_reuse && pwallet->IsSpentKey(scriptPubKey);
                   #  # ]
     618                 :             : 
     619   [ #  #  #  #  :           0 :         if (destinations.size() && (!fValidAddress || !destinations.count(address)))
                   #  # ]
     620                 :           0 :             continue;
     621                 :             : 
     622                 :           0 :         UniValue entry(UniValue::VOBJ);
     623   [ #  #  #  #  :           0 :         entry.pushKV("txid", out.outpoint.hash.GetHex());
             #  #  #  # ]
     624   [ #  #  #  #  :           0 :         entry.pushKV("vout", (int)out.outpoint.n);
                   #  # ]
     625                 :             : 
     626         [ #  # ]:           0 :         if (fValidAddress) {
     627   [ #  #  #  #  :           0 :             entry.pushKV("address", EncodeDestination(address));
             #  #  #  # ]
     628                 :             : 
     629         [ #  # ]:           0 :             const auto* address_book_entry = pwallet->FindAddressBookEntry(address);
     630         [ #  # ]:           0 :             if (address_book_entry) {
     631   [ #  #  #  #  :           0 :                 entry.pushKV("label", address_book_entry->GetLabel());
             #  #  #  # ]
     632                 :             :             }
     633                 :             : 
     634         [ #  # ]:           0 :             std::unique_ptr<SigningProvider> provider = pwallet->GetSolvingProvider(scriptPubKey);
     635         [ #  # ]:           0 :             if (provider) {
     636   [ #  #  #  # ]:           0 :                 if (scriptPubKey.IsPayToScriptHash()) {
     637   [ #  #  #  # ]:           0 :                     const CScriptID hash = ToScriptID(std::get<ScriptHash>(address));
     638                 :           0 :                     CScript redeemScript;
     639   [ #  #  #  # ]:           0 :                     if (provider->GetCScript(hash, redeemScript)) {
     640   [ #  #  #  #  :           0 :                         entry.pushKV("redeemScript", HexStr(redeemScript));
          #  #  #  #  #  
                      # ]
     641                 :             :                         // Now check if the redeemScript is actually a P2WSH script
     642                 :           0 :                         CTxDestination witness_destination;
     643   [ #  #  #  # ]:           0 :                         if (redeemScript.IsPayToWitnessScriptHash()) {
     644         [ #  # ]:           0 :                             bool extracted = ExtractDestination(redeemScript, witness_destination);
     645         [ #  # ]:           0 :                             CHECK_NONFATAL(extracted);
     646                 :             :                             // Also return the witness script
     647         [ #  # ]:           0 :                             const WitnessV0ScriptHash& whash = std::get<WitnessV0ScriptHash>(witness_destination);
     648         [ #  # ]:           0 :                             CScriptID id{RIPEMD160(whash)};
     649                 :           0 :                             CScript witnessScript;
     650   [ #  #  #  # ]:           0 :                             if (provider->GetCScript(id, witnessScript)) {
     651   [ #  #  #  #  :           0 :                                 entry.pushKV("witnessScript", HexStr(witnessScript));
          #  #  #  #  #  
                      # ]
     652                 :             :                             }
     653                 :           0 :                         }
     654                 :           0 :                     }
     655   [ #  #  #  # ]:           0 :                 } else if (scriptPubKey.IsPayToWitnessScriptHash()) {
     656         [ #  # ]:           0 :                     const WitnessV0ScriptHash& whash = std::get<WitnessV0ScriptHash>(address);
     657         [ #  # ]:           0 :                     CScriptID id{RIPEMD160(whash)};
     658                 :           0 :                     CScript witnessScript;
     659   [ #  #  #  # ]:           0 :                     if (provider->GetCScript(id, witnessScript)) {
     660   [ #  #  #  #  :           0 :                         entry.pushKV("witnessScript", HexStr(witnessScript));
          #  #  #  #  #  
                      # ]
     661                 :             :                     }
     662                 :           0 :                 }
     663                 :             :             }
     664                 :           0 :         }
     665                 :             : 
     666   [ #  #  #  #  :           0 :         entry.pushKV("scriptPubKey", HexStr(scriptPubKey));
          #  #  #  #  #  
                      # ]
     667   [ #  #  #  #  :           0 :         entry.pushKV("amount", ValueFromAmount(out.txout.nValue));
                   #  # ]
     668   [ #  #  #  #  :           0 :         entry.pushKV("confirmations", out.depth);
                   #  # ]
     669         [ #  # ]:           0 :         if (!out.depth) {
     670                 :           0 :             size_t ancestor_count, descendant_count, ancestor_size;
     671                 :           0 :             CAmount ancestor_fees;
     672         [ #  # ]:           0 :             pwallet->chain().getTransactionAncestry(out.outpoint.hash, ancestor_count, descendant_count, &ancestor_size, &ancestor_fees);
     673         [ #  # ]:           0 :             if (ancestor_count) {
     674   [ #  #  #  #  :           0 :                 entry.pushKV("ancestorcount", uint64_t(ancestor_count));
                   #  # ]
     675   [ #  #  #  #  :           0 :                 entry.pushKV("ancestorsize", uint64_t(ancestor_size));
                   #  # ]
     676   [ #  #  #  #  :           0 :                 entry.pushKV("ancestorfees", uint64_t(ancestor_fees));
                   #  # ]
     677                 :             :             }
     678                 :             :         }
     679   [ #  #  #  #  :           0 :         entry.pushKV("spendable", out.spendable);
                   #  # ]
     680   [ #  #  #  #  :           0 :         entry.pushKV("solvable", out.solvable);
                   #  # ]
     681         [ #  # ]:           0 :         if (out.solvable) {
     682         [ #  # ]:           0 :             std::unique_ptr<SigningProvider> provider = pwallet->GetSolvingProvider(scriptPubKey);
     683         [ #  # ]:           0 :             if (provider) {
     684         [ #  # ]:           0 :                 auto descriptor = InferDescriptor(scriptPubKey, *provider);
     685   [ #  #  #  #  :           0 :                 entry.pushKV("desc", descriptor->ToString());
             #  #  #  # ]
     686                 :           0 :             }
     687                 :           0 :         }
     688         [ #  # ]:           0 :         PushParentDescriptors(*pwallet, scriptPubKey, entry);
     689   [ #  #  #  #  :           0 :         if (avoid_reuse) entry.pushKV("reused", reused);
             #  #  #  # ]
     690   [ #  #  #  #  :           0 :         entry.pushKV("safe", out.safe);
                   #  # ]
     691         [ #  # ]:           0 :         results.push_back(std::move(entry));
     692                 :           0 :     }
     693                 :             : 
     694         [ #  # ]:           0 :     return results;
     695   [ #  #  #  #  :           0 : },
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     696   [ #  #  #  #  :           0 :     };
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     697   [ #  #  #  #  :           0 : }
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     698                 :             : } // namespace wallet
        

Generated by: LCOV version 2.0-1