mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-08 10:31:50 -05:00
refactor: merge ScriptPubKeyToUniv & ScriptToUniv into one function
This commit is contained in:
parent
74b011bbfa
commit
828a094ecf
6 changed files with 17 additions and 23 deletions
|
@ -53,8 +53,7 @@ UniValue ValueFromAmount(const CAmount amount);
|
|||
std::string FormatScript(const CScript& script);
|
||||
std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags = 0);
|
||||
std::string SighashToStr(unsigned char sighash_type);
|
||||
void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool include_hex, bool include_address = true);
|
||||
void ScriptToUniv(const CScript& script, UniValue& out);
|
||||
void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex = true, bool include_address = false);
|
||||
void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex = true, int serialize_flags = 0, const CTxUndo* txundo = nullptr, TxVerbosity verbosity = TxVerbosity::SHOW_DETAILS);
|
||||
|
||||
#endif // BITCOIN_CORE_IO_H
|
||||
|
|
|
@ -143,25 +143,22 @@ std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags)
|
|||
return HexStr(ssTx);
|
||||
}
|
||||
|
||||
void ScriptToUniv(const CScript& script, UniValue& out)
|
||||
{
|
||||
ScriptPubKeyToUniv(script, out, /* include_hex */ true, /* include_address */ false);
|
||||
}
|
||||
|
||||
void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool include_hex, bool include_address)
|
||||
void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address)
|
||||
{
|
||||
CTxDestination address;
|
||||
|
||||
out.pushKV("asm", ScriptToAsmStr(scriptPubKey));
|
||||
out.pushKV("asm", ScriptToAsmStr(script));
|
||||
if (include_address) {
|
||||
out.pushKV("desc", InferDescriptor(scriptPubKey, DUMMY_SIGNING_PROVIDER)->ToString());
|
||||
out.pushKV("desc", InferDescriptor(script, DUMMY_SIGNING_PROVIDER)->ToString());
|
||||
}
|
||||
if (include_hex) {
|
||||
out.pushKV("hex", HexStr(script));
|
||||
}
|
||||
if (include_hex) out.pushKV("hex", HexStr(scriptPubKey));
|
||||
|
||||
std::vector<std::vector<unsigned char>> solns;
|
||||
const TxoutType type{Solver(scriptPubKey, solns)};
|
||||
const TxoutType type{Solver(script, solns)};
|
||||
|
||||
if (include_address && ExtractDestination(scriptPubKey, address) && type != TxoutType::PUBKEY) {
|
||||
if (include_address && ExtractDestination(script, address) && type != TxoutType::PUBKEY) {
|
||||
out.pushKV("address", EncodeDestination(address));
|
||||
}
|
||||
out.pushKV("type", GetTxnOutputType(type));
|
||||
|
@ -215,7 +212,7 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
|
|||
|
||||
if (verbosity == TxVerbosity::SHOW_DETAILS_AND_PREVOUT) {
|
||||
UniValue o_script_pub_key(UniValue::VOBJ);
|
||||
ScriptPubKeyToUniv(prev_txout.scriptPubKey, o_script_pub_key, /*include_hex=*/ true);
|
||||
ScriptToUniv(prev_txout.scriptPubKey, /*out=*/o_script_pub_key, /*include_hex=*/true, /*include_address=*/true);
|
||||
|
||||
UniValue p(UniValue::VOBJ);
|
||||
p.pushKV("generated", bool(prev_coin.fCoinBase));
|
||||
|
@ -240,7 +237,7 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
|
|||
out.pushKV("n", (int64_t)i);
|
||||
|
||||
UniValue o(UniValue::VOBJ);
|
||||
ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
|
||||
ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
|
||||
out.pushKV("scriptPubKey", o);
|
||||
vout.push_back(out);
|
||||
|
||||
|
|
|
@ -855,7 +855,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
|
|||
|
||||
// include the script in a json output
|
||||
UniValue o(UniValue::VOBJ);
|
||||
ScriptPubKeyToUniv(coin.out.scriptPubKey, o, true);
|
||||
ScriptToUniv(coin.out.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
|
||||
utxo.pushKV("scriptPubKey", o);
|
||||
utxos.push_back(utxo);
|
||||
}
|
||||
|
|
|
@ -1026,7 +1026,7 @@ static RPCHelpMan gettxout()
|
|||
}
|
||||
ret.pushKV("value", ValueFromAmount(coin.out.nValue));
|
||||
UniValue o(UniValue::VOBJ);
|
||||
ScriptPubKeyToUniv(coin.out.scriptPubKey, o, true);
|
||||
ScriptToUniv(coin.out.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
|
||||
ret.pushKV("scriptPubKey", o);
|
||||
ret.pushKV("coinbase", (bool)coin.fCoinBase);
|
||||
|
||||
|
|
|
@ -435,7 +435,7 @@ static RPCHelpMan decodescript()
|
|||
} else {
|
||||
// Empty scripts are valid
|
||||
}
|
||||
ScriptPubKeyToUniv(script, r, /* include_hex */ false);
|
||||
ScriptToUniv(script, /*out=*/r, /*include_hex=*/false, /*include_address=*/true);
|
||||
|
||||
std::vector<std::vector<unsigned char>> solutions_data;
|
||||
const TxoutType which_type{Solver(script, solutions_data)};
|
||||
|
@ -512,7 +512,7 @@ static RPCHelpMan decodescript()
|
|||
// Scripts that are not fit for P2WPKH are encoded as P2WSH.
|
||||
segwitScr = GetScriptForDestination(WitnessV0ScriptHash(script));
|
||||
}
|
||||
ScriptPubKeyToUniv(segwitScr, sr, /* include_hex */ true);
|
||||
ScriptToUniv(segwitScr, /*out=*/sr, /*include_hex=*/true, /*include_address=*/true);
|
||||
sr.pushKV("p2sh-segwit", EncodeDestination(ScriptHash(segwitScr)));
|
||||
r.pushKV("segwit", sr);
|
||||
}
|
||||
|
@ -956,7 +956,7 @@ static RPCHelpMan decodepsbt()
|
|||
txout = input.witness_utxo;
|
||||
|
||||
UniValue o(UniValue::VOBJ);
|
||||
ScriptPubKeyToUniv(txout.scriptPubKey, o, /* include_hex */ true);
|
||||
ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
|
||||
|
||||
UniValue out(UniValue::VOBJ);
|
||||
out.pushKV("amount", ValueFromAmount(txout.nValue));
|
||||
|
|
|
@ -29,7 +29,5 @@ FUZZ_TARGET_INIT(script_format, initialize_script_format)
|
|||
(void)ScriptToAsmStr(script, /*fAttemptSighashDecode=*/fuzzed_data_provider.ConsumeBool());
|
||||
|
||||
UniValue o1(UniValue::VOBJ);
|
||||
ScriptPubKeyToUniv(script, o1, /*include_hex=*/fuzzed_data_provider.ConsumeBool());
|
||||
UniValue o3(UniValue::VOBJ);
|
||||
ScriptToUniv(script, o3);
|
||||
ScriptToUniv(script, /*out=*/o1, /*include_hex=*/fuzzed_data_provider.ConsumeBool(), /*include_address=*/fuzzed_data_provider.ConsumeBool());
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue