LCOV - code coverage report
Current view: top level - src/wallet/rpc - coins.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.2 % 450 442
Test Date: 2025-10-25 05:06:34 Functions: 100.0 % 15 15
Branches: 50.8 % 1590 808

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

Generated by: LCOV version 2.0-1