Skip to content

Commit f51b237

Browse files
committed
refactor: rpc: use uint256::FromHex for ParseHashV
uint256S() is deprecated for being unsafe, and will be removed in a future commit.
1 parent 0533e65 commit f51b237

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/rpc/util.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <script/signingprovider.h>
2020
#include <script/solver.h>
2121
#include <tinyformat.h>
22+
#include <uint256.h>
2223
#include <univalue.h>
2324
#include <util/check.h>
2425
#include <util/result.h>
@@ -102,11 +103,11 @@ CFeeRate ParseFeeRate(const UniValue& json)
102103
uint256 ParseHashV(const UniValue& v, std::string_view name)
103104
{
104105
const std::string& strHex(v.get_str());
105-
if (64 != strHex.length())
106-
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be of length %d (not %d, for '%s')", name, 64, strHex.length(), strHex));
107-
if (!IsHex(strHex)) // Note: IsHex("") is false
108-
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));
109-
return uint256S(strHex);
106+
if (auto rv{uint256::FromHex(strHex)}) return *rv;
107+
if (auto expected_len{uint256::size() * 2}; strHex.length() != expected_len) {
108+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be of length %d (not %d, for '%s')", name, expected_len, strHex.length(), strHex));
109+
}
110+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));
110111
}
111112
uint256 ParseHashO(const UniValue& o, std::string_view strKey)
112113
{

0 commit comments

Comments
 (0)