mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-03 09:56:38 -05:00
kernel: Split ParseSighashString
This split is done in preparation for the next commit where the dependency on UniValue in the kernel library is removed.
This commit is contained in:
parent
4a1aae6749
commit
10eb3a9faa
6 changed files with 47 additions and 22 deletions
7
doc/release-notes-28113.md
Normal file
7
doc/release-notes-28113.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
RPC Wallet
|
||||
----------
|
||||
|
||||
- The `signrawtransactionwithkey`, `signrawtransactionwithwallet`,
|
||||
`walletprocesspsbt` and `descriptorprocesspsbt` calls now return more
|
||||
specific RPC_INVALID_PARAMETER instead of RPC_PARSE_ERROR if their
|
||||
sighashtype argument is malformed or not a string.
|
|
@ -6,6 +6,7 @@
|
|||
#define BITCOIN_CORE_IO_H
|
||||
|
||||
#include <consensus/amount.h>
|
||||
#include <util/result.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
@ -46,7 +47,7 @@ bool DecodeHexBlockHeader(CBlockHeader&, const std::string& hex_header);
|
|||
*/
|
||||
bool ParseHashStr(const std::string& strHex, uint256& result);
|
||||
std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName);
|
||||
int ParseSighashString(const UniValue& sighash);
|
||||
[[nodiscard]] util::Result<int> SighashFromStr(const std::string& sighash);
|
||||
|
||||
// core_write.cpp
|
||||
UniValue ValueFromAmount(const CAmount amount);
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <serialize.h>
|
||||
#include <streams.h>
|
||||
#include <univalue.h>
|
||||
#include <util/result.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <version.h>
|
||||
|
||||
|
@ -252,10 +253,8 @@ std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strN
|
|||
return ParseHex(strHex);
|
||||
}
|
||||
|
||||
int ParseSighashString(const UniValue& sighash)
|
||||
util::Result<int> SighashFromStr(const std::string& sighash)
|
||||
{
|
||||
int hash_type = SIGHASH_DEFAULT;
|
||||
if (!sighash.isNull()) {
|
||||
static std::map<std::string, int> map_sighash_values = {
|
||||
{std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
|
||||
{std::string("ALL"), int(SIGHASH_ALL)},
|
||||
|
@ -265,13 +264,10 @@ int ParseSighashString(const UniValue& sighash)
|
|||
{std::string("SINGLE"), int(SIGHASH_SINGLE)},
|
||||
{std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
|
||||
};
|
||||
const std::string& strHashType = sighash.get_str();
|
||||
const auto& it = map_sighash_values.find(strHashType);
|
||||
const auto& it = map_sighash_values.find(sighash);
|
||||
if (it != map_sighash_values.end()) {
|
||||
hash_type = it->second;
|
||||
return it->second;
|
||||
} else {
|
||||
throw std::runtime_error(strHashType + " is not a valid sighash parameter.");
|
||||
return util::Error{Untranslated(sighash + " is not a valid sighash parameter.")};
|
||||
}
|
||||
}
|
||||
return hash_type;
|
||||
}
|
||||
|
|
|
@ -3,8 +3,10 @@
|
|||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <clientversion.h>
|
||||
#include <core_io.h>
|
||||
#include <common/args.h>
|
||||
#include <consensus/amount.h>
|
||||
#include <script/interpreter.h>
|
||||
#include <key_io.h>
|
||||
#include <outputtype.h>
|
||||
#include <rpc/util.h>
|
||||
|
@ -12,6 +14,7 @@
|
|||
#include <script/signingprovider.h>
|
||||
#include <tinyformat.h>
|
||||
#include <util/check.h>
|
||||
#include <util/result.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <util/string.h>
|
||||
#include <util/translation.h>
|
||||
|
@ -310,6 +313,21 @@ UniValue DescribeAddress(const CTxDestination& dest)
|
|||
return std::visit(DescribeAddressVisitor(), dest);
|
||||
}
|
||||
|
||||
int ParseSighashString(const UniValue& sighash)
|
||||
{
|
||||
if (sighash.isNull()) {
|
||||
return SIGHASH_DEFAULT;
|
||||
}
|
||||
if (!sighash.isStr()) {
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "sighash needs to be null or string");
|
||||
}
|
||||
const auto result{SighashFromStr(sighash.get_str())};
|
||||
if (!result) {
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, util::ErrorString(result).original);
|
||||
}
|
||||
return result.value();
|
||||
}
|
||||
|
||||
unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target)
|
||||
{
|
||||
const int target{value.getInt<int>()};
|
||||
|
|
|
@ -100,6 +100,9 @@ CTxDestination AddAndGetMultisigDestination(const int required, const std::vecto
|
|||
|
||||
UniValue DescribeAddress(const CTxDestination& dest);
|
||||
|
||||
/** Parse a sighash string representation and raise an RPC error if it is invalid. */
|
||||
int ParseSighashString(const UniValue& sighash);
|
||||
|
||||
//! Parse a confirm target option and raise an RPC error if it is invalid.
|
||||
unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target);
|
||||
|
||||
|
|
|
@ -75,7 +75,7 @@ FUZZ_TARGET_INIT(parse_univalue, initialize_parse_univalue)
|
|||
}
|
||||
try {
|
||||
(void)ParseSighashString(univalue);
|
||||
} catch (const std::runtime_error&) {
|
||||
} catch (const UniValue&) {
|
||||
}
|
||||
try {
|
||||
(void)AmountFromValue(univalue);
|
||||
|
|
Loading…
Add table
Reference in a new issue