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

Generated by: LCOV version 2.0-1