// Copyright (c) 2010 Satoshi Nakamoto
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Copyright (c) 2020-2025 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <chain.h>
#include <clientversion.h>
#include <config.h>
#include <core_io.h>
#include <httpserver.h>
#include <index/coinstatsindex.h>
#include <index/txindex.h>
#include <key_io.h>
#include <logging.h>
#include <net.h>
#include <netbase.h>
#include <outputtype.h>
#include <rpc/blockchain.h>
#include <rpc/server.h>
#include <rpc/util.h>
#include <timedata.h>
#include <util/check.h>
#include <util/strencodings.h>
#include <util/system.h>
#include <validation.h>
#include <warnings.h>

#include <univalue.h>

#include <cstdint>
#include <utility>
#ifdef HAVE_MALLOC_INFO
#include <malloc.h>
#endif

static UniValue validateaddress(const Config &config,
                                const JSONRPCRequest &request) {
    if (request.fHelp || request.params.size() != 1) {
        throw std::runtime_error(
            RPCHelpMan{"validateaddress",
                "\nReturn information about the given Bitcoin Cash address.\n",
                {
                    {"address", RPCArg::Type::STR, /* opt */ false, /* default_val */ "", "The Bitcoin Cash address to validate"},
                }}
                .ToString() +
            "\nResult:\n"
            "{\n"
            "  \"isvalid\" : true|false,       (boolean) If the address is "
            "valid or not. If not, this is the only property returned.\n"
            "  \"address\" : \"address\",        (string) The Bitcoin Cash address "
            "validated\n"
            "  \"scriptPubKey\" : \"hex\",       (string) The hex-encoded "
            "scriptPubKey generated by the address\n"
            "  \"isscript\" : true|false,      (boolean) If the key is a "
            "script\n"
            "  \"istokenaware\" : true|false,  (boolean) If this address type "
            "signals support for Cash Tokens\n"
            "}\n"
            "\nExamples:\n" +
            HelpExampleCli("validateaddress",
                           "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"") +
            HelpExampleRpc("validateaddress",
                           "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\""));
    }

    bool tokenAware{};
    CTxDestination dest = DecodeDestination(request.params[0].get_str(), config.GetChainParams(), &tokenAware);
    const bool isValid = IsValidDestination(dest);

    UniValue::Object ret;
    ret.emplace_back("isvalid", isValid);

    if (isValid) {
        ret.emplace_back("address", EncodeDestination(dest, config, tokenAware));

        CScript scriptPubKey = GetScriptForDestination(dest);
        ret.emplace_back("scriptPubKey", HexStr(scriptPubKey));

        DescribeAddress(dest, ret); // isscript

        ret.emplace_back("istokenaware", tokenAware);
    }
    return ret;
}

static UniValue createmultisig(const Config &config,
                               const JSONRPCRequest &request) {
    if (request.fHelp || request.params.size() < 2 ||
        request.params.size() > 2) {

        const std::string msg =
            RPCHelpMan{"createmultisig",
                "\nCreates a multi-signature address with n signature of m keys required.\n"
                "It returns a json object with the address and redeemScript.\n",
                {
                    {"nrequired", RPCArg::Type::NUM, /* opt */ false, /* default_val */ "", "The number of required signatures out of the n keys."},
                    {"keys", RPCArg::Type::ARR, /* opt */ false, /* default_val */ "", "A json array of hex-encoded public keys.",
                        {
                            {"key", RPCArg::Type::STR_HEX, /* opt */ false, /* default_val */ "", "The hex-encoded public key"},
                        }},
                }}
                .ToString() +
            "\nResult:\n"
            "{\n"
            "  \"address\":\"multisigaddress\",  (string) The value of the new "
            "multisig address.\n"
            "  \"redeemScript\":\"script\"       (string) The string value of "
            "the hex-encoded redemption script.\n"
            "}\n"

            "\nExamples:\n"
            "\nCreate a multisig address from 2 public keys\n" +
            HelpExampleCli("createmultisig",
                           "2 "
                           "\"["
                           "\\\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd3"
                           "42cf11ae157a7ace5fd\\\","
                           "\\\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e1"
                           "7e107ef3f6aa5a61626\\\"]\"") +
            "\nAs a JSON-RPC call\n" +
            HelpExampleRpc("createmultisig",
                           "2, "
                           "\"["
                           "\\\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd3"
                           "42cf11ae157a7ace5fd\\\","
                           "\\\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e1"
                           "7e107ef3f6aa5a61626\\\"]\"");
        throw std::runtime_error(msg);
    }

    int required = request.params[0].get_int();

    // Get the public keys
    std::vector<CPubKey> pubkeys;
    for (const UniValue &key : request.params[1].get_array()) {
        const std::string& keyStr = key.get_str();
        if ((keyStr.length() ==
                 2 * CPubKey::COMPRESSED_PUBLIC_KEY_SIZE ||
             keyStr.length() == 2 * CPubKey::PUBLIC_KEY_SIZE) &&
            IsHex(keyStr)) {
            pubkeys.push_back(HexToPubKey(keyStr));
        } else {
            throw JSONRPCError(
                RPC_INVALID_ADDRESS_OR_KEY,
                strprintf("Invalid public key: %s\n", keyStr));
        }
    }

    // Get the output type
    OutputType output_type = OutputType::LEGACY;

    // Construct using pay-to-script-hash:
    const CScript inner = CreateMultisigRedeemscript(required, pubkeys);
    CBasicKeyStore keystore;
    const CTxDestination dest =
        AddAndGetDestinationForScript(keystore, inner, output_type, false /* no p2sh32 */,
                                      false /* use legacy vm limits for multisig */);

    UniValue::Object result;
    result.reserve(2);
    result.emplace_back("address", EncodeDestination(dest, config));
    result.emplace_back("redeemScript", HexStr(inner));

    return result;
}

static UniValue verifymessage(const Config &config,
                              const JSONRPCRequest &request) {
    if (request.fHelp || request.params.size() != 3) {
        throw std::runtime_error(
            RPCHelpMan{"verifymessage",
                "\nVerify a signed message\n",
                {
                    {"address", RPCArg::Type::STR, /* opt */ false, /* default_val */ "", "The Bitcoin Cash address to use for the signature."},
                    {"signature", RPCArg::Type::STR, /* opt */ false, /* default_val */ "", "The signature provided by the signer in base 64 encoding (see signmessage)."},
                    {"message", RPCArg::Type::STR, /* opt */ false, /* default_val */ "", "The message that was signed."},
                }}
                .ToString() +
            "\nResult:\n"
            "true|false   (boolean) If the signature is verified or not.\n"
            "\nExamples:\n"
            "\nUnlock the wallet for 30 seconds\n" +
            HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
            "\nCreate the signature\n" +
            HelpExampleCli(
                "signmessage",
                "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +
            "\nVerify the signature\n" +
            HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4"
                                            "XX\" \"signature\" \"my "
                                            "message\"") +
            "\nAs a JSON-RPC call\n" +
            HelpExampleRpc("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4"
                                            "XX\", \"signature\", \"my "
                                            "message\""));
    }

    LOCK(cs_main);

    const std::string &strAddress = request.params[0].get_str();
    const std::string &strSign = request.params[1].get_str();
    const std::string &strMessage = request.params[2].get_str();

    CTxDestination destination =
        DecodeDestination(strAddress, config.GetChainParams());
    if (!IsValidDestination(destination)) {
        throw JSONRPCError(RPC_TYPE_ERROR, "Invalid address");
    }

    const CKeyID *keyID = std::get_if<CKeyID>(&destination);
    if (!keyID) {
        throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
    }

    bool fInvalid = false;
    std::vector<uint8_t> vchSig = DecodeBase64(strSign.c_str(), &fInvalid);

    if (fInvalid) {
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY,
                           "Malformed base64 encoding");
    }

    CHashWriter ss(SER_GETHASH, 0);
    ss << strMessageMagic;
    ss << strMessage;

    CPubKey pubkey;
    if (!pubkey.RecoverCompact(ss.GetHash(), vchSig)) {
        return false;
    }

    return (pubkey.GetID() == *keyID);
}

static UniValue signmessagewithprivkey(const Config &config,
                                       const JSONRPCRequest &request) {
    if (request.fHelp || request.params.size() != 2) {
        throw std::runtime_error(
            RPCHelpMan{"signmessagewithprivkey",
                "\nSign a message with the private key of an address\n",
                {
                    {"privkey", RPCArg::Type::STR, /* opt */ false, /* default_val */ "", "The private key to sign the message with."},
                    {"message", RPCArg::Type::STR, /* opt */ false, /* default_val */ "", "The message to create a signature of."},
                }}
                .ToString() +
            "\nResult:\n"
            "\"signature\"          (string) The signature of the message "
            "encoded in base 64\n"
            "\nExamples:\n"
            "\nCreate the signature\n" +
            HelpExampleCli("signmessagewithprivkey",
                           "\"privkey\" \"my message\"") +
            "\nVerify the signature\n" +
            HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4"
                                            "XX\" \"signature\" \"my "
                                            "message\"") +
            "\nAs a JSON-RPC call\n" +
            HelpExampleRpc("signmessagewithprivkey",
                           "\"privkey\", \"my message\""));
    }

    const std::string &strPrivkey = request.params[0].get_str();
    const std::string &strMessage = request.params[1].get_str();

    CKey key = DecodeSecret(strPrivkey);
    if (!key.IsValid()) {
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
    }

    CHashWriter ss(SER_GETHASH, 0);
    ss << strMessageMagic;
    ss << strMessage;

    std::vector<uint8_t> vchSig;
    if (!key.SignCompact(ss.GetHash(), vchSig)) {
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed");
    }

    return EncodeBase64(vchSig);
}

