diff options
author | Michael Dietz <michael.dietz@waya.ai> | 2021-05-10 15:14:16 -0400 |
---|---|---|
committer | Michael Dietz <michael.dietz@waya.ai> | 2021-09-24 14:22:53 -0500 |
commit | d64deac7b823a0eba97ab3a3686054eefe330d3c (patch) | |
tree | 64d3de6c88d7e33b77f79bfae9a4dfe939e5d68c /src | |
parent | 8721638daa8502c7f8de5ae24a9393d7290a2ce5 (diff) | |
download | bitcoin-d64deac7b823a0eba97ab3a3686054eefe330d3c.tar.xz |
refactor: share logic between ScriptPubKeyToUniv and ScriptToUniv
Diffstat (limited to 'src')
-rw-r--r-- | src/core_io.h | 4 | ||||
-rw-r--r-- | src/core_write.cpp | 20 | ||||
-rw-r--r-- | src/rpc/rawtransaction.cpp | 10 | ||||
-rw-r--r-- | src/test/fuzz/script.cpp | 4 |
4 files changed, 13 insertions, 25 deletions
diff --git a/src/core_io.h b/src/core_io.h index 01340ae2ee..dec0297637 100644 --- a/src/core_io.h +++ b/src/core_io.h @@ -44,8 +44,8 @@ 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 fIncludeHex); -void ScriptToUniv(const CScript& script, UniValue& out, bool include_address); +void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex, bool include_address = true); +void ScriptToUniv(const CScript& script, UniValue& out); void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex = true, int serialize_flags = 0, const CTxUndo* txundo = nullptr); #endif // BITCOIN_CORE_IO_H diff --git a/src/core_write.cpp b/src/core_write.cpp index 4850ef969f..aa560806cd 100644 --- a/src/core_write.cpp +++ b/src/core_write.cpp @@ -141,22 +141,12 @@ std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags) return HexStr(ssTx); } -void ScriptToUniv(const CScript& script, UniValue& out, bool include_address) +void ScriptToUniv(const CScript& script, UniValue& out) { - out.pushKV("asm", ScriptToAsmStr(script)); - out.pushKV("hex", HexStr(script)); - - std::vector<std::vector<unsigned char>> solns; - TxoutType type = Solver(script, solns); - out.pushKV("type", GetTxnOutputType(type)); - - CTxDestination address; - if (include_address && ExtractDestination(script, address) && type != TxoutType::PUBKEY) { - out.pushKV("address", EncodeDestination(address)); - } + ScriptPubKeyToUniv(script, out, /* fIncludeHex */ true, /* include_address */ false); } -void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex) +void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex, bool include_address) { CTxDestination address; @@ -165,9 +155,9 @@ void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool fInclud out.pushKV("hex", HexStr(scriptPubKey)); std::vector<std::vector<unsigned char>> solns; - TxoutType type = Solver(scriptPubKey, solns); + const TxoutType type{Solver(scriptPubKey, solns)}; - if (ExtractDestination(scriptPubKey, address) && type != TxoutType::PUBKEY) { + if (include_address && ExtractDestination(scriptPubKey, address) && type != TxoutType::PUBKEY) { out.pushKV("address", EncodeDestination(address)); } out.pushKV("type", GetTxnOutputType(type)); diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index d01ff6110c..7d21c4f59b 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -1161,7 +1161,7 @@ static RPCHelpMan decodepsbt() txout = input.witness_utxo; UniValue o(UniValue::VOBJ); - ScriptToUniv(txout.scriptPubKey, o, true); + ScriptPubKeyToUniv(txout.scriptPubKey, o, /* fIncludeHex */ true); UniValue out(UniValue::VOBJ); out.pushKV("amount", ValueFromAmount(txout.nValue)); @@ -1208,12 +1208,12 @@ static RPCHelpMan decodepsbt() // Redeem script and witness script if (!input.redeem_script.empty()) { UniValue r(UniValue::VOBJ); - ScriptToUniv(input.redeem_script, r, false); + ScriptToUniv(input.redeem_script, r); in.pushKV("redeem_script", r); } if (!input.witness_script.empty()) { UniValue r(UniValue::VOBJ); - ScriptToUniv(input.witness_script, r, false); + ScriptToUniv(input.witness_script, r); in.pushKV("witness_script", r); } @@ -1268,12 +1268,12 @@ static RPCHelpMan decodepsbt() // Redeem script and witness script if (!output.redeem_script.empty()) { UniValue r(UniValue::VOBJ); - ScriptToUniv(output.redeem_script, r, false); + ScriptToUniv(output.redeem_script, r); out.pushKV("redeem_script", r); } if (!output.witness_script.empty()) { UniValue r(UniValue::VOBJ); - ScriptToUniv(output.witness_script, r, false); + ScriptToUniv(output.witness_script, r); out.pushKV("witness_script", r); } diff --git a/src/test/fuzz/script.cpp b/src/test/fuzz/script.cpp index bf699be325..74c576322a 100644 --- a/src/test/fuzz/script.cpp +++ b/src/test/fuzz/script.cpp @@ -113,9 +113,7 @@ FUZZ_TARGET_INIT(script, initialize_script) UniValue o2(UniValue::VOBJ); ScriptPubKeyToUniv(script, o2, false); UniValue o3(UniValue::VOBJ); - ScriptToUniv(script, o3, true); - UniValue o4(UniValue::VOBJ); - ScriptToUniv(script, o4, false); + ScriptToUniv(script, o3); { const std::vector<uint8_t> bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider); |