Merge bitcoin/bitcoin#23642: refactor: Call type-solver earlier in decodescript

3333070208 refactor: Call type-solver earlier in decodescript (MarcoFalke)
fab0d998f4 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 3333070208
  theStack:
    Code-review ACK 3333070208

Tree-SHA512: 49db7bc614d2491cd3ec0177d21ad1e9924dbece1eb5635290cd7fd18cb30adf4711b891daf522e7c4f6baab3033b66393bbfcd1d4726f24f90a433124f925d6
This commit is contained in:
MarcoFalke 2021-12-02 13:56:51 +01:00
commit 0bd7ca9a03
No known key found for this signature in database
GPG key ID: CE2B75697E69A548

View file

@ -540,32 +540,33 @@ static std::string GetAllOutputTypes()
static RPCHelpMan decodescript() static RPCHelpMan decodescript()
{ {
return RPCHelpMan{"decodescript", return RPCHelpMan{
"\nDecode a hex-encoded script.\n", "decodescript",
{ "\nDecode a hex-encoded script.\n",
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded script"}, {
}, {"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded script"},
RPCResult{ },
RPCResult::Type::OBJ, "", "", RPCResult{
{ RPCResult::Type::OBJ, "", "",
{RPCResult::Type::STR, "asm", "Script public key"}, {
{RPCResult::Type::STR, "type", "The output type (e.g. "+GetAllOutputTypes()+")"}, {RPCResult::Type::STR, "asm", "Script public key"},
{RPCResult::Type::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"}, {RPCResult::Type::STR, "type", "The output type (e.g. " + GetAllOutputTypes() + ")"},
{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::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"},
{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, "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, "asm", "String representation 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_HEX, "hex", "Hex string of the script public key"},
{RPCResult::Type::STR, "address", /* optional */ true, "The Bitcoin address (only if a well-defined address exists)"}, {RPCResult::Type::STR, "type", "The type of the script public key (e.g. witness_v0_keyhash or witness_v0_scripthash)"},
{RPCResult::Type::STR, "p2sh-segwit", "address of the P2SH script wrapping this witness redeem script"}, {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\"") RPCExamples{
+ HelpExampleRpc("decodescript", "\"hexstring\"") HelpExampleCli("decodescript", "\"hexstring\"")
}, + HelpExampleRpc("decodescript", "\"hexstring\"")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{ {
RPCTypeCheck(request.params, {UniValue::VSTR}); RPCTypeCheck(request.params, {UniValue::VSTR});
@ -580,18 +581,16 @@ static RPCHelpMan decodescript()
} }
ScriptPubKeyToUniv(script, r, /* include_hex */ false); ScriptPubKeyToUniv(script, r, /* include_hex */ false);
UniValue type; std::vector<std::vector<unsigned char>> solutions_data;
type = find_value(r, "type"); 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, // 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. // don't return the address for a P2SH of the P2SH.
r.pushKV("p2sh", EncodeDestination(ScriptHash(script))); r.pushKV("p2sh", EncodeDestination(ScriptHash(script)));
// P2SH and witness programs cannot be wrapped in P2WSH, if this 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. // 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") { if (which_type == TxoutType::PUBKEY || which_type == TxoutType::PUBKEYHASH || which_type == TxoutType::MULTISIG || which_type == TxoutType::NONSTANDARD) {
std::vector<std::vector<unsigned char>> solutions_data;
TxoutType which_type = Solver(script, solutions_data);
// Uncompressed pubkeys cannot be used with segwit checksigs. // Uncompressed pubkeys cannot be used with segwit checksigs.
// If the script contains an uncompressed pubkey, skip encoding of a segwit program. // If the script contains an uncompressed pubkey, skip encoding of a segwit program.
if ((which_type == TxoutType::PUBKEY) || (which_type == TxoutType::MULTISIG)) { if ((which_type == TxoutType::PUBKEY) || (which_type == TxoutType::MULTISIG)) {