static UniValue setmocktime(const Config &config,
                            const JSONRPCRequest &request) {
    if (request.fHelp || request.params.size() != 1) {
        throw std::runtime_error(RPCHelpMan{
            "setmocktime",
            "\nSet the local time to given timestamp (-regtest only)\n",
            {
                {"timestamp", RPCArg::Type::NUM, /* opt */ false, /* default_val */ "",
                 "Unix seconds-since-epoch timestamp\n"
                 "Pass 0 to go back to using the system time."},
            }}.ToString());
    }

    if (!config.GetChainParams().MineBlocksOnDemand()) {
        throw std::runtime_error(
            "setmocktime for regression testing (-regtest mode) only");
    }

    // For now, don't change mocktime if we're in the middle of validation, as
    // this could have an effect on mempool time-based eviction, as well as
    // IsInitialBlockDownload().
    // TODO: figure out the right way to synchronize around mocktime, and
    // ensure all call sites of GetTime() are accessing this safely.
    LOCK(cs_main);

    RPCTypeCheck(request.params, {UniValue::VNUM});
    const int64_t mockTime{request.params[0].get_int64()};
    if (mockTime < 0) {
        throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Mocktime can not be negative: %s.", mockTime));
    }
    SetMockTime(mockTime);

    return UniValue();
}

static UniValue::Object RPCLockedMemoryInfo() {
    LockedPool::Stats stats = LockedPoolManager::Instance().stats();
    UniValue::Object obj;
    obj.reserve(6);
    obj.emplace_back("used", stats.used);
    obj.emplace_back("free", stats.free);
    obj.emplace_back("total", stats.total);
    obj.emplace_back("locked", stats.locked);
    obj.emplace_back("chunks_used", stats.chunks_used);
    obj.emplace_back("chunks_free", stats.chunks_free);
    return obj;
}

