Skip to content

Commit 0bd7ca9

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23642: refactor: Call type-solver earlier in decodescript
3333070 refactor: Call type-solver earlier in decodescript (MarcoFalke) fab0d99 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 3333070 theStack: Code-review ACK 3333070 Tree-SHA512: 49db7bc614d2491cd3ec0177d21ad1e9924dbece1eb5635290cd7fd18cb30adf4711b891daf522e7c4f6baab3033b66393bbfcd1d4726f24f90a433124f925d6
2 parents 6acda4b + 3333070 commit 0bd7ca9

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

src/rpc/rawtransaction.cpp

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -540,32 +540,33 @@ static std::string GetAllOutputTypes()
540540

541541
static RPCHelpMan decodescript()
542542
{
543-
return RPCHelpMan{"decodescript",
544-
"\nDecode a hex-encoded script.\n",
545-
{
546-
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded script"},
547-
},
548-
RPCResult{
549-
RPCResult::Type::OBJ, "", "",
550-
{
551-
{RPCResult::Type::STR, "asm", "Script public key"},
552-
{RPCResult::Type::STR, "type", "The output type (e.g. "+GetAllOutputTypes()+")"},
553-
{RPCResult::Type::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"},
554-
{RPCResult::Type::STR, "p2sh", /* optional */ true, "address of P2SH script wrapping this redeem script (not returned if the script is already a P2SH)"},
555-
{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)",
556-
{
557-
{RPCResult::Type::STR, "asm", "String representation of the script public key"},
558-
{RPCResult::Type::STR_HEX, "hex", "Hex string of the script public key"},
559-
{RPCResult::Type::STR, "type", "The type of the script public key (e.g. witness_v0_keyhash or witness_v0_scripthash)"},
560-
{RPCResult::Type::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"},
561-
{RPCResult::Type::STR, "p2sh-segwit", "address of the P2SH script wrapping this witness redeem script"},
562-
}},
563-
}
564-
},
565-
RPCExamples{
566-
HelpExampleCli("decodescript", "\"hexstring\"")
567-
+ HelpExampleRpc("decodescript", "\"hexstring\"")
568-
},
543+
return RPCHelpMan{
544+
"decodescript",
545+
"\nDecode a hex-encoded script.\n",
546+
{
547+
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded script"},
548+
},
549+
RPCResult{
550+
RPCResult::Type::OBJ, "", "",
551+
{
552+
{RPCResult::Type::STR, "asm", "Script public key"},
553+
{RPCResult::Type::STR, "type", "The output type (e.g. " + GetAllOutputTypes() + ")"},
554+
{RPCResult::Type::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"},
555+
{RPCResult::Type::STR, "p2sh", /* optional */ true, "address of P2SH script wrapping this redeem script (not returned if the script is already a P2SH)"},
556+
{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)",
557+
{
558+
{RPCResult::Type::STR, "asm", "String representation of the script public key"},
559+
{RPCResult::Type::STR_HEX, "hex", "Hex string of the script public key"},
560+
{RPCResult::Type::STR, "type", "The type of the script public key (e.g. witness_v0_keyhash or witness_v0_scripthash)"},
561+
{RPCResult::Type::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"},
562+
{RPCResult::Type::STR, "p2sh-segwit", "address of the P2SH script wrapping this witness redeem script"},
563+
}},
564+
},
565+
},
566+
RPCExamples{
567+
HelpExampleCli("decodescript", "\"hexstring\"")
568+
+ HelpExampleRpc("decodescript", "\"hexstring\"")
569+
},
569570
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
570571
{
571572
RPCTypeCheck(request.params, {UniValue::VSTR});
@@ -580,18 +581,16 @@ static RPCHelpMan decodescript()
580581
}
581582
ScriptPubKeyToUniv(script, r, /* include_hex */ false);
582583

583-
UniValue type;
584-
type = find_value(r, "type");
584+
std::vector<std::vector<unsigned char>> solutions_data;
585+
const TxoutType which_type{Solver(script, solutions_data)};
585586

586-
if (type.isStr() && type.get_str() != "scripthash") {
587+
if (which_type != TxoutType::SCRIPTHASH) {
587588
// P2SH cannot be wrapped in a P2SH. If this script is already a P2SH,
588589
// don't return the address for a P2SH of the P2SH.
589590
r.pushKV("p2sh", EncodeDestination(ScriptHash(script)));
590591
// P2SH and witness programs cannot be wrapped in P2WSH, if this script
591592
// is a witness program, don't return addresses for a segwit programs.
592-
if (type.get_str() == "pubkey" || type.get_str() == "pubkeyhash" || type.get_str() == "multisig" || type.get_str() == "nonstandard") {
593-
std::vector<std::vector<unsigned char>> solutions_data;
594-
TxoutType which_type = Solver(script, solutions_data);
593+
if (which_type == TxoutType::PUBKEY || which_type == TxoutType::PUBKEYHASH || which_type == TxoutType::MULTISIG || which_type == TxoutType::NONSTANDARD) {
595594
// Uncompressed pubkeys cannot be used with segwit checksigs.
596595
// If the script contains an uncompressed pubkey, skip encoding of a segwit program.
597596
if ((which_type == TxoutType::PUBKEY) || (which_type == TxoutType::MULTISIG)) {

0 commit comments

Comments
 (0)