LCOV - code coverage report
Current view: top level - src/wallet - rpcwallet.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 3597 3761 95.6 %
Date: 2021-06-29 14:35:33 Functions: 127 130 97.7 %
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: 878 1062 82.7 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2010 Satoshi Nakamoto
#       2                 :            : // Copyright (c) 2009-2020 The Bitcoin Core developers
#       3                 :            : // Distributed under the MIT software license, see the accompanying
#       4                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#       5                 :            : 
#       6                 :            : #include <amount.h>
#       7                 :            : #include <core_io.h>
#       8                 :            : #include <interfaces/chain.h>
#       9                 :            : #include <key_io.h>
#      10                 :            : #include <node/context.h>
#      11                 :            : #include <outputtype.h>
#      12                 :            : #include <policy/feerate.h>
#      13                 :            : #include <policy/fees.h>
#      14                 :            : #include <policy/policy.h>
#      15                 :            : #include <policy/rbf.h>
#      16                 :            : #include <rpc/rawtransaction_util.h>
#      17                 :            : #include <rpc/server.h>
#      18                 :            : #include <rpc/util.h>
#      19                 :            : #include <script/descriptor.h>
#      20                 :            : #include <script/sign.h>
#      21                 :            : #include <util/bip32.h>
#      22                 :            : #include <util/fees.h>
#      23                 :            : #include <util/message.h> // For MessageSign()
#      24                 :            : #include <util/moneystr.h>
#      25                 :            : #include <util/string.h>
#      26                 :            : #include <util/system.h>
#      27                 :            : #include <util/translation.h>
#      28                 :            : #include <util/url.h>
#      29                 :            : #include <util/vector.h>
#      30                 :            : #include <wallet/coincontrol.h>
#      31                 :            : #include <wallet/context.h>
#      32                 :            : #include <wallet/feebumper.h>
#      33                 :            : #include <wallet/load.h>
#      34                 :            : #include <wallet/rpcwallet.h>
#      35                 :            : #include <wallet/wallet.h>
#      36                 :            : #include <wallet/walletdb.h>
#      37                 :            : #include <wallet/walletutil.h>
#      38                 :            : 
#      39                 :            : #include <optional>
#      40                 :            : #include <stdint.h>
#      41                 :            : 
#      42                 :            : #include <univalue.h>
#      43                 :            : 
#      44                 :            : 
#      45                 :            : using interfaces::FoundBlock;
#      46                 :            : 
#      47                 :            : static const std::string WALLET_ENDPOINT_BASE = "/wallet/";
#      48                 :            : static const std::string HELP_REQUIRING_PASSPHRASE{"\nRequires wallet passphrase to be set with walletpassphrase call if wallet is encrypted.\n"};
#      49                 :            : 
#      50                 :       2327 : static inline bool GetAvoidReuseFlag(const CWallet& wallet, const UniValue& param) {
#      51                 :       2327 :     bool can_avoid_reuse = wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE);
#      52         [ +  + ]:       2327 :     bool avoid_reuse = param.isNull() ? can_avoid_reuse : param.get_bool();
#      53                 :            : 
#      54 [ +  + ][ -  + ]:       2327 :     if (avoid_reuse && !can_avoid_reuse) {
#      55                 :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "wallet does not have the \"avoid reuse\" feature enabled");
#      56                 :          0 :     }
#      57                 :            : 
#      58                 :       2327 :     return avoid_reuse;
#      59                 :       2327 : }
#      60                 :            : 
#      61                 :            : 
#      62                 :            : /** Used by RPC commands that have an include_watchonly parameter.
#      63                 :            :  *  We default to true for watchonly wallets if include_watchonly isn't
#      64                 :            :  *  explicitly set.
#      65                 :            :  */
#      66                 :            : static bool ParseIncludeWatchonly(const UniValue& include_watchonly, const CWallet& wallet)
#      67                 :       2157 : {
#      68         [ +  + ]:       2157 :     if (include_watchonly.isNull()) {
#      69                 :            :         // if include_watchonly isn't explicitly set, then check if we have a watchonly wallet
#      70                 :       1442 :         return wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
#      71                 :       1442 :     }
#      72                 :            : 
#      73                 :            :     // otherwise return whatever include_watchonly was set to
#      74                 :        715 :     return include_watchonly.get_bool();
#      75                 :        715 : }
#      76                 :            : 
#      77                 :            : 
#      78                 :            : /** Checks if a CKey is in the given CWallet compressed or otherwise*/
#      79                 :            : bool HaveKey(const SigningProvider& wallet, const CKey& key)
#      80                 :          8 : {
#      81                 :          8 :     CKey key2;
#      82                 :          8 :     key2.Set(key.begin(), key.end(), !key.IsCompressed());
#      83 [ +  + ][ -  + ]:          8 :     return wallet.HaveKey(key.GetPubKey().GetID()) || wallet.HaveKey(key2.GetPubKey().GetID());
#      84                 :          8 : }
#      85                 :            : 
#      86                 :            : bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string& wallet_name)
#      87                 :      24403 : {
#      88 [ +  + ][ +  + ]:      24403 :     if (URL_DECODE && request.URI.substr(0, WALLET_ENDPOINT_BASE.size()) == WALLET_ENDPOINT_BASE) {
#                 [ +  + ]
#      89                 :            :         // wallet endpoint was used
#      90                 :       6353 :         wallet_name = URL_DECODE(request.URI.substr(WALLET_ENDPOINT_BASE.size()));
#      91                 :       6353 :         return true;
#      92                 :       6353 :     }
#      93                 :      18050 :     return false;
#      94                 :      18050 : }
#      95                 :            : 
#      96                 :            : std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
#      97                 :      24234 : {
#      98         [ -  + ]:      24234 :     CHECK_NONFATAL(request.mode == JSONRPCRequest::EXECUTE);
#      99                 :      24234 :     std::string wallet_name;
#     100         [ +  + ]:      24234 :     if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
#     101                 :       6304 :         std::shared_ptr<CWallet> pwallet = GetWallet(wallet_name);
#     102         [ +  + ]:       6304 :         if (!pwallet) throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
#     103                 :       6293 :         return pwallet;
#     104                 :       6293 :     }
#     105                 :            : 
#     106                 :      17930 :     std::vector<std::shared_ptr<CWallet>> wallets = GetWallets();
#     107         [ +  + ]:      17930 :     if (wallets.size() == 1) {
#     108                 :      17909 :         return wallets[0];
#     109                 :      17909 :     }
#     110                 :            : 
#     111         [ +  + ]:         21 :     if (wallets.empty()) {
#     112                 :          7 :         throw JSONRPCError(
#     113                 :          7 :             RPC_WALLET_NOT_FOUND, "No wallet is loaded. Load a wallet using loadwallet or create a new one with createwallet. (Note: A default wallet is no longer automatically created)");
#     114                 :          7 :     }
#     115                 :         14 :     throw JSONRPCError(RPC_WALLET_NOT_SPECIFIED,
#     116                 :         14 :         "Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path).");
#     117                 :         14 : }
#     118                 :            : 
#     119                 :            : void EnsureWalletIsUnlocked(const CWallet& wallet)
#     120                 :       6272 : {
#     121         [ +  + ]:       6272 :     if (wallet.IsLocked()) {
#     122                 :         18 :         throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please enter the wallet passphrase with walletpassphrase first.");
#     123                 :         18 :     }
#     124                 :       6272 : }
#     125                 :            : 
#     126                 :            : WalletContext& EnsureWalletContext(const std::any& context)
#     127                 :        637 : {
#     128                 :        637 :     auto wallet_context = util::AnyPtr<WalletContext>(context);
#     129         [ -  + ]:        637 :     if (!wallet_context) {
#     130                 :          0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet context not found");
#     131                 :          0 :     }
#     132                 :        637 :     return *wallet_context;
#     133                 :        637 : }
#     134                 :            : 
#     135                 :            : // also_create should only be set to true only when the RPC is expected to add things to a blank wallet and make it no longer blank
#     136                 :            : LegacyScriptPubKeyMan& EnsureLegacyScriptPubKeyMan(CWallet& wallet, bool also_create)
#     137                 :        884 : {
#     138                 :        884 :     LegacyScriptPubKeyMan* spk_man = wallet.GetLegacyScriptPubKeyMan();
#     139 [ +  + ][ +  + ]:        884 :     if (!spk_man && also_create) {
#     140                 :          7 :         spk_man = wallet.GetOrCreateLegacyScriptPubKeyMan();
#     141                 :          7 :     }
#     142         [ +  + ]:        884 :     if (!spk_man) {
#     143                 :          9 :         throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
#     144                 :          9 :     }
#     145                 :        875 :     return *spk_man;
#     146                 :        875 : }
#     147                 :            : 
#     148                 :            : static void WalletTxToJSON(interfaces::Chain& chain, const CWalletTx& wtx, UniValue& entry)
#     149                 :       3181 : {
#     150                 :       3181 :     int confirms = wtx.GetDepthInMainChain();
#     151                 :       3181 :     entry.pushKV("confirmations", confirms);
#     152         [ +  + ]:       3181 :     if (wtx.IsCoinBase())
#     153                 :       2070 :         entry.pushKV("generated", true);
#     154         [ +  + ]:       3181 :     if (confirms > 0)
#     155                 :       2237 :     {
#     156                 :       2237 :         entry.pushKV("blockhash", wtx.m_confirm.hashBlock.GetHex());
#     157                 :       2237 :         entry.pushKV("blockheight", wtx.m_confirm.block_height);
#     158                 :       2237 :         entry.pushKV("blockindex", wtx.m_confirm.nIndex);
#     159                 :       2237 :         int64_t block_time;
#     160         [ -  + ]:       2237 :         CHECK_NONFATAL(chain.findBlock(wtx.m_confirm.hashBlock, FoundBlock().time(block_time)));
#     161                 :       2237 :         entry.pushKV("blocktime", block_time);
#     162                 :       2237 :     } else {
#     163                 :        944 :         entry.pushKV("trusted", wtx.IsTrusted());
#     164                 :        944 :     }
#     165                 :       3181 :     uint256 hash = wtx.GetHash();
#     166                 :       3181 :     entry.pushKV("txid", hash.GetHex());
#     167                 :       3181 :     UniValue conflicts(UniValue::VARR);
#     168         [ +  + ]:       3181 :     for (const uint256& conflict : wtx.GetConflicts())
#     169                 :        170 :         conflicts.push_back(conflict.GetHex());
#     170                 :       3181 :     entry.pushKV("walletconflicts", conflicts);
#     171                 :       3181 :     entry.pushKV("time", wtx.GetTxTime());
#     172                 :       3181 :     entry.pushKV("timereceived", (int64_t)wtx.nTimeReceived);
#     173                 :            : 
#     174                 :            :     // Add opt-in RBF status
#     175                 :       3181 :     std::string rbfStatus = "no";
#     176         [ +  + ]:       3181 :     if (confirms <= 0) {
#     177                 :        944 :         RBFTransactionState rbfState = chain.isRBFOptIn(*wtx.tx);
#     178         [ +  + ]:        944 :         if (rbfState == RBFTransactionState::UNKNOWN)
#     179                 :        442 :             rbfStatus = "unknown";
#     180         [ +  + ]:        502 :         else if (rbfState == RBFTransactionState::REPLACEABLE_BIP125)
#     181                 :         80 :             rbfStatus = "yes";
#     182                 :        944 :     }
#     183                 :       3181 :     entry.pushKV("bip125-replaceable", rbfStatus);
#     184                 :            : 
#     185         [ +  + ]:       3181 :     for (const std::pair<const std::string, std::string>& item : wtx.mapValue)
#     186                 :         25 :         entry.pushKV(item.first, item.second);
#     187                 :       3181 : }
#     188                 :            : 
#     189                 :            : static std::string LabelFromValue(const UniValue& value)
#     190                 :        429 : {
#     191                 :        429 :     std::string label = value.get_str();
#     192         [ -  + ]:        429 :     if (label == "*")
#     193                 :          0 :         throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, "Invalid label name");
#     194                 :        429 :     return label;
#     195                 :        429 : }
#     196                 :            : 
#     197                 :            : /**
#     198                 :            :  * Update coin control with fee estimation based on the given parameters
#     199                 :            :  *
#     200                 :            :  * @param[in]     wallet            Wallet reference
#     201                 :            :  * @param[in,out] cc                Coin control to be updated
#     202                 :            :  * @param[in]     conf_target       UniValue integer; confirmation target in blocks, values between 1 and 1008 are valid per policy/fees.h;
#     203                 :            :  * @param[in]     estimate_mode     UniValue string; fee estimation mode, valid values are "unset", "economical" or "conservative";
#     204                 :            :  * @param[in]     fee_rate          UniValue real; fee rate in sat/vB;
#     205                 :            :  *                                      if present, both conf_target and estimate_mode must either be null, or "unset"
#     206                 :            :  * @param[in]     override_min_fee  bool; whether to set fOverrideFeeRate to true to disable minimum fee rate checks and instead
#     207                 :            :  *                                      verify only that fee_rate is greater than 0
#     208                 :            :  * @throws a JSONRPCError if conf_target, estimate_mode, or fee_rate contain invalid values or are in conflict
#     209                 :            :  */
#     210                 :            : static void SetFeeEstimateMode(const CWallet& wallet, CCoinControl& cc, const UniValue& conf_target, const UniValue& estimate_mode, const UniValue& fee_rate, bool override_min_fee)
#     211                 :       2756 : {
#     212         [ +  + ]:       2756 :     if (!fee_rate.isNull()) {
#     213         [ +  + ]:        382 :         if (!conf_target.isNull()) {
#     214                 :          6 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot specify both conf_target and fee_rate. Please provide either a confirmation target in blocks for automatic fee estimation, or an explicit fee rate.");
#     215                 :          6 :         }
#     216 [ +  + ][ +  + ]:        376 :         if (!estimate_mode.isNull() && estimate_mode.get_str() != "unset") {
#     217                 :          6 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot specify both estimate_mode and fee_rate");
#     218                 :          6 :         }
#     219                 :            :         // Fee rates in sat/vB cannot represent more than 3 significant digits.
#     220                 :        370 :         cc.m_feerate = CFeeRate{AmountFromValue(fee_rate, /* decimals */ 3)};
#     221         [ +  + ]:        370 :         if (override_min_fee) cc.fOverrideFeeRate = true;
#     222                 :            :         // Default RBF to true for explicit fee_rate, if unset.
#     223         [ +  + ]:        370 :         if (!cc.m_signal_bip125_rbf) cc.m_signal_bip125_rbf = true;
#     224                 :        370 :         return;
#     225                 :        370 :     }
#     226 [ +  + ][ +  + ]:       2374 :     if (!estimate_mode.isNull() && !FeeModeFromString(estimate_mode.get_str(), cc.m_fee_mode)) {
#     227                 :         58 :         throw JSONRPCError(RPC_INVALID_PARAMETER, InvalidEstimateModeErrorMessage());
#     228                 :         58 :     }
#     229         [ +  + ]:       2316 :     if (!conf_target.isNull()) {
#     230                 :         70 :         cc.m_confirm_target = ParseConfirmTarget(conf_target, wallet.chain().estimateMaxBlocks());
#     231                 :         70 :     }
#     232                 :       2316 : }
#     233                 :            : 
#     234                 :            : static RPCHelpMan getnewaddress()
#     235                 :      14003 : {
#     236                 :      14003 :     return RPCHelpMan{"getnewaddress",
#     237                 :      14003 :                 "\nReturns a new Bitcoin address for receiving payments.\n"
#     238                 :      14003 :                 "If 'label' is specified, it is added to the address book \n"
#     239                 :      14003 :                 "so payments received with the address will be associated with 'label'.\n",
#     240                 :      14003 :                 {
#     241                 :      14003 :                     {"label", RPCArg::Type::STR, RPCArg::Default{""}, "The label name for the address to be linked to. It can also be set to the empty string \"\" to represent the default label. The label does not need to exist, it will be created if there is no label by the given name."},
#     242                 :      14003 :                     {"address_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -addresstype"}, "The address type to use. Options are \"legacy\", \"p2sh-segwit\", and \"bech32\"."},
#     243                 :      14003 :                 },
#     244                 :      14003 :                 RPCResult{
#     245                 :      14003 :                     RPCResult::Type::STR, "address", "The new bitcoin address"
#     246                 :      14003 :                 },
#     247                 :      14003 :                 RPCExamples{
#     248                 :      14003 :                     HelpExampleCli("getnewaddress", "")
#     249                 :      14003 :             + HelpExampleRpc("getnewaddress", "")
#     250                 :      14003 :                 },
#     251                 :      14003 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     252                 :      14003 : {
#     253                 :      12683 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     254         [ -  + ]:      12683 :     if (!pwallet) return NullUniValue;
#     255                 :            : 
#     256                 :      12683 :     LOCK(pwallet->cs_wallet);
#     257                 :            : 
#     258         [ +  + ]:      12683 :     if (!pwallet->CanGetAddresses()) {
#     259                 :         29 :         throw JSONRPCError(RPC_WALLET_ERROR, "Error: This wallet has no available keys");
#     260                 :         29 :     }
#     261                 :            : 
#     262                 :            :     // Parse the label first so we don't generate a key if there's an error
#     263                 :      12654 :     std::string label;
#     264         [ +  + ]:      12654 :     if (!request.params[0].isNull())
#     265                 :        255 :         label = LabelFromValue(request.params[0]);
#     266                 :            : 
#     267                 :      12654 :     OutputType output_type = pwallet->m_default_address_type;
#     268         [ +  + ]:      12654 :     if (!request.params[1].isNull()) {
#     269         [ +  + ]:       2037 :         if (!ParseOutputType(request.params[1].get_str(), output_type)) {
#     270                 :          2 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Unknown address type '%s'", request.params[1].get_str()));
#     271                 :          2 :         }
#     272                 :      12652 :     }
#     273                 :            : 
#     274                 :      12652 :     CTxDestination dest;
#     275                 :      12652 :     std::string error;
#     276         [ +  + ]:      12652 :     if (!pwallet->GetNewDestination(output_type, label, dest, error)) {
#     277                 :          7 :         throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error);
#     278                 :          7 :     }
#     279                 :            : 
#     280                 :      12645 :     return EncodeDestination(dest);
#     281                 :      12645 : },
#     282                 :      14003 :     };
#     283                 :      14003 : }
#     284                 :            : 
#     285                 :            : static RPCHelpMan getrawchangeaddress()
#     286                 :       1547 : {
#     287                 :       1547 :     return RPCHelpMan{"getrawchangeaddress",
#     288                 :       1547 :                 "\nReturns a new Bitcoin address, for receiving change.\n"
#     289                 :       1547 :                 "This is for use with raw transactions, NOT normal use.\n",
#     290                 :       1547 :                 {
#     291                 :       1547 :                     {"address_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -changetype"}, "The address type to use. Options are \"legacy\", \"p2sh-segwit\", and \"bech32\"."},
#     292                 :       1547 :                 },
#     293                 :       1547 :                 RPCResult{
#     294                 :       1547 :                     RPCResult::Type::STR, "address", "The address"
#     295                 :       1547 :                 },
#     296                 :       1547 :                 RPCExamples{
#     297                 :       1547 :                     HelpExampleCli("getrawchangeaddress", "")
#     298                 :       1547 :             + HelpExampleRpc("getrawchangeaddress", "")
#     299                 :       1547 :                 },
#     300                 :       1547 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     301                 :       1547 : {
#     302                 :        229 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     303         [ -  + ]:        229 :     if (!pwallet) return NullUniValue;
#     304                 :            : 
#     305                 :        229 :     LOCK(pwallet->cs_wallet);
#     306                 :            : 
#     307         [ +  + ]:        229 :     if (!pwallet->CanGetAddresses(true)) {
#     308                 :         30 :         throw JSONRPCError(RPC_WALLET_ERROR, "Error: This wallet has no available keys");
#     309                 :         30 :     }
#     310                 :            : 
#     311                 :        199 :     OutputType output_type = pwallet->m_default_change_type.value_or(pwallet->m_default_address_type);
#     312         [ +  + ]:        199 :     if (!request.params[0].isNull()) {
#     313         [ +  + ]:        114 :         if (!ParseOutputType(request.params[0].get_str(), output_type)) {
#     314                 :          4 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Unknown address type '%s'", request.params[0].get_str()));
#     315                 :          4 :         }
#     316                 :        195 :     }
#     317                 :            : 
#     318                 :        195 :     CTxDestination dest;
#     319                 :        195 :     std::string error;
#     320         [ +  + ]:        195 :     if (!pwallet->GetNewChangeDestination(output_type, dest, error)) {
#     321                 :          2 :         throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error);
#     322                 :          2 :     }
#     323                 :        193 :     return EncodeDestination(dest);
#     324                 :        193 : },
#     325                 :       1547 :     };
#     326                 :       1547 : }
#     327                 :            : 
#     328                 :            : 
#     329                 :            : static RPCHelpMan setlabel()
#     330                 :       1347 : {
#     331                 :       1347 :     return RPCHelpMan{"setlabel",
#     332                 :       1347 :                 "\nSets the label associated with the given address.\n",
#     333                 :       1347 :                 {
#     334                 :       1347 :                     {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to be associated with a label."},
#     335                 :       1347 :                     {"label", RPCArg::Type::STR, RPCArg::Optional::NO, "The label to assign to the address."},
#     336                 :       1347 :                 },
#     337                 :       1347 :                 RPCResult{RPCResult::Type::NONE, "", ""},
#     338                 :       1347 :                 RPCExamples{
#     339                 :       1347 :                     HelpExampleCli("setlabel", "\"" + EXAMPLE_ADDRESS[0] + "\" \"tabby\"")
#     340                 :       1347 :             + HelpExampleRpc("setlabel", "\"" + EXAMPLE_ADDRESS[0] + "\", \"tabby\"")
#     341                 :       1347 :                 },
#     342                 :       1347 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     343                 :       1347 : {
#     344                 :         29 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     345         [ -  + ]:         29 :     if (!pwallet) return NullUniValue;
#     346                 :            : 
#     347                 :         29 :     LOCK(pwallet->cs_wallet);
#     348                 :            : 
#     349                 :         29 :     CTxDestination dest = DecodeDestination(request.params[0].get_str());
#     350         [ +  + ]:         29 :     if (!IsValidDestination(dest)) {
#     351                 :          1 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
#     352                 :          1 :     }
#     353                 :            : 
#     354                 :         28 :     std::string label = LabelFromValue(request.params[1]);
#     355                 :            : 
#     356         [ +  - ]:         28 :     if (pwallet->IsMine(dest)) {
#     357                 :         28 :         pwallet->SetAddressBook(dest, label, "receive");
#     358                 :         28 :     } else {
#     359                 :          0 :         pwallet->SetAddressBook(dest, label, "send");
#     360                 :          0 :     }
#     361                 :            : 
#     362                 :         28 :     return NullUniValue;
#     363                 :         28 : },
#     364                 :       1347 :     };
#     365                 :       1347 : }
#     366                 :            : 
#     367                 :       2082 : void ParseRecipients(const UniValue& address_amounts, const UniValue& subtract_fee_outputs, std::vector<CRecipient> &recipients) {
#     368                 :       2082 :     std::set<CTxDestination> destinations;
#     369                 :       2082 :     int i = 0;
#     370         [ +  + ]:       9953 :     for (const std::string& address: address_amounts.getKeys()) {
#     371                 :       9953 :         CTxDestination dest = DecodeDestination(address);
#     372         [ -  + ]:       9953 :         if (!IsValidDestination(dest)) {
#     373                 :          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ") + address);
#     374                 :          0 :         }
#     375                 :            : 
#     376         [ -  + ]:       9953 :         if (destinations.count(dest)) {
#     377                 :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ") + address);
#     378                 :          0 :         }
#     379                 :       9953 :         destinations.insert(dest);
#     380                 :            : 
#     381                 :       9953 :         CScript script_pub_key = GetScriptForDestination(dest);
#     382                 :       9953 :         CAmount amount = AmountFromValue(address_amounts[i++]);
#     383                 :            : 
#     384                 :       9953 :         bool subtract_fee = false;
#     385         [ +  + ]:       9989 :         for (unsigned int idx = 0; idx < subtract_fee_outputs.size(); idx++) {
#     386                 :         36 :             const UniValue& addr = subtract_fee_outputs[idx];
#     387         [ +  - ]:         36 :             if (addr.get_str() == address) {
#     388                 :         36 :                 subtract_fee = true;
#     389                 :         36 :             }
#     390                 :         36 :         }
#     391                 :            : 
#     392                 :       9953 :         CRecipient recipient = {script_pub_key, amount, subtract_fee};
#     393                 :       9953 :         recipients.push_back(recipient);
#     394                 :       9953 :     }
#     395                 :       2082 : }
#     396                 :            : 
#     397                 :            : UniValue SendMoney(CWallet& wallet, const CCoinControl &coin_control, std::vector<CRecipient> &recipients, mapValue_t map_value, bool verbose)
#     398                 :       2078 : {
#     399                 :       2078 :     EnsureWalletIsUnlocked(wallet);
#     400                 :            : 
#     401                 :            :     // This function is only used by sendtoaddress and sendmany.
#     402                 :            :     // This should always try to sign, if we don't have private keys, don't try to do anything here.
#     403         [ +  + ]:       2078 :     if (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
#     404                 :          4 :         throw JSONRPCError(RPC_WALLET_ERROR, "Error: Private keys are disabled for this wallet");
#     405                 :          4 :     }
#     406                 :            : 
#     407                 :            :     // Shuffle recipient list
#     408                 :       2074 :     std::shuffle(recipients.begin(), recipients.end(), FastRandomContext());
#     409                 :            : 
#     410                 :            :     // Send
#     411                 :       2074 :     CAmount nFeeRequired = 0;
#     412                 :       2074 :     int nChangePosRet = -1;
#     413                 :       2074 :     bilingual_str error;
#     414                 :       2074 :     CTransactionRef tx;
#     415                 :       2074 :     FeeCalculation fee_calc_out;
#     416                 :       2074 :     const bool fCreated = wallet.CreateTransaction(recipients, tx, nFeeRequired, nChangePosRet, error, coin_control, fee_calc_out, true);
#     417         [ +  + ]:       2074 :     if (!fCreated) {
#     418                 :         40 :         throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, error.original);
#     419                 :         40 :     }
#     420                 :       2034 :     wallet.CommitTransaction(tx, std::move(map_value), {} /* orderForm */);
#     421         [ +  + ]:       2034 :     if (verbose) {
#     422                 :          4 :         UniValue entry(UniValue::VOBJ);
#     423                 :          4 :         entry.pushKV("txid", tx->GetHash().GetHex());
#     424                 :          4 :         entry.pushKV("fee_reason", StringForFeeReason(fee_calc_out.reason));
#     425                 :          4 :         return entry;
#     426                 :          4 :     }
#     427                 :       2030 :     return tx->GetHash().GetHex();
#     428                 :       2030 : }
#     429                 :            : 
#     430                 :            : static RPCHelpMan sendtoaddress()
#     431                 :       3269 : {
#     432                 :       3269 :     return RPCHelpMan{"sendtoaddress",
#     433                 :       3269 :                 "\nSend an amount to a given address." +
#     434                 :       3269 :         HELP_REQUIRING_PASSPHRASE,
#     435                 :       3269 :                 {
#     436                 :       3269 :                     {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to send to."},
#     437                 :       3269 :                     {"amount", RPCArg::Type::AMOUNT, RPCArg::Optional::NO, "The amount in " + CURRENCY_UNIT + " to send. eg 0.1"},
#     438                 :       3269 :                     {"comment", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "A comment used to store what the transaction is for.\n"
#     439                 :       3269 :                                          "This is not part of the transaction, just kept in your wallet."},
#     440                 :       3269 :                     {"comment_to", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "A comment to store the name of the person or organization\n"
#     441                 :       3269 :                                          "to which you're sending the transaction. This is not part of the \n"
#     442                 :       3269 :                                          "transaction, just kept in your wallet."},
#     443                 :       3269 :                     {"subtractfeefromamount", RPCArg::Type::BOOL, RPCArg::Default{false}, "The fee will be deducted from the amount being sent.\n"
#     444                 :       3269 :                                          "The recipient will receive less bitcoins than you enter in the amount field."},
#     445                 :       3269 :                     {"replaceable", RPCArg::Type::BOOL, RPCArg::DefaultHint{"wallet default"}, "Allow this transaction to be replaced by a transaction with higher fees via BIP 125"},
#     446                 :       3269 :                     {"conf_target", RPCArg::Type::NUM, RPCArg::DefaultHint{"wallet -txconfirmtarget"}, "Confirmation target in blocks"},
#     447                 :       3269 :                     {"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"unset"}, std::string() + "The fee estimate mode, must be one of (case insensitive):\n"
#     448                 :       3269 :             "       \"" + FeeModes("\"\n\"") + "\""},
#     449                 :       3269 :                     {"avoid_reuse", RPCArg::Type::BOOL, RPCArg::Default{true}, "(only available if avoid_reuse wallet flag is set) Avoid spending from dirty addresses; addresses are considered\n"
#     450                 :       3269 :                                          "dirty if they have previously been used in a transaction. If true, this also activates avoidpartialspends, grouping outputs by their addresses."},
#     451                 :       3269 :                     {"fee_rate", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"not set, fall back to wallet fee estimation"}, "Specify a fee rate in " + CURRENCY_ATOM + "/vB."},
#     452                 :       3269 :                     {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "If true, return extra information about the transaction."},
#     453                 :       3269 :                 },
#     454                 :       3269 :                 {
#     455                 :       3269 :                     RPCResult{"if verbose is not set or set to false",
#     456                 :       3269 :                         RPCResult::Type::STR_HEX, "txid", "The transaction id."
#     457                 :       3269 :                     },
#     458                 :       3269 :                     RPCResult{"if verbose is set to true",
#     459                 :       3269 :                         RPCResult::Type::OBJ, "", "",
#     460                 :       3269 :                         {
#     461                 :       3269 :                             {RPCResult::Type::STR_HEX, "txid", "The transaction id."},
#     462                 :       3269 :                             {RPCResult::Type::STR, "fee reason", "The transaction fee reason."}
#     463                 :       3269 :                         },
#     464                 :       3269 :                     },
#     465                 :       3269 :                 },
#     466                 :       3269 :                 RPCExamples{
#     467                 :       3269 :                     "\nSend 0.1 BTC\n"
#     468                 :       3269 :                     + HelpExampleCli("sendtoaddress", "\"" + EXAMPLE_ADDRESS[0] + "\" 0.1") +
#     469                 :       3269 :                     "\nSend 0.1 BTC with a confirmation target of 6 blocks in economical fee estimate mode using positional arguments\n"
#     470                 :       3269 :                     + HelpExampleCli("sendtoaddress", "\"" + EXAMPLE_ADDRESS[0] + "\" 0.1 \"donation\" \"sean's outpost\" false true 6 economical") +
#     471                 :       3269 :                     "\nSend 0.1 BTC with a fee rate of 1.1 " + CURRENCY_ATOM + "/vB, subtract fee from amount, BIP125-replaceable, using positional arguments\n"
#     472                 :       3269 :                     + HelpExampleCli("sendtoaddress", "\"" + EXAMPLE_ADDRESS[0] + "\" 0.1 \"drinks\" \"room77\" true true null \"unset\" null 1.1") +
#     473                 :       3269 :                     "\nSend 0.2 BTC with a confirmation target of 6 blocks in economical fee estimate mode using named arguments\n"
#     474                 :       3269 :                     + HelpExampleCli("-named sendtoaddress", "address=\"" + EXAMPLE_ADDRESS[0] + "\" amount=0.2 conf_target=6 estimate_mode=\"economical\"") +
#     475                 :       3269 :                     "\nSend 0.5 BTC with a fee rate of 25 " + CURRENCY_ATOM + "/vB using named arguments\n"
#     476                 :       3269 :                     + HelpExampleCli("-named sendtoaddress", "address=\"" + EXAMPLE_ADDRESS[0] + "\" amount=0.5 fee_rate=25")
#     477                 :       3269 :                     + HelpExampleCli("-named sendtoaddress", "address=\"" + EXAMPLE_ADDRESS[0] + "\" amount=0.5 fee_rate=25 subtractfeefromamount=false replaceable=true avoid_reuse=true comment=\"2 pizzas\" comment_to=\"jeremy\" verbose=true")
#     478                 :       3269 :                 },
#     479                 :       3269 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     480                 :       3269 : {
#     481                 :       1951 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     482         [ -  + ]:       1951 :     if (!pwallet) return NullUniValue;
#     483                 :            : 
#     484                 :            :     // Make sure the results are valid at least up to the most recent block
#     485                 :            :     // the user could have gotten from another RPC command prior to now
#     486                 :       1951 :     pwallet->BlockUntilSyncedToCurrentChain();
#     487                 :            : 
#     488                 :       1951 :     LOCK(pwallet->cs_wallet);
#     489                 :            : 
#     490                 :            :     // Wallet comments
#     491                 :       1951 :     mapValue_t mapValue;
#     492 [ +  + ][ +  + ]:       1951 :     if (!request.params[2].isNull() && !request.params[2].get_str().empty())
#     493                 :          2 :         mapValue["comment"] = request.params[2].get_str();
#     494 [ +  + ][ +  + ]:       1951 :     if (!request.params[3].isNull() && !request.params[3].get_str().empty())
#     495                 :          2 :         mapValue["to"] = request.params[3].get_str();
#     496                 :            : 
#     497                 :       1951 :     bool fSubtractFeeFromAmount = false;
#     498         [ +  + ]:       1951 :     if (!request.params[4].isNull()) {
#     499                 :         34 :         fSubtractFeeFromAmount = request.params[4].get_bool();
#     500                 :         34 :     }
#     501                 :            : 
#     502                 :       1951 :     CCoinControl coin_control;
#     503         [ +  + ]:       1951 :     if (!request.params[5].isNull()) {
#     504                 :          2 :         coin_control.m_signal_bip125_rbf = request.params[5].get_bool();
#     505                 :          2 :     }
#     506                 :            : 
#     507                 :       1951 :     coin_control.m_avoid_address_reuse = GetAvoidReuseFlag(*pwallet, request.params[8]);
#     508                 :            :     // We also enable partial spend avoidance if reuse avoidance is set.
#     509                 :       1951 :     coin_control.m_avoid_partial_spends |= coin_control.m_avoid_address_reuse;
#     510                 :            : 
#     511                 :       1951 :     SetFeeEstimateMode(*pwallet, coin_control, /* conf_target */ request.params[6], /* estimate_mode */ request.params[7], /* fee_rate */ request.params[9], /* override_min_fee */ false);
#     512                 :            : 
#     513                 :       1951 :     EnsureWalletIsUnlocked(*pwallet);
#     514                 :            : 
#     515                 :       1951 :     UniValue address_amounts(UniValue::VOBJ);
#     516                 :       1951 :     const std::string address = request.params[0].get_str();
#     517                 :       1951 :     address_amounts.pushKV(address, request.params[1]);
#     518                 :       1951 :     UniValue subtractFeeFromAmount(UniValue::VARR);
#     519         [ +  + ]:       1951 :     if (fSubtractFeeFromAmount) {
#     520                 :         32 :         subtractFeeFromAmount.push_back(address);
#     521                 :         32 :     }
#     522                 :            : 
#     523                 :       1951 :     std::vector<CRecipient> recipients;
#     524                 :       1951 :     ParseRecipients(address_amounts, subtractFeeFromAmount, recipients);
#     525         [ +  + ]:       1951 :     const bool verbose{request.params[10].isNull() ? false : request.params[10].get_bool()};
#     526                 :            : 
#     527                 :       1951 :     return SendMoney(*pwallet, coin_control, recipients, mapValue, verbose);
#     528                 :       1951 : },
#     529                 :       3269 :     };
#     530                 :       3269 : }
#     531                 :            : 
#     532                 :            : static RPCHelpMan listaddressgroupings()
#     533                 :       1322 : {
#     534                 :       1322 :     return RPCHelpMan{"listaddressgroupings",
#     535                 :       1322 :                 "\nLists groups of addresses which have had their common ownership\n"
#     536                 :       1322 :                 "made public by common use as inputs or as the resulting change\n"
#     537                 :       1322 :                 "in past transactions\n",
#     538                 :       1322 :                 {},
#     539                 :       1322 :                 RPCResult{
#     540                 :       1322 :                     RPCResult::Type::ARR, "", "",
#     541                 :       1322 :                     {
#     542                 :       1322 :                         {RPCResult::Type::ARR, "", "",
#     543                 :       1322 :                         {
#     544                 :       1322 :                             {RPCResult::Type::ARR_FIXED, "", "",
#     545                 :       1322 :                             {
#     546                 :       1322 :                                 {RPCResult::Type::STR, "address", "The bitcoin address"},
#     547                 :       1322 :                                 {RPCResult::Type::STR_AMOUNT, "amount", "The amount in " + CURRENCY_UNIT},
#     548                 :       1322 :                                 {RPCResult::Type::STR, "label", /* optional */ true, "The label"},
#     549                 :       1322 :                             }},
#     550                 :       1322 :                         }},
#     551                 :       1322 :                     }
#     552                 :       1322 :                 },
#     553                 :       1322 :                 RPCExamples{
#     554                 :       1322 :                     HelpExampleCli("listaddressgroupings", "")
#     555                 :       1322 :             + HelpExampleRpc("listaddressgroupings", "")
#     556                 :       1322 :                 },
#     557                 :       1322 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     558                 :       1322 : {
#     559                 :          4 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     560         [ -  + ]:          4 :     if (!pwallet) return NullUniValue;
#     561                 :            : 
#     562                 :            :     // Make sure the results are valid at least up to the most recent block
#     563                 :            :     // the user could have gotten from another RPC command prior to now
#     564                 :          4 :     pwallet->BlockUntilSyncedToCurrentChain();
#     565                 :            : 
#     566                 :          4 :     LOCK(pwallet->cs_wallet);
#     567                 :            : 
#     568                 :          4 :     UniValue jsonGroupings(UniValue::VARR);
#     569                 :          4 :     std::map<CTxDestination, CAmount> balances = pwallet->GetAddressBalances();
#     570         [ +  + ]:          6 :     for (const std::set<CTxDestination>& grouping : pwallet->GetAddressGroupings()) {
#     571                 :          6 :         UniValue jsonGrouping(UniValue::VARR);
#     572         [ +  + ]:          6 :         for (const CTxDestination& address : grouping)
#     573                 :          8 :         {
#     574                 :          8 :             UniValue addressInfo(UniValue::VARR);
#     575                 :          8 :             addressInfo.push_back(EncodeDestination(address));
#     576                 :          8 :             addressInfo.push_back(ValueFromAmount(balances[address]));
#     577                 :          8 :             {
#     578                 :          8 :                 const auto* address_book_entry = pwallet->FindAddressBookEntry(address);
#     579         [ +  - ]:          8 :                 if (address_book_entry) {
#     580                 :          8 :                     addressInfo.push_back(address_book_entry->GetLabel());
#     581                 :          8 :                 }
#     582                 :          8 :             }
#     583                 :          8 :             jsonGrouping.push_back(addressInfo);
#     584                 :          8 :         }
#     585                 :          6 :         jsonGroupings.push_back(jsonGrouping);
#     586                 :          6 :     }
#     587                 :          4 :     return jsonGroupings;
#     588                 :          4 : },
#     589                 :       1322 :     };
#     590                 :       1322 : }
#     591                 :            : 
#     592                 :            : static RPCHelpMan signmessage()
#     593                 :       1349 : {
#     594                 :       1349 :     return RPCHelpMan{"signmessage",
#     595                 :       1349 :                 "\nSign a message with the private key of an address" +
#     596                 :       1349 :         HELP_REQUIRING_PASSPHRASE,
#     597                 :       1349 :                 {
#     598                 :       1349 :                     {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to use for the private key."},
#     599                 :       1349 :                     {"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message to create a signature of."},
#     600                 :       1349 :                 },
#     601                 :       1349 :                 RPCResult{
#     602                 :       1349 :                     RPCResult::Type::STR, "signature", "The signature of the message encoded in base 64"
#     603                 :       1349 :                 },
#     604                 :       1349 :                 RPCExamples{
#     605                 :       1349 :             "\nUnlock the wallet for 30 seconds\n"
#     606                 :       1349 :             + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
#     607                 :       1349 :             "\nCreate the signature\n"
#     608                 :       1349 :             + HelpExampleCli("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +
#     609                 :       1349 :             "\nVerify the signature\n"
#     610                 :       1349 :             + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
#     611                 :       1349 :             "\nAs a JSON-RPC call\n"
#     612                 :       1349 :             + HelpExampleRpc("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"my message\"")
#     613                 :       1349 :                 },
#     614                 :       1349 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     615                 :       1349 : {
#     616                 :         26 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     617         [ -  + ]:         26 :     if (!pwallet) return NullUniValue;
#     618                 :            : 
#     619                 :         26 :     LOCK(pwallet->cs_wallet);
#     620                 :            : 
#     621                 :         26 :     EnsureWalletIsUnlocked(*pwallet);
#     622                 :            : 
#     623                 :         26 :     std::string strAddress = request.params[0].get_str();
#     624                 :         26 :     std::string strMessage = request.params[1].get_str();
#     625                 :            : 
#     626                 :         26 :     CTxDestination dest = DecodeDestination(strAddress);
#     627         [ +  + ]:         26 :     if (!IsValidDestination(dest)) {
#     628                 :          1 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
#     629                 :          1 :     }
#     630                 :            : 
#     631                 :         25 :     const PKHash* pkhash = std::get_if<PKHash>(&dest);
#     632         [ -  + ]:         25 :     if (!pkhash) {
#     633                 :          0 :         throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
#     634                 :          0 :     }
#     635                 :            : 
#     636                 :         25 :     std::string signature;
#     637                 :         25 :     SigningResult err = pwallet->SignMessage(strMessage, *pkhash, signature);
#     638         [ -  + ]:         25 :     if (err == SigningResult::SIGNING_FAILED) {
#     639                 :          0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, SigningResultString(err));
#     640         [ -  + ]:         25 :     } else if (err != SigningResult::OK){
#     641                 :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, SigningResultString(err));
#     642                 :          0 :     }
#     643                 :            : 
#     644                 :         25 :     return signature;
#     645                 :         25 : },
#     646                 :       1349 :     };
#     647                 :       1349 : }
#     648                 :            : 
#     649                 :            : static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
#     650                 :         52 : {
#     651                 :         52 :     std::set<CTxDestination> address_set;
#     652                 :            : 
#     653         [ +  + ]:         52 :     if (by_label) {
#     654                 :            :         // Get the set of addresses assigned to label
#     655                 :         34 :         std::string label = LabelFromValue(params[0]);
#     656                 :         34 :         address_set = wallet.GetLabelAddresses(label);
#     657                 :         34 :     } else {
#     658                 :            :         // Get the address
#     659                 :         18 :         CTxDestination dest = DecodeDestination(params[0].get_str());
#     660         [ -  + ]:         18 :         if (!IsValidDestination(dest)) {
#     661                 :          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
#     662                 :          0 :         }
#     663                 :         18 :         CScript script_pub_key = GetScriptForDestination(dest);
#     664         [ +  + ]:         18 :         if (!wallet.IsMine(script_pub_key)) {
#     665                 :          2 :             throw JSONRPCError(RPC_WALLET_ERROR, "Address not found in wallet");
#     666                 :          2 :         }
#     667                 :         16 :         address_set.insert(dest);
#     668                 :         16 :     }
#     669                 :            : 
#     670                 :            :     // Minimum confirmations
#     671                 :         52 :     int min_depth = 1;
#     672         [ +  + ]:         50 :     if (!params[1].isNull())
#     673                 :          2 :         min_depth = params[1].get_int();
#     674                 :            : 
#     675                 :            :     // Tally
#     676                 :         50 :     CAmount amount = 0;
#     677         [ +  + ]:       4400 :     for (const std::pair<const uint256, CWalletTx>& wtx_pair : wallet.mapWallet) {
#     678                 :       4400 :         const CWalletTx& wtx = wtx_pair.second;
#     679 [ +  + ][ -  + ]:       4400 :         if (wtx.IsCoinBase() || !wallet.chain().checkFinalTx(*wtx.tx) || wtx.GetDepthInMainChain() < min_depth) {
#                 [ +  + ]
#     680                 :       4126 :             continue;
#     681                 :       4126 :         }
#     682                 :            : 
#     683         [ +  + ]:        518 :         for (const CTxOut& txout : wtx.tx->vout) {
#     684                 :        518 :             CTxDestination address;
#     685 [ +  - ][ +  + ]:        518 :             if (ExtractDestination(txout.scriptPubKey, address) && wallet.IsMine(address) && address_set.count(address)) {
#                 [ +  + ]
#     686                 :         64 :                 amount += txout.nValue;
#     687                 :         64 :             }
#     688                 :        518 :         }
#     689                 :        274 :     }
#     690                 :            : 
#     691                 :         50 :     return amount;
#     692                 :         52 : }
#     693                 :            : 
#     694                 :            : 
#     695                 :            : static RPCHelpMan getreceivedbyaddress()
#     696                 :       1336 : {
#     697                 :       1336 :     return RPCHelpMan{"getreceivedbyaddress",
#     698                 :       1336 :                 "\nReturns the total amount received by the given address in transactions with at least minconf confirmations.\n",
#     699                 :       1336 :                 {
#     700                 :       1336 :                     {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address for transactions."},
#     701                 :       1336 :                     {"minconf", RPCArg::Type::NUM, RPCArg::Default{1}, "Only include transactions confirmed at least this many times."},
#     702                 :       1336 :                 },
#     703                 :       1336 :                 RPCResult{
#     704                 :       1336 :                     RPCResult::Type::STR_AMOUNT, "amount", "The total amount in " + CURRENCY_UNIT + " received at this address."
#     705                 :       1336 :                 },
#     706                 :       1336 :                 RPCExamples{
#     707                 :       1336 :             "\nThe amount from transactions with at least 1 confirmation\n"
#     708                 :       1336 :             + HelpExampleCli("getreceivedbyaddress", "\"" + EXAMPLE_ADDRESS[0] + "\"") +
#     709                 :       1336 :             "\nThe amount including unconfirmed transactions, zero confirmations\n"
#     710                 :       1336 :             + HelpExampleCli("getreceivedbyaddress", "\"" + EXAMPLE_ADDRESS[0] + "\" 0") +
#     711                 :       1336 :             "\nThe amount with at least 6 confirmations\n"
#     712                 :       1336 :             + HelpExampleCli("getreceivedbyaddress", "\"" + EXAMPLE_ADDRESS[0] + "\" 6") +
#     713                 :       1336 :             "\nAs a JSON-RPC call\n"
#     714                 :       1336 :             + HelpExampleRpc("getreceivedbyaddress", "\"" + EXAMPLE_ADDRESS[0] + "\", 6")
#     715                 :       1336 :                 },
#     716                 :       1336 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     717                 :       1336 : {
#     718                 :         18 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     719         [ -  + ]:         18 :     if (!pwallet) return NullUniValue;
#     720                 :            : 
#     721                 :            :     // Make sure the results are valid at least up to the most recent block
#     722                 :            :     // the user could have gotten from another RPC command prior to now
#     723                 :         18 :     pwallet->BlockUntilSyncedToCurrentChain();
#     724                 :            : 
#     725                 :         18 :     LOCK(pwallet->cs_wallet);
#     726                 :            : 
#     727                 :         18 :     return ValueFromAmount(GetReceived(*pwallet, request.params, /* by_label */ false));
#     728                 :         18 : },
#     729                 :       1336 :     };
#     730                 :       1336 : }
#     731                 :            : 
#     732                 :            : 
#     733                 :            : static RPCHelpMan getreceivedbylabel()
#     734                 :       1352 : {
#     735                 :       1352 :     return RPCHelpMan{"getreceivedbylabel",
#     736                 :       1352 :                 "\nReturns the total amount received by addresses with <label> in transactions with at least [minconf] confirmations.\n",
#     737                 :       1352 :                 {
#     738                 :       1352 :                     {"label", RPCArg::Type::STR, RPCArg::Optional::NO, "The selected label, may be the default label using \"\"."},
#     739                 :       1352 :                     {"minconf", RPCArg::Type::NUM, RPCArg::Default{1}, "Only include transactions confirmed at least this many times."},
#     740                 :       1352 :                 },
#     741                 :       1352 :                 RPCResult{
#     742                 :       1352 :                     RPCResult::Type::STR_AMOUNT, "amount", "The total amount in " + CURRENCY_UNIT + " received for this label."
#     743                 :       1352 :                 },
#     744                 :       1352 :                 RPCExamples{
#     745                 :       1352 :             "\nAmount received by the default label with at least 1 confirmation\n"
#     746                 :       1352 :             + HelpExampleCli("getreceivedbylabel", "\"\"") +
#     747                 :       1352 :             "\nAmount received at the tabby label including unconfirmed amounts with zero confirmations\n"
#     748                 :       1352 :             + HelpExampleCli("getreceivedbylabel", "\"tabby\" 0") +
#     749                 :       1352 :             "\nThe amount with at least 6 confirmations\n"
#     750                 :       1352 :             + HelpExampleCli("getreceivedbylabel", "\"tabby\" 6") +
#     751                 :       1352 :             "\nAs a JSON-RPC call\n"
#     752                 :       1352 :             + HelpExampleRpc("getreceivedbylabel", "\"tabby\", 6")
#     753                 :       1352 :                 },
#     754                 :       1352 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     755                 :       1352 : {
#     756                 :         34 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     757         [ -  + ]:         34 :     if (!pwallet) return NullUniValue;
#     758                 :            : 
#     759                 :            :     // Make sure the results are valid at least up to the most recent block
#     760                 :            :     // the user could have gotten from another RPC command prior to now
#     761                 :         34 :     pwallet->BlockUntilSyncedToCurrentChain();
#     762                 :            : 
#     763                 :         34 :     LOCK(pwallet->cs_wallet);
#     764                 :            : 
#     765                 :         34 :     return ValueFromAmount(GetReceived(*pwallet, request.params, /* by_label */ true));
#     766                 :         34 : },
#     767                 :       1352 :     };
#     768                 :       1352 : }
#     769                 :            : 
#     770                 :            : 
#     771                 :            : static RPCHelpMan getbalance()
#     772                 :       1696 : {
#     773                 :       1696 :     return RPCHelpMan{"getbalance",
#     774                 :       1696 :                 "\nReturns the total available balance.\n"
#     775                 :       1696 :                 "The available balance is what the wallet considers currently spendable, and is\n"
#     776                 :       1696 :                 "thus affected by options which limit spendability such as -spendzeroconfchange.\n",
#     777                 :       1696 :                 {
#     778                 :       1696 :                     {"dummy", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "Remains for backward compatibility. Must be excluded or set to \"*\"."},
#     779                 :       1696 :                     {"minconf", RPCArg::Type::NUM, RPCArg::Default{0}, "Only include transactions confirmed at least this many times."},
#     780                 :       1696 :                     {"include_watchonly", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Also include balance in watch-only addresses (see 'importaddress')"},
#     781                 :       1696 :                     {"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."},
#     782                 :       1696 :                 },
#     783                 :       1696 :                 RPCResult{
#     784                 :       1696 :                     RPCResult::Type::STR_AMOUNT, "amount", "The total amount in " + CURRENCY_UNIT + " received for this wallet."
#     785                 :       1696 :                 },
#     786                 :       1696 :                 RPCExamples{
#     787                 :       1696 :             "\nThe total amount in the wallet with 0 or more confirmations\n"
#     788                 :       1696 :             + HelpExampleCli("getbalance", "") +
#     789                 :       1696 :             "\nThe total amount in the wallet with at least 6 confirmations\n"
#     790                 :       1696 :             + HelpExampleCli("getbalance", "\"*\" 6") +
#     791                 :       1696 :             "\nAs a JSON-RPC call\n"
#     792                 :       1696 :             + HelpExampleRpc("getbalance", "\"*\", 6")
#     793                 :       1696 :                 },
#     794                 :       1696 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     795                 :       1696 : {
#     796                 :        378 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     797         [ -  + ]:        378 :     if (!pwallet) return NullUniValue;
#     798                 :            : 
#     799                 :            :     // Make sure the results are valid at least up to the most recent block
#     800                 :            :     // the user could have gotten from another RPC command prior to now
#     801                 :        378 :     pwallet->BlockUntilSyncedToCurrentChain();
#     802                 :            : 
#     803                 :        378 :     LOCK(pwallet->cs_wallet);
#     804                 :            : 
#     805                 :        378 :     const UniValue& dummy_value = request.params[0];
#     806 [ +  + ][ +  + ]:        378 :     if (!dummy_value.isNull() && dummy_value.get_str() != "*") {
#     807                 :          2 :         throw JSONRPCError(RPC_METHOD_DEPRECATED, "dummy first argument must be excluded or set to \"*\".");
#     808                 :          2 :     }
#     809                 :            : 
#     810                 :        376 :     int min_depth = 0;
#     811         [ +  + ]:        376 :     if (!request.params[1].isNull()) {
#     812                 :         40 :         min_depth = request.params[1].get_int();
#     813                 :         40 :     }
#     814                 :            : 
#     815                 :        376 :     bool include_watchonly = ParseIncludeWatchonly(request.params[2], *pwallet);
#     816                 :            : 
#     817                 :        376 :     bool avoid_reuse = GetAvoidReuseFlag(*pwallet, request.params[3]);
#     818                 :            : 
#     819                 :        376 :     const auto bal = pwallet->GetBalance(min_depth, avoid_reuse);
#     820                 :            : 
#     821         [ +  + ]:        376 :     return ValueFromAmount(bal.m_mine_trusted + (include_watchonly ? bal.m_watchonly_trusted : 0));
#     822                 :        376 : },
#     823                 :       1696 :     };
#     824                 :       1696 : }
#     825                 :            : 
#     826                 :            : static RPCHelpMan getunconfirmedbalance()
#     827                 :       1326 : {
#     828                 :       1326 :     return RPCHelpMan{"getunconfirmedbalance",
#     829                 :       1326 :                 "DEPRECATED\nIdentical to getbalances().mine.untrusted_pending\n",
#     830                 :       1326 :                 {},
#     831                 :       1326 :                 RPCResult{RPCResult::Type::NUM, "", "The balance"},
#     832                 :       1326 :                 RPCExamples{""},
#     833                 :       1326 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     834                 :       1326 : {
#     835                 :          8 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     836         [ -  + ]:          8 :     if (!pwallet) return NullUniValue;
#     837                 :            : 
#     838                 :            :     // Make sure the results are valid at least up to the most recent block
#     839                 :            :     // the user could have gotten from another RPC command prior to now
#     840                 :          8 :     pwallet->BlockUntilSyncedToCurrentChain();
#     841                 :            : 
#     842                 :          8 :     LOCK(pwallet->cs_wallet);
#     843                 :            : 
#     844                 :          8 :     return ValueFromAmount(pwallet->GetBalance().m_mine_untrusted_pending);
#     845                 :          8 : },
#     846                 :       1326 :     };
#     847                 :       1326 : }
#     848                 :            : 
#     849                 :            : 
#     850                 :            : static RPCHelpMan sendmany()
#     851                 :       1532 : {
#     852                 :       1532 :     return RPCHelpMan{"sendmany",
#     853                 :       1532 :                 "\nSend multiple times. Amounts are double-precision floating point numbers." +
#     854                 :       1532 :         HELP_REQUIRING_PASSPHRASE,
#     855                 :       1532 :                 {
#     856                 :       1532 :                     {"dummy", RPCArg::Type::STR, RPCArg::Optional::NO, "Must be set to \"\" for backwards compatibility.", "\"\""},
#     857                 :       1532 :                     {"amounts", RPCArg::Type::OBJ_USER_KEYS, RPCArg::Optional::NO, "The addresses and amounts",
#     858                 :       1532 :                         {
#     859                 :       1532 :                             {"address", RPCArg::Type::AMOUNT, RPCArg::Optional::NO, "The bitcoin address is the key, the numeric amount (can be string) in " + CURRENCY_UNIT + " is the value"},
#     860                 :       1532 :                         },
#     861                 :       1532 :                     },
#     862                 :       1532 :                     {"minconf", RPCArg::Type::NUM, RPCArg::Optional::OMITTED_NAMED_ARG, "Ignored dummy value"},
#     863                 :       1532 :                     {"comment", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "A comment"},
#     864                 :       1532 :                     {"subtractfeefrom", RPCArg::Type::ARR, RPCArg::Optional::OMITTED_NAMED_ARG, "The addresses.\n"
#     865                 :       1532 :                                        "The fee will be equally deducted from the amount of each selected address.\n"
#     866                 :       1532 :                                        "Those recipients will receive less bitcoins than you enter in their corresponding amount field.\n"
#     867                 :       1532 :                                        "If no addresses are specified here, the sender pays the fee.",
#     868                 :       1532 :                         {
#     869                 :       1532 :                             {"address", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Subtract fee from this address"},
#     870                 :       1532 :                         },
#     871                 :       1532 :                     },
#     872                 :       1532 :                     {"replaceable", RPCArg::Type::BOOL, RPCArg::DefaultHint{"wallet default"}, "Allow this transaction to be replaced by a transaction with higher fees via BIP 125"},
#     873                 :       1532 :                     {"conf_target", RPCArg::Type::NUM, RPCArg::DefaultHint{"wallet -txconfirmtarget"}, "Confirmation target in blocks"},
#     874                 :       1532 :                     {"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"unset"}, std::string() + "The fee estimate mode, must be one of (case insensitive):\n"
#     875                 :       1532 :             "       \"" + FeeModes("\"\n\"") + "\""},
#     876                 :       1532 :                     {"fee_rate", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"not set, fall back to wallet fee estimation"}, "Specify a fee rate in " + CURRENCY_ATOM + "/vB."},
#     877                 :       1532 :                     {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "If true, return extra infomration about the transaction."},
#     878                 :       1532 :                 },
#     879                 :       1532 :                 {
#     880                 :       1532 :                     RPCResult{"if verbose is not set or set to false",
#     881                 :       1532 :                         RPCResult::Type::STR_HEX, "txid", "The transaction id for the send. Only 1 transaction is created regardless of\n"
#     882                 :       1532 :                 "the number of addresses."
#     883                 :       1532 :                     },
#     884                 :       1532 :                     RPCResult{"if verbose is set to true",
#     885                 :       1532 :                         RPCResult::Type::OBJ, "", "",
#     886                 :       1532 :                         {
#     887                 :       1532 :                             {RPCResult::Type::STR_HEX, "txid", "The transaction id for the send. Only 1 transaction is created regardless of\n"
#     888                 :       1532 :                 "the number of addresses."},
#     889                 :       1532 :                             {RPCResult::Type::STR, "fee reason", "The transaction fee reason."}
#     890                 :       1532 :                         },
#     891                 :       1532 :                     },
#     892                 :       1532 :                 },
#     893                 :       1532 :                 RPCExamples{
#     894                 :       1532 :             "\nSend two amounts to two different addresses:\n"
#     895                 :       1532 :             + HelpExampleCli("sendmany", "\"\" \"{\\\"" + EXAMPLE_ADDRESS[0] + "\\\":0.01,\\\"" + EXAMPLE_ADDRESS[1] + "\\\":0.02}\"") +
#     896                 :       1532 :             "\nSend two amounts to two different addresses setting the confirmation and comment:\n"
#     897                 :       1532 :             + HelpExampleCli("sendmany", "\"\" \"{\\\"" + EXAMPLE_ADDRESS[0] + "\\\":0.01,\\\"" + EXAMPLE_ADDRESS[1] + "\\\":0.02}\" 6 \"testing\"") +
#     898                 :       1532 :             "\nSend two amounts to two different addresses, subtract fee from amount:\n"
#     899                 :       1532 :             + HelpExampleCli("sendmany", "\"\" \"{\\\"" + EXAMPLE_ADDRESS[0] + "\\\":0.01,\\\"" + EXAMPLE_ADDRESS[1] + "\\\":0.02}\" 1 \"\" \"[\\\"" + EXAMPLE_ADDRESS[0] + "\\\",\\\"" + EXAMPLE_ADDRESS[1] + "\\\"]\"") +
#     900                 :       1532 :             "\nAs a JSON-RPC call\n"
#     901                 :       1532 :             + HelpExampleRpc("sendmany", "\"\", {\"" + EXAMPLE_ADDRESS[0] + "\":0.01,\"" + EXAMPLE_ADDRESS[1] + "\":0.02}, 6, \"testing\"")
#     902                 :       1532 :                 },
#     903                 :       1532 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     904                 :       1532 : {
#     905                 :        214 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     906         [ -  + ]:        214 :     if (!pwallet) return NullUniValue;
#     907                 :            : 
#     908                 :            :     // Make sure the results are valid at least up to the most recent block
#     909                 :            :     // the user could have gotten from another RPC command prior to now
#     910                 :        214 :     pwallet->BlockUntilSyncedToCurrentChain();
#     911                 :            : 
#     912                 :        214 :     LOCK(pwallet->cs_wallet);
#     913                 :            : 
#     914 [ +  + ][ -  + ]:        214 :     if (!request.params[0].isNull() && !request.params[0].get_str().empty()) {
#     915                 :          0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Dummy value must be set to \"\"");
#     916                 :          0 :     }
#     917                 :        214 :     UniValue sendTo = request.params[1].get_obj();
#     918                 :            : 
#     919                 :        214 :     mapValue_t mapValue;
#     920 [ +  + ][ -  + ]:        214 :     if (!request.params[3].isNull() && !request.params[3].get_str().empty())
#     921                 :          0 :         mapValue["comment"] = request.params[3].get_str();
#     922                 :            : 
#     923                 :        214 :     UniValue subtractFeeFromAmount(UniValue::VARR);
#     924         [ +  + ]:        214 :     if (!request.params[4].isNull())
#     925                 :          6 :         subtractFeeFromAmount = request.params[4].get_array();
#     926                 :            : 
#     927                 :        214 :     CCoinControl coin_control;
#     928         [ -  + ]:        214 :     if (!request.params[5].isNull()) {
#     929                 :          0 :         coin_control.m_signal_bip125_rbf = request.params[5].get_bool();
#     930                 :          0 :     }
#     931                 :            : 
#     932                 :        214 :     SetFeeEstimateMode(*pwallet, coin_control, /* conf_target */ request.params[6], /* estimate_mode */ request.params[7], /* fee_rate */ request.params[8], /* override_min_fee */ false);
#     933                 :            : 
#     934                 :        214 :     std::vector<CRecipient> recipients;
#     935                 :        214 :     ParseRecipients(sendTo, subtractFeeFromAmount, recipients);
#     936         [ +  + ]:        214 :     const bool verbose{request.params[9].isNull() ? false : request.params[9].get_bool()};
#     937                 :            : 
#     938                 :        214 :     return SendMoney(*pwallet, coin_control, recipients, std::move(mapValue), verbose);
#     939                 :        214 : },
#     940                 :       1532 :     };
#     941                 :       1532 : }
#     942                 :            : 
#     943                 :            : 
#     944                 :            : static RPCHelpMan addmultisigaddress()
#     945                 :       1434 : {
#     946                 :       1434 :     return RPCHelpMan{"addmultisigaddress",
#     947                 :       1434 :                 "\nAdd an nrequired-to-sign multisignature address to the wallet. Requires a new wallet backup.\n"
#     948                 :       1434 :                 "Each key is a Bitcoin address or hex-encoded public key.\n"
#     949                 :       1434 :                 "This functionality is only intended for use with non-watchonly addresses.\n"
#     950                 :       1434 :                 "See `importaddress` for watchonly p2sh address support.\n"
#     951                 :       1434 :                 "If 'label' is specified, assign address to that label.\n",
#     952                 :       1434 :                 {
#     953                 :       1434 :                     {"nrequired", RPCArg::Type::NUM, RPCArg::Optional::NO, "The number of required signatures out of the n keys or addresses."},
#     954                 :       1434 :                     {"keys", RPCArg::Type::ARR, RPCArg::Optional::NO, "The bitcoin addresses or hex-encoded public keys",
#     955                 :       1434 :                         {
#     956                 :       1434 :                             {"key", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "bitcoin address or hex-encoded public key"},
#     957                 :       1434 :                         },
#     958                 :       1434 :                         },
#     959                 :       1434 :                     {"label", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "A label to assign the addresses to."},
#     960                 :       1434 :                     {"address_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -addresstype"}, "The address type to use. Options are \"legacy\", \"p2sh-segwit\", and \"bech32\"."},
#     961                 :       1434 :                 },
#     962                 :       1434 :                 RPCResult{
#     963                 :       1434 :                     RPCResult::Type::OBJ, "", "",
#     964                 :       1434 :                     {
#     965                 :       1434 :                         {RPCResult::Type::STR, "address", "The value of the new multisig address"},
#     966                 :       1434 :                         {RPCResult::Type::STR_HEX, "redeemScript", "The string value of the hex-encoded redemption script"},
#     967                 :       1434 :                         {RPCResult::Type::STR, "descriptor", "The descriptor for this multisig"},
#     968                 :       1434 :                     }
#     969                 :       1434 :                 },
#     970                 :       1434 :                 RPCExamples{
#     971                 :       1434 :             "\nAdd a multisig address from 2 addresses\n"
#     972                 :       1434 :             + HelpExampleCli("addmultisigaddress", "2 \"[\\\"" + EXAMPLE_ADDRESS[0] + "\\\",\\\"" + EXAMPLE_ADDRESS[1] + "\\\"]\"") +
#     973                 :       1434 :             "\nAs a JSON-RPC call\n"
#     974                 :       1434 :             + HelpExampleRpc("addmultisigaddress", "2, \"[\\\"" + EXAMPLE_ADDRESS[0] + "\\\",\\\"" + EXAMPLE_ADDRESS[1] + "\\\"]\"")
#     975                 :       1434 :                 },
#     976                 :       1434 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     977                 :       1434 : {
#     978                 :        116 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     979         [ -  + ]:        116 :     if (!pwallet) return NullUniValue;
#     980                 :            : 
#     981                 :        116 :     LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet);
#     982                 :            : 
#     983                 :        116 :     LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
#     984                 :            : 
#     985                 :        116 :     std::string label;
#     986         [ +  + ]:        116 :     if (!request.params[2].isNull())
#     987                 :         33 :         label = LabelFromValue(request.params[2]);
#     988                 :            : 
#     989                 :        116 :     int required = request.params[0].get_int();
#     990                 :            : 
#     991                 :            :     // Get the public keys
#     992                 :        116 :     const UniValue& keys_or_addrs = request.params[1].get_array();
#     993                 :        116 :     std::vector<CPubKey> pubkeys;
#     994         [ +  + ]:        421 :     for (unsigned int i = 0; i < keys_or_addrs.size(); ++i) {
#     995 [ +  + ][ +  + ]:        305 :         if (IsHex(keys_or_addrs[i].get_str()) && (keys_or_addrs[i].get_str().length() == 66 || keys_or_addrs[i].get_str().length() == 130)) {
#                 [ +  - ]
#     996                 :        165 :             pubkeys.push_back(HexToPubKey(keys_or_addrs[i].get_str()));
#     997                 :        165 :         } else {
#     998                 :        140 :             pubkeys.push_back(AddrToPubKey(spk_man, keys_or_addrs[i].get_str()));
#     999                 :        140 :         }
#    1000                 :        305 :     }
#    1001                 :            : 
#    1002                 :        116 :     OutputType output_type = pwallet->m_default_address_type;
#    1003         [ +  + ]:        116 :     if (!request.params[3].isNull()) {
#    1004         [ +  + ]:         41 :         if (!ParseOutputType(request.params[3].get_str(), output_type)) {
#    1005                 :          1 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Unknown address type '%s'", request.params[3].get_str()));
#    1006                 :          1 :         }
#    1007                 :        115 :     }
#    1008                 :            : 
#    1009                 :            :     // Construct using pay-to-script-hash:
#    1010                 :        115 :     CScript inner;
#    1011                 :        115 :     CTxDestination dest = AddAndGetMultisigDestination(required, pubkeys, output_type, spk_man, inner);
#    1012                 :        115 :     pwallet->SetAddressBook(dest, label, "send");
#    1013                 :            : 
#    1014                 :            :     // Make the descriptor
#    1015                 :        115 :     std::unique_ptr<Descriptor> descriptor = InferDescriptor(GetScriptForDestination(dest), spk_man);
#    1016                 :            : 
#    1017                 :        115 :     UniValue result(UniValue::VOBJ);
#    1018                 :        115 :     result.pushKV("address", EncodeDestination(dest));
#    1019                 :        115 :     result.pushKV("redeemScript", HexStr(inner));
#    1020                 :        115 :     result.pushKV("descriptor", descriptor->ToString());
#    1021                 :        115 :     return result;
#    1022                 :        115 : },
#    1023                 :       1434 :     };
#    1024                 :       1434 : }
#    1025                 :            : 
#    1026                 :            : struct tallyitem
#    1027                 :            : {
#    1028                 :            :     CAmount nAmount{0};
#    1029                 :            :     int nConf{std::numeric_limits<int>::max()};
#    1030                 :            :     std::vector<uint256> txids;
#    1031                 :            :     bool fIsWatchonly{false};
#    1032                 :            :     tallyitem()
#    1033                 :        278 :     {
#    1034                 :        278 :     }
#    1035                 :            : };
#    1036                 :            : 
#    1037                 :            : static UniValue ListReceived(const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
#    1038                 :        368 : {
#    1039                 :            :     // Minimum confirmations
#    1040                 :        368 :     int nMinDepth = 1;
#    1041         [ +  + ]:        368 :     if (!params[0].isNull())
#    1042                 :        350 :         nMinDepth = params[0].get_int();
#    1043                 :            : 
#    1044                 :            :     // Whether to include empty labels
#    1045                 :        368 :     bool fIncludeEmpty = false;
#    1046         [ +  + ]:        368 :     if (!params[1].isNull())
#    1047                 :         22 :         fIncludeEmpty = params[1].get_bool();
#    1048                 :            : 
#    1049                 :        368 :     isminefilter filter = ISMINE_SPENDABLE;
#    1050                 :            : 
#    1051         [ +  + ]:        368 :     if (ParseIncludeWatchonly(params[2], wallet)) {
#    1052                 :        346 :         filter |= ISMINE_WATCH_ONLY;
#    1053                 :        346 :     }
#    1054                 :            : 
#    1055                 :        368 :     bool has_filtered_address = false;
#    1056                 :        368 :     CTxDestination filtered_address = CNoDestination();
#    1057 [ +  + ][ +  + ]:        368 :     if (!by_label && params.size() > 3) {
#    1058         [ +  + ]:        336 :         if (!IsValidDestinationString(params[3].get_str())) {
#    1059                 :          2 :             throw JSONRPCError(RPC_WALLET_ERROR, "address_filter parameter was invalid");
#    1060                 :          2 :         }
#    1061                 :        334 :         filtered_address = DecodeDestination(params[3].get_str());
#    1062                 :        334 :         has_filtered_address = true;
#    1063                 :        334 :     }
#    1064                 :            : 
#    1065                 :            :     // Tally
#    1066                 :        368 :     std::map<CTxDestination, tallyitem> mapTally;
#    1067         [ +  + ]:      17094 :     for (const std::pair<const uint256, CWalletTx>& pairWtx : wallet.mapWallet) {
#    1068                 :      17094 :         const CWalletTx& wtx = pairWtx.second;
#    1069                 :            : 
#    1070 [ +  + ][ -  + ]:      17094 :         if (wtx.IsCoinBase() || !wallet.chain().checkFinalTx(*wtx.tx)) {
#    1071                 :       7520 :             continue;
#    1072                 :       7520 :         }
#    1073                 :            : 
#    1074                 :       9574 :         int nDepth = wtx.GetDepthInMainChain();
#    1075         [ +  + ]:       9574 :         if (nDepth < nMinDepth)
#    1076                 :          6 :             continue;
#    1077                 :            : 
#    1078         [ +  + ]:       9568 :         for (const CTxOut& txout : wtx.tx->vout)
#    1079                 :      19136 :         {
#    1080                 :      19136 :             CTxDestination address;
#    1081         [ -  + ]:      19136 :             if (!ExtractDestination(txout.scriptPubKey, address))
#    1082                 :          0 :                 continue;
#    1083                 :            : 
#    1084 [ +  + ][ +  + ]:      19136 :             if (has_filtered_address && !(filtered_address == address)) {
#    1085                 :      18762 :                 continue;
#    1086                 :      18762 :             }
#    1087                 :            : 
#    1088                 :        374 :             isminefilter mine = wallet.IsMine(address);
#    1089         [ +  + ]:        374 :             if(!(mine & filter))
#    1090                 :         52 :                 continue;
#    1091                 :            : 
#    1092                 :        322 :             tallyitem& item = mapTally[address];
#    1093                 :        322 :             item.nAmount += txout.nValue;
#    1094                 :        322 :             item.nConf = std::min(item.nConf, nDepth);
#    1095                 :        322 :             item.txids.push_back(wtx.GetHash());
#    1096         [ +  + ]:        322 :             if (mine & ISMINE_WATCH_ONLY)
#    1097                 :        184 :                 item.fIsWatchonly = true;
#    1098                 :        322 :         }
#    1099                 :       9568 :     }
#    1100                 :            : 
#    1101                 :            :     // Reply
#    1102                 :        366 :     UniValue ret(UniValue::VARR);
#    1103                 :        366 :     std::map<std::string, tallyitem> label_tally;
#    1104                 :            : 
#    1105                 :            :     // Create m_address_book iterator
#    1106                 :            :     // If we aren't filtering, go from begin() to end()
#    1107                 :        366 :     auto start = wallet.m_address_book.begin();
#    1108                 :        366 :     auto end = wallet.m_address_book.end();
#    1109                 :            :     // If we are filtering, find() the applicable entry
#    1110         [ +  + ]:        366 :     if (has_filtered_address) {
#    1111                 :        334 :         start = wallet.m_address_book.find(filtered_address);
#    1112         [ +  + ]:        334 :         if (start != end) {
#    1113                 :        332 :             end = std::next(start);
#    1114                 :        332 :         }
#    1115                 :        334 :     }
#    1116                 :            : 
#    1117         [ +  + ]:        858 :     for (auto item_it = start; item_it != end; ++item_it)
#    1118                 :        492 :     {
#    1119         [ -  + ]:        492 :         if (item_it->second.IsChange()) continue;
#    1120                 :        492 :         const CTxDestination& address = item_it->first;
#    1121                 :        492 :         const std::string& label = item_it->second.GetLabel();
#    1122                 :        492 :         auto it = mapTally.find(address);
#    1123 [ +  + ][ +  + ]:        492 :         if (it == mapTally.end() && !fIncludeEmpty)
#                 [ +  + ]
#    1124                 :        196 :             continue;
#    1125                 :            : 
#    1126                 :        296 :         CAmount nAmount = 0;
#    1127                 :        296 :         int nConf = std::numeric_limits<int>::max();
#    1128                 :        296 :         bool fIsWatchonly = false;
#    1129         [ +  + ]:        296 :         if (it != mapTally.end())
#    1130                 :        264 :         {
#    1131                 :        264 :             nAmount = (*it).second.nAmount;
#    1132                 :        264 :             nConf = (*it).second.nConf;
#    1133                 :        264 :             fIsWatchonly = (*it).second.fIsWatchonly;
#    1134                 :        264 :         }
#    1135                 :            : 
#    1136         [ +  + ]:        296 :         if (by_label)
#    1137                 :         36 :         {
#    1138                 :         36 :             tallyitem& _item = label_tally[label];
#    1139                 :         36 :             _item.nAmount += nAmount;
#    1140                 :         36 :             _item.nConf = std::min(_item.nConf, nConf);
#    1141                 :         36 :             _item.fIsWatchonly = fIsWatchonly;
#    1142                 :         36 :         }
#    1143                 :        260 :         else
#    1144                 :        260 :         {
#    1145                 :        260 :             UniValue obj(UniValue::VOBJ);
#    1146         [ +  + ]:        260 :             if(fIsWatchonly)
#    1147                 :        146 :                 obj.pushKV("involvesWatchonly", true);
#    1148                 :        260 :             obj.pushKV("address",       EncodeDestination(address));
#    1149                 :        260 :             obj.pushKV("amount",        ValueFromAmount(nAmount));
#    1150         [ +  + ]:        260 :             obj.pushKV("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf));
#    1151                 :        260 :             obj.pushKV("label", label);
#    1152                 :        260 :             UniValue transactions(UniValue::VARR);
#    1153         [ +  + ]:        260 :             if (it != mapTally.end())
#    1154                 :        238 :             {
#    1155         [ +  + ]:        238 :                 for (const uint256& _item : (*it).second.txids)
#    1156                 :        292 :                 {
#    1157                 :        292 :                     transactions.push_back(_item.GetHex());
#    1158                 :        292 :                 }
#    1159                 :        238 :             }
#    1160                 :        260 :             obj.pushKV("txids", transactions);
#    1161                 :        260 :             ret.push_back(obj);
#    1162                 :        260 :         }
#    1163                 :        296 :     }
#    1164                 :            : 
#    1165         [ +  + ]:        366 :     if (by_label)
#    1166                 :         12 :     {
#    1167         [ +  + ]:         12 :         for (const auto& entry : label_tally)
#    1168                 :         14 :         {
#    1169                 :         14 :             CAmount nAmount = entry.second.nAmount;
#    1170                 :         14 :             int nConf = entry.second.nConf;
#    1171                 :         14 :             UniValue obj(UniValue::VOBJ);
#    1172         [ +  + ]:         14 :             if (entry.second.fIsWatchonly)
#    1173                 :          2 :                 obj.pushKV("involvesWatchonly", true);
#    1174                 :         14 :             obj.pushKV("amount",        ValueFromAmount(nAmount));
#    1175         [ +  + ]:         14 :             obj.pushKV("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf));
#    1176                 :         14 :             obj.pushKV("label",         entry.first);
#    1177                 :         14 :             ret.push_back(obj);
#    1178                 :         14 :         }
#    1179                 :         12 :     }
#    1180                 :            : 
#    1181                 :        366 :     return ret;
#    1182                 :        368 : }
#    1183                 :            : 
#    1184                 :            : static RPCHelpMan listreceivedbyaddress()
#    1185                 :       1674 : {
#    1186                 :       1674 :     return RPCHelpMan{"listreceivedbyaddress",
#    1187                 :       1674 :                 "\nList balances by receiving address.\n",
#    1188                 :       1674 :                 {
#    1189                 :       1674 :                     {"minconf", RPCArg::Type::NUM, RPCArg::Default{1}, "The minimum number of confirmations before payments are included."},
#    1190                 :       1674 :                     {"include_empty", RPCArg::Type::BOOL, RPCArg::Default{false}, "Whether to include addresses that haven't received any payments."},
#    1191                 :       1674 :                     {"include_watchonly", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Whether to include watch-only addresses (see 'importaddress')"},
#    1192                 :       1674 :                     {"address_filter", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "If present, only return information on this address."},
#    1193                 :       1674 :                 },
#    1194                 :       1674 :                 RPCResult{
#    1195                 :       1674 :                     RPCResult::Type::ARR, "", "",
#    1196                 :       1674 :                     {
#    1197                 :       1674 :                         {RPCResult::Type::OBJ, "", "",
#    1198                 :       1674 :                         {
#    1199                 :       1674 :                             {RPCResult::Type::BOOL, "involvesWatchonly", "Only returns true if imported addresses were involved in transaction"},
#    1200                 :       1674 :                             {RPCResult::Type::STR, "address", "The receiving address"},
#    1201                 :       1674 :                             {RPCResult::Type::STR_AMOUNT, "amount", "The total amount in " + CURRENCY_UNIT + " received by the address"},
#    1202                 :       1674 :                             {RPCResult::Type::NUM, "confirmations", "The number of confirmations of the most recent transaction included"},
#    1203                 :       1674 :                             {RPCResult::Type::STR, "label", "The label of the receiving address. The default label is \"\""},
#    1204                 :       1674 :                             {RPCResult::Type::ARR, "txids", "",
#    1205                 :       1674 :                             {
#    1206                 :       1674 :                                 {RPCResult::Type::STR_HEX, "txid", "The ids of transactions received with the address"},
#    1207                 :       1674 :                             }},
#    1208                 :       1674 :                         }},
#    1209                 :       1674 :                     }
#    1210                 :       1674 :                 },
#    1211                 :       1674 :                 RPCExamples{
#    1212                 :       1674 :                     HelpExampleCli("listreceivedbyaddress", "")
#    1213                 :       1674 :             + HelpExampleCli("listreceivedbyaddress", "6 true")
#    1214                 :       1674 :             + HelpExampleRpc("listreceivedbyaddress", "6, true, true")
#    1215                 :       1674 :             + HelpExampleRpc("listreceivedbyaddress", "6, true, true, \"" + EXAMPLE_ADDRESS[0] + "\"")
#    1216                 :       1674 :                 },
#    1217                 :       1674 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    1218                 :       1674 : {
#    1219                 :        356 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    1220         [ -  + ]:        356 :     if (!pwallet) return NullUniValue;
#    1221                 :            : 
#    1222                 :            :     // Make sure the results are valid at least up to the most recent block
#    1223                 :            :     // the user could have gotten from another RPC command prior to now
#    1224                 :        356 :     pwallet->BlockUntilSyncedToCurrentChain();
#    1225                 :            : 
#    1226                 :        356 :     LOCK(pwallet->cs_wallet);
#    1227                 :            : 
#    1228                 :        356 :     return ListReceived(*pwallet, request.params, false);
#    1229                 :        356 : },
#    1230                 :       1674 :     };
#    1231                 :       1674 : }
#    1232                 :            : 
#    1233                 :            : static RPCHelpMan listreceivedbylabel()
#    1234                 :       1330 : {
#    1235                 :       1330 :     return RPCHelpMan{"listreceivedbylabel",
#    1236                 :       1330 :                 "\nList received transactions by label.\n",
#    1237                 :       1330 :                 {
#    1238                 :       1330 :                     {"minconf", RPCArg::Type::NUM, RPCArg::Default{1}, "The minimum number of confirmations before payments are included."},
#    1239                 :       1330 :                     {"include_empty", RPCArg::Type::BOOL, RPCArg::Default{false}, "Whether to include labels that haven't received any payments."},
#    1240                 :       1330 :                     {"include_watchonly", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Whether to include watch-only addresses (see 'importaddress')"},
#    1241                 :       1330 :                 },
#    1242                 :       1330 :                 RPCResult{
#    1243                 :       1330 :                     RPCResult::Type::ARR, "", "",
#    1244                 :       1330 :                     {
#    1245                 :       1330 :                         {RPCResult::Type::OBJ, "", "",
#    1246                 :       1330 :                         {
#    1247                 :       1330 :                             {RPCResult::Type::BOOL, "involvesWatchonly", "Only returns true if imported addresses were involved in transaction"},
#    1248                 :       1330 :                             {RPCResult::Type::STR_AMOUNT, "amount", "The total amount received by addresses with this label"},
#    1249                 :       1330 :                             {RPCResult::Type::NUM, "confirmations", "The number of confirmations of the most recent transaction included"},
#    1250                 :       1330 :                             {RPCResult::Type::STR, "label", "The label of the receiving address. The default label is \"\""},
#    1251                 :       1330 :                         }},
#    1252                 :       1330 :                     }
#    1253                 :       1330 :                 },
#    1254                 :       1330 :                 RPCExamples{
#    1255                 :       1330 :                     HelpExampleCli("listreceivedbylabel", "")
#    1256                 :       1330 :             + HelpExampleCli("listreceivedbylabel", "6 true")
#    1257                 :       1330 :             + HelpExampleRpc("listreceivedbylabel", "6, true, true")
#    1258                 :       1330 :                 },
#    1259                 :       1330 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    1260                 :       1330 : {
#    1261                 :         12 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    1262         [ -  + ]:         12 :     if (!pwallet) return NullUniValue;
#    1263                 :            : 
#    1264                 :            :     // Make sure the results are valid at least up to the most recent block
#    1265                 :            :     // the user could have gotten from another RPC command prior to now
#    1266                 :         12 :     pwallet->BlockUntilSyncedToCurrentChain();
#    1267                 :            : 
#    1268                 :         12 :     LOCK(pwallet->cs_wallet);
#    1269                 :            : 
#    1270                 :         12 :     return ListReceived(*pwallet, request.params, true);
#    1271                 :         12 : },
#    1272                 :       1330 :     };
#    1273                 :       1330 : }
#    1274                 :            : 
#    1275                 :            : static void MaybePushAddress(UniValue & entry, const CTxDestination &dest)
#    1276                 :       3301 : {
#    1277         [ +  + ]:       3301 :     if (IsValidDestination(dest)) {
#    1278                 :       3299 :         entry.pushKV("address", EncodeDestination(dest));
#    1279                 :       3299 :     }
#    1280                 :       3301 : }
#    1281                 :            : 
#    1282                 :            : /**
#    1283                 :            :  * List transactions based on the given criteria.
#    1284                 :            :  *
#    1285                 :            :  * @param  wallet         The wallet.
#    1286                 :            :  * @param  wtx            The wallet transaction.
#    1287                 :            :  * @param  nMinDepth      The minimum confirmation depth.
#    1288                 :            :  * @param  fLong          Whether to include the JSON version of the transaction.
#    1289                 :            :  * @param  ret            The UniValue into which the result is stored.
#    1290                 :            :  * @param  filter_ismine  The "is mine" filter flags.
#    1291                 :            :  * @param  filter_label   Optional label string to filter incoming transactions.
#    1292                 :            :  */
#    1293                 :            : static void ListTransactions(const CWallet& wallet, const CWalletTx& wtx, int nMinDepth, bool fLong, UniValue& ret, const isminefilter& filter_ismine, const std::string* filter_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
#    1294                 :      17877 : {
#    1295                 :      17877 :     CAmount nFee;
#    1296                 :      17877 :     std::list<COutputEntry> listReceived;
#    1297                 :      17877 :     std::list<COutputEntry> listSent;
#    1298                 :            : 
#    1299                 :      17877 :     wtx.GetAmounts(listReceived, listSent, nFee, filter_ismine);
#    1300                 :            : 
#    1301                 :      17877 :     bool involvesWatchonly = wtx.IsFromMe(ISMINE_WATCH_ONLY);
#    1302                 :            : 
#    1303                 :            :     // Sent
#    1304         [ +  + ]:      17877 :     if (!filter_label)
#    1305                 :       2743 :     {
#    1306         [ +  + ]:       2743 :         for (const COutputEntry& s : listSent)
#    1307                 :        565 :         {
#    1308                 :        565 :             UniValue entry(UniValue::VOBJ);
#    1309 [ +  + ][ +  + ]:        565 :             if (involvesWatchonly || (wallet.IsMine(s.destination) & ISMINE_WATCH_ONLY)) {
#    1310                 :          9 :                 entry.pushKV("involvesWatchonly", true);
#    1311                 :          9 :             }
#    1312                 :        565 :             MaybePushAddress(entry, s.destination);
#    1313                 :        565 :             entry.pushKV("category", "send");
#    1314                 :        565 :             entry.pushKV("amount", ValueFromAmount(-s.amount));
#    1315                 :        565 :             const auto* address_book_entry = wallet.FindAddressBookEntry(s.destination);
#    1316         [ +  + ]:        565 :             if (address_book_entry) {
#    1317                 :        206 :                 entry.pushKV("label", address_book_entry->GetLabel());
#    1318                 :        206 :             }
#    1319                 :        565 :             entry.pushKV("vout", s.vout);
#    1320                 :        565 :             entry.pushKV("fee", ValueFromAmount(-nFee));
#    1321         [ +  + ]:        565 :             if (fLong)
#    1322                 :        301 :                 WalletTxToJSON(wallet.chain(), wtx, entry);
#    1323                 :        565 :             entry.pushKV("abandoned", wtx.isAbandoned());
#    1324                 :        565 :             ret.push_back(entry);
#    1325                 :        565 :         }
#    1326                 :       2743 :     }
#    1327                 :            : 
#    1328                 :            :     // Received
#    1329 [ +  + ][ +  + ]:      17877 :     if (listReceived.size() > 0 && wtx.GetDepthInMainChain() >= nMinDepth) {
#    1330         [ +  + ]:      17549 :         for (const COutputEntry& r : listReceived)
#    1331                 :      17594 :         {
#    1332                 :      17594 :             std::string label;
#    1333                 :      17594 :             const auto* address_book_entry = wallet.FindAddressBookEntry(r.destination);
#    1334         [ +  + ]:      17594 :             if (address_book_entry) {
#    1335                 :      16619 :                 label = address_book_entry->GetLabel();
#    1336                 :      16619 :             }
#    1337 [ +  + ][ +  + ]:      17594 :             if (filter_label && label != *filter_label) {
#    1338                 :      14858 :                 continue;
#    1339                 :      14858 :             }
#    1340                 :       2736 :             UniValue entry(UniValue::VOBJ);
#    1341 [ -  + ][ +  + ]:       2736 :             if (involvesWatchonly || (wallet.IsMine(r.destination) & ISMINE_WATCH_ONLY)) {
#    1342                 :        200 :                 entry.pushKV("involvesWatchonly", true);
#    1343                 :        200 :             }
#    1344                 :       2736 :             MaybePushAddress(entry, r.destination);
#    1345         [ +  + ]:       2736 :             if (wtx.IsCoinBase())
#    1346                 :       2070 :             {
#    1347         [ +  + ]:       2070 :                 if (wtx.GetDepthInMainChain() < 1)
#    1348                 :        406 :                     entry.pushKV("category", "orphan");
#    1349         [ +  + ]:       1664 :                 else if (wtx.IsImmatureCoinBase())
#    1350                 :       1238 :                     entry.pushKV("category", "immature");
#    1351                 :        426 :                 else
#    1352                 :        426 :                     entry.pushKV("category", "generate");
#    1353                 :       2070 :             }
#    1354                 :        666 :             else
#    1355                 :        666 :             {
#    1356                 :        666 :                 entry.pushKV("category", "receive");
#    1357                 :        666 :             }
#    1358                 :       2736 :             entry.pushKV("amount", ValueFromAmount(r.amount));
#    1359         [ +  + ]:       2736 :             if (address_book_entry) {
#    1360                 :       1761 :                 entry.pushKV("label", label);
#    1361                 :       1761 :             }
#    1362                 :       2736 :             entry.pushKV("vout", r.vout);
#    1363         [ +  + ]:       2736 :             if (fLong)
#    1364                 :       2585 :                 WalletTxToJSON(wallet.chain(), wtx, entry);
#    1365                 :       2736 :             ret.push_back(entry);
#    1366                 :       2736 :         }
#    1367                 :      17549 :     }
#    1368                 :      17877 : }
#    1369                 :            : 
#    1370                 :            : static const std::vector<RPCResult> TransactionDescriptionString()
#    1371                 :       4726 : {
#    1372                 :       4726 :     return{{RPCResult::Type::NUM, "confirmations", "The number of confirmations for the transaction. Negative confirmations means the\n"
#    1373                 :       4726 :                "transaction conflicted that many blocks ago."},
#    1374                 :       4726 :            {RPCResult::Type::BOOL, "generated", "Only present if transaction only input is a coinbase one."},
#    1375                 :       4726 :            {RPCResult::Type::BOOL, "trusted", "Only present if we consider transaction to be trusted and so safe to spend from."},
#    1376                 :       4726 :            {RPCResult::Type::STR_HEX, "blockhash", "The block hash containing the transaction."},
#    1377                 :       4726 :            {RPCResult::Type::NUM, "blockheight", "The block height containing the transaction."},
#    1378                 :       4726 :            {RPCResult::Type::NUM, "blockindex", "The index of the transaction in the block that includes it."},
#    1379                 :       4726 :            {RPCResult::Type::NUM_TIME, "blocktime", "The block time expressed in " + UNIX_EPOCH_TIME + "."},
#    1380                 :       4726 :            {RPCResult::Type::STR_HEX, "txid", "The transaction id."},
#    1381                 :       4726 :            {RPCResult::Type::ARR, "walletconflicts", "Conflicting transaction ids.",
#    1382                 :       4726 :            {
#    1383                 :       4726 :                {RPCResult::Type::STR_HEX, "txid", "The transaction id."},
#    1384                 :       4726 :            }},
#    1385                 :       4726 :            {RPCResult::Type::NUM_TIME, "time", "The transaction time expressed in " + UNIX_EPOCH_TIME + "."},
#    1386                 :       4726 :            {RPCResult::Type::NUM_TIME, "timereceived", "The time received expressed in " + UNIX_EPOCH_TIME + "."},
#    1387                 :       4726 :            {RPCResult::Type::STR, "comment", "If a comment is associated with the transaction, only present if not empty."},
#    1388                 :       4726 :            {RPCResult::Type::STR, "bip125-replaceable", "(\"yes|no|unknown\") Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
#    1389                 :       4726 :                "may be unknown for unconfirmed transactions not in the mempool"}};
#    1390                 :       4726 : }
#    1391                 :            : 
#    1392                 :            : static RPCHelpMan listtransactions()
#    1393                 :       1746 : {
#    1394                 :       1746 :     return RPCHelpMan{"listtransactions",
#    1395                 :       1746 :                 "\nIf a label name is provided, this will return only incoming transactions paying to addresses with the specified label.\n"
#    1396                 :       1746 :                 "\nReturns up to 'count' most recent transactions skipping the first 'from' transactions.\n",
#    1397                 :       1746 :                 {
#    1398                 :       1746 :                     {"label|dummy", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "If set, should be a valid label name to return only incoming transactions\n"
#    1399                 :       1746 :                           "with the specified label, or \"*\" to disable filtering and return all transactions."},
#    1400                 :       1746 :                     {"count", RPCArg::Type::NUM, RPCArg::Default{10}, "The number of transactions to return"},
#    1401                 :       1746 :                     {"skip", RPCArg::Type::NUM, RPCArg::Default{0}, "The number of transactions to skip"},
#    1402                 :       1746 :                     {"include_watchonly", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Include transactions to watch-only addresses (see 'importaddress')"},
#    1403                 :       1746 :                 },
#    1404                 :       1746 :                 RPCResult{
#    1405                 :       1746 :                     RPCResult::Type::ARR, "", "",
#    1406                 :       1746 :                     {
#    1407                 :       1746 :                         {RPCResult::Type::OBJ, "", "", Cat(Cat<std::vector<RPCResult>>(
#    1408                 :       1746 :                         {
#    1409                 :       1746 :                             {RPCResult::Type::BOOL, "involvesWatchonly", "Only returns true if imported addresses were involved in transaction."},
#    1410                 :       1746 :                             {RPCResult::Type::STR, "address", "The bitcoin address of the transaction."},
#    1411                 :       1746 :                             {RPCResult::Type::STR, "category", "The transaction category.\n"
#    1412                 :       1746 :                                 "\"send\"                  Transactions sent.\n"
#    1413                 :       1746 :                                 "\"receive\"               Non-coinbase transactions received.\n"
#    1414                 :       1746 :                                 "\"generate\"              Coinbase transactions received with more than 100 confirmations.\n"
#    1415                 :       1746 :                                 "\"immature\"              Coinbase transactions received with 100 or fewer confirmations.\n"
#    1416                 :       1746 :                                 "\"orphan\"                Orphaned coinbase transactions received."},
#    1417                 :       1746 :                             {RPCResult::Type::STR_AMOUNT, "amount", "The amount in " + CURRENCY_UNIT + ". This is negative for the 'send' category, and is positive\n"
#    1418                 :       1746 :                                 "for all other categories"},
#    1419                 :       1746 :                             {RPCResult::Type::STR, "label", "A comment for the address/transaction, if any"},
#    1420                 :       1746 :                             {RPCResult::Type::NUM, "vout", "the vout value"},
#    1421                 :       1746 :                             {RPCResult::Type::STR_AMOUNT, "fee", "The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the\n"
#    1422                 :       1746 :                                  "'send' category of transactions."},
#    1423                 :       1746 :                         },
#    1424                 :       1746 :                         TransactionDescriptionString()),
#    1425                 :       1746 :                         {
#    1426                 :       1746 :                             {RPCResult::Type::BOOL, "abandoned", "'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
#    1427                 :       1746 :                                  "'send' category of transactions."},
#    1428                 :       1746 :                         })},
#    1429                 :       1746 :                     }
#    1430                 :       1746 :                 },
#    1431                 :       1746 :                 RPCExamples{
#    1432                 :       1746 :             "\nList the most recent 10 transactions in the systems\n"
#    1433                 :       1746 :             + HelpExampleCli("listtransactions", "") +
#    1434                 :       1746 :             "\nList transactions 100 to 120\n"
#    1435                 :       1746 :             + HelpExampleCli("listtransactions", "\"*\" 20 100") +
#    1436                 :       1746 :             "\nAs a JSON-RPC call\n"
#    1437                 :       1746 :             + HelpExampleRpc("listtransactions", "\"*\", 20, 100")
#    1438                 :       1746 :                 },
#    1439                 :       1746 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    1440                 :       1746 : {
#    1441                 :        428 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    1442         [ -  + ]:        428 :     if (!pwallet) return NullUniValue;
#    1443                 :            : 
#    1444                 :            :     // Make sure the results are valid at least up to the most recent block
#    1445                 :            :     // the user could have gotten from another RPC command prior to now
#    1446                 :        428 :     pwallet->BlockUntilSyncedToCurrentChain();
#    1447                 :            : 
#    1448                 :        428 :     const std::string* filter_label = nullptr;
#    1449 [ +  + ][ +  + ]:        428 :     if (!request.params[0].isNull() && request.params[0].get_str() != "*") {
#    1450                 :        330 :         filter_label = &request.params[0].get_str();
#    1451         [ -  + ]:        330 :         if (filter_label->empty()) {
#    1452                 :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Label argument must be a valid label name or \"*\".");
#    1453                 :          0 :         }
#    1454                 :        428 :     }
#    1455                 :        428 :     int nCount = 10;
#    1456         [ +  + ]:        428 :     if (!request.params[1].isNull())
#    1457                 :        340 :         nCount = request.params[1].get_int();
#    1458                 :        428 :     int nFrom = 0;
#    1459         [ +  + ]:        428 :     if (!request.params[2].isNull())
#    1460                 :         12 :         nFrom = request.params[2].get_int();
#    1461                 :        428 :     isminefilter filter = ISMINE_SPENDABLE;
#    1462                 :            : 
#    1463         [ +  + ]:        428 :     if (ParseIncludeWatchonly(request.params[3], *pwallet)) {
#    1464                 :        343 :         filter |= ISMINE_WATCH_ONLY;
#    1465                 :        343 :     }
#    1466                 :            : 
#    1467         [ -  + ]:        428 :     if (nCount < 0)
#    1468                 :          0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative count");
#    1469         [ -  + ]:        428 :     if (nFrom < 0)
#    1470                 :          0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative from");
#    1471                 :            : 
#    1472                 :        428 :     UniValue ret(UniValue::VARR);
#    1473                 :            : 
#    1474                 :        428 :     {
#    1475                 :        428 :         LOCK(pwallet->cs_wallet);
#    1476                 :            : 
#    1477                 :        428 :         const CWallet::TxItems & txOrdered = pwallet->wtxOrdered;
#    1478                 :            : 
#    1479                 :            :         // iterate backwards until we have nCount items to return:
#    1480         [ +  + ]:      16900 :         for (CWallet::TxItems::const_reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
#    1481                 :      16542 :         {
#    1482                 :      16542 :             CWalletTx *const pwtx = (*it).second;
#    1483                 :      16542 :             ListTransactions(*pwallet, *pwtx, 0, true, ret, filter, filter_label);
#    1484         [ +  + ]:      16542 :             if ((int)ret.size() >= (nCount+nFrom)) break;
#    1485                 :      16542 :         }
#    1486                 :        428 :     }
#    1487                 :            : 
#    1488                 :            :     // ret is newest to oldest
#    1489                 :            : 
#    1490         [ -  + ]:        428 :     if (nFrom > (int)ret.size())
#    1491                 :          0 :         nFrom = ret.size();
#    1492         [ +  + ]:        428 :     if ((nFrom + nCount) > (int)ret.size())
#    1493                 :        358 :         nCount = ret.size() - nFrom;
#    1494                 :            : 
#    1495                 :        428 :     const std::vector<UniValue>& txs = ret.getValues();
#    1496                 :        428 :     UniValue result{UniValue::VARR};
#    1497                 :        428 :     result.push_backV({ txs.rend() - nFrom - nCount, txs.rend() - nFrom }); // Return oldest to newest
#    1498                 :        428 :     return result;
#    1499                 :        428 : },
#    1500                 :       1746 :     };
#    1501                 :       1746 : }
#    1502                 :            : 
#    1503                 :            : static RPCHelpMan listsinceblock()
#    1504                 :       1364 : {
#    1505                 :       1364 :     return RPCHelpMan{"listsinceblock",
#    1506                 :       1364 :                 "\nGet all transactions in blocks since block [blockhash], or all transactions if omitted.\n"
#    1507                 :       1364 :                 "If \"blockhash\" is no longer a part of the main chain, transactions from the fork point onward are included.\n"
#    1508                 :       1364 :                 "Additionally, if include_removed is set, transactions affecting the wallet which were removed are returned in the \"removed\" array.\n",
#    1509                 :       1364 :                 {
#    1510                 :       1364 :                     {"blockhash", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "If set, the block hash to list transactions since, otherwise list all transactions."},
#    1511                 :       1364 :                     {"target_confirmations", RPCArg::Type::NUM, RPCArg::Default{1}, "Return the nth block hash from the main chain. e.g. 1 would mean the best block hash. Note: this is not used as a filter, but only affects [lastblock] in the return value"},
#    1512                 :       1364 :                     {"include_watchonly", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Include transactions to watch-only addresses (see 'importaddress')"},
#    1513                 :       1364 :                     {"include_removed", RPCArg::Type::BOOL, RPCArg::Default{true}, "Show transactions that were removed due to a reorg in the \"removed\" array\n"
#    1514                 :       1364 :                                                                        "(not guaranteed to work on pruned nodes)"},
#    1515                 :       1364 :                 },
#    1516                 :       1364 :                 RPCResult{
#    1517                 :       1364 :                     RPCResult::Type::OBJ, "", "",
#    1518                 :       1364 :                     {
#    1519                 :       1364 :                         {RPCResult::Type::ARR, "transactions", "",
#    1520                 :       1364 :                         {
#    1521                 :       1364 :                             {RPCResult::Type::OBJ, "", "", Cat(Cat<std::vector<RPCResult>>(
#    1522                 :       1364 :                             {
#    1523                 :       1364 :                                 {RPCResult::Type::BOOL, "involvesWatchonly", "Only returns true if imported addresses were involved in transaction."},
#    1524                 :       1364 :                                 {RPCResult::Type::STR, "address", "The bitcoin address of the transaction."},
#    1525                 :       1364 :                                 {RPCResult::Type::STR, "category", "The transaction category.\n"
#    1526                 :       1364 :                                     "\"send\"                  Transactions sent.\n"
#    1527                 :       1364 :                                     "\"receive\"               Non-coinbase transactions received.\n"
#    1528                 :       1364 :                                     "\"generate\"              Coinbase transactions received with more than 100 confirmations.\n"
#    1529                 :       1364 :                                     "\"immature\"              Coinbase transactions received with 100 or fewer confirmations.\n"
#    1530                 :       1364 :                                     "\"orphan\"                Orphaned coinbase transactions received."},
#    1531                 :       1364 :                                 {RPCResult::Type::STR_AMOUNT, "amount", "The amount in " + CURRENCY_UNIT + ". This is negative for the 'send' category, and is positive\n"
#    1532                 :       1364 :                                     "for all other categories"},
#    1533                 :       1364 :                                 {RPCResult::Type::NUM, "vout", "the vout value"},
#    1534                 :       1364 :                                 {RPCResult::Type::STR_AMOUNT, "fee", "The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the\n"
#    1535                 :       1364 :                                      "'send' category of transactions."},
#    1536                 :       1364 :                             },
#    1537                 :       1364 :                             TransactionDescriptionString()),
#    1538                 :       1364 :                             {
#    1539                 :       1364 :                                 {RPCResult::Type::BOOL, "abandoned", "'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
#    1540                 :       1364 :                                      "'send' category of transactions."},
#    1541                 :       1364 :                                 {RPCResult::Type::STR, "label", "A comment for the address/transaction, if any"},
#    1542                 :       1364 :                                 {RPCResult::Type::STR, "to", "If a comment to is associated with the transaction."},
#    1543                 :       1364 :                             })},
#    1544                 :       1364 :                         }},
#    1545                 :       1364 :                         {RPCResult::Type::ARR, "removed", "<structure is the same as \"transactions\" above, only present if include_removed=true>\n"
#    1546                 :       1364 :                             "Note: transactions that were re-added in the active chain will appear as-is in this array, and may thus have a positive confirmation count."
#    1547                 :       1364 :                         , {{RPCResult::Type::ELISION, "", ""},}},
#    1548                 :       1364 :                         {RPCResult::Type::STR_HEX, "lastblock", "The hash of the block (target_confirmations-1) from the best block on the main chain, or the genesis hash if the referenced block does not exist yet. This is typically used to feed back into listsinceblock the next time you call it. So you would generally use a target_confirmations of say 6, so you will be continually re-notified of transactions until they've reached 6 confirmations plus any new ones"},
#    1549                 :       1364 :                     }
#    1550                 :       1364 :                 },
#    1551                 :       1364 :                 RPCExamples{
#    1552                 :       1364 :                     HelpExampleCli("listsinceblock", "")
#    1553                 :       1364 :             + HelpExampleCli("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\" 6")
#    1554                 :       1364 :             + HelpExampleRpc("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\", 6")
#    1555                 :       1364 :                 },
#    1556                 :       1364 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    1557                 :       1364 : {
#    1558                 :         46 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    1559         [ -  + ]:         46 :     if (!pwallet) return NullUniValue;
#    1560                 :            : 
#    1561                 :         46 :     const CWallet& wallet = *pwallet;
#    1562                 :            :     // Make sure the results are valid at least up to the most recent block
#    1563                 :            :     // the user could have gotten from another RPC command prior to now
#    1564                 :         46 :     wallet.BlockUntilSyncedToCurrentChain();
#    1565                 :            : 
#    1566                 :         46 :     LOCK(wallet.cs_wallet);
#    1567                 :            : 
#    1568                 :         46 :     std::optional<int> height;    // Height of the specified block or the common ancestor, if the block provided was in a deactivated chain.
#    1569                 :         46 :     std::optional<int> altheight; // Height of the specified block, even if it's in a deactivated chain.
#    1570                 :         46 :     int target_confirms = 1;
#    1571                 :         46 :     isminefilter filter = ISMINE_SPENDABLE;
#    1572                 :            : 
#    1573                 :         46 :     uint256 blockId;
#    1574 [ +  + ][ +  + ]:         46 :     if (!request.params[0].isNull() && !request.params[0].get_str().empty()) {
#    1575                 :         30 :         blockId = ParseHashV(request.params[0], "blockhash");
#    1576                 :         30 :         height = int{};
#    1577                 :         30 :         altheight = int{};
#    1578         [ +  + ]:         30 :         if (!wallet.chain().findCommonAncestor(blockId, wallet.GetLastBlockHash(), /* ancestor out */ FoundBlock().height(*height), /* blockId out */ FoundBlock().height(*altheight))) {
#    1579                 :          4 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
#    1580                 :          4 :         }
#    1581                 :         42 :     }
#    1582                 :            : 
#    1583         [ +  + ]:         42 :     if (!request.params[1].isNull()) {
#    1584                 :          6 :         target_confirms = request.params[1].get_int();
#    1585                 :            : 
#    1586         [ +  + ]:          6 :         if (target_confirms < 1) {
#    1587                 :          2 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter");
#    1588                 :          2 :         }
#    1589                 :         40 :     }
#    1590                 :            : 
#    1591         [ +  + ]:         40 :     if (ParseIncludeWatchonly(request.params[2], wallet)) {
#    1592                 :          2 :         filter |= ISMINE_WATCH_ONLY;
#    1593                 :          2 :     }
#    1594                 :            : 
#    1595 [ +  + ][ -  + ]:         40 :     bool include_removed = (request.params[3].isNull() || request.params[3].get_bool());
#    1596                 :            : 
#    1597         [ +  + ]:         40 :     int depth = height ? wallet.GetLastBlockHeight() + 1 - *height : -1;
#    1598                 :            : 
#    1599                 :         40 :     UniValue transactions(UniValue::VARR);
#    1600                 :            : 
#    1601         [ +  + ]:       1676 :     for (const std::pair<const uint256, CWalletTx>& pairWtx : wallet.mapWallet) {
#    1602                 :       1676 :         const CWalletTx& tx = pairWtx.second;
#    1603                 :            : 
#    1604 [ +  + ][ +  + ]:       1676 :         if (depth == -1 || abs(tx.GetDepthInMainChain()) < depth) {
#    1605                 :       1036 :             ListTransactions(wallet, tx, 0, true, transactions, filter, nullptr /* filter_label */);
#    1606                 :       1036 :         }
#    1607                 :       1676 :     }
#    1608                 :            : 
#    1609                 :            :     // when a reorg'd block is requested, we also list any relevant transactions
#    1610                 :            :     // in the blocks of the chain that was detached
#    1611                 :         40 :     UniValue removed(UniValue::VARR);
#    1612 [ +  + ][ +  + ]:         64 :     while (include_removed && altheight && *altheight > *height) {
#                 [ +  + ]
#    1613                 :         24 :         CBlock block;
#    1614 [ -  + ][ -  + ]:         24 :         if (!wallet.chain().findBlock(blockId, FoundBlock().data(block)) || block.IsNull()) {
#                 [ -  + ]
#    1615                 :          0 :             throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
#    1616                 :          0 :         }
#    1617         [ +  + ]:         28 :         for (const CTransactionRef& tx : block.vtx) {
#    1618                 :         28 :             auto it = wallet.mapWallet.find(tx->GetHash());
#    1619         [ +  + ]:         28 :             if (it != wallet.mapWallet.end()) {
#    1620                 :            :                 // We want all transactions regardless of confirmation count to appear here,
#    1621                 :            :                 // even negative confirmation ones, hence the big negative.
#    1622                 :          4 :                 ListTransactions(wallet, it->second, -100000000, true, removed, filter, nullptr /* filter_label */);
#    1623                 :          4 :             }
#    1624                 :         28 :         }
#    1625                 :         24 :         blockId = block.hashPrevBlock;
#    1626                 :         24 :         --*altheight;
#    1627                 :         24 :     }
#    1628                 :            : 
#    1629                 :         40 :     uint256 lastblock;
#    1630                 :         40 :     target_confirms = std::min(target_confirms, wallet.GetLastBlockHeight() + 1);
#    1631         [ -  + ]:         40 :     CHECK_NONFATAL(wallet.chain().findAncestorByHeight(wallet.GetLastBlockHash(), wallet.GetLastBlockHeight() + 1 - target_confirms, FoundBlock().hash(lastblock)));
#    1632                 :            : 
#    1633                 :         40 :     UniValue ret(UniValue::VOBJ);
#    1634                 :         40 :     ret.pushKV("transactions", transactions);
#    1635         [ +  + ]:         40 :     if (include_removed) ret.pushKV("removed", removed);
#    1636                 :         40 :     ret.pushKV("lastblock", lastblock.GetHex());
#    1637                 :            : 
#    1638                 :         40 :     return ret;
#    1639                 :         40 : },
#    1640                 :       1364 :     };
#    1641                 :       1364 : }
#    1642                 :            : 
#    1643                 :            : static RPCHelpMan gettransaction()
#    1644                 :       1616 : {
#    1645                 :       1616 :     return RPCHelpMan{"gettransaction",
#    1646                 :       1616 :                 "\nGet detailed information about in-wallet transaction <txid>\n",
#    1647                 :       1616 :                 {
#    1648                 :       1616 :                     {"txid", RPCArg::Type::STR, RPCArg::Optional::NO, "The transaction id"},
#    1649                 :       1616 :                     {"include_watchonly", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"},
#    1650                 :       1616 :                             "Whether to include watch-only addresses in balance calculation and details[]"},
#    1651                 :       1616 :                     {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false},
#    1652                 :       1616 :                             "Whether to include a `decoded` field containing the decoded transaction (equivalent to RPC decoderawtransaction)"},
#    1653                 :       1616 :                 },
#    1654                 :       1616 :                 RPCResult{
#    1655                 :       1616 :                     RPCResult::Type::OBJ, "", "", Cat(Cat<std::vector<RPCResult>>(
#    1656                 :       1616 :                     {
#    1657                 :       1616 :                         {RPCResult::Type::STR_AMOUNT, "amount", "The amount in " + CURRENCY_UNIT},
#    1658                 :       1616 :                         {RPCResult::Type::STR_AMOUNT, "fee", "The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the\n"
#    1659                 :       1616 :                                      "'send' category of transactions."},
#    1660                 :       1616 :                     },
#    1661                 :       1616 :                     TransactionDescriptionString()),
#    1662                 :       1616 :                     {
#    1663                 :       1616 :                         {RPCResult::Type::ARR, "details", "",
#    1664                 :       1616 :                         {
#    1665                 :       1616 :                             {RPCResult::Type::OBJ, "", "",
#    1666                 :       1616 :                             {
#    1667                 :       1616 :                                 {RPCResult::Type::BOOL, "involvesWatchonly", "Only returns true if imported addresses were involved in transaction."},
#    1668                 :       1616 :                                 {RPCResult::Type::STR, "address", "The bitcoin address involved in the transaction."},
#    1669                 :       1616 :                                 {RPCResult::Type::STR, "category", "The transaction category.\n"
#    1670                 :       1616 :                                     "\"send\"                  Transactions sent.\n"
#    1671                 :       1616 :                                     "\"receive\"               Non-coinbase transactions received.\n"
#    1672                 :       1616 :                                     "\"generate\"              Coinbase transactions received with more than 100 confirmations.\n"
#    1673                 :       1616 :                                     "\"immature\"              Coinbase transactions received with 100 or fewer confirmations.\n"
#    1674                 :       1616 :                                     "\"orphan\"                Orphaned coinbase transactions received."},
#    1675                 :       1616 :                                 {RPCResult::Type::STR_AMOUNT, "amount", "The amount in " + CURRENCY_UNIT},
#    1676                 :       1616 :                                 {RPCResult::Type::STR, "label", "A comment for the address/transaction, if any"},
#    1677                 :       1616 :                                 {RPCResult::Type::NUM, "vout", "the vout value"},
#    1678                 :       1616 :                                 {RPCResult::Type::STR_AMOUNT, "fee", "The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the \n"
#    1679                 :       1616 :                                     "'send' category of transactions."},
#    1680                 :       1616 :                                 {RPCResult::Type::BOOL, "abandoned", "'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
#    1681                 :       1616 :                                      "'send' category of transactions."},
#    1682                 :       1616 :                             }},
#    1683                 :       1616 :                         }},
#    1684                 :       1616 :                         {RPCResult::Type::STR_HEX, "hex", "Raw data for transaction"},
#    1685                 :       1616 :                         {RPCResult::Type::OBJ, "decoded", "Optional, the decoded transaction (only present when `verbose` is passed)",
#    1686                 :       1616 :                         {
#    1687                 :       1616 :                             {RPCResult::Type::ELISION, "", "Equivalent to the RPC decoderawtransaction method, or the RPC getrawtransaction method when `verbose` is passed."},
#    1688                 :       1616 :                         }},
#    1689                 :       1616 :                     })
#    1690                 :       1616 :                 },
#    1691                 :       1616 :                 RPCExamples{
#    1692                 :       1616 :                     HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
#    1693                 :       1616 :             + HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\" true")
#    1694                 :       1616 :             + HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\" false true")
#    1695                 :       1616 :             + HelpExampleRpc("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
#    1696                 :       1616 :                 },
#    1697                 :       1616 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    1698                 :       1616 : {
#    1699                 :        298 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    1700         [ -  + ]:        298 :     if (!pwallet) return NullUniValue;
#    1701                 :            : 
#    1702                 :            :     // Make sure the results are valid at least up to the most recent block
#    1703                 :            :     // the user could have gotten from another RPC command prior to now
#    1704                 :        298 :     pwallet->BlockUntilSyncedToCurrentChain();
#    1705                 :            : 
#    1706                 :        298 :     LOCK(pwallet->cs_wallet);
#    1707                 :            : 
#    1708                 :        298 :     uint256 hash(ParseHashV(request.params[0], "txid"));
#    1709                 :            : 
#    1710                 :        298 :     isminefilter filter = ISMINE_SPENDABLE;
#    1711                 :            : 
#    1712         [ +  + ]:        298 :     if (ParseIncludeWatchonly(request.params[1], *pwallet)) {
#    1713                 :         10 :         filter |= ISMINE_WATCH_ONLY;
#    1714                 :         10 :     }
#    1715                 :            : 
#    1716         [ +  + ]:        298 :     bool verbose = request.params[2].isNull() ? false : request.params[2].get_bool();
#    1717                 :            : 
#    1718                 :        298 :     UniValue entry(UniValue::VOBJ);
#    1719                 :        298 :     auto it = pwallet->mapWallet.find(hash);
#    1720         [ +  + ]:        298 :     if (it == pwallet->mapWallet.end()) {
#    1721                 :          3 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid or non-wallet transaction id");
#    1722                 :          3 :     }
#    1723                 :        295 :     const CWalletTx& wtx = it->second;
#    1724                 :            : 
#    1725                 :        295 :     CAmount nCredit = wtx.GetCredit(filter);
#    1726                 :        295 :     CAmount nDebit = wtx.GetDebit(filter);
#    1727                 :        295 :     CAmount nNet = nCredit - nDebit;
#    1728         [ +  + ]:        295 :     CAmount nFee = (wtx.IsFromMe(filter) ? wtx.tx->GetValueOut() - nDebit : 0);
#    1729                 :            : 
#    1730                 :        295 :     entry.pushKV("amount", ValueFromAmount(nNet - nFee));
#    1731         [ +  + ]:        295 :     if (wtx.IsFromMe(filter))
#    1732                 :        251 :         entry.pushKV("fee", ValueFromAmount(nFee));
#    1733                 :            : 
#    1734                 :        295 :     WalletTxToJSON(pwallet->chain(), wtx, entry);
#    1735                 :            : 
#    1736                 :        295 :     UniValue details(UniValue::VARR);
#    1737                 :        295 :     ListTransactions(*pwallet, wtx, 0, false, details, filter, nullptr /* filter_label */);
#    1738                 :        295 :     entry.pushKV("details", details);
#    1739                 :            : 
#    1740                 :        295 :     std::string strHex = EncodeHexTx(*wtx.tx, pwallet->chain().rpcSerializationFlags());
#    1741                 :        295 :     entry.pushKV("hex", strHex);
#    1742                 :            : 
#    1743         [ +  + ]:        295 :     if (verbose) {
#    1744                 :          3 :         UniValue decoded(UniValue::VOBJ);
#    1745                 :          3 :         TxToUniv(*wtx.tx, uint256(), pwallet->chain().rpcEnableDeprecated("addresses"), decoded, false);
#    1746                 :          3 :         entry.pushKV("decoded", decoded);
#    1747                 :          3 :     }
#    1748                 :            : 
#    1749                 :        295 :     return entry;
#    1750                 :        295 : },
#    1751                 :       1616 :     };
#    1752                 :       1616 : }
#    1753                 :            : 
#    1754                 :            : static RPCHelpMan abandontransaction()
#    1755                 :       1330 : {
#    1756                 :       1330 :     return RPCHelpMan{"abandontransaction",
#    1757                 :       1330 :                 "\nMark in-wallet transaction <txid> as abandoned\n"
#    1758                 :       1330 :                 "This will mark this transaction and all its in-wallet descendants as abandoned which will allow\n"
#    1759                 :       1330 :                 "for their inputs to be respent.  It can be used to replace \"stuck\" or evicted transactions.\n"
#    1760                 :       1330 :                 "It only works on transactions which are not included in a block and are not currently in the mempool.\n"
#    1761                 :       1330 :                 "It has no effect on transactions which are already abandoned.\n",
#    1762                 :       1330 :                 {
#    1763                 :       1330 :                     {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id"},
#    1764                 :       1330 :                 },
#    1765                 :       1330 :                 RPCResult{RPCResult::Type::NONE, "", ""},
#    1766                 :       1330 :                 RPCExamples{
#    1767                 :       1330 :                     HelpExampleCli("abandontransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
#    1768                 :       1330 :             + HelpExampleRpc("abandontransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
#    1769                 :       1330 :                 },
#    1770                 :       1330 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    1771                 :       1330 : {
#    1772                 :         12 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    1773         [ -  + ]:         12 :     if (!pwallet) return NullUniValue;
#    1774                 :            : 
#    1775                 :            :     // Make sure the results are valid at least up to the most recent block
#    1776                 :            :     // the user could have gotten from another RPC command prior to now
#    1777                 :         12 :     pwallet->BlockUntilSyncedToCurrentChain();
#    1778                 :            : 
#    1779                 :         12 :     LOCK(pwallet->cs_wallet);
#    1780                 :            : 
#    1781                 :         12 :     uint256 hash(ParseHashV(request.params[0], "txid"));
#    1782                 :            : 
#    1783         [ +  + ]:         12 :     if (!pwallet->mapWallet.count(hash)) {
#    1784                 :          2 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid or non-wallet transaction id");
#    1785                 :          2 :     }
#    1786         [ +  + ]:         10 :     if (!pwallet->AbandonTransaction(hash)) {
#    1787                 :          4 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not eligible for abandonment");
#    1788                 :          4 :     }
#    1789                 :            : 
#    1790                 :          6 :     return NullUniValue;
#    1791                 :          6 : },
#    1792                 :       1330 :     };
#    1793                 :       1330 : }
#    1794                 :            : 
#    1795                 :            : 
#    1796                 :            : static RPCHelpMan backupwallet()
#    1797                 :       1361 : {
#    1798                 :       1361 :     return RPCHelpMan{"backupwallet",
#    1799                 :       1361 :                 "\nSafely copies current wallet file to destination, which can be a directory or a path with filename.\n",
#    1800                 :       1361 :                 {
#    1801                 :       1361 :                     {"destination", RPCArg::Type::STR, RPCArg::Optional::NO, "The destination directory or file"},
#    1802                 :       1361 :                 },
#    1803                 :       1361 :                 RPCResult{RPCResult::Type::NONE, "", ""},
#    1804                 :       1361 :                 RPCExamples{
#    1805                 :       1361 :                     HelpExampleCli("backupwallet", "\"backup.dat\"")
#    1806                 :       1361 :             + HelpExampleRpc("backupwallet", "\"backup.dat\"")
#    1807                 :       1361 :                 },
#    1808                 :       1361 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    1809                 :       1361 : {
#    1810                 :         43 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    1811         [ -  + ]:         43 :     if (!pwallet) return NullUniValue;
#    1812                 :            : 
#    1813                 :            :     // Make sure the results are valid at least up to the most recent block
#    1814                 :            :     // the user could have gotten from another RPC command prior to now
#    1815                 :         43 :     pwallet->BlockUntilSyncedToCurrentChain();
#    1816                 :            : 
#    1817                 :         43 :     LOCK(pwallet->cs_wallet);
#    1818                 :            : 
#    1819                 :         43 :     std::string strDest = request.params[0].get_str();
#    1820         [ +  + ]:         43 :     if (!pwallet->BackupWallet(strDest)) {
#    1821                 :          8 :         throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!");
#    1822                 :          8 :     }
#    1823                 :            : 
#    1824                 :         35 :     return NullUniValue;
#    1825                 :         35 : },
#    1826                 :       1361 :     };
#    1827                 :       1361 : }
#    1828                 :            : 
#    1829                 :            : 
#    1830                 :            : static RPCHelpMan keypoolrefill()
#    1831                 :       1334 : {
#    1832                 :       1334 :     return RPCHelpMan{"keypoolrefill",
#    1833                 :       1334 :                 "\nFills the keypool."+
#    1834                 :       1334 :         HELP_REQUIRING_PASSPHRASE,
#    1835                 :       1334 :                 {
#    1836                 :       1334 :                     {"newsize", RPCArg::Type::NUM, RPCArg::Default{100}, "The new keypool size"},
#    1837                 :       1334 :                 },
#    1838                 :       1334 :                 RPCResult{RPCResult::Type::NONE, "", ""},
#    1839                 :       1334 :                 RPCExamples{
#    1840                 :       1334 :                     HelpExampleCli("keypoolrefill", "")
#    1841                 :       1334 :             + HelpExampleRpc("keypoolrefill", "")
#    1842                 :       1334 :                 },
#    1843                 :       1334 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    1844                 :       1334 : {
#    1845                 :         16 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    1846         [ -  + ]:         16 :     if (!pwallet) return NullUniValue;
#    1847                 :            : 
#    1848 [ +  + ][ -  + ]:         16 :     if (pwallet->IsLegacy() && pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
#    1849                 :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Error: Private keys are disabled for this wallet");
#    1850                 :          0 :     }
#    1851                 :            : 
#    1852                 :         16 :     LOCK(pwallet->cs_wallet);
#    1853                 :            : 
#    1854                 :            :     // 0 is interpreted by TopUpKeyPool() as the default keypool size given by -keypool
#    1855                 :         16 :     unsigned int kpSize = 0;
#    1856         [ +  + ]:         16 :     if (!request.params[0].isNull()) {
#    1857         [ -  + ]:         13 :         if (request.params[0].get_int() < 0)
#    1858                 :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected valid size.");
#    1859                 :         13 :         kpSize = (unsigned int)request.params[0].get_int();
#    1860                 :         13 :     }
#    1861                 :            : 
#    1862                 :         16 :     EnsureWalletIsUnlocked(*pwallet);
#    1863                 :         16 :     pwallet->TopUpKeyPool(kpSize);
#    1864                 :            : 
#    1865         [ -  + ]:         16 :     if (pwallet->GetKeyPoolSize() < kpSize) {
#    1866                 :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Error refreshing keypool.");
#    1867                 :          0 :     }
#    1868                 :            : 
#    1869                 :         16 :     return NullUniValue;
#    1870                 :         16 : },
#    1871                 :       1334 :     };
#    1872                 :       1334 : }
#    1873                 :            : 
#    1874                 :            : 
#    1875                 :            : static RPCHelpMan walletpassphrase()
#    1876                 :       1391 : {
#    1877                 :       1391 :     return RPCHelpMan{"walletpassphrase",
#    1878                 :       1391 :                 "\nStores the wallet decryption key in memory for 'timeout' seconds.\n"
#    1879                 :       1391 :                 "This is needed prior to performing transactions related to private keys such as sending bitcoins\n"
#    1880                 :       1391 :             "\nNote:\n"
#    1881                 :       1391 :             "Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
#    1882                 :       1391 :             "time that overrides the old one.\n",
#    1883                 :       1391 :                 {
#    1884                 :       1391 :                     {"passphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The wallet passphrase"},
#    1885                 :       1391 :                     {"timeout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The time to keep the decryption key in seconds; capped at 100000000 (~3 years)."},
#    1886                 :       1391 :                 },
#    1887                 :       1391 :                 RPCResult{RPCResult::Type::NONE, "", ""},
#    1888                 :       1391 :                 RPCExamples{
#    1889                 :       1391 :             "\nUnlock the wallet for 60 seconds\n"
#    1890                 :       1391 :             + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 60") +
#    1891                 :       1391 :             "\nLock the wallet again (before 60 seconds)\n"
#    1892                 :       1391 :             + HelpExampleCli("walletlock", "") +
#    1893                 :       1391 :             "\nAs a JSON-RPC call\n"
#    1894                 :       1391 :             + HelpExampleRpc("walletpassphrase", "\"my pass phrase\", 60")
#    1895                 :       1391 :                 },
#    1896                 :       1391 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    1897                 :       1391 : {
#    1898                 :         73 :     std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
#    1899         [ -  + ]:         73 :     if (!wallet) return NullUniValue;
#    1900                 :         73 :     CWallet* const pwallet = wallet.get();
#    1901                 :            : 
#    1902                 :         73 :     int64_t nSleepTime;
#    1903                 :         73 :     int64_t relock_time;
#    1904                 :            :     // Prevent concurrent calls to walletpassphrase with the same wallet.
#    1905                 :         73 :     LOCK(pwallet->m_unlock_mutex);
#    1906                 :         73 :     {
#    1907                 :         73 :         LOCK(pwallet->cs_wallet);
#    1908                 :            : 
#    1909         [ +  + ]:         73 :         if (!pwallet->IsCrypted()) {
#    1910                 :          8 :             throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrase was called.");
#    1911                 :          8 :         }
#    1912                 :            : 
#    1913                 :            :         // Note that the walletpassphrase is stored in request.params[0] which is not mlock()ed
#    1914                 :         65 :         SecureString strWalletPass;
#    1915                 :         65 :         strWalletPass.reserve(100);
#    1916                 :            :         // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
#    1917                 :            :         // Alternately, find a way to make request.params[0] mlock()'d to begin with.
#    1918                 :         65 :         strWalletPass = request.params[0].get_str().c_str();
#    1919                 :            : 
#    1920                 :            :         // Get the timeout
#    1921                 :         65 :         nSleepTime = request.params[1].get_int64();
#    1922                 :            :         // Timeout cannot be negative, otherwise it will relock immediately
#    1923         [ +  + ]:         65 :         if (nSleepTime < 0) {
#    1924                 :          2 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Timeout cannot be negative.");
#    1925                 :          2 :         }
#    1926                 :            :         // Clamp timeout
#    1927                 :         63 :         constexpr int64_t MAX_SLEEP_TIME = 100000000; // larger values trigger a macos/libevent bug?
#    1928         [ +  + ]:         63 :         if (nSleepTime > MAX_SLEEP_TIME) {
#    1929                 :          2 :             nSleepTime = MAX_SLEEP_TIME;
#    1930                 :          2 :         }
#    1931                 :            : 
#    1932         [ +  + ]:         63 :         if (strWalletPass.empty()) {
#    1933                 :          2 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty");
#    1934                 :          2 :         }
#    1935                 :            : 
#    1936         [ +  + ]:         61 :         if (!pwallet->Unlock(strWalletPass)) {
#    1937                 :          4 :             throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
#    1938                 :          4 :         }
#    1939                 :            : 
#    1940                 :         57 :         pwallet->TopUpKeyPool();
#    1941                 :            : 
#    1942                 :         57 :         pwallet->nRelockTime = GetTime() + nSleepTime;
#    1943                 :         57 :         relock_time = pwallet->nRelockTime;
#    1944                 :         57 :     }
#    1945                 :            : 
#    1946                 :            :     // rpcRunLater must be called without cs_wallet held otherwise a deadlock
#    1947                 :            :     // can occur. The deadlock would happen when RPCRunLater removes the
#    1948                 :            :     // previous timer (and waits for the callback to finish if already running)
#    1949                 :            :     // and the callback locks cs_wallet.
#    1950                 :         57 :     AssertLockNotHeld(wallet->cs_wallet);
#    1951                 :            :     // Keep a weak pointer to the wallet so that it is possible to unload the
#    1952                 :            :     // wallet before the following callback is called. If a valid shared pointer
#    1953                 :            :     // is acquired in the callback then the wallet is still loaded.
#    1954                 :         57 :     std::weak_ptr<CWallet> weak_wallet = wallet;
#    1955                 :         57 :     pwallet->chain().rpcRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), [weak_wallet, relock_time] {
#    1956         [ +  + ]:          7 :         if (auto shared_wallet = weak_wallet.lock()) {
#    1957                 :          4 :             LOCK(shared_wallet->cs_wallet);
#    1958                 :            :             // Skip if this is not the most recent rpcRunLater callback.
#    1959         [ -  + ]:          4 :             if (shared_wallet->nRelockTime != relock_time) return;
#    1960                 :          4 :             shared_wallet->Lock();
#    1961                 :          4 :             shared_wallet->nRelockTime = 0;
#    1962                 :          4 :         }
#    1963                 :          7 :     }, nSleepTime);
#    1964                 :            : 
#    1965                 :         57 :     return NullUniValue;
#    1966                 :         57 : },
#    1967                 :       1391 :     };
#    1968                 :       1391 : }
#    1969                 :            : 
#    1970                 :            : 
#    1971                 :            : static RPCHelpMan walletpassphrasechange()
#    1972                 :       1324 : {
#    1973                 :       1324 :     return RPCHelpMan{"walletpassphrasechange",
#    1974                 :       1324 :                 "\nChanges the wallet passphrase from 'oldpassphrase' to 'newpassphrase'.\n",
#    1975                 :       1324 :                 {
#    1976                 :       1324 :                     {"oldpassphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The current passphrase"},
#    1977                 :       1324 :                     {"newpassphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The new passphrase"},
#    1978                 :       1324 :                 },
#    1979                 :       1324 :                 RPCResult{RPCResult::Type::NONE, "", ""},
#    1980                 :       1324 :                 RPCExamples{
#    1981                 :       1324 :                     HelpExampleCli("walletpassphrasechange", "\"old one\" \"new one\"")
#    1982                 :       1324 :             + HelpExampleRpc("walletpassphrasechange", "\"old one\", \"new one\"")
#    1983                 :       1324 :                 },
#    1984                 :       1324 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    1985                 :       1324 : {
#    1986                 :          6 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    1987         [ -  + ]:          6 :     if (!pwallet) return NullUniValue;
#    1988                 :            : 
#    1989                 :          6 :     LOCK(pwallet->cs_wallet);
#    1990                 :            : 
#    1991         [ +  + ]:          6 :     if (!pwallet->IsCrypted()) {
#    1992                 :          2 :         throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrasechange was called.");
#    1993                 :          2 :     }
#    1994                 :            : 
#    1995                 :            :     // TODO: get rid of these .c_str() calls by implementing SecureString::operator=(std::string)
#    1996                 :            :     // Alternately, find a way to make request.params[0] mlock()'d to begin with.
#    1997                 :          4 :     SecureString strOldWalletPass;
#    1998                 :          4 :     strOldWalletPass.reserve(100);
#    1999                 :          4 :     strOldWalletPass = request.params[0].get_str().c_str();
#    2000                 :            : 
#    2001                 :          4 :     SecureString strNewWalletPass;
#    2002                 :          4 :     strNewWalletPass.reserve(100);
#    2003                 :          4 :     strNewWalletPass = request.params[1].get_str().c_str();
#    2004                 :            : 
#    2005 [ +  + ][ -  + ]:          4 :     if (strOldWalletPass.empty() || strNewWalletPass.empty()) {
#    2006                 :          2 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty");
#    2007                 :          2 :     }
#    2008                 :            : 
#    2009         [ -  + ]:          2 :     if (!pwallet->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass)) {
#    2010                 :          0 :         throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
#    2011                 :          0 :     }
#    2012                 :            : 
#    2013                 :          2 :     return NullUniValue;
#    2014                 :          2 : },
#    2015                 :       1324 :     };
#    2016                 :       1324 : }
#    2017                 :            : 
#    2018                 :            : 
#    2019                 :            : static RPCHelpMan walletlock()
#    2020                 :       1334 : {
#    2021                 :       1334 :     return RPCHelpMan{"walletlock",
#    2022                 :       1334 :                 "\nRemoves the wallet encryption key from memory, locking the wallet.\n"
#    2023                 :       1334 :                 "After calling this method, you will need to call walletpassphrase again\n"
#    2024                 :       1334 :                 "before being able to call any methods which require the wallet to be unlocked.\n",
#    2025                 :       1334 :                 {},
#    2026                 :       1334 :                 RPCResult{RPCResult::Type::NONE, "", ""},
#    2027                 :       1334 :                 RPCExamples{
#    2028                 :       1334 :             "\nSet the passphrase for 2 minutes to perform a transaction\n"
#    2029                 :       1334 :             + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 120") +
#    2030                 :       1334 :             "\nPerform a send (requires passphrase set)\n"
#    2031                 :       1334 :             + HelpExampleCli("sendtoaddress", "\"" + EXAMPLE_ADDRESS[0] + "\" 1.0") +
#    2032                 :       1334 :             "\nClear the passphrase since we are done before 2 minutes is up\n"
#    2033                 :       1334 :             + HelpExampleCli("walletlock", "") +
#    2034                 :       1334 :             "\nAs a JSON-RPC call\n"
#    2035                 :       1334 :             + HelpExampleRpc("walletlock", "")
#    2036                 :       1334 :                 },
#    2037                 :       1334 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    2038                 :       1334 : {
#    2039                 :         16 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    2040         [ -  + ]:         16 :     if (!pwallet) return NullUniValue;
#    2041                 :            : 
#    2042                 :         16 :     LOCK(pwallet->cs_wallet);
#    2043                 :            : 
#    2044         [ -  + ]:         16 :     if (!pwallet->IsCrypted()) {
#    2045                 :          0 :         throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletlock was called.");
#    2046                 :          0 :     }
#    2047                 :            : 
#    2048                 :         16 :     pwallet->Lock();
#    2049                 :         16 :     pwallet->nRelockTime = 0;
#    2050                 :            : 
#    2051                 :         16 :     return NullUniValue;
#    2052                 :         16 : },
#    2053                 :       1334 :     };
#    2054                 :       1334 : }
#    2055                 :            : 
#    2056                 :            : 
#    2057                 :            : static RPCHelpMan encryptwallet()
#    2058                 :       1345 : {
#    2059                 :       1345 :     return RPCHelpMan{"encryptwallet",
#    2060                 :       1345 :                 "\nEncrypts the wallet with 'passphrase'. This is for first time encryption.\n"
#    2061                 :       1345 :                 "After this, any calls that interact with private keys such as sending or signing \n"
#    2062                 :       1345 :                 "will require the passphrase to be set prior the making these calls.\n"
#    2063                 :       1345 :                 "Use the walletpassphrase call for this, and then walletlock call.\n"
#    2064                 :       1345 :                 "If the wallet is already encrypted, use the walletpassphrasechange call.\n",
#    2065                 :       1345 :                 {
#    2066                 :       1345 :                     {"passphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The pass phrase to encrypt the wallet with. It must be at least 1 character, but should be long."},
#    2067                 :       1345 :                 },
#    2068                 :       1345 :                 RPCResult{RPCResult::Type::STR, "", "A string with further instructions"},
#    2069                 :       1345 :                 RPCExamples{
#    2070                 :       1345 :             "\nEncrypt your wallet\n"
#    2071                 :       1345 :             + HelpExampleCli("encryptwallet", "\"my pass phrase\"") +
#    2072                 :       1345 :             "\nNow set the passphrase to use the wallet, such as for signing or sending bitcoin\n"
#    2073                 :       1345 :             + HelpExampleCli("walletpassphrase", "\"my pass phrase\"") +
#    2074                 :       1345 :             "\nNow we can do something like sign\n"
#    2075                 :       1345 :             + HelpExampleCli("signmessage", "\"address\" \"test message\"") +
#    2076                 :       1345 :             "\nNow lock the wallet again by removing the passphrase\n"
#    2077                 :       1345 :             + HelpExampleCli("walletlock", "") +
#    2078                 :       1345 :             "\nAs a JSON-RPC call\n"
#    2079                 :       1345 :             + HelpExampleRpc("encryptwallet", "\"my pass phrase\"")
#    2080                 :       1345 :                 },
#    2081                 :       1345 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    2082                 :       1345 : {
#    2083                 :         27 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    2084         [ -  + ]:         27 :     if (!pwallet) return NullUniValue;
#    2085                 :            : 
#    2086                 :         27 :     LOCK(pwallet->cs_wallet);
#    2087                 :            : 
#    2088         [ +  + ]:         27 :     if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
#    2089                 :          3 :         throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: wallet does not contain private keys, nothing to encrypt.");
#    2090                 :          3 :     }
#    2091                 :            : 
#    2092         [ +  + ]:         24 :     if (pwallet->IsCrypted()) {
#    2093                 :          2 :         throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an encrypted wallet, but encryptwallet was called.");
#    2094                 :          2 :     }
#    2095                 :            : 
#    2096                 :            :     // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
#    2097                 :            :     // Alternately, find a way to make request.params[0] mlock()'d to begin with.
#    2098                 :         22 :     SecureString strWalletPass;
#    2099                 :         22 :     strWalletPass.reserve(100);
#    2100                 :         22 :     strWalletPass = request.params[0].get_str().c_str();
#    2101                 :            : 
#    2102         [ +  + ]:         22 :     if (strWalletPass.empty()) {
#    2103                 :          2 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty");
#    2104                 :          2 :     }
#    2105                 :            : 
#    2106         [ -  + ]:         20 :     if (!pwallet->EncryptWallet(strWalletPass)) {
#    2107                 :          0 :         throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: Failed to encrypt the wallet.");
#    2108                 :          0 :     }
#    2109                 :            : 
#    2110                 :         20 :     return "wallet encrypted; The keypool has been flushed and a new HD seed was generated (if you are using HD). You need to make a new backup.";
#    2111                 :         20 : },
#    2112                 :       1345 :     };
#    2113                 :       1345 : }
#    2114                 :            : 
#    2115                 :            : static RPCHelpMan lockunspent()
#    2116                 :       1342 : {
#    2117                 :       1342 :     return RPCHelpMan{"lockunspent",
#    2118                 :       1342 :                 "\nUpdates list of temporarily unspendable outputs.\n"
#    2119                 :       1342 :                 "Temporarily lock (unlock=false) or unlock (unlock=true) specified transaction outputs.\n"
#    2120                 :       1342 :                 "If no transaction outputs are specified when unlocking then all current locked transaction outputs are unlocked.\n"
#    2121                 :       1342 :                 "A locked transaction output will not be chosen by automatic coin selection, when spending bitcoins.\n"
#    2122                 :       1342 :                 "Manually selected coins are automatically unlocked.\n"
#    2123                 :       1342 :                 "Locks are stored in memory only. Nodes start with zero locked outputs, and the locked output list\n"
#    2124                 :       1342 :                 "is always cleared (by virtue of process exit) when a node stops or fails.\n"
#    2125                 :       1342 :                 "Also see the listunspent call\n",
#    2126                 :       1342 :                 {
#    2127                 :       1342 :                     {"unlock", RPCArg::Type::BOOL, RPCArg::Optional::NO, "Whether to unlock (true) or lock (false) the specified transactions"},
#    2128                 :       1342 :                     {"transactions", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "The transaction outputs and within each, the txid (string) vout (numeric).",
#    2129                 :       1342 :                         {
#    2130                 :       1342 :                             {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
#    2131                 :       1342 :                                 {
#    2132                 :       1342 :                                     {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id"},
#    2133                 :       1342 :                                     {"vout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The output number"},
#    2134                 :       1342 :                                 },
#    2135                 :       1342 :                             },
#    2136                 :       1342 :                         },
#    2137                 :       1342 :                     },
#    2138                 :       1342 :                 },
#    2139                 :       1342 :                 RPCResult{
#    2140                 :       1342 :                     RPCResult::Type::BOOL, "", "Whether the command was successful or not"
#    2141                 :       1342 :                 },
#    2142                 :       1342 :                 RPCExamples{
#    2143                 :       1342 :             "\nList the unspent transactions\n"
#    2144                 :       1342 :             + HelpExampleCli("listunspent", "") +
#    2145                 :       1342 :             "\nLock an unspent transaction\n"
#    2146                 :       1342 :             + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
#    2147                 :       1342 :             "\nList the locked transactions\n"
#    2148                 :       1342 :             + HelpExampleCli("listlockunspent", "") +
#    2149                 :       1342 :             "\nUnlock the transaction again\n"
#    2150                 :       1342 :             + HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
#    2151                 :       1342 :             "\nAs a JSON-RPC call\n"
#    2152                 :       1342 :             + HelpExampleRpc("lockunspent", "false, \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"")
#    2153                 :       1342 :                 },
#    2154                 :       1342 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    2155                 :       1342 : {
#    2156                 :         24 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    2157         [ -  + ]:         24 :     if (!pwallet) return NullUniValue;
#    2158                 :            : 
#    2159                 :            :     // Make sure the results are valid at least up to the most recent block
#    2160                 :            :     // the user could have gotten from another RPC command prior to now
#    2161                 :         24 :     pwallet->BlockUntilSyncedToCurrentChain();
#    2162                 :            : 
#    2163                 :         24 :     LOCK(pwallet->cs_wallet);
#    2164                 :            : 
#    2165                 :         24 :     RPCTypeCheckArgument(request.params[0], UniValue::VBOOL);
#    2166                 :            : 
#    2167                 :         24 :     bool fUnlock = request.params[0].get_bool();
#    2168                 :            : 
#    2169         [ -  + ]:         24 :     if (request.params[1].isNull()) {
#    2170         [ #  # ]:          0 :         if (fUnlock)
#    2171                 :          0 :             pwallet->UnlockAllCoins();
#    2172                 :          0 :         return true;
#    2173                 :          0 :     }
#    2174                 :            : 
#    2175                 :         24 :     RPCTypeCheckArgument(request.params[1], UniValue::VARR);
#    2176                 :            : 
#    2177                 :         24 :     const UniValue& output_params = request.params[1];
#    2178                 :            : 
#    2179                 :            :     // Create and validate the COutPoints first.
#    2180                 :            : 
#    2181                 :         24 :     std::vector<COutPoint> outputs;
#    2182                 :         24 :     outputs.reserve(output_params.size());
#    2183                 :            : 
#    2184         [ +  + ]:         38 :     for (unsigned int idx = 0; idx < output_params.size(); idx++) {
#    2185                 :         24 :         const UniValue& o = output_params[idx].get_obj();
#    2186                 :            : 
#    2187                 :         24 :         RPCTypeCheckObj(o,
#    2188                 :         24 :             {
#    2189                 :         24 :                 {"txid", UniValueType(UniValue::VSTR)},
#    2190                 :         24 :                 {"vout", UniValueType(UniValue::VNUM)},
#    2191                 :         24 :             });
#    2192                 :            : 
#    2193                 :         24 :         const uint256 txid(ParseHashO(o, "txid"));
#    2194                 :         24 :         const int nOutput = find_value(o, "vout").get_int();
#    2195         [ -  + ]:         24 :         if (nOutput < 0) {
#    2196                 :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative");
#    2197                 :          0 :         }
#    2198                 :            : 
#    2199                 :         24 :         const COutPoint outpt(txid, nOutput);
#    2200                 :            : 
#    2201                 :         24 :         const auto it = pwallet->mapWallet.find(outpt.hash);
#    2202         [ +  + ]:         24 :         if (it == pwallet->mapWallet.end()) {
#    2203                 :          2 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, unknown transaction");
#    2204                 :          2 :         }
#    2205                 :            : 
#    2206                 :         22 :         const CWalletTx& trans = it->second;
#    2207                 :            : 
#    2208         [ +  + ]:         22 :         if (outpt.n >= trans.tx->vout.size()) {
#    2209                 :          2 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout index out of bounds");
#    2210                 :          2 :         }
#    2211                 :            : 
#    2212         [ +  + ]:         20 :         if (pwallet->IsSpent(outpt.hash, outpt.n)) {
#    2213                 :          2 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected unspent output");
#    2214                 :          2 :         }
#    2215                 :            : 
#    2216                 :         18 :         const bool is_locked = pwallet->IsLockedCoin(outpt.hash, outpt.n);
#    2217                 :            : 
#    2218 [ +  + ][ +  + ]:         18 :         if (fUnlock && !is_locked) {
#    2219                 :          2 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected locked output");
#    2220                 :          2 :         }
#    2221                 :            : 
#    2222 [ +  + ][ +  + ]:         16 :         if (!fUnlock && is_locked) {
#    2223                 :          2 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, output already locked");
#    2224                 :          2 :         }
#    2225                 :            : 
#    2226                 :         14 :         outputs.push_back(outpt);
#    2227                 :         14 :     }
#    2228                 :            : 
#    2229                 :            :     // Atomically set (un)locked status for the outputs.
#    2230         [ +  + ]:         24 :     for (const COutPoint& outpt : outputs) {
#    2231         [ +  + ]:         10 :         if (fUnlock) pwallet->UnlockCoin(outpt);
#    2232                 :          6 :         else pwallet->LockCoin(outpt);
#    2233                 :         10 :     }
#    2234                 :            : 
#    2235                 :         14 :     return true;
#    2236                 :         24 : },
#    2237                 :       1342 :     };
#    2238                 :       1342 : }
#    2239                 :            : 
#    2240                 :            : static RPCHelpMan listlockunspent()
#    2241                 :       1334 : {
#    2242                 :       1334 :     return RPCHelpMan{"listlockunspent",
#    2243                 :       1334 :                 "\nReturns list of temporarily unspendable outputs.\n"
#    2244                 :       1334 :                 "See the lockunspent call to lock and unlock transactions for spending.\n",
#    2245                 :       1334 :                 {},
#    2246                 :       1334 :                 RPCResult{
#    2247                 :       1334 :                     RPCResult::Type::ARR, "", "",
#    2248                 :       1334 :                     {
#    2249                 :       1334 :                         {RPCResult::Type::OBJ, "", "",
#    2250                 :       1334 :                         {
#    2251                 :       1334 :                             {RPCResult::Type::STR_HEX, "txid", "The transaction id locked"},
#    2252                 :       1334 :                             {RPCResult::Type::NUM, "vout", "The vout value"},
#    2253                 :       1334 :                         }},
#    2254                 :       1334 :                     }
#    2255                 :       1334 :                 },
#    2256                 :       1334 :                 RPCExamples{
#    2257                 :       1334 :             "\nList the unspent transactions\n"
#    2258                 :       1334 :             + HelpExampleCli("listunspent", "") +
#    2259                 :       1334 :             "\nLock an unspent transaction\n"
#    2260                 :       1334 :             + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
#    2261                 :       1334 :             "\nList the locked transactions\n"
#    2262                 :       1334 :             + HelpExampleCli("listlockunspent", "") +
#    2263                 :       1334 :             "\nUnlock the transaction again\n"
#    2264                 :       1334 :             + HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
#    2265                 :       1334 :             "\nAs a JSON-RPC call\n"
#    2266                 :       1334 :             + HelpExampleRpc("listlockunspent", "")
#    2267                 :       1334 :                 },
#    2268                 :       1334 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    2269                 :       1334 : {
#    2270                 :         16 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    2271         [ -  + ]:         16 :     if (!pwallet) return NullUniValue;
#    2272                 :            : 
#    2273                 :         16 :     LOCK(pwallet->cs_wallet);
#    2274                 :            : 
#    2275                 :         16 :     std::vector<COutPoint> vOutpts;
#    2276                 :         16 :     pwallet->ListLockedCoins(vOutpts);
#    2277                 :            : 
#    2278                 :         16 :     UniValue ret(UniValue::VARR);
#    2279                 :            : 
#    2280         [ +  + ]:         16 :     for (const COutPoint& outpt : vOutpts) {
#    2281                 :          8 :         UniValue o(UniValue::VOBJ);
#    2282                 :            : 
#    2283                 :          8 :         o.pushKV("txid", outpt.hash.GetHex());
#    2284                 :          8 :         o.pushKV("vout", (int)outpt.n);
#    2285                 :          8 :         ret.push_back(o);
#    2286                 :          8 :     }
#    2287                 :            : 
#    2288                 :         16 :     return ret;
#    2289                 :         16 : },
#    2290                 :       1334 :     };
#    2291                 :       1334 : }
#    2292                 :            : 
#    2293                 :            : static RPCHelpMan settxfee()
#    2294                 :       1351 : {
#    2295                 :       1351 :     return RPCHelpMan{"settxfee",
#    2296                 :       1351 :                 "\nSet the transaction fee rate in " + CURRENCY_UNIT + "/kvB for this wallet. Overrides the global -paytxfee command line parameter.\n"
#    2297                 :       1351 :                 "Can be deactivated by passing 0 as the fee. In that case automatic fee selection will be used by default.\n",
#    2298                 :       1351 :                 {
#    2299                 :       1351 :                     {"amount", RPCArg::Type::AMOUNT, RPCArg::Optional::NO, "The transaction fee rate in " + CURRENCY_UNIT + "/kvB"},
#    2300                 :       1351 :                 },
#    2301                 :       1351 :                 RPCResult{
#    2302                 :       1351 :                     RPCResult::Type::BOOL, "", "Returns true if successful"
#    2303                 :       1351 :                 },
#    2304                 :       1351 :                 RPCExamples{
#    2305                 :       1351 :                     HelpExampleCli("settxfee", "0.00001")
#    2306                 :       1351 :             + HelpExampleRpc("settxfee", "0.00001")
#    2307                 :       1351 :                 },
#    2308                 :       1351 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    2309                 :       1351 : {
#    2310                 :         33 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    2311         [ -  + ]:         33 :     if (!pwallet) return NullUniValue;
#    2312                 :            : 
#    2313                 :         33 :     LOCK(pwallet->cs_wallet);
#    2314                 :            : 
#    2315                 :         33 :     CAmount nAmount = AmountFromValue(request.params[0]);
#    2316                 :         33 :     CFeeRate tx_fee_rate(nAmount, 1000);
#    2317                 :         33 :     CFeeRate max_tx_fee_rate(pwallet->m_default_max_tx_fee, 1000);
#    2318         [ +  + ]:         33 :     if (tx_fee_rate == CFeeRate(0)) {
#    2319                 :            :         // automatic selection
#    2320         [ +  + ]:         28 :     } else if (tx_fee_rate < pwallet->chain().relayMinFee()) {
#    2321                 :          2 :         throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("txfee cannot be less than min relay tx fee (%s)", pwallet->chain().relayMinFee().ToString()));
#    2322         [ +  + ]:         26 :     } else if (tx_fee_rate < pwallet->m_min_fee) {
#    2323                 :          2 :         throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("txfee cannot be less than wallet min fee (%s)", pwallet->m_min_fee.ToString()));
#    2324         [ +  + ]:         24 :     } else if (tx_fee_rate > max_tx_fee_rate) {
#    2325                 :          2 :         throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("txfee cannot be more than wallet max tx fee (%s)", max_tx_fee_rate.ToString()));
#    2326                 :          2 :     }
#    2327                 :            : 
#    2328                 :         27 :     pwallet->m_pay_tx_fee = tx_fee_rate;
#    2329                 :         27 :     return true;
#    2330                 :         27 : },
#    2331                 :       1351 :     };
#    2332                 :       1351 : }
#    2333                 :            : 
#    2334                 :            : static RPCHelpMan getbalances()
#    2335                 :       2225 : {
#    2336                 :       2225 :     return RPCHelpMan{
#    2337                 :       2225 :         "getbalances",
#    2338                 :       2225 :         "Returns an object with all balances in " + CURRENCY_UNIT + ".\n",
#    2339                 :       2225 :         {},
#    2340                 :       2225 :         RPCResult{
#    2341                 :       2225 :             RPCResult::Type::OBJ, "", "",
#    2342                 :       2225 :             {
#    2343                 :       2225 :                 {RPCResult::Type::OBJ, "mine", "balances from outputs that the wallet can sign",
#    2344                 :       2225 :                 {
#    2345                 :       2225 :                     {RPCResult::Type::STR_AMOUNT, "trusted", "trusted balance (outputs created by the wallet or confirmed outputs)"},
#    2346                 :       2225 :                     {RPCResult::Type::STR_AMOUNT, "untrusted_pending", "untrusted pending balance (outputs created by others that are in the mempool)"},
#    2347                 :       2225 :                     {RPCResult::Type::STR_AMOUNT, "immature", "balance from immature coinbase outputs"},
#    2348                 :       2225 :                     {RPCResult::Type::STR_AMOUNT, "used", "(only present if avoid_reuse is set) balance from coins sent to addresses that were previously spent from (potentially privacy violating)"},
#    2349                 :       2225 :                 }},
#    2350                 :       2225 :                 {RPCResult::Type::OBJ, "watchonly", "watchonly balances (not present if wallet does not watch anything)",
#    2351                 :       2225 :                 {
#    2352                 :       2225 :                     {RPCResult::Type::STR_AMOUNT, "trusted", "trusted balance (outputs created by the wallet or confirmed outputs)"},
#    2353                 :       2225 :                     {RPCResult::Type::STR_AMOUNT, "untrusted_pending", "untrusted pending balance (outputs created by others that are in the mempool)"},
#    2354                 :       2225 :                     {RPCResult::Type::STR_AMOUNT, "immature", "balance from immature coinbase outputs"},
#    2355                 :       2225 :                 }},
#    2356                 :       2225 :             }
#    2357                 :       2225 :             },
#    2358                 :       2225 :         RPCExamples{
#    2359                 :       2225 :             HelpExampleCli("getbalances", "") +
#    2360                 :       2225 :             HelpExampleRpc("getbalances", "")},
#    2361                 :       2225 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    2362                 :       2225 : {
#    2363                 :        907 :     std::shared_ptr<CWallet> const rpc_wallet = GetWalletForJSONRPCRequest(request);
#    2364         [ -  + ]:        907 :     if (!rpc_wallet) return NullUniValue;
#    2365                 :        907 :     CWallet& wallet = *rpc_wallet;
#    2366                 :            : 
#    2367                 :            :     // Make sure the results are valid at least up to the most recent block
#    2368                 :            :     // the user could have gotten from another RPC command prior to now
#    2369                 :        907 :     wallet.BlockUntilSyncedToCurrentChain();
#    2370                 :            : 
#    2371                 :        907 :     LOCK(wallet.cs_wallet);
#    2372                 :            : 
#    2373                 :        907 :     const auto bal = wallet.GetBalance();
#    2374                 :        907 :     UniValue balances{UniValue::VOBJ};
#    2375                 :        907 :     {
#    2376                 :        907 :         UniValue balances_mine{UniValue::VOBJ};
#    2377                 :        907 :         balances_mine.pushKV("trusted", ValueFromAmount(bal.m_mine_trusted));
#    2378                 :        907 :         balances_mine.pushKV("untrusted_pending", ValueFromAmount(bal.m_mine_untrusted_pending));
#    2379                 :        907 :         balances_mine.pushKV("immature", ValueFromAmount(bal.m_mine_immature));
#    2380         [ +  + ]:        907 :         if (wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE)) {
#    2381                 :            :             // If the AVOID_REUSE flag is set, bal has been set to just the un-reused address balance. Get
#    2382                 :            :             // the total balance, and then subtract bal to get the reused address balance.
#    2383                 :         28 :             const auto full_bal = wallet.GetBalance(0, false);
#    2384                 :         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));
#    2385                 :         28 :         }
#    2386                 :        907 :         balances.pushKV("mine", balances_mine);
#    2387                 :        907 :     }
#    2388                 :        907 :     auto spk_man = wallet.GetLegacyScriptPubKeyMan();
#    2389 [ +  + ][ +  + ]:        907 :     if (spk_man && spk_man->HaveWatchOnly()) {
#    2390                 :         13 :         UniValue balances_watchonly{UniValue::VOBJ};
#    2391                 :         13 :         balances_watchonly.pushKV("trusted", ValueFromAmount(bal.m_watchonly_trusted));
#    2392                 :         13 :         balances_watchonly.pushKV("untrusted_pending", ValueFromAmount(bal.m_watchonly_untrusted_pending));
#    2393                 :         13 :         balances_watchonly.pushKV("immature", ValueFromAmount(bal.m_watchonly_immature));
#    2394                 :         13 :         balances.pushKV("watchonly", balances_watchonly);
#    2395                 :         13 :     }
#    2396                 :        907 :     return balances;
#    2397                 :        907 : },
#    2398                 :       2225 :     };
#    2399                 :       2225 : }
#    2400                 :            : 
#    2401                 :            : static RPCHelpMan getwalletinfo()
#    2402                 :       2315 : {
#    2403                 :       2315 :     return RPCHelpMan{"getwalletinfo",
#    2404                 :       2315 :                 "Returns an object containing various wallet state info.\n",
#    2405                 :       2315 :                 {},
#    2406                 :       2315 :                 RPCResult{
#    2407                 :       2315 :                     RPCResult::Type::OBJ, "", "",
#    2408                 :       2315 :                     {
#    2409                 :       2315 :                         {
#    2410                 :       2315 :                         {RPCResult::Type::STR, "walletname", "the wallet name"},
#    2411                 :       2315 :                         {RPCResult::Type::NUM, "walletversion", "the wallet version"},
#    2412                 :       2315 :                         {RPCResult::Type::STR, "format", "the database format (bdb or sqlite)"},
#    2413                 :       2315 :                         {RPCResult::Type::STR_AMOUNT, "balance", "DEPRECATED. Identical to getbalances().mine.trusted"},
#    2414                 :       2315 :                         {RPCResult::Type::STR_AMOUNT, "unconfirmed_balance", "DEPRECATED. Identical to getbalances().mine.untrusted_pending"},
#    2415                 :       2315 :                         {RPCResult::Type::STR_AMOUNT, "immature_balance", "DEPRECATED. Identical to getbalances().mine.immature"},
#    2416                 :       2315 :                         {RPCResult::Type::NUM, "txcount", "the total number of transactions in the wallet"},
#    2417                 :       2315 :                         {RPCResult::Type::NUM_TIME, "keypoololdest", "the " + UNIX_EPOCH_TIME + " of the oldest pre-generated key in the key pool. Legacy wallets only."},
#    2418                 :       2315 :                         {RPCResult::Type::NUM, "keypoolsize", "how many new keys are pre-generated (only counts external keys)"},
#    2419                 :       2315 :                         {RPCResult::Type::NUM, "keypoolsize_hd_internal", "how many new keys are pre-generated for internal use (used for change outputs, only appears if the wallet is using this feature, otherwise external keys are used)"},
#    2420                 :       2315 :                         {RPCResult::Type::NUM_TIME, "unlocked_until", /* optional */ true, "the " + UNIX_EPOCH_TIME + " until which the wallet is unlocked for transfers, or 0 if the wallet is locked (only present for passphrase-encrypted wallets)"},
#    2421                 :       2315 :                         {RPCResult::Type::STR_AMOUNT, "paytxfee", "the transaction fee configuration, set in " + CURRENCY_UNIT + "/kvB"},
#    2422                 :       2315 :                         {RPCResult::Type::STR_HEX, "hdseedid", /* optional */ true, "the Hash160 of the HD seed (only present when HD is enabled)"},
#    2423                 :       2315 :                         {RPCResult::Type::BOOL, "private_keys_enabled", "false if privatekeys are disabled for this wallet (enforced watch-only wallet)"},
#    2424                 :       2315 :                         {RPCResult::Type::BOOL, "avoid_reuse", "whether this wallet tracks clean/dirty coins in terms of reuse"},
#    2425                 :       2315 :                         {RPCResult::Type::OBJ, "scanning", "current scanning details, or false if no scan is in progress",
#    2426                 :       2315 :                         {
#    2427                 :       2315 :                             {RPCResult::Type::NUM, "duration", "elapsed seconds since scan start"},
#    2428                 :       2315 :                             {RPCResult::Type::NUM, "progress", "scanning progress percentage [0.0, 1.0]"},
#    2429                 :       2315 :                         }},
#    2430                 :       2315 :                         {RPCResult::Type::BOOL, "descriptors", "whether this wallet uses descriptors for scriptPubKey management"},
#    2431                 :       2315 :                     }},
#    2432                 :       2315 :                 },
#    2433                 :       2315 :                 RPCExamples{
#    2434                 :       2315 :                     HelpExampleCli("getwalletinfo", "")
#    2435                 :       2315 :             + HelpExampleRpc("getwalletinfo", "")
#    2436                 :       2315 :                 },
#    2437                 :       2315 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    2438                 :       2315 : {
#    2439                 :        997 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    2440         [ -  + ]:        997 :     if (!pwallet) return NullUniValue;
#    2441                 :            : 
#    2442                 :            :     // Make sure the results are valid at least up to the most recent block
#    2443                 :            :     // the user could have gotten from another RPC command prior to now
#    2444                 :        997 :     pwallet->BlockUntilSyncedToCurrentChain();
#    2445                 :            : 
#    2446                 :        997 :     LOCK(pwallet->cs_wallet);
#    2447                 :            : 
#    2448                 :        997 :     UniValue obj(UniValue::VOBJ);
#    2449                 :            : 
#    2450                 :        997 :     size_t kpExternalSize = pwallet->KeypoolCountExternalKeys();
#    2451                 :        997 :     const auto bal = pwallet->GetBalance();
#    2452                 :        997 :     int64_t kp_oldest = pwallet->GetOldestKeyPoolTime();
#    2453                 :        997 :     obj.pushKV("walletname", pwallet->GetName());
#    2454                 :        997 :     obj.pushKV("walletversion", pwallet->GetVersion());
#    2455                 :        997 :     obj.pushKV("format", pwallet->GetDatabase().Format());
#    2456                 :        997 :     obj.pushKV("balance", ValueFromAmount(bal.m_mine_trusted));
#    2457                 :        997 :     obj.pushKV("unconfirmed_balance", ValueFromAmount(bal.m_mine_untrusted_pending));
#    2458                 :        997 :     obj.pushKV("immature_balance", ValueFromAmount(bal.m_mine_immature));
#    2459                 :        997 :     obj.pushKV("txcount",       (int)pwallet->mapWallet.size());
#    2460         [ +  + ]:        997 :     if (kp_oldest > 0) {
#    2461                 :        678 :         obj.pushKV("keypoololdest", kp_oldest);
#    2462                 :        678 :     }
#    2463                 :        997 :     obj.pushKV("keypoolsize", (int64_t)kpExternalSize);
#    2464                 :            : 
#    2465                 :        997 :     LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
#    2466         [ +  + ]:        997 :     if (spk_man) {
#    2467                 :        658 :         CKeyID seed_id = spk_man->GetHDChain().seed_id;
#    2468         [ +  + ]:        658 :         if (!seed_id.IsNull()) {
#    2469                 :        578 :             obj.pushKV("hdseedid", seed_id.GetHex());
#    2470                 :        578 :         }
#    2471                 :        658 :     }
#    2472                 :            : 
#    2473         [ +  + ]:        997 :     if (pwallet->CanSupportFeature(FEATURE_HD_SPLIT)) {
#    2474                 :        978 :         obj.pushKV("keypoolsize_hd_internal",   (int64_t)(pwallet->GetKeyPoolSize() - kpExternalSize));
#    2475                 :        978 :     }
#    2476         [ +  + ]:        997 :     if (pwallet->IsCrypted()) {
#    2477                 :         22 :         obj.pushKV("unlocked_until", pwallet->nRelockTime);
#    2478                 :         22 :     }
#    2479                 :        997 :     obj.pushKV("paytxfee", ValueFromAmount(pwallet->m_pay_tx_fee.GetFeePerK()));
#    2480                 :        997 :     obj.pushKV("private_keys_enabled", !pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
#    2481                 :        997 :     obj.pushKV("avoid_reuse", pwallet->IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE));
#    2482         [ -  + ]:        997 :     if (pwallet->IsScanning()) {
#    2483                 :          0 :         UniValue scanning(UniValue::VOBJ);
#    2484                 :          0 :         scanning.pushKV("duration", pwallet->ScanningDuration() / 1000);
#    2485                 :          0 :         scanning.pushKV("progress", pwallet->ScanningProgress());
#    2486                 :          0 :         obj.pushKV("scanning", scanning);
#    2487                 :        997 :     } else {
#    2488                 :        997 :         obj.pushKV("scanning", false);
#    2489                 :        997 :     }
#    2490                 :        997 :     obj.pushKV("descriptors", pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
#    2491                 :        997 :     return obj;
#    2492                 :        997 : },
#    2493                 :       2315 :     };
#    2494                 :       2315 : }
#    2495                 :            : 
#    2496                 :            : static RPCHelpMan listwalletdir()
#    2497                 :       1331 : {
#    2498                 :       1331 :     return RPCHelpMan{"listwalletdir",
#    2499                 :       1331 :                 "Returns a list of wallets in the wallet directory.\n",
#    2500                 :       1331 :                 {},
#    2501                 :       1331 :                 RPCResult{
#    2502                 :       1331 :                     RPCResult::Type::OBJ, "", "",
#    2503                 :       1331 :                     {
#    2504                 :       1331 :                         {RPCResult::Type::ARR, "wallets", "",
#    2505                 :       1331 :                         {
#    2506                 :       1331 :                             {RPCResult::Type::OBJ, "", "",
#    2507                 :       1331 :                             {
#    2508                 :       1331 :                                 {RPCResult::Type::STR, "name", "The wallet name"},
#    2509                 :       1331 :                             }},
#    2510                 :       1331 :                         }},
#    2511                 :       1331 :                     }
#    2512                 :       1331 :                 },
#    2513                 :       1331 :                 RPCExamples{
#    2514                 :       1331 :                     HelpExampleCli("listwalletdir", "")
#    2515                 :       1331 :             + HelpExampleRpc("listwalletdir", "")
#    2516                 :       1331 :                 },
#    2517                 :       1331 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    2518                 :       1331 : {
#    2519                 :         13 :     UniValue wallets(UniValue::VARR);
#    2520         [ +  + ]:         88 :     for (const auto& path : ListDatabases(GetWalletDir())) {
#    2521                 :         88 :         UniValue wallet(UniValue::VOBJ);
#    2522                 :         88 :         wallet.pushKV("name", path.string());
#    2523                 :         88 :         wallets.push_back(wallet);
#    2524                 :         88 :     }
#    2525                 :            : 
#    2526                 :         13 :     UniValue result(UniValue::VOBJ);
#    2527                 :         13 :     result.pushKV("wallets", wallets);
#    2528                 :         13 :     return result;
#    2529                 :         13 : },
#    2530                 :       1331 :     };
#    2531                 :       1331 : }
#    2532                 :            : 
#    2533                 :            : static RPCHelpMan listwallets()
#    2534                 :       1436 : {
#    2535                 :       1436 :     return RPCHelpMan{"listwallets",
#    2536                 :       1436 :                 "Returns a list of currently loaded wallets.\n"
#    2537                 :       1436 :                 "For full information on the wallet, use \"getwalletinfo\"\n",
#    2538                 :       1436 :                 {},
#    2539                 :       1436 :                 RPCResult{
#    2540                 :       1436 :                     RPCResult::Type::ARR, "", "",
#    2541                 :       1436 :                     {
#    2542                 :       1436 :                         {RPCResult::Type::STR, "walletname", "the wallet name"},
#    2543                 :       1436 :                     }
#    2544                 :       1436 :                 },
#    2545                 :       1436 :                 RPCExamples{
#    2546                 :       1436 :                     HelpExampleCli("listwallets", "")
#    2547                 :       1436 :             + HelpExampleRpc("listwallets", "")
#    2548                 :       1436 :                 },
#    2549                 :       1436 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    2550                 :       1436 : {
#    2551                 :        118 :     UniValue obj(UniValue::VARR);
#    2552                 :            : 
#    2553         [ +  + ]:        385 :     for (const std::shared_ptr<CWallet>& wallet : GetWallets()) {
#    2554                 :        385 :         LOCK(wallet->cs_wallet);
#    2555                 :        385 :         obj.push_back(wallet->GetName());
#    2556                 :        385 :     }
#    2557                 :            : 
#    2558                 :        118 :     return obj;
#    2559                 :        118 : },
#    2560                 :       1436 :     };
#    2561                 :       1436 : }
#    2562                 :            : 
#    2563                 :            : static RPCHelpMan loadwallet()
#    2564                 :       1535 : {
#    2565                 :       1535 :     return RPCHelpMan{"loadwallet",
#    2566                 :       1535 :                 "\nLoads a wallet from a wallet file or directory."
#    2567                 :       1535 :                 "\nNote that all wallet command-line options used when starting bitcoind will be"
#    2568                 :       1535 :                 "\napplied to the new wallet (eg -rescan, etc).\n",
#    2569                 :       1535 :                 {
#    2570                 :       1535 :                     {"filename", RPCArg::Type::STR, RPCArg::Optional::NO, "The wallet directory or .dat file."},
#    2571                 :       1535 :                     {"load_on_startup", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED_NAMED_ARG, "Save wallet name to persistent settings and load on startup. True to add wallet to startup list, false to remove, null to leave unchanged."},
#    2572                 :       1535 :                 },
#    2573                 :       1535 :                 RPCResult{
#    2574                 :       1535 :                     RPCResult::Type::OBJ, "", "",
#    2575                 :       1535 :                     {
#    2576                 :       1535 :                         {RPCResult::Type::STR, "name", "The wallet name if loaded successfully."},
#    2577                 :       1535 :                         {RPCResult::Type::STR, "warning", "Warning message if wallet was not loaded cleanly."},
#    2578                 :       1535 :                     }
#    2579                 :       1535 :                 },
#    2580                 :       1535 :                 RPCExamples{
#    2581                 :       1535 :                     HelpExampleCli("loadwallet", "\"test.dat\"")
#    2582                 :       1535 :             + HelpExampleRpc("loadwallet", "\"test.dat\"")
#    2583                 :       1535 :                 },
#    2584                 :       1535 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    2585                 :       1535 : {
#    2586                 :        217 :     WalletContext& context = EnsureWalletContext(request.context);
#    2587                 :        217 :     const std::string name(request.params[0].get_str());
#    2588                 :            : 
#    2589                 :        217 :     DatabaseOptions options;
#    2590                 :        217 :     DatabaseStatus status;
#    2591                 :        217 :     options.require_existing = true;
#    2592                 :        217 :     bilingual_str error;
#    2593                 :        217 :     std::vector<bilingual_str> warnings;
#    2594         [ +  + ]:        217 :     std::optional<bool> load_on_start = request.params[1].isNull() ? std::nullopt : std::optional<bool>(request.params[1].get_bool());
#    2595                 :        217 :     std::shared_ptr<CWallet> const wallet = LoadWallet(*context.chain, name, load_on_start, options, status, error, warnings);
#    2596         [ +  + ]:        217 :     if (!wallet) {
#    2597                 :            :         // Map bad format to not found, since bad format is returned when the
#    2598                 :            :         // wallet directory exists, but doesn't contain a data file.
#    2599                 :         29 :         RPCErrorCode code = RPC_WALLET_ERROR;
#    2600                 :         29 :         switch (status) {
#    2601         [ +  + ]:          5 :             case DatabaseStatus::FAILED_NOT_FOUND:
#    2602         [ +  + ]:          8 :             case DatabaseStatus::FAILED_BAD_FORMAT:
#    2603                 :          8 :                 code = RPC_WALLET_NOT_FOUND;
#    2604                 :          8 :                 break;
#    2605         [ +  + ]:          5 :             case DatabaseStatus::FAILED_ALREADY_LOADED:
#    2606                 :          4 :                 code = RPC_WALLET_ALREADY_LOADED;
#    2607                 :          4 :                 break;
#    2608         [ +  + ]:         17 :             default: // RPC_WALLET_ERROR is returned for all other cases.
#    2609                 :         17 :                 break;
#    2610                 :         29 :         }
#    2611                 :         29 :         throw JSONRPCError(code, error.original);
#    2612                 :         29 :     }
#    2613                 :            : 
#    2614                 :        188 :     UniValue obj(UniValue::VOBJ);
#    2615                 :        188 :     obj.pushKV("name", wallet->GetName());
#    2616                 :        188 :     obj.pushKV("warning", Join(warnings, Untranslated("\n")).original);
#    2617                 :            : 
#    2618                 :        188 :     return obj;
#    2619                 :        188 : },
#    2620                 :       1535 :     };
#    2621                 :       1535 : }
#    2622                 :            : 
#    2623                 :            : static RPCHelpMan setwalletflag()
#    2624                 :       1328 : {
#    2625                 :       1328 :             std::string flags = "";
#    2626         [ +  + ]:       1328 :             for (auto& it : WALLET_FLAG_MAP)
#    2627         [ +  + ]:       7968 :                 if (it.second & MUTABLE_WALLET_FLAGS)
#    2628         [ +  - ]:       1328 :                     flags += (flags == "" ? "" : ", ") + it.first;
#    2629                 :            : 
#    2630                 :       1328 :     return RPCHelpMan{"setwalletflag",
#    2631                 :       1328 :                 "\nChange the state of the given wallet flag for a wallet.\n",
#    2632                 :       1328 :                 {
#    2633                 :       1328 :                     {"flag", RPCArg::Type::STR, RPCArg::Optional::NO, "The name of the flag to change. Current available flags: " + flags},
#    2634                 :       1328 :                     {"value", RPCArg::Type::BOOL, RPCArg::Default{true}, "The new state."},
#    2635                 :       1328 :                 },
#    2636                 :       1328 :                 RPCResult{
#    2637                 :       1328 :                     RPCResult::Type::OBJ, "", "",
#    2638                 :       1328 :                     {
#    2639                 :       1328 :                         {RPCResult::Type::STR, "flag_name", "The name of the flag that was modified"},
#    2640                 :       1328 :                         {RPCResult::Type::BOOL, "flag_state", "The new state of the flag"},
#    2641                 :       1328 :                         {RPCResult::Type::STR, "warnings", "Any warnings associated with the change"},
#    2642                 :       1328 :                     }
#    2643                 :       1328 :                 },
#    2644                 :       1328 :                 RPCExamples{
#    2645                 :       1328 :                     HelpExampleCli("setwalletflag", "avoid_reuse")
#    2646                 :       1328 :                   + HelpExampleRpc("setwalletflag", "\"avoid_reuse\"")
#    2647                 :       1328 :                 },
#    2648                 :       1328 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    2649                 :       1328 : {
#    2650                 :         10 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    2651         [ -  + ]:         10 :     if (!pwallet) return NullUniValue;
#    2652                 :            : 
#    2653                 :         10 :     std::string flag_str = request.params[0].get_str();
#    2654 [ +  + ][ +  + ]:         10 :     bool value = request.params[1].isNull() || request.params[1].get_bool();
#    2655                 :            : 
#    2656         [ -  + ]:         10 :     if (!WALLET_FLAG_MAP.count(flag_str)) {
#    2657                 :          0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Unknown wallet flag: %s", flag_str));
#    2658                 :          0 :     }
#    2659                 :            : 
#    2660                 :         10 :     auto flag = WALLET_FLAG_MAP.at(flag_str);
#    2661                 :            : 
#    2662         [ +  + ]:         10 :     if (!(flag & MUTABLE_WALLET_FLAGS)) {
#    2663                 :          4 :         throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Wallet flag is immutable: %s", flag_str));
#    2664                 :          4 :     }
#    2665                 :            : 
#    2666                 :          6 :     UniValue res(UniValue::VOBJ);
#    2667                 :            : 
#    2668         [ +  + ]:          6 :     if (pwallet->IsWalletFlagSet(flag) == value) {
#    2669         [ +  + ]:          4 :         throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Wallet flag is already set to %s: %s", value ? "true" : "false", flag_str));
#    2670                 :          4 :     }
#    2671                 :            : 
#    2672                 :          2 :     res.pushKV("flag_name", flag_str);
#    2673                 :          2 :     res.pushKV("flag_state", value);
#    2674                 :            : 
#    2675         [ +  - ]:          2 :     if (value) {
#    2676                 :          2 :         pwallet->SetWalletFlag(flag);
#    2677                 :          2 :     } else {
#    2678                 :          0 :         pwallet->UnsetWalletFlag(flag);
#    2679                 :          0 :     }
#    2680                 :            : 
#    2681 [ +  - ][ +  - ]:          2 :     if (flag && value && WALLET_FLAG_CAVEATS.count(flag)) {
#         [ +  - ][ +  - ]
#    2682                 :          2 :         res.pushKV("warnings", WALLET_FLAG_CAVEATS.at(flag));
#    2683                 :          2 :     }
#    2684                 :            : 
#    2685                 :          2 :     return res;
#    2686                 :          2 : },
#    2687                 :       1328 :     };
#    2688                 :       1328 : }
#    2689                 :            : 
#    2690                 :            : static RPCHelpMan createwallet()
#    2691                 :       1738 : {
#    2692                 :       1738 :     return RPCHelpMan{
#    2693                 :       1738 :         "createwallet",
#    2694                 :       1738 :         "\nCreates and loads a new wallet.\n",
#    2695                 :       1738 :         {
#    2696                 :       1738 :             {"wallet_name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name for the new wallet. If this is a path, the wallet will be created at the path location."},
#    2697                 :       1738 :             {"disable_private_keys", RPCArg::Type::BOOL, RPCArg::Default{false}, "Disable the possibility of private keys (only watchonlys are possible in this mode)."},
#    2698                 :       1738 :             {"blank", RPCArg::Type::BOOL, RPCArg::Default{false}, "Create a blank wallet. A blank wallet has no keys or HD seed. One can be set using sethdseed."},
#    2699                 :       1738 :             {"passphrase", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "Encrypt the wallet with this passphrase."},
#    2700                 :       1738 :             {"avoid_reuse", RPCArg::Type::BOOL, RPCArg::Default{false}, "Keep track of coin reuse, and treat dirty and clean coins differently with privacy considerations in mind."},
#    2701                 :       1738 :             {"descriptors", RPCArg::Type::BOOL, RPCArg::Default{false}, "Create a native descriptor wallet. The wallet will use descriptors internally to handle address creation"},
#    2702                 :       1738 :             {"load_on_startup", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED_NAMED_ARG, "Save wallet name to persistent settings and load on startup. True to add wallet to startup list, false to remove, null to leave unchanged."},
#    2703                 :       1738 :             {"external_signer", RPCArg::Type::BOOL, RPCArg::Default{false}, "Use an external signer such as a hardware wallet. Requires -signer to be configured. Wallet creation will fail if keys cannot be fetched. Requires disable_private_keys and descriptors set to true."},
#    2704                 :       1738 :         },
#    2705                 :       1738 :         RPCResult{
#    2706                 :       1738 :             RPCResult::Type::OBJ, "", "",
#    2707                 :       1738 :             {
#    2708                 :       1738 :                 {RPCResult::Type::STR, "name", "The wallet name if created successfully. If the wallet was created using a full path, the wallet_name will be the full path."},
#    2709                 :       1738 :                 {RPCResult::Type::STR, "warning", "Warning message if wallet was not loaded cleanly."},
#    2710                 :       1738 :             }
#    2711                 :       1738 :         },
#    2712                 :       1738 :         RPCExamples{
#    2713                 :       1738 :             HelpExampleCli("createwallet", "\"testwallet\"")
#    2714                 :       1738 :             + HelpExampleRpc("createwallet", "\"testwallet\"")
#    2715                 :       1738 :             + HelpExampleCliNamed("createwallet", {{"wallet_name", "descriptors"}, {"avoid_reuse", true}, {"descriptors", true}, {"load_on_startup", true}})
#    2716                 :       1738 :             + HelpExampleRpcNamed("createwallet", {{"wallet_name", "descriptors"}, {"avoid_reuse", true}, {"descriptors", true}, {"load_on_startup", true}})
#    2717                 :       1738 :         },
#    2718                 :       1738 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    2719                 :       1738 : {
#    2720                 :        420 :     WalletContext& context = EnsureWalletContext(request.context);
#    2721                 :        420 :     uint64_t flags = 0;
#    2722 [ +  + ][ +  + ]:        420 :     if (!request.params[1].isNull() && request.params[1].get_bool()) {
#    2723                 :         43 :         flags |= WALLET_FLAG_DISABLE_PRIVATE_KEYS;
#    2724                 :         43 :     }
#    2725                 :            : 
#    2726 [ +  + ][ +  + ]:        420 :     if (!request.params[2].isNull() && request.params[2].get_bool()) {
#    2727                 :         38 :         flags |= WALLET_FLAG_BLANK_WALLET;
#    2728                 :         38 :     }
#    2729                 :        420 :     SecureString passphrase;
#    2730                 :        420 :     passphrase.reserve(100);
#    2731                 :        420 :     std::vector<bilingual_str> warnings;
#    2732         [ +  - ]:        420 :     if (!request.params[3].isNull()) {
#    2733                 :        420 :         passphrase = request.params[3].get_str().c_str();
#    2734         [ +  + ]:        420 :         if (passphrase.empty()) {
#    2735                 :            :             // Empty string means unencrypted
#    2736                 :        410 :             warnings.emplace_back(Untranslated("Empty string given as passphrase, wallet will not be encrypted."));
#    2737                 :        410 :         }
#    2738                 :        420 :     }
#    2739                 :            : 
#    2740 [ +  + ][ +  + ]:        420 :     if (!request.params[4].isNull() && request.params[4].get_bool()) {
#    2741                 :          3 :         flags |= WALLET_FLAG_AVOID_REUSE;
#    2742                 :          3 :     }
#    2743 [ +  - ][ +  + ]:        420 :     if (!request.params[5].isNull() && request.params[5].get_bool()) {
#    2744                 :            : #ifndef USE_SQLITE
#    2745                 :            :         throw JSONRPCError(RPC_WALLET_ERROR, "Compiled without sqlite support (required for descriptor wallets)");
#    2746                 :            : #endif
#    2747                 :        152 :         flags |= WALLET_FLAG_DESCRIPTORS;
#    2748                 :        152 :         warnings.emplace_back(Untranslated("Wallet is an experimental descriptor wallet"));
#    2749                 :        152 :     }
#    2750 [ -  + ][ #  # ]:        420 :     if (!request.params[7].isNull() && request.params[7].get_bool()) {
#    2751                 :            : #ifdef ENABLE_EXTERNAL_SIGNER
#    2752                 :            :         flags |= WALLET_FLAG_EXTERNAL_SIGNER;
#    2753                 :            : #else
#    2754                 :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Compiled without external signing support (required for external signing)");
#    2755                 :          0 : #endif
#    2756                 :          0 :     }
#    2757                 :            : 
#    2758                 :            : #ifndef USE_BDB
#    2759                 :            :     if (!(flags & WALLET_FLAG_DESCRIPTORS)) {
#    2760                 :            :         throw JSONRPCError(RPC_WALLET_ERROR, "Compiled without bdb support (required for legacy wallets)");
#    2761                 :            :     }
#    2762                 :            : #endif
#    2763                 :            : 
#    2764                 :        420 :     DatabaseOptions options;
#    2765                 :        420 :     DatabaseStatus status;
#    2766                 :        420 :     options.require_create = true;
#    2767                 :        420 :     options.create_flags = flags;
#    2768                 :        420 :     options.create_passphrase = passphrase;
#    2769                 :        420 :     bilingual_str error;
#    2770         [ +  + ]:        420 :     std::optional<bool> load_on_start = request.params[6].isNull() ? std::nullopt : std::optional<bool>(request.params[6].get_bool());
#    2771                 :        420 :     std::shared_ptr<CWallet> wallet = CreateWallet(*context.chain, request.params[0].get_str(), load_on_start, options, status, error, warnings);
#    2772         [ +  + ]:        420 :     if (!wallet) {
#    2773         [ -  + ]:          7 :         RPCErrorCode code = status == DatabaseStatus::FAILED_ENCRYPT ? RPC_WALLET_ENCRYPTION_FAILED : RPC_WALLET_ERROR;
#    2774                 :          7 :         throw JSONRPCError(code, error.original);
#    2775                 :          7 :     }
#    2776                 :            : 
#    2777                 :        413 :     UniValue obj(UniValue::VOBJ);
#    2778                 :        413 :     obj.pushKV("name", wallet->GetName());
#    2779                 :        413 :     obj.pushKV("warning", Join(warnings, Untranslated("\n")).original);
#    2780                 :            : 
#    2781                 :        413 :     return obj;
#    2782                 :        413 : },
#    2783                 :       1738 :     };
#    2784                 :       1738 : }
#    2785                 :            : 
#    2786                 :            : static RPCHelpMan unloadwallet()
#    2787                 :       1487 : {
#    2788                 :       1487 :     return RPCHelpMan{"unloadwallet",
#    2789                 :       1487 :                 "Unloads the wallet referenced by the request endpoint otherwise unloads the wallet specified in the argument.\n"
#    2790                 :       1487 :                 "Specifying the wallet name on a wallet endpoint is invalid.",
#    2791                 :       1487 :                 {
#    2792                 :       1487 :                     {"wallet_name", RPCArg::Type::STR, RPCArg::DefaultHint{"the wallet name from the RPC endpoint"}, "The name of the wallet to unload. If provided both here and in the RPC endpoint, the two must be identical."},
#    2793                 :       1487 :                     {"load_on_startup", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED_NAMED_ARG, "Save wallet name to persistent settings and load on startup. True to add wallet to startup list, false to remove, null to leave unchanged."},
#    2794                 :       1487 :                 },
#    2795                 :       1487 :                 RPCResult{RPCResult::Type::OBJ, "", "", {
#    2796                 :       1487 :                     {RPCResult::Type::STR, "warning", "Warning message if wallet was not unloaded cleanly."},
#    2797                 :       1487 :                 }},
#    2798                 :       1487 :                 RPCExamples{
#    2799                 :       1487 :                     HelpExampleCli("unloadwallet", "wallet_name")
#    2800                 :       1487 :             + HelpExampleRpc("unloadwallet", "wallet_name")
#    2801                 :       1487 :                 },
#    2802                 :       1487 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    2803                 :       1487 : {
#    2804                 :        169 :     std::string wallet_name;
#    2805         [ +  + ]:        169 :     if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
#    2806 [ +  + ][ +  + ]:         49 :         if (!(request.params[0].isNull() || request.params[0].get_str() == wallet_name)) {
#    2807                 :          3 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "RPC endpoint wallet and wallet_name parameter specify different wallets");
#    2808                 :          3 :         }
#    2809                 :        120 :     } else {
#    2810                 :        120 :         wallet_name = request.params[0].get_str();
#    2811                 :        120 :     }
#    2812                 :            : 
#    2813                 :        169 :     std::shared_ptr<CWallet> wallet = GetWallet(wallet_name);
#    2814         [ +  + ]:        166 :     if (!wallet) {
#    2815                 :          6 :         throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
#    2816                 :          6 :     }
#    2817                 :            : 
#    2818                 :            :     // Release the "main" shared pointer and prevent further notifications.
#    2819                 :            :     // Note that any attempt to load the same wallet would fail until the wallet
#    2820                 :            :     // is destroyed (see CheckUniqueFileid).
#    2821                 :        160 :     std::vector<bilingual_str> warnings;
#    2822         [ +  + ]:        160 :     std::optional<bool> load_on_start = request.params[1].isNull() ? std::nullopt : std::optional<bool>(request.params[1].get_bool());
#    2823         [ -  + ]:        160 :     if (!RemoveWallet(wallet, load_on_start, warnings)) {
#    2824                 :          0 :         throw JSONRPCError(RPC_MISC_ERROR, "Requested wallet already unloaded");
#    2825                 :          0 :     }
#    2826                 :            : 
#    2827                 :        160 :     UnloadWallet(std::move(wallet));
#    2828                 :            : 
#    2829                 :        160 :     UniValue result(UniValue::VOBJ);
#    2830                 :        160 :     result.pushKV("warning", Join(warnings, Untranslated("\n")).original);
#    2831                 :        160 :     return result;
#    2832                 :        160 : },
#    2833                 :       1487 :     };
#    2834                 :       1487 : }
#    2835                 :            : 
#    2836                 :            : static RPCHelpMan listunspent()
#    2837                 :       1828 : {
#    2838                 :       1828 :     return RPCHelpMan{
#    2839                 :       1828 :                 "listunspent",
#    2840                 :       1828 :                 "\nReturns array of unspent transaction outputs\n"
#    2841                 :       1828 :                 "with between minconf and maxconf (inclusive) confirmations.\n"
#    2842                 :       1828 :                 "Optionally filter to only include txouts paid to specified addresses.\n",
#    2843                 :       1828 :                 {
#    2844                 :       1828 :                     {"minconf", RPCArg::Type::NUM, RPCArg::Default{1}, "The minimum confirmations to filter"},
#    2845                 :       1828 :                     {"maxconf", RPCArg::Type::NUM, RPCArg::Default{9999999}, "The maximum confirmations to filter"},
#    2846                 :       1828 :                     {"addresses", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "The bitcoin addresses to filter",
#    2847                 :       1828 :                         {
#    2848                 :       1828 :                             {"address", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "bitcoin address"},
#    2849                 :       1828 :                         },
#    2850                 :       1828 :                     },
#    2851                 :       1828 :                     {"include_unsafe", RPCArg::Type::BOOL, RPCArg::Default{true}, "Include outputs that are not safe to spend\n"
#    2852                 :       1828 :                               "See description of \"safe\" attribute below."},
#    2853                 :       1828 :                     {"query_options", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED_NAMED_ARG, "JSON with query options",
#    2854                 :       1828 :                         {
#    2855                 :       1828 :                             {"minimumAmount", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(0)}, "Minimum value of each UTXO in " + CURRENCY_UNIT + ""},
#    2856                 :       1828 :                             {"maximumAmount", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"unlimited"}, "Maximum value of each UTXO in " + CURRENCY_UNIT + ""},
#    2857                 :       1828 :                             {"maximumCount", RPCArg::Type::NUM, RPCArg::DefaultHint{"unlimited"}, "Maximum number of UTXOs"},
#    2858                 :       1828 :                             {"minimumSumAmount", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"unlimited"}, "Minimum sum value of all UTXOs in " + CURRENCY_UNIT + ""},
#    2859                 :       1828 :                         },
#    2860                 :       1828 :                         "query_options"},
#    2861                 :       1828 :                 },
#    2862                 :       1828 :                 RPCResult{
#    2863                 :       1828 :                     RPCResult::Type::ARR, "", "",
#    2864                 :       1828 :                     {
#    2865                 :       1828 :                         {RPCResult::Type::OBJ, "", "",
#    2866                 :       1828 :                         {
#    2867                 :       1828 :                             {RPCResult::Type::STR_HEX, "txid", "the transaction id"},
#    2868                 :       1828 :                             {RPCResult::Type::NUM, "vout", "the vout value"},
#    2869                 :       1828 :                             {RPCResult::Type::STR, "address", "the bitcoin address"},
#    2870                 :       1828 :                             {RPCResult::Type::STR, "label", "The associated label, or \"\" for the default label"},
#    2871                 :       1828 :                             {RPCResult::Type::STR, "scriptPubKey", "the script key"},
#    2872                 :       1828 :                             {RPCResult::Type::STR_AMOUNT, "amount", "the transaction output amount in " + CURRENCY_UNIT},
#    2873                 :       1828 :                             {RPCResult::Type::NUM, "confirmations", "The number of confirmations"},
#    2874                 :       1828 :                             {RPCResult::Type::STR_HEX, "redeemScript", "The redeemScript if scriptPubKey is P2SH"},
#    2875                 :       1828 :                             {RPCResult::Type::STR, "witnessScript", "witnessScript if the scriptPubKey is P2WSH or P2SH-P2WSH"},
#    2876                 :       1828 :                             {RPCResult::Type::BOOL, "spendable", "Whether we have the private keys to spend this output"},
#    2877                 :       1828 :                             {RPCResult::Type::BOOL, "solvable", "Whether we know how to spend this output, ignoring the lack of keys"},
#    2878                 :       1828 :                             {RPCResult::Type::BOOL, "reused", "(only present if avoid_reuse is set) Whether this output is reused/dirty (sent to an address that was previously spent from)"},
#    2879                 :       1828 :                             {RPCResult::Type::STR, "desc", "(only when solvable) A descriptor for spending this output"},
#    2880                 :       1828 :                             {RPCResult::Type::BOOL, "safe", "Whether this output is considered safe to spend. Unconfirmed transactions\n"
#    2881                 :       1828 :                                                             "from outside keys and unconfirmed replacement transactions are considered unsafe\n"
#    2882                 :       1828 :                                                             "and are not eligible for spending by fundrawtransaction and sendtoaddress."},
#    2883                 :       1828 :                         }},
#    2884                 :       1828 :                     }
#    2885                 :       1828 :                 },
#    2886                 :       1828 :                 RPCExamples{
#    2887                 :       1828 :                     HelpExampleCli("listunspent", "")
#    2888                 :       1828 :             + HelpExampleCli("listunspent", "6 9999999 \"[\\\"" + EXAMPLE_ADDRESS[0] + "\\\",\\\"" + EXAMPLE_ADDRESS[1] + "\\\"]\"")
#    2889                 :       1828 :             + HelpExampleRpc("listunspent", "6, 9999999 \"[\\\"" + EXAMPLE_ADDRESS[0] + "\\\",\\\"" + EXAMPLE_ADDRESS[1] + "\\\"]\"")
#    2890                 :       1828 :             + HelpExampleCli("listunspent", "6 9999999 '[]' true '{ \"minimumAmount\": 0.005 }'")
#    2891                 :       1828 :             + HelpExampleRpc("listunspent", "6, 9999999, [] , true, { \"minimumAmount\": 0.005 } ")
#    2892                 :       1828 :                 },
#    2893                 :       1828 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    2894                 :       1828 : {
#    2895                 :        510 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    2896         [ -  + ]:        510 :     if (!pwallet) return NullUniValue;
#    2897                 :            : 
#    2898                 :        510 :     int nMinDepth = 1;
#    2899         [ +  + ]:        510 :     if (!request.params[0].isNull()) {
#    2900                 :         67 :         RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
#    2901                 :         67 :         nMinDepth = request.params[0].get_int();
#    2902                 :         67 :     }
#    2903                 :            : 
#    2904                 :        510 :     int nMaxDepth = 9999999;
#    2905         [ +  + ]:        510 :     if (!request.params[1].isNull()) {
#    2906                 :         14 :         RPCTypeCheckArgument(request.params[1], UniValue::VNUM);
#    2907                 :         14 :         nMaxDepth = request.params[1].get_int();
#    2908                 :         14 :     }
#    2909                 :            : 
#    2910                 :        510 :     std::set<CTxDestination> destinations;
#    2911         [ +  + ]:        510 :     if (!request.params[2].isNull()) {
#    2912                 :          4 :         RPCTypeCheckArgument(request.params[2], UniValue::VARR);
#    2913                 :          4 :         UniValue inputs = request.params[2].get_array();
#    2914         [ +  + ]:          8 :         for (unsigned int idx = 0; idx < inputs.size(); idx++) {
#    2915                 :          4 :             const UniValue& input = inputs[idx];
#    2916                 :          4 :             CTxDestination dest = DecodeDestination(input.get_str());
#    2917         [ -  + ]:          4 :             if (!IsValidDestination(dest)) {
#    2918                 :          0 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ") + input.get_str());
#    2919                 :          0 :             }
#    2920         [ -  + ]:          4 :             if (!destinations.insert(dest).second) {
#    2921                 :          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ") + input.get_str());
#    2922                 :          0 :             }
#    2923                 :          4 :         }
#    2924                 :          4 :     }
#    2925                 :            : 
#    2926                 :        510 :     bool include_unsafe = true;
#    2927         [ +  + ]:        510 :     if (!request.params[3].isNull()) {
#    2928                 :          6 :         RPCTypeCheckArgument(request.params[3], UniValue::VBOOL);
#    2929                 :          6 :         include_unsafe = request.params[3].get_bool();
#    2930                 :          6 :     }
#    2931                 :            : 
#    2932                 :        510 :     CAmount nMinimumAmount = 0;
#    2933                 :        510 :     CAmount nMaximumAmount = MAX_MONEY;
#    2934                 :        510 :     CAmount nMinimumSumAmount = MAX_MONEY;
#    2935                 :        510 :     uint64_t nMaximumCount = 0;
#    2936                 :            : 
#    2937         [ +  + ]:        510 :     if (!request.params[4].isNull()) {
#    2938                 :         83 :         const UniValue& options = request.params[4].get_obj();
#    2939                 :            : 
#    2940                 :         83 :         RPCTypeCheckObj(options,
#    2941                 :         83 :             {
#    2942                 :         83 :                 {"minimumAmount", UniValueType()},
#    2943                 :         83 :                 {"maximumAmount", UniValueType()},
#    2944                 :         83 :                 {"minimumSumAmount", UniValueType()},
#    2945                 :         83 :                 {"maximumCount", UniValueType(UniValue::VNUM)},
#    2946                 :         83 :             },
#    2947                 :         83 :             true, true);
#    2948                 :            : 
#    2949         [ +  - ]:         83 :         if (options.exists("minimumAmount"))
#    2950                 :         83 :             nMinimumAmount = AmountFromValue(options["minimumAmount"]);
#    2951                 :            : 
#    2952         [ -  + ]:         83 :         if (options.exists("maximumAmount"))
#    2953                 :          0 :             nMaximumAmount = AmountFromValue(options["maximumAmount"]);
#    2954                 :            : 
#    2955         [ -  + ]:         83 :         if (options.exists("minimumSumAmount"))
#    2956                 :          0 :             nMinimumSumAmount = AmountFromValue(options["minimumSumAmount"]);
#    2957                 :            : 
#    2958         [ -  + ]:         83 :         if (options.exists("maximumCount"))
#    2959                 :          0 :             nMaximumCount = options["maximumCount"].get_int64();
#    2960                 :         83 :     }
#    2961                 :            : 
#    2962                 :            :     // Make sure the results are valid at least up to the most recent block
#    2963                 :            :     // the user could have gotten from another RPC command prior to now
#    2964                 :        510 :     pwallet->BlockUntilSyncedToCurrentChain();
#    2965                 :            : 
#    2966                 :        510 :     UniValue results(UniValue::VARR);
#    2967                 :        510 :     std::vector<COutput> vecOutputs;
#    2968                 :        510 :     {
#    2969                 :        510 :         CCoinControl cctl;
#    2970                 :        510 :         cctl.m_avoid_address_reuse = false;
#    2971                 :        510 :         cctl.m_min_depth = nMinDepth;
#    2972                 :        510 :         cctl.m_max_depth = nMaxDepth;
#    2973                 :        510 :         cctl.m_include_unsafe_inputs = include_unsafe;
#    2974                 :        510 :         LOCK(pwallet->cs_wallet);
#    2975                 :        510 :         pwallet->AvailableCoins(vecOutputs, &cctl, nMinimumAmount, nMaximumAmount, nMinimumSumAmount, nMaximumCount);
#    2976                 :        510 :     }
#    2977                 :            : 
#    2978                 :        510 :     LOCK(pwallet->cs_wallet);
#    2979                 :            : 
#    2980                 :        510 :     const bool avoid_reuse = pwallet->IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE);
#    2981                 :            : 
#    2982         [ +  + ]:      43429 :     for (const COutput& out : vecOutputs) {
#    2983                 :      43429 :         CTxDestination address;
#    2984                 :      43429 :         const CScript& scriptPubKey = out.tx->tx->vout[out.i].scriptPubKey;
#    2985                 :      43429 :         bool fValidAddress = ExtractDestination(scriptPubKey, address);
#    2986 [ +  + ][ +  + ]:      43429 :         bool reused = avoid_reuse && pwallet->IsSpentKey(out.tx->GetHash(), out.i);
#    2987                 :            : 
#    2988 [ +  + ][ -  + ]:      43429 :         if (destinations.size() && (!fValidAddress || !destinations.count(address)))
#                 [ +  + ]
#    2989                 :        363 :             continue;
#    2990                 :            : 
#    2991                 :      43066 :         UniValue entry(UniValue::VOBJ);
#    2992                 :      43066 :         entry.pushKV("txid", out.tx->GetHash().GetHex());
#    2993                 :      43066 :         entry.pushKV("vout", out.i);
#    2994                 :            : 
#    2995         [ +  + ]:      43066 :         if (fValidAddress) {
#    2996                 :      42879 :             entry.pushKV("address", EncodeDestination(address));
#    2997                 :            : 
#    2998                 :      42879 :             const auto* address_book_entry = pwallet->FindAddressBookEntry(address);
#    2999         [ +  + ]:      42879 :             if (address_book_entry) {
#    3000                 :      37644 :                 entry.pushKV("label", address_book_entry->GetLabel());
#    3001                 :      37644 :             }
#    3002                 :            : 
#    3003                 :      42879 :             std::unique_ptr<SigningProvider> provider = pwallet->GetSolvingProvider(scriptPubKey);
#    3004         [ +  - ]:      42879 :             if (provider) {
#    3005         [ +  + ]:      42879 :                 if (scriptPubKey.IsPayToScriptHash()) {
#    3006                 :       1955 :                     const CScriptID& hash = CScriptID(std::get<ScriptHash>(address));
#    3007                 :       1955 :                     CScript redeemScript;
#    3008         [ +  + ]:       1955 :                     if (provider->GetCScript(hash, redeemScript)) {
#    3009                 :       1929 :                         entry.pushKV("redeemScript", HexStr(redeemScript));
#    3010                 :            :                         // Now check if the redeemScript is actually a P2WSH script
#    3011                 :       1929 :                         CTxDestination witness_destination;
#    3012         [ +  + ]:       1929 :                         if (redeemScript.IsPayToWitnessScriptHash()) {
#    3013                 :        338 :                             bool extracted = ExtractDestination(redeemScript, witness_destination);
#    3014         [ -  + ]:        338 :                             CHECK_NONFATAL(extracted);
#    3015                 :            :                             // Also return the witness script
#    3016                 :        338 :                             const WitnessV0ScriptHash& whash = std::get<WitnessV0ScriptHash>(witness_destination);
#    3017                 :        338 :                             CScriptID id;
#    3018                 :        338 :                             CRIPEMD160().Write(whash.begin(), whash.size()).Finalize(id.begin());
#    3019                 :        338 :                             CScript witnessScript;
#    3020         [ +  - ]:        338 :                             if (provider->GetCScript(id, witnessScript)) {
#    3021                 :        338 :                                 entry.pushKV("witnessScript", HexStr(witnessScript));
#    3022                 :        338 :                             }
#    3023                 :        338 :                         }
#    3024                 :       1929 :                     }
#    3025         [ +  + ]:      40924 :                 } else if (scriptPubKey.IsPayToWitnessScriptHash()) {
#    3026                 :        485 :                     const WitnessV0ScriptHash& whash = std::get<WitnessV0ScriptHash>(address);
#    3027                 :        485 :                     CScriptID id;
#    3028                 :        485 :                     CRIPEMD160().Write(whash.begin(), whash.size()).Finalize(id.begin());
#    3029                 :        485 :                     CScript witnessScript;
#    3030         [ +  + ]:        485 :                     if (provider->GetCScript(id, witnessScript)) {
#    3031                 :        484 :                         entry.pushKV("witnessScript", HexStr(witnessScript));
#    3032                 :        484 :                     }
#    3033                 :        485 :                 }
#    3034                 :      42879 :             }
#    3035                 :      42879 :         }
#    3036                 :            : 
#    3037                 :      43066 :         entry.pushKV("scriptPubKey", HexStr(scriptPubKey));
#    3038                 :      43066 :         entry.pushKV("amount", ValueFromAmount(out.tx->tx->vout[out.i].nValue));
#    3039                 :      43066 :         entry.pushKV("confirmations", out.nDepth);
#    3040                 :      43066 :         entry.pushKV("spendable", out.fSpendable);
#    3041                 :      43066 :         entry.pushKV("solvable", out.fSolvable);
#    3042         [ +  + ]:      43066 :         if (out.fSolvable) {
#    3043                 :      42938 :             std::unique_ptr<SigningProvider> provider = pwallet->GetSolvingProvider(scriptPubKey);
#    3044         [ +  - ]:      42938 :             if (provider) {
#    3045                 :      42938 :                 auto descriptor = InferDescriptor(scriptPubKey, *provider);
#    3046                 :      42938 :                 entry.pushKV("desc", descriptor->ToString());
#    3047                 :      42938 :             }
#    3048                 :      42938 :         }
#    3049         [ +  + ]:      43066 :         if (avoid_reuse) entry.pushKV("reused", reused);
#    3050                 :      43066 :         entry.pushKV("safe", out.fSafe);
#    3051                 :      43066 :         results.push_back(entry);
#    3052                 :      43066 :     }
#    3053                 :            : 
#    3054                 :        510 :     return results;
#    3055                 :        510 : },
#    3056                 :       1828 :     };
#    3057                 :       1828 : }
#    3058                 :            : 
#    3059                 :            : void FundTransaction(CWallet& wallet, CMutableTransaction& tx, CAmount& fee_out, int& change_position, const UniValue& options, CCoinControl& coinControl, bool override_min_fee)
#    3060                 :        713 : {
#    3061                 :            :     // Make sure the results are valid at least up to the most recent block
#    3062                 :            :     // the user could have gotten from another RPC command prior to now
#    3063                 :        713 :     wallet.BlockUntilSyncedToCurrentChain();
#    3064                 :            : 
#    3065                 :        713 :     change_position = -1;
#    3066                 :        713 :     bool lockUnspents = false;
#    3067                 :        713 :     UniValue subtractFeeFromOutputs;
#    3068                 :        713 :     std::set<int> setSubtractFeeFromOutputs;
#    3069                 :            : 
#    3070         [ +  + ]:        713 :     if (!options.isNull()) {
#    3071         [ +  + ]:        615 :       if (options.type() == UniValue::VBOOL) {
#    3072                 :            :         // backward compatibility bool only fallback
#    3073                 :          2 :         coinControl.fAllowWatchOnly = options.get_bool();
#    3074                 :          2 :       }
#    3075                 :        613 :       else {
#    3076                 :        613 :         RPCTypeCheckArgument(options, UniValue::VOBJ);
#    3077                 :        613 :         RPCTypeCheckObj(options,
#    3078                 :        613 :             {
#    3079                 :        613 :                 {"add_inputs", UniValueType(UniValue::VBOOL)},
#    3080                 :        613 :                 {"include_unsafe", UniValueType(UniValue::VBOOL)},
#    3081                 :        613 :                 {"add_to_wallet", UniValueType(UniValue::VBOOL)},
#    3082                 :        613 :                 {"changeAddress", UniValueType(UniValue::VSTR)},
#    3083                 :        613 :                 {"change_address", UniValueType(UniValue::VSTR)},
#    3084                 :        613 :                 {"changePosition", UniValueType(UniValue::VNUM)},
#    3085                 :        613 :                 {"change_position", UniValueType(UniValue::VNUM)},
#    3086                 :        613 :                 {"change_type", UniValueType(UniValue::VSTR)},
#    3087                 :        613 :                 {"includeWatching", UniValueType(UniValue::VBOOL)},
#    3088                 :        613 :                 {"include_watching", UniValueType(UniValue::VBOOL)},
#    3089                 :        613 :                 {"inputs", UniValueType(UniValue::VARR)},
#    3090                 :        613 :                 {"lockUnspents", UniValueType(UniValue::VBOOL)},
#    3091                 :        613 :                 {"lock_unspents", UniValueType(UniValue::VBOOL)},
#    3092                 :        613 :                 {"locktime", UniValueType(UniValue::VNUM)},
#    3093                 :        613 :                 {"fee_rate", UniValueType()}, // will be checked by AmountFromValue() in SetFeeEstimateMode()
#    3094                 :        613 :                 {"feeRate", UniValueType()}, // will be checked by AmountFromValue() below
#    3095                 :        613 :                 {"psbt", UniValueType(UniValue::VBOOL)},
#    3096                 :        613 :                 {"subtractFeeFromOutputs", UniValueType(UniValue::VARR)},
#    3097                 :        613 :                 {"subtract_fee_from_outputs", UniValueType(UniValue::VARR)},
#    3098                 :        613 :                 {"replaceable", UniValueType(UniValue::VBOOL)},
#    3099                 :        613 :                 {"conf_target", UniValueType(UniValue::VNUM)},
#    3100                 :        613 :                 {"estimate_mode", UniValueType(UniValue::VSTR)},
#    3101                 :        613 :             },
#    3102                 :        613 :             true, true);
#    3103                 :            : 
#    3104         [ +  + ]:        613 :         if (options.exists("add_inputs") ) {
#    3105                 :        240 :             coinControl.m_add_inputs = options["add_inputs"].get_bool();
#    3106                 :        240 :         }
#    3107                 :            : 
#    3108 [ +  + ][ +  + ]:        613 :         if (options.exists("changeAddress") || options.exists("change_address")) {
#                 [ +  + ]
#    3109         [ +  + ]:         32 :             const std::string change_address_str = (options.exists("change_address") ? options["change_address"] : options["changeAddress"]).get_str();
#    3110                 :         32 :             CTxDestination dest = DecodeDestination(change_address_str);
#    3111                 :            : 
#    3112         [ +  + ]:         32 :             if (!IsValidDestination(dest)) {
#    3113                 :          4 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Change address must be a valid bitcoin address");
#    3114                 :          4 :             }
#    3115                 :            : 
#    3116                 :         28 :             coinControl.destChange = dest;
#    3117                 :         28 :         }
#    3118                 :            : 
#    3119 [ +  + ][ +  + ]:        613 :         if (options.exists("changePosition") || options.exists("change_position")) {
#                 [ +  + ]
#    3120         [ +  + ]:         12 :             change_position = (options.exists("change_position") ? options["change_position"] : options["changePosition"]).get_int();
#    3121                 :         12 :         }
#    3122                 :            : 
#    3123         [ +  + ]:        609 :         if (options.exists("change_type")) {
#    3124 [ +  + ][ +  + ]:         12 :             if (options.exists("changeAddress") || options.exists("change_address")) {
#                 [ -  + ]
#    3125                 :          2 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot specify both change address and address type options");
#    3126                 :          2 :             }
#    3127                 :         10 :             OutputType out_type;
#    3128         [ +  + ]:         10 :             if (!ParseOutputType(options["change_type"].get_str(), out_type)) {
#    3129                 :          2 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Unknown change type '%s'", options["change_type"].get_str()));
#    3130                 :          2 :             }
#    3131                 :          8 :             coinControl.m_change_type.emplace(out_type);
#    3132                 :          8 :         }
#    3133                 :            : 
#    3134         [ +  + ]:        609 :         const UniValue include_watching_option = options.exists("include_watching") ? options["include_watching"] : options["includeWatching"];
#    3135                 :        605 :         coinControl.fAllowWatchOnly = ParseIncludeWatchonly(include_watching_option, wallet);
#    3136                 :            : 
#    3137 [ +  + ][ +  + ]:        605 :         if (options.exists("lockUnspents") || options.exists("lock_unspents")) {
#                 [ +  + ]
#    3138         [ +  + ]:          8 :             lockUnspents = (options.exists("lock_unspents") ? options["lock_unspents"] : options["lockUnspents"]).get_bool();
#    3139                 :          8 :         }
#    3140                 :            : 
#    3141         [ +  + ]:        605 :         if (options.exists("include_unsafe")) {
#    3142                 :          8 :             coinControl.m_include_unsafe_inputs = options["include_unsafe"].get_bool();
#    3143                 :          8 :         }
#    3144                 :            : 
#    3145         [ +  + ]:        605 :         if (options.exists("feeRate")) {
#    3146         [ +  + ]:        105 :             if (options.exists("fee_rate")) {
#    3147                 :          4 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot specify both fee_rate (" + CURRENCY_ATOM + "/vB) and feeRate (" + CURRENCY_UNIT + "/kvB)");
#    3148                 :          4 :             }
#    3149         [ +  + ]:        101 :             if (options.exists("conf_target")) {
#    3150                 :          4 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot specify both conf_target and feeRate. Please provide either a confirmation target in blocks for automatic fee estimation, or an explicit fee rate.");
#    3151                 :          4 :             }
#    3152         [ +  + ]:         97 :             if (options.exists("estimate_mode")) {
#    3153                 :          4 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot specify both estimate_mode and feeRate");
#    3154                 :          4 :             }
#    3155                 :         93 :             coinControl.m_feerate = CFeeRate(AmountFromValue(options["feeRate"]));
#    3156                 :         93 :             coinControl.fOverrideFeeRate = true;
#    3157                 :         93 :         }
#    3158                 :            : 
#    3159 [ +  + ][ +  + ]:        605 :         if (options.exists("subtractFeeFromOutputs") || options.exists("subtract_fee_from_outputs") )
#                 [ +  + ]
#    3160         [ +  + ]:         32 :             subtractFeeFromOutputs = (options.exists("subtract_fee_from_outputs") ? options["subtract_fee_from_outputs"] : options["subtractFeeFromOutputs"]).get_array();
#    3161                 :            : 
#    3162         [ +  + ]:        593 :         if (options.exists("replaceable")) {
#    3163                 :         10 :             coinControl.m_signal_bip125_rbf = options["replaceable"].get_bool();
#    3164                 :         10 :         }
#    3165                 :        593 :         SetFeeEstimateMode(wallet, coinControl, options["conf_target"], options["estimate_mode"], options["fee_rate"], override_min_fee);
#    3166                 :        593 :       }
#    3167                 :        615 :     } else {
#    3168                 :            :         // if options is null and not a bool
#    3169                 :         98 :         coinControl.fAllowWatchOnly = ParseIncludeWatchonly(NullUniValue, wallet);
#    3170                 :         98 :     }
#    3171                 :            : 
#    3172         [ -  + ]:        713 :     if (tx.vout.size() == 0)
#    3173                 :          0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "TX must have at least one output");
#    3174                 :            : 
#    3175 [ +  + ][ -  + ]:        693 :     if (change_position != -1 && (change_position < 0 || (unsigned int)change_position > tx.vout.size()))
#                 [ +  + ]
#    3176                 :          2 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "changePosition out of bounds");
#    3177                 :            : 
#    3178         [ +  + ]:        723 :     for (unsigned int idx = 0; idx < subtractFeeFromOutputs.size(); idx++) {
#    3179                 :         32 :         int pos = subtractFeeFromOutputs[idx].get_int();
#    3180         [ -  + ]:         32 :         if (setSubtractFeeFromOutputs.count(pos))
#    3181                 :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid parameter, duplicated position: %d", pos));
#    3182         [ -  + ]:         32 :         if (pos < 0)
#    3183                 :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid parameter, negative position: %d", pos));
#    3184         [ -  + ]:         32 :         if (pos >= int(tx.vout.size()))
#    3185                 :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid parameter, position too large: %d", pos));
#    3186                 :         32 :         setSubtractFeeFromOutputs.insert(pos);
#    3187                 :         32 :     }
#    3188                 :            : 
#    3189                 :        691 :     bilingual_str error;
#    3190                 :            : 
#    3191         [ +  + ]:        691 :     if (!wallet.FundTransaction(tx, fee_out, change_position, error, lockUnspents, setSubtractFeeFromOutputs, coinControl)) {
#    3192                 :         85 :         throw JSONRPCError(RPC_WALLET_ERROR, error.original);
#    3193                 :         85 :     }
#    3194                 :        691 : }
#    3195                 :            : 
#    3196                 :            : static RPCHelpMan fundrawtransaction()
#    3197                 :       1607 : {
#    3198                 :       1607 :     return RPCHelpMan{"fundrawtransaction",
#    3199                 :       1607 :                 "\nIf the transaction has no inputs, they will be automatically selected to meet its out value.\n"
#    3200                 :       1607 :                 "It will add at most one change output to the outputs.\n"
#    3201                 :       1607 :                 "No existing outputs will be modified unless \"subtractFeeFromOutputs\" is specified.\n"
#    3202                 :       1607 :                 "Note that inputs which were signed may need to be resigned after completion since in/outputs have been added.\n"
#    3203                 :       1607 :                 "The inputs added will not be signed, use signrawtransactionwithkey\n"
#    3204                 :       1607 :                 " or signrawtransactionwithwallet for that.\n"
#    3205                 :       1607 :                 "Note that all existing inputs must have their previous output transaction be in the wallet.\n"
#    3206                 :       1607 :                 "Note that all inputs selected must be of standard form and P2SH scripts must be\n"
#    3207                 :       1607 :                 "in the wallet using importaddress or addmultisigaddress (to calculate fees).\n"
#    3208                 :       1607 :                 "You can see whether this is the case by checking the \"solvable\" field in the listunspent output.\n"
#    3209                 :       1607 :                 "Only pay-to-pubkey, multisig, and P2SH versions thereof are currently supported for watch-only\n",
#    3210                 :       1607 :                 {
#    3211                 :       1607 :                     {"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex string of the raw transaction"},
#    3212                 :       1607 :                     {"options", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED_NAMED_ARG, "for backward compatibility: passing in a true instead of an object will result in {\"includeWatching\":true}",
#    3213                 :       1607 :                         {
#    3214                 :       1607 :                             {"add_inputs", RPCArg::Type::BOOL, RPCArg::Default{true}, "For a transaction with existing inputs, automatically include more if they are not enough."},
#    3215                 :       1607 :                             {"include_unsafe", RPCArg::Type::BOOL, RPCArg::Default{false}, "Include inputs that are not safe to spend (unconfirmed transactions from outside keys and unconfirmed replacement transactions).\n"
#    3216                 :       1607 :                                                           "Warning: the resulting transaction may become invalid if one of the unsafe inputs disappears.\n"
#    3217                 :       1607 :                                                           "If that happens, you will need to fund the transaction with different inputs and republish it."},
#    3218                 :       1607 :                             {"changeAddress", RPCArg::Type::STR, RPCArg::DefaultHint{"pool address"}, "The bitcoin address to receive the change"},
#    3219                 :       1607 :                             {"changePosition", RPCArg::Type::NUM, RPCArg::DefaultHint{"random"}, "The index of the change output"},
#    3220                 :       1607 :                             {"change_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -changetype"}, "The output type to use. Only valid if changeAddress is not specified. Options are \"legacy\", \"p2sh-segwit\", and \"bech32\"."},
#    3221                 :       1607 :                             {"includeWatching", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Also select inputs which are watch only.\n"
#    3222                 :       1607 :                                                           "Only solvable inputs can be used. Watch-only destinations are solvable if the public key and/or output script was imported,\n"
#    3223                 :       1607 :                                                           "e.g. with 'importpubkey' or 'importmulti' with the 'pubkeys' or 'desc' field."},
#    3224                 :       1607 :                             {"lockUnspents", RPCArg::Type::BOOL, RPCArg::Default{false}, "Lock selected unspent outputs"},
#    3225                 :       1607 :                             {"fee_rate", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"not set, fall back to wallet fee estimation"}, "Specify a fee rate in " + CURRENCY_ATOM + "/vB."},
#    3226                 :       1607 :                             {"feeRate", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"not set, fall back to wallet fee estimation"}, "Specify a fee rate in " + CURRENCY_UNIT + "/kvB."},
#    3227                 :       1607 :                             {"subtractFeeFromOutputs", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "The integers.\n"
#    3228                 :       1607 :                                                           "The fee will be equally deducted from the amount of each specified output.\n"
#    3229                 :       1607 :                                                           "Those recipients will receive less bitcoins than you enter in their corresponding amount field.\n"
#    3230                 :       1607 :                                                           "If no outputs are specified here, the sender pays the fee.",
#    3231                 :       1607 :                                 {
#    3232                 :       1607 :                                     {"vout_index", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "The zero-based output index, before a change output is added."},
#    3233                 :       1607 :                                 },
#    3234                 :       1607 :                             },
#    3235                 :       1607 :                             {"replaceable", RPCArg::Type::BOOL, RPCArg::DefaultHint{"wallet default"}, "Marks this transaction as BIP125 replaceable.\n"
#    3236                 :       1607 :                                                           "Allows this transaction to be replaced by a transaction with higher fees"},
#    3237                 :       1607 :                             {"conf_target", RPCArg::Type::NUM, RPCArg::DefaultHint{"wallet -txconfirmtarget"}, "Confirmation target in blocks"},
#    3238                 :       1607 :                             {"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"unset"}, std::string() + "The fee estimate mode, must be one of (case insensitive):\n"
#    3239                 :       1607 :                             "       \"" + FeeModes("\"\n\"") + "\""},
#    3240                 :       1607 :                         },
#    3241                 :       1607 :                         "options"},
#    3242                 :       1607 :                     {"iswitness", RPCArg::Type::BOOL, RPCArg::DefaultHint{"depends on heuristic tests"}, "Whether the transaction hex is a serialized witness transaction.\n"
#    3243                 :       1607 :                         "If iswitness is not present, heuristic tests will be used in decoding.\n"
#    3244                 :       1607 :                         "If true, only witness deserialization will be tried.\n"
#    3245                 :       1607 :                         "If false, only non-witness deserialization will be tried.\n"
#    3246                 :       1607 :                         "This boolean should reflect whether the transaction has inputs\n"
#    3247                 :       1607 :                         "(e.g. fully valid, or on-chain transactions), if known by the caller."
#    3248                 :       1607 :                     },
#    3249                 :       1607 :                 },
#    3250                 :       1607 :                 RPCResult{
#    3251                 :       1607 :                     RPCResult::Type::OBJ, "", "",
#    3252                 :       1607 :                     {
#    3253                 :       1607 :                         {RPCResult::Type::STR_HEX, "hex", "The resulting raw transaction (hex-encoded string)"},
#    3254                 :       1607 :                         {RPCResult::Type::STR_AMOUNT, "fee", "Fee in " + CURRENCY_UNIT + " the resulting transaction pays"},
#    3255                 :       1607 :                         {RPCResult::Type::NUM, "changepos", "The position of the added change output, or -1"},
#    3256                 :       1607 :                     }
#    3257                 :       1607 :                                 },
#    3258                 :       1607 :                                 RPCExamples{
#    3259                 :       1607 :                             "\nCreate a transaction with no inputs\n"
#    3260                 :       1607 :                             + HelpExampleCli("createrawtransaction", "\"[]\" \"{\\\"myaddress\\\":0.01}\"") +
#    3261                 :       1607 :                             "\nAdd sufficient unsigned inputs to meet the output value\n"
#    3262                 :       1607 :                             + HelpExampleCli("fundrawtransaction", "\"rawtransactionhex\"") +
#    3263                 :       1607 :                             "\nSign the transaction\n"
#    3264                 :       1607 :                             + HelpExampleCli("signrawtransactionwithwallet", "\"fundedtransactionhex\"") +
#    3265                 :       1607 :                             "\nSend the transaction\n"
#    3266                 :       1607 :                             + HelpExampleCli("sendrawtransaction", "\"signedtransactionhex\"")
#    3267                 :       1607 :                                 },
#    3268                 :       1607 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    3269                 :       1607 : {
#    3270                 :        289 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    3271         [ -  + ]:        289 :     if (!pwallet) return NullUniValue;
#    3272                 :            : 
#    3273                 :        289 :     RPCTypeCheck(request.params, {UniValue::VSTR, UniValueType(), UniValue::VBOOL});
#    3274                 :            : 
#    3275                 :            :     // parse hex string from parameter
#    3276                 :        289 :     CMutableTransaction tx;
#    3277         [ +  - ]:        289 :     bool try_witness = request.params[2].isNull() ? true : request.params[2].get_bool();
#    3278         [ +  - ]:        289 :     bool try_no_witness = request.params[2].isNull() ? true : !request.params[2].get_bool();
#    3279         [ -  + ]:        289 :     if (!DecodeHexTx(tx, request.params[0].get_str(), try_no_witness, try_witness)) {
#    3280                 :          0 :         throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
#    3281                 :          0 :     }
#    3282                 :            : 
#    3283                 :        289 :     CAmount fee;
#    3284                 :        289 :     int change_position;
#    3285                 :        289 :     CCoinControl coin_control;
#    3286                 :            :     // Automatically select (additional) coins. Can be overridden by options.add_inputs.
#    3287                 :        289 :     coin_control.m_add_inputs = true;
#    3288                 :        289 :     FundTransaction(*pwallet, tx, fee, change_position, request.params[1], coin_control, /* override_min_fee */ true);
#    3289                 :            : 
#    3290                 :        289 :     UniValue result(UniValue::VOBJ);
#    3291                 :        289 :     result.pushKV("hex", EncodeHexTx(CTransaction(tx)));
#    3292                 :        289 :     result.pushKV("fee", ValueFromAmount(fee));
#    3293                 :        289 :     result.pushKV("changepos", change_position);
#    3294                 :            : 
#    3295                 :        289 :     return result;
#    3296                 :        289 : },
#    3297                 :       1607 :     };
#    3298                 :       1607 : }
#    3299                 :            : 
#    3300                 :            : RPCHelpMan signrawtransactionwithwallet()
#    3301                 :       2531 : {
#    3302                 :       2531 :     return RPCHelpMan{"signrawtransactionwithwallet",
#    3303                 :       2531 :                 "\nSign inputs for raw transaction (serialized, hex-encoded).\n"
#    3304                 :       2531 :                 "The second optional argument (may be null) is an array of previous transaction outputs that\n"
#    3305                 :       2531 :                 "this transaction depends on but may not yet be in the block chain." +
#    3306                 :       2531 :         HELP_REQUIRING_PASSPHRASE,
#    3307                 :       2531 :                 {
#    3308                 :       2531 :                     {"hexstring", RPCArg::Type::STR, RPCArg::Optional::NO, "The transaction hex string"},
#    3309                 :       2531 :                     {"prevtxs", RPCArg::Type::ARR, RPCArg::Optional::OMITTED_NAMED_ARG, "The previous dependent transaction outputs",
#    3310                 :       2531 :                         {
#    3311                 :       2531 :                             {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
#    3312                 :       2531 :                                 {
#    3313                 :       2531 :                                     {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id"},
#    3314                 :       2531 :                                     {"vout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The output number"},
#    3315                 :       2531 :                                     {"scriptPubKey", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "script key"},
#    3316                 :       2531 :                                     {"redeemScript", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "(required for P2SH) redeem script"},
#    3317                 :       2531 :                                     {"witnessScript", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "(required for P2WSH or P2SH-P2WSH) witness script"},
#    3318                 :       2531 :                                     {"amount", RPCArg::Type::AMOUNT, RPCArg::Optional::OMITTED, "(required for Segwit inputs) the amount spent"},
#    3319                 :       2531 :                                 },
#    3320                 :       2531 :                             },
#    3321                 :       2531 :                         },
#    3322                 :       2531 :                     },
#    3323                 :       2531 :                     {"sighashtype", RPCArg::Type::STR, RPCArg::Default{"ALL"}, "The signature hash type. Must be one of\n"
#    3324                 :       2531 :             "       \"ALL\"\n"
#    3325                 :       2531 :             "       \"NONE\"\n"
#    3326                 :       2531 :             "       \"SINGLE\"\n"
#    3327                 :       2531 :             "       \"ALL|ANYONECANPAY\"\n"
#    3328                 :       2531 :             "       \"NONE|ANYONECANPAY\"\n"
#    3329                 :       2531 :             "       \"SINGLE|ANYONECANPAY\""},
#    3330                 :       2531 :                 },
#    3331                 :       2531 :                 RPCResult{
#    3332                 :       2531 :                     RPCResult::Type::OBJ, "", "",
#    3333                 :       2531 :                     {
#    3334                 :       2531 :                         {RPCResult::Type::STR_HEX, "hex", "The hex-encoded raw transaction with signature(s)"},
#    3335                 :       2531 :                         {RPCResult::Type::BOOL, "complete", "If the transaction has a complete set of signatures"},
#    3336                 :       2531 :                         {RPCResult::Type::ARR, "errors", /* optional */ true, "Script verification errors (if there are any)",
#    3337                 :       2531 :                         {
#    3338                 :       2531 :                             {RPCResult::Type::OBJ, "", "",
#    3339                 :       2531 :                             {
#    3340                 :       2531 :                                 {RPCResult::Type::STR_HEX, "txid", "The hash of the referenced, previous transaction"},
#    3341                 :       2531 :                                 {RPCResult::Type::NUM, "vout", "The index of the output to spent and used as input"},
#    3342                 :       2531 :                                 {RPCResult::Type::STR_HEX, "scriptSig", "The hex-encoded signature script"},
#    3343                 :       2531 :                                 {RPCResult::Type::NUM, "sequence", "Script sequence number"},
#    3344                 :       2531 :                                 {RPCResult::Type::STR, "error", "Verification or signing error related to the input"},
#    3345                 :       2531 :                             }},
#    3346                 :       2531 :                         }},
#    3347                 :       2531 :                     }
#    3348                 :       2531 :                 },
#    3349                 :       2531 :                 RPCExamples{
#    3350                 :       2531 :                     HelpExampleCli("signrawtransactionwithwallet", "\"myhex\"")
#    3351                 :       2531 :             + HelpExampleRpc("signrawtransactionwithwallet", "\"myhex\"")
#    3352                 :       2531 :                 },
#    3353                 :       2531 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    3354                 :       2531 : {
#    3355                 :       1213 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    3356         [ -  + ]:       1213 :     if (!pwallet) return NullUniValue;
#    3357                 :            : 
#    3358                 :       1213 :     RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VARR, UniValue::VSTR}, true);
#    3359                 :            : 
#    3360                 :       1213 :     CMutableTransaction mtx;
#    3361         [ -  + ]:       1213 :     if (!DecodeHexTx(mtx, request.params[0].get_str())) {
#    3362                 :          0 :         throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed. Make sure the tx has at least one input.");
#    3363                 :          0 :     }
#    3364                 :            : 
#    3365                 :            :     // Sign the transaction
#    3366                 :       1213 :     LOCK(pwallet->cs_wallet);
#    3367                 :       1213 :     EnsureWalletIsUnlocked(*pwallet);
#    3368                 :            : 
#    3369                 :            :     // Fetch previous transactions (inputs):
#    3370                 :       1213 :     std::map<COutPoint, Coin> coins;
#    3371         [ +  + ]:       8540 :     for (const CTxIn& txin : mtx.vin) {
#    3372                 :       8540 :         coins[txin.prevout]; // Create empty map entry keyed by prevout.
#    3373                 :       8540 :     }
#    3374                 :       1213 :     pwallet->chain().findCoins(coins);
#    3375                 :            : 
#    3376                 :            :     // Parse the prevtxs array
#    3377                 :       1213 :     ParsePrevouts(request.params[1], nullptr, coins);
#    3378                 :            : 
#    3379                 :       1213 :     int nHashType = ParseSighashString(request.params[2]);
#    3380                 :            : 
#    3381                 :            :     // Script verification errors
#    3382                 :       1213 :     std::map<int, std::string> input_errors;
#    3383                 :            : 
#    3384                 :       1213 :     bool complete = pwallet->SignTransaction(mtx, coins, nHashType, input_errors);
#    3385                 :       1213 :     UniValue result(UniValue::VOBJ);
#    3386                 :       1213 :     SignTransactionResultToJSON(mtx, complete, coins, input_errors, result);
#    3387                 :       1213 :     return result;
#    3388                 :       1213 : },
#    3389                 :       2531 :     };
#    3390                 :       2531 : }
#    3391                 :            : 
#    3392                 :            : static RPCHelpMan bumpfee_helper(std::string method_name)
#    3393                 :       2872 : {
#    3394                 :       2872 :     const bool want_psbt = method_name == "psbtbumpfee";
#    3395                 :       2872 :     const std::string incremental_fee{CFeeRate(DEFAULT_INCREMENTAL_RELAY_FEE).ToString(FeeEstimateMode::SAT_VB)};
#    3396                 :            : 
#    3397                 :       2872 :     return RPCHelpMan{method_name,
#    3398                 :       2872 :         "\nBumps the fee of an opt-in-RBF transaction T, replacing it with a new transaction B.\n"
#    3399         [ +  + ]:       2872 :         + std::string(want_psbt ? "Returns a PSBT instead of creating and signing a new transaction.\n" : "") +
#    3400                 :       2872 :         "An opt-in RBF transaction with the given txid must be in the wallet.\n"
#    3401                 :       2872 :         "The command will pay the additional fee by reducing change outputs or adding inputs when necessary.\n"
#    3402                 :       2872 :         "It may add a new change output if one does not already exist.\n"
#    3403                 :       2872 :         "All inputs in the original transaction will be included in the replacement transaction.\n"
#    3404                 :       2872 :         "The command will fail if the wallet or mempool contains a transaction that spends one of T's outputs.\n"
#    3405                 :       2872 :         "By default, the new fee will be calculated automatically using the estimatesmartfee RPC.\n"
#    3406                 :       2872 :         "The user can specify a confirmation target for estimatesmartfee.\n"
#    3407                 :       2872 :         "Alternatively, the user can specify a fee rate in " + CURRENCY_ATOM + "/vB for the new transaction.\n"
#    3408                 :       2872 :         "At a minimum, the new fee rate must be high enough to pay an additional new relay fee (incrementalfee\n"
#    3409                 :       2872 :         "returned by getnetworkinfo) to enter the node's mempool.\n"
#    3410                 :       2872 :         "* WARNING: before version 0.21, fee_rate was in " + CURRENCY_UNIT + "/kvB. As of 0.21, fee_rate is in " + CURRENCY_ATOM + "/vB. *\n",
#    3411                 :       2872 :         {
#    3412                 :       2872 :             {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The txid to be bumped"},
#    3413                 :       2872 :             {"options", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED_NAMED_ARG, "",
#    3414                 :       2872 :                 {
#    3415                 :       2872 :                     {"conf_target", RPCArg::Type::NUM, RPCArg::DefaultHint{"wallet -txconfirmtarget"}, "Confirmation target in blocks\n"},
#    3416                 :       2872 :                     {"fee_rate", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"not set, fall back to wallet fee estimation"},
#    3417                 :       2872 :                              "\nSpecify a fee rate in " + CURRENCY_ATOM + "/vB instead of relying on the built-in fee estimator.\n"
#    3418                 :       2872 :                              "Must be at least " + incremental_fee + " higher than the current transaction fee rate.\n"
#    3419                 :       2872 :                              "WARNING: before version 0.21, fee_rate was in " + CURRENCY_UNIT + "/kvB. As of 0.21, fee_rate is in " + CURRENCY_ATOM + "/vB.\n"},
#    3420                 :       2872 :                     {"replaceable", RPCArg::Type::BOOL, RPCArg::Default{true}, "Whether the new transaction should still be\n"
#    3421                 :       2872 :                              "marked bip-125 replaceable. If true, the sequence numbers in the transaction will\n"
#    3422                 :       2872 :                              "be left unchanged from the original. If false, any input sequence numbers in the\n"
#    3423                 :       2872 :                              "original transaction that were less than 0xfffffffe will be increased to 0xfffffffe\n"
#    3424                 :       2872 :                              "so the new transaction will not be explicitly bip-125 replaceable (though it may\n"
#    3425                 :       2872 :                              "still be replaceable in practice, for example if it has unconfirmed ancestors which\n"
#    3426                 :       2872 :                              "are replaceable).\n"},
#    3427                 :       2872 :                     {"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"unset"}, "The fee estimate mode, must be one of (case insensitive):\n"
#    3428                 :       2872 :                              "\"" + FeeModes("\"\n\"") + "\""},
#    3429                 :       2872 :                 },
#    3430                 :       2872 :                 "options"},
#    3431                 :       2872 :         },
#    3432                 :       2872 :         RPCResult{
#    3433                 :       2872 :             RPCResult::Type::OBJ, "", "", Cat(
#    3434         [ +  + ]:       2872 :                 want_psbt ?
#    3435                 :       1324 :                 std::vector<RPCResult>{{RPCResult::Type::STR, "psbt", "The base64-encoded unsigned PSBT of the new transaction."}} :
#    3436                 :       2872 :                 std::vector<RPCResult>{{RPCResult::Type::STR_HEX, "txid", "The id of the new transaction."}},
#    3437                 :       2872 :             {
#    3438                 :       2872 :                 {RPCResult::Type::STR_AMOUNT, "origfee", "The fee of the replaced transaction."},
#    3439                 :       2872 :                 {RPCResult::Type::STR_AMOUNT, "fee", "The fee of the new transaction."},
#    3440                 :       2872 :                 {RPCResult::Type::ARR, "errors", "Errors encountered during processing (may be empty).",
#    3441                 :       2872 :                 {
#    3442                 :       2872 :                     {RPCResult::Type::STR, "", ""},
#    3443                 :       2872 :                 }},
#    3444                 :       2872 :             })
#    3445                 :       2872 :         },
#    3446                 :       2872 :         RPCExamples{
#    3447         [ +  + ]:       2872 :     "\nBump the fee, get the new transaction\'s " + std::string(want_psbt ? "psbt" : "txid") + "\n" +
#    3448                 :       2872 :             HelpExampleCli(method_name, "<txid>")
#    3449                 :       2872 :         },
#    3450                 :       2872 :         [want_psbt](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    3451                 :       2872 : {
#    3452                 :        236 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    3453         [ -  + ]:        236 :     if (!pwallet) return NullUniValue;
#    3454                 :            : 
#    3455 [ +  + ][ +  + ]:        236 :     if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !want_psbt) {
#    3456                 :          2 :         throw JSONRPCError(RPC_WALLET_ERROR, "bumpfee is not available with wallets that have private keys disabled. Use psbtbumpfee instead.");
#    3457                 :          2 :     }
#    3458                 :            : 
#    3459                 :        234 :     RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VOBJ});
#    3460                 :        234 :     uint256 hash(ParseHashV(request.params[0], "txid"));
#    3461                 :            : 
#    3462                 :        234 :     CCoinControl coin_control;
#    3463                 :        234 :     coin_control.fAllowWatchOnly = pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
#    3464                 :            :     // optional parameters
#    3465                 :        234 :     coin_control.m_signal_bip125_rbf = true;
#    3466                 :            : 
#    3467         [ +  + ]:        234 :     if (!request.params[1].isNull()) {
#    3468                 :         92 :         UniValue options = request.params[1];
#    3469                 :         92 :         RPCTypeCheckObj(options,
#    3470                 :         92 :             {
#    3471                 :         92 :                 {"confTarget", UniValueType(UniValue::VNUM)},
#    3472                 :         92 :                 {"conf_target", UniValueType(UniValue::VNUM)},
#    3473                 :         92 :                 {"fee_rate", UniValueType()}, // will be checked by AmountFromValue() in SetFeeEstimateMode()
#    3474                 :         92 :                 {"replaceable", UniValueType(UniValue::VBOOL)},
#    3475                 :         92 :                 {"estimate_mode", UniValueType(UniValue::VSTR)},
#    3476                 :         92 :             },
#    3477                 :         92 :             true, true);
#    3478                 :            : 
#    3479 [ +  + ][ +  + ]:         92 :         if (options.exists("confTarget") && options.exists("conf_target")) {
#                 [ +  - ]
#    3480                 :          2 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "confTarget and conf_target options should not both be set. Use conf_target (confTarget is deprecated).");
#    3481                 :          2 :         }
#    3482                 :            : 
#    3483         [ -  + ]:         90 :         auto conf_target = options.exists("confTarget") ? options["confTarget"] : options["conf_target"];
#    3484                 :            : 
#    3485         [ +  + ]:         90 :         if (options.exists("replaceable")) {
#    3486                 :          2 :             coin_control.m_signal_bip125_rbf = options["replaceable"].get_bool();
#    3487                 :          2 :         }
#    3488                 :         90 :         SetFeeEstimateMode(*pwallet, coin_control, conf_target, options["estimate_mode"], options["fee_rate"], /* override_min_fee */ false);
#    3489                 :         90 :     }
#    3490                 :            : 
#    3491                 :            :     // Make sure the results are valid at least up to the most recent block
#    3492                 :            :     // the user could have gotten from another RPC command prior to now
#    3493                 :        234 :     pwallet->BlockUntilSyncedToCurrentChain();
#    3494                 :            : 
#    3495                 :        232 :     LOCK(pwallet->cs_wallet);
#    3496                 :            : 
#    3497                 :        232 :     EnsureWalletIsUnlocked(*pwallet);
#    3498                 :            : 
#    3499                 :            : 
#    3500                 :        232 :     std::vector<bilingual_str> errors;
#    3501                 :        232 :     CAmount old_fee;
#    3502                 :        232 :     CAmount new_fee;
#    3503                 :        232 :     CMutableTransaction mtx;
#    3504                 :        232 :     feebumper::Result res;
#    3505                 :            :     // Targeting feerate bump.
#    3506                 :        232 :     res = feebumper::CreateRateBumpTransaction(*pwallet, hash, coin_control, errors, old_fee, new_fee, mtx);
#    3507         [ +  + ]:        232 :     if (res != feebumper::Result::OK) {
#    3508                 :         30 :         switch(res) {
#    3509         [ -  + ]:          0 :             case feebumper::Result::INVALID_ADDRESS_OR_KEY:
#    3510                 :          0 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, errors[0].original);
#    3511                 :          0 :                 break;
#    3512         [ -  + ]:          0 :             case feebumper::Result::INVALID_REQUEST:
#    3513                 :          0 :                 throw JSONRPCError(RPC_INVALID_REQUEST, errors[0].original);
#    3514                 :          0 :                 break;
#    3515         [ +  + ]:         16 :             case feebumper::Result::INVALID_PARAMETER:
#    3516                 :         16 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, errors[0].original);
#    3517                 :          0 :                 break;
#    3518         [ +  + ]:         14 :             case feebumper::Result::WALLET_ERROR:
#    3519                 :         14 :                 throw JSONRPCError(RPC_WALLET_ERROR, errors[0].original);
#    3520                 :          0 :                 break;
#    3521         [ -  + ]:          0 :             default:
#    3522                 :          0 :                 throw JSONRPCError(RPC_MISC_ERROR, errors[0].original);
#    3523                 :          0 :                 break;
#    3524                 :        202 :         }
#    3525                 :        202 :     }
#    3526                 :            : 
#    3527                 :        202 :     UniValue result(UniValue::VOBJ);
#    3528                 :            : 
#    3529                 :            :     // For bumpfee, return the new transaction id.
#    3530                 :            :     // For psbtbumpfee, return the base64-encoded unsigned PSBT of the new transaction.
#    3531         [ +  + ]:        202 :     if (!want_psbt) {
#    3532         [ -  + ]:        140 :         if (!feebumper::SignTransaction(*pwallet, mtx)) {
#    3533                 :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, "Can't sign transaction.");
#    3534                 :          0 :         }
#    3535                 :            : 
#    3536                 :        140 :         uint256 txid;
#    3537         [ -  + ]:        140 :         if (feebumper::CommitTransaction(*pwallet, hash, std::move(mtx), errors, txid) != feebumper::Result::OK) {
#    3538                 :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, errors[0].original);
#    3539                 :          0 :         }
#    3540                 :            : 
#    3541                 :        140 :         result.pushKV("txid", txid.GetHex());
#    3542                 :        140 :     } else {
#    3543                 :         62 :         PartiallySignedTransaction psbtx(mtx);
#    3544                 :         62 :         bool complete = false;
#    3545                 :         62 :         const TransactionError err = pwallet->FillPSBT(psbtx, complete, SIGHASH_ALL, false /* sign */, true /* bip32derivs */);
#    3546         [ -  + ]:         62 :         CHECK_NONFATAL(err == TransactionError::OK);
#    3547         [ -  + ]:         62 :         CHECK_NONFATAL(!complete);
#    3548                 :         62 :         CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
#    3549                 :         62 :         ssTx << psbtx;
#    3550                 :         62 :         result.pushKV("psbt", EncodeBase64(ssTx.str()));
#    3551                 :         62 :     }
#    3552                 :            : 
#    3553                 :        202 :     result.pushKV("origfee", ValueFromAmount(old_fee));
#    3554                 :        202 :     result.pushKV("fee", ValueFromAmount(new_fee));
#    3555                 :        202 :     UniValue result_errors(UniValue::VARR);
#    3556         [ -  + ]:        202 :     for (const bilingual_str& error : errors) {
#    3557                 :          0 :         result_errors.push_back(error.original);
#    3558                 :          0 :     }
#    3559                 :        202 :     result.pushKV("errors", result_errors);
#    3560                 :            : 
#    3561                 :        202 :     return result;
#    3562                 :        202 : },
#    3563                 :       2872 :     };
#    3564                 :       2872 : }
#    3565                 :            : 
#    3566                 :       1548 : static RPCHelpMan bumpfee() { return bumpfee_helper("bumpfee"); }
#    3567                 :       1324 : static RPCHelpMan psbtbumpfee() { return bumpfee_helper("psbtbumpfee"); }
#    3568                 :            : 
#    3569                 :            : static RPCHelpMan rescanblockchain()
#    3570                 :       1325 : {
#    3571                 :       1325 :     return RPCHelpMan{"rescanblockchain",
#    3572                 :       1325 :                 "\nRescan the local blockchain for wallet related transactions.\n"
#    3573                 :       1325 :                 "Note: Use \"getwalletinfo\" to query the scanning progress.\n",
#    3574                 :       1325 :                 {
#    3575                 :       1325 :                     {"start_height", RPCArg::Type::NUM, RPCArg::Default{0}, "block height where the rescan should start"},
#    3576                 :       1325 :                     {"stop_height", RPCArg::Type::NUM, RPCArg::Optional::OMITTED_NAMED_ARG, "the last block height that should be scanned. If none is provided it will rescan up to the tip at return time of this call."},
#    3577                 :       1325 :                 },
#    3578                 :       1325 :                 RPCResult{
#    3579                 :       1325 :                     RPCResult::Type::OBJ, "", "",
#    3580                 :       1325 :                     {
#    3581                 :       1325 :                         {RPCResult::Type::NUM, "start_height", "The block height where the rescan started (the requested height or 0)"},
#    3582                 :       1325 :                         {RPCResult::Type::NUM, "stop_height", "The height of the last rescanned block. May be null in rare cases if there was a reorg and the call didn't scan any blocks because they were already scanned in the background."},
#    3583                 :       1325 :                     }
#    3584                 :       1325 :                 },
#    3585                 :       1325 :                 RPCExamples{
#    3586                 :       1325 :                     HelpExampleCli("rescanblockchain", "100000 120000")
#    3587                 :       1325 :             + HelpExampleRpc("rescanblockchain", "100000, 120000")
#    3588                 :       1325 :                 },
#    3589                 :       1325 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    3590                 :       1325 : {
#    3591                 :          7 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    3592         [ -  + ]:          7 :     if (!pwallet) return NullUniValue;
#    3593                 :            : 
#    3594                 :          7 :     WalletRescanReserver reserver(*pwallet);
#    3595         [ -  + ]:          7 :     if (!reserver.reserve()) {
#    3596                 :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
#    3597                 :          0 :     }
#    3598                 :            : 
#    3599                 :          7 :     int start_height = 0;
#    3600                 :          7 :     std::optional<int> stop_height;
#    3601                 :          7 :     uint256 start_block;
#    3602                 :          7 :     {
#    3603                 :          7 :         LOCK(pwallet->cs_wallet);
#    3604                 :          7 :         int tip_height = pwallet->GetLastBlockHeight();
#    3605                 :            : 
#    3606         [ +  + ]:          7 :         if (!request.params[0].isNull()) {
#    3607                 :          2 :             start_height = request.params[0].get_int();
#    3608 [ -  + ][ -  + ]:          2 :             if (start_height < 0 || start_height > tip_height) {
#    3609                 :          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid start_height");
#    3610                 :          0 :             }
#    3611                 :          7 :         }
#    3612                 :            : 
#    3613         [ +  + ]:          7 :         if (!request.params[1].isNull()) {
#    3614                 :          2 :             stop_height = request.params[1].get_int();
#    3615 [ -  + ][ -  + ]:          2 :             if (*stop_height < 0 || *stop_height > tip_height) {
#    3616                 :          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid stop_height");
#    3617         [ -  + ]:          2 :             } else if (*stop_height < start_height) {
#    3618                 :          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "stop_height must be greater than start_height");
#    3619                 :          0 :             }
#    3620                 :          7 :         }
#    3621                 :            : 
#    3622                 :            :         // We can't rescan beyond non-pruned blocks, stop and throw an error
#    3623         [ -  + ]:          7 :         if (!pwallet->chain().hasBlocks(pwallet->GetLastBlockHash(), start_height, stop_height)) {
#    3624                 :          0 :             throw JSONRPCError(RPC_MISC_ERROR, "Can't rescan beyond pruned data. Use RPC call getblockchaininfo to determine your pruned height.");
#    3625                 :          0 :         }
#    3626                 :            : 
#    3627         [ -  + ]:          7 :         CHECK_NONFATAL(pwallet->chain().findAncestorByHeight(pwallet->GetLastBlockHash(), start_height, FoundBlock().hash(start_block)));
#    3628                 :          7 :     }
#    3629                 :            : 
#    3630                 :          7 :     CWallet::ScanResult result =
#    3631                 :          7 :         pwallet->ScanForWalletTransactions(start_block, start_height, stop_height, reserver, true /* fUpdate */);
#    3632         [ -  + ]:          7 :     switch (result.status) {
#    3633         [ +  - ]:          7 :     case CWallet::ScanResult::SUCCESS:
#    3634                 :          7 :         break;
#    3635         [ -  + ]:          0 :     case CWallet::ScanResult::FAILURE:
#    3636                 :          0 :         throw JSONRPCError(RPC_MISC_ERROR, "Rescan failed. Potentially corrupted data files.");
#    3637         [ -  + ]:          0 :     case CWallet::ScanResult::USER_ABORT:
#    3638                 :          0 :         throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted.");
#    3639                 :            :         // no default case, so the compiler can warn about missing cases
#    3640                 :          7 :     }
#    3641                 :          7 :     UniValue response(UniValue::VOBJ);
#    3642                 :          7 :     response.pushKV("start_height", start_height);
#    3643         [ +  - ]:          7 :     response.pushKV("stop_height", result.last_scanned_height ? *result.last_scanned_height : UniValue());
#    3644                 :          7 :     return response;
#    3645                 :          7 : },
#    3646                 :       1325 :     };
#    3647                 :       1325 : }
#    3648                 :            : 
#    3649                 :            : class DescribeWalletAddressVisitor
#    3650                 :            : {
#    3651                 :            : public:
#    3652                 :            :     const SigningProvider * const provider;
#    3653                 :            : 
#    3654                 :            :     void ProcessSubScript(const CScript& subscript, UniValue& obj) const
#    3655                 :        363 :     {
#    3656                 :            :         // Always present: script type and redeemscript
#    3657                 :        363 :         std::vector<std::vector<unsigned char>> solutions_data;
#    3658                 :        363 :         TxoutType which_type = Solver(subscript, solutions_data);
#    3659                 :        363 :         obj.pushKV("script", GetTxnOutputType(which_type));
#    3660                 :        363 :         obj.pushKV("hex", HexStr(subscript));
#    3661                 :            : 
#    3662                 :        363 :         CTxDestination embedded;
#    3663         [ +  + ]:        363 :         if (ExtractDestination(subscript, embedded)) {
#    3664                 :            :             // Only when the script corresponds to an address.
#    3665                 :        234 :             UniValue subobj(UniValue::VOBJ);
#    3666                 :        234 :             UniValue detail = DescribeAddress(embedded);
#    3667                 :        234 :             subobj.pushKVs(detail);
#    3668                 :        234 :             UniValue wallet_detail = std::visit(*this, embedded);
#    3669                 :        234 :             subobj.pushKVs(wallet_detail);
#    3670                 :        234 :             subobj.pushKV("address", EncodeDestination(embedded));
#    3671                 :        234 :             subobj.pushKV("scriptPubKey", HexStr(subscript));
#    3672                 :            :             // Always report the pubkey at the top level, so that `getnewaddress()['pubkey']` always works.
#    3673         [ +  + ]:        234 :             if (subobj.exists("pubkey")) obj.pushKV("pubkey", subobj["pubkey"]);
#    3674                 :        234 :             obj.pushKV("embedded", std::move(subobj));
#    3675         [ +  - ]:        234 :         } else if (which_type == TxoutType::MULTISIG) {
#    3676                 :            :             // Also report some information on multisig scripts (which do not have a corresponding address).
#    3677                 :            :             // TODO: abstract out the common functionality between this logic and ExtractDestinations.
#    3678                 :        129 :             obj.pushKV("sigsrequired", solutions_data[0][0]);
#    3679                 :        129 :             UniValue pubkeys(UniValue::VARR);
#    3680         [ +  + ]:        447 :             for (size_t i = 1; i < solutions_data.size() - 1; ++i) {
#    3681                 :        318 :                 CPubKey key(solutions_data[i].begin(), solutions_data[i].end());
#    3682                 :        318 :                 pubkeys.push_back(HexStr(key));
#    3683                 :        318 :             }
#    3684                 :        129 :             obj.pushKV("pubkeys", std::move(pubkeys));
#    3685                 :        129 :         }
#    3686                 :        363 :     }
#    3687                 :            : 
#    3688                 :       1183 :     explicit DescribeWalletAddressVisitor(const SigningProvider* _provider) : provider(_provider) {}
#    3689                 :            : 
#    3690                 :          0 :     UniValue operator()(const CNoDestination& dest) const { return UniValue(UniValue::VOBJ); }
#    3691                 :            : 
#    3692                 :            :     UniValue operator()(const PKHash& pkhash) const
#    3693                 :        245 :     {
#    3694                 :        245 :         CKeyID keyID{ToKeyID(pkhash)};
#    3695                 :        245 :         UniValue obj(UniValue::VOBJ);
#    3696                 :        245 :         CPubKey vchPubKey;
#    3697 [ +  + ][ +  + ]:        245 :         if (provider && provider->GetPubKey(keyID, vchPubKey)) {
#    3698                 :        226 :             obj.pushKV("pubkey", HexStr(vchPubKey));
#    3699                 :        226 :             obj.pushKV("iscompressed", vchPubKey.IsCompressed());
#    3700                 :        226 :         }
#    3701                 :        245 :         return obj;
#    3702                 :        245 :     }
#    3703                 :            : 
#    3704                 :            :     UniValue operator()(const ScriptHash& scripthash) const
#    3705                 :        292 :     {
#    3706                 :        292 :         CScriptID scriptID(scripthash);
#    3707                 :        292 :         UniValue obj(UniValue::VOBJ);
#    3708                 :        292 :         CScript subscript;
#    3709 [ +  + ][ +  + ]:        292 :         if (provider && provider->GetCScript(scriptID, subscript)) {
#    3710                 :        285 :             ProcessSubScript(subscript, obj);
#    3711                 :        285 :         }
#    3712                 :        292 :         return obj;
#    3713                 :        292 :     }
#    3714                 :            : 
#    3715                 :            :     UniValue operator()(const WitnessV0KeyHash& id) const
#    3716                 :        782 :     {
#    3717                 :        782 :         UniValue obj(UniValue::VOBJ);
#    3718                 :        782 :         CPubKey pubkey;
#    3719 [ +  + ][ +  + ]:        782 :         if (provider && provider->GetPubKey(ToKeyID(id), pubkey)) {
#                 [ +  + ]
#    3720                 :        717 :             obj.pushKV("pubkey", HexStr(pubkey));
#    3721                 :        717 :         }
#    3722                 :        782 :         return obj;
#    3723                 :        782 :     }
#    3724                 :            : 
#    3725                 :            :     UniValue operator()(const WitnessV0ScriptHash& id) const
#    3726                 :         98 :     {
#    3727                 :         98 :         UniValue obj(UniValue::VOBJ);
#    3728                 :         98 :         CScript subscript;
#    3729                 :         98 :         CRIPEMD160 hasher;
#    3730                 :         98 :         uint160 hash;
#    3731                 :         98 :         hasher.Write(id.begin(), 32).Finalize(hash.begin());
#    3732 [ +  + ][ +  + ]:         98 :         if (provider && provider->GetCScript(CScriptID(hash), subscript)) {
#                 [ +  - ]
#    3733                 :         78 :             ProcessSubScript(subscript, obj);
#    3734                 :         78 :         }
#    3735                 :         98 :         return obj;
#    3736                 :         98 :     }
#    3737                 :            : 
#    3738                 :          0 :     UniValue operator()(const WitnessUnknown& id) const { return UniValue(UniValue::VOBJ); }
#    3739                 :            : };
#    3740                 :            : 
#    3741                 :            : static UniValue DescribeWalletAddress(const CWallet& wallet, const CTxDestination& dest)
#    3742                 :       1183 : {
#    3743                 :       1183 :     UniValue ret(UniValue::VOBJ);
#    3744                 :       1183 :     UniValue detail = DescribeAddress(dest);
#    3745                 :       1183 :     CScript script = GetScriptForDestination(dest);
#    3746                 :       1183 :     std::unique_ptr<SigningProvider> provider = nullptr;
#    3747                 :       1183 :     provider = wallet.GetSolvingProvider(script);
#    3748                 :       1183 :     ret.pushKVs(detail);
#    3749                 :       1183 :     ret.pushKVs(std::visit(DescribeWalletAddressVisitor(provider.get()), dest));
#    3750                 :       1183 :     return ret;
#    3751                 :       1183 : }
#    3752                 :            : 
#    3753                 :            : /** Convert CAddressBookData to JSON record.  */
#    3754                 :            : static UniValue AddressBookDataToJSON(const CAddressBookData& data, const bool verbose)
#    3755                 :        144 : {
#    3756                 :        144 :     UniValue ret(UniValue::VOBJ);
#    3757         [ -  + ]:        144 :     if (verbose) {
#    3758                 :          0 :         ret.pushKV("name", data.GetLabel());
#    3759                 :          0 :     }
#    3760                 :        144 :     ret.pushKV("purpose", data.purpose);
#    3761                 :        144 :     return ret;
#    3762                 :        144 : }
#    3763                 :            : 
#    3764                 :            : RPCHelpMan getaddressinfo()
#    3765                 :       2507 : {
#    3766                 :       2507 :     return RPCHelpMan{"getaddressinfo",
#    3767                 :       2507 :                 "\nReturn information about the given bitcoin address.\n"
#    3768                 :       2507 :                 "Some of the information will only be present if the address is in the active wallet.\n",
#    3769                 :       2507 :                 {
#    3770                 :       2507 :                     {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address for which to get information."},
#    3771                 :       2507 :                 },
#    3772                 :       2507 :                 RPCResult{
#    3773                 :       2507 :                     RPCResult::Type::OBJ, "", "",
#    3774                 :       2507 :                     {
#    3775                 :       2507 :                         {RPCResult::Type::STR, "address", "The bitcoin address validated."},
#    3776                 :       2507 :                         {RPCResult::Type::STR_HEX, "scriptPubKey", "The hex-encoded scriptPubKey generated by the address."},
#    3777                 :       2507 :                         {RPCResult::Type::BOOL, "ismine", "If the address is yours."},
#    3778                 :       2507 :                         {RPCResult::Type::BOOL, "iswatchonly", "If the address is watchonly."},
#    3779                 :       2507 :                         {RPCResult::Type::BOOL, "solvable", "If we know how to spend coins sent to this address, ignoring the possible lack of private keys."},
#    3780                 :       2507 :                         {RPCResult::Type::STR, "desc", /* optional */ true, "A descriptor for spending coins sent to this address (only when solvable)."},
#    3781                 :       2507 :                         {RPCResult::Type::STR, "parent_desc", /* optional */ true, "The descriptor used to derive this address if this is a descriptor wallet"},
#    3782                 :       2507 :                         {RPCResult::Type::BOOL, "isscript", "If the key is a script."},
#    3783                 :       2507 :                         {RPCResult::Type::BOOL, "ischange", "If the address was used for change output."},
#    3784                 :       2507 :                         {RPCResult::Type::BOOL, "iswitness", "If the address is a witness address."},
#    3785                 :       2507 :                         {RPCResult::Type::NUM, "witness_version", /* optional */ true, "The version number of the witness program."},
#    3786                 :       2507 :                         {RPCResult::Type::STR_HEX, "witness_program", /* optional */ true, "The hex value of the witness program."},
#    3787                 :       2507 :                         {RPCResult::Type::STR, "script", /* optional */ true, "The output script type. Only if isscript is true and the redeemscript is known. Possible\n"
#    3788                 :       2507 :                                                                      "types: nonstandard, pubkey, pubkeyhash, scripthash, multisig, nulldata, witness_v0_keyhash,\n"
#    3789                 :       2507 :                             "witness_v0_scripthash, witness_unknown."},
#    3790                 :       2507 :                         {RPCResult::Type::STR_HEX, "hex", /* optional */ true, "The redeemscript for the p2sh address."},
#    3791                 :       2507 :                         {RPCResult::Type::ARR, "pubkeys", /* optional */ true, "Array of pubkeys associated with the known redeemscript (only if script is multisig).",
#    3792                 :       2507 :                         {
#    3793                 :       2507 :                             {RPCResult::Type::STR, "pubkey", ""},
#    3794                 :       2507 :                         }},
#    3795                 :       2507 :                         {RPCResult::Type::NUM, "sigsrequired", /* optional */ true, "The number of signatures required to spend multisig output (only if script is multisig)."},
#    3796                 :       2507 :                         {RPCResult::Type::STR_HEX, "pubkey", /* optional */ true, "The hex value of the raw public key for single-key addresses (possibly embedded in P2SH or P2WSH)."},
#    3797                 :       2507 :                         {RPCResult::Type::OBJ, "embedded", /* optional */ true, "Information about the address embedded in P2SH or P2WSH, if relevant and known.",
#    3798                 :       2507 :                         {
#    3799                 :       2507 :                             {RPCResult::Type::ELISION, "", "Includes all getaddressinfo output fields for the embedded address, excluding metadata (timestamp, hdkeypath, hdseedid)\n"
#    3800                 :       2507 :                             "and relation to the wallet (ismine, iswatchonly)."},
#    3801                 :       2507 :                         }},
#    3802                 :       2507 :                         {RPCResult::Type::BOOL, "iscompressed", /* optional */ true, "If the pubkey is compressed."},
#    3803                 :       2507 :                         {RPCResult::Type::NUM_TIME, "timestamp", /* optional */ true, "The creation time of the key, if available, expressed in " + UNIX_EPOCH_TIME + "."},
#    3804                 :       2507 :                         {RPCResult::Type::STR, "hdkeypath", /* optional */ true, "The HD keypath, if the key is HD and available."},
#    3805                 :       2507 :                         {RPCResult::Type::STR_HEX, "hdseedid", /* optional */ true, "The Hash160 of the HD seed."},
#    3806                 :       2507 :                         {RPCResult::Type::STR_HEX, "hdmasterfingerprint", /* optional */ true, "The fingerprint of the master key."},
#    3807                 :       2507 :                         {RPCResult::Type::ARR, "labels", "Array of labels associated with the address. Currently limited to one label but returned\n"
#    3808                 :       2507 :                             "as an array to keep the API stable if multiple labels are enabled in the future.",
#    3809                 :       2507 :                         {
#    3810                 :       2507 :                             {RPCResult::Type::STR, "label name", "Label name (defaults to \"\")."},
#    3811                 :       2507 :                         }},
#    3812                 :       2507 :                     }
#    3813                 :       2507 :                 },
#    3814                 :       2507 :                 RPCExamples{
#    3815                 :       2507 :                     HelpExampleCli("getaddressinfo", "\"" + EXAMPLE_ADDRESS[0] + "\"") +
#    3816                 :       2507 :                     HelpExampleRpc("getaddressinfo", "\"" + EXAMPLE_ADDRESS[0] + "\"")
#    3817                 :       2507 :                 },
#    3818                 :       2507 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    3819                 :       2507 : {
#    3820                 :       1189 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    3821         [ -  + ]:       1189 :     if (!pwallet) return NullUniValue;
#    3822                 :            : 
#    3823                 :       1189 :     LOCK(pwallet->cs_wallet);
#    3824                 :            : 
#    3825                 :       1189 :     std::string error_msg;
#    3826                 :       1189 :     CTxDestination dest = DecodeDestination(request.params[0].get_str(), error_msg);
#    3827                 :            : 
#    3828                 :            :     // Make sure the destination is valid
#    3829         [ +  + ]:       1189 :     if (!IsValidDestination(dest)) {
#    3830                 :            :         // Set generic error message in case 'DecodeDestination' didn't set it
#    3831         [ -  + ]:          6 :         if (error_msg.empty()) error_msg = "Invalid address";
#    3832                 :            : 
#    3833                 :          6 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error_msg);
#    3834                 :          6 :     }
#    3835                 :            : 
#    3836                 :       1183 :     UniValue ret(UniValue::VOBJ);
#    3837                 :            : 
#    3838                 :       1183 :     std::string currentAddress = EncodeDestination(dest);
#    3839                 :       1183 :     ret.pushKV("address", currentAddress);
#    3840                 :            : 
#    3841                 :       1183 :     CScript scriptPubKey = GetScriptForDestination(dest);
#    3842                 :       1183 :     ret.pushKV("scriptPubKey", HexStr(scriptPubKey));
#    3843                 :            : 
#    3844                 :       1183 :     std::unique_ptr<SigningProvider> provider = pwallet->GetSolvingProvider(scriptPubKey);
#    3845                 :            : 
#    3846                 :       1183 :     isminetype mine = pwallet->IsMine(dest);
#    3847                 :       1183 :     ret.pushKV("ismine", bool(mine & ISMINE_SPENDABLE));
#    3848                 :            : 
#    3849 [ +  + ][ +  + ]:       1183 :     bool solvable = provider && IsSolvable(*provider, scriptPubKey);
#    3850                 :       1183 :     ret.pushKV("solvable", solvable);
#    3851                 :            : 
#    3852         [ +  + ]:       1183 :     if (solvable) {
#    3853                 :       1072 :        ret.pushKV("desc", InferDescriptor(scriptPubKey, *provider)->ToString());
#    3854                 :       1072 :     }
#    3855                 :            : 
#    3856                 :       1183 :     DescriptorScriptPubKeyMan* desc_spk_man = dynamic_cast<DescriptorScriptPubKeyMan*>(pwallet->GetScriptPubKeyMan(scriptPubKey));
#    3857         [ +  + ]:       1183 :     if (desc_spk_man) {
#    3858                 :        313 :         std::string desc_str;
#    3859         [ +  + ]:        313 :         if (desc_spk_man->GetDescriptorString(desc_str, false)) {
#    3860                 :        309 :             ret.pushKV("parent_desc", desc_str);
#    3861                 :        309 :         }
#    3862                 :        313 :     }
#    3863                 :            : 
#    3864                 :       1183 :     ret.pushKV("iswatchonly", bool(mine & ISMINE_WATCH_ONLY));
#    3865                 :            : 
#    3866                 :       1183 :     UniValue detail = DescribeWalletAddress(*pwallet, dest);
#    3867                 :       1183 :     ret.pushKVs(detail);
#    3868                 :            : 
#    3869                 :       1183 :     ret.pushKV("ischange", pwallet->IsChange(scriptPubKey));
#    3870                 :            : 
#    3871                 :       1183 :     ScriptPubKeyMan* spk_man = pwallet->GetScriptPubKeyMan(scriptPubKey);
#    3872         [ +  + ]:       1183 :     if (spk_man) {
#    3873         [ +  + ]:       1090 :         if (const std::unique_ptr<CKeyMetadata> meta = spk_man->GetMetadata(dest)) {
#    3874                 :        964 :             ret.pushKV("timestamp", meta->nCreateTime);
#    3875         [ +  + ]:        964 :             if (meta->has_key_origin) {
#    3876                 :        910 :                 ret.pushKV("hdkeypath", WriteHDKeypath(meta->key_origin.path));
#    3877                 :        910 :                 ret.pushKV("hdseedid", meta->hd_seed_id.GetHex());
#    3878                 :        910 :                 ret.pushKV("hdmasterfingerprint", HexStr(meta->key_origin.fingerprint));
#    3879                 :        910 :             }
#    3880                 :        964 :         }
#    3881                 :       1090 :     }
#    3882                 :            : 
#    3883                 :            :     // Return a `labels` array containing the label associated with the address,
#    3884                 :            :     // equivalent to the `label` field above. Currently only one label can be
#    3885                 :            :     // associated with an address, but we return an array so the API remains
#    3886                 :            :     // stable if we allow multiple labels to be associated with an address in
#    3887                 :            :     // the future.
#    3888                 :       1183 :     UniValue labels(UniValue::VARR);
#    3889                 :       1183 :     const auto* address_book_entry = pwallet->FindAddressBookEntry(dest);
#    3890         [ +  + ]:       1183 :     if (address_book_entry) {
#    3891                 :        952 :         labels.push_back(address_book_entry->GetLabel());
#    3892                 :        952 :     }
#    3893                 :       1183 :     ret.pushKV("labels", std::move(labels));
#    3894                 :            : 
#    3895                 :       1183 :     return ret;
#    3896                 :       1183 : },
#    3897                 :       2507 :     };
#    3898                 :       2507 : }
#    3899                 :            : 
#    3900                 :            : static RPCHelpMan getaddressesbylabel()
#    3901                 :       1397 : {
#    3902                 :       1397 :     return RPCHelpMan{"getaddressesbylabel",
#    3903                 :       1397 :                 "\nReturns the list of addresses assigned the specified label.\n",
#    3904                 :       1397 :                 {
#    3905                 :       1397 :                     {"label", RPCArg::Type::STR, RPCArg::Optional::NO, "The label."},
#    3906                 :       1397 :                 },
#    3907                 :       1397 :                 RPCResult{
#    3908                 :       1397 :                     RPCResult::Type::OBJ_DYN, "", "json object with addresses as keys",
#    3909                 :       1397 :                     {
#    3910                 :       1397 :                         {RPCResult::Type::OBJ, "address", "json object with information about address",
#    3911                 :       1397 :                         {
#    3912                 :       1397 :                             {RPCResult::Type::STR, "purpose", "Purpose of address (\"send\" for sending address, \"receive\" for receiving address)"},
#    3913                 :       1397 :                         }},
#    3914                 :       1397 :                     }
#    3915                 :       1397 :                 },
#    3916                 :       1397 :                 RPCExamples{
#    3917                 :       1397 :                     HelpExampleCli("getaddressesbylabel", "\"tabby\"")
#    3918                 :       1397 :             + HelpExampleRpc("getaddressesbylabel", "\"tabby\"")
#    3919                 :       1397 :                 },
#    3920                 :       1397 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    3921                 :       1397 : {
#    3922                 :         79 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    3923         [ -  + ]:         79 :     if (!pwallet) return NullUniValue;
#    3924                 :            : 
#    3925                 :         79 :     LOCK(pwallet->cs_wallet);
#    3926                 :            : 
#    3927                 :         79 :     std::string label = LabelFromValue(request.params[0]);
#    3928                 :            : 
#    3929                 :            :     // Find all addresses that have the given label
#    3930                 :         79 :     UniValue ret(UniValue::VOBJ);
#    3931                 :         79 :     std::set<std::string> addresses;
#    3932         [ +  + ]:       1389 :     for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->m_address_book) {
#    3933         [ -  + ]:       1389 :         if (item.second.IsChange()) continue;
#    3934         [ +  + ]:       1389 :         if (item.second.GetLabel() == label) {
#    3935                 :        144 :             std::string address = EncodeDestination(item.first);
#    3936                 :            :             // CWallet::m_address_book is not expected to contain duplicate
#    3937                 :            :             // address strings, but build a separate set as a precaution just in
#    3938                 :            :             // case it does.
#    3939                 :        144 :             bool unique = addresses.emplace(address).second;
#    3940         [ -  + ]:        144 :             CHECK_NONFATAL(unique);
#    3941                 :            :             // UniValue::pushKV checks if the key exists in O(N)
#    3942                 :            :             // and since duplicate addresses are unexpected (checked with
#    3943                 :            :             // std::set in O(log(N))), UniValue::__pushKV is used instead,
#    3944                 :            :             // which currently is O(1).
#    3945                 :        144 :             ret.__pushKV(address, AddressBookDataToJSON(item.second, false));
#    3946                 :        144 :         }
#    3947                 :       1389 :     }
#    3948                 :            : 
#    3949         [ +  + ]:         79 :     if (ret.empty()) {
#    3950                 :         10 :         throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, std::string("No addresses with label " + label));
#    3951                 :         10 :     }
#    3952                 :            : 
#    3953                 :         69 :     return ret;
#    3954                 :         69 : },
#    3955                 :       1397 :     };
#    3956                 :       1397 : }
#    3957                 :            : 
#    3958                 :            : static RPCHelpMan listlabels()
#    3959                 :       1391 : {
#    3960                 :       1391 :     return RPCHelpMan{"listlabels",
#    3961                 :       1391 :                 "\nReturns the list of all labels, or labels that are assigned to addresses with a specific purpose.\n",
#    3962                 :       1391 :                 {
#    3963                 :       1391 :                     {"purpose", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "Address purpose to list labels for ('send','receive'). An empty string is the same as not providing this argument."},
#    3964                 :       1391 :                 },
#    3965                 :       1391 :                 RPCResult{
#    3966                 :       1391 :                     RPCResult::Type::ARR, "", "",
#    3967                 :       1391 :                     {
#    3968                 :       1391 :                         {RPCResult::Type::STR, "label", "Label name"},
#    3969                 :       1391 :                     }
#    3970                 :       1391 :                 },
#    3971                 :       1391 :                 RPCExamples{
#    3972                 :       1391 :             "\nList all labels\n"
#    3973                 :       1391 :             + HelpExampleCli("listlabels", "") +
#    3974                 :       1391 :             "\nList labels that have receiving addresses\n"
#    3975                 :       1391 :             + HelpExampleCli("listlabels", "receive") +
#    3976                 :       1391 :             "\nList labels that have sending addresses\n"
#    3977                 :       1391 :             + HelpExampleCli("listlabels", "send") +
#    3978                 :       1391 :             "\nAs a JSON-RPC call\n"
#    3979                 :       1391 :             + HelpExampleRpc("listlabels", "receive")
#    3980                 :       1391 :                 },
#    3981                 :       1391 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    3982                 :       1391 : {
#    3983                 :         73 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    3984         [ -  + ]:         73 :     if (!pwallet) return NullUniValue;
#    3985                 :            : 
#    3986                 :         73 :     LOCK(pwallet->cs_wallet);
#    3987                 :            : 
#    3988                 :         73 :     std::string purpose;
#    3989         [ -  + ]:         73 :     if (!request.params[0].isNull()) {
#    3990                 :          0 :         purpose = request.params[0].get_str();
#    3991                 :          0 :     }
#    3992                 :            : 
#    3993                 :            :     // Add to a set to sort by label name, then insert into Univalue array
#    3994                 :         73 :     std::set<std::string> label_set;
#    3995         [ +  + ]:       1277 :     for (const std::pair<const CTxDestination, CAddressBookData>& entry : pwallet->m_address_book) {
#    3996         [ -  + ]:       1277 :         if (entry.second.IsChange()) continue;
#    3997 [ +  - ][ #  # ]:       1277 :         if (purpose.empty() || entry.second.purpose == purpose) {
#    3998                 :       1277 :             label_set.insert(entry.second.GetLabel());
#    3999                 :       1277 :         }
#    4000                 :       1277 :     }
#    4001                 :            : 
#    4002                 :         73 :     UniValue ret(UniValue::VARR);
#    4003         [ +  + ]:        417 :     for (const std::string& name : label_set) {
#    4004                 :        417 :         ret.push_back(name);
#    4005                 :        417 :     }
#    4006                 :            : 
#    4007                 :         73 :     return ret;
#    4008                 :         73 : },
#    4009                 :       1391 :     };
#    4010                 :       1391 : }
#    4011                 :            : 
#    4012                 :            : static RPCHelpMan send()
#    4013                 :       1548 : {
#    4014                 :       1548 :     return RPCHelpMan{"send",
#    4015                 :       1548 :         "\nEXPERIMENTAL warning: this call may be changed in future releases.\n"
#    4016                 :       1548 :         "\nSend a transaction.\n",
#    4017                 :       1548 :         {
#    4018                 :       1548 :             {"outputs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The outputs (key-value pairs), where none of the keys are duplicated.\n"
#    4019                 :       1548 :                     "That is, each address can only appear once and there can only be one 'data' object.\n"
#    4020                 :       1548 :                     "For convenience, a dictionary, which holds the key-value pairs directly, is also accepted.",
#    4021                 :       1548 :                 {
#    4022                 :       1548 :                     {"", RPCArg::Type::OBJ_USER_KEYS, RPCArg::Optional::OMITTED, "",
#    4023                 :       1548 :                         {
#    4024                 :       1548 :                             {"address", RPCArg::Type::AMOUNT, RPCArg::Optional::NO, "A key-value pair. The key (string) is the bitcoin address, the value (float or string) is the amount in " + CURRENCY_UNIT + ""},
#    4025                 :       1548 :                         },
#    4026                 :       1548 :                         },
#    4027                 :       1548 :                     {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
#    4028                 :       1548 :                         {
#    4029                 :       1548 :                             {"data", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "A key-value pair. The key must be \"data\", the value is hex-encoded data"},
#    4030                 :       1548 :                         },
#    4031                 :       1548 :                     },
#    4032                 :       1548 :                 },
#    4033                 :       1548 :             },
#    4034                 :       1548 :             {"conf_target", RPCArg::Type::NUM, RPCArg::DefaultHint{"wallet -txconfirmtarget"}, "Confirmation target in blocks"},
#    4035                 :       1548 :             {"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"unset"}, std::string() + "The fee estimate mode, must be one of (case insensitive):\n"
#    4036                 :       1548 :                         "       \"" + FeeModes("\"\n\"") + "\""},
#    4037                 :       1548 :             {"fee_rate", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"not set, fall back to wallet fee estimation"}, "Specify a fee rate in " + CURRENCY_ATOM + "/vB."},
#    4038                 :       1548 :             {"options", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED_NAMED_ARG, "",
#    4039                 :       1548 :                 {
#    4040                 :       1548 :                     {"add_inputs", RPCArg::Type::BOOL, RPCArg::Default{false}, "If inputs are specified, automatically include more if they are not enough."},
#    4041                 :       1548 :                     {"include_unsafe", RPCArg::Type::BOOL, RPCArg::Default{false}, "Include inputs that are not safe to spend (unconfirmed transactions from outside keys and unconfirmed replacement transactions).\n"
#    4042                 :       1548 :                                                           "Warning: the resulting transaction may become invalid if one of the unsafe inputs disappears.\n"
#    4043                 :       1548 :                                                           "If that happens, you will need to fund the transaction with different inputs and republish it."},
#    4044                 :       1548 :                     {"add_to_wallet", RPCArg::Type::BOOL, RPCArg::Default{true}, "When false, returns a serialized transaction which will not be added to the wallet or broadcast"},
#    4045                 :       1548 :                     {"change_address", RPCArg::Type::STR_HEX, RPCArg::DefaultHint{"pool address"}, "The bitcoin address to receive the change"},
#    4046                 :       1548 :                     {"change_position", RPCArg::Type::NUM, RPCArg::DefaultHint{"random"}, "The index of the change output"},
#    4047                 :       1548 :                     {"change_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -changetype"}, "The output type to use. Only valid if change_address is not specified. Options are \"legacy\", \"p2sh-segwit\", and \"bech32\"."},
#    4048                 :       1548 :                     {"conf_target", RPCArg::Type::NUM, RPCArg::DefaultHint{"wallet -txconfirmtarget"}, "Confirmation target in blocks"},
#    4049                 :       1548 :                     {"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"unset"}, std::string() + "The fee estimate mode, must be one of (case insensitive):\n"
#    4050                 :       1548 :             "       \"" + FeeModes("\"\n\"") + "\""},
#    4051                 :       1548 :                     {"fee_rate", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"not set, fall back to wallet fee estimation"}, "Specify a fee rate in " + CURRENCY_ATOM + "/vB."},
#    4052                 :       1548 :                     {"include_watching", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Also select inputs which are watch only.\n"
#    4053                 :       1548 :                                           "Only solvable inputs can be used. Watch-only destinations are solvable if the public key and/or output script was imported,\n"
#    4054                 :       1548 :                                           "e.g. with 'importpubkey' or 'importmulti' with the 'pubkeys' or 'desc' field."},
#    4055                 :       1548 :                     {"inputs", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "Specify inputs instead of adding them automatically. A JSON array of JSON objects",
#    4056                 :       1548 :                         {
#    4057                 :       1548 :                             {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id"},
#    4058                 :       1548 :                             {"vout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The output number"},
#    4059                 :       1548 :                             {"sequence", RPCArg::Type::NUM, RPCArg::Optional::NO, "The sequence number"},
#    4060                 :       1548 :                         },
#    4061                 :       1548 :                     },
#    4062                 :       1548 :                     {"locktime", RPCArg::Type::NUM, RPCArg::Default{0}, "Raw locktime. Non-0 value also locktime-activates inputs"},
#    4063                 :       1548 :                     {"lock_unspents", RPCArg::Type::BOOL, RPCArg::Default{false}, "Lock selected unspent outputs"},
#    4064                 :       1548 :                     {"psbt", RPCArg::Type::BOOL,  RPCArg::DefaultHint{"automatic"}, "Always return a PSBT, implies add_to_wallet=false."},
#    4065                 :       1548 :                     {"subtract_fee_from_outputs", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "Outputs to subtract the fee from, specified as integer indices.\n"
#    4066                 :       1548 :                     "The fee will be equally deducted from the amount of each specified output.\n"
#    4067                 :       1548 :                     "Those recipients will receive less bitcoins than you enter in their corresponding amount field.\n"
#    4068                 :       1548 :                     "If no outputs are specified here, the sender pays the fee.",
#    4069                 :       1548 :                         {
#    4070                 :       1548 :                             {"vout_index", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "The zero-based output index, before a change output is added."},
#    4071                 :       1548 :                         },
#    4072                 :       1548 :                     },
#    4073                 :       1548 :                     {"replaceable", RPCArg::Type::BOOL, RPCArg::DefaultHint{"wallet default"}, "Marks this transaction as BIP125 replaceable.\n"
#    4074                 :       1548 :                                                   "Allows this transaction to be replaced by a transaction with higher fees"},
#    4075                 :       1548 :                 },
#    4076                 :       1548 :                 "options"},
#    4077                 :       1548 :         },
#    4078                 :       1548 :         RPCResult{
#    4079                 :       1548 :             RPCResult::Type::OBJ, "", "",
#    4080                 :       1548 :                 {
#    4081                 :       1548 :                     {RPCResult::Type::BOOL, "complete", "If the transaction has a complete set of signatures"},
#    4082                 :       1548 :                     {RPCResult::Type::STR_HEX, "txid", "The transaction id for the send. Only 1 transaction is created regardless of the number of addresses."},
#    4083                 :       1548 :                     {RPCResult::Type::STR_HEX, "hex", "If add_to_wallet is false, the hex-encoded raw transaction with signature(s)"},
#    4084                 :       1548 :                     {RPCResult::Type::STR, "psbt", "If more signatures are needed, or if add_to_wallet is false, the base64-encoded (partially) signed transaction"}
#    4085                 :       1548 :                 }
#    4086                 :       1548 :         },
#    4087                 :       1548 :         RPCExamples{""
#    4088                 :       1548 :         "\nSend 0.1 BTC with a confirmation target of 6 blocks in economical fee estimate mode\n"
#    4089                 :       1548 :         + HelpExampleCli("send", "'{\"" + EXAMPLE_ADDRESS[0] + "\": 0.1}' 6 economical\n") +
#    4090                 :       1548 :         "Send 0.2 BTC with a fee rate of 1.1 " + CURRENCY_ATOM + "/vB using positional arguments\n"
#    4091                 :       1548 :         + HelpExampleCli("send", "'{\"" + EXAMPLE_ADDRESS[0] + "\": 0.2}' null \"unset\" 1.1\n") +
#    4092                 :       1548 :         "Send 0.2 BTC with a fee rate of 1 " + CURRENCY_ATOM + "/vB using the options argument\n"
#    4093                 :       1548 :         + HelpExampleCli("send", "'{\"" + EXAMPLE_ADDRESS[0] + "\": 0.2}' null \"unset\" null '{\"fee_rate\": 1}'\n") +
#    4094                 :       1548 :         "Send 0.3 BTC with a fee rate of 25 " + CURRENCY_ATOM + "/vB using named arguments\n"
#    4095                 :       1548 :         + HelpExampleCli("-named send", "outputs='{\"" + EXAMPLE_ADDRESS[0] + "\": 0.3}' fee_rate=25\n") +
#    4096                 :       1548 :         "Create a transaction that should confirm the next block, with a specific input, and return result without adding to wallet or broadcasting to the network\n"
#    4097                 :       1548 :         + HelpExampleCli("send", "'{\"" + EXAMPLE_ADDRESS[0] + "\": 0.1}' 1 economical '{\"add_to_wallet\": false, \"inputs\": [{\"txid\":\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\", \"vout\":1}]}'")
#    4098                 :       1548 :         },
#    4099                 :       1548 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    4100                 :       1548 :         {
#    4101                 :        230 :             RPCTypeCheck(request.params, {
#    4102                 :        230 :                 UniValueType(), // outputs (ARR or OBJ, checked later)
#    4103                 :        230 :                 UniValue::VNUM, // conf_target
#    4104                 :        230 :                 UniValue::VSTR, // estimate_mode
#    4105                 :        230 :                 UniValueType(), // fee_rate, will be checked by AmountFromValue() in SetFeeEstimateMode()
#    4106                 :        230 :                 UniValue::VOBJ, // options
#    4107                 :        230 :                 }, true
#    4108                 :        230 :             );
#    4109                 :            : 
#    4110                 :        230 :             std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    4111         [ -  + ]:        230 :             if (!pwallet) return NullUniValue;
#    4112                 :            : 
#    4113         [ +  + ]:        230 :             UniValue options{request.params[4].isNull() ? UniValue::VOBJ : request.params[4]};
#    4114 [ +  + ][ +  + ]:        230 :             if (options.exists("conf_target") || options.exists("estimate_mode")) {
#                 [ +  + ]
#    4115 [ +  + ][ -  + ]:         48 :                 if (!request.params[1].isNull() || !request.params[2].isNull()) {
#    4116                 :          6 :                     throw JSONRPCError(RPC_INVALID_PARAMETER, "Pass conf_target and estimate_mode either as arguments or in the options object, but not both");
#    4117                 :          6 :                 }
#    4118                 :        182 :             } else {
#    4119                 :        182 :                 options.pushKV("conf_target", request.params[1]);
#    4120                 :        182 :                 options.pushKV("estimate_mode", request.params[2]);
#    4121                 :        182 :             }
#    4122         [ +  + ]:        230 :             if (options.exists("fee_rate")) {
#    4123         [ +  + ]:         56 :                 if (!request.params[3].isNull()) {
#    4124                 :          2 :                     throw JSONRPCError(RPC_INVALID_PARAMETER, "Pass the fee_rate either as an argument, or in the options object, but not both");
#    4125                 :          2 :                 }
#    4126                 :        168 :             } else {
#    4127                 :        168 :                 options.pushKV("fee_rate", request.params[3]);
#    4128                 :        168 :             }
#    4129 [ +  + ][ -  + ]:        224 :             if (!options["conf_target"].isNull() && (options["estimate_mode"].isNull() || (options["estimate_mode"].get_str() == "unset"))) {
#         [ -  + ][ -  + ]
#    4130                 :          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Specify estimate_mode");
#    4131                 :          0 :             }
#    4132         [ +  + ]:        222 :             if (options.exists("feeRate")) {
#    4133                 :          2 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Use fee_rate (" + CURRENCY_ATOM + "/vB) instead of feeRate");
#    4134                 :          2 :             }
#    4135         [ -  + ]:        220 :             if (options.exists("changeAddress")) {
#    4136                 :          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Use change_address");
#    4137                 :          0 :             }
#    4138         [ -  + ]:        220 :             if (options.exists("changePosition")) {
#    4139                 :          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Use change_position");
#    4140                 :          0 :             }
#    4141         [ -  + ]:        220 :             if (options.exists("includeWatching")) {
#    4142                 :          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Use include_watching");
#    4143                 :          0 :             }
#    4144         [ -  + ]:        220 :             if (options.exists("lockUnspents")) {
#    4145                 :          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Use lock_unspents");
#    4146                 :          0 :             }
#    4147         [ -  + ]:        220 :             if (options.exists("subtractFeeFromOutputs")) {
#    4148                 :          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Use subtract_fee_from_outputs");
#    4149                 :          0 :             }
#    4150                 :            : 
#    4151 [ +  + ][ +  - ]:        220 :             const bool psbt_opt_in = options.exists("psbt") && options["psbt"].get_bool();
#    4152                 :            : 
#    4153                 :        220 :             CAmount fee;
#    4154                 :        220 :             int change_position;
#    4155                 :        220 :             bool rbf = pwallet->m_signal_rbf;
#    4156         [ +  + ]:        220 :             if (options.exists("replaceable")) {
#    4157                 :          4 :                 rbf = options["replaceable"].get_bool();
#    4158                 :          4 :             }
#    4159                 :        220 :             CMutableTransaction rawTx = ConstructTransaction(options["inputs"], request.params[0], options["locktime"], rbf);
#    4160                 :        220 :             CCoinControl coin_control;
#    4161                 :            :             // Automatically select coins, unless at least one is manually selected. Can
#    4162                 :            :             // be overridden by options.add_inputs.
#    4163                 :        220 :             coin_control.m_add_inputs = rawTx.vin.size() == 0;
#    4164                 :        220 :             FundTransaction(*pwallet, rawTx, fee, change_position, options, coin_control, /* override_min_fee */ false);
#    4165                 :            : 
#    4166                 :        220 :             bool add_to_wallet = true;
#    4167         [ +  + ]:        220 :             if (options.exists("add_to_wallet")) {
#    4168                 :         41 :                 add_to_wallet = options["add_to_wallet"].get_bool();
#    4169                 :         41 :             }
#    4170                 :            : 
#    4171                 :            :             // Make a blank psbt
#    4172                 :        220 :             PartiallySignedTransaction psbtx(rawTx);
#    4173                 :            : 
#    4174                 :            :             // First fill transaction with our data without signing,
#    4175                 :            :             // so external signers are not asked sign more than once.
#    4176                 :        220 :             bool complete;
#    4177                 :        220 :             pwallet->FillPSBT(psbtx, complete, SIGHASH_ALL, false, true);
#    4178                 :        220 :             const TransactionError err = pwallet->FillPSBT(psbtx, complete, SIGHASH_ALL, true, false);
#    4179         [ -  + ]:        220 :             if (err != TransactionError::OK) {
#    4180                 :          0 :                 throw JSONRPCTransactionError(err);
#    4181                 :          0 :             }
#    4182                 :            : 
#    4183                 :        220 :             CMutableTransaction mtx;
#    4184                 :        220 :             complete = FinalizeAndExtractPSBT(psbtx, mtx);
#    4185                 :            : 
#    4186                 :        220 :             UniValue result(UniValue::VOBJ);
#    4187                 :            : 
#    4188 [ +  + ][ +  + ]:        220 :             if (psbt_opt_in || !complete || !add_to_wallet) {
#                 [ +  + ]
#    4189                 :            :                 // Serialize the PSBT
#    4190                 :         41 :                 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
#    4191                 :         41 :                 ssTx << psbtx;
#    4192                 :         41 :                 result.pushKV("psbt", EncodeBase64(ssTx.str()));
#    4193                 :         41 :             }
#    4194                 :            : 
#    4195         [ +  + ]:        220 :             if (complete) {
#    4196                 :         56 :                 std::string err_string;
#    4197                 :         56 :                 std::string hex = EncodeHexTx(CTransaction(mtx));
#    4198                 :         56 :                 CTransactionRef tx(MakeTransactionRef(std::move(mtx)));
#    4199                 :         56 :                 result.pushKV("txid", tx->GetHash().GetHex());
#    4200 [ +  + ][ +  + ]:         56 :                 if (add_to_wallet && !psbt_opt_in) {
#    4201                 :         20 :                     pwallet->CommitTransaction(tx, {}, {} /* orderForm */);
#    4202                 :         36 :                 } else {
#    4203                 :         36 :                     result.pushKV("hex", hex);
#    4204                 :         36 :                 }
#    4205                 :         56 :             }
#    4206                 :        220 :             result.pushKV("complete", complete);
#    4207                 :            : 
#    4208                 :        220 :             return result;
#    4209                 :        220 :         }
#    4210                 :       1548 :     };
#    4211                 :       1548 : }
#    4212                 :            : 
#    4213                 :            : static RPCHelpMan sethdseed()
#    4214                 :       1339 : {
#    4215                 :       1339 :     return RPCHelpMan{"sethdseed",
#    4216                 :       1339 :                 "\nSet or generate a new HD wallet seed. Non-HD wallets will not be upgraded to being a HD wallet. Wallets that are already\n"
#    4217                 :       1339 :                 "HD will have a new HD seed set so that new keys added to the keypool will be derived from this new seed.\n"
#    4218                 :       1339 :                 "\nNote that you will need to MAKE A NEW BACKUP of your wallet after setting the HD wallet seed." +
#    4219                 :       1339 :         HELP_REQUIRING_PASSPHRASE,
#    4220                 :       1339 :                 {
#    4221                 :       1339 :                     {"newkeypool", RPCArg::Type::BOOL, RPCArg::Default{true}, "Whether to flush old unused addresses, including change addresses, from the keypool and regenerate it.\n"
#    4222                 :       1339 :                                          "If true, the next address from getnewaddress and change address from getrawchangeaddress will be from this new seed.\n"
#    4223                 :       1339 :                                          "If false, addresses (including change addresses if the wallet already had HD Chain Split enabled) from the existing\n"
#    4224                 :       1339 :                                          "keypool will be used until it has been depleted."},
#    4225                 :       1339 :                     {"seed", RPCArg::Type::STR, RPCArg::DefaultHint{"random seed"}, "The WIF private key to use as the new HD seed.\n"
#    4226                 :       1339 :                                          "The seed value can be retrieved using the dumpwallet command. It is the private key marked hdseed=1"},
#    4227                 :       1339 :                 },
#    4228                 :       1339 :                 RPCResult{RPCResult::Type::NONE, "", ""},
#    4229                 :       1339 :                 RPCExamples{
#    4230                 :       1339 :                     HelpExampleCli("sethdseed", "")
#    4231                 :       1339 :             + HelpExampleCli("sethdseed", "false")
#    4232                 :       1339 :             + HelpExampleCli("sethdseed", "true \"wifkey\"")
#    4233                 :       1339 :             + HelpExampleRpc("sethdseed", "true, \"wifkey\"")
#    4234                 :       1339 :                 },
#    4235                 :       1339 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    4236                 :       1339 : {
#    4237                 :         20 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    4238         [ -  + ]:         20 :     if (!pwallet) return NullUniValue;
#    4239                 :            : 
#    4240                 :         20 :     LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet, true);
#    4241                 :            : 
#    4242         [ -  + ]:         20 :     if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
#    4243                 :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Cannot set a HD seed to a wallet with private keys disabled");
#    4244                 :          0 :     }
#    4245                 :            : 
#    4246                 :         20 :     LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
#    4247                 :            : 
#    4248                 :            :     // Do not do anything to non-HD wallets
#    4249         [ -  + ]:         20 :     if (!pwallet->CanSupportFeature(FEATURE_HD)) {
#    4250                 :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Cannot set an HD seed on a non-HD wallet. Use the upgradewallet RPC in order to upgrade a non-HD wallet to HD");
#    4251                 :          0 :     }
#    4252                 :            : 
#    4253                 :         20 :     EnsureWalletIsUnlocked(*pwallet);
#    4254                 :            : 
#    4255                 :         20 :     bool flush_key_pool = true;
#    4256         [ +  + ]:         20 :     if (!request.params[0].isNull()) {
#    4257                 :         14 :         flush_key_pool = request.params[0].get_bool();
#    4258                 :         14 :     }
#    4259                 :            : 
#    4260                 :         20 :     CPubKey master_pub_key;
#    4261         [ +  + ]:         20 :     if (request.params[1].isNull()) {
#    4262                 :          8 :         master_pub_key = spk_man.GenerateNewSeed();
#    4263                 :         12 :     } else {
#    4264                 :         12 :         CKey key = DecodeSecret(request.params[1].get_str());
#    4265         [ +  + ]:         12 :         if (!key.IsValid()) {
#    4266                 :          1 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
#    4267                 :          1 :         }
#    4268                 :            : 
#    4269         [ +  + ]:         11 :         if (HaveKey(spk_man, key)) {
#    4270                 :          2 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Already have this key (either as an HD seed or as a loose private key)");
#    4271                 :          2 :         }
#    4272                 :            : 
#    4273                 :          9 :         master_pub_key = spk_man.DeriveNewSeed(key);
#    4274                 :          9 :     }
#    4275                 :            : 
#    4276                 :         20 :     spk_man.SetHDSeed(master_pub_key);
#    4277         [ +  + ]:         17 :     if (flush_key_pool) spk_man.NewKeyPool();
#    4278                 :            : 
#    4279                 :         17 :     return NullUniValue;
#    4280                 :         20 : },
#    4281                 :       1339 :     };
#    4282                 :       1339 : }
#    4283                 :            : 
#    4284                 :            : static RPCHelpMan walletprocesspsbt()
#    4285                 :       1529 : {
#    4286                 :       1529 :     return RPCHelpMan{"walletprocesspsbt",
#    4287                 :       1529 :                 "\nUpdate a PSBT with input information from our wallet and then sign inputs\n"
#    4288                 :       1529 :                 "that we can sign for." +
#    4289                 :       1529 :         HELP_REQUIRING_PASSPHRASE,
#    4290                 :       1529 :                 {
#    4291                 :       1529 :                     {"psbt", RPCArg::Type::STR, RPCArg::Optional::NO, "The transaction base64 string"},
#    4292                 :       1529 :                     {"sign", RPCArg::Type::BOOL, RPCArg::Default{true}, "Also sign the transaction when updating"},
#    4293                 :       1529 :                     {"sighashtype", RPCArg::Type::STR, RPCArg::Default{"ALL"}, "The signature hash type to sign with if not specified by the PSBT. Must be one of\n"
#    4294                 :       1529 :             "       \"ALL\"\n"
#    4295                 :       1529 :             "       \"NONE\"\n"
#    4296                 :       1529 :             "       \"SINGLE\"\n"
#    4297                 :       1529 :             "       \"ALL|ANYONECANPAY\"\n"
#    4298                 :       1529 :             "       \"NONE|ANYONECANPAY\"\n"
#    4299                 :       1529 :             "       \"SINGLE|ANYONECANPAY\""},
#    4300                 :       1529 :                     {"bip32derivs", RPCArg::Type::BOOL, RPCArg::Default{true}, "Include BIP 32 derivation paths for public keys if we know them"},
#    4301                 :       1529 :                 },
#    4302                 :       1529 :                 RPCResult{
#    4303                 :       1529 :                     RPCResult::Type::OBJ, "", "",
#    4304                 :       1529 :                     {
#    4305                 :       1529 :                         {RPCResult::Type::STR, "psbt", "The base64-encoded partially signed transaction"},
#    4306                 :       1529 :                         {RPCResult::Type::BOOL, "complete", "If the transaction has a complete set of signatures"},
#    4307                 :       1529 :                     }
#    4308                 :       1529 :                 },
#    4309                 :       1529 :                 RPCExamples{
#    4310                 :       1529 :                     HelpExampleCli("walletprocesspsbt", "\"psbt\"")
#    4311                 :       1529 :                 },
#    4312                 :       1529 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    4313                 :       1529 : {
#    4314                 :        211 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    4315         [ -  + ]:        211 :     if (!pwallet) return NullUniValue;
#    4316                 :            : 
#    4317                 :        211 :     RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VBOOL, UniValue::VSTR});
#    4318                 :            : 
#    4319                 :            :     // Unserialize the transaction
#    4320                 :        211 :     PartiallySignedTransaction psbtx;
#    4321                 :        211 :     std::string error;
#    4322         [ +  + ]:        211 :     if (!DecodeBase64PSBT(psbtx, request.params[0].get_str(), error)) {
#    4323                 :          2 :         throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed %s", error));
#    4324                 :          2 :     }
#    4325                 :            : 
#    4326                 :            :     // Get the sighash type
#    4327                 :        209 :     int nHashType = ParseSighashString(request.params[2]);
#    4328                 :            : 
#    4329                 :            :     // Fill transaction with our data and also sign
#    4330         [ +  + ]:        209 :     bool sign = request.params[1].isNull() ? true : request.params[1].get_bool();
#    4331         [ +  + ]:        209 :     bool bip32derivs = request.params[3].isNull() ? true : request.params[3].get_bool();
#    4332                 :        209 :     bool complete = true;
#    4333                 :        209 :     const TransactionError err = pwallet->FillPSBT(psbtx, complete, nHashType, sign, bip32derivs);
#    4334         [ +  + ]:        209 :     if (err != TransactionError::OK) {
#    4335                 :          2 :         throw JSONRPCTransactionError(err);
#    4336                 :          2 :     }
#    4337                 :            : 
#    4338                 :        207 :     UniValue result(UniValue::VOBJ);
#    4339                 :        207 :     CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
#    4340                 :        207 :     ssTx << psbtx;
#    4341                 :        207 :     result.pushKV("psbt", EncodeBase64(ssTx.str()));
#    4342                 :        207 :     result.pushKV("complete", complete);
#    4343                 :            : 
#    4344                 :        207 :     return result;
#    4345                 :        207 : },
#    4346                 :       1529 :     };
#    4347                 :       1529 : }
#    4348                 :            : 
#    4349                 :            : static RPCHelpMan walletcreatefundedpsbt()
#    4350                 :       1524 : {
#    4351                 :       1524 :     return RPCHelpMan{"walletcreatefundedpsbt",
#    4352                 :       1524 :                 "\nCreates and funds a transaction in the Partially Signed Transaction format.\n"
#    4353                 :       1524 :                 "Implements the Creator and Updater roles.\n",
#    4354                 :       1524 :                 {
#    4355                 :       1524 :                     {"inputs", RPCArg::Type::ARR, RPCArg::Optional::OMITTED_NAMED_ARG, "Leave empty to add inputs automatically. See add_inputs option.",
#    4356                 :       1524 :                         {
#    4357                 :       1524 :                             {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
#    4358                 :       1524 :                                 {
#    4359                 :       1524 :                                     {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id"},
#    4360                 :       1524 :                                     {"vout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The output number"},
#    4361                 :       1524 :                                     {"sequence", RPCArg::Type::NUM, RPCArg::DefaultHint{"depends on the value of the 'locktime' and 'options.replaceable' arguments"}, "The sequence number"},
#    4362                 :       1524 :                                 },
#    4363                 :       1524 :                             },
#    4364                 :       1524 :                         },
#    4365                 :       1524 :                         },
#    4366                 :       1524 :                     {"outputs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The outputs (key-value pairs), where none of the keys are duplicated.\n"
#    4367                 :       1524 :                             "That is, each address can only appear once and there can only be one 'data' object.\n"
#    4368                 :       1524 :                             "For compatibility reasons, a dictionary, which holds the key-value pairs directly, is also\n"
#    4369                 :       1524 :                             "accepted as second parameter.",
#    4370                 :       1524 :                         {
#    4371                 :       1524 :                             {"", RPCArg::Type::OBJ_USER_KEYS, RPCArg::Optional::OMITTED, "",
#    4372                 :       1524 :                                 {
#    4373                 :       1524 :                                     {"address", RPCArg::Type::AMOUNT, RPCArg::Optional::NO, "A key-value pair. The key (string) is the bitcoin address, the value (float or string) is the amount in " + CURRENCY_UNIT + ""},
#    4374                 :       1524 :                                 },
#    4375                 :       1524 :                                 },
#    4376                 :       1524 :                             {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
#    4377                 :       1524 :                                 {
#    4378                 :       1524 :                                     {"data", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "A key-value pair. The key must be \"data\", the value is hex-encoded data"},
#    4379                 :       1524 :                                 },
#    4380                 :       1524 :                             },
#    4381                 :       1524 :                         },
#    4382                 :       1524 :                     },
#    4383                 :       1524 :                     {"locktime", RPCArg::Type::NUM, RPCArg::Default{0}, "Raw locktime. Non-0 value also locktime-activates inputs"},
#    4384                 :       1524 :                     {"options", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED_NAMED_ARG, "",
#    4385                 :       1524 :                         {
#    4386                 :       1524 :                             {"add_inputs", RPCArg::Type::BOOL, RPCArg::Default{false}, "If inputs are specified, automatically include more if they are not enough."},
#    4387                 :       1524 :                             {"include_unsafe", RPCArg::Type::BOOL, RPCArg::Default{false}, "Include inputs that are not safe to spend (unconfirmed transactions from outside keys and unconfirmed replacement transactions).\n"
#    4388                 :       1524 :                                                           "Warning: the resulting transaction may become invalid if one of the unsafe inputs disappears.\n"
#    4389                 :       1524 :                                                           "If that happens, you will need to fund the transaction with different inputs and republish it."},
#    4390                 :       1524 :                             {"changeAddress", RPCArg::Type::STR_HEX, RPCArg::DefaultHint{"pool address"}, "The bitcoin address to receive the change"},
#    4391                 :       1524 :                             {"changePosition", RPCArg::Type::NUM, RPCArg::DefaultHint{"random"}, "The index of the change output"},
#    4392                 :       1524 :                             {"change_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -changetype"}, "The output type to use. Only valid if changeAddress is not specified. Options are \"legacy\", \"p2sh-segwit\", and \"bech32\"."},
#    4393                 :       1524 :                             {"includeWatching", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Also select inputs which are watch only"},
#    4394                 :       1524 :                             {"lockUnspents", RPCArg::Type::BOOL, RPCArg::Default{false}, "Lock selected unspent outputs"},
#    4395                 :       1524 :                             {"fee_rate", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"not set, fall back to wallet fee estimation"}, "Specify a fee rate in " + CURRENCY_ATOM + "/vB."},
#    4396                 :       1524 :                             {"feeRate", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"not set, fall back to wallet fee estimation"}, "Specify a fee rate in " + CURRENCY_UNIT + "/kvB."},
#    4397                 :       1524 :                             {"subtractFeeFromOutputs", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "The outputs to subtract the fee from.\n"
#    4398                 :       1524 :                                                           "The fee will be equally deducted from the amount of each specified output.\n"
#    4399                 :       1524 :                                                           "Those recipients will receive less bitcoins than you enter in their corresponding amount field.\n"
#    4400                 :       1524 :                                                           "If no outputs are specified here, the sender pays the fee.",
#    4401                 :       1524 :                                 {
#    4402                 :       1524 :                                     {"vout_index", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "The zero-based output index, before a change output is added."},
#    4403                 :       1524 :                                 },
#    4404                 :       1524 :                             },
#    4405                 :       1524 :                             {"replaceable", RPCArg::Type::BOOL, RPCArg::DefaultHint{"wallet default"}, "Marks this transaction as BIP125 replaceable.\n"
#    4406                 :       1524 :                                                           "Allows this transaction to be replaced by a transaction with higher fees"},
#    4407                 :       1524 :                             {"conf_target", RPCArg::Type::NUM, RPCArg::DefaultHint{"wallet -txconfirmtarget"}, "Confirmation target in blocks"},
#    4408                 :       1524 :                             {"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"unset"}, std::string() + "The fee estimate mode, must be one of (case insensitive):\n"
#    4409                 :       1524 :                             "         \"" + FeeModes("\"\n\"") + "\""},
#    4410                 :       1524 :                         },
#    4411                 :       1524 :                         "options"},
#    4412                 :       1524 :                     {"bip32derivs", RPCArg::Type::BOOL, RPCArg::Default{true}, "Include BIP 32 derivation paths for public keys if we know them"},
#    4413                 :       1524 :                 },
#    4414                 :       1524 :                 RPCResult{
#    4415                 :       1524 :                     RPCResult::Type::OBJ, "", "",
#    4416                 :       1524 :                     {
#    4417                 :       1524 :                         {RPCResult::Type::STR, "psbt", "The resulting raw transaction (base64-encoded string)"},
#    4418                 :       1524 :                         {RPCResult::Type::STR_AMOUNT, "fee", "Fee in " + CURRENCY_UNIT + " the resulting transaction pays"},
#    4419                 :       1524 :                         {RPCResult::Type::NUM, "changepos", "The position of the added change output, or -1"},
#    4420                 :       1524 :                     }
#    4421                 :       1524 :                                 },
#    4422                 :       1524 :                                 RPCExamples{
#    4423                 :       1524 :                             "\nCreate a transaction with no inputs\n"
#    4424                 :       1524 :                             + HelpExampleCli("walletcreatefundedpsbt", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"[{\\\"data\\\":\\\"00010203\\\"}]\"")
#    4425                 :       1524 :                                 },
#    4426                 :       1524 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    4427                 :       1524 : {
#    4428                 :        206 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    4429         [ -  + ]:        206 :     if (!pwallet) return NullUniValue;
#    4430                 :            : 
#    4431                 :        206 :     RPCTypeCheck(request.params, {
#    4432                 :        206 :         UniValue::VARR,
#    4433                 :        206 :         UniValueType(), // ARR or OBJ, checked later
#    4434                 :        206 :         UniValue::VNUM,
#    4435                 :        206 :         UniValue::VOBJ,
#    4436                 :        206 :         UniValue::VBOOL
#    4437                 :        206 :         }, true
#    4438                 :        206 :     );
#    4439                 :            : 
#    4440                 :        206 :     CAmount fee;
#    4441                 :        206 :     int change_position;
#    4442                 :        206 :     bool rbf = pwallet->m_signal_rbf;
#    4443                 :        206 :     const UniValue &replaceable_arg = request.params[3]["replaceable"];
#    4444         [ +  + ]:        206 :     if (!replaceable_arg.isNull()) {
#    4445                 :          4 :         RPCTypeCheckArgument(replaceable_arg, UniValue::VBOOL);
#    4446                 :          4 :         rbf = replaceable_arg.isTrue();
#    4447                 :          4 :     }
#    4448                 :        206 :     CMutableTransaction rawTx = ConstructTransaction(request.params[0], request.params[1], request.params[2], rbf);
#    4449                 :        206 :     CCoinControl coin_control;
#    4450                 :            :     // Automatically select coins, unless at least one is manually selected. Can
#    4451                 :            :     // be overridden by options.add_inputs.
#    4452                 :        206 :     coin_control.m_add_inputs = rawTx.vin.size() == 0;
#    4453                 :        206 :     FundTransaction(*pwallet, rawTx, fee, change_position, request.params[3], coin_control, /* override_min_fee */ true);
#    4454                 :            : 
#    4455                 :            :     // Make a blank psbt
#    4456                 :        206 :     PartiallySignedTransaction psbtx(rawTx);
#    4457                 :            : 
#    4458                 :            :     // Fill transaction with out data but don't sign
#    4459         [ +  + ]:        206 :     bool bip32derivs = request.params[4].isNull() ? true : request.params[4].get_bool();
#    4460                 :        206 :     bool complete = true;
#    4461                 :        206 :     const TransactionError err = pwallet->FillPSBT(psbtx, complete, 1, false, bip32derivs);
#    4462         [ -  + ]:        206 :     if (err != TransactionError::OK) {
#    4463                 :          0 :         throw JSONRPCTransactionError(err);
#    4464                 :          0 :     }
#    4465                 :            : 
#    4466                 :            :     // Serialize the PSBT
#    4467                 :        206 :     CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
#    4468                 :        206 :     ssTx << psbtx;
#    4469                 :            : 
#    4470                 :        206 :     UniValue result(UniValue::VOBJ);
#    4471                 :        206 :     result.pushKV("psbt", EncodeBase64(ssTx.str()));
#    4472                 :        206 :     result.pushKV("fee", ValueFromAmount(fee));
#    4473                 :        206 :     result.pushKV("changepos", change_position);
#    4474                 :        206 :     return result;
#    4475                 :        206 : },
#    4476                 :       1524 :     };
#    4477                 :       1524 : }
#    4478                 :            : 
#    4479                 :            : static RPCHelpMan upgradewallet()
#    4480                 :       1318 : {
#    4481                 :       1318 :     return RPCHelpMan{"upgradewallet",
#    4482                 :       1318 :         "\nUpgrade the wallet. Upgrades to the latest version if no version number is specified.\n"
#    4483                 :       1318 :         "New keys may be generated and a new wallet backup will need to be made.",
#    4484                 :       1318 :         {
#    4485                 :       1318 :             {"version", RPCArg::Type::NUM, RPCArg::Default{FEATURE_LATEST}, "The version number to upgrade to. Default is the latest wallet version."}
#    4486                 :       1318 :         },
#    4487                 :       1318 :         RPCResult{
#    4488                 :       1318 :             RPCResult::Type::OBJ, "", "",
#    4489                 :       1318 :             {
#    4490                 :       1318 :                 {RPCResult::Type::STR, "wallet_name", "Name of wallet this operation was performed on"},
#    4491                 :       1318 :                 {RPCResult::Type::NUM, "previous_version", "Version of wallet before this operation"},
#    4492                 :       1318 :                 {RPCResult::Type::NUM, "current_version", "Version of wallet after this operation"},
#    4493                 :       1318 :                 {RPCResult::Type::STR, "result", /* optional */ true, "Description of result, if no error"},
#    4494                 :       1318 :                 {RPCResult::Type::STR, "error", /* optional */ true, "Error message (if there is one)"}
#    4495                 :       1318 :             },
#    4496                 :       1318 :         },
#    4497                 :       1318 :         RPCExamples{
#    4498                 :       1318 :             HelpExampleCli("upgradewallet", "169900")
#    4499                 :       1318 :             + HelpExampleRpc("upgradewallet", "169900")
#    4500                 :       1318 :         },
#    4501                 :       1318 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    4502                 :       1318 : {
#    4503                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#    4504         [ #  # ]:          0 :     if (!pwallet) return NullUniValue;
#    4505                 :            : 
#    4506                 :          0 :     RPCTypeCheck(request.params, {UniValue::VNUM}, true);
#    4507                 :            : 
#    4508                 :          0 :     EnsureWalletIsUnlocked(*pwallet);
#    4509                 :            : 
#    4510                 :          0 :     int version = 0;
#    4511         [ #  # ]:          0 :     if (!request.params[0].isNull()) {
#    4512                 :          0 :         version = request.params[0].get_int();
#    4513                 :          0 :     }
#    4514                 :          0 :     bilingual_str error;
#    4515                 :          0 :     const int previous_version{pwallet->GetVersion()};
#    4516                 :          0 :     const bool wallet_upgraded{pwallet->UpgradeWallet(version, error)};
#    4517                 :          0 :     const int current_version{pwallet->GetVersion()};
#    4518                 :          0 :     std::string result;
#    4519                 :            : 
#    4520         [ #  # ]:          0 :     if (wallet_upgraded) {
#    4521         [ #  # ]:          0 :         if (previous_version == current_version) {
#    4522                 :          0 :             result = "Already at latest version. Wallet version unchanged.";
#    4523                 :          0 :         } else {
#    4524                 :          0 :             result = strprintf("Wallet upgraded successfully from version %i to version %i.", previous_version, current_version);
#    4525                 :          0 :         }
#    4526                 :          0 :     }
#    4527                 :            : 
#    4528                 :          0 :     UniValue obj(UniValue::VOBJ);
#    4529                 :          0 :     obj.pushKV("wallet_name", pwallet->GetName());
#    4530                 :          0 :     obj.pushKV("previous_version", previous_version);
#    4531                 :          0 :     obj.pushKV("current_version", current_version);
#    4532         [ #  # ]:          0 :     if (!result.empty()) {
#    4533                 :          0 :         obj.pushKV("result", result);
#    4534                 :          0 :     } else {
#    4535         [ #  # ]:          0 :         CHECK_NONFATAL(!error.empty());
#    4536                 :          0 :         obj.pushKV("error", error.original);
#    4537                 :          0 :     }
#    4538                 :          0 :     return obj;
#    4539                 :          0 : },
#    4540                 :       1318 :     };
#    4541                 :       1318 : }
#    4542                 :            : 
#    4543                 :            : #ifdef ENABLE_EXTERNAL_SIGNER
#    4544                 :            : static RPCHelpMan walletdisplayaddress()
#    4545                 :            : {
#    4546                 :            :     return RPCHelpMan{"walletdisplayaddress",
#    4547                 :            :         "Display address on an external signer for verification.",
#    4548                 :            :         {
#    4549                 :            :             {"address",     RPCArg::Type::STR, RPCArg::Optional::NO, /* default_val */ "", "bitcoin address to display"},
#    4550                 :            :         },
#    4551                 :            :         RPCResult{
#    4552                 :            :             RPCResult::Type::OBJ,"","",
#    4553                 :            :             {
#    4554                 :            :                 {RPCResult::Type::STR, "address", "The address as confirmed by the signer"},
#    4555                 :            :             }
#    4556                 :            :         },
#    4557                 :            :         RPCExamples{""},
#    4558                 :            :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#    4559                 :            :         {
#    4560                 :            :             std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
#    4561                 :            :             if (!wallet) return NullUniValue;
#    4562                 :            :             CWallet* const pwallet = wallet.get();
#    4563                 :            : 
#    4564                 :            :             LOCK(pwallet->cs_wallet);
#    4565                 :            : 
#    4566                 :            :             CTxDestination dest = DecodeDestination(request.params[0].get_str());
#    4567                 :            : 
#    4568                 :            :             // Make sure the destination is valid
#    4569                 :            :             if (!IsValidDestination(dest)) {
#    4570                 :            :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
#    4571                 :            :             }
#    4572                 :            : 
#    4573                 :            :             if (!pwallet->DisplayAddress(dest)) {
#    4574                 :            :                 throw JSONRPCError(RPC_MISC_ERROR, "Failed to display address");
#    4575                 :            :             }
#    4576                 :            : 
#    4577                 :            :             UniValue result(UniValue::VOBJ);
#    4578                 :            :             result.pushKV("address", request.params[0].get_str());
#    4579                 :            :             return result;
#    4580                 :            :         }
#    4581                 :            :     };
#    4582                 :            : }
#    4583                 :            : #endif // ENABLE_EXTERNAL_SIGNER
#    4584                 :            : 
#    4585                 :            : RPCHelpMan abortrescan();
#    4586                 :            : RPCHelpMan dumpprivkey();
#    4587                 :            : RPCHelpMan importprivkey();
#    4588                 :            : RPCHelpMan importaddress();
#    4589                 :            : RPCHelpMan importpubkey();
#    4590                 :            : RPCHelpMan dumpwallet();
#    4591                 :            : RPCHelpMan importwallet();
#    4592                 :            : RPCHelpMan importprunedfunds();
#    4593                 :            : RPCHelpMan removeprunedfunds();
#    4594                 :            : RPCHelpMan importmulti();
#    4595                 :            : RPCHelpMan importdescriptors();
#    4596                 :            : RPCHelpMan listdescriptors();
#    4597                 :            : 
#    4598                 :            : Span<const CRPCCommand> GetWalletRPCCommands()
#    4599                 :        664 : {
#    4600                 :            : // clang-format off
#    4601                 :        664 : static const CRPCCommand commands[] =
#    4602                 :        664 : { //  category              actor (function)
#    4603                 :            :   //  ------------------    ------------------------
#    4604                 :        664 :     { "rawtransactions",    &fundrawtransaction,             },
#    4605                 :        664 :     { "wallet",             &abandontransaction,             },
#    4606                 :        664 :     { "wallet",             &abortrescan,                    },
#    4607                 :        664 :     { "wallet",             &addmultisigaddress,             },
#    4608                 :        664 :     { "wallet",             &backupwallet,                   },
#    4609                 :        664 :     { "wallet",             &bumpfee,                        },
#    4610                 :        664 :     { "wallet",             &psbtbumpfee,                    },
#    4611                 :        664 :     { "wallet",             &createwallet,                   },
#    4612                 :        664 :     { "wallet",             &dumpprivkey,                    },
#    4613                 :        664 :     { "wallet",             &dumpwallet,                     },
#    4614                 :        664 :     { "wallet",             &encryptwallet,                  },
#    4615                 :        664 :     { "wallet",             &getaddressesbylabel,            },
#    4616                 :        664 :     { "wallet",             &getaddressinfo,                 },
#    4617                 :        664 :     { "wallet",             &getbalance,                     },
#    4618                 :        664 :     { "wallet",             &getnewaddress,                  },
#    4619                 :        664 :     { "wallet",             &getrawchangeaddress,            },
#    4620                 :        664 :     { "wallet",             &getreceivedbyaddress,           },
#    4621                 :        664 :     { "wallet",             &getreceivedbylabel,             },
#    4622                 :        664 :     { "wallet",             &gettransaction,                 },
#    4623                 :        664 :     { "wallet",             &getunconfirmedbalance,          },
#    4624                 :        664 :     { "wallet",             &getbalances,                    },
#    4625                 :        664 :     { "wallet",             &getwalletinfo,                  },
#    4626                 :        664 :     { "wallet",             &importaddress,                  },
#    4627                 :        664 :     { "wallet",             &importdescriptors,              },
#    4628                 :        664 :     { "wallet",             &importmulti,                    },
#    4629                 :        664 :     { "wallet",             &importprivkey,                  },
#    4630                 :        664 :     { "wallet",             &importprunedfunds,              },
#    4631                 :        664 :     { "wallet",             &importpubkey,                   },
#    4632                 :        664 :     { "wallet",             &importwallet,                   },
#    4633                 :        664 :     { "wallet",             &keypoolrefill,                  },
#    4634                 :        664 :     { "wallet",             &listaddressgroupings,           },
#    4635                 :        664 :     { "wallet",             &listdescriptors,                },
#    4636                 :        664 :     { "wallet",             &listlabels,                     },
#    4637                 :        664 :     { "wallet",             &listlockunspent,                },
#    4638                 :        664 :     { "wallet",             &listreceivedbyaddress,          },
#    4639                 :        664 :     { "wallet",             &listreceivedbylabel,            },
#    4640                 :        664 :     { "wallet",             &listsinceblock,                 },
#    4641                 :        664 :     { "wallet",             &listtransactions,               },
#    4642                 :        664 :     { "wallet",             &listunspent,                    },
#    4643                 :        664 :     { "wallet",             &listwalletdir,                  },
#    4644                 :        664 :     { "wallet",             &listwallets,                    },
#    4645                 :        664 :     { "wallet",             &loadwallet,                     },
#    4646                 :        664 :     { "wallet",             &lockunspent,                    },
#    4647                 :        664 :     { "wallet",             &removeprunedfunds,              },
#    4648                 :        664 :     { "wallet",             &rescanblockchain,               },
#    4649                 :        664 :     { "wallet",             &send,                           },
#    4650                 :        664 :     { "wallet",             &sendmany,                       },
#    4651                 :        664 :     { "wallet",             &sendtoaddress,                  },
#    4652                 :        664 :     { "wallet",             &sethdseed,                      },
#    4653                 :        664 :     { "wallet",             &setlabel,                       },
#    4654                 :        664 :     { "wallet",             &settxfee,                       },
#    4655                 :        664 :     { "wallet",             &setwalletflag,                  },
#    4656                 :        664 :     { "wallet",             &signmessage,                    },
#    4657                 :        664 :     { "wallet",             &signrawtransactionwithwallet,   },
#    4658                 :        664 :     { "wallet",             &unloadwallet,                   },
#    4659                 :        664 :     { "wallet",             &upgradewallet,                  },
#    4660                 :        664 :     { "wallet",             &walletcreatefundedpsbt,         },
#    4661                 :            : #ifdef ENABLE_EXTERNAL_SIGNER
#    4662                 :            :     { "wallet",             &walletdisplayaddress,           },
#    4663                 :            : #endif // ENABLE_EXTERNAL_SIGNER
#    4664                 :        664 :     { "wallet",             &walletlock,                     },
#    4665                 :        664 :     { "wallet",             &walletpassphrase,               },
#    4666                 :        664 :     { "wallet",             &walletpassphrasechange,         },
#    4667                 :        664 :     { "wallet",             &walletprocesspsbt,              },
#    4668                 :        664 : };
#    4669                 :            : // clang-format on
#    4670                 :        664 :     return MakeSpan(commands);
#    4671                 :        664 : }

Generated by: LCOV version 1.14