#ifdef HAVE_MALLOC_INFO
static std::string RPCMallocInfo() {
    char *ptr = nullptr;
    size_t size = 0;
    FILE *f = open_memstream(&ptr, &size);
    if (f) {
        malloc_info(0, f);
        fclose(f);
        if (ptr) {
            std::string rv(ptr, size);
            free(ptr);
            return rv;
        }
    }
    return "";
}
#endif

static UniValue getmemoryinfo(const Config &config,
                              const JSONRPCRequest &request) {
    /* Please, avoid using the word "pool" here in the RPC interface or help,
     * as users will undoubtedly confuse it with the other "memory pool"
     */
    if (request.fHelp || request.params.size() > 1) {
        throw std::runtime_error(
            RPCHelpMan{"getmemoryinfo",
                "Returns an object containing information about memory usage.\n",
                {
                    {"mode", RPCArg::Type::STR, /* opt */ true, /* default_val */ "", "determines what kind of information is returned. This argument is optional, the default mode is \"stats\".\n"
            "  - \"stats\" returns general statistics about memory usage in the daemon.\n"
            "  - \"mallocinfo\" returns an XML string describing low-level heap state (only available if compiled with glibc 2.10+)."},
                }}
                .ToString() +
            "\nResult (mode \"stats\"):\n"
            "{\n"
            "  \"locked\": {               (json object) Information about "
            "locked memory manager\n"
            "    \"used\": xxxxx,          (numeric) Number of bytes used\n"
            "    \"free\": xxxxx,          (numeric) Number of bytes available "
            "in current arenas\n"
            "    \"total\": xxxxxxx,       (numeric) Total number of bytes "
            "managed\n"
            "    \"locked\": xxxxxx,       (numeric) Amount of bytes that "
            "succeeded locking. If this number is smaller than total, locking "
            "pages failed at some point and key data could be swapped to "
            "disk.\n"
            "    \"chunks_used\": xxxxx,   (numeric) Number allocated chunks\n"
            "    \"chunks_free\": xxxxx,   (numeric) Number unused chunks\n"
            "  }\n"
            "}\n"
            "\nResult (mode \"mallocinfo\"):\n"
            "\"<malloc version=\"1\">...\"\n"
            "\nExamples:\n" +
            HelpExampleCli("getmemoryinfo", "") +
            HelpExampleRpc("getmemoryinfo", ""));
    }

    std::string mode =
        request.params[0].isNull() ? "stats" : request.params[0].get_str();
    if (mode == "stats") {
        UniValue::Object obj;
        obj.reserve(1);
        obj.emplace_back("locked", RPCLockedMemoryInfo());
        return obj;
    } else if (mode == "mallocinfo") {
#ifdef HAVE_MALLOC_INFO
        return RPCMallocInfo();
#else
        throw JSONRPCError(
            RPC_INVALID_PARAMETER,
            "mallocinfo is only available when compiled with glibc 2.10+");
#endif
    } else {
        throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown mode " + std::move(mode));
    }
}

static void EnableOrDisableLogCategories(const UniValue::Array& cats, bool enable) {
    for (auto& cat : cats) {
        auto& catStr = cat.get_str();

        bool success;
        if (enable) {
            success = LogInstance().EnableCategory(catStr);
        } else {
            success = LogInstance().DisableCategory(catStr);
        }

        if (!success) {
            throw JSONRPCError(RPC_INVALID_PARAMETER,
                               "unknown logging category " + catStr);
        }
    }
}

