LCOV - code coverage report
Current view: top level - src/rpc - rawtransaction.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 886 1370 64.7 %
Date: 2023-10-05 15:40:34 Functions: 43 50 86.0 %
Branches: 1725 5063 34.1 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2010 Satoshi Nakamoto
       2                 :            : // Copyright (c) 2009-2022 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 <base58.h>
       7                 :            : #include <chain.h>
       8                 :            : #include <coins.h>
       9                 :            : #include <consensus/amount.h>
      10                 :            : #include <consensus/validation.h>
      11                 :            : #include <core_io.h>
      12                 :            : #include <index/txindex.h>
      13                 :            : #include <key_io.h>
      14                 :            : #include <node/blockstorage.h>
      15                 :            : #include <node/coin.h>
      16                 :            : #include <node/context.h>
      17         [ +  - ]:        173 : #include <node/psbt.h>
      18         [ +  - ]:        173 : #include <node/transaction.h>
      19                 :            : #include <policy/packages.h>
      20                 :            : #include <policy/policy.h>
      21                 :            : #include <policy/rbf.h>
      22                 :            : #include <primitives/transaction.h>
      23                 :            : #include <psbt.h>
      24                 :            : #include <random.h>
      25                 :            : #include <rpc/blockchain.h>
      26                 :            : #include <rpc/rawtransaction_util.h>
      27                 :        173 : #include <rpc/server.h>
      28                 :            : #include <rpc/server_util.h>
      29                 :            : #include <rpc/util.h>
      30                 :            : #include <script/script.h>
      31                 :            : #include <script/sign.h>
      32                 :            : #include <script/signingprovider.h>
      33                 :            : #include <script/solver.h>
      34                 :            : #include <uint256.h>
      35                 :            : #include <undo.h>
      36                 :            : #include <util/bip32.h>
      37                 :            : #include <util/check.h>
      38                 :            : #include <util/strencodings.h>
      39                 :            : #include <util/string.h>
      40                 :            : #include <util/vector.h>
      41                 :            : #include <validation.h>
      42                 :            : #include <validationinterface.h>
      43                 :            : 
      44                 :            : #include <numeric>
      45                 :            : #include <stdint.h>
      46                 :            : 
      47                 :            : #include <univalue.h>
      48                 :            : 
      49                 :            : using node::AnalyzePSBT;
      50                 :            : using node::FindCoins;
      51                 :            : using node::GetTransaction;
      52                 :            : using node::NodeContext;
      53                 :            : using node::PSBTAnalysis;
      54                 :            : 
      55                 :          0 : static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry,
      56                 :            :                      Chainstate& active_chainstate, const CTxUndo* txundo = nullptr,
      57                 :            :                      TxVerbosity verbosity = TxVerbosity::SHOW_DETAILS)
      58                 :            : {
      59                 :          0 :     CHECK_NONFATAL(verbosity >= TxVerbosity::SHOW_DETAILS);
      60                 :            :     // Call into TxToUniv() in bitcoin-common to decode the transaction hex.
      61                 :            :     //
      62                 :            :     // Blockchain contextual information (confirmations and blocktime) is not
      63                 :            :     // available to code in bitcoin-common, so we query them here and push the
      64                 :            :     // data into the returned UniValue.
      65                 :          0 :     TxToUniv(tx, /*block_hash=*/uint256(), entry, /*include_hex=*/true, RPCSerializationFlags(), txundo, verbosity);
      66                 :            : 
      67         [ #  # ]:          0 :     if (!hashBlock.IsNull()) {
      68                 :          0 :         LOCK(cs_main);
      69                 :            : 
      70   [ #  #  #  #  :          0 :         entry.pushKV("blockhash", hashBlock.GetHex());
             #  #  #  # ]
      71         [ #  # ]:          0 :         const CBlockIndex* pindex = active_chainstate.m_blockman.LookupBlockIndex(hashBlock);
      72         [ #  # ]:          0 :         if (pindex) {
      73   [ #  #  #  # ]:          0 :             if (active_chainstate.m_chain.Contains(pindex)) {
      74   [ #  #  #  #  :        173 :                 entry.pushKV("confirmations", 1 + active_chainstate.m_chain.Height() - pindex->nHeight);
             #  #  #  # ]
      75   [ #  #  #  #  :          0 :                 entry.pushKV("time", pindex->GetBlockTime());
             #  #  #  # ]
      76   [ #  #  #  #  :          0 :                 entry.pushKV("blocktime", pindex->GetBlockTime());
             #  #  #  # ]
      77                 :          0 :             }
      78                 :            :             else
      79   [ #  #  #  #  :          0 :                 entry.pushKV("confirmations", 0);
                   #  # ]
      80                 :          0 :         }
      81                 :          0 :     }
      82                 :          0 : }
      83                 :            : 
      84                 :        309 : static std::vector<RPCResult> ScriptPubKeyDoc() {
      85         [ #  # ]:        309 :     return
      86         [ +  - ]:       1854 :          {
      87   [ +  -  +  -  :        309 :              {RPCResult::Type::STR, "asm", "Disassembly of the public key script"},
                   +  - ]
      88   [ +  -  +  -  :        309 :              {RPCResult::Type::STR, "desc", "Inferred descriptor for the output"},
                   +  - ]
      89   [ +  -  +  -  :        309 :              {RPCResult::Type::STR_HEX, "hex", "The raw public key script bytes, hex-encoded"},
                   +  - ]
      90   [ +  -  +  -  :        309 :              {RPCResult::Type::STR, "address", /*optional=*/true, "The Bitcoin address (only if a well-defined address exists)"},
                   +  - ]
      91   [ +  -  +  -  :        309 :              {RPCResult::Type::STR, "type", "The type (one of: " + GetAllOutputTypes() + ")"},
          +  -  +  -  +  
                      - ]
      92                 :            :          };
      93                 :          0 : }
      94                 :            : 
      95                 :        263 : static std::vector<RPCResult> DecodeTxDoc(const std::string& txid_field_doc)
      96                 :            : {
      97   [ +  -  #  #  :       2630 :     return {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      98   [ +  -  +  -  :        263 :         {RPCResult::Type::STR_HEX, "txid", txid_field_doc},
                   +  - ]
      99   [ +  -  +  -  :        263 :         {RPCResult::Type::STR_HEX, "hash", "The transaction hash (differs from txid for witness transactions)"},
                   +  - ]
     100   [ +  -  +  -  :        263 :         {RPCResult::Type::NUM, "size", "The serialized transaction size"},
                   +  - ]
     101   [ +  -  +  -  :        263 :         {RPCResult::Type::NUM, "vsize", "The virtual transaction size (differs from size for witness transactions)"},
                   +  - ]
     102   [ +  -  +  -  :        263 :         {RPCResult::Type::NUM, "weight", "The transaction's weight (between vsize*4-3 and vsize*4)"},
                   +  - ]
     103   [ +  -  +  -  :        263 :         {RPCResult::Type::NUM, "version", "The version"},
                   +  - ]
     104   [ +  -  +  -  :        263 :         {RPCResult::Type::NUM_TIME, "locktime", "The lock time"},
                   +  - ]
     105   [ +  -  +  -  :        526 :         {RPCResult::Type::ARR, "vin", "",
                   +  - ]
     106         [ +  - ]:        526 :         {
     107   [ +  -  +  -  :        526 :             {RPCResult::Type::OBJ, "", "",
                   +  - ]
     108         [ +  - ]:       1841 :             {
     109   [ +  -  +  -  :        263 :                 {RPCResult::Type::STR_HEX, "coinbase", /*optional=*/true, "The coinbase value (only if coinbase transaction)"},
                   +  - ]
     110   [ +  -  +  -  :        263 :                 {RPCResult::Type::STR_HEX, "txid", /*optional=*/true, "The transaction id (if not coinbase transaction)"},
                   +  - ]
     111   [ +  -  +  -  :        263 :                 {RPCResult::Type::NUM, "vout", /*optional=*/true, "The output number (if not coinbase transaction)"},
                   +  - ]
     112   [ +  -  +  -  :        526 :                 {RPCResult::Type::OBJ, "scriptSig", /*optional=*/true, "The script (if not coinbase transaction)",
                   +  - ]
     113         [ +  - ]:        789 :                 {
     114   [ +  -  +  -  :        263 :                     {RPCResult::Type::STR, "asm", "Disassembly of the signature script"},
                   +  - ]
     115   [ +  -  +  -  :        263 :                     {RPCResult::Type::STR_HEX, "hex", "The raw signature script bytes, hex-encoded"},
                   +  - ]
     116                 :            :                 }},
     117   [ +  -  +  -  :        526 :                 {RPCResult::Type::ARR, "txinwitness", /*optional=*/true, "",
                   +  - ]
     118         [ +  - ]:        526 :                 {
     119   [ +  -  +  -  :        263 :                     {RPCResult::Type::STR_HEX, "hex", "hex-encoded witness data (if any)"},
                   +  - ]
     120                 :            :                 }},
     121   [ +  -  +  -  :        263 :                 {RPCResult::Type::NUM, "sequence", "The script sequence number"},
                   +  - ]
     122                 :            :             }},
     123                 :            :         }},
     124   [ +  -  +  -  :        526 :         {RPCResult::Type::ARR, "vout", "",
                   +  - ]
     125         [ +  - ]:        526 :         {
     126   [ +  -  +  -  :        526 :             {RPCResult::Type::OBJ, "", "",
                   +  - ]
     127         [ +  - ]:       1052 :             {
     128   [ +  -  +  -  :        263 :                 {RPCResult::Type::STR_AMOUNT, "value", "The value in " + CURRENCY_UNIT},
                   +  - ]
     129   [ +  -  +  -  :        263 :                 {RPCResult::Type::NUM, "n", "index"},
                   +  - ]
     130   [ +  -  +  -  :        263 :                 {RPCResult::Type::OBJ, "scriptPubKey", "", ScriptPubKeyDoc()},
             +  -  +  - ]
     131                 :            :             }},
     132                 :            :         }},
     133                 :            :     };
     134                 :          0 : }
     135                 :            : 
     136                 :         83 : static std::vector<RPCArg> CreateTxDoc()
     137                 :            : {
     138   [ +  -  #  #  :        415 :     return {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     139   [ +  -  +  -  :        166 :         {"inputs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The inputs",
                   +  - ]
     140         [ +  - ]:        166 :             {
     141   [ +  -  +  -  :        166 :                 {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
                   +  - ]
     142         [ +  - ]:        332 :                     {
     143   [ +  -  +  -  :         83 :                         {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id"},
                   +  - ]
     144   [ +  -  +  -  :         83 :                         {"vout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The output number"},
                   +  - ]
     145   [ +  -  +  -  :         83 :                         {"sequence", RPCArg::Type::NUM, RPCArg::DefaultHint{"depends on the value of the 'replaceable' and 'locktime' arguments"}, "The sequence number"},
             +  -  +  - ]
     146                 :            :                     },
     147                 :            :                 },
     148                 :            :             },
     149                 :            :         },
     150   [ +  -  +  -  :        295 :         {"outputs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The outputs specified as key-value pairs.\n"
             +  -  +  - ]
     151                 :        129 :                 "Each key may only appear once, i.e. there can only be one 'data' output, and no address may be duplicated.\n"
     152                 :            :                 "At least one output of either type must be specified.\n"
     153                 :            :                 "For compatibility reasons, a dictionary, which holds the key-value pairs directly, is also\n"
     154                 :            :                 "                             accepted as second parameter.",
     155         [ +  - ]:        249 :             {
     156   [ +  -  +  -  :        166 :                 {"", RPCArg::Type::OBJ_USER_KEYS, RPCArg::Optional::OMITTED, "",
                   +  - ]
     157         [ +  - ]:        166 :                     {
     158   [ +  -  +  -  :         83 :                         {"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},
                   +  - ]
     159                 :            :                     },
     160                 :            :                 },
     161   [ +  -  +  -  :        166 :                 {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
                   +  - ]
     162         [ +  - ]:        166 :                     {
     163   [ +  -  +  -  :         83 :                         {"data", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "A key-value pair. The key must be \"data\", the value is hex-encoded data"},
                   +  - ]
     164                 :            :                     },
     165                 :            :                 },
     166                 :            :             },
     167                 :        249 :          RPCArgOptions{.skip_type_check = true}},
     168   [ +  -  +  -  :         83 :         {"locktime", RPCArg::Type::NUM, RPCArg::Default{0}, "Raw locktime. Non-0 value also locktime-activates inputs"},
             +  -  +  - ]
     169   [ +  -  +  -  :         83 :         {"replaceable", RPCArg::Type::BOOL, RPCArg::Default{true}, "Marks this transaction as BIP125-replaceable.\n"
             +  -  +  - ]
     170                 :            :                 "Allows this transaction to be replaced by a transaction with higher fees. If provided, it is an error if explicit sequence numbers are incompatible."},
     171                 :            :     };
     172                 :          0 : }
     173                 :            : 
     174                 :            : // Update PSBT with information from the mempool, the UTXO set, the txindex, and the provided descriptors.
     175                 :            : // Optionally, sign the inputs that we can using information from the descriptors.
     176                 :         41 : PartiallySignedTransaction ProcessPSBT(const std::string& psbt_string, const std::any& context, const HidingSigningProvider& provider, int sighash_type, bool finalize)
     177                 :            : {
     178                 :            :     // Unserialize the transactions
     179                 :         41 :     PartiallySignedTransaction psbtx;
     180                 :         41 :     std::string error;
     181   [ +  -  +  + ]:         41 :     if (!DecodeBase64PSBT(psbtx, psbt_string, error)) {
     182   [ +  -  +  -  :          1 :         throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed %s", error));
             -  +  +  - ]
     183                 :            :     }
     184                 :            : 
     185   [ -  +  #  # ]:         40 :     if (g_txindex) g_txindex->BlockUntilSyncedToCurrentChain();
     186         [ +  - ]:         40 :     const NodeContext& node = EnsureAnyNodeContext(context);
     187                 :            : 
     188                 :            :     // If we can't find the corresponding full transaction for all of our inputs,
     189                 :            :     // this will be used to find just the utxos for the segwit inputs for which
     190                 :            :     // the full transaction isn't found
     191                 :         40 :     std::map<COutPoint, Coin> coins;
     192                 :            : 
     193                 :            :     // Fetch previous transactions:
     194                 :            :     // First, look in the txindex and the mempool
     195   [ +  -  +  + ]:         73 :     for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
     196         [ +  - ]:         33 :         PSBTInput& psbt_input = psbtx.inputs.at(i);
     197   [ +  -  +  - ]:         33 :         const CTxIn& tx_in = psbtx.tx->vin.at(i);
     198                 :            : 
     199                 :            :         // The `non_witness_utxo` is the whole previous transaction
     200   [ +  -  -  + ]:         33 :         if (psbt_input.non_witness_utxo) continue;
     201                 :            : 
     202                 :         33 :         CTransactionRef tx;
     203                 :            : 
     204                 :            :         // Look in the txindex
     205         [ -  + ]:         33 :         if (g_txindex) {
     206         [ #  # ]:          0 :             uint256 block_hash;
     207         [ #  # ]:          0 :             g_txindex->FindTx(tx_in.prevout.hash, block_hash, tx);
     208                 :          0 :         }
     209                 :            :         // If we still don't have it look in the mempool
     210   [ +  -  +  - ]:         33 :         if (!tx) {
     211         [ +  - ]:         33 :             tx = node.mempool->get(tx_in.prevout.hash);
     212                 :         33 :         }
     213   [ +  -  +  - ]:         33 :         if (tx) {
     214                 :          0 :             psbt_input.non_witness_utxo = tx;
     215                 :          0 :         } else {
     216         [ +  - ]:         33 :             coins[tx_in.prevout]; // Create empty map entry keyed by prevout
     217                 :            :         }
     218                 :         33 :     }
     219                 :            : 
     220                 :            :     // If we still haven't found all of the inputs, look for the missing ones in the utxo set
     221         [ +  + ]:         40 :     if (!coins.empty()) {
     222         [ +  - ]:         11 :         FindCoins(node, coins);
     223   [ +  -  +  + ]:         44 :         for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
     224         [ +  - ]:         33 :             PSBTInput& input = psbtx.inputs.at(i);
     225                 :            : 
     226                 :            :             // If there are still missing utxos, add them if they were found in the utxo set
     227   [ +  -  +  - ]:         33 :             if (!input.non_witness_utxo) {
     228   [ +  -  +  - ]:         33 :                 const CTxIn& tx_in = psbtx.tx->vin.at(i);
     229         [ +  - ]:         33 :                 const Coin& coin = coins.at(tx_in.prevout);
     230   [ +  -  -  +  :         33 :                 if (!coin.out.IsNull() && IsSegWitOutput(provider, coin.out.scriptPubKey)) {
             #  #  #  # ]
     231         [ #  # ]:          0 :                     input.witness_utxo = coin.out;
     232                 :          0 :                 }
     233                 :         33 :             }
     234                 :         33 :         }
     235                 :         11 :     }
     236                 :            : 
     237         [ +  - ]:         40 :     const PrecomputedTransactionData& txdata = PrecomputePSBTData(psbtx);
     238                 :            : 
     239   [ +  -  +  + ]:         73 :     for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
     240   [ +  -  +  -  :         33 :         if (PSBTInputSigned(psbtx.inputs.at(i))) {
                   +  + ]
     241                 :          1 :             continue;
     242                 :            :         }
     243                 :            : 
     244                 :            :         // Update script/keypath information using descriptor data.
     245                 :            :         // Note that SignPSBTInput does a lot more than just constructing ECDSA signatures.
     246                 :            :         // We only actually care about those if our signing provider doesn't hide private
     247                 :            :         // information, as is the case with `descriptorprocesspsbt`
     248         [ +  - ]:         32 :         SignPSBTInput(provider, psbtx, /*index=*/i, &txdata, sighash_type, /*out_sigdata=*/nullptr, finalize);
     249                 :         32 :     }
     250                 :            : 
     251                 :            :     // Update script/keypath information using descriptor data.
     252   [ +  -  +  + ]:        105 :     for (unsigned int i = 0; i < psbtx.tx->vout.size(); ++i) {
     253         [ +  - ]:         65 :         UpdatePSBTOutput(provider, psbtx, i);
     254                 :         65 :     }
     255                 :            : 
     256         [ +  - ]:         40 :     RemoveUnnecessaryTransactions(psbtx, /*sighash_type=*/1);
     257                 :            : 
     258                 :         40 :     return psbtx;
     259         [ +  - ]:         42 : }
     260                 :            : 
     261                 :         46 : static RPCHelpMan getrawtransaction()
     262                 :            : {
     263   [ -  +  #  #  :         46 :     return RPCHelpMan{
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     264         [ +  - ]:         46 :                 "getrawtransaction",
     265                 :            : 
     266         [ +  - ]:         46 :                 "By default, this call only returns a transaction if it is in the mempool. If -txindex is enabled\n"
     267                 :            :                 "and no blockhash argument is passed, it will return the transaction if it is in the mempool or any block.\n"
     268                 :            :                 "If a blockhash argument is passed, it will return the transaction if\n"
     269                 :            :                 "the specified block is available and the transaction is in that block.\n\n"
     270                 :            :                 "Hint: Use gettransaction for wallet transactions.\n\n"
     271                 :            : 
     272                 :            :                 "If verbosity is 0 or omitted, returns the serialized transaction as a hex-encoded string.\n"
     273                 :            :                 "If verbosity is 1, returns a JSON Object with information about the transaction.\n"
     274                 :            :                 "If verbosity is 2, returns a JSON Object with information about the transaction, including fee and prevout information.",
     275         [ +  - ]:        184 :                 {
     276   [ +  -  +  -  :         46 :                     {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id"},
                   +  - ]
     277   [ +  -  +  -  :         46 :                     {"verbosity|verbose", RPCArg::Type::NUM, RPCArg::Default{0}, "0 for hex-encoded data, 1 for a JSON object, and 2 for JSON object with fee and prevout",
             +  -  +  - ]
     278                 :        138 :                      RPCArgOptions{.skip_type_check = true}},
     279   [ +  -  +  -  :         46 :                     {"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "The block in which to look for the transaction"},
                   +  - ]
     280                 :            :                 },
     281         [ +  - ]:        184 :                 {
     282   [ +  -  +  - ]:         92 :                     RPCResult{"if verbosity is not set or set to 0",
     283   [ +  -  +  - ]:         46 :                          RPCResult::Type::STR, "data", "The serialized transaction as a hex-encoded string for 'txid'"
     284                 :            :                      },
     285   [ +  -  +  - ]:         92 :                      RPCResult{"if verbosity is set to 1",
     286   [ +  -  +  - ]:         46 :                          RPCResult::Type::OBJ, "", "",
     287         [ +  - ]:         46 :                          Cat<std::vector<RPCResult>>(
     288         [ +  - ]:        322 :                          {
     289   [ +  -  +  -  :         46 :                              {RPCResult::Type::BOOL, "in_active_chain", /*optional=*/true, "Whether specified block is in the active chain or not (only present with explicit \"blockhash\" argument)"},
                   +  - ]
     290   [ +  -  +  -  :         46 :                              {RPCResult::Type::STR_HEX, "blockhash", /*optional=*/true, "the block hash"},
                   +  - ]
     291   [ +  -  +  -  :         46 :                              {RPCResult::Type::NUM, "confirmations", /*optional=*/true, "The confirmations"},
                   +  - ]
     292   [ +  -  +  -  :         46 :                              {RPCResult::Type::NUM_TIME, "blocktime", /*optional=*/true, "The block time expressed in " + UNIX_EPOCH_TIME},
                   +  - ]
     293   [ +  -  +  -  :         46 :                              {RPCResult::Type::NUM, "time", /*optional=*/true, "Same as \"blocktime\""},
                   +  - ]
     294   [ +  -  +  -  :         46 :                              {RPCResult::Type::STR_HEX, "hex", "The serialized, hex-encoded data for 'txid'"},
                   +  - ]
     295                 :            :                          },
     296   [ +  -  +  - ]:         46 :                          DecodeTxDoc(/*txid_field_doc=*/"The transaction id (same as provided)")),
     297                 :            :                     },
     298   [ +  -  +  - ]:         92 :                     RPCResult{"for verbosity = 2",
     299   [ +  -  +  - ]:         46 :                         RPCResult::Type::OBJ, "", "",
     300         [ +  - ]:        184 :                         {
     301   [ +  -  +  -  :         46 :                             {RPCResult::Type::ELISION, "", "Same output as verbosity = 1"},
                   +  - ]
     302   [ +  -  +  -  :         46 :                             {RPCResult::Type::NUM, "fee", /*optional=*/true, "transaction fee in " + CURRENCY_UNIT + ", omitted if block undo data is not available"},
             +  -  +  - ]
     303   [ +  -  +  -  :         92 :                             {RPCResult::Type::ARR, "vin", "",
                   +  - ]
     304         [ +  - ]:         92 :                             {
     305   [ +  -  +  -  :         92 :                                 {RPCResult::Type::OBJ, "", "utxo being spent",
                   +  - ]
     306         [ +  - ]:        138 :                                 {
     307   [ +  -  +  -  :         46 :                                     {RPCResult::Type::ELISION, "", "Same output as verbosity = 1"},
                   +  - ]
     308   [ +  -  +  -  :         92 :                                     {RPCResult::Type::OBJ, "prevout", /*optional=*/true, "The previous output, omitted if block undo data is not available",
                   +  - ]
     309         [ +  - ]:        230 :                                     {
     310   [ +  -  +  -  :         46 :                                         {RPCResult::Type::BOOL, "generated", "Coinbase or not"},
                   +  - ]
     311   [ +  -  +  -  :         46 :                                         {RPCResult::Type::NUM, "height", "The height of the prevout"},
                   +  - ]
     312   [ +  -  +  -  :         46 :                                         {RPCResult::Type::STR_AMOUNT, "value", "The value in " + CURRENCY_UNIT},
                   +  - ]
     313   [ +  -  +  -  :         46 :                                         {RPCResult::Type::OBJ, "scriptPubKey", "", ScriptPubKeyDoc()},
             +  -  +  - ]
     314                 :            :                                     }},
     315                 :            :                                 }},
     316                 :            :                             }},
     317                 :            :                         }},
     318                 :            :                 },
     319         [ +  - ]:         46 :                 RPCExamples{
     320   [ +  -  +  -  :         46 :                     HelpExampleCli("getrawtransaction", "\"mytxid\"")
                   +  - ]
     321   [ +  -  +  -  :         46 :             + HelpExampleCli("getrawtransaction", "\"mytxid\" 1")
             +  -  +  - ]
     322   [ +  -  +  -  :         46 :             + HelpExampleRpc("getrawtransaction", "\"mytxid\", 1")
             +  -  +  - ]
     323   [ +  -  +  -  :         46 :             + HelpExampleCli("getrawtransaction", "\"mytxid\" 0 \"myblockhash\"")
             +  -  +  - ]
     324   [ +  -  +  -  :         46 :             + HelpExampleCli("getrawtransaction", "\"mytxid\" 1 \"myblockhash\"")
             +  -  +  - ]
     325   [ +  -  +  -  :         46 :             + HelpExampleCli("getrawtransaction", "\"mytxid\" 2 \"myblockhash\"")
             +  -  +  - ]
     326                 :            :                 },
     327         [ +  - ]:         52 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     328                 :            : {
     329                 :          6 :     const NodeContext& node = EnsureAnyNodeContext(request.context);
     330                 :          6 :     ChainstateManager& chainman = EnsureChainman(node);
     331                 :            : 
     332   [ +  -  +  + ]:         11 :     uint256 hash = ParseHashV(request.params[0], "parameter 1");
     333                 :          5 :     const CBlockIndex* blockindex = nullptr;
     334                 :            : 
     335         [ +  + ]:          5 :     if (hash == chainman.GetParams().GenesisBlock().hashMerkleRoot) {
     336                 :            :         // Special exception for the genesis block coinbase transaction
     337   [ +  -  +  -  :          1 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "The genesis block coinbase is not considered an ordinary transaction and cannot be retrieved");
                   +  - ]
     338                 :            :     }
     339                 :            : 
     340                 :            :     // Accept either a bool (true) or a num (>=0) to indicate verbosity.
     341                 :          4 :     int verbosity{0};
     342         [ -  + ]:          4 :     if (!request.params[1].isNull()) {
     343         [ +  + ]:          4 :         if (request.params[1].isBool()) {
     344                 :          3 :             verbosity = request.params[1].get_bool();
     345                 :          3 :         } else {
     346                 :          1 :             verbosity = request.params[1].getInt<int>();
     347                 :            :         }
     348                 :          4 :     }
     349                 :            : 
     350         [ +  + ]:          4 :     if (!request.params[2].isNull()) {
     351                 :          3 :         LOCK(cs_main);
     352                 :            : 
     353   [ +  -  +  -  :          3 :         uint256 blockhash = ParseHashV(request.params[2], "parameter 3");
                   +  + ]
     354         [ +  - ]:          2 :         blockindex = chainman.m_blockman.LookupBlockIndex(blockhash);
     355         [ -  + ]:          2 :         if (!blockindex) {
     356   [ +  -  +  -  :          2 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block hash not found");
                   +  - ]
     357                 :            :         }
     358                 :          3 :     }
     359                 :            : 
     360                 :          1 :     bool f_txindex_ready = false;
     361   [ -  +  #  # ]:          1 :     if (g_txindex && !blockindex) {
     362                 :          0 :         f_txindex_ready = g_txindex->BlockUntilSyncedToCurrentChain();
     363                 :          0 :     }
     364                 :            : 
     365                 :          1 :     uint256 hash_block;
     366                 :          1 :     const CTransactionRef tx = GetTransaction(blockindex, node.mempool.get(), hash, hash_block, chainman.m_blockman);
     367   [ +  -  -  + ]:          1 :     if (!tx) {
     368                 :          1 :         std::string errmsg;
     369         [ -  + ]:          1 :         if (blockindex) {
     370   [ #  #  #  # ]:          0 :             const bool block_has_data = WITH_LOCK(::cs_main, return blockindex->nStatus & BLOCK_HAVE_DATA);
     371         [ #  # ]:          0 :             if (!block_has_data) {
     372   [ #  #  #  #  :          0 :                 throw JSONRPCError(RPC_MISC_ERROR, "Block not available");
                   #  # ]
     373                 :            :             }
     374         [ #  # ]:          0 :             errmsg = "No such transaction found in the provided block";
     375         [ +  - ]:          1 :         } else if (!g_txindex) {
     376         [ -  + ]:          1 :             errmsg = "No such mempool transaction. Use -txindex or provide a block hash to enable blockchain transaction queries";
     377         [ #  # ]:          1 :         } else if (!f_txindex_ready) {
     378         [ #  # ]:          0 :             errmsg = "No such mempool transaction. Blockchain transactions are still in the process of being indexed";
     379                 :          0 :         } else {
     380         [ #  # ]:          0 :             errmsg = "No such mempool or blockchain transaction";
     381                 :            :         }
     382   [ +  -  +  -  :          1 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, errmsg + ". Use gettransaction for wallet transactions.");
                   +  - ]
     383                 :          1 :     }
     384                 :            : 
     385         [ #  # ]:          0 :     if (verbosity <= 0) {
     386   [ #  #  #  #  :          0 :         return EncodeHexTx(*tx, RPCSerializationFlags());
                   #  # ]
     387                 :            :     }
     388                 :            : 
     389         [ #  # ]:          0 :     UniValue result(UniValue::VOBJ);
     390         [ #  # ]:          0 :     if (blockindex) {
     391   [ #  #  #  # ]:          0 :         LOCK(cs_main);
     392   [ #  #  #  #  :          0 :         result.pushKV("in_active_chain", chainman.ActiveChain().Contains(blockindex));
          #  #  #  #  #  
                      # ]
     393                 :          0 :     }
     394                 :            :     // If request is verbosity >= 1 but no blockhash was given, then look up the blockindex
     395   [ #  #  #  #  :          0 :     if (request.params[2].isNull()) {
                   #  # ]
     396   [ #  #  #  # ]:          0 :         LOCK(cs_main);
     397         [ #  # ]:          0 :         blockindex = chainman.m_blockman.LookupBlockIndex(hash_block);
     398                 :          0 :     }
     399         [ #  # ]:          0 :     if (verbosity == 1) {
     400   [ #  #  #  # ]:          0 :         TxToJSON(*tx, hash_block, result, chainman.ActiveChainstate());
     401                 :          0 :         return result;
     402                 :            :     }
     403                 :            : 
     404                 :          0 :     CBlockUndo blockUndo;
     405         [ #  # ]:          0 :     CBlock block;
     406   [ #  #  #  #  :          0 :     const bool is_block_pruned{WITH_LOCK(cs_main, return chainman.m_blockman.IsBlockPruned(blockindex))};
                   #  # ]
     407                 :            : 
     408   [ #  #  #  #  :          0 :     if (tx->IsCoinBase() ||
                   #  # ]
     409   [ #  #  #  # ]:          0 :         !blockindex || is_block_pruned ||
     410   [ #  #  #  #  :          0 :         !(chainman.m_blockman.UndoReadFromDisk(blockUndo, *blockindex) && chainman.m_blockman.ReadBlockFromDisk(block, *blockindex))) {
                   #  # ]
     411   [ #  #  #  # ]:          0 :         TxToJSON(*tx, hash_block, result, chainman.ActiveChainstate());
     412                 :          0 :         return result;
     413                 :            :     }
     414                 :            : 
     415                 :          0 :     CTxUndo* undoTX {nullptr};
     416         [ #  # ]:          0 :     auto it = std::find_if(block.vtx.begin(), block.vtx.end(), [tx](CTransactionRef t){ return *t == *tx; });
     417         [ #  # ]:          0 :     if (it != block.vtx.end()) {
     418                 :            :         // -1 as blockundo does not have coinbase tx
     419         [ #  # ]:          0 :         undoTX = &blockUndo.vtxundo.at(it - block.vtx.begin() - 1);
     420                 :          0 :     }
     421   [ #  #  #  # ]:          0 :     TxToJSON(*tx, hash_block, result, chainman.ActiveChainstate(), undoTX, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
     422                 :          0 :     return result;
     423         [ #  # ]:          7 : },
     424                 :            :     };
     425                 :          0 : }
     426                 :            : 
     427                 :         39 : static RPCHelpMan createrawtransaction()
     428                 :            : {
     429   [ +  -  +  - ]:         78 :     return RPCHelpMan{"createrawtransaction",
     430         [ +  - ]:         39 :                 "\nCreate a transaction spending the given inputs and creating new outputs.\n"
     431                 :            :                 "Outputs can be addresses or data.\n"
     432                 :            :                 "Returns hex-encoded raw transaction.\n"
     433                 :            :                 "Note that the transaction's inputs are not signed, and\n"
     434                 :            :                 "it is not stored in the wallet or transmitted to the network.\n",
     435         [ +  - ]:         39 :                 CreateTxDoc(),
     436   [ +  -  +  - ]:         39 :                 RPCResult{
     437   [ +  -  +  - ]:         39 :                     RPCResult::Type::STR_HEX, "transaction", "hex string of the transaction"
     438                 :            :                 },
     439         [ +  - ]:         39 :                 RPCExamples{
     440   [ +  -  +  -  :         39 :                     HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"[{\\\"address\\\":0.01}]\"")
                   +  - ]
     441   [ +  -  +  -  :         39 :             + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"[{\\\"data\\\":\\\"00010203\\\"}]\"")
             +  -  +  - ]
     442   [ +  -  +  -  :         39 :             + HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"[{\\\"address\\\":0.01}]\"")
             +  -  +  - ]
     443   [ +  -  +  -  :         39 :             + HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"[{\\\"data\\\":\\\"00010203\\\"}]\"")
             +  -  +  - ]
     444                 :            :                 },
     445         [ +  - ]:         41 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     446                 :            : {
     447                 :          2 :     std::optional<bool> rbf;
     448         [ +  + ]:          2 :     if (!request.params[3].isNull()) {
     449                 :          1 :         rbf = request.params[3].get_bool();
     450                 :          1 :     }
     451                 :          2 :     CMutableTransaction rawTx = ConstructTransaction(request.params[0], request.params[1], request.params[2], rbf);
     452                 :            : 
     453   [ #  #  #  #  :          2 :     return EncodeHexTx(CTransaction(rawTx));
                   #  # ]
     454                 :          0 : },
     455                 :            :     };
     456                 :          0 : }
     457                 :            : 
     458                 :        217 : static RPCHelpMan decoderawtransaction()
     459                 :            : {
     460   [ +  -  +  -  :        434 :     return RPCHelpMan{"decoderawtransaction",
                   #  # ]
     461         [ +  - ]:        217 :                 "Return a JSON object representing the serialized, hex-encoded transaction.",
     462         [ +  - ]:        651 :                 {
     463   [ +  -  +  -  :        217 :                     {"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction hex string"},
                   +  - ]
     464   [ +  -  +  -  :        217 :                     {"iswitness", RPCArg::Type::BOOL, RPCArg::DefaultHint{"depends on heuristic tests"}, "Whether the transaction hex is a serialized witness transaction.\n"
             +  -  +  - ]
     465                 :            :                         "If iswitness is not present, heuristic tests will be used in decoding.\n"
     466                 :            :                         "If true, only witness deserialization will be tried.\n"
     467                 :            :                         "If false, only non-witness deserialization will be tried.\n"
     468                 :            :                         "This boolean should reflect whether the transaction has inputs\n"
     469                 :            :                         "(e.g. fully valid, or on-chain transactions), if known by the caller."
     470                 :            :                     },
     471                 :            :                 },
     472   [ +  -  +  - ]:        217 :                 RPCResult{
     473   [ +  -  +  - ]:        217 :                     RPCResult::Type::OBJ, "", "",
     474   [ +  -  +  - ]:        217 :                     DecodeTxDoc(/*txid_field_doc=*/"The transaction id"),
     475                 :            :                 },
     476         [ +  - ]:        217 :                 RPCExamples{
     477   [ +  -  +  -  :        217 :                     HelpExampleCli("decoderawtransaction", "\"hexstring\"")
                   +  - ]
     478   [ +  -  +  -  :        217 :             + HelpExampleRpc("decoderawtransaction", "\"hexstring\"")
             +  -  +  - ]
     479                 :            :                 },
     480         [ +  - ]:        396 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     481                 :            : {
     482                 :        179 :     CMutableTransaction mtx;
     483                 :            : 
     484   [ +  -  +  +  :        179 :     bool try_witness = request.params[1].isNull() ? true : request.params[1].get_bool();
             +  -  +  - ]
     485   [ +  -  +  +  :        179 :     bool try_no_witness = request.params[1].isNull() ? true : !request.params[1].get_bool();
             +  -  +  - ]
     486                 :            : 
     487   [ +  -  +  -  :        179 :     if (!DecodeHexTx(mtx, request.params[0].get_str(), try_no_witness, try_witness)) {
             +  -  +  + ]
     488   [ +  -  +  -  :          5 :         throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
             -  +  +  - ]
     489                 :            :     }
     490                 :            : 
     491         [ +  - ]:        174 :     UniValue result(UniValue::VOBJ);
     492   [ +  -  +  -  :        174 :     TxToUniv(CTransaction(std::move(mtx)), /*block_hash=*/uint256(), /*entry=*/result, /*include_hex=*/false);
                   +  - ]
     493                 :            : 
     494                 :        174 :     return result;
     495         [ +  - ]:        184 : },
     496                 :            :     };
     497                 :          0 : }
     498                 :            : 
     499                 :        481 : static RPCHelpMan decodescript()
     500                 :            : {
     501   [ +  -  #  #  :        481 :     return RPCHelpMan{
             #  #  #  # ]
     502         [ +  - ]:        481 :         "decodescript",
     503         [ +  - ]:        481 :         "\nDecode a hex-encoded script.\n",
     504         [ +  - ]:        962 :         {
     505   [ +  -  +  -  :        481 :             {"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded script"},
                   +  - ]
     506                 :            :         },
     507   [ +  -  +  - ]:        481 :         RPCResult{
     508   [ +  -  +  - ]:        481 :             RPCResult::Type::OBJ, "", "",
     509         [ +  - ]:       3367 :             {
     510   [ +  -  +  -  :        481 :                 {RPCResult::Type::STR, "asm", "Script public key"},
                   +  - ]
     511   [ +  -  +  -  :        481 :                 {RPCResult::Type::STR, "desc", "Inferred descriptor for the script"},
                   +  - ]
     512   [ +  -  +  -  :        481 :                 {RPCResult::Type::STR, "type", "The output type (e.g. " + GetAllOutputTypes() + ")"},
          +  -  +  -  +  
                      - ]
     513   [ +  -  +  -  :        481 :                 {RPCResult::Type::STR, "address", /*optional=*/true, "The Bitcoin address (only if a well-defined address exists)"},
                   +  - ]
     514   [ +  -  +  - ]:        962 :                 {RPCResult::Type::STR, "p2sh", /*optional=*/true,
     515         [ +  - ]:        481 :                  "address of P2SH script wrapping this redeem script (not returned for types that should not be wrapped)"},
     516   [ +  -  +  - ]:        962 :                 {RPCResult::Type::OBJ, "segwit", /*optional=*/true,
     517         [ +  - ]:        481 :                  "Result of a witness script public key wrapping this redeem script (not returned for types that should not be wrapped)",
     518         [ +  - ]:       3367 :                  {
     519   [ +  -  +  -  :        481 :                      {RPCResult::Type::STR, "asm", "String representation of the script public key"},
                   +  - ]
     520   [ +  -  +  -  :        481 :                      {RPCResult::Type::STR_HEX, "hex", "Hex string of the script public key"},
                   +  - ]
     521   [ +  -  +  -  :        481 :                      {RPCResult::Type::STR, "type", "The type of the script public key (e.g. witness_v0_keyhash or witness_v0_scripthash)"},
                   +  - ]
     522   [ +  -  +  -  :        481 :                      {RPCResult::Type::STR, "address", /*optional=*/true, "The Bitcoin address (only if a well-defined address exists)"},
                   +  - ]
     523   [ +  -  +  -  :        481 :                      {RPCResult::Type::STR, "desc", "Inferred descriptor for the script"},
                   +  - ]
     524   [ +  -  +  -  :        481 :                      {RPCResult::Type::STR, "p2sh-segwit", "address of the P2SH script wrapping this witness redeem script"},
                   +  - ]
     525                 :            :                  }},
     526                 :            :             },
     527                 :            :         },
     528         [ +  - ]:        481 :         RPCExamples{
     529   [ +  -  +  -  :        481 :             HelpExampleCli("decodescript", "\"hexstring\"")
                   +  - ]
     530   [ +  -  +  -  :        481 :           + HelpExampleRpc("decodescript", "\"hexstring\"")
             +  -  +  - ]
     531                 :            :         },
     532         [ +  - ]:        861 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     533                 :            : {
     534         [ +  - ]:        381 :     UniValue r(UniValue::VOBJ);
     535         [ +  - ]:        380 :     CScript script;
     536   [ +  -  +  -  :        380 :     if (request.params[0].get_str().size() > 0){
                   +  + ]
     537   [ +  -  +  -  :        379 :         std::vector<unsigned char> scriptData(ParseHexV(request.params[0], "argument"));
                   +  + ]
     538         [ +  - ]:        378 :         script = CScript(scriptData.begin(), scriptData.end());
     539                 :        378 :     } else {
     540                 :            :         // Empty scripts are valid
     541                 :            :     }
     542         [ +  - ]:        379 :     ScriptToUniv(script, /*out=*/r, /*include_hex=*/false, /*include_address=*/true);
     543                 :            : 
     544                 :        379 :     std::vector<std::vector<unsigned char>> solutions_data;
     545         [ +  - ]:        379 :     const TxoutType which_type{Solver(script, solutions_data)};
     546                 :            : 
     547         [ +  - ]:        758 :     const bool can_wrap{[&] {
     548      [ +  +  - ]:        379 :         switch (which_type) {
     549                 :            :         case TxoutType::MULTISIG:
     550                 :            :         case TxoutType::NONSTANDARD:
     551                 :            :         case TxoutType::PUBKEY:
     552                 :            :         case TxoutType::PUBKEYHASH:
     553                 :            :         case TxoutType::WITNESS_V0_KEYHASH:
     554                 :            :         case TxoutType::WITNESS_V0_SCRIPTHASH:
     555                 :            :             // Can be wrapped if the checks below pass
     556                 :        372 :             break;
     557                 :            :         case TxoutType::NULL_DATA:
     558                 :            :         case TxoutType::SCRIPTHASH:
     559                 :            :         case TxoutType::WITNESS_UNKNOWN:
     560                 :            :         case TxoutType::WITNESS_V1_TAPROOT:
     561                 :            :             // Should not be wrapped
     562                 :          7 :             return false;
     563                 :            :         } // no default case, so the compiler can warn about missing cases
     564   [ +  +  +  + ]:        372 :         if (!script.HasValidOps() || script.IsUnspendable()) {
     565                 :        143 :             return false;
     566                 :            :         }
     567         [ +  + ]:      31941 :         for (CScript::const_iterator it{script.begin()}; it != script.end();) {
     568                 :            :             opcodetype op;
     569                 :      31727 :             CHECK_NONFATAL(script.GetOp(it, op));
     570   [ +  -  +  + ]:      31727 :             if (op == OP_CHECKSIGADD || IsOpSuccess(op)) {
     571                 :         15 :                 return false;
     572                 :            :             }
     573                 :            :         }
     574                 :        214 :         return true;
     575                 :        379 :     }()};
     576                 :            : 
     577         [ +  + ]:        379 :     if (can_wrap) {
     578   [ +  -  +  -  :        214 :         r.pushKV("p2sh", EncodeDestination(ScriptHash(script)));
          +  -  +  -  +  
                      - ]
     579                 :            :         // P2SH and witness programs cannot be wrapped in P2WSH, if this script
     580                 :            :         // is a witness program, don't return addresses for a segwit programs.
     581         [ +  - ]:        428 :         const bool can_wrap_P2WSH{[&] {
     582   [ +  +  +  - ]:        214 :             switch (which_type) {
     583                 :            :             case TxoutType::MULTISIG:
     584                 :            :             case TxoutType::PUBKEY:
     585                 :            :             // Uncompressed pubkeys cannot be used with segwit checksigs.
     586                 :            :             // If the script contains an uncompressed pubkey, skip encoding of a segwit program.
     587         [ +  + ]:          2 :                 for (const auto& solution : solutions_data) {
     588   [ -  +  +  - ]:          1 :                     if ((solution.size() != 1) && !CPubKey(solution).IsCompressed()) {
     589                 :          0 :                         return false;
     590                 :            :                     }
     591                 :            :                 }
     592                 :          1 :                 return true;
     593                 :            :             case TxoutType::NONSTANDARD:
     594                 :            :             case TxoutType::PUBKEYHASH:
     595                 :            :                 // Can be P2WSH wrapped
     596                 :        211 :                 return true;
     597                 :            :             case TxoutType::NULL_DATA:
     598                 :            :             case TxoutType::SCRIPTHASH:
     599                 :            :             case TxoutType::WITNESS_UNKNOWN:
     600                 :            :             case TxoutType::WITNESS_V0_KEYHASH:
     601                 :            :             case TxoutType::WITNESS_V0_SCRIPTHASH:
     602                 :            :             case TxoutType::WITNESS_V1_TAPROOT:
     603                 :            :                 // Should not be wrapped
     604                 :          2 :                 return false;
     605                 :            :             } // no default case, so the compiler can warn about missing cases
     606         [ #  # ]:          0 :             NONFATAL_UNREACHABLE();
     607                 :        214 :         }()};
     608         [ +  + ]:        214 :         if (can_wrap_P2WSH) {
     609         [ +  - ]:        212 :             UniValue sr(UniValue::VOBJ);
     610         [ +  - ]:        212 :             CScript segwitScr;
     611                 :        212 :             FlatSigningProvider provider;
     612         [ +  + ]:        212 :             if (which_type == TxoutType::PUBKEY) {
     613   [ +  -  +  -  :          1 :                 segwitScr = GetScriptForDestination(WitnessV0KeyHash(Hash160(solutions_data[0])));
                   +  - ]
     614         [ -  + ]:        212 :             } else if (which_type == TxoutType::PUBKEYHASH) {
     615   [ #  #  #  #  :          0 :                 segwitScr = GetScriptForDestination(WitnessV0KeyHash(uint160{solutions_data[0]}));
             #  #  #  # ]
     616                 :          0 :             } else {
     617                 :            :                 // Scripts that are not fit for P2WPKH are encoded as P2WSH.
     618   [ +  -  +  -  :        211 :                 provider.scripts[CScriptID(script)] = script;
                   +  - ]
     619   [ +  -  -  + ]:        211 :                 segwitScr = GetScriptForDestination(WitnessV0ScriptHash(script));
     620                 :            :             }
     621         [ +  - ]:        212 :             ScriptToUniv(segwitScr, /*out=*/sr, /*include_hex=*/true, /*include_address=*/true, /*provider=*/&provider);
     622   [ +  -  +  -  :        212 :             sr.pushKV("p2sh-segwit", EncodeDestination(ScriptHash(segwitScr)));
          +  -  +  -  +  
                      - ]
     623   [ +  -  +  -  :        212 :             r.pushKV("segwit", sr);
                   +  - ]
     624                 :        212 :         }
     625                 :        214 :     }
     626                 :            : 
     627                 :        379 :     return r;
     628         [ +  - ]:        381 : },
     629                 :            :     };
     630                 :          0 : }
     631                 :            : 
     632                 :         79 : static RPCHelpMan combinerawtransaction()
     633                 :            : {
     634   [ +  -  -  +  :        158 :     return RPCHelpMan{"combinerawtransaction",
             #  #  #  # ]
     635         [ +  - ]:         79 :                 "\nCombine multiple partially signed transactions into one transaction.\n"
     636                 :            :                 "The combined transaction may be another partially signed transaction or a \n"
     637                 :            :                 "fully signed transaction.",
     638         [ +  - ]:        158 :                 {
     639   [ +  -  +  -  :        158 :                     {"txs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The hex strings of partially signed transactions",
                   +  - ]
     640         [ +  - ]:        158 :                         {
     641   [ +  -  +  -  :         79 :                             {"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "A hex-encoded raw transaction"},
                   +  - ]
     642                 :            :                         },
     643                 :            :                         },
     644                 :            :                 },
     645   [ +  -  +  - ]:         79 :                 RPCResult{
     646   [ +  -  +  - ]:         79 :                     RPCResult::Type::STR, "", "The hex-encoded raw transaction with signature(s)"
     647                 :            :                 },
     648         [ +  - ]:         79 :                 RPCExamples{
     649   [ +  -  +  -  :         79 :                     HelpExampleCli("combinerawtransaction", R"('["myhex1", "myhex2", "myhex3"]')")
                   +  - ]
     650                 :            :                 },
     651         [ +  - ]:        122 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     652                 :            : {
     653                 :            : 
     654                 :         43 :     UniValue txs = request.params[0].get_array();
     655   [ +  -  +  - ]:         43 :     std::vector<CMutableTransaction> txVariants(txs.size());
     656                 :            : 
     657   [ +  -  +  + ]:        276 :     for (unsigned int idx = 0; idx < txs.size(); idx++) {
     658   [ +  -  +  +  :        245 :         if (!DecodeHexTx(txVariants[idx], txs[idx].get_str())) {
             +  -  +  + ]
     659   [ +  -  +  -  :         11 :             throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed for tx %d. Make sure the tx has at least one input.", idx));
                   +  - ]
     660                 :            :         }
     661                 :        233 :     }
     662                 :            : 
     663         [ +  - ]:         31 :     if (txVariants.empty()) {
     664   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Missing transactions");
                   #  # ]
     665                 :            :     }
     666                 :            : 
     667                 :            :     // mergedTx will end up with all the signatures; it
     668                 :            :     // starts as a clone of the rawtx:
     669         [ +  - ]:         31 :     CMutableTransaction mergedTx(txVariants[0]);
     670                 :            : 
     671                 :            :     // Fetch previous transactions (inputs):
     672                 :         31 :     CCoinsView viewDummy;
     673         [ +  - ]:         31 :     CCoinsViewCache view(&viewDummy);
     674                 :            :     {
     675         [ +  - ]:         31 :         NodeContext& node = EnsureAnyNodeContext(request.context);
     676         [ +  - ]:         31 :         const CTxMemPool& mempool = EnsureMemPool(node);
     677         [ +  - ]:         31 :         ChainstateManager& chainman = EnsureChainman(node);
     678   [ +  -  +  - ]:         31 :         LOCK2(cs_main, mempool.cs);
     679   [ +  -  +  - ]:         31 :         CCoinsViewCache &viewChain = chainman.ActiveChainstate().CoinsTip();
     680         [ +  - ]:         31 :         CCoinsViewMemPool viewMempool(&viewChain, mempool);
     681         [ +  - ]:         31 :         view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
     682                 :            : 
     683         [ +  + ]:        242 :         for (const CTxIn& txin : mergedTx.vin) {
     684         [ +  - ]:        211 :             view.AccessCoin(txin.prevout); // Load entries from viewChain into view; can fail.
     685                 :            :         }
     686                 :            : 
     687         [ +  - ]:         31 :         view.SetBackend(viewDummy); // switch back to avoid locking mempool for too long
     688                 :         31 :     }
     689                 :            : 
     690                 :            :     // Use CTransaction for the constant parts of the
     691                 :            :     // transaction to avoid rehashing.
     692         [ +  - ]:         31 :     const CTransaction txConst(mergedTx);
     693                 :            :     // Sign what we can:
     694         [ +  + ]:         31 :     for (unsigned int i = 0; i < mergedTx.vin.size(); i++) {
     695                 :         16 :         CTxIn& txin = mergedTx.vin[i];
     696         [ +  - ]:         16 :         const Coin& coin = view.AccessCoin(txin.prevout);
     697   [ +  -  -  + ]:         16 :         if (coin.IsSpent()) {
     698   [ +  -  +  -  :         16 :             throw JSONRPCError(RPC_VERIFY_ERROR, "Input not found or already spent");
                   +  - ]
     699                 :            :         }
     700         [ #  # ]:          0 :         SignatureData sigdata;
     701                 :            : 
     702                 :            :         // ... and merge in other signatures:
     703         [ #  # ]:          0 :         for (const CMutableTransaction& txv : txVariants) {
     704         [ #  # ]:          0 :             if (txv.vin.size() > i) {
     705   [ #  #  #  # ]:          0 :                 sigdata.MergeSignatureData(DataFromTransaction(txv, i, coin.out));
     706                 :          0 :             }
     707                 :            :         }
     708   [ #  #  #  # ]:          0 :         ProduceSignature(DUMMY_SIGNING_PROVIDER, MutableTransactionSignatureCreator(mergedTx, i, coin.out.nValue, 1), coin.out.scriptPubKey, sigdata);
     709                 :            : 
     710         [ #  # ]:          0 :         UpdateInput(txin, sigdata);
     711                 :          0 :     }
     712                 :            : 
     713   [ +  -  +  -  :         15 :     return EncodeHexTx(CTransaction(mergedTx));
                   +  - ]
     714                 :         70 : },
     715                 :            :     };
     716                 :          0 : }
     717                 :            : 
     718                 :        232 : static RPCHelpMan signrawtransactionwithkey()
     719                 :            : {
     720   [ +  -  +  -  :        464 :     return RPCHelpMan{"signrawtransactionwithkey",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     721         [ +  - ]:        232 :                 "\nSign inputs for raw transaction (serialized, hex-encoded).\n"
     722                 :            :                 "The second argument is an array of base58-encoded private\n"
     723                 :            :                 "keys that will be the only keys used to sign the transaction.\n"
     724                 :            :                 "The third optional argument (may be null) is an array of previous transaction outputs that\n"
     725                 :            :                 "this transaction depends on but may not yet be in the block chain.\n",
     726         [ +  - ]:       1160 :                 {
     727   [ +  -  +  -  :        232 :                     {"hexstring", RPCArg::Type::STR, RPCArg::Optional::NO, "The transaction hex string"},
                   +  - ]
     728   [ +  -  +  -  :        464 :                     {"privkeys", RPCArg::Type::ARR, RPCArg::Optional::NO, "The base58-encoded private keys for signing",
                   +  - ]
     729         [ +  - ]:        464 :                         {
     730   [ +  -  +  -  :        232 :                             {"privatekey", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "private key in base58-encoding"},
                   +  - ]
     731                 :            :                         },
     732                 :            :                         },
     733   [ +  -  +  -  :        464 :                     {"prevtxs", RPCArg::Type::ARR, RPCArg::Optional::OMITTED, "The previous dependent transaction outputs",
                   +  - ]
     734         [ +  - ]:        464 :                         {
     735   [ +  -  +  -  :        464 :                             {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
                   +  - ]
     736         [ +  - ]:       1624 :                                 {
     737   [ +  -  +  -  :        232 :                                     {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id"},
                   +  - ]
     738   [ +  -  +  -  :        232 :                                     {"vout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The output number"},
                   +  - ]
     739   [ +  -  +  -  :        232 :                                     {"scriptPubKey", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "script key"},
                   +  - ]
     740   [ +  -  +  -  :        232 :                                     {"redeemScript", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "(required for P2SH) redeem script"},
                   +  - ]
     741   [ +  -  +  -  :        232 :                                     {"witnessScript", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "(required for P2WSH or P2SH-P2WSH) witness script"},
                   +  - ]
     742   [ +  -  +  -  :        232 :                                     {"amount", RPCArg::Type::AMOUNT, RPCArg::Optional::OMITTED, "(required for Segwit inputs) the amount spent"},
                   +  - ]
     743                 :            :                                 },
     744                 :            :                                 },
     745                 :            :                         },
     746                 :            :                         },
     747   [ +  -  +  -  :        232 :                     {"sighashtype", RPCArg::Type::STR, RPCArg::Default{"DEFAULT for Taproot, ALL otherwise"}, "The signature hash type. Must be one of:\n"
             +  -  +  - ]
     748                 :            :             "       \"DEFAULT\"\n"
     749                 :            :             "       \"ALL\"\n"
     750                 :            :             "       \"NONE\"\n"
     751                 :            :             "       \"SINGLE\"\n"
     752                 :            :             "       \"ALL|ANYONECANPAY\"\n"
     753                 :            :             "       \"NONE|ANYONECANPAY\"\n"
     754                 :            :             "       \"SINGLE|ANYONECANPAY\"\n"
     755                 :            :                     },
     756                 :            :                 },
     757   [ +  -  +  - ]:        232 :                 RPCResult{
     758   [ +  -  +  - ]:        232 :                     RPCResult::Type::OBJ, "", "",
     759         [ +  - ]:        928 :                     {
     760   [ +  -  +  -  :        232 :                         {RPCResult::Type::STR_HEX, "hex", "The hex-encoded raw transaction with signature(s)"},
                   +  - ]
     761   [ +  -  +  -  :        232 :                         {RPCResult::Type::BOOL, "complete", "If the transaction has a complete set of signatures"},
                   +  - ]
     762   [ +  -  +  -  :        464 :                         {RPCResult::Type::ARR, "errors", /*optional=*/true, "Script verification errors (if there are any)",
                   +  - ]
     763         [ +  - ]:        464 :                         {
     764   [ +  -  +  -  :        464 :                             {RPCResult::Type::OBJ, "", "",
                   +  - ]
     765         [ +  - ]:       1624 :                             {
     766   [ +  -  +  -  :        232 :                                 {RPCResult::Type::STR_HEX, "txid", "The hash of the referenced, previous transaction"},
                   +  - ]
     767   [ +  -  +  -  :        232 :                                 {RPCResult::Type::NUM, "vout", "The index of the output to spent and used as input"},
                   +  - ]
     768   [ +  -  +  -  :        464 :                                 {RPCResult::Type::ARR, "witness", "",
                   +  - ]
     769         [ +  - ]:        464 :                                 {
     770   [ +  -  +  -  :        232 :                                     {RPCResult::Type::STR_HEX, "witness", ""},
                   +  - ]
     771                 :            :                                 }},
     772   [ +  -  +  -  :        232 :                                 {RPCResult::Type::STR_HEX, "scriptSig", "The hex-encoded signature script"},
                   +  - ]
     773   [ +  -  +  -  :        232 :                                 {RPCResult::Type::NUM, "sequence", "Script sequence number"},
                   +  - ]
     774   [ +  -  +  -  :        232 :                                 {RPCResult::Type::STR, "error", "Verification or signing error related to the input"},
                   +  - ]
     775                 :            :                             }},
     776                 :            :                         }},
     777                 :            :                     }
     778                 :            :                 },
     779         [ +  - ]:        232 :                 RPCExamples{
     780   [ +  -  +  -  :        232 :                     HelpExampleCli("signrawtransactionwithkey", "\"myhex\" \"[\\\"key1\\\",\\\"key2\\\"]\"")
                   +  - ]
     781   [ +  -  +  -  :        232 :             + HelpExampleRpc("signrawtransactionwithkey", "\"myhex\", \"[\\\"key1\\\",\\\"key2\\\"]\"")
             +  -  +  - ]
     782                 :            :                 },
     783         [ +  - ]:        427 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     784                 :            : {
     785                 :        195 :     CMutableTransaction mtx;
     786   [ +  -  +  -  :        195 :     if (!DecodeHexTx(mtx, request.params[0].get_str())) {
             +  -  +  + ]
     787   [ +  -  +  -  :          1 :         throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed. Make sure the tx has at least one input.");
                   +  - ]
     788                 :            :     }
     789                 :            : 
     790                 :        194 :     FillableSigningProvider keystore;
     791   [ +  -  +  - ]:        194 :     const UniValue& keys = request.params[1].get_array();
     792         [ +  + ]:       4376 :     for (unsigned int idx = 0; idx < keys.size(); ++idx) {
     793   [ +  -  +  - ]:       4226 :         UniValue k = keys[idx];
     794   [ +  +  +  - ]:       4226 :         CKey key = DecodeSecret(k.get_str());
     795   [ +  -  +  + ]:       4225 :         if (!key.IsValid()) {
     796   [ +  -  +  -  :         43 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
                   +  - ]
     797                 :            :         }
     798         [ +  - ]:       4182 :         keystore.AddKey(key);
     799                 :       4226 :     }
     800                 :            : 
     801                 :            :     // Fetch previous transactions (inputs):
     802                 :        150 :     std::map<COutPoint, Coin> coins;
     803         [ +  + ]:       4136 :     for (const CTxIn& txin : mtx.vin) {
     804         [ +  - ]:       3986 :         coins[txin.prevout]; // Create empty map entry keyed by prevout.
     805                 :            :     }
     806         [ +  - ]:        150 :     NodeContext& node = EnsureAnyNodeContext(request.context);
     807         [ +  - ]:        150 :     FindCoins(node, coins);
     808                 :            : 
     809                 :            :     // Parse the prevtxs array
     810   [ +  -  +  + ]:        150 :     ParsePrevouts(request.params[2], &keystore, coins);
     811                 :            : 
     812         [ +  - ]:        143 :     UniValue result(UniValue::VOBJ);
     813   [ +  -  +  - ]:        143 :     SignTransaction(mtx, &keystore, coins, request.params[3], result);
     814                 :        143 :     return result;
     815         [ +  - ]:        240 : },
     816                 :            :     };
     817                 :          0 : }
     818                 :            : 
     819   [ -  +  #  #  :        173 : const RPCResult decodepsbt_inputs{
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     820   [ +  -  +  - ]:        173 :     RPCResult::Type::ARR, "inputs", "",
     821         [ +  - ]:        346 :     {
     822   [ +  -  +  -  :        346 :         {RPCResult::Type::OBJ, "", "",
                   +  - ]
     823         [ +  - ]:       3806 :         {
     824   [ +  -  +  -  :        346 :             {RPCResult::Type::OBJ, "non_witness_utxo", /*optional=*/true, "Decoded network transaction for non-witness UTXOs",
                   +  - ]
     825         [ +  - ]:        346 :             {
     826   [ +  -  +  -  :        173 :                 {RPCResult::Type::ELISION, "",""},
                   +  - ]
     827                 :            :             }},
     828   [ +  -  +  -  :        346 :             {RPCResult::Type::OBJ, "witness_utxo", /*optional=*/true, "Transaction output for witness UTXOs",
                   +  - ]
     829         [ +  - ]:        519 :             {
     830   [ +  -  +  -  :        173 :                 {RPCResult::Type::NUM, "amount", "The value in " + CURRENCY_UNIT},
                   +  - ]
     831   [ +  -  +  -  :        346 :                 {RPCResult::Type::OBJ, "scriptPubKey", "",
                   +  - ]
     832         [ +  - ]:       1038 :                 {
     833   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "asm", "Disassembly of the public key script"},
                   +  - ]
     834   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "desc", "Inferred descriptor for the output"},
                   +  - ]
     835   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR_HEX, "hex", "The raw public key script bytes, hex-encoded"},
                   +  - ]
     836   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "type", "The type, eg 'pubkeyhash'"},
                   +  - ]
     837   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "address", /*optional=*/true, "The Bitcoin address (only if a well-defined address exists)"},
                   +  - ]
     838                 :            :                 }},
     839                 :            :             }},
     840   [ +  -  +  -  :        346 :             {RPCResult::Type::OBJ_DYN, "partial_signatures", /*optional=*/true, "",
                   +  - ]
     841         [ +  - ]:        346 :             {
     842   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR, "pubkey", "The public key and signature that corresponds to it."},
                   +  - ]
     843                 :            :             }},
     844   [ +  -  +  -  :        173 :             {RPCResult::Type::STR, "sighash", /*optional=*/true, "The sighash type to be used"},
                   +  - ]
     845   [ +  -  +  -  :        346 :             {RPCResult::Type::OBJ, "redeem_script", /*optional=*/true, "",
                   +  - ]
     846         [ +  - ]:        692 :             {
     847   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR, "asm", "Disassembly of the redeem script"},
                   +  - ]
     848   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR_HEX, "hex", "The raw redeem script bytes, hex-encoded"},
                   +  - ]
     849   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR, "type", "The type, eg 'pubkeyhash'"},
                   +  - ]
     850                 :            :             }},
     851   [ +  -  +  -  :        346 :             {RPCResult::Type::OBJ, "witness_script", /*optional=*/true, "",
                   +  - ]
     852         [ +  - ]:        692 :             {
     853   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR, "asm", "Disassembly of the witness script"},
                   +  - ]
     854   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR_HEX, "hex", "The raw witness script bytes, hex-encoded"},
                   +  - ]
     855   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR, "type", "The type, eg 'pubkeyhash'"},
                   +  - ]
     856                 :            :             }},
     857   [ +  -  +  -  :        346 :             {RPCResult::Type::ARR, "bip32_derivs", /*optional=*/true, "",
                   +  - ]
     858         [ +  - ]:        346 :             {
     859   [ +  -  +  -  :        346 :                 {RPCResult::Type::OBJ, "", "",
                   +  - ]
     860         [ +  - ]:        692 :                 {
     861   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "pubkey", "The public key with the derivation path as the value."},
                   +  - ]
     862   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "master_fingerprint", "The fingerprint of the master key"},
                   +  - ]
     863   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "path", "The path"},
                   +  - ]
     864                 :            :                 }},
     865                 :            :             }},
     866   [ +  -  +  -  :        346 :             {RPCResult::Type::OBJ, "final_scriptSig", /*optional=*/true, "",
                   +  - ]
     867         [ +  - ]:        519 :             {
     868   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR, "asm", "Disassembly of the final signature script"},
                   +  - ]
     869   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR_HEX, "hex", "The raw final signature script bytes, hex-encoded"},
                   +  - ]
     870                 :            :             }},
     871   [ +  -  +  -  :        346 :             {RPCResult::Type::ARR, "final_scriptwitness", /*optional=*/true, "",
                   +  - ]
     872         [ +  - ]:        346 :             {
     873   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR_HEX, "", "hex-encoded witness data (if any)"},
                   +  - ]
     874                 :            :             }},
     875   [ +  -  +  -  :        346 :             {RPCResult::Type::OBJ_DYN, "ripemd160_preimages", /*optional=*/ true, "",
                   +  - ]
     876         [ +  - ]:        346 :             {
     877   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR, "hash", "The hash and preimage that corresponds to it."},
                   +  - ]
     878                 :            :             }},
     879   [ +  -  +  -  :        346 :             {RPCResult::Type::OBJ_DYN, "sha256_preimages", /*optional=*/ true, "",
                   +  - ]
     880         [ +  - ]:        346 :             {
     881   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR, "hash", "The hash and preimage that corresponds to it."},
                   +  - ]
     882                 :            :             }},
     883   [ +  -  +  -  :        346 :             {RPCResult::Type::OBJ_DYN, "hash160_preimages", /*optional=*/ true, "",
                   +  - ]
     884         [ +  - ]:        346 :             {
     885   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR, "hash", "The hash and preimage that corresponds to it."},
                   +  - ]
     886                 :            :             }},
     887   [ +  -  +  -  :        346 :             {RPCResult::Type::OBJ_DYN, "hash256_preimages", /*optional=*/ true, "",
                   +  - ]
     888         [ +  - ]:        346 :             {
     889   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR, "hash", "The hash and preimage that corresponds to it."},
                   +  - ]
     890                 :            :             }},
     891   [ +  -  +  -  :        173 :             {RPCResult::Type::STR_HEX, "taproot_key_path_sig", /*optional=*/ true, "hex-encoded signature for the Taproot key path spend"},
                   +  - ]
     892   [ +  -  +  -  :        346 :             {RPCResult::Type::ARR, "taproot_script_path_sigs", /*optional=*/ true, "",
                   +  - ]
     893         [ +  - ]:        346 :             {
     894   [ +  -  +  -  :        346 :                 {RPCResult::Type::OBJ, "signature", /*optional=*/ true, "The signature for the pubkey and leaf hash combination",
                   +  - ]
     895         [ +  - ]:        692 :                 {
     896   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "pubkey", "The x-only pubkey for this signature"},
                   +  - ]
     897   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "leaf_hash", "The leaf hash for this signature"},
                   +  - ]
     898   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "sig", "The signature itself"},
                   +  - ]
     899                 :            :                 }},
     900                 :            :             }},
     901   [ +  -  +  -  :        346 :             {RPCResult::Type::ARR, "taproot_scripts", /*optional=*/ true, "",
                   +  - ]
     902         [ +  - ]:        346 :             {
     903   [ +  -  +  -  :        346 :                 {RPCResult::Type::OBJ, "", "",
                   +  - ]
     904         [ +  - ]:        692 :                 {
     905   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR_HEX, "script", "A leaf script"},
                   +  - ]
     906   [ +  -  +  -  :        173 :                     {RPCResult::Type::NUM, "leaf_ver", "The version number for the leaf script"},
                   +  - ]
     907   [ +  -  +  -  :        346 :                     {RPCResult::Type::ARR, "control_blocks", "The control blocks for this script",
                   +  - ]
     908         [ +  - ]:        346 :                     {
     909   [ +  -  +  -  :        173 :                         {RPCResult::Type::STR_HEX, "control_block", "A hex-encoded control block for this script"},
                   +  - ]
     910                 :            :                     }},
     911                 :            :                 }},
     912                 :            :             }},
     913   [ +  -  +  -  :        346 :             {RPCResult::Type::ARR, "taproot_bip32_derivs", /*optional=*/ true, "",
                   +  - ]
     914         [ +  - ]:        346 :             {
     915   [ +  -  +  -  :        346 :                 {RPCResult::Type::OBJ, "", "",
                   +  - ]
     916         [ +  - ]:        865 :                 {
     917   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "pubkey", "The x-only public key this path corresponds to"},
                   +  - ]
     918   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "master_fingerprint", "The fingerprint of the master key"},
                   +  - ]
     919   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "path", "The path"},
                   +  - ]
     920   [ +  -  +  -  :        346 :                     {RPCResult::Type::ARR, "leaf_hashes", "The hashes of the leaves this pubkey appears in",
                   +  - ]
     921         [ +  - ]:        346 :                     {
     922   [ +  -  +  -  :        173 :                         {RPCResult::Type::STR_HEX, "hash", "The hash of a leaf this pubkey appears in"},
                   +  - ]
     923                 :            :                     }},
     924                 :            :                 }},
     925                 :            :             }},
     926   [ +  -  +  -  :        173 :             {RPCResult::Type::STR_HEX, "taproot_internal_key", /*optional=*/ true, "The hex-encoded Taproot x-only internal key"},
                   +  - ]
     927   [ +  -  +  -  :        173 :             {RPCResult::Type::STR_HEX, "taproot_merkle_root", /*optional=*/ true, "The hex-encoded Taproot merkle root"},
                   +  - ]
     928   [ +  -  +  -  :        346 :             {RPCResult::Type::OBJ_DYN, "unknown", /*optional=*/ true, "The unknown input fields",
                   +  - ]
     929         [ +  - ]:        346 :             {
     930   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR_HEX, "key", "(key-value pair) An unknown key-value pair"},
                   +  - ]
     931                 :            :             }},
     932   [ +  -  +  -  :        346 :             {RPCResult::Type::ARR, "proprietary", /*optional=*/true, "The input proprietary map",
                   +  - ]
     933         [ +  - ]:        346 :             {
     934   [ +  -  +  -  :        346 :                 {RPCResult::Type::OBJ, "", "",
                   +  - ]
     935         [ +  - ]:        865 :                 {
     936   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR_HEX, "identifier", "The hex string for the proprietary identifier"},
                   +  - ]
     937   [ +  -  +  -  :        173 :                     {RPCResult::Type::NUM, "subtype", "The number for the subtype"},
                   +  - ]
     938   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR_HEX, "key", "The hex for the key"},
                   +  - ]
     939   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR_HEX, "value", "The hex for the value"},
                   +  - ]
     940                 :            :                 }},
     941                 :            :             }},
     942                 :            :         }},
     943                 :            :     }
     944                 :            : };
     945                 :            : 
     946   [ -  +  #  #  :        173 : const RPCResult decodepsbt_outputs{
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     947   [ +  -  +  - ]:        173 :     RPCResult::Type::ARR, "outputs", "",
     948         [ +  - ]:        346 :     {
     949   [ +  -  +  -  :        346 :         {RPCResult::Type::OBJ, "", "",
                   +  - ]
     950         [ +  - ]:       1557 :         {
     951   [ +  -  +  -  :        346 :             {RPCResult::Type::OBJ, "redeem_script", /*optional=*/true, "",
                   +  - ]
     952         [ +  - ]:        692 :             {
     953   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR, "asm", "Disassembly of the redeem script"},
                   +  - ]
     954   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR_HEX, "hex", "The raw redeem script bytes, hex-encoded"},
                   +  - ]
     955   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR, "type", "The type, eg 'pubkeyhash'"},
                   +  - ]
     956                 :            :             }},
     957   [ +  -  +  -  :        346 :             {RPCResult::Type::OBJ, "witness_script", /*optional=*/true, "",
                   +  - ]
     958         [ +  - ]:        692 :             {
     959   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR, "asm", "Disassembly of the witness script"},
                   +  - ]
     960   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR_HEX, "hex", "The raw witness script bytes, hex-encoded"},
                   +  - ]
     961   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR, "type", "The type, eg 'pubkeyhash'"},
                   +  - ]
     962                 :            :             }},
     963   [ +  -  +  -  :        346 :             {RPCResult::Type::ARR, "bip32_derivs", /*optional=*/true, "",
                   +  - ]
     964         [ +  - ]:        346 :             {
     965   [ +  -  +  -  :        346 :                 {RPCResult::Type::OBJ, "", "",
                   +  - ]
     966         [ +  - ]:        692 :                 {
     967   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "pubkey", "The public key this path corresponds to"},
                   +  - ]
     968   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "master_fingerprint", "The fingerprint of the master key"},
                   +  - ]
     969   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "path", "The path"},
                   +  - ]
     970                 :            :                 }},
     971                 :            :             }},
     972   [ +  -  +  -  :        173 :             {RPCResult::Type::STR_HEX, "taproot_internal_key", /*optional=*/ true, "The hex-encoded Taproot x-only internal key"},
                   +  - ]
     973   [ +  -  +  -  :        346 :             {RPCResult::Type::ARR, "taproot_tree", /*optional=*/ true, "The tuples that make up the Taproot tree, in depth first search order",
                   +  - ]
     974         [ +  - ]:        346 :             {
     975   [ +  -  +  -  :        346 :                 {RPCResult::Type::OBJ, "tuple", /*optional=*/ true, "A single leaf script in the taproot tree",
                   +  - ]
     976         [ +  - ]:        692 :                 {
     977   [ +  -  +  -  :        173 :                     {RPCResult::Type::NUM, "depth", "The depth of this element in the tree"},
                   +  - ]
     978   [ +  -  +  -  :        173 :                     {RPCResult::Type::NUM, "leaf_ver", "The version of this leaf"},
                   +  - ]
     979   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "script", "The hex-encoded script itself"},
                   +  - ]
     980                 :            :                 }},
     981                 :            :             }},
     982   [ +  -  +  -  :        346 :             {RPCResult::Type::ARR, "taproot_bip32_derivs", /*optional=*/ true, "",
                   +  - ]
     983         [ +  - ]:        346 :             {
     984   [ +  -  +  -  :        346 :                 {RPCResult::Type::OBJ, "", "",
                   +  - ]
     985         [ +  - ]:        865 :                 {
     986   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "pubkey", "The x-only public key this path corresponds to"},
                   +  - ]
     987   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "master_fingerprint", "The fingerprint of the master key"},
                   +  - ]
     988   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR, "path", "The path"},
                   +  - ]
     989   [ +  -  +  -  :        346 :                     {RPCResult::Type::ARR, "leaf_hashes", "The hashes of the leaves this pubkey appears in",
                   +  - ]
     990         [ +  - ]:        346 :                     {
     991   [ +  -  +  -  :        173 :                         {RPCResult::Type::STR_HEX, "hash", "The hash of a leaf this pubkey appears in"},
                   +  - ]
     992                 :            :                     }},
     993                 :            :                 }},
     994                 :            :             }},
     995   [ +  -  +  -  :        346 :             {RPCResult::Type::OBJ_DYN, "unknown", /*optional=*/true, "The unknown output fields",
                   +  - ]
     996         [ +  - ]:        346 :             {
     997   [ +  -  +  -  :        173 :                 {RPCResult::Type::STR_HEX, "key", "(key-value pair) An unknown key-value pair"},
                   +  - ]
     998                 :            :             }},
     999   [ +  -  +  -  :        346 :             {RPCResult::Type::ARR, "proprietary", /*optional=*/true, "The output proprietary map",
                   +  - ]
    1000         [ +  - ]:        346 :             {
    1001   [ +  -  +  -  :        346 :                 {RPCResult::Type::OBJ, "", "",
                   +  - ]
    1002         [ +  - ]:        865 :                 {
    1003   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR_HEX, "identifier", "The hex string for the proprietary identifier"},
                   +  - ]
    1004   [ +  -  +  -  :        173 :                     {RPCResult::Type::NUM, "subtype", "The number for the subtype"},
                   +  - ]
    1005   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR_HEX, "key", "The hex for the key"},
                   +  - ]
    1006   [ +  -  +  -  :        173 :                     {RPCResult::Type::STR_HEX, "value", "The hex for the value"},
                   +  - ]
    1007                 :            :                 }},
    1008                 :            :             }},
    1009                 :            :         }},
    1010                 :            :     }
    1011                 :            : };
    1012                 :            : 
    1013                 :         39 : static RPCHelpMan decodepsbt()
    1014                 :            : {
    1015   [ -  +  #  #  :         39 :     return RPCHelpMan{
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1016         [ +  - ]:         39 :         "decodepsbt",
    1017         [ +  - ]:         39 :         "Return a JSON object representing the serialized, base64-encoded partially signed Bitcoin transaction.",
    1018         [ +  - ]:         78 :                 {
    1019   [ +  -  +  -  :         39 :                     {"psbt", RPCArg::Type::STR, RPCArg::Optional::NO, "The PSBT base64 string"},
                   +  - ]
    1020                 :            :                 },
    1021   [ +  -  +  - ]:         39 :                 RPCResult{
    1022   [ +  -  +  - ]:         39 :                     RPCResult::Type::OBJ, "", "",
    1023         [ +  - ]:        273 :                     {
    1024   [ +  -  +  -  :         78 :                         {RPCResult::Type::OBJ, "tx", "The decoded network-serialized unsigned transaction.",
                   +  - ]
    1025         [ +  - ]:         78 :                         {
    1026   [ +  -  +  -  :         39 :                             {RPCResult::Type::ELISION, "", "The layout is the same as the output of decoderawtransaction."},
                   +  - ]
    1027                 :            :                         }},
    1028   [ +  -  +  -  :         78 :                         {RPCResult::Type::ARR, "global_xpubs", "",
                   +  - ]
    1029         [ +  - ]:         78 :                         {
    1030   [ +  -  +  -  :         78 :                             {RPCResult::Type::OBJ, "", "",
                   +  - ]
    1031         [ +  - ]:        156 :                             {
    1032   [ +  -  +  -  :         39 :                                 {RPCResult::Type::STR, "xpub", "The extended public key this path corresponds to"},
                   +  - ]
    1033   [ +  -  +  -  :         39 :                                 {RPCResult::Type::STR_HEX, "master_fingerprint", "The fingerprint of the master key"},
                   +  - ]
    1034   [ +  -  +  -  :         39 :                                 {RPCResult::Type::STR, "path", "The path"},
                   +  - ]
    1035                 :            :                             }},
    1036                 :            :                         }},
    1037   [ +  -  +  -  :         39 :                         {RPCResult::Type::NUM, "psbt_version", "The PSBT version number. Not to be confused with the unsigned transaction version"},
                   +  - ]
    1038   [ +  -  +  -  :         78 :                         {RPCResult::Type::ARR, "proprietary", "The global proprietary map",
                   +  - ]
    1039         [ +  - ]:         78 :                         {
    1040   [ +  -  +  -  :         78 :                             {RPCResult::Type::OBJ, "", "",
                   +  - ]
    1041         [ +  - ]:        195 :                             {
    1042   [ +  -  +  -  :         39 :                                 {RPCResult::Type::STR_HEX, "identifier", "The hex string for the proprietary identifier"},
                   +  - ]
    1043   [ +  -  +  -  :         39 :                                 {RPCResult::Type::NUM, "subtype", "The number for the subtype"},
                   +  - ]
    1044   [ +  -  +  -  :         39 :                                 {RPCResult::Type::STR_HEX, "key", "The hex for the key"},
                   +  - ]
    1045   [ +  -  +  -  :         39 :                                 {RPCResult::Type::STR_HEX, "value", "The hex for the value"},
                   +  - ]
    1046                 :            :                             }},
    1047                 :            :                         }},
    1048   [ +  -  +  -  :         78 :                         {RPCResult::Type::OBJ_DYN, "unknown", "The unknown global fields",
                   +  - ]
    1049         [ +  - ]:         78 :                         {
    1050   [ +  -  +  -  :         39 :                              {RPCResult::Type::STR_HEX, "key", "(key-value pair) An unknown key-value pair"},
                   +  - ]
    1051                 :            :                         }},
    1052         [ +  - ]:         39 :                         decodepsbt_inputs,
    1053         [ +  - ]:         39 :                         decodepsbt_outputs,
    1054   [ +  -  +  -  :         39 :                         {RPCResult::Type::STR_AMOUNT, "fee", /*optional=*/true, "The transaction fee paid if all UTXOs slots in the PSBT have been filled."},
                   +  - ]
    1055                 :            :                     }
    1056                 :            :                 },
    1057         [ +  - ]:         39 :                 RPCExamples{
    1058   [ +  -  +  -  :         39 :                     HelpExampleCli("decodepsbt", "\"psbt\"")
                   +  - ]
    1059                 :            :                 },
    1060         [ +  - ]:         41 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
    1061                 :            : {
    1062                 :            :     // Unserialize the transactions
    1063                 :          2 :     PartiallySignedTransaction psbtx;
    1064                 :          2 :     std::string error;
    1065   [ +  -  +  -  :          2 :     if (!DecodeBase64PSBT(psbtx, request.params[0].get_str(), error)) {
             +  -  -  + ]
    1066   [ +  -  +  -  :          2 :         throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed %s", error));
             +  -  +  - ]
    1067                 :            :     }
    1068                 :            : 
    1069         [ #  # ]:          0 :     UniValue result(UniValue::VOBJ);
    1070                 :            : 
    1071                 :            :     // Add the decoded tx
    1072         [ #  # ]:          0 :     UniValue tx_univ(UniValue::VOBJ);
    1073   [ #  #  #  #  :          0 :     TxToUniv(CTransaction(*psbtx.tx), /*block_hash=*/uint256(), /*entry=*/tx_univ, /*include_hex=*/false);
             #  #  #  # ]
    1074   [ #  #  #  #  :          0 :     result.pushKV("tx", tx_univ);
                   #  # ]
    1075                 :            : 
    1076                 :            :     // Add the global xpubs
    1077         [ #  # ]:          0 :     UniValue global_xpubs(UniValue::VARR);
    1078   [ #  #  #  # ]:          0 :     for (std::pair<KeyOriginInfo, std::set<CExtPubKey>> xpub_pair : psbtx.m_xpubs) {
    1079         [ #  # ]:          0 :         for (auto& xpub : xpub_pair.second) {
    1080                 :          0 :             std::vector<unsigned char> ser_xpub;
    1081         [ #  # ]:          0 :             ser_xpub.assign(BIP32_EXTKEY_WITH_VERSION_SIZE, 0);
    1082         [ #  # ]:          0 :             xpub.EncodeWithVersion(ser_xpub.data());
    1083                 :            : 
    1084         [ #  # ]:          0 :             UniValue keypath(UniValue::VOBJ);
    1085   [ #  #  #  #  :          0 :             keypath.pushKV("xpub", EncodeBase58Check(ser_xpub));
          #  #  #  #  #  
                      # ]
    1086   [ #  #  #  #  :          0 :             keypath.pushKV("master_fingerprint", HexStr(Span<unsigned char>(xpub_pair.first.fingerprint, xpub_pair.first.fingerprint + 4)));
             #  #  #  # ]
    1087   [ #  #  #  #  :          0 :             keypath.pushKV("path", WriteHDKeypath(xpub_pair.first.path));
             #  #  #  # ]
    1088   [ #  #  #  # ]:          0 :             global_xpubs.push_back(keypath);
    1089                 :          0 :         }
    1090                 :          0 :     }
    1091   [ #  #  #  #  :          0 :     result.pushKV("global_xpubs", global_xpubs);
                   #  # ]
    1092                 :            : 
    1093                 :            :     // PSBT version
    1094   [ #  #  #  #  :          0 :     result.pushKV("psbt_version", static_cast<uint64_t>(psbtx.GetVersion()));
             #  #  #  # ]
    1095                 :            : 
    1096                 :            :     // Proprietary
    1097         [ #  # ]:          0 :     UniValue proprietary(UniValue::VARR);
    1098         [ #  # ]:          0 :     for (const auto& entry : psbtx.m_proprietary) {
    1099         [ #  # ]:          0 :         UniValue this_prop(UniValue::VOBJ);
    1100   [ #  #  #  #  :          0 :         this_prop.pushKV("identifier", HexStr(entry.identifier));
          #  #  #  #  #  
                      # ]
    1101   [ #  #  #  #  :          0 :         this_prop.pushKV("subtype", entry.subtype);
                   #  # ]
    1102   [ #  #  #  #  :          0 :         this_prop.pushKV("key", HexStr(entry.key));
          #  #  #  #  #  
                      # ]
    1103   [ #  #  #  #  :          0 :         this_prop.pushKV("value", HexStr(entry.value));
          #  #  #  #  #  
                      # ]
    1104   [ #  #  #  # ]:          0 :         proprietary.push_back(this_prop);
    1105                 :          0 :     }
    1106   [ #  #  #  #  :          0 :     result.pushKV("proprietary", proprietary);
                   #  # ]
    1107                 :            : 
    1108                 :            :     // Unknown data
    1109         [ #  # ]:          0 :     UniValue unknowns(UniValue::VOBJ);
    1110   [ #  #  #  # ]:          0 :     for (auto entry : psbtx.unknown) {
    1111   [ #  #  #  #  :          0 :         unknowns.pushKV(HexStr(entry.first), HexStr(entry.second));
          #  #  #  #  #  
                #  #  # ]
    1112                 :          0 :     }
    1113   [ #  #  #  #  :          0 :     result.pushKV("unknown", unknowns);
                   #  # ]
    1114                 :            : 
    1115                 :            :     // inputs
    1116                 :          0 :     CAmount total_in = 0;
    1117                 :          0 :     bool have_all_utxos = true;
    1118         [ #  # ]:          0 :     UniValue inputs(UniValue::VARR);
    1119         [ #  # ]:          0 :     for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
    1120                 :          0 :         const PSBTInput& input = psbtx.inputs[i];
    1121         [ #  # ]:          0 :         UniValue in(UniValue::VOBJ);
    1122                 :            :         // UTXOs
    1123                 :          0 :         bool have_a_utxo = false;
    1124         [ #  # ]:          0 :         CTxOut txout;
    1125         [ #  # ]:          0 :         if (!input.witness_utxo.IsNull()) {
    1126         [ #  # ]:          0 :             txout = input.witness_utxo;
    1127                 :            : 
    1128         [ #  # ]:          0 :             UniValue o(UniValue::VOBJ);
    1129         [ #  # ]:          0 :             ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
    1130                 :            : 
    1131         [ #  # ]:          0 :             UniValue out(UniValue::VOBJ);
    1132   [ #  #  #  #  :          0 :             out.pushKV("amount", ValueFromAmount(txout.nValue));
                   #  # ]
    1133   [ #  #  #  #  :          0 :             out.pushKV("scriptPubKey", o);
                   #  # ]
    1134                 :            : 
    1135   [ #  #  #  #  :          0 :             in.pushKV("witness_utxo", out);
                   #  # ]
    1136                 :            : 
    1137                 :          0 :             have_a_utxo = true;
    1138                 :          0 :         }
    1139   [ #  #  #  # ]:          0 :         if (input.non_witness_utxo) {
    1140         [ #  # ]:          0 :             txout = input.non_witness_utxo->vout[psbtx.tx->vin[i].prevout.n];
    1141                 :            : 
    1142         [ #  # ]:          0 :             UniValue non_wit(UniValue::VOBJ);
    1143   [ #  #  #  # ]:          0 :             TxToUniv(*input.non_witness_utxo, /*block_hash=*/uint256(), /*entry=*/non_wit, /*include_hex=*/false);
    1144   [ #  #  #  #  :          0 :             in.pushKV("non_witness_utxo", non_wit);
                   #  # ]
    1145                 :            : 
    1146                 :          0 :             have_a_utxo = true;
    1147                 :          0 :         }
    1148         [ #  # ]:          0 :         if (have_a_utxo) {
    1149   [ #  #  #  #  :          0 :             if (MoneyRange(txout.nValue) && MoneyRange(total_in + txout.nValue)) {
             #  #  #  # ]
    1150                 :          0 :                 total_in += txout.nValue;
    1151                 :          0 :             } else {
    1152                 :            :                 // Hack to just not show fee later
    1153                 :          0 :                 have_all_utxos = false;
    1154                 :            :             }
    1155                 :          0 :         } else {
    1156                 :          0 :             have_all_utxos = false;
    1157                 :            :         }
    1158                 :            : 
    1159                 :            :         // Partial sigs
    1160         [ #  # ]:          0 :         if (!input.partial_sigs.empty()) {
    1161         [ #  # ]:          0 :             UniValue partial_sigs(UniValue::VOBJ);
    1162         [ #  # ]:          0 :             for (const auto& sig : input.partial_sigs) {
    1163   [ #  #  #  #  :          0 :                 partial_sigs.pushKV(HexStr(sig.second.first), HexStr(sig.second.second));
          #  #  #  #  #  
                #  #  # ]
    1164                 :            :             }
    1165   [ #  #  #  #  :          0 :             in.pushKV("partial_signatures", partial_sigs);
                   #  # ]
    1166                 :          0 :         }
    1167                 :            : 
    1168                 :            :         // Sighash
    1169         [ #  # ]:          0 :         if (input.sighash_type != std::nullopt) {
    1170   [ #  #  #  #  :          0 :             in.pushKV("sighash", SighashToStr((unsigned char)*input.sighash_type));
          #  #  #  #  #  
                      # ]
    1171                 :          0 :         }
    1172                 :            : 
    1173                 :            :         // Redeem script and witness script
    1174   [ #  #  #  # ]:          0 :         if (!input.redeem_script.empty()) {
    1175         [ #  # ]:          0 :             UniValue r(UniValue::VOBJ);
    1176         [ #  # ]:          0 :             ScriptToUniv(input.redeem_script, /*out=*/r);
    1177   [ #  #  #  #  :          0 :             in.pushKV("redeem_script", r);
                   #  # ]
    1178                 :          0 :         }
    1179   [ #  #  #  # ]:          0 :         if (!input.witness_script.empty()) {
    1180         [ #  # ]:          0 :             UniValue r(UniValue::VOBJ);
    1181         [ #  # ]:          0 :             ScriptToUniv(input.witness_script, /*out=*/r);
    1182   [ #  #  #  #  :          0 :             in.pushKV("witness_script", r);
                   #  # ]
    1183                 :          0 :         }
    1184                 :            : 
    1185                 :            :         // keypaths
    1186         [ #  # ]:          0 :         if (!input.hd_keypaths.empty()) {
    1187         [ #  # ]:          0 :             UniValue keypaths(UniValue::VARR);
    1188   [ #  #  #  # ]:          0 :             for (auto entry : input.hd_keypaths) {
    1189         [ #  # ]:          0 :                 UniValue keypath(UniValue::VOBJ);
    1190   [ #  #  #  #  :          0 :                 keypath.pushKV("pubkey", HexStr(entry.first));
          #  #  #  #  #  
                      # ]
    1191                 :            : 
    1192   [ #  #  #  #  :          0 :                 keypath.pushKV("master_fingerprint", strprintf("%08x", ReadBE32(entry.second.fingerprint)));
          #  #  #  #  #  
                      # ]
    1193   [ #  #  #  #  :          0 :                 keypath.pushKV("path", WriteHDKeypath(entry.second.path));
             #  #  #  # ]
    1194   [ #  #  #  # ]:          0 :                 keypaths.push_back(keypath);
    1195                 :          0 :             }
    1196   [ #  #  #  #  :          0 :             in.pushKV("bip32_derivs", keypaths);
                   #  # ]
    1197                 :          0 :         }
    1198                 :            : 
    1199                 :            :         // Final scriptSig and scriptwitness
    1200   [ #  #  #  # ]:          0 :         if (!input.final_script_sig.empty()) {
    1201         [ #  # ]:          0 :             UniValue scriptsig(UniValue::VOBJ);
    1202   [ #  #  #  #  :          0 :             scriptsig.pushKV("asm", ScriptToAsmStr(input.final_script_sig, true));
             #  #  #  # ]
    1203   [ #  #  #  #  :          0 :             scriptsig.pushKV("hex", HexStr(input.final_script_sig));
          #  #  #  #  #  
                      # ]
    1204   [ #  #  #  #  :          0 :             in.pushKV("final_scriptSig", scriptsig);
                   #  # ]
    1205                 :          0 :         }
    1206   [ #  #  #  # ]:          0 :         if (!input.final_script_witness.IsNull()) {
    1207         [ #  # ]:          0 :             UniValue txinwitness(UniValue::VARR);
    1208         [ #  # ]:          0 :             for (const auto& item : input.final_script_witness.stack) {
    1209   [ #  #  #  #  :          0 :                 txinwitness.push_back(HexStr(item));
             #  #  #  # ]
    1210                 :            :             }
    1211   [ #  #  #  #  :          0 :             in.pushKV("final_scriptwitness", txinwitness);
                   #  # ]
    1212                 :          0 :         }
    1213                 :            : 
    1214                 :            :         // Ripemd160 hash preimages
    1215         [ #  # ]:          0 :         if (!input.ripemd160_preimages.empty()) {
    1216         [ #  # ]:          0 :             UniValue ripemd160_preimages(UniValue::VOBJ);
    1217         [ #  # ]:          0 :             for (const auto& [hash, preimage] : input.ripemd160_preimages) {
    1218   [ #  #  #  #  :          0 :                 ripemd160_preimages.pushKV(HexStr(hash), HexStr(preimage));
          #  #  #  #  #  
                #  #  # ]
    1219                 :            :             }
    1220   [ #  #  #  #  :          0 :             in.pushKV("ripemd160_preimages", ripemd160_preimages);
                   #  # ]
    1221                 :          0 :         }
    1222                 :            : 
    1223                 :            :         // Sha256 hash preimages
    1224         [ #  # ]:          0 :         if (!input.sha256_preimages.empty()) {
    1225         [ #  # ]:          0 :             UniValue sha256_preimages(UniValue::VOBJ);
    1226         [ #  # ]:          0 :             for (const auto& [hash, preimage] : input.sha256_preimages) {
    1227   [ #  #  #  #  :          0 :                 sha256_preimages.pushKV(HexStr(hash), HexStr(preimage));
          #  #  #  #  #  
                #  #  # ]
    1228                 :            :             }
    1229   [ #  #  #  #  :          0 :             in.pushKV("sha256_preimages", sha256_preimages);
                   #  # ]
    1230                 :          0 :         }
    1231                 :            : 
    1232                 :            :         // Hash160 hash preimages
    1233         [ #  # ]:          0 :         if (!input.hash160_preimages.empty()) {
    1234         [ #  # ]:          0 :             UniValue hash160_preimages(UniValue::VOBJ);
    1235         [ #  # ]:          0 :             for (const auto& [hash, preimage] : input.hash160_preimages) {
    1236   [ #  #  #  #  :          0 :                 hash160_preimages.pushKV(HexStr(hash), HexStr(preimage));
          #  #  #  #  #  
                #  #  # ]
    1237                 :            :             }
    1238   [ #  #  #  #  :          0 :             in.pushKV("hash160_preimages", hash160_preimages);
                   #  # ]
    1239                 :          0 :         }
    1240                 :            : 
    1241                 :            :         // Hash256 hash preimages
    1242         [ #  # ]:          0 :         if (!input.hash256_preimages.empty()) {
    1243         [ #  # ]:          0 :             UniValue hash256_preimages(UniValue::VOBJ);
    1244         [ #  # ]:          0 :             for (const auto& [hash, preimage] : input.hash256_preimages) {
    1245   [ #  #  #  #  :          0 :                 hash256_preimages.pushKV(HexStr(hash), HexStr(preimage));
          #  #  #  #  #  
                #  #  # ]
    1246                 :            :             }
    1247   [ #  #  #  #  :          0 :             in.pushKV("hash256_preimages", hash256_preimages);
                   #  # ]
    1248                 :          0 :         }
    1249                 :            : 
    1250                 :            :         // Taproot key path signature
    1251         [ #  # ]:          0 :         if (!input.m_tap_key_sig.empty()) {
    1252   [ #  #  #  #  :          0 :             in.pushKV("taproot_key_path_sig", HexStr(input.m_tap_key_sig));
          #  #  #  #  #  
                      # ]
    1253                 :          0 :         }
    1254                 :            : 
    1255                 :            :         // Taproot script path signatures
    1256         [ #  # ]:          0 :         if (!input.m_tap_script_sigs.empty()) {
    1257         [ #  # ]:          0 :             UniValue script_sigs(UniValue::VARR);
    1258         [ #  # ]:          0 :             for (const auto& [pubkey_leaf, sig] : input.m_tap_script_sigs) {
    1259                 :          0 :                 const auto& [xonly, leaf_hash] = pubkey_leaf;
    1260         [ #  # ]:          0 :                 UniValue sigobj(UniValue::VOBJ);
    1261   [ #  #  #  #  :          0 :                 sigobj.pushKV("pubkey", HexStr(xonly));
          #  #  #  #  #  
                      # ]
    1262   [ #  #  #  #  :          0 :                 sigobj.pushKV("leaf_hash", HexStr(leaf_hash));
          #  #  #  #  #  
                      # ]
    1263   [ #  #  #  #  :          0 :                 sigobj.pushKV("sig", HexStr(sig));
          #  #  #  #  #  
                      # ]
    1264   [ #  #  #  # ]:          0 :                 script_sigs.push_back(sigobj);
    1265                 :          0 :             }
    1266   [ #  #  #  #  :          0 :             in.pushKV("taproot_script_path_sigs", script_sigs);
                   #  # ]
    1267                 :          0 :         }
    1268                 :            : 
    1269                 :            :         // Taproot leaf scripts
    1270         [ #  # ]:          0 :         if (!input.m_tap_scripts.empty()) {
    1271         [ #  # ]:          0 :             UniValue tap_scripts(UniValue::VARR);
    1272         [ #  # ]:          0 :             for (const auto& [leaf, control_blocks] : input.m_tap_scripts) {
    1273                 :          0 :                 const auto& [script, leaf_ver] = leaf;
    1274         [ #  # ]:          0 :                 UniValue script_info(UniValue::VOBJ);
    1275   [ #  #  #  #  :          0 :                 script_info.pushKV("script", HexStr(script));
          #  #  #  #  #  
                      # ]
    1276   [ #  #  #  #  :          0 :                 script_info.pushKV("leaf_ver", leaf_ver);
                   #  # ]
    1277         [ #  # ]:          0 :                 UniValue control_blocks_univ(UniValue::VARR);
    1278         [ #  # ]:          0 :                 for (const auto& control_block : control_blocks) {
    1279   [ #  #  #  #  :          0 :                     control_blocks_univ.push_back(HexStr(control_block));
             #  #  #  # ]
    1280                 :            :                 }
    1281   [ #  #  #  #  :          0 :                 script_info.pushKV("control_blocks", control_blocks_univ);
                   #  # ]
    1282   [ #  #  #  # ]:          0 :                 tap_scripts.push_back(script_info);
    1283                 :          0 :             }
    1284   [ #  #  #  #  :          0 :             in.pushKV("taproot_scripts", tap_scripts);
                   #  # ]
    1285                 :          0 :         }
    1286                 :            : 
    1287                 :            :         // Taproot bip32 keypaths
    1288         [ #  # ]:          0 :         if (!input.m_tap_bip32_paths.empty()) {
    1289         [ #  # ]:          0 :             UniValue keypaths(UniValue::VARR);
    1290         [ #  # ]:          0 :             for (const auto& [xonly, leaf_origin] : input.m_tap_bip32_paths) {
    1291                 :          0 :                 const auto& [leaf_hashes, origin] = leaf_origin;
    1292         [ #  # ]:          0 :                 UniValue path_obj(UniValue::VOBJ);
    1293   [ #  #  #  #  :          0 :                 path_obj.pushKV("pubkey", HexStr(xonly));
          #  #  #  #  #  
                      # ]
    1294   [ #  #  #  #  :          0 :                 path_obj.pushKV("master_fingerprint", strprintf("%08x", ReadBE32(origin.fingerprint)));
          #  #  #  #  #  
                      # ]
    1295   [ #  #  #  #  :          0 :                 path_obj.pushKV("path", WriteHDKeypath(origin.path));
             #  #  #  # ]
    1296         [ #  # ]:          0 :                 UniValue leaf_hashes_arr(UniValue::VARR);
    1297         [ #  # ]:          0 :                 for (const auto& leaf_hash : leaf_hashes) {
    1298   [ #  #  #  #  :          0 :                     leaf_hashes_arr.push_back(HexStr(leaf_hash));
             #  #  #  # ]
    1299                 :            :                 }
    1300   [ #  #  #  #  :          0 :                 path_obj.pushKV("leaf_hashes", leaf_hashes_arr);
                   #  # ]
    1301   [ #  #  #  # ]:          0 :                 keypaths.push_back(path_obj);
    1302                 :          0 :             }
    1303   [ #  #  #  #  :          0 :             in.pushKV("taproot_bip32_derivs", keypaths);
                   #  # ]
    1304                 :          0 :         }
    1305                 :            : 
    1306                 :            :         // Taproot internal key
    1307   [ #  #  #  # ]:          0 :         if (!input.m_tap_internal_key.IsNull()) {
    1308   [ #  #  #  #  :          0 :             in.pushKV("taproot_internal_key", HexStr(input.m_tap_internal_key));
          #  #  #  #  #  
                      # ]
    1309                 :          0 :         }
    1310                 :            : 
    1311                 :            :         // Write taproot merkle root
    1312   [ #  #  #  # ]:          0 :         if (!input.m_tap_merkle_root.IsNull()) {
    1313   [ #  #  #  #  :          0 :             in.pushKV("taproot_merkle_root", HexStr(input.m_tap_merkle_root));
          #  #  #  #  #  
                      # ]
    1314                 :          0 :         }
    1315                 :            : 
    1316                 :            :         // Proprietary
    1317         [ #  # ]:          0 :         if (!input.m_proprietary.empty()) {
    1318         [ #  # ]:          0 :             UniValue proprietary(UniValue::VARR);
    1319         [ #  # ]:          0 :             for (const auto& entry : input.m_proprietary) {
    1320         [ #  # ]:          0 :                 UniValue this_prop(UniValue::VOBJ);
    1321   [ #  #  #  #  :          0 :                 this_prop.pushKV("identifier", HexStr(entry.identifier));
          #  #  #  #  #  
                      # ]
    1322   [ #  #  #  #  :          0 :                 this_prop.pushKV("subtype", entry.subtype);
                   #  # ]
    1323   [ #  #  #  #  :          0 :                 this_prop.pushKV("key", HexStr(entry.key));
          #  #  #  #  #  
                      # ]
    1324   [ #  #  #  #  :          0 :                 this_prop.pushKV("value", HexStr(entry.value));
          #  #  #  #  #  
                      # ]
    1325   [ #  #  #  # ]:          0 :                 proprietary.push_back(this_prop);
    1326                 :          0 :             }
    1327   [ #  #  #  #  :          0 :             in.pushKV("proprietary", proprietary);
                   #  # ]
    1328                 :          0 :         }
    1329                 :            : 
    1330                 :            :         // Unknown data
    1331         [ #  # ]:          0 :         if (input.unknown.size() > 0) {
    1332         [ #  # ]:          0 :             UniValue unknowns(UniValue::VOBJ);
    1333   [ #  #  #  # ]:          0 :             for (auto entry : input.unknown) {
    1334   [ #  #  #  #  :          0 :                 unknowns.pushKV(HexStr(entry.first), HexStr(entry.second));
          #  #  #  #  #  
                #  #  # ]
    1335                 :          0 :             }
    1336   [ #  #  #  #  :          0 :             in.pushKV("unknown", unknowns);
                   #  # ]
    1337                 :          0 :         }
    1338                 :            : 
    1339   [ #  #  #  # ]:          0 :         inputs.push_back(in);
    1340                 :          0 :     }
    1341   [ #  #  #  #  :          0 :     result.pushKV("inputs", inputs);
                   #  # ]
    1342                 :            : 
    1343                 :            :     // outputs
    1344                 :          0 :     CAmount output_value = 0;
    1345         [ #  # ]:          0 :     UniValue outputs(UniValue::VARR);
    1346         [ #  # ]:          0 :     for (unsigned int i = 0; i < psbtx.outputs.size(); ++i) {
    1347                 :          0 :         const PSBTOutput& output = psbtx.outputs[i];
    1348         [ #  # ]:          0 :         UniValue out(UniValue::VOBJ);
    1349                 :            :         // Redeem script and witness script
    1350   [ #  #  #  # ]:          0 :         if (!output.redeem_script.empty()) {
    1351         [ #  # ]:          0 :             UniValue r(UniValue::VOBJ);
    1352         [ #  # ]:          0 :             ScriptToUniv(output.redeem_script, /*out=*/r);
    1353   [ #  #  #  #  :          0 :             out.pushKV("redeem_script", r);
                   #  # ]
    1354                 :          0 :         }
    1355   [ #  #  #  # ]:          0 :         if (!output.witness_script.empty()) {
    1356         [ #  # ]:          0 :             UniValue r(UniValue::VOBJ);
    1357         [ #  # ]:          0 :             ScriptToUniv(output.witness_script, /*out=*/r);
    1358   [ #  #  #  #  :          0 :             out.pushKV("witness_script", r);
                   #  # ]
    1359                 :          0 :         }
    1360                 :            : 
    1361                 :            :         // keypaths
    1362         [ #  # ]:          0 :         if (!output.hd_keypaths.empty()) {
    1363         [ #  # ]:          0 :             UniValue keypaths(UniValue::VARR);
    1364   [ #  #  #  # ]:          0 :             for (auto entry : output.hd_keypaths) {
    1365         [ #  # ]:          0 :                 UniValue keypath(UniValue::VOBJ);
    1366   [ #  #  #  #  :          0 :                 keypath.pushKV("pubkey", HexStr(entry.first));
          #  #  #  #  #  
                      # ]
    1367   [ #  #  #  #  :          0 :                 keypath.pushKV("master_fingerprint", strprintf("%08x", ReadBE32(entry.second.fingerprint)));
          #  #  #  #  #  
                      # ]
    1368   [ #  #  #  #  :          0 :                 keypath.pushKV("path", WriteHDKeypath(entry.second.path));
             #  #  #  # ]
    1369   [ #  #  #  # ]:          0 :                 keypaths.push_back(keypath);
    1370                 :          0 :             }
    1371   [ #  #  #  #  :          0 :             out.pushKV("bip32_derivs", keypaths);
                   #  # ]
    1372                 :          0 :         }
    1373                 :            : 
    1374                 :            :         // Taproot internal key
    1375   [ #  #  #  # ]:          0 :         if (!output.m_tap_internal_key.IsNull()) {
    1376   [ #  #  #  #  :          0 :             out.pushKV("taproot_internal_key", HexStr(output.m_tap_internal_key));
          #  #  #  #  #  
                      # ]
    1377                 :          0 :         }
    1378                 :            : 
    1379                 :            :         // Taproot tree
    1380         [ #  # ]:          0 :         if (!output.m_tap_tree.empty()) {
    1381         [ #  # ]:          0 :             UniValue tree(UniValue::VARR);
    1382         [ #  # ]:          0 :             for (const auto& [depth, leaf_ver, script] : output.m_tap_tree) {
    1383         [ #  # ]:          0 :                 UniValue elem(UniValue::VOBJ);
    1384   [ #  #  #  #  :          0 :                 elem.pushKV("depth", (int)depth);
                   #  # ]
    1385   [ #  #  #  #  :          0 :                 elem.pushKV("leaf_ver", (int)leaf_ver);
                   #  # ]
    1386   [ #  #  #  #  :          0 :                 elem.pushKV("script", HexStr(script));
          #  #  #  #  #  
                      # ]
    1387   [ #  #  #  # ]:          0 :                 tree.push_back(elem);
    1388                 :          0 :             }
    1389   [ #  #  #  #  :          0 :             out.pushKV("taproot_tree", tree);
                   #  # ]
    1390                 :          0 :         }
    1391                 :            : 
    1392                 :            :         // Taproot bip32 keypaths
    1393         [ #  # ]:          0 :         if (!output.m_tap_bip32_paths.empty()) {
    1394         [ #  # ]:          0 :             UniValue keypaths(UniValue::VARR);
    1395         [ #  # ]:          0 :             for (const auto& [xonly, leaf_origin] : output.m_tap_bip32_paths) {
    1396                 :          0 :                 const auto& [leaf_hashes, origin] = leaf_origin;
    1397         [ #  # ]:          0 :                 UniValue path_obj(UniValue::VOBJ);
    1398   [ #  #  #  #  :          0 :                 path_obj.pushKV("pubkey", HexStr(xonly));
          #  #  #  #  #  
                      # ]
    1399   [ #  #  #  #  :          0 :                 path_obj.pushKV("master_fingerprint", strprintf("%08x", ReadBE32(origin.fingerprint)));
          #  #  #  #  #  
                      # ]
    1400   [ #  #  #  #  :          0 :                 path_obj.pushKV("path", WriteHDKeypath(origin.path));
             #  #  #  # ]
    1401         [ #  # ]:          0 :                 UniValue leaf_hashes_arr(UniValue::VARR);
    1402         [ #  # ]:          0 :                 for (const auto& leaf_hash : leaf_hashes) {
    1403   [ #  #  #  #  :          0 :                     leaf_hashes_arr.push_back(HexStr(leaf_hash));
             #  #  #  # ]
    1404                 :            :                 }
    1405   [ #  #  #  #  :          0 :                 path_obj.pushKV("leaf_hashes", leaf_hashes_arr);
                   #  # ]
    1406   [ #  #  #  # ]:          0 :                 keypaths.push_back(path_obj);
    1407                 :          0 :             }
    1408   [ #  #  #  #  :          0 :             out.pushKV("taproot_bip32_derivs", keypaths);
                   #  # ]
    1409                 :          0 :         }
    1410                 :            : 
    1411                 :            :         // Proprietary
    1412         [ #  # ]:          0 :         if (!output.m_proprietary.empty()) {
    1413         [ #  # ]:          0 :             UniValue proprietary(UniValue::VARR);
    1414         [ #  # ]:          0 :             for (const auto& entry : output.m_proprietary) {
    1415         [ #  # ]:          0 :                 UniValue this_prop(UniValue::VOBJ);
    1416   [ #  #  #  #  :          0 :                 this_prop.pushKV("identifier", HexStr(entry.identifier));
          #  #  #  #  #  
                      # ]
    1417   [ #  #  #  #  :          0 :                 this_prop.pushKV("subtype", entry.subtype);
                   #  # ]
    1418   [ #  #  #  #  :          0 :                 this_prop.pushKV("key", HexStr(entry.key));
          #  #  #  #  #  
                      # ]
    1419   [ #  #  #  #  :          0 :                 this_prop.pushKV("value", HexStr(entry.value));
          #  #  #  #  #  
                      # ]
    1420   [ #  #  #  # ]:          0 :                 proprietary.push_back(this_prop);
    1421                 :          0 :             }
    1422   [ #  #  #  #  :          0 :             out.pushKV("proprietary", proprietary);
                   #  # ]
    1423                 :          0 :         }
    1424                 :            : 
    1425                 :            :         // Unknown data
    1426         [ #  # ]:          0 :         if (output.unknown.size() > 0) {
    1427         [ #  # ]:          0 :             UniValue unknowns(UniValue::VOBJ);
    1428   [ #  #  #  # ]:          0 :             for (auto entry : output.unknown) {
    1429   [ #  #  #  #  :          0 :                 unknowns.pushKV(HexStr(entry.first), HexStr(entry.second));
          #  #  #  #  #  
                #  #  # ]
    1430                 :          0 :             }
    1431   [ #  #  #  #  :          0 :             out.pushKV("unknown", unknowns);
                   #  # ]
    1432                 :          0 :         }
    1433                 :            : 
    1434   [ #  #  #  # ]:          0 :         outputs.push_back(out);
    1435                 :            : 
    1436                 :            :         // Fee calculation
    1437   [ #  #  #  #  :          0 :         if (MoneyRange(psbtx.tx->vout[i].nValue) && MoneyRange(output_value + psbtx.tx->vout[i].nValue)) {
             #  #  #  # ]
    1438                 :          0 :             output_value += psbtx.tx->vout[i].nValue;
    1439                 :          0 :         } else {
    1440                 :            :             // Hack to just not show fee later
    1441                 :          0 :             have_all_utxos = false;
    1442                 :            :         }
    1443                 :          0 :     }
    1444   [ #  #  #  #  :          0 :     result.pushKV("outputs", outputs);
                   #  # ]
    1445         [ #  # ]:          0 :     if (have_all_utxos) {
    1446   [ #  #  #  #  :          0 :         result.pushKV("fee", ValueFromAmount(total_in - output_value));
                   #  # ]
    1447                 :          0 :     }
    1448                 :            : 
    1449                 :          0 :     return result;
    1450         [ #  # ]:          4 : },
    1451                 :            :     };
    1452                 :          0 : }
    1453                 :            : 
    1454                 :        467 : static RPCHelpMan combinepsbt()
    1455                 :            : {
    1456   [ +  -  -  +  :        934 :     return RPCHelpMan{"combinepsbt",
             #  #  #  # ]
    1457         [ +  - ]:        467 :                 "\nCombine multiple partially signed Bitcoin transactions into one transaction.\n"
    1458                 :            :                 "Implements the Combiner role.\n",
    1459         [ +  - ]:        934 :                 {
    1460   [ +  -  +  -  :        934 :                     {"txs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The base64 strings of partially signed transactions",
                   +  - ]
    1461         [ +  - ]:        934 :                         {
    1462   [ +  -  +  -  :        467 :                             {"psbt", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "A base64 string of a PSBT"},
                   +  - ]
    1463                 :            :                         },
    1464                 :            :                         },
    1465                 :            :                 },
    1466   [ +  -  +  - ]:        467 :                 RPCResult{
    1467   [ +  -  +  - ]:        467 :                     RPCResult::Type::STR, "", "The base64-encoded partially signed transaction"
    1468                 :            :                 },
    1469         [ +  - ]:        467 :                 RPCExamples{
    1470   [ +  -  +  -  :        467 :                     HelpExampleCli("combinepsbt", R"('["mybase64_1", "mybase64_2", "mybase64_3"]')")
                   +  - ]
    1471                 :            :                 },
    1472         [ +  - ]:        870 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
    1473                 :            : {
    1474                 :            :     // Unserialize the transactions
    1475                 :        403 :     std::vector<PartiallySignedTransaction> psbtxs;
    1476   [ +  -  +  -  :        403 :     UniValue txs = request.params[0].get_array();
                   +  - ]
    1477   [ +  -  +  - ]:        403 :     if (txs.empty()) {
    1478   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter 'txs' cannot be empty");
                   #  # ]
    1479                 :            :     }
    1480         [ +  + ]:       4502 :     for (unsigned int i = 0; i < txs.size(); ++i) {
    1481         [ +  - ]:       4165 :         PartiallySignedTransaction psbtx;
    1482                 :       4165 :         std::string error;
    1483   [ +  -  +  +  :       4165 :         if (!DecodeBase64PSBT(psbtx, txs[i].get_str(), error)) {
             +  -  +  + ]
    1484   [ +  -  +  -  :         65 :             throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed %s", error));
                   +  - ]
    1485                 :            :         }
    1486         [ +  - ]:       4099 :         psbtxs.push_back(psbtx);
    1487                 :       4165 :     }
    1488                 :            : 
    1489         [ +  - ]:        337 :     PartiallySignedTransaction merged_psbt;
    1490         [ +  - ]:        337 :     const TransactionError error = CombinePSBTs(merged_psbt, psbtxs);
    1491         [ +  + ]:        337 :     if (error != TransactionError::OK) {
    1492   [ +  -  +  -  :         40 :         throw JSONRPCTransactionError(error);
                   +  - ]
    1493                 :            :     }
    1494                 :            : 
    1495         [ +  - ]:        297 :     CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
    1496         [ +  - ]:        297 :     ssTx << merged_psbt;
    1497   [ +  -  +  -  :        297 :     return EncodeBase64(ssTx);
                   +  - ]
    1498                 :        509 : },
    1499                 :            :     };
    1500                 :          0 : }
    1501                 :            : 
    1502                 :        539 : static RPCHelpMan finalizepsbt()
    1503                 :            : {
    1504   [ +  -  -  +  :       1078 :     return RPCHelpMan{"finalizepsbt",
             #  #  #  # ]
    1505         [ +  - ]:        539 :                 "Finalize the inputs of a PSBT. If the transaction is fully signed, it will produce a\n"
    1506                 :            :                 "network serialized transaction which can be broadcast with sendrawtransaction. Otherwise a PSBT will be\n"
    1507                 :            :                 "created which has the final_scriptSig and final_scriptWitness fields filled for inputs that are complete.\n"
    1508                 :            :                 "Implements the Finalizer and Extractor roles.\n",
    1509         [ +  - ]:       1617 :                 {
    1510   [ +  -  +  -  :        539 :                     {"psbt", RPCArg::Type::STR, RPCArg::Optional::NO, "A base64 string of a PSBT"},
                   +  - ]
    1511   [ +  -  +  -  :        539 :                     {"extract", RPCArg::Type::BOOL, RPCArg::Default{true}, "If true and the transaction is complete,\n"
             +  -  +  - ]
    1512                 :            :             "                             extract and return the complete transaction in normal network serialization instead of the PSBT."},
    1513                 :            :                 },
    1514   [ +  -  +  - ]:        539 :                 RPCResult{
    1515   [ +  -  +  - ]:        539 :                     RPCResult::Type::OBJ, "", "",
    1516         [ +  - ]:       2156 :                     {
    1517   [ +  -  +  -  :        539 :                         {RPCResult::Type::STR, "psbt", /*optional=*/true, "The base64-encoded partially signed transaction if not extracted"},
                   +  - ]
    1518   [ +  -  +  -  :        539 :                         {RPCResult::Type::STR_HEX, "hex", /*optional=*/true, "The hex-encoded network transaction if extracted"},
                   +  - ]
    1519   [ +  -  +  -  :        539 :                         {RPCResult::Type::BOOL, "complete", "If the transaction has a complete set of signatures"},
                   +  - ]
    1520                 :            :                     }
    1521                 :            :                 },
    1522         [ +  - ]:        539 :                 RPCExamples{
    1523   [ +  -  +  -  :        539 :                     HelpExampleCli("finalizepsbt", "\"psbt\"")
                   +  - ]
    1524                 :            :                 },
    1525         [ +  - ]:       1018 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
    1526                 :            : {
    1527                 :            :     // Unserialize the transactions
    1528                 :        479 :     PartiallySignedTransaction psbtx;
    1529                 :        479 :     std::string error;
    1530   [ +  -  +  -  :        479 :     if (!DecodeBase64PSBT(psbtx, request.params[0].get_str(), error)) {
             +  -  +  + ]
    1531   [ +  -  +  -  :          8 :         throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed %s", error));
             -  +  +  - ]
    1532                 :            :     }
    1533                 :            : 
    1534   [ +  -  +  +  :        475 :     bool extract = request.params[1].isNull() || (!request.params[1].isNull() && request.params[1].get_bool());
          +  -  +  -  +  
                -  +  - ]
    1535                 :            : 
    1536         [ +  - ]:        471 :     CMutableTransaction mtx;
    1537         [ +  - ]:        471 :     bool complete = FinalizeAndExtractPSBT(psbtx, mtx);
    1538                 :            : 
    1539         [ +  - ]:        471 :     UniValue result(UniValue::VOBJ);
    1540         [ +  - ]:        471 :     CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
    1541                 :        471 :     std::string result_str;
    1542                 :            : 
    1543   [ +  +  +  + ]:        471 :     if (complete && extract) {
    1544         [ +  - ]:          3 :         ssTx << mtx;
    1545   [ +  -  +  - ]:          3 :         result_str = HexStr(ssTx);
    1546   [ +  -  +  -  :          3 :         result.pushKV("hex", result_str);
                   +  - ]
    1547                 :          3 :     } else {
    1548         [ +  - ]:        468 :         ssTx << psbtx;
    1549   [ +  -  +  - ]:        468 :         result_str = EncodeBase64(ssTx.str());
    1550   [ +  -  +  -  :        468 :         result.pushKV("psbt", result_str);
                   +  - ]
    1551                 :            :     }
    1552   [ +  -  +  -  :        471 :     result.pushKV("complete", complete);
                   +  - ]
    1553                 :            : 
    1554                 :        471 :     return result;
    1555         [ +  - ]:        487 : },
    1556                 :            :     };
    1557                 :          0 : }
    1558                 :            : 
    1559                 :         44 : static RPCHelpMan createpsbt()
    1560                 :            : {
    1561   [ +  -  -  + ]:         88 :     return RPCHelpMan{"createpsbt",
    1562         [ +  - ]:         44 :                 "\nCreates a transaction in the Partially Signed Transaction format.\n"
    1563                 :            :                 "Implements the Creator role.\n",
    1564         [ +  - ]:         44 :                 CreateTxDoc(),
    1565   [ +  -  +  - ]:         44 :                 RPCResult{
    1566   [ +  -  +  - ]:         44 :                     RPCResult::Type::STR, "", "The resulting raw transaction (base64-encoded string)"
    1567                 :            :                 },
    1568         [ +  - ]:         44 :                 RPCExamples{
    1569   [ +  -  +  -  :         44 :                     HelpExampleCli("createpsbt", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"[{\\\"data\\\":\\\"00010203\\\"}]\"")
                   +  - ]
    1570                 :            :                 },
    1571         [ +  - ]:         46 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
    1572                 :            : {
    1573                 :            : 
    1574                 :          2 :     std::optional<bool> rbf;
    1575         [ +  + ]:          2 :     if (!request.params[3].isNull()) {
    1576                 :          1 :         rbf = request.params[3].get_bool();
    1577                 :          1 :     }
    1578                 :          2 :     CMutableTransaction rawTx = ConstructTransaction(request.params[0], request.params[1], request.params[2], rbf);
    1579                 :            : 
    1580                 :            :     // Make a blank psbt
    1581         [ #  # ]:          2 :     PartiallySignedTransaction psbtx;
    1582         [ #  # ]:          0 :     psbtx.tx = rawTx;
    1583         [ #  # ]:          0 :     for (unsigned int i = 0; i < rawTx.vin.size(); ++i) {
    1584   [ #  #  #  # ]:          0 :         psbtx.inputs.push_back(PSBTInput());
    1585                 :          0 :     }
    1586         [ #  # ]:          0 :     for (unsigned int i = 0; i < rawTx.vout.size(); ++i) {
    1587   [ #  #  #  # ]:          0 :         psbtx.outputs.push_back(PSBTOutput());
    1588                 :          0 :     }
    1589                 :            : 
    1590                 :            :     // Serialize the PSBT
    1591         [ #  # ]:          0 :     CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
    1592         [ #  # ]:          0 :     ssTx << psbtx;
    1593                 :            : 
    1594   [ #  #  #  #  :          0 :     return EncodeBase64(ssTx);
                   #  # ]
    1595                 :          0 : },
    1596                 :            :     };
    1597                 :          0 : }
    1598                 :            : 
    1599                 :        107 : static RPCHelpMan converttopsbt()
    1600                 :            : {
    1601   [ +  -  -  +  :        214 :     return RPCHelpMan{"converttopsbt",
                   #  # ]
    1602         [ +  - ]:        107 :                 "\nConverts a network serialized transaction to a PSBT. This should be used only with createrawtransaction and fundrawtransaction\n"
    1603                 :            :                 "createpsbt and walletcreatefundedpsbt should be used for new applications.\n",
    1604         [ +  - ]:        428 :                 {
    1605   [ +  -  +  -  :        107 :                     {"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex string of a raw transaction"},
                   +  - ]
    1606   [ +  -  +  -  :        107 :                     {"permitsigdata", RPCArg::Type::BOOL, RPCArg::Default{false}, "If true, any signatures in the input will be discarded and conversion\n"
             +  -  +  - ]
    1607                 :            :                             "                              will continue. If false, RPC will fail if any signatures are present."},
    1608   [ +  -  +  -  :        107 :                     {"iswitness", RPCArg::Type::BOOL, RPCArg::DefaultHint{"depends on heuristic tests"}, "Whether the transaction hex is a serialized witness transaction.\n"
             +  -  +  - ]
    1609                 :            :                         "If iswitness is not present, heuristic tests will be used in decoding.\n"
    1610                 :            :                         "If true, only witness deserialization will be tried.\n"
    1611                 :            :                         "If false, only non-witness deserialization will be tried.\n"
    1612                 :            :                         "This boolean should reflect whether the transaction has inputs\n"
    1613                 :            :                         "(e.g. fully valid, or on-chain transactions), if known by the caller."
    1614                 :            :                     },
    1615                 :            :                 },
    1616   [ +  -  +  - ]:        107 :                 RPCResult{
    1617   [ +  -  +  - ]:        107 :                     RPCResult::Type::STR, "", "The resulting raw transaction (base64-encoded string)"
    1618                 :            :                 },
    1619         [ +  - ]:        107 :                 RPCExamples{
    1620                 :            :                             "\nCreate a transaction\n"
    1621   [ +  -  +  -  :        107 :                             + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"[{\\\"data\\\":\\\"00010203\\\"}]\"") +
          +  -  +  -  +  
                      - ]
    1622                 :            :                             "\nConvert the transaction to a PSBT\n"
    1623   [ +  -  +  -  :        107 :                             + HelpExampleCli("converttopsbt", "\"rawtransaction\"")
             +  -  +  - ]
    1624                 :            :                 },
    1625         [ +  - ]:        176 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
    1626                 :            : {
    1627                 :            :     // parse hex string from parameter
    1628                 :         69 :     CMutableTransaction tx;
    1629   [ +  -  +  +  :         69 :     bool permitsigdata = request.params[1].isNull() ? false : request.params[1].get_bool();
             +  -  +  - ]
    1630         [ +  - ]:         69 :     bool witness_specified = !request.params[2].isNull();
    1631   [ +  +  +  -  :         69 :     bool iswitness = witness_specified ? request.params[2].get_bool() : false;
                   +  - ]
    1632         [ +  + ]:         69 :     const bool try_witness = witness_specified ? iswitness : true;
    1633         [ +  + ]:         69 :     const bool try_no_witness = witness_specified ? !iswitness : true;
    1634   [ +  -  +  -  :         69 :     if (!DecodeHexTx(tx, request.params[0].get_str(), try_no_witness, try_witness)) {
             +  -  +  + ]
    1635   [ +  -  +  -  :          1 :         throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
                   +  - ]
    1636                 :            :     }
    1637                 :            : 
    1638                 :            :     // Remove all scriptSigs and scriptWitnesses from inputs
    1639         [ +  + ]:        620 :     for (CTxIn& input : tx.vin) {
    1640   [ +  -  +  +  :        575 :         if ((!input.scriptSig.empty() || !input.scriptWitness.IsNull()) && !permitsigdata) {
                   +  + ]
    1641   [ +  -  +  -  :         23 :             throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Inputs must not have scriptSigs and scriptWitnesses");
                   +  - ]
    1642                 :            :         }
    1643         [ +  - ]:        552 :         input.scriptSig.clear();
    1644         [ +  - ]:        552 :         input.scriptWitness.SetNull();
    1645                 :            :     }
    1646                 :            : 
    1647                 :            :     // Make a blank psbt
    1648         [ +  - ]:         45 :     PartiallySignedTransaction psbtx;
    1649         [ +  - ]:         45 :     psbtx.tx = tx;
    1650         [ +  + ]:        592 :     for (unsigned int i = 0; i < tx.vin.size(); ++i) {
    1651   [ +  -  +  - ]:        547 :         psbtx.inputs.push_back(PSBTInput());
    1652                 :        547 :     }
    1653         [ +  + ]:       1102 :     for (unsigned int i = 0; i < tx.vout.size(); ++i) {
    1654   [ +  -  +  - ]:       1057 :         psbtx.outputs.push_back(PSBTOutput());
    1655                 :       1057 :     }
    1656                 :            : 
    1657                 :            :     // Serialize the PSBT
    1658         [ +  - ]:         45 :     CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
    1659         [ +  - ]:         45 :     ssTx << psbtx;
    1660                 :            : 
    1661   [ +  -  +  -  :         45 :     return EncodeBase64(ssTx);
                   +  - ]
    1662                 :         93 : },
    1663                 :            :     };
    1664                 :          0 : }
    1665                 :            : 
    1666                 :         84 : static RPCHelpMan utxoupdatepsbt()
    1667                 :            : {
    1668   [ +  -  -  +  :        168 :     return RPCHelpMan{"utxoupdatepsbt",
          #  #  #  #  #  
                      # ]
    1669         [ +  - ]:         84 :             "\nUpdates all segwit inputs and outputs in a PSBT with data from output descriptors, the UTXO set, txindex, or the mempool.\n",
    1670         [ +  - ]:        252 :             {
    1671   [ +  -  +  -  :         84 :                 {"psbt", RPCArg::Type::STR, RPCArg::Optional::NO, "A base64 string of a PSBT"},
                   +  - ]
    1672   [ +  -  +  -  :        252 :                 {"descriptors", RPCArg::Type::ARR, RPCArg::Optional::OMITTED, "An array of either strings or objects", {
             +  -  +  - ]
    1673   [ +  -  +  -  :         84 :                     {"", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "An output descriptor"},
                   +  - ]
    1674   [ +  -  +  -  :        252 :                     {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "An object with an output descriptor and extra information", {
             +  -  +  - ]
    1675   [ +  -  +  -  :         84 :                          {"desc", RPCArg::Type::STR, RPCArg::Optional::NO, "An output descriptor"},
                   +  - ]
    1676   [ +  -  +  -  :         84 :                          {"range", RPCArg::Type::RANGE, RPCArg::Default{1000}, "Up to what index HD chains should be explored (either end or [begin,end])"},
             +  -  +  - ]
    1677                 :            :                     }},
    1678                 :            :                 }},
    1679                 :            :             },
    1680   [ +  -  +  - ]:         84 :             RPCResult {
    1681   [ +  -  +  - ]:         84 :                     RPCResult::Type::STR, "", "The base64-encoded partially signed transaction with inputs updated"
    1682                 :            :             },
    1683         [ +  - ]:         84 :             RPCExamples {
    1684   [ +  -  +  -  :         84 :                 HelpExampleCli("utxoupdatepsbt", "\"psbt\"")
                   +  - ]
    1685                 :            :             },
    1686         [ +  - ]:        129 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
    1687                 :            : {
    1688                 :            :     // Parse descriptors, if any.
    1689                 :         45 :     FlatSigningProvider provider;
    1690   [ +  -  +  + ]:         45 :     if (!request.params[1].isNull()) {
    1691   [ +  -  +  -  :          4 :         auto descs = request.params[1].get_array();
                   +  - ]
    1692         [ +  - ]:          4 :         for (size_t i = 0; i < descs.size(); ++i) {
    1693   [ +  -  -  + ]:          4 :             EvalDescriptorStringOrObject(descs[i], provider);
    1694                 :          0 :         }
    1695                 :          4 :     }
    1696                 :            : 
    1697                 :            :     // We don't actually need private keys further on; hide them as a precaution.
    1698         [ +  + ]:         41 :     const PartiallySignedTransaction& psbtx = ProcessPSBT(
    1699   [ +  -  +  - ]:         41 :         request.params[0].get_str(),
    1700                 :         41 :         request.context,
    1701         [ +  - ]:         41 :         HidingSigningProvider(&provider, /*hide_secret=*/true, /*hide_origin=*/false),
    1702                 :            :         /*sighash_type=*/SIGHASH_ALL,
    1703                 :            :         /*finalize=*/false);
    1704                 :            : 
    1705         [ +  - ]:         40 :     CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
    1706         [ +  - ]:         40 :     ssTx << psbtx;
    1707   [ +  -  +  -  :         40 :     return EncodeBase64(ssTx);
                   -  + ]
    1708                 :         45 : },
    1709                 :            :     };
    1710                 :          0 : }
    1711                 :            : 
    1712                 :         42 : static RPCHelpMan joinpsbts()
    1713                 :            : {
    1714   [ +  -  -  +  :         84 :     return RPCHelpMan{"joinpsbts",
             #  #  #  # ]
    1715         [ +  - ]:         42 :             "\nJoins multiple distinct PSBTs with different inputs and outputs into one PSBT with inputs and outputs from all of the PSBTs\n"
    1716                 :            :             "No input in any of the PSBTs can be in more than one of the PSBTs.\n",
    1717         [ +  - ]:         84 :             {
    1718   [ +  -  +  -  :         84 :                 {"txs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The base64 strings of partially signed transactions",
                   +  - ]
    1719         [ +  - ]:         84 :                     {
    1720   [ +  -  +  -  :         42 :                         {"psbt", RPCArg::Type::STR, RPCArg::Optional::NO, "A base64 string of a PSBT"}
                   +  - ]
    1721                 :            :                     }}
    1722                 :            :             },
    1723   [ +  -  +  - ]:         42 :             RPCResult {
    1724   [ +  -  +  - ]:         42 :                     RPCResult::Type::STR, "", "The base64-encoded partially signed transaction"
    1725                 :            :             },
    1726         [ +  - ]:         42 :             RPCExamples {
    1727   [ +  -  +  -  :         42 :                 HelpExampleCli("joinpsbts", "\"psbt\"")
                   +  - ]
    1728                 :            :             },
    1729         [ +  - ]:         45 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
    1730                 :            : {
    1731                 :            :     // Unserialize the transactions
    1732                 :          3 :     std::vector<PartiallySignedTransaction> psbtxs;
    1733   [ +  -  +  -  :          3 :     UniValue txs = request.params[0].get_array();
                   +  - ]
    1734                 :            : 
    1735         [ +  + ]:          3 :     if (txs.size() <= 1) {
    1736   [ +  -  +  -  :          1 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "At least two PSBTs are required to join PSBTs.");
                   +  - ]
    1737                 :            :     }
    1738                 :            : 
    1739                 :          2 :     uint32_t best_version = 1;
    1740                 :          2 :     uint32_t best_locktime = 0xffffffff;
    1741         [ -  + ]:          2 :     for (unsigned int i = 0; i < txs.size(); ++i) {
    1742         [ +  - ]:          2 :         PartiallySignedTransaction psbtx;
    1743                 :          2 :         std::string error;
    1744   [ +  -  +  +  :          2 :         if (!DecodeBase64PSBT(psbtx, txs[i].get_str(), error)) {
             +  -  -  + ]
    1745   [ +  -  +  -  :          1 :             throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed %s", error));
                   +  - ]
    1746                 :            :         }
    1747         [ #  # ]:          0 :         psbtxs.push_back(psbtx);
    1748                 :            :         // Choose the highest version number
    1749         [ #  # ]:          0 :         if (static_cast<uint32_t>(psbtx.tx->nVersion) > best_version) {
    1750                 :          0 :             best_version = static_cast<uint32_t>(psbtx.tx->nVersion);
    1751                 :          0 :         }
    1752                 :            :         // Choose the lowest lock time
    1753         [ #  # ]:          0 :         if (psbtx.tx->nLockTime < best_locktime) {
    1754                 :          0 :             best_locktime = psbtx.tx->nLockTime;
    1755                 :          0 :         }
    1756                 :          2 :     }
    1757                 :            : 
    1758                 :            :     // Create a blank psbt where everything will be added
    1759         [ #  # ]:          0 :     PartiallySignedTransaction merged_psbt;
    1760   [ #  #  #  # ]:          0 :     merged_psbt.tx = CMutableTransaction();
    1761                 :          0 :     merged_psbt.tx->nVersion = static_cast<int32_t>(best_version);
    1762                 :          0 :     merged_psbt.tx->nLockTime = best_locktime;
    1763                 :            : 
    1764                 :            :     // Merge
    1765         [ #  # ]:          0 :     for (auto& psbt : psbtxs) {
    1766         [ #  # ]:          0 :         for (unsigned int i = 0; i < psbt.tx->vin.size(); ++i) {
    1767   [ #  #  #  # ]:          0 :             if (!merged_psbt.AddInput(psbt.tx->vin[i], psbt.inputs[i])) {
    1768   [ #  #  #  #  :          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Input %s:%d exists in multiple PSBTs", psbt.tx->vin[i].prevout.hash.ToString(), psbt.tx->vin[i].prevout.n));
             #  #  #  # ]
    1769                 :            :             }
    1770                 :          0 :         }
    1771         [ #  # ]:          0 :         for (unsigned int i = 0; i < psbt.tx->vout.size(); ++i) {
    1772         [ #  # ]:          0 :             merged_psbt.AddOutput(psbt.tx->vout[i], psbt.outputs[i]);
    1773                 :          0 :         }
    1774         [ #  # ]:          0 :         for (auto& xpub_pair : psbt.m_xpubs) {
    1775   [ #  #  #  # ]:          0 :             if (merged_psbt.m_xpubs.count(xpub_pair.first) == 0) {
    1776   [ #  #  #  # ]:          0 :                 merged_psbt.m_xpubs[xpub_pair.first] = xpub_pair.second;
    1777                 :          0 :             } else {
    1778   [ #  #  #  # ]:          0 :                 merged_psbt.m_xpubs[xpub_pair.first].insert(xpub_pair.second.begin(), xpub_pair.second.end());
    1779                 :            :             }
    1780                 :            :         }
    1781         [ #  # ]:          0 :         merged_psbt.unknown.insert(psbt.unknown.begin(), psbt.unknown.end());
    1782                 :            :     }
    1783                 :            : 
    1784                 :            :     // Generate list of shuffled indices for shuffling inputs and outputs of the merged PSBT
    1785         [ #  # ]:          0 :     std::vector<int> input_indices(merged_psbt.inputs.size());
    1786         [ #  # ]:          0 :     std::iota(input_indices.begin(), input_indices.end(), 0);
    1787         [ #  # ]:          0 :     std::vector<int> output_indices(merged_psbt.outputs.size());
    1788         [ #  # ]:          0 :     std::iota(output_indices.begin(), output_indices.end(), 0);
    1789                 :            : 
    1790                 :            :     // Shuffle input and output indices lists
    1791         [ #  # ]:          0 :     Shuffle(input_indices.begin(), input_indices.end(), FastRandomContext());
    1792         [ #  # ]:          0 :     Shuffle(output_indices.begin(), output_indices.end(), FastRandomContext());
    1793                 :            : 
    1794         [ #  # ]:          0 :     PartiallySignedTransaction shuffled_psbt;
    1795   [ #  #  #  # ]:          0 :     shuffled_psbt.tx = CMutableTransaction();
    1796                 :          0 :     shuffled_psbt.tx->nVersion = merged_psbt.tx->nVersion;
    1797                 :          0 :     shuffled_psbt.tx->nLockTime = merged_psbt.tx->nLockTime;
    1798         [ #  # ]:          0 :     for (int i : input_indices) {
    1799         [ #  # ]:          0 :         shuffled_psbt.AddInput(merged_psbt.tx->vin[i], merged_psbt.inputs[i]);
    1800                 :            :     }
    1801         [ #  # ]:          0 :     for (int i : output_indices) {
    1802         [ #  # ]:          0 :         shuffled_psbt.AddOutput(merged_psbt.tx->vout[i], merged_psbt.outputs[i]);
    1803                 :            :     }
    1804         [ #  # ]:          0 :     shuffled_psbt.unknown.insert(merged_psbt.unknown.begin(), merged_psbt.unknown.end());
    1805                 :            : 
    1806         [ #  # ]:          0 :     CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
    1807         [ #  # ]:          0 :     ssTx << shuffled_psbt;
    1808   [ #  #  #  #  :          0 :     return EncodeBase64(ssTx);
                   #  # ]
    1809                 :          6 : },
    1810                 :            :     };
    1811                 :          0 : }
    1812                 :            : 
    1813                 :         85 : static RPCHelpMan analyzepsbt()
    1814                 :            : {
    1815   [ +  -  -  +  :        170 :     return RPCHelpMan{"analyzepsbt",
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1816         [ +  - ]:         85 :             "\nAnalyzes and provides information about the current status of a PSBT and its inputs\n",
    1817         [ +  - ]:        170 :             {
    1818   [ +  -  +  -  :         85 :                 {"psbt", RPCArg::Type::STR, RPCArg::Optional::NO, "A base64 string of a PSBT"}
                   +  - ]
    1819                 :            :             },
    1820   [ +  -  +  - ]:         85 :             RPCResult {
    1821   [ +  -  +  - ]:         85 :                 RPCResult::Type::OBJ, "", "",
    1822         [ +  - ]:        595 :                 {
    1823   [ +  -  +  -  :        170 :                     {RPCResult::Type::ARR, "inputs", /*optional=*/true, "",
                   +  - ]
    1824         [ +  - ]:        170 :                     {
    1825   [ +  -  +  -  :        170 :                         {RPCResult::Type::OBJ, "", "",
                   +  - ]
    1826         [ +  - ]:        425 :                         {
    1827   [ +  -  +  -  :         85 :                             {RPCResult::Type::BOOL, "has_utxo", "Whether a UTXO is provided"},
                   +  - ]
    1828   [ +  -  +  -  :         85 :                             {RPCResult::Type::BOOL, "is_final", "Whether the input is finalized"},
                   +  - ]
    1829   [ +  -  +  -  :        170 :                             {RPCResult::Type::OBJ, "missing", /*optional=*/true, "Things that are missing that are required to complete this input",
                   +  - ]
    1830         [ +  - ]:        425 :                             {
    1831   [ +  -  +  -  :        170 :                                 {RPCResult::Type::ARR, "pubkeys", /*optional=*/true, "",
                   +  - ]
    1832         [ +  - ]:        170 :                                 {
    1833   [ +  -  +  -  :         85 :                                     {RPCResult::Type::STR_HEX, "keyid", "Public key ID, hash160 of the public key, of a public key whose BIP 32 derivation path is missing"},
                   +  - ]
    1834                 :            :                                 }},
    1835   [ +  -  +  -  :        170 :                                 {RPCResult::Type::ARR, "signatures", /*optional=*/true, "",
                   +  - ]
    1836         [ +  - ]:        170 :                                 {
    1837   [ +  -  +  -  :         85 :                                     {RPCResult::Type::STR_HEX, "keyid", "Public key ID, hash160 of the public key, of a public key whose signature is missing"},
                   +  - ]
    1838                 :            :                                 }},
    1839   [ +  -  +  -  :         85 :                                 {RPCResult::Type::STR_HEX, "redeemscript", /*optional=*/true, "Hash160 of the redeemScript that is missing"},
                   +  - ]
    1840   [ +  -  +  -  :         85 :                                 {RPCResult::Type::STR_HEX, "witnessscript", /*optional=*/true, "SHA256 of the witnessScript that is missing"},
                   +  - ]
    1841                 :            :                             }},
    1842   [ +  -  +  -  :         85 :                             {RPCResult::Type::STR, "next", /*optional=*/true, "Role of the next person that this input needs to go to"},
                   +  - ]
    1843                 :            :                         }},
    1844                 :            :                     }},
    1845   [ +  -  +  -  :         85 :                     {RPCResult::Type::NUM, "estimated_vsize", /*optional=*/true, "Estimated vsize of the final signed transaction"},
                   +  - ]
    1846   [ +  -  +  -  :         85 :                     {RPCResult::Type::STR_AMOUNT, "estimated_feerate", /*optional=*/true, "Estimated feerate of the final signed transaction in " + CURRENCY_UNIT + "/kvB. Shown only if all UTXO slots in the PSBT have been filled"},
             +  -  +  - ]
    1847   [ +  -  +  -  :         85 :                     {RPCResult::Type::STR_AMOUNT, "fee", /*optional=*/true, "The transaction fee paid. Shown only if all UTXO slots in the PSBT have been filled"},
                   +  - ]
    1848   [ +  -  +  -  :         85 :                     {RPCResult::Type::STR, "next", "Role of the next person that this psbt needs to go to"},
                   +  - ]
    1849   [ +  -  +  -  :         85 :                     {RPCResult::Type::STR, "error", /*optional=*/true, "Error message (if there is one)"},
                   +  - ]
    1850                 :            :                 }
    1851                 :            :             },
    1852         [ +  - ]:         85 :             RPCExamples {
    1853   [ +  -  +  -  :         85 :                 HelpExampleCli("analyzepsbt", "\"psbt\"")
                   +  - ]
    1854                 :            :             },
    1855         [ +  - ]:        109 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
    1856                 :            : {
    1857                 :            :     // Unserialize the transaction
    1858                 :         24 :     PartiallySignedTransaction psbtx;
    1859                 :         24 :     std::string error;
    1860   [ +  -  +  -  :         24 :     if (!DecodeBase64PSBT(psbtx, request.params[0].get_str(), error)) {
             +  -  +  + ]
    1861   [ +  -  +  -  :          2 :         throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed %s", error));
             -  +  +  - ]
    1862                 :            :     }
    1863                 :            : 
    1864   [ +  -  +  - ]:         22 :     PSBTAnalysis psbta = AnalyzePSBT(psbtx);
    1865                 :            : 
    1866         [ +  - ]:         22 :     UniValue result(UniValue::VOBJ);
    1867         [ +  - ]:         22 :     UniValue inputs_result(UniValue::VARR);
    1868         [ +  + ]:         25 :     for (const auto& input : psbta.inputs) {
    1869         [ +  - ]:          3 :         UniValue input_univ(UniValue::VOBJ);
    1870         [ -  + ]:          3 :         UniValue missing(UniValue::VOBJ);
    1871                 :            : 
    1872   [ -  +  -  +  :          3 :         input_univ.pushKV("has_utxo", input.has_utxo);
                   -  + ]
    1873   [ -  +  -  +  :          3 :         input_univ.pushKV("is_final", input.is_final);
                   -  + ]
    1874   [ -  +  -  +  :          3 :         input_univ.pushKV("next", PSBTRoleName(input.next));
             -  +  -  + ]
    1875                 :            : 
    1876         [ +  - ]:          3 :         if (!input.missing_pubkeys.empty()) {
    1877         [ #  # ]:          0 :             UniValue missing_pubkeys_univ(UniValue::VARR);
    1878         [ #  # ]:          0 :             for (const CKeyID& pubkey : input.missing_pubkeys) {
    1879   [ #  #  #  #  :          0 :                 missing_pubkeys_univ.push_back(HexStr(pubkey));
             #  #  #  # ]
    1880                 :            :             }
    1881   [ #  #  #  #  :          0 :             missing.pushKV("pubkeys", missing_pubkeys_univ);
                   #  # ]
    1882                 :          0 :         }
    1883   [ +  -  +  - ]:          3 :         if (!input.missing_redeem_script.IsNull()) {
    1884   [ #  #  #  #  :          0 :             missing.pushKV("redeemscript", HexStr(input.missing_redeem_script));
          #  #  #  #  #  
                      # ]
    1885                 :          0 :         }
    1886   [ +  -  +  - ]:          3 :         if (!input.missing_witness_script.IsNull()) {
    1887   [ #  #  #  #  :          0 :             missing.pushKV("witnessscript", HexStr(input.missing_witness_script));
          #  #  #  #  #  
                      # ]
    1888                 :          0 :         }
    1889         [ +  - ]:          3 :         if (!input.missing_sigs.empty()) {
    1890         [ #  # ]:          0 :             UniValue missing_sigs_univ(UniValue::VARR);
    1891         [ #  # ]:          0 :             for (const CKeyID& pubkey : input.missing_sigs) {
    1892   [ #  #  #  #  :          0 :                 missing_sigs_univ.push_back(HexStr(pubkey));
             #  #  #  # ]
    1893                 :            :             }
    1894   [ #  #  #  #  :          0 :             missing.pushKV("signatures", missing_sigs_univ);
                   #  # ]
    1895                 :          0 :         }
    1896   [ +  -  +  - ]:          3 :         if (!missing.getKeys().empty()) {
    1897   [ #  #  #  #  :          0 :             input_univ.pushKV("missing", missing);
                   #  # ]
    1898                 :          0 :         }
    1899   [ +  -  +  - ]:          3 :         inputs_result.push_back(input_univ);
    1900                 :          3 :     }
    1901   [ +  +  +  -  :         22 :     if (!inputs_result.empty()) result.pushKV("inputs", inputs_result);
             +  -  +  - ]
    1902                 :            : 
    1903         [ +  + ]:         22 :     if (psbta.estimated_vsize != std::nullopt) {
    1904   [ +  -  +  -  :         17 :         result.pushKV("estimated_vsize", (int)*psbta.estimated_vsize);
             +  -  +  - ]
    1905                 :         17 :     }
    1906         [ +  + ]:         22 :     if (psbta.estimated_feerate != std::nullopt) {
    1907   [ +  -  +  -  :         17 :         result.pushKV("estimated_feerate", ValueFromAmount(psbta.estimated_feerate->GetFeePerK()));
          +  -  +  -  +  
                      - ]
    1908                 :         17 :     }
    1909         [ +  + ]:         22 :     if (psbta.fee != std::nullopt) {
    1910   [ +  -  +  -  :         17 :         result.pushKV("fee", ValueFromAmount(*psbta.fee));
             +  -  +  - ]
    1911                 :         17 :     }
    1912   [ +  -  +  -  :         22 :     result.pushKV("next", PSBTRoleName(psbta.next));
             +  -  +  - ]
    1913         [ +  + ]:         22 :     if (!psbta.error.empty()) {
    1914   [ +  -  +  -  :          4 :         result.pushKV("error", psbta.error);
                   +  - ]
    1915                 :          4 :     }
    1916                 :            : 
    1917                 :         22 :     return result;
    1918         [ +  - ]:         26 : },
    1919                 :            :     };
    1920                 :          0 : }
    1921                 :            : 
    1922                 :         38 : RPCHelpMan descriptorprocesspsbt()
    1923                 :            : {
    1924   [ +  -  +  -  :         76 :     return RPCHelpMan{"descriptorprocesspsbt",
          #  #  #  #  #  
                #  #  # ]
    1925         [ +  - ]:         38 :                 "\nUpdate all segwit inputs in a PSBT with information from output descriptors, the UTXO set or the mempool. \n"
    1926                 :            :                 "Then, sign the inputs we are able to with information from the output descriptors. ",
    1927         [ +  - ]:        228 :                 {
    1928   [ +  -  +  -  :         38 :                     {"psbt", RPCArg::Type::STR, RPCArg::Optional::NO, "The transaction base64 string"},
                   +  - ]
    1929   [ +  -  +  -  :        114 :                     {"descriptors", RPCArg::Type::ARR, RPCArg::Optional::NO, "An array of either strings or objects", {
             +  -  +  - ]
    1930   [ +  -  +  -  :         38 :                         {"", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "An output descriptor"},
                   +  - ]
    1931   [ +  -  +  -  :        114 :                         {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "An object with an output descriptor and extra information", {
             +  -  +  - ]
    1932   [ +  -  +  -  :         38 :                              {"desc", RPCArg::Type::STR, RPCArg::Optional::NO, "An output descriptor"},
                   +  - ]
    1933   [ +  -  +  -  :         38 :                              {"range", RPCArg::Type::RANGE, RPCArg::Default{1000}, "Up to what index HD chains should be explored (either end or [begin,end])"},
             +  -  +  - ]
    1934                 :            :                         }},
    1935                 :            :                     }},
    1936   [ +  -  +  -  :         38 :                     {"sighashtype", RPCArg::Type::STR, RPCArg::Default{"DEFAULT for Taproot, ALL otherwise"}, "The signature hash type to sign with if not specified by the PSBT. Must be one of\n"
             +  -  +  - ]
    1937                 :            :             "       \"DEFAULT\"\n"
    1938                 :            :             "       \"ALL\"\n"
    1939                 :            :             "       \"NONE\"\n"
    1940                 :            :             "       \"SINGLE\"\n"
    1941                 :            :             "       \"ALL|ANYONECANPAY\"\n"
    1942                 :            :             "       \"NONE|ANYONECANPAY\"\n"
    1943                 :            :             "       \"SINGLE|ANYONECANPAY\""},
    1944   [ +  -  +  -  :         38 :                     {"bip32derivs", RPCArg::Type::BOOL, RPCArg::Default{true}, "Include BIP 32 derivation paths for public keys if we know them"},
             +  -  +  - ]
    1945   [ +  -  +  -  :         38 :                     {"finalize", RPCArg::Type::BOOL, RPCArg::Default{true}, "Also finalize inputs if possible"},
             +  -  +  - ]
    1946                 :            :                 },
    1947   [ +  -  +  - ]:         38 :                 RPCResult{
    1948   [ +  -  +  - ]:         38 :                     RPCResult::Type::OBJ, "", "",
    1949         [ +  - ]:        152 :                     {
    1950   [ +  -  +  -  :         38 :                         {RPCResult::Type::STR, "psbt", "The base64-encoded partially signed transaction"},
                   +  - ]
    1951   [ +  -  +  -  :         38 :                         {RPCResult::Type::BOOL, "complete", "If the transaction has a complete set of signatures"},
                   +  - ]
    1952   [ +  -  +  -  :         38 :                         {RPCResult::Type::STR_HEX, "hex", /*optional=*/true, "The hex-encoded network transaction if complete"},
                   +  - ]
    1953                 :            :                     }
    1954                 :            :                 },
    1955         [ +  - ]:         38 :                 RPCExamples{
    1956   [ +  -  +  -  :         76 :                     HelpExampleCli("descriptorprocesspsbt", "\"psbt\" \"[\\\"descriptor1\\\", \\\"descriptor2\\\"]\"") +
             +  -  +  - ]
    1957   [ +  -  +  -  :         38 :                     HelpExampleCli("descriptorprocesspsbt", "\"psbt\" \"[{\\\"desc\\\":\\\"mydescriptor\\\", \\\"range\\\":21}]\"")
                   +  - ]
    1958                 :            :                 },
    1959         [ +  - ]:         39 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
    1960                 :            : {
    1961                 :            :     // Add descriptor information to a signing provider
    1962                 :          1 :     FlatSigningProvider provider;
    1963                 :            : 
    1964   [ +  -  +  -  :          1 :     auto descs = request.params[1].get_array();
                   +  - ]
    1965         [ -  + ]:          1 :     for (size_t i = 0; i < descs.size(); ++i) {
    1966   [ +  -  -  + ]:          1 :         EvalDescriptorStringOrObject(descs[i], provider, /*expand_priv=*/true);
    1967                 :          0 :     }
    1968                 :            : 
    1969   [ #  #  #  # ]:          0 :     int sighash_type = ParseSighashString(request.params[2]);
    1970   [ #  #  #  #  :          0 :     bool bip32derivs = request.params[3].isNull() ? true : request.params[3].get_bool();
             #  #  #  # ]
    1971   [ #  #  #  #  :          0 :     bool finalize = request.params[4].isNull() ? true : request.params[4].get_bool();
             #  #  #  # ]
    1972                 :            : 
    1973         [ #  # ]:          0 :     const PartiallySignedTransaction& psbtx = ProcessPSBT(
    1974   [ #  #  #  # ]:          0 :         request.params[0].get_str(),
    1975                 :          0 :         request.context,
    1976         [ #  # ]:          0 :         HidingSigningProvider(&provider, /*hide_secret=*/false, !bip32derivs),
    1977                 :          0 :         sighash_type,
    1978                 :          0 :         finalize);
    1979                 :            : 
    1980                 :            :     // Check whether or not all of the inputs are now signed
    1981                 :          0 :     bool complete = true;
    1982         [ #  # ]:          0 :     for (const auto& input : psbtx.inputs) {
    1983         [ #  # ]:          0 :         complete &= PSBTInputSigned(input);
    1984                 :            :     }
    1985                 :            : 
    1986         [ #  # ]:          0 :     CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
    1987         [ #  # ]:          0 :     ssTx << psbtx;
    1988                 :            : 
    1989         [ #  # ]:          0 :     UniValue result(UniValue::VOBJ);
    1990                 :            : 
    1991   [ #  #  #  #  :          0 :     result.pushKV("psbt", EncodeBase64(ssTx));
          #  #  #  #  #  
                      # ]
    1992   [ #  #  #  #  :          0 :     result.pushKV("complete", complete);
                   #  # ]
    1993         [ #  # ]:          0 :     if (complete) {
    1994         [ #  # ]:          0 :         CMutableTransaction mtx;
    1995         [ #  # ]:          0 :         PartiallySignedTransaction psbtx_copy = psbtx;
    1996   [ #  #  #  # ]:          0 :         CHECK_NONFATAL(FinalizeAndExtractPSBT(psbtx_copy, mtx));
    1997         [ #  # ]:          0 :         CDataStream ssTx_final(SER_NETWORK, PROTOCOL_VERSION);
    1998         [ #  # ]:          0 :         ssTx_final << mtx;
    1999   [ #  #  #  #  :          0 :         result.pushKV("hex", HexStr(ssTx_final));
          #  #  #  #  #  
                      # ]
    2000                 :          0 :     }
    2001                 :          0 :     return result;
    2002         [ #  # ]:          1 : },
    2003                 :            :     };
    2004                 :          0 : }
    2005                 :            : 
    2006                 :        224 : void RegisterRawTransactionRPCCommands(CRPCTable& t)
    2007                 :            : {
    2008   [ +  +  -  +  :        479 :     static const CRPCCommand commands[]{
                   #  # ]
    2009   [ +  -  +  - ]:         17 :         {"rawtransactions", &getrawtransaction},
    2010   [ +  -  +  - ]:         17 :         {"rawtransactions", &createrawtransaction},
    2011   [ +  -  +  - ]:         17 :         {"rawtransactions", &decoderawtransaction},
    2012   [ +  -  +  - ]:         17 :         {"rawtransactions", &decodescript},
    2013   [ +  -  +  - ]:         17 :         {"rawtransactions", &combinerawtransaction},
    2014   [ +  -  +  - ]:         17 :         {"rawtransactions", &signrawtransactionwithkey},
    2015   [ +  -  +  - ]:         17 :         {"rawtransactions", &decodepsbt},
    2016   [ +  -  +  - ]:         17 :         {"rawtransactions", &combinepsbt},
    2017   [ +  -  +  - ]:         17 :         {"rawtransactions", &finalizepsbt},
    2018   [ +  -  +  - ]:         17 :         {"rawtransactions", &createpsbt},
    2019   [ +  -  +  - ]:         17 :         {"rawtransactions", &converttopsbt},
    2020   [ +  -  +  - ]:         17 :         {"rawtransactions", &utxoupdatepsbt},
    2021   [ +  -  +  - ]:         17 :         {"rawtransactions", &descriptorprocesspsbt},
    2022   [ +  -  +  - ]:         17 :         {"rawtransactions", &joinpsbts},
    2023   [ +  -  +  - ]:         17 :         {"rawtransactions", &analyzepsbt},
    2024                 :            :     };
    2025         [ +  + ]:       3584 :     for (const auto& c : commands) {
    2026                 :       3360 :         t.appendCommand(c.name, &c);
    2027                 :            :     }
    2028                 :        224 : }

Generated by: LCOV version 1.14