mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-08 10:31:50 -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
|
#define BITCOIN_CORE_IO_H
|
||||||
|
|
||||||
#include <consensus/amount.h>
|
#include <consensus/amount.h>
|
||||||
|
#include <util/result.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -46,7 +47,7 @@ bool DecodeHexBlockHeader(CBlockHeader&, const std::string& hex_header);
|
||||||
*/
|
*/
|
||||||
bool ParseHashStr(const std::string& strHex, uint256& result);
|
bool ParseHashStr(const std::string& strHex, uint256& result);
|
||||||
std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName);
|
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
|
// core_write.cpp
|
||||||
UniValue ValueFromAmount(const CAmount amount);
|
UniValue ValueFromAmount(const CAmount amount);
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#include <serialize.h>
|
#include <serialize.h>
|
||||||
#include <streams.h>
|
#include <streams.h>
|
||||||
#include <univalue.h>
|
#include <univalue.h>
|
||||||
|
#include <util/result.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <version.h>
|
#include <version.h>
|
||||||
|
|
||||||
|
@ -252,26 +253,21 @@ std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strN
|
||||||
return ParseHex(strHex);
|
return ParseHex(strHex);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ParseSighashString(const UniValue& sighash)
|
util::Result<int> SighashFromStr(const std::string& sighash)
|
||||||
{
|
{
|
||||||
int hash_type = SIGHASH_DEFAULT;
|
static std::map<std::string, int> map_sighash_values = {
|
||||||
if (!sighash.isNull()) {
|
{std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
|
||||||
static std::map<std::string, int> map_sighash_values = {
|
{std::string("ALL"), int(SIGHASH_ALL)},
|
||||||
{std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
|
{std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
|
||||||
{std::string("ALL"), int(SIGHASH_ALL)},
|
{std::string("NONE"), int(SIGHASH_NONE)},
|
||||||
{std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
|
{std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
|
||||||
{std::string("NONE"), int(SIGHASH_NONE)},
|
{std::string("SINGLE"), int(SIGHASH_SINGLE)},
|
||||||
{std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
|
{std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
|
||||||
{std::string("SINGLE"), int(SIGHASH_SINGLE)},
|
};
|
||||||
{std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
|
const auto& it = map_sighash_values.find(sighash);
|
||||||
};
|
if (it != map_sighash_values.end()) {
|
||||||
const std::string& strHashType = sighash.get_str();
|
return it->second;
|
||||||
const auto& it = map_sighash_values.find(strHashType);
|
} else {
|
||||||
if (it != map_sighash_values.end()) {
|
return util::Error{Untranslated(sighash + " is not a valid sighash parameter.")};
|
||||||
hash_type = it->second;
|
|
||||||
} else {
|
|
||||||
throw std::runtime_error(strHashType + " is not a valid sighash parameter.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return hash_type;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,10 @@
|
||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
#include <clientversion.h>
|
#include <clientversion.h>
|
||||||
|
#include <core_io.h>
|
||||||
#include <common/args.h>
|
#include <common/args.h>
|
||||||
#include <consensus/amount.h>
|
#include <consensus/amount.h>
|
||||||
|
#include <script/interpreter.h>
|
||||||
#include <key_io.h>
|
#include <key_io.h>
|
||||||
#include <outputtype.h>
|
#include <outputtype.h>
|
||||||
#include <rpc/util.h>
|
#include <rpc/util.h>
|
||||||
|
@ -12,6 +14,7 @@
|
||||||
#include <script/signingprovider.h>
|
#include <script/signingprovider.h>
|
||||||
#include <tinyformat.h>
|
#include <tinyformat.h>
|
||||||
#include <util/check.h>
|
#include <util/check.h>
|
||||||
|
#include <util/result.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <util/string.h>
|
#include <util/string.h>
|
||||||
#include <util/translation.h>
|
#include <util/translation.h>
|
||||||
|
@ -310,6 +313,21 @@ UniValue DescribeAddress(const CTxDestination& dest)
|
||||||
return std::visit(DescribeAddressVisitor(), 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)
|
unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target)
|
||||||
{
|
{
|
||||||
const int target{value.getInt<int>()};
|
const int target{value.getInt<int>()};
|
||||||
|
|
|
@ -100,6 +100,9 @@ CTxDestination AddAndGetMultisigDestination(const int required, const std::vecto
|
||||||
|
|
||||||
UniValue DescribeAddress(const CTxDestination& dest);
|
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.
|
//! Parse a confirm target option and raise an RPC error if it is invalid.
|
||||||
unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target);
|
unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target);
|
||||||
|
|
||||||
|
|
|
@ -75,7 +75,7 @@ FUZZ_TARGET_INIT(parse_univalue, initialize_parse_univalue)
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
(void)ParseSighashString(univalue);
|
(void)ParseSighashString(univalue);
|
||||||
} catch (const std::runtime_error&) {
|
} catch (const UniValue&) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
(void)AmountFromValue(univalue);
|
(void)AmountFromValue(univalue);
|
||||||
|
|
Loading…
Add table
Reference in a new issue