static UniValue logging(const Config &config, const JSONRPCRequest &request) {
    if (request.fHelp || request.params.size() > 2) {
        throw std::runtime_error(
            RPCHelpMan{"logging",
            "Gets and sets the logging configuration.\n"
            "When called without an argument, returns the list of categories "
            "with status that are currently being debug logged or not.\n"
            "When called with arguments, adds or removes categories from debug "
            "logging and return the lists above.\n"
            "The arguments are evaluated in order \"include\", \"exclude\".\n"
            "If an item is both included and excluded, it will thus end up "
            "being excluded.\n"
            "The valid logging categories are: " +
            ListLogCategories() +
            "\n"
            "In addition, the following are available as category names with "
            "special meanings:\n"
            "  - \"all\",  \"1\" : represent all logging categories.\n"
            "  - \"none\", \"0\" : even if other logging categories are specified, ignore all of them.\n"
            ,
                {
                    {"include", RPCArg::Type::ARR, /* opt */ true, /* default_val */ "", "A json array of categories to add debug logging",
                        {
                            {"include_category", RPCArg::Type::STR, /* opt */ false, /* default_val */ "", "the valid logging category"},
                        }},
                    {"exclude", RPCArg::Type::ARR, /* opt */ true, /* default_val */ "", "A json array of categories to remove debug logging",
                        {
                            {"exclude_category", RPCArg::Type::STR, /* opt */ false, /* default_val */ "", "the valid logging category"},
                        }},
                }}
                .ToString() +
            "\nResult:\n"
            "{                   (json object where keys are the logging "
            "categories, and values indicates its status\n"
            "  \"category\": 0|1,  (numeric) if being debug logged or not. "
            "0:inactive, 1:active\n"
            "  ...\n"
            "}\n"
            "\nExamples:\n" +
            HelpExampleCli("logging", "\"[\\\"all\\\"]\" \"[\\\"http\\\"]\"") +
            HelpExampleRpc("logging", "[\"all\"], \"[libevent]\""));
    }

    uint32_t original_log_categories = LogInstance().GetCategoryMask();
    if (request.params[0].isArray()) {
        EnableOrDisableLogCategories(request.params[0].get_array(), true);
    }

    if (request.params[1].isArray()) {
        EnableOrDisableLogCategories(request.params[1].get_array(), false);
    }

    uint32_t updated_log_categories = LogInstance().GetCategoryMask();
    uint32_t changed_log_categories =
        original_log_categories ^ updated_log_categories;

    /**
     * Update libevent logging if BCLog::LIBEVENT has changed.
     * If the library version doesn't allow it, UpdateHTTPServerLogging()
     * returns false, in which case we should clear the BCLog::LIBEVENT flag.
     * Throw an error if the user has explicitly asked to change only the
     * libevent flag and it failed.
     */
    if (changed_log_categories & BCLog::LIBEVENT) {
        if (!UpdateHTTPServerLogging(
                LogInstance().WillLogCategory(BCLog::LIBEVENT))) {
            LogInstance().DisableCategory(BCLog::LIBEVENT);
            if (changed_log_categories == BCLog::LIBEVENT) {
                throw JSONRPCError(RPC_INVALID_PARAMETER,
                                   "libevent logging cannot be updated when "
                                   "using libevent before v2.1.1.");
            }
        }
    }

    UniValue::Object result;
    std::vector<CLogCategoryActive> vLogCatActive = ListActiveLogCategories();
    result.reserve(vLogCatActive.size());
    for (auto &logCatActive : vLogCatActive) {
        result.emplace_back(std::move(logCatActive.category), logCatActive.active);
    }

    return result;
}

static UniValue echo(const Config &config, const JSONRPCRequest &request) {
    if (request.fHelp) {
        throw std::runtime_error(
            RPCHelpMan{"echo|echojson ...",
                "\nSimply echo back the input arguments. This command is for testing.\n"
                "\nIt will return an internal bug report when exactly 100 arguments are passed.\n"
                "\nThe difference between echo and echojson is that echojson has argument conversion enabled in the client-side table in "
                "bitcoin-cli and the GUI. There is no server-side difference.",
                {}}
                .ToString() +
            "");
    }

    CHECK_NONFATAL(request.params.size() != 100);

    return UniValue(request.params);
}

