LCOV - code coverage report
Current view: top level - src/wallet/rpc - coins.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 574 586 98.0 %
Date: 2022-04-21 14:51:19 Functions: 17 17 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 148 174 85.1 %

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

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a