LCOV - code coverage report
Current view: top level - src/rpc - rawtransaction_util.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 81.4 % 295 240
Test Date: 2026-10-11 07:59:32 Functions: 100.0 % 10 10
Branches: 41.6 % 838 349

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

Generated by: LCOV version 2.0-1