From 1acc61f8746bc6efb905e121a9f607c4f5982b35 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 2 Apr 2019 16:42:51 -0400 Subject: [build] Move rpc utility methods to rpc/util Moves the following utility methods to rpc/util and moves that unit to libbitcoin_common so they can be accessed by all libraries. - `RPCTypeCheck` - `RPCTypeCheckArgument` - `RPCTypeCheckObj` - `AmountFromValue` - `ParseHashV``ParseHashO` - `ParseHexV` - `ParseHexO` - `HelpExampleCli` - `HelpExampleRpc` --- src/rpc/server.cpp | 104 ----------------------------------------------------- 1 file changed, 104 deletions(-) (limited to 'src/rpc/server.cpp') diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index e803fabcc6..a97e3704ed 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -77,99 +77,6 @@ void RPCServer::OnStopped(std::function slot) g_rpcSignals.Stopped.connect(slot); } -void RPCTypeCheck(const UniValue& params, - 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); - } - } - } -} - -CAmount AmountFromValue(const UniValue& value) -{ - if (!value.isNum() && !value.isStr()) - throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string"); - CAmount amount; - if (!ParseFixedPoint(value.getValStr(), 8, &amount)) - throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount"); - if (!MoneyRange(amount)) - throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range"); - return amount; -} - -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)); - if (!IsHex(strHex)) // Note: IsHex("") is false - 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(const std::string& strCommand, const JSONRPCRequest& helpreq) const { std::string strRet; @@ -581,17 +488,6 @@ std::vector CRPCTable::listCommands() const 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) -- cgit v1.2.3