LCOV - code coverage report
Current view: top level - src/rpc - rawtransaction_util.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 96.6 % 295 285
Test Date: 2026-10-03 06:57:10 Functions: 100.0 % 10 10
Branches: 57.2 % 838 479

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present 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 <rpc/rawtransaction_util.h>
       7                 :             : 
       8                 :             : #include <coins.h>
       9                 :             : #include <consensus/amount.h>
      10                 :             : #include <core_io.h>
      11                 :             : #include <crypto/hex_base.h>
      12                 :             : #include <key_io.h>
      13                 :             : #include <policy/feerate.h>
      14                 :             : #include <policy/policy.h>
      15                 :             : #include <primitives/transaction.h>
      16                 :             : #include <rpc/protocol.h>
      17                 :             : #include <rpc/request.h>
      18                 :             : #include <rpc/util.h>
      19                 :             : #include <script/interpreter.h>
      20                 :             : #include <script/script.h>
      21                 :             : #include <script/sign.h>
      22                 :             : #include <script/signingprovider.h>
      23                 :             : #include <tinyformat.h>
      24                 :             : #include <univalue.h>
      25                 :             : #include <util/check.h>
      26                 :             : #include <util/rbf.h>
      27                 :             : #include <util/translation.h>
      28                 :             : #include <util/vector.h>
      29                 :             : 
      30                 :             : #include <cstddef>
      31                 :             : #include <set>
      32                 :             : #include <span>
      33                 :             : #include <variant>
      34                 :             : 
      35                 :        1245 : void AddInputs(CMutableTransaction& rawTx, const UniValue& inputs_in, std::optional<bool> rbf)
      36                 :             : {
      37         [ +  + ]:        1245 :     UniValue inputs;
      38         [ +  + ]:        1245 :     if (inputs_in.isNull()) {
      39                 :         382 :         inputs = UniValue::VARR;
      40                 :             :     } else {
      41   [ +  -  +  - ]:         863 :         inputs = inputs_in.get_array();
      42                 :             :     }
      43                 :             : 
      44   [ -  +  +  + ]:        4560 :     for (unsigned int idx = 0; idx < inputs.size(); idx++) {
      45         [ +  - ]:        3324 :         const UniValue& input = inputs[idx];
      46         [ +  + ]:        3324 :         const UniValue& o = input.get_obj();
      47                 :             : 
      48         [ +  + ]:        3323 :         Txid txid = Txid::FromUint256(ParseHashO(o, "txid"));
      49                 :             : 
      50         [ +  - ]:        3320 :         const UniValue& vout_v = o.find_value("vout");
      51         [ +  + ]:        3320 :         if (!vout_v.isNum())
      52   [ +  -  +  - ]:           4 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
      53         [ +  - ]:        3318 :         int nOutput = vout_v.getInt<int>();
      54         [ +  + ]:        3318 :         if (nOutput < 0)
      55   [ +  -  +  - ]:           2 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative");
      56                 :             : 
      57                 :        3317 :         uint32_t nSequence;
      58                 :             : 
      59   [ +  +  +  + ]:        3317 :         if (rbf.value_or(true)) {
      60                 :             :             nSequence = MAX_BIP125_RBF_SEQUENCE; /* CTxIn::SEQUENCE_FINAL - 2 */
      61         [ +  + ]:           4 :         } else if (rawTx.nLockTime) {
      62                 :             :             nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; /* CTxIn::SEQUENCE_FINAL - 1 */
      63                 :             :         } else {
      64                 :           3 :             nSequence = CTxIn::SEQUENCE_FINAL;
      65                 :             :         }
      66                 :             : 
      67                 :             :         // set the sequence number if passed in the parameters object
      68         [ +  - ]:        3317 :         const UniValue& sequenceObj = o.find_value("sequence");
      69         [ +  + ]:        3317 :         if (sequenceObj.isNum()) {
      70         [ +  - ]:          56 :             int64_t seqNr64 = sequenceObj.getInt<int64_t>();
      71         [ +  + ]:          56 :             if (seqNr64 < 0 || seqNr64 > CTxIn::SEQUENCE_FINAL) {
      72   [ +  -  +  - ]:           4 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, sequence number is out of range");
      73                 :             :             } else {
      74                 :          54 :                 nSequence = (uint32_t)seqNr64;
      75                 :             :             }
      76                 :             :         }
      77                 :             : 
      78         [ +  - ]:        3315 :         CTxIn in(COutPoint(txid, nOutput), CScript(), nSequence);
      79                 :             : 
      80         [ +  - ]:        3315 :         rawTx.vin.push_back(in);
      81                 :        3315 :     }
      82                 :        1245 : }
      83                 :             : 
      84                 :        1747 : UniValue NormalizeOutputs(const UniValue& outputs_in)
      85                 :             : {
      86         [ -  + ]:        1747 :     if (outputs_in.isNull()) {
      87   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, output argument must be non-null");
      88                 :             :     }
      89                 :             : 
      90                 :        1747 :     const bool outputs_is_obj = outputs_in.isObject();
      91         [ +  + ]:        1747 :     UniValue outputs = outputs_is_obj ? outputs_in.get_obj() : outputs_in.get_array();
      92                 :             : 
      93         [ +  + ]:        1746 :     if (!outputs_is_obj) {
      94                 :             :         // Translate array of key-value pairs into dict
      95                 :        1057 :         UniValue outputs_dict = UniValue(UniValue::VOBJ);
      96   [ -  +  +  + ]:        8858 :         for (size_t i = 0; i < outputs.size(); ++i) {
      97         [ +  - ]:        7803 :             const UniValue& output = outputs[i];
      98         [ +  + ]:        7803 :             if (!output.isObject()) {
      99   [ +  -  +  - ]:           2 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, key-value pair not an object as expected");
     100                 :             :             }
     101   [ -  +  +  + ]:        7802 :             if (output.size() != 1) {
     102   [ +  -  +  - ]:           2 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, key-value pair must contain exactly one key");
     103                 :             :             }
     104   [ +  -  +  - ]:        7801 :             outputs_dict.pushKVs(output);
     105                 :             :         }
     106                 :        1055 :         outputs = std::move(outputs_dict);
     107                 :        1057 :     }
     108                 :        1744 :     return outputs;
     109                 :           2 : }
     110                 :             : 
     111                 :        3079 : std::vector<std::pair<CTxDestination, CAmount>> ParseOutputs(const UniValue& outputs)
     112                 :             : {
     113                 :             :     // Duplicate checking
     114         [ +  - ]:        3079 :     std::set<CTxDestination> destinations;
     115                 :        3079 :     std::vector<std::pair<CTxDestination, CAmount>> parsed_outputs;
     116                 :        3079 :     bool has_data{false};
     117         [ +  - ]:        3079 :     const auto& keys{outputs.getKeys()};
     118         [ +  - ]:        3079 :     const auto& values{outputs.getValues()};
     119   [ -  +  +  + ]:       18196 :     for (size_t i{0}; i < keys.size(); ++i) {
     120         [ +  + ]:       15134 :         const auto& name_{keys[i]};
     121                 :       15134 :         const auto& value{values[i]};
     122         [ +  + ]:       15134 :         if (name_ == "data") {
     123         [ +  + ]:          27 :             if (has_data) {
     124   [ +  -  +  - ]:           6 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, duplicate key: data");
     125                 :             :             }
     126                 :          24 :             has_data = true;
     127   [ +  -  +  + ]:          24 :             std::vector<unsigned char> data = ParseHexV(value.getValStr(), "Data");
     128   [ +  -  -  + ]:          20 :             CTxDestination destination{CNoDestination{CScript() << OP_RETURN << data}};
     129                 :          20 :             CAmount amount{0};
     130         [ +  - ]:          20 :             parsed_outputs.emplace_back(destination, amount);
     131                 :          20 :         } else {
     132         [ +  - ]:       15107 :             CTxDestination destination{DecodeDestination(name_)};
     133         [ +  + ]:       15107 :             CAmount amount{AmountFromValue(value)};
     134   [ +  -  +  + ]:       15102 :             if (!IsValidDestination(destination)) {
     135   [ +  -  +  -  :           2 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ") + name_);
                   +  - ]
     136                 :             :             }
     137                 :             : 
     138   [ +  -  +  + ]:       15101 :             if (!destinations.insert(destination).second) {
     139   [ +  -  +  -  :           8 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ") + name_);
                   +  - ]
     140                 :             :             }
     141         [ +  - ]:       15097 :             parsed_outputs.emplace_back(destination, amount);
     142                 :       15107 :         }
     143                 :             :     }
     144                 :        3062 :     return parsed_outputs;
     145                 :        3079 : }
     146                 :             : 
     147                 :        1246 : void AddOutputs(CMutableTransaction& rawTx, const UniValue& outputs_in)
     148                 :             : {
     149                 :        1246 :     UniValue outputs(UniValue::VOBJ);
     150         [ +  + ]:        1246 :     outputs = NormalizeOutputs(outputs_in);
     151                 :             : 
     152         [ +  + ]:        1243 :     std::vector<std::pair<CTxDestination, CAmount>> parsed_outputs = ParseOutputs(outputs);
     153   [ +  -  +  + ]:        6649 :     for (const auto& [destination, nAmount] : parsed_outputs) {
     154         [ +  - ]:        5420 :         CScript scriptPubKey = GetScriptForDestination(destination);
     155                 :             : 
     156         [ +  - ]:        5420 :         CTxOut out(nAmount, scriptPubKey);
     157         [ +  - ]:        5420 :         rawTx.vout.push_back(out);
     158                 :        5420 :     }
     159                 :        1246 : }
     160                 :             : 
     161                 :        1249 : CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniValue& outputs_in, const UniValue& locktime, std::optional<bool> rbf, const uint32_t version)
     162                 :             : {
     163                 :        1249 :     CMutableTransaction rawTx;
     164                 :             : 
     165         [ +  + ]:        1249 :     if (!locktime.isNull()) {
     166         [ +  - ]:         193 :         int64_t nLockTime = locktime.getInt<int64_t>();
     167         [ +  + ]:         193 :         if (nLockTime < 0 || nLockTime > LOCKTIME_MAX)
     168   [ +  -  +  - ]:           4 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, locktime out of range");
     169                 :         191 :         rawTx.nLockTime = nLockTime;
     170                 :             :     }
     171                 :             : 
     172         [ +  + ]:        1247 :     if (version < TX_MIN_STANDARD_VERSION || version > TX_MAX_STANDARD_VERSION) {
     173   [ +  -  +  - ]:           4 :         throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid parameter, version out of range(%d~%d)", TX_MIN_STANDARD_VERSION, TX_MAX_STANDARD_VERSION));
     174                 :             :     }
     175                 :        1245 :     rawTx.version = version;
     176                 :             : 
     177         [ +  + ]:        1245 :     AddInputs(rawTx, inputs_in, rbf);
     178         [ +  + ]:        1236 :     AddOutputs(rawTx, outputs_in);
     179                 :             : 
     180   [ +  +  +  +  :        1449 :     if (rbf.has_value() && rbf.value() && rawTx.vin.size() > 0 && !SignalsOptInRBF(CTransaction(rawTx))) {
          -  +  +  +  +  
          -  +  -  +  +  
                   +  + ]
     181   [ +  -  +  - ]:           2 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter combination: Sequence number(s) contradict replaceable option");
     182                 :             :     }
     183                 :             : 
     184                 :        1220 :     return rawTx;
     185                 :          29 : }
     186                 :             : 
     187                 :             : /** Pushes a JSON object for script verification or signing errors to vErrorsRet. */
     188                 :          96 : static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::string& strMessage)
     189                 :             : {
     190                 :          96 :     UniValue entry(UniValue::VOBJ);
     191   [ +  -  +  -  :         192 :     entry.pushKV("txid", txin.prevout.hash.ToString());
             +  -  +  - ]
     192   [ +  -  +  -  :         192 :     entry.pushKV("vout", txin.prevout.n);
                   +  - ]
     193                 :          96 :     UniValue witness(UniValue::VARR);
     194   [ -  +  +  + ]:         598 :     for (unsigned int i = 0; i < txin.scriptWitness.stack.size(); i++) {
     195   [ -  +  +  -  :         502 :         witness.push_back(HexStr(txin.scriptWitness.stack[i]));
             +  -  +  - ]
     196                 :             :     }
     197   [ +  -  +  - ]:         192 :     entry.pushKV("witness", std::move(witness));
     198   [ +  +  +  -  :         288 :     entry.pushKV("scriptSig", HexStr(txin.scriptSig));
          +  -  +  -  +  
                      - ]
     199   [ +  -  +  -  :         192 :     entry.pushKV("sequence", txin.nSequence);
                   +  - ]
     200   [ +  -  +  -  :         192 :     entry.pushKV("error", strMessage);
                   +  - ]
     201         [ +  - ]:          96 :     vErrorsRet.push_back(std::move(entry));
     202                 :          96 : }
     203                 :             : 
     204                 :         520 : void ParsePrevouts(const UniValue& prevTxsUnival, FlatSigningProvider* keystore, std::map<COutPoint, Coin>& coins)
     205                 :             : {
     206         [ +  + ]:         520 :     if (!prevTxsUnival.isNull()) {
     207                 :         210 :         const UniValue& prevTxs = prevTxsUnival.get_array();
     208   [ -  +  +  + ]:         726 :         for (unsigned int idx = 0; idx < prevTxs.size(); ++idx) {
     209                 :         609 :             const UniValue& p = prevTxs[idx];
     210         [ -  + ]:         609 :             if (!p.isObject()) {
     211   [ #  #  #  # ]:           0 :                 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "expected object with {\"txid'\",\"vout\",\"scriptPubKey\"}");
     212                 :             :             }
     213                 :             : 
     214                 :         609 :             const UniValue& prevOut = p.get_obj();
     215                 :             : 
     216   [ +  +  +  +  :        3054 :             RPCTypeCheckObj(prevOut,
                   +  + ]
     217                 :             :                 {
     218         [ +  - ]:         609 :                     {"txid", UniValueType(UniValue::VSTR)},
     219         [ +  - ]:         609 :                     {"vout", UniValueType(UniValue::VNUM)},
     220         [ +  - ]:         609 :                     {"scriptPubKey", UniValueType(UniValue::VSTR)},
     221                 :             :                 });
     222                 :             : 
     223                 :         600 :             Txid txid = Txid::FromUint256(ParseHashO(prevOut, "txid"));
     224                 :             : 
     225                 :         600 :             int nOut = prevOut.find_value("vout").getInt<int>();
     226         [ -  + ]:         600 :             if (nOut < 0) {
     227   [ #  #  #  # ]:           0 :                 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout cannot be negative");
     228                 :             :             }
     229                 :             : 
     230                 :         600 :             COutPoint out(txid, nOut);
     231                 :         600 :             std::vector<unsigned char> pkData(ParseHexO(prevOut, "scriptPubKey"));
     232                 :         600 :             CScript scriptPubKey(pkData.begin(), pkData.end());
     233                 :             : 
     234                 :         600 :             {
     235                 :         600 :                 auto coin = coins.find(out);
     236   [ +  -  +  +  :         600 :                 if (coin != coins.end() && !coin->second.IsSpent() && coin->second.out.scriptPubKey != scriptPubKey) {
                   -  + ]
     237         [ #  # ]:           0 :                     std::string err("Previous output scriptPubKey mismatch:\n");
     238   [ #  #  #  # ]:           0 :                     err = err + ScriptToAsmStr(coin->second.out.scriptPubKey) + "\nvs:\n"+
     239   [ #  #  #  # ]:           0 :                         ScriptToAsmStr(scriptPubKey);
     240         [ #  # ]:           0 :                     throw JSONRPCError(RPC_DESERIALIZATION_ERROR, err);
     241                 :           0 :                 }
     242                 :         600 :                 Coin newcoin;
     243                 :         600 :                 newcoin.out.scriptPubKey = scriptPubKey;
     244                 :         600 :                 newcoin.out.nValue = MAX_MONEY;
     245   [ +  -  +  + ]:        1200 :                 if (prevOut.exists("amount")) {
     246   [ +  -  +  - ]:         587 :                     newcoin.out.nValue = AmountFromValue(prevOut.find_value("amount"));
     247                 :             :                 }
     248                 :         600 :                 newcoin.nHeight = 1;
     249         [ +  - ]:         600 :                 coins[out] = std::move(newcoin);
     250                 :           0 :             }
     251                 :             : 
     252                 :             :             // if redeemScript and private keys were given, add redeemScript to the keystore so it can be signed
     253         [ +  - ]:         600 :             const bool is_p2sh = scriptPubKey.IsPayToScriptHash();
     254         [ +  - ]:         600 :             const bool is_p2wsh = scriptPubKey.IsPayToWitnessScriptHash();
     255   [ +  +  +  + ]:         600 :             if (keystore && (is_p2sh || is_p2wsh)) {
     256   [ +  -  +  +  :        1080 :                 RPCTypeCheckObj(prevOut,
                   -  - ]
     257                 :             :                     {
     258         [ +  - ]:         270 :                         {"redeemScript", UniValueType(UniValue::VSTR)},
     259         [ +  - ]:         270 :                         {"witnessScript", UniValueType(UniValue::VSTR)},
     260                 :             :                     }, true);
     261         [ +  - ]:         270 :                 const UniValue& rs{prevOut.find_value("redeemScript")};
     262         [ +  - ]:         270 :                 const UniValue& ws{prevOut.find_value("witnessScript")};
     263   [ +  +  +  + ]:         270 :                 if (rs.isNull() && ws.isNull()) {
     264   [ +  -  +  - ]:          42 :                     throw JSONRPCError(RPC_INVALID_PARAMETER, "Missing redeemScript/witnessScript");
     265                 :             :                 }
     266                 :             : 
     267                 :             :                 // work from witnessScript when possible
     268   [ +  +  +  -  :         249 :                 std::vector<unsigned char> scriptData(!ws.isNull() ? ParseHexV(ws, "witnessScript") : ParseHexV(rs, "redeemScript"));
                   +  - ]
     269                 :         249 :                 CScript script(scriptData.begin(), scriptData.end());
     270   [ +  -  +  - ]:         249 :                 keystore->scripts.emplace(CScriptID(script), script);
     271                 :             :                 // Automatically also add the P2WSH wrapped version of the script (to deal with P2SH-P2WSH).
     272                 :             :                 // This is done for redeemScript only for compatibility, it is encouraged to use the explicit witnessScript field instead.
     273   [ +  -  +  - ]:         249 :                 CScript witness_output_script{GetScriptForDestination(WitnessV0ScriptHash(script))};
     274   [ +  -  +  - ]:         249 :                 keystore->scripts.emplace(CScriptID(witness_output_script), witness_output_script);
     275                 :             : 
     276   [ +  +  +  + ]:         249 :                 if (!ws.isNull() && !rs.isNull()) {
     277                 :             :                     // if both witnessScript and redeemScript are provided,
     278                 :             :                     // they should either be the same (for backwards compat),
     279                 :             :                     // or the redeemScript should be the encoded form of
     280                 :             :                     // the witnessScript (ie, for p2sh-p2wsh)
     281   [ +  -  +  -  :          45 :                     if (ws.get_str() != rs.get_str()) {
                   +  + ]
     282         [ +  - ]:          24 :                         std::vector<unsigned char> redeemScriptData(ParseHexV(rs, "redeemScript"));
     283                 :          24 :                         CScript redeemScript(redeemScriptData.begin(), redeemScriptData.end());
     284         [ +  + ]:          24 :                         if (redeemScript != witness_output_script) {
     285   [ +  -  +  - ]:          42 :                             throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript does not correspond to witnessScript");
     286                 :             :                         }
     287                 :          45 :                     }
     288                 :             :                 }
     289                 :             : 
     290         [ +  + ]:         228 :                 if (is_p2sh) {
     291         [ +  - ]:         180 :                     const CTxDestination p2sh{ScriptHash(script)};
     292         [ +  - ]:         180 :                     const CTxDestination p2sh_p2wsh{ScriptHash(witness_output_script)};
     293   [ +  -  +  + ]:         180 :                     if (scriptPubKey == GetScriptForDestination(p2sh)) {
     294                 :             :                         // traditional p2sh; arguably an error if
     295                 :             :                         // we got here with rs.IsNull(), because
     296                 :             :                         // that means the p2sh script was specified
     297                 :             :                         // via witnessScript param, but for now
     298                 :             :                         // we'll just quietly accept it
     299   [ +  -  +  + ]:          58 :                     } else if (scriptPubKey == GetScriptForDestination(p2sh_p2wsh)) {
     300                 :             :                         // p2wsh encoded as p2sh; ideally the witness
     301                 :             :                         // script was specified in the witnessScript
     302                 :             :                         // param, but also support specifying it via
     303                 :             :                         // redeemScript param for backwards compat
     304                 :             :                         // (in which case ws.IsNull() == true)
     305                 :             :                     } else {
     306                 :             :                         // otherwise, can't generate scriptPubKey from
     307                 :             :                         // either script, so we got unusable parameters
     308   [ +  -  +  - ]:          52 :                         throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript/witnessScript does not match scriptPubKey");
     309                 :             :                     }
     310         [ +  - ]:         254 :                 } else if (is_p2wsh) {
     311                 :             :                     // plain p2wsh; could throw an error if script
     312                 :             :                     // was specified by redeemScript rather than
     313                 :             :                     // witnessScript (ie, ws.IsNull() == true), but
     314                 :             :                     // accept it for backwards compat
     315         [ +  - ]:          48 :                     const CTxDestination p2wsh{WitnessV0ScriptHash(script)};
     316   [ +  -  +  + ]:          48 :                     if (scriptPubKey != GetScriptForDestination(p2wsh)) {
     317   [ +  -  +  - ]:          32 :                         throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript/witnessScript does not match scriptPubKey");
     318                 :             :                     }
     319                 :          48 :                 }
     320                 :         375 :             }
     321                 :         600 :         }
     322                 :             :     }
     323   [ +  -  +  -  :        1315 : }
          +  -  +  -  +  
          -  +  -  +  -  
             -  +  -  - ]
     324                 :             : 
     325                 :         113 : void SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, const std::map<COutPoint, Coin>& coins, const UniValue& hashType, UniValue& result)
     326                 :             : {
     327                 :         113 :     std::optional<int> nHashType = ParseSighashString(hashType);
     328         [ +  - ]:         112 :     if (!nHashType) {
     329                 :         112 :         nHashType = SIGHASH_DEFAULT;
     330                 :             :     }
     331                 :             : 
     332                 :             :     // Script verification errors
     333         [ +  - ]:         112 :     std::map<int, bilingual_str> input_errors;
     334                 :             : 
     335         [ +  - ]:         112 :     bool complete = SignTransaction(mtx, keystore, coins, {.sighash_type = *nHashType}, input_errors);
     336         [ +  - ]:         112 :     SignTransactionResultToJSON(mtx, complete, coins, input_errors, result);
     337                 :             : }
     338                 :             : 
     339                 :         425 : void SignTransactionResultToJSON(CMutableTransaction& mtx, bool complete, const std::map<COutPoint, Coin>& coins, const std::map<int, bilingual_str>& input_errors, UniValue& result)
     340                 :             : {
     341                 :             :     // Make errors UniValue
     342                 :         425 :     UniValue vErrors(UniValue::VARR);
     343         [ +  + ]:         521 :     for (const auto& err_pair : input_errors) {
     344         [ +  + ]:          98 :         if (err_pair.second.original == "Missing amount") {
     345                 :             :             // This particular error needs to be an exception for some reason
     346   [ +  -  +  -  :           4 :             throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing amount for %s", coins.at(mtx.vin.at(err_pair.first).prevout).out.ToString()));
          +  -  +  -  +  
                      - ]
     347                 :             :         }
     348   [ +  -  +  - ]:          96 :         TxInErrorToJSON(mtx.vin.at(err_pair.first), vErrors, err_pair.second.original);
     349                 :             :     }
     350                 :             : 
     351   [ +  -  +  -  :         846 :     result.pushKV("hex", EncodeHexTx(CTransaction(mtx)));
          +  -  +  -  +  
                      - ]
     352   [ +  -  +  -  :         846 :     result.pushKV("complete", complete);
                   +  - ]
     353   [ -  +  +  + ]:         423 :     if (!vErrors.empty()) {
     354   [ +  -  -  + ]:         188 :         if (result.exists("errors")) {
     355   [ #  #  #  #  :           0 :             vErrors.push_backV(result["errors"].getValues());
             #  #  #  # ]
     356                 :             :         }
     357   [ +  -  +  - ]:         188 :         result.pushKV("errors", std::move(vErrors));
     358                 :             :     }
     359                 :         425 : }
     360                 :             : 
     361                 :       48227 : std::vector<RPCResult> TxDoc(const TxDocOptions& opts)
     362                 :             : {
     363   [ +  +  -  + ]:       48227 :     CHECK_NONFATAL(!opts.fee_doc || opts.fee);
     364   [ +  +  -  + ]:       48227 :     CHECK_NONFATAL(!opts.prevout_doc || opts.prevout);
     365   [ +  +  +  - ]:       59407 :     CHECK_NONFATAL(!opts.vin_item_doc || opts.vin_inner_elision);
     366   [ +  +  +  - ]:       63840 :     CHECK_NONFATAL(opts.elision_mode != ElisionMode::WithSummary || opts.elision_summary.has_value());
     367                 :             : 
     368                 :       48227 :     const std::string fee_doc{opts.fee_doc.value_or(
     369   [ +  -  +  - ]:      144681 :         "transaction fee in " + CURRENCY_UNIT + ", omitted if block undo data is not available")};
     370                 :       48227 :     const std::string prevout_doc{opts.prevout_doc.value_or(
     371         [ +  - ]:       48227 :         "The previous output, omitted if block undo data is not available")};
     372         [ +  - ]:       48227 :     const std::string vin_item_doc{opts.vin_item_doc.value_or("utxo being spent")};
     373                 :             : 
     374                 :       48227 :     auto vin_inner = std::vector<RPCResult>{
     375   [ +  -  +  - ]:       96454 :         {RPCResult::Type::STR_HEX, "coinbase", /*optional=*/true, "The coinbase value (only if coinbase transaction)"},
     376   [ +  -  +  - ]:       96454 :         {RPCResult::Type::STR_HEX, "txid", /*optional=*/true, "The transaction id (if not coinbase transaction)"},
     377   [ +  -  +  - ]:       96454 :         {RPCResult::Type::NUM, "vout", /*optional=*/true, "The output number (if not coinbase transaction)"},
     378   [ +  -  +  - ]:       96454 :         {RPCResult::Type::OBJ, "scriptSig", /*optional=*/true, "The script (if not coinbase transaction)",
     379                 :             :         {
     380   [ +  -  +  - ]:       96454 :             {RPCResult::Type::STR, "asm", "Disassembly of the signature script"},
     381   [ +  -  +  - ]:       96454 :             {RPCResult::Type::STR_HEX, "hex", "The raw signature script bytes, hex-encoded"},
     382                 :             :         }},
     383   [ +  -  +  - ]:       96454 :         {RPCResult::Type::ARR, "txinwitness", /*optional=*/true, "",
     384                 :             :         {
     385   [ +  -  +  - ]:       96454 :             {RPCResult::Type::STR_HEX, "hex", "hex-encoded witness data (if any)"},
     386                 :             :         }},
     387   [ +  -  +  -  :      916313 :     };
          -  +  +  +  +  
          +  +  +  -  -  
             -  -  -  - ]
     388         [ +  + ]:       48227 :     if (opts.prevout) {
     389                 :       34790 :         vin_inner.emplace_back(
     390         [ +  - ]:       17395 :             RPCResult::Type::OBJ, "prevout", opts.prevout_optional, prevout_doc,
     391                 :       17395 :             std::vector<RPCResult>{
     392   [ +  -  +  - ]:       34790 :                 {RPCResult::Type::BOOL, "generated", "Coinbase or not"},
     393   [ +  -  +  - ]:       34790 :                 {RPCResult::Type::NUM, "height", "The height of the prevout"},
     394   [ +  -  +  - ]:       34790 :                 {RPCResult::Type::STR_AMOUNT, "value", "The value in " + CURRENCY_UNIT},
     395   [ +  -  +  -  :       34790 :                 {RPCResult::Type::OBJ, "scriptPubKey", "", ScriptPubKeyDoc()},
                   +  - ]
     396   [ +  -  +  +  :      156555 :             }
                   -  - ]
     397                 :             :         );
     398                 :             :     }
     399         [ +  - ]:       48227 :     vin_inner.emplace_back(RPCResult::Type::NUM, "sequence", "The script sequence number");
     400                 :             : 
     401         [ +  + ]:       48227 :     if (opts.vin_inner_elision) {
     402   [ -  +  +  - ]:       52185 :         vin_inner = ElideGroup(std::move(vin_inner), *opts.vin_inner_elision);
     403         [ +  - ]:       17395 :         if (opts.prevout) {
     404                 :             :             // prevout remains visible even when other fields are elided
     405                 :       17395 :             std::vector<RPCResult> new_vin;
     406   [ -  +  +  - ]:       17395 :             new_vin.reserve(vin_inner.size());
     407         [ +  + ]:      139160 :             for (const auto& r : vin_inner) {
     408         [ +  + ]:      121765 :                 if (r.m_key_name == "prevout") {
     409         [ +  - ]:       17395 :                     RPCResultOptions unopts = r.m_opts;
     410                 :       17395 :                     unopts.print_elision = HelpElisionNone{};
     411         [ +  - ]:       17395 :                     new_vin.emplace_back(r, std::move(unopts));
     412                 :       17395 :                 } else {
     413         [ +  - ]:      104370 :                     new_vin.push_back(r);
     414                 :             :                 }
     415                 :             :             }
     416                 :       17395 :             vin_inner = std::move(new_vin);
     417                 :       17395 :         }
     418                 :             :     }
     419                 :             : 
     420                 :       48227 :     auto fields = std::vector<RPCResult>{
     421   [ +  -  -  + ]:       48227 :         {RPCResult::Type::STR_HEX, "txid", opts.txid_field_doc},
     422   [ +  -  +  - ]:       96454 :         {RPCResult::Type::STR_HEX, "hash", "The transaction hash (differs from txid for witness transactions)"},
     423   [ +  -  +  - ]:       96454 :         {RPCResult::Type::NUM, "size", "The serialized transaction size"},
     424   [ +  -  +  - ]:       96454 :         {RPCResult::Type::NUM, "vsize", "The virtual transaction size (differs from size for witness transactions)"},
     425   [ +  -  +  - ]:       96454 :         {RPCResult::Type::NUM, "weight", "The transaction's weight (between vsize*4-3 and vsize*4)"},
     426   [ +  -  +  - ]:       96454 :         {RPCResult::Type::NUM, "version", "The version"},
     427   [ +  -  +  - ]:       96454 :         {RPCResult::Type::NUM_TIME, "locktime", "The lock time"},
     428   [ +  -  +  - ]:       96454 :         {RPCResult::Type::ARR, "vin", "",
     429                 :             :         {
     430   [ +  -  +  +  :      113849 :             {RPCResult::Type::OBJ, "", opts.vin_inner_elision ? vin_item_doc : "", std::move(vin_inner)},
             +  -  +  - ]
     431                 :             :         }},
     432   [ +  -  +  - ]:       96454 :         {RPCResult::Type::ARR, "vout", "",
     433                 :             :         {
     434   [ +  -  +  -  :      434043 :             {RPCResult::Type::OBJ, "", "", Cat(
          +  -  +  -  +  
                +  -  - ]
     435                 :             :                 {
     436   [ +  -  +  - ]:       96454 :                     {RPCResult::Type::STR_AMOUNT, "value", "The value in " + CURRENCY_UNIT},
     437   [ +  -  +  - ]:       96454 :                     {RPCResult::Type::NUM, "n", "index"},
     438   [ +  -  +  -  :       96454 :                     {RPCResult::Type::OBJ, "scriptPubKey", "", ScriptPubKeyDoc()},
                   +  - ]
     439                 :             :                 },
     440   [ +  +  +  -  :      101074 :                 opts.wallet ?
          +  +  +  +  -  
                -  -  - ]
     441   [ +  -  +  -  :       57467 :                     std::vector<RPCResult>{{RPCResult::Type::BOOL, "ischange", /*optional=*/true, "Output script is change (only present if true)"}} :
          +  +  +  +  +  
          +  -  -  -  -  
                   -  - ]
     442                 :             :                     std::vector<RPCResult>{}
     443                 :             :             )},
     444                 :             :         }},
     445   [ +  -  +  -  :     1157448 :     };
          -  +  +  +  +  
          +  +  +  -  -  
             -  -  -  - ]
     446                 :             : 
     447   [ +  +  +  - ]:       48227 :     if (opts.fee) fields.emplace_back(RPCResult::Type::NUM, "fee", /*optional=*/true, fee_doc);
     448   [ +  +  +  - ]:       48227 :     if (opts.hex) fields.emplace_back(RPCResult::Type::STR_HEX, "hex", "The hex-encoded transaction data");
     449                 :             : 
     450         [ +  + ]:       48227 :     if (opts.elision_mode != ElisionMode::None) {
     451                 :       33008 :         const bool silent = opts.elision_mode == ElisionMode::Silent;
     452                 :       33008 :         std::vector<RPCResult> new_fields;
     453   [ -  +  +  - ]:       33008 :         new_fields.reserve(fields.size());
     454                 :       33008 :         bool first = true;
     455         [ +  + ]:      374800 :         for (const auto& f : fields) {
     456   [ +  +  +  + ]:      341792 :             if (!silent && f.m_key_name == "fee") {
     457         [ +  - ]:       11180 :                 new_fields.push_back(f);
     458                 :       11180 :                 continue;
     459                 :             :             }
     460   [ +  +  +  + ]:      330612 :             if (f.m_key_name == "vin" && opts.vin_inner_elision) {
     461         [ +  - ]:       17395 :                 new_fields.push_back(f);
     462                 :       17395 :                 continue;
     463                 :             :             }
     464         [ +  + ]:      313217 :             if (!silent && first) {
     465         [ +  - ]:       15613 :                 RPCResultOptions eopts = f.m_opts;
     466         [ +  - ]:       15613 :                 eopts.print_elision = opts.elision_summary.value_or("");
     467         [ +  - ]:       15613 :                 new_fields.emplace_back(f, std::move(eopts));
     468                 :       15613 :                 first = false;
     469                 :       15613 :             } else {
     470         [ +  - ]:      297604 :                 RPCResultOptions eopts = f.m_opts;
     471                 :      297604 :                 eopts.print_elision = HelpElisionSkip{};
     472         [ +  - ]:      297604 :                 new_fields.emplace_back(f, std::move(eopts));
     473                 :      297604 :             }
     474                 :             :         }
     475                 :       33008 :         fields = std::move(new_fields);
     476                 :       33008 :     }
     477                 :             : 
     478                 :       96454 :     return fields;
     479   [ +  -  +  -  :     2362222 : }
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  +  -  -  -  
          -  -  -  -  -  
             -  -  -  - ]
        

Generated by: LCOV version 2.0-1