LCOV - code coverage report
Current view: top level - src/rpc - rawtransaction_util.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 68.6 % 296 203
Test Date: 2026-11-19 05:38:02 Functions: 100.0 % 10 10
Branches: 39.7 % 838 333

             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                 :         175 : static void AddInputs(CMutableTransaction& rawTx, const UniValue& inputs_in, std::optional<bool> rbf)
      37                 :             : {
      38         [ -  + ]:         175 :     UniValue inputs;
      39         [ -  + ]:         175 :     if (inputs_in.isNull()) {
      40                 :           0 :         inputs = UniValue::VARR;
      41                 :             :     } else {
      42   [ +  -  +  - ]:         175 :         inputs = inputs_in.get_array();
      43                 :             :     }
      44                 :             : 
      45   [ -  +  +  + ]:         175 :     for (unsigned int idx = 0; idx < inputs.size(); idx++) {
      46         [ +  - ]:           5 :         const UniValue& input = inputs[idx];
      47         [ +  + ]:           5 :         const UniValue& o = input.get_obj();
      48                 :             : 
      49         [ -  + ]:           1 :         Txid txid = Txid::FromUint256(ParseHashO(o, "txid"));
      50                 :             : 
      51         [ #  # ]:           0 :         const UniValue& vout_v = o.find_value("vout");
      52         [ #  # ]:           0 :         if (!vout_v.isNum())
      53   [ #  #  #  # ]:           0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
      54         [ #  # ]:           0 :         int nOutput = vout_v.getInt<int>();
      55         [ #  # ]:           0 :         if (nOutput < 0)
      56   [ #  #  #  # ]:           0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative");
      57                 :             : 
      58                 :           0 :         uint32_t nSequence;
      59                 :             : 
      60   [ #  #  #  # ]:           0 :         if (rbf.value_or(true)) {
      61                 :             :             nSequence = MAX_BIP125_RBF_SEQUENCE; /* CTxIn::SEQUENCE_FINAL - 2 */
      62         [ #  # ]:           0 :         } else if (rawTx.nLockTime) {
      63                 :             :             nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; /* CTxIn::SEQUENCE_FINAL - 1 */
      64                 :             :         } else {
      65                 :           0 :             nSequence = CTxIn::SEQUENCE_FINAL;
      66                 :             :         }
      67                 :             : 
      68                 :             :         // set the sequence number if passed in the parameters object
      69         [ #  # ]:           0 :         const UniValue& sequenceObj = o.find_value("sequence");
      70         [ #  # ]:           0 :         if (sequenceObj.isNum()) {
      71         [ #  # ]:           0 :             int64_t seqNr64 = sequenceObj.getInt<int64_t>();
      72         [ #  # ]:           0 :             if (seqNr64 < 0 || seqNr64 > CTxIn::SEQUENCE_FINAL) {
      73   [ #  #  #  # ]:           0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, sequence number is out of range");
      74                 :             :             } else {
      75                 :           0 :                 nSequence = (uint32_t)seqNr64;
      76                 :             :             }
      77                 :             :         }
      78                 :             : 
      79         [ #  # ]:           0 :         CTxIn in(COutPoint(txid, nOutput), CScript(), nSequence);
      80                 :             : 
      81         [ #  # ]:           0 :         rawTx.vin.push_back(in);
      82                 :           0 :     }
      83                 :         175 : }
      84                 :             : 
      85                 :         170 : UniValue NormalizeOutputs(const UniValue& outputs_in)
      86                 :             : {
      87         [ +  + ]:         170 :     if (outputs_in.isNull()) {
      88   [ +  -  +  - ]:           6 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, output argument must be non-null");
      89                 :             :     }
      90                 :             : 
      91                 :         167 :     const bool outputs_is_obj = outputs_in.isObject();
      92         [ +  + ]:         167 :     UniValue outputs = outputs_is_obj ? outputs_in.get_obj() : outputs_in.get_array();
      93                 :             : 
      94         [ +  + ]:         166 :     if (!outputs_is_obj) {
      95                 :             :         // Translate array of key-value pairs into dict
      96                 :          49 :         UniValue outputs_dict = UniValue(UniValue::VOBJ);
      97   [ -  +  +  + ]:         359 :         for (size_t i = 0; i < outputs.size(); ++i) {
      98         [ +  - ]:         317 :             const UniValue& output = outputs[i];
      99         [ +  + ]:         317 :             if (!output.isObject()) {
     100   [ +  -  +  - ]:          10 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, key-value pair not an object as expected");
     101                 :             :             }
     102   [ -  +  +  + ]:         312 :             if (output.size() != 1) {
     103   [ +  -  +  - ]:           4 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, key-value pair must contain exactly one key");
     104                 :             :             }
     105   [ +  -  +  - ]:         310 :             outputs_dict.pushKVs(output);
     106                 :             :         }
     107                 :          42 :         outputs = std::move(outputs_dict);
     108                 :          49 :     }
     109                 :         159 :     return outputs;
     110                 :           7 : }
     111                 :             : 
     112                 :         159 : std::vector<std::pair<CTxDestination, CAmount>> ParseOutputs(const UniValue& outputs)
     113                 :             : {
     114                 :             :     // Duplicate checking
     115         [ +  - ]:         159 :     std::set<CTxDestination> destinations;
     116                 :         159 :     std::vector<std::pair<CTxDestination, CAmount>> parsed_outputs;
     117                 :         159 :     bool has_data{false};
     118         [ +  - ]:         159 :     const auto& keys{outputs.getKeys()};
     119         [ +  - ]:         159 :     const auto& values{outputs.getValues()};
     120   [ -  +  +  + ]:         159 :     for (size_t i{0}; i < keys.size(); ++i) {
     121         [ +  + ]:         152 :         const auto& name_{keys[i]};
     122                 :         152 :         const auto& value{values[i]};
     123         [ +  + ]:         152 :         if (name_ == "data") {
     124         [ -  + ]:           2 :             if (has_data) {
     125   [ #  #  #  # ]:           0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, duplicate key: data");
     126                 :             :             }
     127                 :           2 :             has_data = true;
     128   [ +  -  -  + ]:           2 :             std::vector<unsigned char> data = ParseHexV(value.getValStr(), "Data");
     129   [ #  #  #  # ]:           0 :             CTxDestination destination{CNoDestination{CScript() << OP_RETURN << data}};
     130                 :           0 :             CAmount amount{0};
     131         [ #  # ]:           0 :             parsed_outputs.emplace_back(destination, amount);
     132                 :           0 :         } else {
     133         [ +  - ]:         150 :             CTxDestination destination{DecodeDestination(name_)};
     134         [ +  + ]:         150 :             CAmount amount{AmountFromValue(value)};
     135   [ +  -  +  - ]:           2 :             if (!IsValidDestination(destination)) {
     136   [ +  -  +  -  :           4 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ") + name_);
                   +  - ]
     137                 :             :             }
     138                 :             : 
     139   [ #  #  #  # ]:           0 :             if (!destinations.insert(destination).second) {
     140   [ #  #  #  #  :           0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ") + name_);
                   #  # ]
     141                 :             :             }
     142         [ #  # ]:           0 :             parsed_outputs.emplace_back(destination, amount);
     143                 :         150 :         }
     144                 :             :     }
     145                 :           7 :     return parsed_outputs;
     146                 :         159 : }
     147                 :             : 
     148                 :         170 : void AddOutputs(CMutableTransaction& rawTx, const UniValue& outputs_in)
     149                 :             : {
     150                 :         170 :     UniValue outputs(UniValue::VOBJ);
     151         [ +  + ]:         170 :     outputs = NormalizeOutputs(outputs_in);
     152                 :             : 
     153         [ +  + ]:         159 :     std::vector<std::pair<CTxDestination, CAmount>> parsed_outputs = ParseOutputs(outputs);
     154   [ -  -  -  + ]:           7 :     for (const auto& [destination, nAmount] : parsed_outputs) {
     155         [ #  # ]:           0 :         CScript scriptPubKey = GetScriptForDestination(destination);
     156                 :             : 
     157         [ #  # ]:           0 :         CTxOut out(nAmount, scriptPubKey);
     158         [ #  # ]:           0 :         rawTx.vout.push_back(out);
     159                 :           0 :     }
     160                 :         170 : }
     161                 :             : 
     162                 :         191 : CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniValue& outputs_in, const UniValue& locktime, std::optional<bool> rbf, const uint32_t version)
     163                 :             : {
     164                 :         191 :     CMutableTransaction rawTx;
     165                 :             : 
     166         [ +  + ]:         191 :     if (!locktime.isNull()) {
     167         [ +  + ]:          22 :         int64_t nLockTime = locktime.getInt<int64_t>();
     168         [ +  + ]:          12 :         if (nLockTime < 0 || nLockTime > LOCKTIME_MAX)
     169   [ +  -  +  - ]:           8 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, locktime out of range");
     170                 :           8 :         rawTx.nLockTime = nLockTime;
     171                 :             :     }
     172                 :             : 
     173         [ +  + ]:         177 :     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                 :         175 :     rawTx.version = version;
     177                 :             : 
     178         [ +  + ]:         175 :     AddInputs(rawTx, inputs_in, rbf);
     179         [ +  + ]:         170 :     AddOutputs(rawTx, outputs_in);
     180                 :             : 
     181                 :           7 :     if (rbf.has_value() && rbf.value() && rawTx.vin.size() > 0 && !SignalsOptInRBF(CTransaction(rawTx))) {
           [ +  +  +  +  
          -  +  -  +  -  
          -  -  -  -  -  
                   -  + ]
     182   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter combination: Sequence number(s) contradict replaceable option");
     183                 :             :     }
     184                 :             : 
     185                 :           7 :     return rawTx;
     186                 :         184 : }
     187                 :             : 
     188                 :             : /** Pushes a JSON object for script verification or signing errors to vErrorsRet. */
     189                 :        7437 : static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::string& strMessage)
     190                 :             : {
     191                 :        7437 :     UniValue entry(UniValue::VOBJ);
     192   [ +  -  +  -  :       14874 :     entry.pushKV("txid", txin.prevout.hash.ToString());
             +  -  +  - ]
     193   [ +  -  +  -  :       14874 :     entry.pushKV("vout", txin.prevout.n);
                   +  - ]
     194                 :        7437 :     UniValue witness(UniValue::VARR);
     195   [ -  +  +  + ]:       10547 :     for (unsigned int i = 0; i < txin.scriptWitness.stack.size(); i++) {
     196   [ -  +  +  -  :        3110 :         witness.push_back(HexStr(txin.scriptWitness.stack[i]));
             +  -  +  - ]
     197                 :             :     }
     198   [ +  -  +  - ]:       14874 :     entry.pushKV("witness", std::move(witness));
     199   [ +  +  +  -  :       22311 :     entry.pushKV("scriptSig", HexStr(txin.scriptSig));
          +  -  +  -  +  
                      - ]
     200   [ +  -  +  -  :       14874 :     entry.pushKV("sequence", txin.nSequence);
                   +  - ]
     201   [ +  -  +  -  :       14874 :     entry.pushKV("error", strMessage);
                   +  - ]
     202         [ +  - ]:        7437 :     vErrorsRet.push_back(std::move(entry));
     203                 :        7437 : }
     204                 :             : 
     205                 :         117 : void ParsePrevouts(const UniValue& prevTxsUnival, FlatSigningProvider* keystore, std::map<COutPoint, Coin>& coins)
     206                 :             : {
     207         [ +  + ]:         117 :     if (!prevTxsUnival.isNull()) {
     208                 :          14 :         const UniValue& prevTxs = prevTxsUnival.get_array();
     209   [ -  +  +  + ]:          14 :         for (unsigned int idx = 0; idx < prevTxs.size(); ++idx) {
     210                 :          12 :             const UniValue& p = prevTxs[idx];
     211         [ +  + ]:          12 :             if (!p.isObject()) {
     212   [ +  -  +  - ]:          20 :                 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "expected object with {\"txid'\",\"vout\",\"scriptPubKey\"}");
     213                 :             :             }
     214                 :             : 
     215                 :           2 :             const UniValue& prevOut = p.get_obj();
     216                 :             : 
     217   [ -  +  -  -  :          12 :             RPCTypeCheckObj(prevOut,
                   +  + ]
     218                 :             :                 {
     219         [ +  - ]:           2 :                     {"txid", UniValueType(UniValue::VSTR)},
     220         [ +  - ]:           2 :                     {"vout", UniValueType(UniValue::VNUM)},
     221         [ +  - ]:           2 :                     {"scriptPubKey", UniValueType(UniValue::VSTR)},
     222                 :             :                 });
     223                 :             : 
     224                 :           0 :             Txid txid = Txid::FromUint256(ParseHashO(prevOut, "txid"));
     225                 :             : 
     226                 :           0 :             int nOut = prevOut.find_value("vout").getInt<int>();
     227         [ #  # ]:           0 :             if (nOut < 0) {
     228   [ #  #  #  # ]:           0 :                 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout cannot be negative");
     229                 :             :             }
     230                 :             : 
     231                 :           0 :             COutPoint out(txid, nOut);
     232                 :           0 :             std::vector<unsigned char> pkData(ParseHexO(prevOut, "scriptPubKey"));
     233                 :           0 :             CScript scriptPubKey(pkData.begin(), pkData.end());
     234                 :             : 
     235                 :           0 :             {
     236                 :           0 :                 auto coin = coins.find(out);
     237   [ #  #  #  #  :           0 :                 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                 :           0 :                 Coin newcoin;
     244                 :           0 :                 newcoin.out.scriptPubKey = scriptPubKey;
     245                 :           0 :                 newcoin.out.nValue = MAX_MONEY;
     246   [ #  #  #  # ]:           0 :                 if (prevOut.exists("amount")) {
     247   [ #  #  #  # ]:           0 :                     newcoin.out.nValue = AmountFromValue(prevOut.find_value("amount"));
     248                 :             :                 }
     249                 :           0 :                 newcoin.nHeight = 1;
     250         [ #  # ]:           0 :                 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         [ #  # ]:           0 :             const bool is_p2sh = scriptPubKey.IsPayToScriptHash();
     255         [ #  # ]:           0 :             const bool is_p2wsh = scriptPubKey.IsPayToWitnessScriptHash();
     256   [ #  #  #  # ]:           0 :             if (keystore && (is_p2sh || is_p2wsh)) {
     257   [ #  #  #  #  :           0 :                 RPCTypeCheckObj(prevOut,
                   #  # ]
     258                 :             :                     {
     259         [ #  # ]:           0 :                         {"redeemScript", UniValueType(UniValue::VSTR)},
     260         [ #  # ]:           0 :                         {"witnessScript", UniValueType(UniValue::VSTR)},
     261                 :             :                     }, true);
     262         [ #  # ]:           0 :                 const UniValue& rs{prevOut.find_value("redeemScript")};
     263         [ #  # ]:           0 :                 const UniValue& ws{prevOut.find_value("witnessScript")};
     264   [ #  #  #  # ]:           0 :                 if (rs.isNull() && ws.isNull()) {
     265   [ #  #  #  # ]:           0 :                     throw JSONRPCError(RPC_INVALID_PARAMETER, "Missing redeemScript/witnessScript");
     266                 :             :                 }
     267                 :             : 
     268                 :             :                 // work from witnessScript when possible
     269   [ #  #  #  #  :           0 :                 std::vector<unsigned char> scriptData(!ws.isNull() ? ParseHexV(ws, "witnessScript") : ParseHexV(rs, "redeemScript"));
                   #  # ]
     270                 :           0 :                 CScript script(scriptData.begin(), scriptData.end());
     271   [ #  #  #  # ]:           0 :                 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   [ #  #  #  # ]:           0 :                 CScript witness_output_script{GetScriptForDestination(WitnessV0ScriptHash(script))};
     275   [ #  #  #  # ]:           0 :                 keystore->scripts.emplace(CScriptID(witness_output_script), witness_output_script);
     276                 :             : 
     277   [ #  #  #  # ]:           0 :                 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   [ #  #  #  #  :           0 :                     if (ws.get_str() != rs.get_str()) {
                   #  # ]
     283         [ #  # ]:           0 :                         std::vector<unsigned char> redeemScriptData(ParseHexV(rs, "redeemScript"));
     284                 :           0 :                         CScript redeemScript(redeemScriptData.begin(), redeemScriptData.end());
     285         [ #  # ]:           0 :                         if (redeemScript != witness_output_script) {
     286   [ #  #  #  # ]:           0 :                             throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript does not correspond to witnessScript");
     287                 :             :                         }
     288                 :           0 :                     }
     289                 :             :                 }
     290                 :             : 
     291         [ #  # ]:           0 :                 if (is_p2sh) {
     292         [ #  # ]:           0 :                     const CTxDestination p2sh{ScriptHash(script)};
     293         [ #  # ]:           0 :                     const CTxDestination p2sh_p2wsh{ScriptHash(witness_output_script)};
     294   [ #  #  #  # ]:           0 :                     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   [ #  #  #  # ]:           0 :                     } 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   [ #  #  #  # ]:           0 :                         throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript/witnessScript does not match scriptPubKey");
     310                 :             :                     }
     311         [ #  # ]:           0 :                 } 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         [ #  # ]:           0 :                     const CTxDestination p2wsh{WitnessV0ScriptHash(script)};
     317   [ #  #  #  # ]:           0 :                     if (scriptPubKey != GetScriptForDestination(p2wsh)) {
     318   [ #  #  #  # ]:           0 :                         throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript/witnessScript does not match scriptPubKey");
     319                 :             :                     }
     320                 :           0 :                 }
     321                 :           0 :             }
     322                 :           0 :         }
     323                 :             :     }
     324                 :         109 : }
           [ +  -  +  -  
          +  -  +  -  -  
          -  -  -  -  -  
             -  +  -  - ]
     325                 :             : 
     326                 :         105 : void SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, const std::map<COutPoint, Coin>& coins, const UniValue& hashType, UniValue& result)
     327                 :             : {
     328                 :         105 :     std::optional<int> nHashType = ParseSighashString(hashType);
     329         [ +  - ]:         103 :     if (!nHashType) {
     330                 :         103 :         nHashType = SIGHASH_DEFAULT;
     331                 :             :     }
     332                 :             : 
     333                 :             :     // Script verification errors
     334         [ +  - ]:         103 :     std::map<int, bilingual_str> input_errors;
     335                 :             : 
     336         [ +  - ]:         103 :     bool complete = SignTransaction(mtx, keystore, coins, {.sighash_type = *nHashType}, input_errors);
     337         [ +  - ]:         103 :     SignTransactionResultToJSON(mtx, complete, coins, input_errors, result);
     338                 :         103 : }
     339                 :             : 
     340                 :         103 : 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                 :         103 :     UniValue vErrors(UniValue::VARR);
     344         [ +  + ]:        7540 :     for (const auto& err_pair : input_errors) {
     345         [ -  + ]:        7437 :         if (err_pair.second.original == "Missing amount") {
     346                 :             :             // This particular error needs to be an exception for some reason
     347   [ #  #  #  #  :           0 :             throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing amount for %s", coins.at(mtx.vin.at(err_pair.first).prevout).out.ToString()));
          #  #  #  #  #  
                      # ]
     348                 :             :         }
     349   [ +  -  +  - ]:        7437 :         TxInErrorToJSON(mtx.vin.at(err_pair.first), vErrors, err_pair.second.original);
     350                 :             :     }
     351                 :             : 
     352   [ +  -  +  -  :         206 :     result.pushKV("hex", EncodeHexTx(CTransaction(mtx)));
          +  -  +  -  +  
                      - ]
     353   [ +  -  +  -  :         206 :     result.pushKV("complete", complete);
                   +  - ]
     354   [ -  +  +  + ]:         103 :     if (!vErrors.empty()) {
     355   [ +  -  -  + ]:         174 :         if (result.exists("errors")) {
     356   [ #  #  #  #  :           0 :             vErrors.push_backV(result["errors"].getValues());
             #  #  #  # ]
     357                 :             :         }
     358   [ +  -  +  - ]:         174 :         result.pushKV("errors", std::move(vErrors));
     359                 :             :     }
     360                 :         103 : }
     361                 :             : 
     362                 :        1753 : std::vector<RPCResult> TxDoc(const TxDocOptions& opts)
     363                 :             : {
     364   [ +  +  -  + ]:        1753 :     CHECK_NONFATAL(!opts.fee_doc || opts.fee);
     365   [ +  +  -  + ]:        1753 :     CHECK_NONFATAL(!opts.prevout_doc || opts.prevout);
     366   [ +  +  +  - ]:        1849 :     CHECK_NONFATAL(!opts.vin_item_doc || opts.vin_inner_elision);
     367   [ +  +  +  - ]:        2658 :     CHECK_NONFATAL(opts.elision_mode != ElisionMode::WithSummary || opts.elision_summary.has_value());
     368                 :             : 
     369                 :        1753 :     const std::string fee_doc{opts.fee_doc.value_or(
     370   [ +  -  +  - ]:        5259 :         "transaction fee in " + CURRENCY_UNIT + ", omitted if block undo data is not available")};
     371                 :        1753 :     const std::string prevout_doc{opts.prevout_doc.value_or(
     372         [ +  - ]:        1753 :         "The previous output, omitted if block undo data is not available")};
     373         [ +  - ]:        1753 :     const std::string vin_item_doc{opts.vin_item_doc.value_or("utxo being spent")};
     374                 :             : 
     375                 :        1753 :     auto vin_inner = std::vector<RPCResult>{
     376   [ +  -  +  - ]:        3506 :         {RPCResult::Type::STR_HEX, "coinbase", /*optional=*/true, "The coinbase value (only if coinbase transaction)"},
     377   [ +  -  +  - ]:        3506 :         {RPCResult::Type::STR_HEX, "txid", /*optional=*/true, "The transaction id (if not coinbase transaction)"},
     378   [ +  -  +  - ]:        3506 :         {RPCResult::Type::NUM, "vout", /*optional=*/true, "The output number (if not coinbase transaction)"},
     379   [ +  -  +  - ]:        3506 :         {RPCResult::Type::OBJ, "scriptSig", /*optional=*/true, "The script (if not coinbase transaction)",
     380                 :             :         {
     381   [ +  -  +  - ]:        3506 :             {RPCResult::Type::STR, "asm", "Disassembly of the signature script"},
     382   [ +  -  +  - ]:        3506 :             {RPCResult::Type::STR_HEX, "hex", "The raw signature script bytes, hex-encoded"},
     383                 :             :         }},
     384   [ +  -  +  - ]:        3506 :         {RPCResult::Type::ARR, "txinwitness", /*optional=*/true, "",
     385                 :             :         {
     386   [ +  -  +  - ]:        3506 :             {RPCResult::Type::STR_HEX, "hex", "hex-encoded witness data (if any)"},
     387                 :             :         }},
     388                 :       33307 :     };
           [ +  -  +  -  
          -  +  +  +  +  
          +  +  +  -  -  
             -  -  -  - ]
     389         [ +  + ]:        1753 :     if (opts.prevout) {
     390                 :         376 :         vin_inner.emplace_back(
     391         [ +  - ]:         188 :             RPCResult::Type::OBJ, "prevout", opts.prevout_optional, prevout_doc,
     392                 :         188 :             std::vector<RPCResult>{
     393   [ +  -  +  - ]:         376 :                 {RPCResult::Type::BOOL, "generated", "Coinbase or not"},
     394   [ +  -  +  - ]:         376 :                 {RPCResult::Type::NUM, "height", "The height of the prevout"},
     395   [ +  -  +  - ]:         376 :                 {RPCResult::Type::STR_AMOUNT, "value", "The value in " + CURRENCY_UNIT},
     396   [ +  -  +  -  :         376 :                 {RPCResult::Type::OBJ, "scriptPubKey", "", ScriptPubKeyDoc()},
                   +  - ]
     397   [ +  -  +  +  :        1692 :             }
                   -  - ]
     398                 :             :         );
     399                 :             :     }
     400         [ +  - ]:        1753 :     vin_inner.emplace_back(RPCResult::Type::NUM, "sequence", "The script sequence number");
     401                 :             : 
     402         [ +  + ]:        1753 :     if (opts.vin_inner_elision) {
     403   [ -  +  +  - ]:         564 :         vin_inner = ElideGroup(std::move(vin_inner), *opts.vin_inner_elision);
     404         [ +  - ]:         188 :         if (opts.prevout) {
     405                 :             :             // prevout remains visible even when other fields are elided
     406                 :         188 :             std::vector<RPCResult> new_vin;
     407   [ -  +  +  - ]:         188 :             new_vin.reserve(vin_inner.size());
     408         [ +  + ]:        1504 :             for (const auto& r : vin_inner) {
     409         [ +  + ]:        1316 :                 if (r.m_key_name == "prevout") {
     410         [ +  - ]:         188 :                     RPCResultOptions unopts = r.m_opts;
     411                 :         188 :                     unopts.print_elision = HelpElisionNone{};
     412         [ +  - ]:         188 :                     new_vin.emplace_back(r, std::move(unopts));
     413                 :         188 :                 } else {
     414         [ +  - ]:        1128 :                     new_vin.push_back(r);
     415                 :             :                 }
     416                 :             :             }
     417                 :         188 :             vin_inner = std::move(new_vin);
     418                 :         188 :         }
     419                 :             :     }
     420                 :             : 
     421                 :        1753 :     auto fields = std::vector<RPCResult>{
     422   [ +  -  -  + ]:        1753 :         {RPCResult::Type::STR_HEX, "txid", opts.txid_field_doc},
     423   [ +  -  +  - ]:        3506 :         {RPCResult::Type::STR_HEX, "hash", "The transaction hash (differs from txid for witness transactions)"},
     424   [ +  -  +  - ]:        3506 :         {RPCResult::Type::NUM, "size", "The serialized transaction size"},
     425   [ +  -  +  - ]:        3506 :         {RPCResult::Type::NUM, "vsize", "The virtual transaction size (differs from size for witness transactions)"},
     426   [ +  -  +  - ]:        3506 :         {RPCResult::Type::NUM, "weight", "The transaction's weight (between vsize*4-3 and vsize*4)"},
     427   [ +  -  +  - ]:        3506 :         {RPCResult::Type::NUM, "version", "The version"},
     428   [ +  -  +  - ]:        3506 :         {RPCResult::Type::NUM_TIME, "locktime", "The lock time"},
     429   [ +  -  +  - ]:        3506 :         {RPCResult::Type::ARR, "vin", "",
     430                 :             :         {
     431   [ +  -  +  +  :        3694 :             {RPCResult::Type::OBJ, "", opts.vin_inner_elision ? vin_item_doc : "", std::move(vin_inner)},
             +  -  +  - ]
     432                 :             :         }},
     433   [ +  -  +  - ]:        3506 :         {RPCResult::Type::ARR, "vout", "",
     434                 :             :         {
     435                 :       15777 :             {RPCResult::Type::OBJ, "", "", Cat(
           [ +  -  +  -  
          +  -  +  -  +  
                +  -  - ]
     436                 :             :                 {
     437   [ +  -  +  - ]:        3506 :                     {RPCResult::Type::STR_AMOUNT, "value", "The value in " + CURRENCY_UNIT},
     438   [ +  -  +  - ]:        3506 :                     {RPCResult::Type::NUM, "n", "index"},
     439   [ +  -  +  -  :        3506 :                     {RPCResult::Type::OBJ, "scriptPubKey", "", ScriptPubKeyDoc()},
                   +  - ]
     440                 :             :                 },
     441                 :        3506 :                 opts.wallet ?
           [ -  +  -  -  
          -  +  -  -  -  
                -  -  - ]
     442                 :        1753 :                     std::vector<RPCResult>{{RPCResult::Type::BOOL, "ischange", /*optional=*/true, "Output script is change (only present if true)"}} :
           [ -  -  -  -  
          -  +  -  +  -  
          +  -  -  -  -  
                   -  - ]
     443                 :             :                     std::vector<RPCResult>{}
     444                 :             :             )},
     445                 :             :         }},
     446                 :       42072 :     };
           [ +  -  +  -  
          -  +  +  +  +  
          +  +  +  -  -  
             -  -  -  - ]
     447                 :             : 
     448   [ +  +  +  - ]:        1753 :     if (opts.fee) fields.emplace_back(RPCResult::Type::NUM, "fee", /*optional=*/true, fee_doc);
     449   [ +  +  +  - ]:        1753 :     if (opts.hex) fields.emplace_back(RPCResult::Type::STR_HEX, "hex", "The hex-encoded transaction data");
     450                 :             : 
     451         [ +  + ]:        1753 :     if (opts.elision_mode != ElisionMode::None) {
     452                 :        1093 :         const bool silent = opts.elision_mode == ElisionMode::Silent;
     453                 :        1093 :         std::vector<RPCResult> new_fields;
     454   [ -  +  +  - ]:        1093 :         new_fields.reserve(fields.size());
     455                 :        1093 :         bool first = true;
     456         [ +  + ]:       11314 :         for (const auto& f : fields) {
     457   [ +  +  +  + ]:       10221 :             if (!silent && f.m_key_name == "fee") {
     458         [ +  - ]:          96 :                 new_fields.push_back(f);
     459                 :          96 :                 continue;
     460                 :             :             }
     461   [ +  +  +  + ]:       10125 :             if (f.m_key_name == "vin" && opts.vin_inner_elision) {
     462         [ +  - ]:         188 :                 new_fields.push_back(f);
     463                 :         188 :                 continue;
     464                 :             :             }
     465         [ +  + ]:        9937 :             if (!silent && first) {
     466         [ +  - ]:         905 :                 RPCResultOptions eopts = f.m_opts;
     467         [ +  - ]:         905 :                 eopts.print_elision = opts.elision_summary.value_or("");
     468         [ +  - ]:         905 :                 new_fields.emplace_back(f, std::move(eopts));
     469                 :         905 :                 first = false;
     470                 :         905 :             } else {
     471         [ +  - ]:        9032 :                 RPCResultOptions eopts = f.m_opts;
     472                 :        9032 :                 eopts.print_elision = HelpElisionSkip{};
     473         [ +  - ]:        9032 :                 new_fields.emplace_back(f, std::move(eopts));
     474                 :        9032 :             }
     475                 :             :         }
     476                 :        1093 :         fields = std::move(new_fields);
     477                 :        1093 :     }
     478                 :             : 
     479                 :        3506 :     return fields;
     480                 :       82142 : }
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  -  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  -  -  -  
          -  -  -  -  -  
             -  -  -  - ]
        

Generated by: LCOV version 2.5.0-full