mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-10 20:03:34 -03:00
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: ACK3333070208
theStack: Code-review ACK3333070208
Tree-SHA512: 49db7bc614d2491cd3ec0177d21ad1e9924dbece1eb5635290cd7fd18cb30adf4711b891daf522e7c4f6baab3033b66393bbfcd1d4726f24f90a433124f925d6
This commit is contained in:
commit
0bd7ca9a03
1 changed files with 31 additions and 32 deletions
|
@ -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)) {
|
||||
|
|
Loading…
Reference in a new issue