Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
psbt.cpp
Go to the documentation of this file.
1// Copyright (c) 2019-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <node/psbt.h>
6#include <psbt.h>
7#include <pubkey.h>
8#include <script/script.h>
9#include <streams.h>
11#include <test/fuzz/fuzz.h>
12#include <test/util/random.h>
13#include <util/check.h>
14
15#include <cstdint>
16#include <optional>
17#include <string>
18#include <vector>
19
23
25{
27 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
30 if (!psbt_res) {
31 return;
32 }
35
36 // We are on purpose not forward compatible, and version 1 is disabled.
37 const auto psbt_version{psbt.GetVersion()};
38 Assert(psbt_version == 0 || psbt_version == 2);
39
40 // A PSBT must roundtrip.
41 std::vector<uint8_t> psbt_ser;
45
46 // And be stable across roundtrips.
47 std::vector<uint8_t> roundtrip_ser;
50
51 const PSBTAnalysis analysis = AnalyzePSBT(psbt);
53 for (const PSBTInputAnalysis& input_analysis : analysis.inputs) {
55 }
56
57 (void)psbt.GetUnsignedTx();
58
59 for (const PSBTInput& input : psbt.inputs) {
60 (void)PSBTInputSigned(input);
61 PSBTInput input_mod = input;
63 if (input.GetUTXO(tx_out)) {
65 (void)tx_out.ToString();
66 }
67 // A PSBT input must roundtrip to signature data.
70 input_mod.FillSignatureData(sig_data);
71 input_fill.FromSignatureData(sig_data);
72
73 // Only final_script_sig and final_script_witness are filled when sigdata is complete
74 if (sig_data.complete) {
75 Assert(input_mod.final_script_sig == input_fill.final_script_sig);
76 Assert(input_mod.final_script_witness == input_fill.final_script_witness);
77 } else {
78 // UTXOs don't go into SignatureData
79 input_mod.non_witness_utxo.reset();
80 input_mod.witness_utxo.SetNull();
81 // Sighash type doesn't go into SignatureData
82 input_mod.sighash_type.reset();
83 // Timelocks don't go into SignatureData
84 input_mod.time_locktime.reset();
85 input_mod.height_locktime.reset();
86 // Proprietary fields are not included in SignatureData
87 input_mod.m_proprietary.clear();
88 // Unknown fields are not included in SignatureData
89 input_mod.unknown.clear();
90
92 }
93 }
95
96 for (const PSBTOutput& output : psbt.outputs) {
97 PSBTOutput output_mod = output;
98 // A PSBT output must roundtrip to signature data.
101 output_mod.FillSignatureData(sig_data);
102 output_fill.FromSignatureData(sig_data);
103
104 // FillSignatureData will not fill tap tree or internal key if the tree is empty or
105 // the key is not fully valid. These need to be cleared before checking for equivalence
106 if (output_mod.m_tap_tree.empty() || !output_mod.m_tap_internal_key.IsFullyValid()) {
107 output_mod.m_tap_tree.clear();
108 std::fill(output_mod.m_tap_internal_key.begin(), output_mod.m_tap_internal_key.end(), 0);
109 }
110 // Sort m_tap_tree to ensure the vectors match
111 std::sort(output_mod.m_tap_tree.begin(), output_mod.m_tap_tree.end());
112 std::sort(output_fill.m_tap_tree.begin(), output_fill.m_tap_tree.end());
113 // Proprietary fields are not included in SignatureData
114 output_mod.m_proprietary.clear();
115 // Unknown fields are not included in SignatureData
116 output_mod.unknown.clear();
117
118 Assert(output_mod.m_tap_internal_key == output_fill.m_tap_internal_key);
120 }
121
122 psbt_mut = psbt;
124
125 psbt_mut = psbt;
126 CMutableTransaction result;
127 if (FinalizeAndExtractPSBT(psbt_mut, result)) {
129 }
130
134 if (psbt_merge_res) {
136 }
137 psbt_mut = psbt;
138 (void)psbt_mut.Merge(psbt_merge);
139 psbt_mut = psbt;
140 std::optional<PartiallySignedTransaction> comb_res = CombinePSBTs({psbt_mut, psbt_merge});
141 if (comb_res) {
143 }
144 for (const auto& psbt_in : psbt_merge.inputs) {
145 (void)psbt_mut.AddInput(psbt_in);
146 }
147 for (const auto& psbt_out : psbt_merge.outputs) {
148 (void)psbt_mut.AddOutput(psbt_out);
149 }
150 psbt_mut.unknown.insert(psbt_merge.unknown.begin(), psbt_merge.unknown.end());
151
153}
#define Assert(val)
Identity function.
Definition check.h:119
An output of a transaction.
bool IsNull() const
std::string ConsumeRandomLengthString(size_t max_length)
A structure for PSBTs which contain per-input information.
Definition psbt.h:282
Txid prev_txid
Definition psbt.h:300
A structure for PSBTs which contains per output information.
Definition psbt.h:939
CAmount amount
Definition psbt.h:956
A version of CTransaction with the PSBT format.
Definition psbt.h:1239
uint32_t GetVersion() const
Definition psbt.cpp:885
Minimal stream for reading from an existing byte array by std::span.
Definition streams.h:83
#define FUZZ_TARGET(...)
Definition fuzz.h:35
util::LineReader reader
PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx)
Provides helpful miscellaneous information about where a PSBT is in the signing workflow.
Definition psbt.cpp:16
std::string PSBTRoleName(PSBTRole role)
Definition psbt.cpp:851
std::optional< PartiallySignedTransaction > CombinePSBTs(const std::vector< PartiallySignedTransaction > &psbtxs)
Combines PSBTs with the same underlying transaction, resulting in a single PSBT with all partial sign...
Definition psbt.cpp:838
void RemoveUnnecessaryTransactions(PartiallySignedTransaction &psbtx)
Reduces the size of the PSBT by dropping unnecessary non_witness_utxos (i.e.
Definition psbt.cpp:760
size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction &psbt)
Counts the unsigned inputs of a PSBT.
Definition psbt.cpp:586
bool FinalizeAndExtractPSBT(PartiallySignedTransaction &psbtx, CMutableTransaction &result)
Finalizes a PSBT if possible, and extracts it to a CMutableTransaction if it could be finalized.
Definition psbt.cpp:818
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed by checking for non-null finalized fields.
Definition psbt.cpp:547
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
Definition psbt.cpp:797
util::Result< PartiallySignedTransaction > DecodeRawPSBT(std::span< const std::byte > tx_data)
Decode a raw (binary blob) PSBT into a PartiallySignedTransaction.
Definition psbt.cpp:871
constexpr deserialize_type deserialize
Definition serialize.h:52
auto MakeByteSpan(const V &v) noexcept
Definition span.h:84
A mutable version of CTransaction.
Holds the results of AnalyzePSBT (miscellaneous information about a PSBT)
Definition psbt.h:30
Holds an analysis of one input from a PSBT.
Definition psbt.h:16
SeedRandomStateForTest(SeedRand::ZEROS)
@ ZEROS
Seed with a compile time constant of zeros.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
FuzzedDataProvider & fuzzed_data_provider
Definition fees.cpp:46