diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -312,7 +312,6 @@ random.cpp rcu.cpp rpc/protocol.cpp - rpc/util.cpp support/cleanse.cpp support/lockedpool.cpp sync.cpp @@ -411,6 +410,7 @@ primitives/block.cpp protocol.cpp psbt.cpp + rpc/util.cpp scheduler.cpp versionbitsinfo.cpp warnings.cpp diff --git a/src/Makefile.am b/src/Makefile.am --- a/src/Makefile.am +++ b/src/Makefile.am @@ -491,6 +491,7 @@ policy/policy.cpp \ protocol.cpp \ psbt.cpp \ + rpc/util.cpp \ scheduler.cpp \ script/descriptor.cpp \ script/ismine.cpp \ @@ -517,7 +518,6 @@ random.cpp \ rcu.cpp \ rpc/protocol.cpp \ - rpc/util.cpp \ support/cleanse.cpp \ sync.cpp \ threadinterrupt.cpp \ diff --git a/src/rpc/server.h b/src/rpc/server.h --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -35,17 +35,6 @@ class Config; -/** - * Wrapper for UniValue::VType, which includes typeAny: used to denote don't - * care type. - */ -struct UniValueType { - UniValueType(UniValue::VType _type) : typeAny(false), type(_type) {} - UniValueType() : typeAny(true) {} - bool typeAny; - UniValue::VType type; -}; - typedef std::map> RPCCommandMap; /** @@ -92,28 +81,6 @@ */ bool RPCIsInWarmup(std::string *outStatus); -/** - * Type-check arguments; throws JSONRPCError if wrong type given. Does not check - * that the right number of arguments are passed, just that any passed are the - * correct type. - */ -void RPCTypeCheck(const UniValue ¶ms, - const std::list &typesExpected, - bool fAllowNull = false); - -/** - * Type-check one argument; throws JSONRPCError if wrong type given. - */ -void RPCTypeCheckArgument(const UniValue &value, - const UniValueType &typeExpected); - -/** - * Check for expected keys/value types in an Object. - */ -void RPCTypeCheckObj(const UniValue &o, - const std::map &typesExpected, - bool fAllowNull = false, bool fStrict = false); - /** * Opaque base class for timers returned by NewTimerFunc. * This provides no methods at the moment, but makes sure that delete cleans up @@ -271,20 +238,6 @@ extern CRPCTable tableRPC; -/** - * Utilities: convert hex-encoded values (throws error if not hex). - */ -extern uint256 ParseHashV(const UniValue &v, std::string strName); -extern uint256 ParseHashO(const UniValue &o, std::string strKey); -extern std::vector ParseHexV(const UniValue &v, std::string strName); -extern std::vector ParseHexO(const UniValue &o, std::string strKey); - -extern Amount AmountFromValue(const UniValue &value); -extern std::string HelpExampleCli(const std::string &methodname, - const std::string &args); -extern std::string HelpExampleRpc(const std::string &methodname, - const std::string &args); - void StartRPC(); void InterruptRPC(); void StopRPC(); diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -115,116 +115,6 @@ g_rpcSignals.Stopped.connect(slot); } -void RPCTypeCheck(const UniValue ¶ms, - const std::list &typesExpected, - bool fAllowNull) { - unsigned int i = 0; - for (const UniValueType &t : typesExpected) { - if (params.size() <= i) { - break; - } - - const UniValue &v = params[i]; - if (!(fAllowNull && v.isNull())) { - RPCTypeCheckArgument(v, t); - } - i++; - } -} - -void RPCTypeCheckArgument(const UniValue &value, - const UniValueType &typeExpected) { - if (!typeExpected.typeAny && value.type() != typeExpected.type) { - throw JSONRPCError(RPC_TYPE_ERROR, - strprintf("Expected type %s, got %s", - uvTypeName(typeExpected.type), - uvTypeName(value.type()))); - } -} - -void RPCTypeCheckObj(const UniValue &o, - const std::map &typesExpected, - bool fAllowNull, bool fStrict) { - for (const auto &t : typesExpected) { - const UniValue &v = find_value(o, t.first); - if (!fAllowNull && v.isNull()) { - throw JSONRPCError(RPC_TYPE_ERROR, - strprintf("Missing %s", t.first)); - } - - if (!(t.second.typeAny || v.type() == t.second.type || - (fAllowNull && v.isNull()))) { - std::string err = strprintf("Expected type %s for %s, got %s", - uvTypeName(t.second.type), t.first, - uvTypeName(v.type())); - throw JSONRPCError(RPC_TYPE_ERROR, err); - } - } - - if (fStrict) { - for (const std::string &k : o.getKeys()) { - if (typesExpected.count(k) == 0) { - std::string err = strprintf("Unexpected key %s", k); - throw JSONRPCError(RPC_TYPE_ERROR, err); - } - } - } -} - -Amount AmountFromValue(const UniValue &value) { - if (!value.isNum() && !value.isStr()) { - throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string"); - } - - int64_t n; - if (!ParseFixedPoint(value.getValStr(), 8, &n)) { - throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount"); - } - - Amount amt = n * SATOSHI; - if (!MoneyRange(amt)) { - throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range"); - } - - return amt; -} - -uint256 ParseHashV(const UniValue &v, std::string strName) { - std::string strHex(v.get_str()); - if (64 != strHex.length()) { - throw JSONRPCError( - RPC_INVALID_PARAMETER, - strprintf("%s must be of length %d (not %d, for '%s')", strName, 64, - strHex.length(), strHex)); - } - // Note: IsHex("") is false - if (!IsHex(strHex)) { - throw JSONRPCError(RPC_INVALID_PARAMETER, - strName + " must be hexadecimal string (not '" + - strHex + "')"); - } - return uint256S(strHex); -} -uint256 ParseHashO(const UniValue &o, std::string strKey) { - return ParseHashV(find_value(o, strKey), strKey); -} -std::vector ParseHexV(const UniValue &v, std::string strName) { - std::string strHex; - if (v.isStr()) { - strHex = v.get_str(); - } - if (!IsHex(strHex)) { - throw JSONRPCError(RPC_INVALID_PARAMETER, - strName + " must be hexadecimal string (not '" + - strHex + "')"); - } - - return ParseHex(strHex); -} -std::vector ParseHexO(const UniValue &o, std::string strKey) { - return ParseHexV(find_value(o, strKey), strKey); -} - std::string CRPCTable::help(Config &config, const std::string &strCommand, const JSONRPCRequest &helpreq) const { std::string strRet; @@ -609,20 +499,6 @@ return commandList; } -std::string HelpExampleCli(const std::string &methodname, - const std::string &args) { - return "> bitcoin-cli " + methodname + " " + args + "\n"; -} - -std::string HelpExampleRpc(const std::string &methodname, - const std::string &args) { - return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", " - "\"id\":\"curltest\", " - "\"method\": \"" + - methodname + "\", \"params\": [" + args + - "] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n"; -} - void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface) { if (!timerInterface) { timerInterface = iface; diff --git a/src/rpc/util.h b/src/rpc/util.h --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -6,7 +6,9 @@ #define BITCOIN_RPC_UTIL_H #include +#include #include