diff --git a/src/rpc/util.h b/src/rpc/util.h --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -87,6 +87,9 @@ UniValue JSONRPCTransactionError(TransactionError terr, const std::string &err_string = ""); +//! Parse a JSON range specified as int64, or [int64, int64] +std::pair ParseRange(const UniValue &value); + struct RPCArg { enum class Type { OBJ, diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -683,3 +683,21 @@ } assert(false); } + +std::pair ParseRange(const UniValue &value) { + if (value.isNum()) { + return {0, value.get_int64()}; + } + if (value.isArray() && value.size() == 2 && value[0].isNum() && + value[1].isNum()) { + int64_t low = value[0].get_int64(); + int64_t high = value[1].get_int64(); + if (low > high) + throw JSONRPCError( + RPC_INVALID_PARAMETER, + "Range specified as [begin,end] must not have begin after end"); + return {low, high}; + } + throw JSONRPCError(RPC_INVALID_PARAMETER, + "Range must be specified as end or as [begin,end]"); +}