Skip to content

Commit 713a76d

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 ba0ced5 commit 713a76d

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
@@ -869,29 +869,33 @@ static RPCHelpMan decoderawtransaction()
869869

870870
static RPCHelpMan decodescript()
871871
{
872-
return RPCHelpMan{"decodescript",
873-
"\nDecode a hex-encoded script.\n",
874-
{
875-
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded script"},
876-
},
877-
RPCResult{
878-
RPCResult::Type::OBJ, "", "",
879-
{
880-
{RPCResult::Type::STR, "asm", "Script public key"},
881-
{RPCResult::Type::STR, "type", "The output type (e.g. "+GetAllOutputTypes()+")"},
882-
{RPCResult::Type::STR, "address", /* optional */ true, "Dash address (only if a well-defined address exists)"},
883-
{RPCResult::Type::NUM, "reqSigs", /* optional */ true, "(DEPRECATED, returned only if config option -deprecatedrpc=addresses is passed) Number of required signatures"},
884-
{RPCResult::Type::ARR, "addresses", /* optional */ true, "(DEPRECATED, returned only if config option -deprecatedrpc=addresses is passed) Array of Dash addresses",
885-
{
886-
{RPCResult::Type::STR, "address", "Dash address"},
887-
}},
888-
{RPCResult::Type::STR, "p2sh", "address of P2SH script wrapping this redeem script (not returned if the script is already a P2SH)"},
889-
}
890-
},
891-
RPCExamples{
892-
HelpExampleCli("decodescript", "\"hexstring\"")
893-
+ HelpExampleRpc("decodescript", "\"hexstring\"")
894-
},
872+
return RPCHelpMan{
873+
"decodescript",
874+
"\nDecode a hex-encoded script.\n",
875+
{
876+
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded script"},
877+
},
878+
RPCResult{
879+
RPCResult::Type::OBJ, "", "",
880+
{
881+
{RPCResult::Type::STR, "asm", "Script public key"},
882+
{RPCResult::Type::STR, "type", "The output type (e.g. " + GetAllOutputTypes() + ")"},
883+
{RPCResult::Type::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"},
884+
{RPCResult::Type::STR, "p2sh", /* optional */ true, "address of P2SH script wrapping this redeem script (not returned if the script is already a P2SH)"},
885+
{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)",
886+
{
887+
{RPCResult::Type::STR, "asm", "String representation of the script public key"},
888+
{RPCResult::Type::STR_HEX, "hex", "Hex string of the script public key"},
889+
{RPCResult::Type::STR, "type", "The type of the script public key (e.g. witness_v0_keyhash or witness_v0_scripthash)"},
890+
{RPCResult::Type::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"},
891+
{RPCResult::Type::STR, "p2sh-segwit", "address of the P2SH script wrapping this witness redeem script"},
892+
}},
893+
},
894+
},
895+
RPCExamples{
896+
HelpExampleCli("decodescript", "\"hexstring\"")
897+
+ HelpExampleRpc("decodescript", "\"hexstring\"")
898+
},
895899
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
896900
{
897901
RPCTypeCheck(request.params, {UniValue::VSTR});
@@ -906,11 +910,10 @@ static RPCHelpMan decodescript()
906910
}
907911
ScriptPubKeyToUniv(script, r, /* fIncludeHex */ false);
908912

909-
UniValue type;
913+
std::vector<std::vector<unsigned char>> solutions_data;
914+
const TxoutType which_type{Solver(script, solutions_data)};
910915

911-
type = find_value(r, "type");
912-
913-
if (type.isStr() && type.get_str() != "scripthash") {
916+
if (which_type != TxoutType::SCRIPTHASH) {
914917
// P2SH cannot be wrapped in a P2SH. If this script is already a P2SH,
915918
// don't return the address for a P2SH of the P2SH.
916919
r.pushKV("p2sh", EncodeDestination(ScriptHash(script)));
@@ -923,35 +926,34 @@ static RPCHelpMan decodescript()
923926

924927
static RPCHelpMan combinerawtransaction()
925928
{
926-
return RPCHelpMan{"combinerawtransaction",
927-
"\nCombine multiple partially signed transactions into one transaction.\n"
928-
"The combined transaction may be another partially signed transaction or a \n"
929-
"fully signed transaction.",
930-
{
931-
{"txs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The of hex strings of partially signed transactions",
932-
{
933-
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "A hex-encoded raw transaction"},
934-
},
935-
},
936-
},
937-
RPCResult{
938-
RPCResult::Type::STR, "", "The hex-encoded raw transaction with signature(s)"
939-
},
940-
RPCExamples{
941-
HelpExampleCli("combinerawtransaction", R"('["myhex1", "myhex2", "myhex3"]')")
942-
},
943-
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
944-
{
929+
return RPCHelpMan{"combinerawtransaction",
930+
"\nCombine multiple partially signed transactions into one transaction.\n"
931+
"The combined transaction may be another partially signed transaction or a \n"
932+
"fully signed transaction.",
933+
{
934+
{"txs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The of hex strings of partially signed transactions",
935+
{
936+
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "A hex-encoded raw transaction"},
937+
},
938+
},
939+
},
940+
RPCResult{
941+
RPCResult::Type::STR, "", "The hex-encoded raw transaction with signature(s)"
942+
},
943+
RPCExamples{
944+
HelpExampleCli("combinerawtransaction", R"('["myhex1", "myhex2", "myhex3"]')")
945+
},
946+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
947+
{
945948

946-
UniValue txs = request.params[0].get_array();
947-
std::vector<CMutableTransaction> txVariants(txs.size());
949+
UniValue txs = request.params[0].get_array();
950+
std::vector<CMutableTransaction> txVariants(txs.size());
948951

949-
for (unsigned int idx = 0; idx < txs.size(); idx++) {
950-
if (!DecodeHexTx(txVariants[idx], txs[idx].get_str())) {
951-
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed for tx %d. Make sure the tx has at least one input.", idx));
952+
for (unsigned int idx = 0; idx < txs.size(); idx++) {
953+
if (!DecodeHexTx(txVariants[idx], txs[idx].get_str())) {
954+
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed for tx %d. Make sure the tx has at least one input.", idx));
955+
}
952956
}
953-
}
954-
955957
if (txVariants.empty()) {
956958
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Missing transactions");
957959
}

0 commit comments

Comments
 (0)