aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2021-12-02 13:56:51 +0100
committerMarcoFalke <falke.marco@gmail.com>2021-12-02 13:56:57 +0100
commit0bd7ca9a036ea33397b04cade07d9acc1c31cd30 (patch)
tree0b36c52bb919baef2ec5c2fe92c4b3f4fe89f30d /src
parent6acda4b00b3fc1bfac02f5de590e1a5386cbc779 (diff)
parent33330702081f67cb05fd86e00b252f6355249513 (diff)
downloadbitcoin-0bd7ca9a036ea33397b04cade07d9acc1c31cd30.tar.xz
Merge bitcoin/bitcoin#23642: refactor: Call type-solver earlier in decodescript
33330702081f67cb05fd86e00b252f6355249513 refactor: Call type-solver earlier in decodescript (MarcoFalke) fab0d998f4bf0f3f09afa51845d91408dd484408 style: Remove whitespace (MarcoFalke) Pull request description: The current logic is a bit confusing. First creating the `UniValue` return dict, then parsing it again to get the type as `std::string`. Clean this up by using a strong type `TxoutType`. Also, remove whitespace. ACKs for top commit: shaavan: ACK 33330702081f67cb05fd86e00b252f6355249513 theStack: Code-review ACK 33330702081f67cb05fd86e00b252f6355249513 Tree-SHA512: 49db7bc614d2491cd3ec0177d21ad1e9924dbece1eb5635290cd7fd18cb30adf4711b891daf522e7c4f6baab3033b66393bbfcd1d4726f24f90a433124f925d6
Diffstat (limited to 'src')
-rw-r--r--src/rpc/rawtransaction.cpp63
1 files changed, 31 insertions, 32 deletions
diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp
index 7f3917b638..b369cce014 100644
--- a/src/rpc/rawtransaction.cpp
+++ b/src/rpc/rawtransaction.cpp
@@ -540,32 +540,33 @@ static std::string GetAllOutputTypes()
static RPCHelpMan decodescript()
{
- return RPCHelpMan{"decodescript",
- "\nDecode a hex-encoded script.\n",
- {
- {"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded script"},
- },
- RPCResult{
- RPCResult::Type::OBJ, "", "",
- {
- {RPCResult::Type::STR, "asm", "Script public key"},
- {RPCResult::Type::STR, "type", "The output type (e.g. "+GetAllOutputTypes()+")"},
- {RPCResult::Type::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"},
- {RPCResult::Type::STR, "p2sh", /* optional */ true, "address of P2SH script wrapping this redeem script (not returned if the script is already a P2SH)"},
- {RPCResult::Type::OBJ, "segwit", /* optional */ true, "Result of a witness script public key wrapping this redeem script (not returned if the script is a P2SH or witness)",
- {
- {RPCResult::Type::STR, "asm", "String representation of the script public key"},
- {RPCResult::Type::STR_HEX, "hex", "Hex string of the script public key"},
- {RPCResult::Type::STR, "type", "The type of the script public key (e.g. witness_v0_keyhash or witness_v0_scripthash)"},
- {RPCResult::Type::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"},
- {RPCResult::Type::STR, "p2sh-segwit", "address of the P2SH script wrapping this witness redeem script"},
- }},
- }
- },
- RPCExamples{
- HelpExampleCli("decodescript", "\"hexstring\"")
- + HelpExampleRpc("decodescript", "\"hexstring\"")
- },
+ return RPCHelpMan{
+ "decodescript",
+ "\nDecode a hex-encoded script.\n",
+ {
+ {"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded script"},
+ },
+ RPCResult{
+ RPCResult::Type::OBJ, "", "",
+ {
+ {RPCResult::Type::STR, "asm", "Script public key"},
+ {RPCResult::Type::STR, "type", "The output type (e.g. " + GetAllOutputTypes() + ")"},
+ {RPCResult::Type::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"},
+ {RPCResult::Type::STR, "p2sh", /* optional */ true, "address of P2SH script wrapping this redeem script (not returned if the script is already a P2SH)"},
+ {RPCResult::Type::OBJ, "segwit", /* optional */ true, "Result of a witness script public key wrapping this redeem script (not returned if the script is a P2SH or witness)",
+ {
+ {RPCResult::Type::STR, "asm", "String representation of the script public key"},
+ {RPCResult::Type::STR_HEX, "hex", "Hex string of the script public key"},
+ {RPCResult::Type::STR, "type", "The type of the script public key (e.g. witness_v0_keyhash or witness_v0_scripthash)"},
+ {RPCResult::Type::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"},
+ {RPCResult::Type::STR, "p2sh-segwit", "address of the P2SH script wrapping this witness redeem script"},
+ }},
+ },
+ },
+ RPCExamples{
+ HelpExampleCli("decodescript", "\"hexstring\"")
+ + HelpExampleRpc("decodescript", "\"hexstring\"")
+ },
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
RPCTypeCheck(request.params, {UniValue::VSTR});
@@ -580,18 +581,16 @@ static RPCHelpMan decodescript()
}
ScriptPubKeyToUniv(script, r, /* include_hex */ false);
- UniValue type;
- type = find_value(r, "type");
+ std::vector<std::vector<unsigned char>> solutions_data;
+ const TxoutType which_type{Solver(script, solutions_data)};
- if (type.isStr() && type.get_str() != "scripthash") {
+ if (which_type != TxoutType::SCRIPTHASH) {
// P2SH cannot be wrapped in a P2SH. If this script is already a P2SH,
// don't return the address for a P2SH of the P2SH.
r.pushKV("p2sh", EncodeDestination(ScriptHash(script)));
// P2SH and witness programs cannot be wrapped in P2WSH, if this script
// is a witness program, don't return addresses for a segwit programs.
- if (type.get_str() == "pubkey" || type.get_str() == "pubkeyhash" || type.get_str() == "multisig" || type.get_str() == "nonstandard") {
- std::vector<std::vector<unsigned char>> solutions_data;
- TxoutType which_type = Solver(script, solutions_data);
+ if (which_type == TxoutType::PUBKEY || which_type == TxoutType::PUBKEYHASH || which_type == TxoutType::MULTISIG || which_type == TxoutType::NONSTANDARD) {
// Uncompressed pubkeys cannot be used with segwit checksigs.
// If the script contains an uncompressed pubkey, skip encoding of a segwit program.
if ((which_type == TxoutType::PUBKEY) || (which_type == TxoutType::MULTISIG)) {