static UniValue getinfo_deprecated(const Config &config,
                                   const JSONRPCRequest &request) {
    throw JSONRPCError(RPC_METHOD_NOT_FOUND,
                       "getinfo\n"
                       "\nThis call was removed in version 0.19.8. Use the "
                       "appropriate fields from:\n"
                       "- getblockchaininfo: blocks, difficulty, chain\n"
                       "- getnetworkinfo: version, protocolversion, "
                       "timeoffset, connections, proxy, relayfee, warnings\n"
                       "- getwalletinfo: balance, keypoololdest, keypoolsize, "
                       "paytxfee, unlocked_until, walletversion\n"
                       "\nbitcoin-cli has the option -getinfo to collect and "
                       "format these in the old format.");
}

static UniValue getindexinfo(const Config &, const JSONRPCRequest &request) {
    if (request.fHelp || request.params.size() > 1) {
        throw std::runtime_error(
            RPCHelpMan{
                "getindexinfo",
                "\nReturns the status of one or all available indices currently running in the node.\n",
                {
                    {"index_name", RPCArg::Type::STR, /* optional */ true, /* default */ "", "Filter results for an index with a specific name."},
                },
                RPCResult{
                    "{                               (json object)\n"
                    "  \"name\" : {                    (json object) The name of the index\n"
                    "    \"synced\" : true|false,      (boolean) Whether the index is synced or not\n"
                    "    \"best_block_height\" : n     (numeric) The block height to which the index is synced\n"
                    "  },\n"
                    "  ...\n"
                    "}\n"
                },
                RPCExamples{
                    HelpExampleCli("getindexinfo", "")
                    + HelpExampleRpc("getindexinfo", "")
                    + HelpExampleCli("getindexinfo", "txindex")
                    + HelpExampleRpc("getindexinfo", "\"txindex\"")
                }
            }.ToStringWithResultsAndExamples()
        );
    }

    UniValue::Object result;
    const std::string index_name = request.params[0].isNull() ? "" : request.params[0].get_str();

    auto SummaryToJSON = [&index_name](const IndexSummary &summary) {
        UniValue::Object ret_summary;
        if (index_name.empty() || index_name == summary.name) {
            UniValue::Object entry;
            entry.reserve(2);
            entry.emplace_back("synced", summary.synced);
            entry.emplace_back("best_block_height", summary.best_block_height);
            ret_summary.emplace_back(summary.name, std::move(entry));
        }
        return ret_summary;
    };

    auto ExtendResult = [&result](UniValue::Object &&obj) {
        for (auto &&kvpair : obj) {
            result.emplace_back(std::move(kvpair));
        }
    };

    if (g_txindex) {
        ExtendResult(SummaryToJSON(g_txindex->GetSummary()));
    }

    if (g_coin_stats_index) {
        ExtendResult(SummaryToJSON(g_coin_stats_index->GetSummary()));
    }

    return result;
}
// clang-format off
static const ContextFreeRPCCommand commands[] = {
    //  category            name                      actor (function)        argNames
    //  ------------------- ------------------------  ----------------------  ----------
    { "control",            "getmemoryinfo",          getmemoryinfo,          {"mode"} },
    { "control",            "logging",                logging,                {"include", "exclude"} },
    { "util",               "validateaddress",        validateaddress,        {"address"} },
    { "util",               "createmultisig",         createmultisig,         {"nrequired","keys"} },
    { "util",               "verifymessage",          verifymessage,          {"address","signature","message"} },
    { "util",               "signmessagewithprivkey", signmessagewithprivkey, {"privkey","message"} },
    { "util",               "getindexinfo",           getindexinfo,           {"index_name"} },
    /* Not shown in help */
    { "hidden",             "setmocktime",            setmocktime,            {"timestamp"}},
    { "hidden",             "echo",                   echo,                   {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
    { "hidden",             "echojson",               echo,                   {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
    { "hidden",             "getinfo",                getinfo_deprecated,     {}},
};
// clang-format on

void RegisterMiscRPCCommands(CRPCTable &t) {
    for (unsigned int vcidx = 0; vcidx < std::size(commands); ++vcidx) {
        t.appendCommand(commands[vcidx].name, &commands[vcidx]);
    }
}
