LCOV - code coverage report
Current view: top level - src/rpc - util.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 73 807 9.0 %
Date: 2023-09-26 12:08:55 Functions: 12 82 14.6 %

          Line data    Source code
       1             : // Copyright (c) 2017-2022 The Bitcoin Core developers
       2             : // Distributed under the MIT software license, see the accompanying
       3             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4             : 
       5             : #include <clientversion.h>
       6             : #include <core_io.h>
       7             : #include <common/args.h>
       8             : #include <consensus/amount.h>
       9             : #include <script/interpreter.h>
      10             : #include <key_io.h>
      11             : #include <outputtype.h>
      12             : #include <rpc/util.h>
      13             : #include <script/descriptor.h>
      14             : #include <script/signingprovider.h>
      15             : #include <script/solver.h>
      16             : #include <tinyformat.h>
      17           2 : #include <util/check.h>
      18           2 : #include <util/result.h>
      19             : #include <util/strencodings.h>
      20             : #include <util/string.h>
      21             : #include <util/translation.h>
      22             : 
      23             : #include <tuple>
      24             : 
      25           2 : const std::string UNIX_EPOCH_TIME = "UNIX epoch time";
      26           2 : const std::string EXAMPLE_ADDRESS[2] = {"bc1q09vm5lfy0j5reeulh4x5752q25uqqvz34hufdl", "bc1q02ad21edsxd23d32dfgqqsz4vv4nmtfzuklhy3"};
      27           2 : 
      28          10 : std::string GetAllOutputTypes()
      29             : {
      30          10 :     std::vector<std::string> ret;
      31             :     using U = std::underlying_type<TxoutType>::type;
      32         110 :     for (U i = (U)TxoutType::NONSTANDARD; i <= (U)TxoutType::WITNESS_UNKNOWN; ++i) {
      33         100 :         ret.emplace_back(GetTxnOutputType(static_cast<TxoutType>(i)));
      34         100 :     }
      35          10 :     return Join(ret, ", ");
      36          10 : }
      37             : 
      38           0 : void RPCTypeCheckObj(const UniValue& o,
      39             :     const std::map<std::string, UniValueType>& typesExpected,
      40             :     bool fAllowNull,
      41             :     bool fStrict)
      42             : {
      43           0 :     for (const auto& t : typesExpected) {
      44           0 :         const UniValue& v = o.find_value(t.first);
      45           0 :         if (!fAllowNull && v.isNull())
      46           0 :             throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first));
      47             : 
      48           0 :         if (!(t.second.typeAny || v.type() == t.second.type || (fAllowNull && v.isNull())))
      49           0 :             throw JSONRPCError(RPC_TYPE_ERROR, strprintf("JSON value of type %s for field %s is not of expected type %s", uvTypeName(v.type()),  t.first, uvTypeName(t.second.type)));
      50             :     }
      51             : 
      52           0 :     if (fStrict)
      53             :     {
      54           0 :         for (const std::string& k : o.getKeys())
      55             :         {
      56           0 :             if (typesExpected.count(k) == 0)
      57             :             {
      58           0 :                 std::string err = strprintf("Unexpected key %s", k);
      59           0 :                 throw JSONRPCError(RPC_TYPE_ERROR, err);
      60           0 :             }
      61             :         }
      62           0 :     }
      63           0 : }
      64             : 
      65           0 : CAmount AmountFromValue(const UniValue& value, int decimals)
      66             : {
      67           0 :     if (!value.isNum() && !value.isStr())
      68           0 :         throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string");
      69             :     CAmount amount;
      70           0 :     if (!ParseFixedPoint(value.getValStr(), decimals, &amount))
      71           0 :         throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
      72           0 :     if (!MoneyRange(amount))
      73           0 :         throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range");
      74           2 :     return amount;
      75           0 : }
      76             : 
      77           0 : uint256 ParseHashV(const UniValue& v, std::string strName)
      78             : {
      79           0 :     const std::string& strHex(v.get_str());
      80           0 :     if (64 != strHex.length())
      81           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be of length %d (not %d, for '%s')", strName, 64, strHex.length(), strHex));
      82           0 :     if (!IsHex(strHex)) // Note: IsHex("") is false
      83           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')");
      84           0 :     return uint256S(strHex);
      85           0 : }
      86           0 : uint256 ParseHashO(const UniValue& o, std::string strKey)
      87             : {
      88           0 :     return ParseHashV(o.find_value(strKey), strKey);
      89           0 : }
      90           0 : std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName)
      91             : {
      92           0 :     std::string strHex;
      93           0 :     if (v.isStr())
      94           0 :         strHex = v.get_str();
      95           0 :     if (!IsHex(strHex))
      96           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')");
      97           0 :     return ParseHex(strHex);
      98           0 : }
      99           0 : std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey)
     100             : {
     101           0 :     return ParseHexV(o.find_value(strKey), strKey);
     102           0 : }
     103             : 
     104             : namespace {
     105             : 
     106             : /**
     107             :  * Quote an argument for shell.
     108             :  *
     109             :  * @note This is intended for help, not for security-sensitive purposes.
     110             :  */
     111           0 : std::string ShellQuote(const std::string& s)
     112             : {
     113           0 :     std::string result;
     114           0 :     result.reserve(s.size() * 2);
     115           0 :     for (const char ch: s) {
     116           0 :         if (ch == '\'') {
     117           0 :             result += "'\''";
     118           0 :         } else {
     119           0 :             result += ch;
     120             :         }
     121             :     }
     122           0 :     return "'" + result + "'";
     123           0 : }
     124             : 
     125             : /**
     126             :  * Shell-quotes the argument if it needs quoting, else returns it literally, to save typing.
     127             :  *
     128             :  * @note This is intended for help, not for security-sensitive purposes.
     129             :  */
     130           0 : std::string ShellQuoteIfNeeded(const std::string& s)
     131             : {
     132           0 :     for (const char ch: s) {
     133           0 :         if (ch == ' ' || ch == '\'' || ch == '"') {
     134           0 :             return ShellQuote(s);
     135             :         }
     136             :     }
     137             : 
     138           0 :     return s;
     139           0 : }
     140             : 
     141             : }
     142             : 
     143         252 : std::string HelpExampleCli(const std::string& methodname, const std::string& args)
     144             : {
     145         252 :     return "> bitcoin-cli " + methodname + " " + args + "\n";
     146           0 : }
     147             : 
     148           0 : std::string HelpExampleCliNamed(const std::string& methodname, const RPCArgList& args)
     149             : {
     150           0 :     std::string result = "> bitcoin-cli -named " + methodname;
     151           0 :     for (const auto& argpair: args) {
     152           0 :         const auto& value = argpair.second.isStr()
     153           0 :                 ? argpair.second.get_str()
     154           0 :                 : argpair.second.write();
     155           0 :         result += " " + argpair.first + "=" + ShellQuoteIfNeeded(value);
     156           0 :     }
     157           0 :     result += "\n";
     158           0 :     return result;
     159           0 : }
     160             : 
     161         182 : std::string HelpExampleRpc(const std::string& methodname, const std::string& args)
     162             : {
     163         182 :     return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", "
     164         182 :         "\"method\": \"" + methodname + "\", \"params\": [" + args + "]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
     165           0 : }
     166             : 
     167           0 : std::string HelpExampleRpcNamed(const std::string& methodname, const RPCArgList& args)
     168             : {
     169           0 :     UniValue params(UniValue::VOBJ);
     170           0 :     for (const auto& param: args) {
     171           0 :         params.pushKV(param.first, param.second);
     172             :     }
     173             : 
     174           0 :     return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", "
     175           0 :            "\"method\": \"" + methodname + "\", \"params\": " + params.write() + "}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
     176           0 : }
     177             : 
     178             : // Converts a hex string to a public key if possible
     179           0 : CPubKey HexToPubKey(const std::string& hex_in)
     180             : {
     181           0 :     if (!IsHex(hex_in)) {
     182           0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in);
     183             :     }
     184           0 :     CPubKey vchPubKey(ParseHex(hex_in));
     185           0 :     if (!vchPubKey.IsFullyValid()) {
     186           0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in);
     187             :     }
     188           0 :     return vchPubKey;
     189           0 : }
     190             : 
     191             : // Retrieves a public key for an address from the given FillableSigningProvider
     192           0 : CPubKey AddrToPubKey(const FillableSigningProvider& keystore, const std::string& addr_in)
     193             : {
     194           0 :     CTxDestination dest = DecodeDestination(addr_in);
     195           0 :     if (!IsValidDestination(dest)) {
     196           0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address: " + addr_in);
     197             :     }
     198           0 :     CKeyID key = GetKeyForDestination(keystore, dest);
     199           0 :     if (key.IsNull()) {
     200           0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("'%s' does not refer to a key", addr_in));
     201             :     }
     202           0 :     CPubKey vchPubKey;
     203           0 :     if (!keystore.GetPubKey(key, vchPubKey)) {
     204           0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("no full public key for address %s", addr_in));
     205             :     }
     206           0 :     if (!vchPubKey.IsFullyValid()) {
     207           0 :        throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet contains an invalid public key");
     208             :     }
     209             :     return vchPubKey;
     210           0 : }
     211             : 
     212             : // Creates a multisig address from a given list of public keys, number of signatures required, and the address type
     213           0 : CTxDestination AddAndGetMultisigDestination(const int required, const std::vector<CPubKey>& pubkeys, OutputType type, FillableSigningProvider& keystore, CScript& script_out)
     214             : {
     215             :     // Gather public keys
     216           0 :     if (required < 1) {
     217           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "a multisignature address must require at least one key to redeem");
     218             :     }
     219           0 :     if ((int)pubkeys.size() < required) {
     220           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("not enough keys supplied (got %u keys, but need at least %d to redeem)", pubkeys.size(), required));
     221             :     }
     222           0 :     if (pubkeys.size() > MAX_PUBKEYS_PER_MULTISIG) {
     223           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Number of keys involved in the multisignature address creation > %d\nReduce the number", MAX_PUBKEYS_PER_MULTISIG));
     224             :     }
     225             : 
     226           0 :     script_out = GetScriptForMultisig(required, pubkeys);
     227             : 
     228             :     // Check if any keys are uncompressed. If so, the type is legacy
     229           0 :     for (const CPubKey& pk : pubkeys) {
     230           0 :         if (!pk.IsCompressed()) {
     231           0 :             type = OutputType::LEGACY;
     232           0 :             break;
     233             :         }
     234             :     }
     235             : 
     236           0 :     if (type == OutputType::LEGACY && script_out.size() > MAX_SCRIPT_ELEMENT_SIZE) {
     237           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, (strprintf("redeemScript exceeds size limit: %d > %d", script_out.size(), MAX_SCRIPT_ELEMENT_SIZE)));
     238             :     }
     239             : 
     240             :     // Make the address
     241           0 :     CTxDestination dest = AddAndGetDestinationForScript(keystore, script_out, type);
     242             : 
     243           0 :     return dest;
     244           0 : }
     245             : 
     246             : class DescribeAddressVisitor
     247             : {
     248             : public:
     249             :     explicit DescribeAddressVisitor() = default;
     250             : 
     251           0 :     UniValue operator()(const CNoDestination& dest) const
     252             :     {
     253           0 :         return UniValue(UniValue::VOBJ);
     254           0 :     }
     255             : 
     256           0 :     UniValue operator()(const PubKeyDestination& dest) const
     257             :     {
     258           0 :         return UniValue(UniValue::VOBJ);
     259           0 :     }
     260             : 
     261           0 :     UniValue operator()(const PKHash& keyID) const
     262             :     {
     263           0 :         UniValue obj(UniValue::VOBJ);
     264           0 :         obj.pushKV("isscript", false);
     265           0 :         obj.pushKV("iswitness", false);
     266           0 :         return obj;
     267           0 :     }
     268             : 
     269           0 :     UniValue operator()(const ScriptHash& scriptID) const
     270             :     {
     271           0 :         UniValue obj(UniValue::VOBJ);
     272           0 :         obj.pushKV("isscript", true);
     273           0 :         obj.pushKV("iswitness", false);
     274           0 :         return obj;
     275           0 :     }
     276             : 
     277           0 :     UniValue operator()(const WitnessV0KeyHash& id) const
     278             :     {
     279           0 :         UniValue obj(UniValue::VOBJ);
     280           0 :         obj.pushKV("isscript", false);
     281           0 :         obj.pushKV("iswitness", true);
     282           0 :         obj.pushKV("witness_version", 0);
     283           0 :         obj.pushKV("witness_program", HexStr(id));
     284           0 :         return obj;
     285           0 :     }
     286             : 
     287           0 :     UniValue operator()(const WitnessV0ScriptHash& id) const
     288             :     {
     289           0 :         UniValue obj(UniValue::VOBJ);
     290           0 :         obj.pushKV("isscript", true);
     291           0 :         obj.pushKV("iswitness", true);
     292           0 :         obj.pushKV("witness_version", 0);
     293           0 :         obj.pushKV("witness_program", HexStr(id));
     294           0 :         return obj;
     295           0 :     }
     296             : 
     297           0 :     UniValue operator()(const WitnessV1Taproot& tap) const
     298             :     {
     299           0 :         UniValue obj(UniValue::VOBJ);
     300           0 :         obj.pushKV("isscript", true);
     301           0 :         obj.pushKV("iswitness", true);
     302           0 :         obj.pushKV("witness_version", 1);
     303           0 :         obj.pushKV("witness_program", HexStr(tap));
     304           0 :         return obj;
     305           0 :     }
     306             : 
     307           0 :     UniValue operator()(const WitnessUnknown& id) const
     308             :     {
     309           0 :         UniValue obj(UniValue::VOBJ);
     310           0 :         obj.pushKV("iswitness", true);
     311           0 :         obj.pushKV("witness_version", id.GetWitnessVersion());
     312           0 :         obj.pushKV("witness_program", HexStr(id.GetWitnessProgram()));
     313           0 :         return obj;
     314           0 :     }
     315             : };
     316             : 
     317           0 : UniValue DescribeAddress(const CTxDestination& dest)
     318             : {
     319           0 :     return std::visit(DescribeAddressVisitor(), dest);
     320             : }
     321             : 
     322             : /**
     323             :  * Returns a sighash value corresponding to the passed in argument.
     324             :  *
     325             :  * @pre The sighash argument should be string or null.
     326             : */
     327           0 : int ParseSighashString(const UniValue& sighash)
     328             : {
     329           0 :     if (sighash.isNull()) {
     330           0 :         return SIGHASH_DEFAULT;
     331             :     }
     332           0 :     const auto result{SighashFromStr(sighash.get_str())};
     333           0 :     if (!result) {
     334           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, util::ErrorString(result).original);
     335             :     }
     336           0 :     return result.value();
     337           0 : }
     338             : 
     339           0 : unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target)
     340             : {
     341           0 :     const int target{value.getInt<int>()};
     342           0 :     const unsigned int unsigned_target{static_cast<unsigned int>(target)};
     343           0 :     if (target < 1 || unsigned_target > max_target) {
     344           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid conf_target, must be between %u and %u", 1, max_target));
     345             :     }
     346           0 :     return unsigned_target;
     347           0 : }
     348             : 
     349           0 : RPCErrorCode RPCErrorFromTransactionError(TransactionError terr)
     350             : {
     351           0 :     switch (terr) {
     352             :         case TransactionError::MEMPOOL_REJECTED:
     353           0 :             return RPC_TRANSACTION_REJECTED;
     354             :         case TransactionError::ALREADY_IN_CHAIN:
     355           0 :             return RPC_TRANSACTION_ALREADY_IN_CHAIN;
     356             :         case TransactionError::P2P_DISABLED:
     357           0 :             return RPC_CLIENT_P2P_DISABLED;
     358             :         case TransactionError::INVALID_PSBT:
     359             :         case TransactionError::PSBT_MISMATCH:
     360           0 :             return RPC_INVALID_PARAMETER;
     361             :         case TransactionError::SIGHASH_MISMATCH:
     362           0 :             return RPC_DESERIALIZATION_ERROR;
     363           0 :         default: break;
     364             :     }
     365           0 :     return RPC_TRANSACTION_ERROR;
     366           0 : }
     367             : 
     368           0 : UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_string)
     369             : {
     370           0 :     if (err_string.length() > 0) {
     371           0 :         return JSONRPCError(RPCErrorFromTransactionError(terr), err_string);
     372             :     } else {
     373           0 :         return JSONRPCError(RPCErrorFromTransactionError(terr), TransactionErrorString(terr).original);
     374             :     }
     375           0 : }
     376             : 
     377             : /**
     378             :  * A pair of strings that can be aligned (through padding) with other Sections
     379             :  * later on
     380             :  */
     381           0 : struct Section {
     382           0 :     Section(const std::string& left, const std::string& right)
     383           0 :         : m_left{left}, m_right{right} {}
     384             :     std::string m_left;
     385             :     const std::string m_right;
     386             : };
     387             : 
     388             : /**
     389             :  * Keeps track of RPCArgs by transforming them into sections for the purpose
     390             :  * of serializing everything to a single string
     391             :  */
     392           0 : struct Sections {
     393             :     std::vector<Section> m_sections;
     394           0 :     size_t m_max_pad{0};
     395             : 
     396           0 :     void PushSection(const Section& s)
     397             :     {
     398           0 :         m_max_pad = std::max(m_max_pad, s.m_left.size());
     399           0 :         m_sections.push_back(s);
     400           0 :     }
     401             : 
     402             :     /**
     403             :      * Recursive helper to translate an RPCArg into sections
     404             :      */
     405           0 :     void Push(const RPCArg& arg, const size_t current_indent = 5, const OuterType outer_type = OuterType::NONE)
     406             :     {
     407           0 :         const auto indent = std::string(current_indent, ' ');
     408           0 :         const auto indent_next = std::string(current_indent + 2, ' ');
     409           0 :         const bool push_name{outer_type == OuterType::OBJ}; // Dictionary keys must have a name
     410           0 :         const bool is_top_level_arg{outer_type == OuterType::NONE}; // True on the first recursion
     411             : 
     412           0 :         switch (arg.m_type) {
     413             :         case RPCArg::Type::STR_HEX:
     414             :         case RPCArg::Type::STR:
     415             :         case RPCArg::Type::NUM:
     416             :         case RPCArg::Type::AMOUNT:
     417             :         case RPCArg::Type::RANGE:
     418             :         case RPCArg::Type::BOOL:
     419             :         case RPCArg::Type::OBJ_NAMED_PARAMS: {
     420           0 :             if (is_top_level_arg) return; // Nothing more to do for non-recursive types on first recursion
     421           0 :             auto left = indent;
     422           0 :             if (arg.m_opts.type_str.size() != 0 && push_name) {
     423           0 :                 left += "\"" + arg.GetName() + "\": " + arg.m_opts.type_str.at(0);
     424           0 :             } else {
     425           0 :                 left += push_name ? arg.ToStringObj(/*oneline=*/false) : arg.ToString(/*oneline=*/false);
     426             :             }
     427           0 :             left += ",";
     428           0 :             PushSection({left, arg.ToDescriptionString(/*is_named_arg=*/push_name)});
     429             :             break;
     430           0 :         }
     431             :         case RPCArg::Type::OBJ:
     432             :         case RPCArg::Type::OBJ_USER_KEYS: {
     433           0 :             const auto right = is_top_level_arg ? "" : arg.ToDescriptionString(/*is_named_arg=*/push_name);
     434           0 :             PushSection({indent + (push_name ? "\"" + arg.GetName() + "\": " : "") + "{", right});
     435           0 :             for (const auto& arg_inner : arg.m_inner) {
     436           0 :                 Push(arg_inner, current_indent + 2, OuterType::OBJ);
     437             :             }
     438           0 :             if (arg.m_type != RPCArg::Type::OBJ) {
     439           0 :                 PushSection({indent_next + "...", ""});
     440           0 :             }
     441           0 :             PushSection({indent + "}" + (is_top_level_arg ? "" : ","), ""});
     442             :             break;
     443           0 :         }
     444             :         case RPCArg::Type::ARR: {
     445           0 :             auto left = indent;
     446           0 :             left += push_name ? "\"" + arg.GetName() + "\": " : "";
     447           0 :             left += "[";
     448           0 :             const auto right = is_top_level_arg ? "" : arg.ToDescriptionString(/*is_named_arg=*/push_name);
     449           0 :             PushSection({left, right});
     450           0 :             for (const auto& arg_inner : arg.m_inner) {
     451           0 :                 Push(arg_inner, current_indent + 2, OuterType::ARR);
     452             :             }
     453           0 :             PushSection({indent_next + "...", ""});
     454           0 :             PushSection({indent + "]" + (is_top_level_arg ? "" : ","), ""});
     455             :             break;
     456           0 :         }
     457         214 :         } // no default case, so the compiler can warn about missing cases
     458           0 :     }
     459             : 
     460             :     /**
     461             :      * Concatenate all sections with proper padding
     462             :      */
     463           0 :     std::string ToString() const
     464             :     {
     465           0 :         std::string ret;
     466           0 :         const size_t pad = m_max_pad + 4;
     467           0 :         for (const auto& s : m_sections) {
     468             :             // The left part of a section is assumed to be a single line, usually it is the name of the JSON struct or a
     469             :             // brace like {, }, [, or ]
     470           0 :             CHECK_NONFATAL(s.m_left.find('\n') == std::string::npos);
     471           0 :             if (s.m_right.empty()) {
     472           0 :                 ret += s.m_left;
     473           0 :                 ret += "\n";
     474           0 :                 continue;
     475             :             }
     476             : 
     477           0 :             std::string left = s.m_left;
     478           0 :             left.resize(pad, ' ');
     479           0 :             ret += left;
     480             : 
     481             :             // Properly pad after newlines
     482           0 :             std::string right;
     483           0 :             size_t begin = 0;
     484           0 :             size_t new_line_pos = s.m_right.find_first_of('\n');
     485           0 :             while (true) {
     486           0 :                 right += s.m_right.substr(begin, new_line_pos - begin);
     487           0 :                 if (new_line_pos == std::string::npos) {
     488           0 :                     break; //No new line
     489             :                 }
     490           0 :                 right += "\n" + std::string(pad, ' ');
     491           0 :                 begin = s.m_right.find_first_not_of(' ', new_line_pos + 1);
     492           0 :                 if (begin == std::string::npos) {
     493           0 :                     break; // Empty line
     494             :                 }
     495           0 :                 new_line_pos = s.m_right.find_first_of('\n', begin + 1);
     496             :             }
     497           0 :             ret += right;
     498           0 :             ret += "\n";
     499           0 :         }
     500           0 :         return ret;
     501           0 :     }
     502             : };
     503             : 
     504           0 : RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples)
     505           0 :     : RPCHelpMan{std::move(name), std::move(description), std::move(args), std::move(results), std::move(examples), nullptr} {}
     506             : 
     507         214 : RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun)
     508         214 :     : m_name{std::move(name)},
     509         214 :       m_fun{std::move(fun)},
     510         214 :       m_description{std::move(description)},
     511         214 :       m_args{std::move(args)},
     512         214 :       m_results{std::move(results)},
     513         214 :       m_examples{std::move(examples)}
     514             : {
     515             :     // Map of parameter names and types just used to check whether the names are
     516             :     // unique. Parameter names always need to be unique, with the exception that
     517             :     // there can be pairs of POSITIONAL and NAMED parameters with the same name.
     518             :     enum ParamType { POSITIONAL = 1, NAMED = 2, NAMED_ONLY = 4 };
     519         214 :     std::map<std::string, int> param_names;
     520             : 
     521         572 :     for (const auto& arg : m_args) {
     522         358 :         std::vector<std::string> names = SplitString(arg.m_names, '|');
     523             :         // Should have unique named arguments
     524         720 :         for (const std::string& name : names) {
     525         362 :             auto& param_type = param_names[name];
     526         362 :             CHECK_NONFATAL(!(param_type & POSITIONAL));
     527         362 :             CHECK_NONFATAL(!(param_type & NAMED_ONLY));
     528         362 :             param_type |= POSITIONAL;
     529             :         }
     530         358 :         if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
     531           8 :             for (const auto& inner : arg.m_inner) {
     532           6 :                 std::vector<std::string> inner_names = SplitString(inner.m_names, '|');
     533          12 :                 for (const std::string& inner_name : inner_names) {
     534           6 :                     auto& param_type = param_names[inner_name];
     535           0 :                     CHECK_NONFATAL(!(param_type & POSITIONAL) || inner.m_opts.also_positional);
     536           6 :                     CHECK_NONFATAL(!(param_type & NAMED));
     537           6 :                     CHECK_NONFATAL(!(param_type & NAMED_ONLY));
     538           6 :                     param_type |= inner.m_opts.also_positional ? NAMED : NAMED_ONLY;
     539             :                 }
     540          18 :             }
     541           2 :         }
     542             :         // Default value type should match argument type only when defined
     543         358 :         if (arg.m_fallback.index() == 2) {
     544          86 :             const RPCArg::Type type = arg.m_type;
     545          86 :             switch (std::get<RPCArg::Default>(arg.m_fallback).getType()) {
     546             :             case UniValue::VOBJ:
     547           0 :                 CHECK_NONFATAL(type == RPCArg::Type::OBJ);
     548           0 :                 break;
     549             :             case UniValue::VARR:
     550           0 :                 CHECK_NONFATAL(type == RPCArg::Type::ARR);
     551           0 :                 break;
     552             :             case UniValue::VSTR:
     553          24 :                 CHECK_NONFATAL(type == RPCArg::Type::STR || type == RPCArg::Type::STR_HEX || type == RPCArg::Type::AMOUNT);
     554          24 :                 break;
     555             :             case UniValue::VNUM:
     556          30 :                 CHECK_NONFATAL(type == RPCArg::Type::NUM || type == RPCArg::Type::AMOUNT || type == RPCArg::Type::RANGE);
     557          30 :                 break;
     558             :             case UniValue::VBOOL:
     559          32 :                 CHECK_NONFATAL(type == RPCArg::Type::BOOL);
     560          32 :                 break;
     561             :             case UniValue::VNULL:
     562             :                 // Null values are accepted in all arguments
     563           0 :                 break;
     564             :             default:
     565           0 :                 NONFATAL_UNREACHABLE();
     566             :                 break;
     567             :             }
     568          86 :         }
     569         358 :     }
     570         226 : }
     571             : 
     572           0 : std::string RPCResults::ToDescriptionString() const
     573             : {
     574           0 :     std::string result;
     575           0 :     for (const auto& r : m_results) {
     576           0 :         if (r.m_type == RPCResult::Type::ANY) continue; // for testing only
     577           0 :         if (r.m_cond.empty()) {
     578           0 :             result += "\nResult:\n";
     579           0 :         } else {
     580           0 :             result += "\nResult (" + r.m_cond + "):\n";
     581             :         }
     582           0 :         Sections sections;
     583           0 :         r.ToSections(sections);
     584           0 :         result += sections.ToString();
     585           0 :     }
     586           0 :     return result;
     587           0 : }
     588             : 
     589           0 : std::string RPCExamples::ToDescriptionString() const
     590             : {
     591           0 :     return m_examples.empty() ? m_examples : "\nExamples:\n" + m_examples;
     592             : }
     593             : 
     594           0 : UniValue RPCHelpMan::HandleRequest(const JSONRPCRequest& request) const
     595             : {
     596           0 :     if (request.mode == JSONRPCRequest::GET_ARGS) {
     597           0 :         return GetArgMap();
     598             :     }
     599             :     /*
     600             :      * Check if the given request is valid according to this command or if
     601             :      * the user is asking for help information, and throw help when appropriate.
     602             :      */
     603           0 :     if (request.mode == JSONRPCRequest::GET_HELP || !IsValidNumArgs(request.params.size())) {
     604           0 :         throw std::runtime_error(ToString());
     605             :     }
     606           0 :     UniValue arg_mismatch{UniValue::VOBJ};
     607           0 :     for (size_t i{0}; i < m_args.size(); ++i) {
     608           0 :         const auto& arg{m_args.at(i)};
     609           0 :         UniValue match{arg.MatchesType(request.params[i])};
     610           0 :         if (!match.isTrue()) {
     611           0 :             arg_mismatch.pushKV(strprintf("Position %s (%s)", i + 1, arg.m_names), std::move(match));
     612           0 :         }
     613           0 :     }
     614           0 :     if (!arg_mismatch.empty()) {
     615           0 :         throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Wrong type passed:\n%s", arg_mismatch.write(4)));
     616             :     }
     617           0 :     CHECK_NONFATAL(m_req == nullptr);
     618           0 :     m_req = &request;
     619           0 :     UniValue ret = m_fun(*this, request);
     620           0 :     m_req = nullptr;
     621           0 :     if (gArgs.GetBoolArg("-rpcdoccheck", DEFAULT_RPC_DOC_CHECK)) {
     622           0 :         UniValue mismatch{UniValue::VARR};
     623           0 :         for (const auto& res : m_results.m_results) {
     624           0 :             UniValue match{res.MatchesType(ret)};
     625           0 :             if (match.isTrue()) {
     626           0 :                 mismatch.setNull();
     627           0 :                 break;
     628             :             }
     629           0 :             mismatch.push_back(match);
     630           0 :         }
     631           0 :         if (!mismatch.isNull()) {
     632           0 :             std::string explain{
     633           0 :                 mismatch.empty() ? "no possible results defined" :
     634           0 :                 mismatch.size() == 1 ? mismatch[0].write(4) :
     635           0 :                 mismatch.write(4)};
     636           0 :             throw std::runtime_error{
     637           0 :                 strprintf("Internal bug detected: RPC call \"%s\" returned incorrect type:\n%s\n%s %s\nPlease report this issue here: %s\n",
     638           0 :                           m_name, explain,
     639           0 :                           PACKAGE_NAME, FormatFullVersion(),
     640             :                           PACKAGE_BUGREPORT)};
     641           0 :         }
     642           0 :     }
     643           0 :     return ret;
     644           0 : }
     645             : 
     646             : using CheckFn = void(const RPCArg&);
     647           0 : static const UniValue* DetailMaybeArg(CheckFn* check, const std::vector<RPCArg>& params, const JSONRPCRequest* req, size_t i)
     648             : {
     649           0 :     CHECK_NONFATAL(i < params.size());
     650           0 :     const UniValue& arg{CHECK_NONFATAL(req)->params[i]};
     651           0 :     const RPCArg& param{params.at(i)};
     652           0 :     if (check) check(param);
     653             : 
     654           0 :     if (!arg.isNull()) return &arg;
     655           0 :     if (!std::holds_alternative<RPCArg::Default>(param.m_fallback)) return nullptr;
     656           0 :     return &std::get<RPCArg::Default>(param.m_fallback);
     657           0 : }
     658             : 
     659           0 : static void CheckRequiredOrDefault(const RPCArg& param)
     660             : {
     661             :     // Must use `Arg<Type>(i)` to get the argument or its default value.
     662           0 :     const bool required{
     663           0 :         std::holds_alternative<RPCArg::Optional>(param.m_fallback) && RPCArg::Optional::NO == std::get<RPCArg::Optional>(param.m_fallback),
     664             :     };
     665           0 :     CHECK_NONFATAL(required || std::holds_alternative<RPCArg::Default>(param.m_fallback));
     666           0 : }
     667             : 
     668             : #define TMPL_INST(check_param, ret_type, return_code)       \
     669             :     template <>                                             \
     670             :     ret_type RPCHelpMan::ArgValue<ret_type>(size_t i) const \
     671             :     {                                                       \
     672             :         const UniValue* maybe_arg{                          \
     673             :             DetailMaybeArg(check_param, m_args, m_req, i),  \
     674             :         };                                                  \
     675             :         return return_code                                  \
     676             :     }                                                       \
     677             :     void force_semicolon(ret_type)
     678             : 
     679             : // Optional arg (without default). Can also be called on required args, if needed.
     680           0 : TMPL_INST(nullptr, std::optional<double>, maybe_arg ? std::optional{maybe_arg->get_real()} : std::nullopt;);
     681           0 : TMPL_INST(nullptr, std::optional<bool>, maybe_arg ? std::optional{maybe_arg->get_bool()} : std::nullopt;);
     682           0 : TMPL_INST(nullptr, const std::string*, maybe_arg ? &maybe_arg->get_str() : nullptr;);
     683             : 
     684             : // Required arg or optional arg with default value.
     685           0 : TMPL_INST(CheckRequiredOrDefault, int, CHECK_NONFATAL(maybe_arg)->getInt<int>(););
     686           0 : TMPL_INST(CheckRequiredOrDefault, uint64_t, CHECK_NONFATAL(maybe_arg)->getInt<uint64_t>(););
     687           0 : TMPL_INST(CheckRequiredOrDefault, const std::string&, CHECK_NONFATAL(maybe_arg)->get_str(););
     688             : 
     689           0 : bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
     690             : {
     691           0 :     size_t num_required_args = 0;
     692           0 :     for (size_t n = m_args.size(); n > 0; --n) {
     693           0 :         if (!m_args.at(n - 1).IsOptional()) {
     694           0 :             num_required_args = n;
     695           0 :             break;
     696             :         }
     697           0 :     }
     698           0 :     return num_required_args <= num_args && num_args <= m_args.size();
     699             : }
     700             : 
     701         107 : std::vector<std::pair<std::string, bool>> RPCHelpMan::GetArgNames() const
     702             : {
     703         107 :     std::vector<std::pair<std::string, bool>> ret;
     704         107 :     ret.reserve(m_args.size());
     705         286 :     for (const auto& arg : m_args) {
     706         179 :         if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
     707           4 :             for (const auto& inner : arg.m_inner) {
     708           3 :                 ret.emplace_back(inner.m_names, /*named_only=*/true);
     709             :             }
     710           1 :         }
     711         179 :         ret.emplace_back(arg.m_names, /*named_only=*/false);
     712             :     }
     713         107 :     return ret;
     714         107 : }
     715             : 
     716           0 : std::string RPCHelpMan::ToString() const
     717             : {
     718           0 :     std::string ret;
     719             : 
     720             :     // Oneline summary
     721           0 :     ret += m_name;
     722           0 :     bool was_optional{false};
     723           0 :     for (const auto& arg : m_args) {
     724           0 :         if (arg.m_opts.hidden) break; // Any arg that follows is also hidden
     725           0 :         const bool optional = arg.IsOptional();
     726           0 :         ret += " ";
     727           0 :         if (optional) {
     728           0 :             if (!was_optional) ret += "( ";
     729           0 :             was_optional = true;
     730           0 :         } else {
     731           0 :             if (was_optional) ret += ") ";
     732           0 :             was_optional = false;
     733             :         }
     734           0 :         ret += arg.ToString(/*oneline=*/true);
     735             :     }
     736           0 :     if (was_optional) ret += " )";
     737             : 
     738             :     // Description
     739           0 :     ret += "\n\n" + TrimString(m_description) + "\n";
     740             : 
     741             :     // Arguments
     742           0 :     Sections sections;
     743           0 :     Sections named_only_sections;
     744           0 :     for (size_t i{0}; i < m_args.size(); ++i) {
     745           0 :         const auto& arg = m_args.at(i);
     746           0 :         if (arg.m_opts.hidden) break; // Any arg that follows is also hidden
     747             : 
     748             :         // Push named argument name and description
     749           0 :         sections.m_sections.emplace_back(::ToString(i + 1) + ". " + arg.GetFirstName(), arg.ToDescriptionString(/*is_named_arg=*/true));
     750           0 :         sections.m_max_pad = std::max(sections.m_max_pad, sections.m_sections.back().m_left.size());
     751             : 
     752             :         // Recursively push nested args
     753           0 :         sections.Push(arg);
     754             : 
     755             :         // Push named-only argument sections
     756           0 :         if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
     757           0 :             for (const auto& arg_inner : arg.m_inner) {
     758           0 :                 named_only_sections.PushSection({arg_inner.GetFirstName(), arg_inner.ToDescriptionString(/*is_named_arg=*/true)});
     759           0 :                 named_only_sections.Push(arg_inner);
     760             :             }
     761           0 :         }
     762           0 :     }
     763             : 
     764           0 :     if (!sections.m_sections.empty()) ret += "\nArguments:\n";
     765           0 :     ret += sections.ToString();
     766           0 :     if (!named_only_sections.m_sections.empty()) ret += "\nNamed Arguments:\n";
     767           0 :     ret += named_only_sections.ToString();
     768             : 
     769             :     // Result
     770           0 :     ret += m_results.ToDescriptionString();
     771             : 
     772             :     // Examples
     773           0 :     ret += m_examples.ToDescriptionString();
     774             : 
     775           0 :     return ret;
     776           0 : }
     777             : 
     778           0 : UniValue RPCHelpMan::GetArgMap() const
     779             : {
     780           0 :     UniValue arr{UniValue::VARR};
     781             : 
     782           0 :     auto push_back_arg_info = [&arr](const std::string& rpc_name, int pos, const std::string& arg_name, const RPCArg::Type& type) {
     783           0 :         UniValue map{UniValue::VARR};
     784           0 :         map.push_back(rpc_name);
     785           0 :         map.push_back(pos);
     786           0 :         map.push_back(arg_name);
     787           0 :         map.push_back(type == RPCArg::Type::STR ||
     788           0 :                       type == RPCArg::Type::STR_HEX);
     789           0 :         arr.push_back(map);
     790           0 :     };
     791             : 
     792           0 :     for (int i{0}; i < int(m_args.size()); ++i) {
     793           0 :         const auto& arg = m_args.at(i);
     794           0 :         std::vector<std::string> arg_names = SplitString(arg.m_names, '|');
     795           0 :         for (const auto& arg_name : arg_names) {
     796           0 :             push_back_arg_info(m_name, i, arg_name, arg.m_type);
     797           0 :             if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
     798           0 :                 for (const auto& inner : arg.m_inner) {
     799           0 :                     std::vector<std::string> inner_names = SplitString(inner.m_names, '|');
     800           0 :                     for (const std::string& inner_name : inner_names) {
     801           0 :                         push_back_arg_info(m_name, i, inner_name, inner.m_type);
     802             :                     }
     803           0 :                 }
     804           0 :             }
     805             :         }
     806           0 :     }
     807           0 :     return arr;
     808           0 : }
     809             : 
     810           0 : static std::optional<UniValue::VType> ExpectedType(RPCArg::Type type)
     811             : {
     812             :     using Type = RPCArg::Type;
     813           0 :     switch (type) {
     814             :     case Type::STR_HEX:
     815             :     case Type::STR: {
     816           0 :         return UniValue::VSTR;
     817             :     }
     818             :     case Type::NUM: {
     819           0 :         return UniValue::VNUM;
     820             :     }
     821             :     case Type::AMOUNT: {
     822             :         // VNUM or VSTR, checked inside AmountFromValue()
     823           0 :         return std::nullopt;
     824             :     }
     825             :     case Type::RANGE: {
     826             :         // VNUM or VARR, checked inside ParseRange()
     827           0 :         return std::nullopt;
     828             :     }
     829             :     case Type::BOOL: {
     830           0 :         return UniValue::VBOOL;
     831             :     }
     832             :     case Type::OBJ:
     833             :     case Type::OBJ_NAMED_PARAMS:
     834             :     case Type::OBJ_USER_KEYS: {
     835           0 :         return UniValue::VOBJ;
     836             :     }
     837             :     case Type::ARR: {
     838           0 :         return UniValue::VARR;
     839             :     }
     840             :     } // no default case, so the compiler can warn about missing cases
     841           0 :     NONFATAL_UNREACHABLE();
     842           0 : }
     843             : 
     844           0 : UniValue RPCArg::MatchesType(const UniValue& request) const
     845             : {
     846           0 :     if (m_opts.skip_type_check) return true;
     847           0 :     if (IsOptional() && request.isNull()) return true;
     848           0 :     const auto exp_type{ExpectedType(m_type)};
     849           0 :     if (!exp_type) return true; // nothing to check
     850             : 
     851           0 :     if (*exp_type != request.getType()) {
     852           0 :         return strprintf("JSON value of type %s is not of expected type %s", uvTypeName(request.getType()), uvTypeName(*exp_type));
     853             :     }
     854           0 :     return true;
     855           0 : }
     856             : 
     857           0 : std::string RPCArg::GetFirstName() const
     858             : {
     859           0 :     return m_names.substr(0, m_names.find('|'));
     860             : }
     861             : 
     862           0 : std::string RPCArg::GetName() const
     863             : {
     864           0 :     CHECK_NONFATAL(std::string::npos == m_names.find('|'));
     865           0 :     return m_names;
     866             : }
     867             : 
     868           0 : bool RPCArg::IsOptional() const
     869             : {
     870           0 :     if (m_fallback.index() != 0) {
     871           0 :         return true;
     872             :     } else {
     873           0 :         return RPCArg::Optional::NO != std::get<RPCArg::Optional>(m_fallback);
     874             :     }
     875           0 : }
     876             : 
     877           0 : std::string RPCArg::ToDescriptionString(bool is_named_arg) const
     878             : {
     879           0 :     std::string ret;
     880           0 :     ret += "(";
     881           0 :     if (m_opts.type_str.size() != 0) {
     882           0 :         ret += m_opts.type_str.at(1);
     883           0 :     } else {
     884           0 :         switch (m_type) {
     885             :         case Type::STR_HEX:
     886             :         case Type::STR: {
     887           0 :             ret += "string";
     888           0 :             break;
     889             :         }
     890             :         case Type::NUM: {
     891           0 :             ret += "numeric";
     892           0 :             break;
     893             :         }
     894             :         case Type::AMOUNT: {
     895           0 :             ret += "numeric or string";
     896           0 :             break;
     897             :         }
     898             :         case Type::RANGE: {
     899           0 :             ret += "numeric or array";
     900           0 :             break;
     901             :         }
     902             :         case Type::BOOL: {
     903           0 :             ret += "boolean";
     904           0 :             break;
     905             :         }
     906             :         case Type::OBJ:
     907             :         case Type::OBJ_NAMED_PARAMS:
     908             :         case Type::OBJ_USER_KEYS: {
     909           0 :             ret += "json object";
     910           0 :             break;
     911             :         }
     912             :         case Type::ARR: {
     913           0 :             ret += "json array";
     914           0 :             break;
     915             :         }
     916             :         } // no default case, so the compiler can warn about missing cases
     917             :     }
     918           0 :     if (m_fallback.index() == 1) {
     919           0 :         ret += ", optional, default=" + std::get<RPCArg::DefaultHint>(m_fallback);
     920           0 :     } else if (m_fallback.index() == 2) {
     921           0 :         ret += ", optional, default=" + std::get<RPCArg::Default>(m_fallback).write();
     922           0 :     } else {
     923           0 :         switch (std::get<RPCArg::Optional>(m_fallback)) {
     924             :         case RPCArg::Optional::OMITTED: {
     925           0 :             if (is_named_arg) ret += ", optional"; // Default value is "null" in dicts. Otherwise,
     926             :             // nothing to do. Element is treated as if not present and has no default value
     927           0 :             break;
     928             :         }
     929             :         case RPCArg::Optional::NO: {
     930           0 :             ret += ", required";
     931           0 :             break;
     932             :         }
     933             :         } // no default case, so the compiler can warn about missing cases
     934             :     }
     935           0 :     ret += ")";
     936           0 :     if (m_type == Type::OBJ_NAMED_PARAMS) ret += " Options object that can be used to pass named arguments, listed below.";
     937           0 :     ret += m_description.empty() ? "" : " " + m_description;
     938           0 :     return ret;
     939           0 : }
     940             : 
     941           0 : void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const int current_indent) const
     942             : {
     943             :     // Indentation
     944           0 :     const std::string indent(current_indent, ' ');
     945           0 :     const std::string indent_next(current_indent + 2, ' ');
     946             : 
     947             :     // Elements in a JSON structure (dictionary or array) are separated by a comma
     948           0 :     const std::string maybe_separator{outer_type != OuterType::NONE ? "," : ""};
     949             : 
     950             :     // The key name if recursed into a dictionary
     951           0 :     const std::string maybe_key{
     952           0 :         outer_type == OuterType::OBJ ?
     953           0 :             "\"" + this->m_key_name + "\" : " :
     954           0 :             ""};
     955             : 
     956             :     // Format description with type
     957           0 :     const auto Description = [&](const std::string& type) {
     958           0 :         return "(" + type + (this->m_optional ? ", optional" : "") + ")" +
     959           0 :                (this->m_description.empty() ? "" : " " + this->m_description);
     960           0 :     };
     961             : 
     962           0 :     switch (m_type) {
     963             :     case Type::ELISION: {
     964             :         // If the inner result is empty, use three dots for elision
     965           0 :         sections.PushSection({indent + "..." + maybe_separator, m_description});
     966           0 :         return;
     967             :     }
     968             :     case Type::ANY: {
     969           0 :         NONFATAL_UNREACHABLE(); // Only for testing
     970             :     }
     971             :     case Type::NONE: {
     972           0 :         sections.PushSection({indent + "null" + maybe_separator, Description("json null")});
     973           0 :         return;
     974             :     }
     975             :     case Type::STR: {
     976           0 :         sections.PushSection({indent + maybe_key + "\"str\"" + maybe_separator, Description("string")});
     977           0 :         return;
     978             :     }
     979             :     case Type::STR_AMOUNT: {
     980           0 :         sections.PushSection({indent + maybe_key + "n" + maybe_separator, Description("numeric")});
     981           0 :         return;
     982             :     }
     983             :     case Type::STR_HEX: {
     984           0 :         sections.PushSection({indent + maybe_key + "\"hex\"" + maybe_separator, Description("string")});
     985           0 :         return;
     986             :     }
     987             :     case Type::NUM: {
     988           0 :         sections.PushSection({indent + maybe_key + "n" + maybe_separator, Description("numeric")});
     989           0 :         return;
     990             :     }
     991             :     case Type::NUM_TIME: {
     992           0 :         sections.PushSection({indent + maybe_key + "xxx" + maybe_separator, Description("numeric")});
     993           0 :         return;
     994             :     }
     995             :     case Type::BOOL: {
     996           0 :         sections.PushSection({indent + maybe_key + "true|false" + maybe_separator, Description("boolean")});
     997           0 :         return;
     998             :     }
     999             :     case Type::ARR_FIXED:
    1000             :     case Type::ARR: {
    1001           0 :         sections.PushSection({indent + maybe_key + "[", Description("json array")});
    1002           0 :         for (const auto& i : m_inner) {
    1003           0 :             i.ToSections(sections, OuterType::ARR, current_indent + 2);
    1004             :         }
    1005           0 :         CHECK_NONFATAL(!m_inner.empty());
    1006           0 :         if (m_type == Type::ARR && m_inner.back().m_type != Type::ELISION) {
    1007           0 :             sections.PushSection({indent_next + "...", ""});
    1008           0 :         } else {
    1009             :             // Remove final comma, which would be invalid JSON
    1010           0 :             sections.m_sections.back().m_left.pop_back();
    1011             :         }
    1012           0 :         sections.PushSection({indent + "]" + maybe_separator, ""});
    1013           0 :         return;
    1014             :     }
    1015             :     case Type::OBJ_DYN:
    1016             :     case Type::OBJ: {
    1017           0 :         if (m_inner.empty()) {
    1018           0 :             sections.PushSection({indent + maybe_key + "{}", Description("empty JSON object")});
    1019           0 :             return;
    1020             :         }
    1021           0 :         sections.PushSection({indent + maybe_key + "{", Description("json object")});
    1022           0 :         for (const auto& i : m_inner) {
    1023           0 :             i.ToSections(sections, OuterType::OBJ, current_indent + 2);
    1024             :         }
    1025           0 :         if (m_type == Type::OBJ_DYN && m_inner.back().m_type != Type::ELISION) {
    1026             :             // If the dictionary keys are dynamic, use three dots for continuation
    1027           0 :             sections.PushSection({indent_next + "...", ""});
    1028           0 :         } else {
    1029             :             // Remove final comma, which would be invalid JSON
    1030           0 :             sections.m_sections.back().m_left.pop_back();
    1031             :         }
    1032           0 :         sections.PushSection({indent + "}" + maybe_separator, ""});
    1033           0 :         return;
    1034             :     }
    1035             :     } // no default case, so the compiler can warn about missing cases
    1036           0 :     NONFATAL_UNREACHABLE();
    1037           0 : }
    1038             : 
    1039           0 : static std::optional<UniValue::VType> ExpectedType(RPCResult::Type type)
    1040             : {
    1041             :     using Type = RPCResult::Type;
    1042           0 :     switch (type) {
    1043             :     case Type::ELISION:
    1044             :     case Type::ANY: {
    1045           0 :         return std::nullopt;
    1046             :     }
    1047             :     case Type::NONE: {
    1048           0 :         return UniValue::VNULL;
    1049             :     }
    1050             :     case Type::STR:
    1051             :     case Type::STR_HEX: {
    1052           0 :         return UniValue::VSTR;
    1053             :     }
    1054             :     case Type::NUM:
    1055             :     case Type::STR_AMOUNT:
    1056             :     case Type::NUM_TIME: {
    1057           0 :         return UniValue::VNUM;
    1058             :     }
    1059             :     case Type::BOOL: {
    1060           0 :         return UniValue::VBOOL;
    1061             :     }
    1062             :     case Type::ARR_FIXED:
    1063             :     case Type::ARR: {
    1064           0 :         return UniValue::VARR;
    1065             :     }
    1066             :     case Type::OBJ_DYN:
    1067             :     case Type::OBJ: {
    1068           0 :         return UniValue::VOBJ;
    1069             :     }
    1070             :     } // no default case, so the compiler can warn about missing cases
    1071           0 :     NONFATAL_UNREACHABLE();
    1072           0 : }
    1073             : 
    1074           0 : UniValue RPCResult::MatchesType(const UniValue& result) const
    1075             : {
    1076           0 :     if (m_skip_type_check) {
    1077           0 :         return true;
    1078             :     }
    1079             : 
    1080           0 :     const auto exp_type = ExpectedType(m_type);
    1081           0 :     if (!exp_type) return true; // can be any type, so nothing to check
    1082             : 
    1083           0 :     if (*exp_type != result.getType()) {
    1084           0 :         return strprintf("returned type is %s, but declared as %s in doc", uvTypeName(result.getType()), uvTypeName(*exp_type));
    1085             :     }
    1086             : 
    1087           0 :     if (UniValue::VARR == result.getType()) {
    1088           0 :         UniValue errors(UniValue::VOBJ);
    1089           0 :         for (size_t i{0}; i < result.get_array().size(); ++i) {
    1090             :             // If there are more results than documented, re-use the last doc_inner.
    1091           0 :             const RPCResult& doc_inner{m_inner.at(std::min(m_inner.size() - 1, i))};
    1092           0 :             UniValue match{doc_inner.MatchesType(result.get_array()[i])};
    1093           0 :             if (!match.isTrue()) errors.pushKV(strprintf("%d", i), match);
    1094           0 :         }
    1095           0 :         if (errors.empty()) return true; // empty result array is valid
    1096           0 :         return errors;
    1097           0 :     }
    1098             : 
    1099           0 :     if (UniValue::VOBJ == result.getType()) {
    1100           0 :         if (!m_inner.empty() && m_inner.at(0).m_type == Type::ELISION) return true;
    1101           0 :         UniValue errors(UniValue::VOBJ);
    1102           0 :         if (m_type == Type::OBJ_DYN) {
    1103           0 :             const RPCResult& doc_inner{m_inner.at(0)}; // Assume all types are the same, randomly pick the first
    1104           0 :             for (size_t i{0}; i < result.get_obj().size(); ++i) {
    1105           0 :                 UniValue match{doc_inner.MatchesType(result.get_obj()[i])};
    1106           0 :                 if (!match.isTrue()) errors.pushKV(result.getKeys()[i], match);
    1107           0 :             }
    1108           0 :             if (errors.empty()) return true; // empty result obj is valid
    1109           0 :             return errors;
    1110             :         }
    1111           0 :         std::set<std::string> doc_keys;
    1112           0 :         for (const auto& doc_entry : m_inner) {
    1113           0 :             doc_keys.insert(doc_entry.m_key_name);
    1114             :         }
    1115           0 :         std::map<std::string, UniValue> result_obj;
    1116           0 :         result.getObjMap(result_obj);
    1117           0 :         for (const auto& result_entry : result_obj) {
    1118           0 :             if (doc_keys.find(result_entry.first) == doc_keys.end()) {
    1119           0 :                 errors.pushKV(result_entry.first, "key returned that was not in doc");
    1120           0 :             }
    1121             :         }
    1122             : 
    1123           0 :         for (const auto& doc_entry : m_inner) {
    1124           0 :             const auto result_it{result_obj.find(doc_entry.m_key_name)};
    1125           0 :             if (result_it == result_obj.end()) {
    1126           0 :                 if (!doc_entry.m_optional) {
    1127           0 :                     errors.pushKV(doc_entry.m_key_name, "key missing, despite not being optional in doc");
    1128           0 :                 }
    1129           0 :                 continue;
    1130             :             }
    1131           0 :             UniValue match{doc_entry.MatchesType(result_it->second)};
    1132           0 :             if (!match.isTrue()) errors.pushKV(doc_entry.m_key_name, match);
    1133           0 :         }
    1134           0 :         if (errors.empty()) return true;
    1135           0 :         return errors;
    1136           0 :     }
    1137             : 
    1138           0 :     return true;
    1139           0 : }
    1140             : 
    1141        1928 : void RPCResult::CheckInnerDoc() const
    1142             : {
    1143        1928 :     if (m_type == Type::OBJ) {
    1144             :         // May or may not be empty
    1145         286 :         return;
    1146             :     }
    1147             :     // Everything else must either be empty or not
    1148        1642 :     const bool inner_needed{m_type == Type::ARR || m_type == Type::ARR_FIXED || m_type == Type::OBJ_DYN};
    1149        1642 :     CHECK_NONFATAL(inner_needed != m_inner.empty());
    1150        1928 : }
    1151             : 
    1152           0 : std::string RPCArg::ToStringObj(const bool oneline) const
    1153             : {
    1154           0 :     std::string res;
    1155           0 :     res += "\"";
    1156           0 :     res += GetFirstName();
    1157           0 :     if (oneline) {
    1158           0 :         res += "\":";
    1159           0 :     } else {
    1160           0 :         res += "\": ";
    1161             :     }
    1162           0 :     switch (m_type) {
    1163             :     case Type::STR:
    1164           0 :         return res + "\"str\"";
    1165             :     case Type::STR_HEX:
    1166           0 :         return res + "\"hex\"";
    1167             :     case Type::NUM:
    1168           0 :         return res + "n";
    1169             :     case Type::RANGE:
    1170           0 :         return res + "n or [n,n]";
    1171             :     case Type::AMOUNT:
    1172           0 :         return res + "amount";
    1173             :     case Type::BOOL:
    1174           0 :         return res + "bool";
    1175             :     case Type::ARR:
    1176           0 :         res += "[";
    1177           0 :         for (const auto& i : m_inner) {
    1178           0 :             res += i.ToString(oneline) + ",";
    1179             :         }
    1180           0 :         return res + "...]";
    1181             :     case Type::OBJ:
    1182             :     case Type::OBJ_NAMED_PARAMS:
    1183             :     case Type::OBJ_USER_KEYS:
    1184             :         // Currently unused, so avoid writing dead code
    1185           0 :         NONFATAL_UNREACHABLE();
    1186             :     } // no default case, so the compiler can warn about missing cases
    1187           0 :     NONFATAL_UNREACHABLE();
    1188           0 : }
    1189             : 
    1190           0 : std::string RPCArg::ToString(const bool oneline) const
    1191             : {
    1192           0 :     if (oneline && !m_opts.oneline_description.empty()) {
    1193           0 :         if (m_opts.oneline_description[0] == '\"' && m_type != Type::STR_HEX && m_type != Type::STR && gArgs.GetBoolArg("-rpcdoccheck", DEFAULT_RPC_DOC_CHECK)) {
    1194           0 :             throw std::runtime_error{
    1195           0 :                 STR_INTERNAL_BUG(strprintf("non-string RPC arg \"%s\" quotes oneline_description:\n%s",
    1196             :                     m_names, m_opts.oneline_description)
    1197             :                 )};
    1198             :         }
    1199           0 :         return m_opts.oneline_description;
    1200             :     }
    1201             : 
    1202           0 :     switch (m_type) {
    1203             :     case Type::STR_HEX:
    1204             :     case Type::STR: {
    1205           0 :         return "\"" + GetFirstName() + "\"";
    1206             :     }
    1207             :     case Type::NUM:
    1208             :     case Type::RANGE:
    1209             :     case Type::AMOUNT:
    1210             :     case Type::BOOL: {
    1211           0 :         return GetFirstName();
    1212             :     }
    1213             :     case Type::OBJ:
    1214             :     case Type::OBJ_NAMED_PARAMS:
    1215             :     case Type::OBJ_USER_KEYS: {
    1216           0 :         const std::string res = Join(m_inner, ",", [&](const RPCArg& i) { return i.ToStringObj(oneline); });
    1217           0 :         if (m_type == Type::OBJ) {
    1218           0 :             return "{" + res + "}";
    1219             :         } else {
    1220           0 :             return "{" + res + ",...}";
    1221             :         }
    1222           0 :     }
    1223             :     case Type::ARR: {
    1224           0 :         std::string res;
    1225           0 :         for (const auto& i : m_inner) {
    1226           0 :             res += i.ToString(oneline) + ",";
    1227             :         }
    1228           0 :         return "[" + res + "...]";
    1229           0 :     }
    1230             :     } // no default case, so the compiler can warn about missing cases
    1231           0 :     NONFATAL_UNREACHABLE();
    1232           0 : }
    1233             : 
    1234           0 : static std::pair<int64_t, int64_t> ParseRange(const UniValue& value)
    1235             : {
    1236           0 :     if (value.isNum()) {
    1237           0 :         return {0, value.getInt<int64_t>()};
    1238             :     }
    1239           0 :     if (value.isArray() && value.size() == 2 && value[0].isNum() && value[1].isNum()) {
    1240           0 :         int64_t low = value[0].getInt<int64_t>();
    1241           0 :         int64_t high = value[1].getInt<int64_t>();
    1242           0 :         if (low > high) throw JSONRPCError(RPC_INVALID_PARAMETER, "Range specified as [begin,end] must not have begin after end");
    1243           0 :         return {low, high};
    1244             :     }
    1245           0 :     throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified as end or as [begin,end]");
    1246           0 : }
    1247             : 
    1248           0 : std::pair<int64_t, int64_t> ParseDescriptorRange(const UniValue& value)
    1249             : {
    1250             :     int64_t low, high;
    1251           0 :     std::tie(low, high) = ParseRange(value);
    1252           0 :     if (low < 0) {
    1253           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should be greater or equal than 0");
    1254             :     }
    1255           0 :     if ((high >> 31) != 0) {
    1256           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "End of range is too high");
    1257             :     }
    1258           0 :     if (high >= low + 1000000) {
    1259           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Range is too large");
    1260             :     }
    1261           0 :     return {low, high};
    1262           0 : }
    1263             : 
    1264           0 : std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, FlatSigningProvider& provider, const bool expand_priv)
    1265             : {
    1266           0 :     std::string desc_str;
    1267           0 :     std::pair<int64_t, int64_t> range = {0, 1000};
    1268           0 :     if (scanobject.isStr()) {
    1269           0 :         desc_str = scanobject.get_str();
    1270           0 :     } else if (scanobject.isObject()) {
    1271           0 :         const UniValue& desc_uni{scanobject.find_value("desc")};
    1272           0 :         if (desc_uni.isNull()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor needs to be provided in scan object");
    1273           0 :         desc_str = desc_uni.get_str();
    1274           0 :         const UniValue& range_uni{scanobject.find_value("range")};
    1275           0 :         if (!range_uni.isNull()) {
    1276           0 :             range = ParseDescriptorRange(range_uni);
    1277           0 :         }
    1278           0 :     } else {
    1279           0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Scan object needs to be either a string or an object");
    1280             :     }
    1281             : 
    1282           0 :     std::string error;
    1283           0 :     auto desc = Parse(desc_str, provider, error);
    1284           0 :     if (!desc) {
    1285           0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
    1286             :     }
    1287           0 :     if (!desc->IsRange()) {
    1288           0 :         range.first = 0;
    1289           0 :         range.second = 0;
    1290           0 :     }
    1291           0 :     std::vector<CScript> ret;
    1292           0 :     for (int i = range.first; i <= range.second; ++i) {
    1293           0 :         std::vector<CScript> scripts;
    1294           0 :         if (!desc->Expand(i, provider, scripts, provider)) {
    1295           0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Cannot derive script without private keys: '%s'", desc_str));
    1296             :         }
    1297           0 :         if (expand_priv) {
    1298           0 :             desc->ExpandPrivate(/*pos=*/i, provider, /*out=*/provider);
    1299           0 :         }
    1300           0 :         std::move(scripts.begin(), scripts.end(), std::back_inserter(ret));
    1301           0 :     }
    1302           0 :     return ret;
    1303           0 : }
    1304             : 
    1305           0 : UniValue GetServicesNames(ServiceFlags services)
    1306             : {
    1307           0 :     UniValue servicesNames(UniValue::VARR);
    1308             : 
    1309           0 :     for (const auto& flag : serviceFlagsToStr(services)) {
    1310           0 :         servicesNames.push_back(flag);
    1311             :     }
    1312             : 
    1313           0 :     return servicesNames;
    1314           0 : }
    1315             : 
    1316             : /** Convert a vector of bilingual strings to a UniValue::VARR containing their original untranslated values. */
    1317           0 : [[nodiscard]] static UniValue BilingualStringsToUniValue(const std::vector<bilingual_str>& bilingual_strings)
    1318             : {
    1319           0 :     CHECK_NONFATAL(!bilingual_strings.empty());
    1320           0 :     UniValue result{UniValue::VARR};
    1321           0 :     for (const auto& s : bilingual_strings) {
    1322           0 :         result.push_back(s.original);
    1323             :     }
    1324           0 :     return result;
    1325           0 : }
    1326             : 
    1327           0 : void PushWarnings(const UniValue& warnings, UniValue& obj)
    1328             : {
    1329           0 :     if (warnings.empty()) return;
    1330           0 :     obj.pushKV("warnings", warnings);
    1331           0 : }
    1332             : 
    1333           0 : void PushWarnings(const std::vector<bilingual_str>& warnings, UniValue& obj)
    1334             : {
    1335           0 :     if (warnings.empty()) return;
    1336           0 :     obj.pushKV("warnings", BilingualStringsToUniValue(warnings));
    1337           0 : }

Generated by: LCOV version 1.14