Skip to content

Commit 285e2a5

Browse files
MarcoFalkevijaydasmp
authored andcommitted
Merge 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
1 parent 19ffc2f commit 285e2a5

File tree

1 file changed

+55
-53
lines changed

1 file changed

+55
-53
lines changed

src/rpc/rawtransaction.cpp

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -879,29 +879,33 @@ static std::string GetAllOutputTypes()
879879

880880
static RPCHelpMan decodescript()
881881
{
882-
return RPCHelpMan{"decodescript",
883-
"\nDecode a hex-encoded script.\n",
884-
{
885-
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded script"},
886-
},
887-
RPCResult{
888-
RPCResult::Type::OBJ, "", "",
889-
{
890-
{RPCResult::Type::STR, "asm", "Script public key"},
891-
{RPCResult::Type::STR, "type", "The output type (e.g. "+GetAllOutputTypes()+")"},
892-
{RPCResult::Type::STR, "address", /* optional */ true, "Dash address (only if a well-defined address exists)"},
893-
{RPCResult::Type::NUM, "reqSigs", /* optional */ true, "(DEPRECATED, returned only if config option -deprecatedrpc=addresses is passed) Number of required signatures"},
894-
{RPCResult::Type::ARR, "addresses", /* optional */ true, "(DEPRECATED, returned only if config option -deprecatedrpc=addresses is passed) Array of Dash addresses",
895-
{
896-
{RPCResult::Type::STR, "address", "Dash address"},
897-
}},
898-
{RPCResult::Type::STR, "p2sh", "address of P2SH script wrapping this redeem script (not returned if the script is already a P2SH)"},
899-
}
900-
},
901-
RPCExamples{
902-
HelpExampleCli("decodescript", "\"hexstring\"")
903-
+ HelpExampleRpc("decodescript", "\"hexstring\"")
904-
},
882+
return RPCHelpMan{
883+
"decodescript",
884+
"\nDecode a hex-encoded script.\n",
885+
{
886+
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded script"},
887+
},
888+
RPCResult{
889+
RPCResult::Type::OBJ, "", "",
890+
{
891+
{RPCResult::Type::STR, "asm", "Script public key"},
892+
{RPCResult::Type::STR, "type", "The output type (e.g. " + GetAllOutputTypes() + ")"},
893+
{RPCResult::Type::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"},
894+
{RPCResult::Type::STR, "p2sh", /* optional */ true, "address of P2SH script wrapping this redeem script (not returned if the script is already a P2SH)"},
895+
{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)",
896+
{
897+
{RPCResult::Type::STR, "asm", "String representation of the script public key"},
898+
{RPCResult::Type::STR_HEX, "hex", "Hex string of the script public key"},
899+
{RPCResult::Type::STR, "type", "The type of the script public key (e.g. witness_v0_keyhash or witness_v0_scripthash)"},
900+
{RPCResult::Type::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"},
901+
{RPCResult::Type::STR, "p2sh-segwit", "address of the P2SH script wrapping this witness redeem script"},
902+
}},
903+
},
904+
},
905+
RPCExamples{
906+
HelpExampleCli("decodescript", "\"hexstring\"")
907+
+ HelpExampleRpc("decodescript", "\"hexstring\"")
908+
},
905909
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
906910
{
907911
RPCTypeCheck(request.params, {UniValue::VSTR});
@@ -916,11 +920,10 @@ static RPCHelpMan decodescript()
916920
}
917921
ScriptPubKeyToUniv(script, r, /* fIncludeHex */ false);
918922

919-
UniValue type;
923+
std::vector<std::vector<unsigned char>> solutions_data;
924+
const TxoutType which_type{Solver(script, solutions_data)};
920925

921-
type = find_value(r, "type");
922-
923-
if (type.isStr() && type.get_str() != "scripthash") {
926+
if (which_type != TxoutType::SCRIPTHASH) {
924927
// P2SH cannot be wrapped in a P2SH. If this script is already a P2SH,
925928
// don't return the address for a P2SH of the P2SH.
926929
r.pushKV("p2sh", EncodeDestination(ScriptHash(script)));
@@ -933,35 +936,34 @@ static RPCHelpMan decodescript()
933936

934937
static RPCHelpMan combinerawtransaction()
935938
{
936-
return RPCHelpMan{"combinerawtransaction",
937-
"\nCombine multiple partially signed transactions into one transaction.\n"
938-
"The combined transaction may be another partially signed transaction or a \n"
939-
"fully signed transaction.",
940-
{
941-
{"txs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The of hex strings of partially signed transactions",
942-
{
943-
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "A hex-encoded raw transaction"},
944-
},
945-
},
946-
},
947-
RPCResult{
948-
RPCResult::Type::STR, "", "The hex-encoded raw transaction with signature(s)"
949-
},
950-
RPCExamples{
951-
HelpExampleCli("combinerawtransaction", R"('["myhex1", "myhex2", "myhex3"]')")
952-
},
953-
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
954-
{
939+
return RPCHelpMan{"combinerawtransaction",
940+
"\nCombine multiple partially signed transactions into one transaction.\n"
941+
"The combined transaction may be another partially signed transaction or a \n"
942+
"fully signed transaction.",
943+
{
944+
{"txs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The of hex strings of partially signed transactions",
945+
{
946+
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "A hex-encoded raw transaction"},
947+
},
948+
},
949+
},
950+
RPCResult{
951+
RPCResult::Type::STR, "", "The hex-encoded raw transaction with signature(s)"
952+
},
953+
RPCExamples{
954+
HelpExampleCli("combinerawtransaction", R"('["myhex1", "myhex2", "myhex3"]')")
955+
},
956+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
957+
{
955958

956-
UniValue txs = request.params[0].get_array();
957-
std::vector<CMutableTransaction> txVariants(txs.size());
959+
UniValue txs = request.params[0].get_array();
960+
std::vector<CMutableTransaction> txVariants(txs.size());
958961

959-
for (unsigned int idx = 0; idx < txs.size(); idx++) {
960-
if (!DecodeHexTx(txVariants[idx], txs[idx].get_str())) {
961-
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed for tx %d. Make sure the tx has at least one input.", idx));
962+
for (unsigned int idx = 0; idx < txs.size(); idx++) {
963+
if (!DecodeHexTx(txVariants[idx], txs[idx].get_str())) {
964+
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed for tx %d. Make sure the tx has at least one input.", idx));
965+
}
962966
}
963-
}
964-
965967
if (txVariants.empty()) {
966968
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Missing transactions");
967969
}

0 commit comments

Comments
 (0)