Branch data Line data Source code
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>
10 : : #include <test/fuzz/FuzzedDataProvider.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 : :
20 : : using node::AnalyzePSBT;
21 : : using node::PSBTAnalysis;
22 : : using node::PSBTInputAnalysis;
23 : :
24 [ + - ]: 6228 : FUZZ_TARGET(psbt)
25 : : {
26 : 5752 : SeedRandomStateForTest(SeedRand::ZEROS);
27 : 5752 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
28 : 5752 : auto str = fuzzed_data_provider.ConsumeRandomLengthString();
29 [ + - ]: 5752 : util::Result<PartiallySignedTransaction> psbt_res = DecodeRawPSBT(MakeByteSpan(str));
30 [ + + ]: 5752 : if (!psbt_res) {
31 : 1257 : return;
32 : : }
33 [ + - ]: 4495 : PartiallySignedTransaction psbt_mut = *psbt_res;
34 [ + - ]: 4495 : const PartiallySignedTransaction psbt = psbt_mut;
35 : :
36 : : // We are on purpose not forward compatible, and version 1 is disabled.
37 [ + - ]: 4495 : const auto psbt_version{psbt.GetVersion()};
38 [ - + ]: 4495 : Assert(psbt_version == 0 || psbt_version == 2);
39 : :
40 : : // A PSBT must roundtrip.
41 : 4495 : std::vector<uint8_t> psbt_ser;
42 [ + - ]: 4495 : VectorWriter{psbt_ser, 0, psbt};
43 [ - + ]: 4495 : SpanReader reader{psbt_ser};
44 [ + - ]: 4495 : PartiallySignedTransaction psbt_roundtrip(deserialize, reader);
45 : :
46 : : // And be stable across roundtrips.
47 : 4495 : std::vector<uint8_t> roundtrip_ser;
48 [ + - ]: 4495 : VectorWriter{roundtrip_ser, 0, psbt_roundtrip};
49 [ - + ]: 4495 : Assert(psbt_ser == roundtrip_ser);
50 : :
51 [ + - + - ]: 4495 : const PSBTAnalysis analysis = AnalyzePSBT(psbt);
52 [ + - ]: 4495 : (void)PSBTRoleName(analysis.next);
53 [ + + ]: 9818 : for (const PSBTInputAnalysis& input_analysis : analysis.inputs) {
54 [ + - ]: 10646 : (void)PSBTRoleName(input_analysis.next);
55 : : }
56 : :
57 [ + - ]: 4495 : (void)psbt.GetUnsignedTx();
58 : :
59 [ + + ]: 11943 : for (const PSBTInput& input : psbt.inputs) {
60 [ + - ]: 7448 : (void)PSBTInputSigned(input);
61 [ + - ]: 7448 : PSBTInput input_mod = input;
62 : 7448 : CTxOut tx_out;
63 [ + - + + ]: 7448 : if (input.GetUTXO(tx_out)) {
64 : 4690 : (void)tx_out.IsNull();
65 [ + - ]: 9380 : (void)tx_out.ToString();
66 : : }
67 : : // A PSBT input must roundtrip to signature data.
68 : 7448 : PSBTInput input_fill{psbt_version, input_mod.prev_txid, input_mod.prev_out, input_mod.sequence};
69 : 7448 : SignatureData sig_data;
70 [ + - ]: 7448 : input_mod.FillSignatureData(sig_data);
71 [ + - ]: 7448 : input_fill.FromSignatureData(sig_data);
72 : :
73 : : // Only final_script_sig and final_script_witness are filled when sigdata is complete
74 [ + + ]: 7448 : if (sig_data.complete) {
75 [ - + ]: 1648 : Assert(input_mod.final_script_sig == input_fill.final_script_sig);
76 [ - + ]: 3296 : Assert(input_mod.final_script_witness == input_fill.final_script_witness);
77 : : } else {
78 : : // UTXOs don't go into SignatureData
79 : 5800 : input_mod.non_witness_utxo.reset();
80 : 5800 : input_mod.witness_utxo.SetNull();
81 : : // Sighash type doesn't go into SignatureData
82 [ + + ]: 5800 : input_mod.sighash_type.reset();
83 : : // Timelocks don't go into SignatureData
84 [ - + ]: 5800 : input_mod.time_locktime.reset();
85 [ - + ]: 5800 : input_mod.height_locktime.reset();
86 : : // Proprietary fields are not included in SignatureData
87 : 5800 : input_mod.m_proprietary.clear();
88 : : // Unknown fields are not included in SignatureData
89 : 5800 : input_mod.unknown.clear();
90 : :
91 [ - + ]: 5800 : Assert(input_mod == input_fill);
92 : : }
93 : 7448 : }
94 [ + - ]: 4495 : (void)CountPSBTUnsignedInputs(psbt);
95 : :
96 [ + + ]: 12180 : for (const PSBTOutput& output : psbt.outputs) {
97 [ + - ]: 7685 : PSBTOutput output_mod = output;
98 : : // A PSBT output must roundtrip to signature data.
99 : 7685 : PSBTOutput output_fill{psbt_version, output_mod.amount, output_mod.script};
100 : 7685 : SignatureData sig_data;
101 [ + - ]: 7685 : output_mod.FillSignatureData(sig_data);
102 [ + - ]: 7685 : 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 [ + + + - : 7685 : if (output_mod.m_tap_tree.empty() || !output_mod.m_tap_internal_key.IsFullyValid()) {
+ + ]
107 : 7260 : output_mod.m_tap_tree.clear();
108 : 7260 : 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 : 7685 : std::sort(output_mod.m_tap_tree.begin(), output_mod.m_tap_tree.end());
112 : 7685 : std::sort(output_fill.m_tap_tree.begin(), output_fill.m_tap_tree.end());
113 : : // Proprietary fields are not included in SignatureData
114 : 7685 : output_mod.m_proprietary.clear();
115 : : // Unknown fields are not included in SignatureData
116 : 7685 : output_mod.unknown.clear();
117 : :
118 [ - + ]: 7685 : Assert(output_mod.m_tap_internal_key == output_fill.m_tap_internal_key);
119 [ - + ]: 7685 : Assert(output_mod == output_fill);
120 : 7685 : }
121 : :
122 [ + - ]: 4495 : psbt_mut = psbt;
123 [ + - ]: 4495 : (void)FinalizePSBT(psbt_mut);
124 : :
125 [ + - ]: 4495 : psbt_mut = psbt;
126 [ + - ]: 4495 : CMutableTransaction result;
127 [ + - + + ]: 4495 : if (FinalizeAndExtractPSBT(psbt_mut, result)) {
128 [ + - ]: 909 : const PartiallySignedTransaction psbt_from_tx{result};
129 : 909 : }
130 : :
131 [ + - ]: 4495 : PartiallySignedTransaction psbt_merge = psbt;
132 [ + - ]: 4495 : str = fuzzed_data_provider.ConsumeRandomLengthString();
133 [ + - ]: 4495 : util::Result<PartiallySignedTransaction> psbt_merge_res = DecodeRawPSBT(MakeByteSpan(str));
134 [ + + ]: 4495 : if (psbt_merge_res) {
135 [ + - ]: 731 : psbt_merge = *psbt_merge_res;
136 : : }
137 [ + - ]: 4495 : psbt_mut = psbt;
138 [ + - ]: 4495 : (void)psbt_mut.Merge(psbt_merge);
139 [ + - ]: 4495 : psbt_mut = psbt;
140 [ + - + - : 13485 : std::optional<PartiallySignedTransaction> comb_res = CombinePSBTs({psbt_mut, psbt_merge});
+ + - - ]
141 [ + + ]: 4495 : if (comb_res) {
142 [ + - ]: 4196 : psbt_mut = *comb_res;
143 : : }
144 [ + + ]: 12030 : for (const auto& psbt_in : psbt_merge.inputs) {
145 [ + - ]: 7535 : (void)psbt_mut.AddInput(psbt_in);
146 : : }
147 [ + + ]: 12403 : for (const auto& psbt_out : psbt_merge.outputs) {
148 [ + - ]: 7908 : (void)psbt_mut.AddOutput(psbt_out);
149 : : }
150 [ + - ]: 4495 : psbt_mut.unknown.insert(psbt_merge.unknown.begin(), psbt_merge.unknown.end());
151 : :
152 [ + - ]: 4495 : RemoveUnnecessaryTransactions(psbt_mut);
153 [ + - + - : 19237 : }
- - ]
|