Bitcoin Core  22.99.0
P2P Digital Currency
psbt.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2021 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 
6 #include <test/fuzz/fuzz.h>
7 
8 #include <node/psbt.h>
9 #include <psbt.h>
10 #include <pubkey.h>
11 #include <script/script.h>
12 #include <streams.h>
13 #include <util/check.h>
14 #include <version.h>
15 
16 #include <cstdint>
17 #include <optional>
18 #include <string>
19 #include <vector>
20 
21 using node::AnalyzePSBT;
22 using node::PSBTAnalysis;
24 
26 {
27  static const ECCVerifyHandle verify_handle;
28 }
29 
31 {
32  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
34  std::string error;
35  if (!DecodeRawPSBT(psbt_mut, fuzzed_data_provider.ConsumeRandomLengthString(), error)) {
36  return;
37  }
38  const PartiallySignedTransaction psbt = psbt_mut;
39 
40  const PSBTAnalysis analysis = AnalyzePSBT(psbt);
41  (void)PSBTRoleName(analysis.next);
42  for (const PSBTInputAnalysis& input_analysis : analysis.inputs) {
43  (void)PSBTRoleName(input_analysis.next);
44  }
45 
46  (void)psbt.IsNull();
47 
48  std::optional<CMutableTransaction> tx = psbt.tx;
49  if (tx) {
50  const CMutableTransaction& mtx = *tx;
51  const PartiallySignedTransaction psbt_from_tx{mtx};
52  }
53 
54  for (const PSBTInput& input : psbt.inputs) {
55  (void)PSBTInputSigned(input);
56  (void)input.IsNull();
57  }
58  (void)CountPSBTUnsignedInputs(psbt);
59 
60  for (const PSBTOutput& output : psbt.outputs) {
61  (void)output.IsNull();
62  }
63 
64  for (size_t i = 0; i < psbt.tx->vin.size(); ++i) {
65  CTxOut tx_out;
66  if (psbt.GetInputUTXO(tx_out, i)) {
67  (void)tx_out.IsNull();
68  (void)tx_out.ToString();
69  }
70  }
71 
72  psbt_mut = psbt;
73  (void)FinalizePSBT(psbt_mut);
74 
75  psbt_mut = psbt;
76  CMutableTransaction result;
77  if (FinalizeAndExtractPSBT(psbt_mut, result)) {
78  const PartiallySignedTransaction psbt_from_tx{result};
79  }
80 
81  PartiallySignedTransaction psbt_merge;
82  if (!DecodeRawPSBT(psbt_merge, fuzzed_data_provider.ConsumeRandomLengthString(), error)) {
83  psbt_merge = psbt;
84  }
85  psbt_mut = psbt;
86  (void)psbt_mut.Merge(psbt_merge);
87  psbt_mut = psbt;
88  (void)CombinePSBTs(psbt_mut, {psbt_mut, psbt_merge});
89  psbt_mut = psbt;
90  for (unsigned int i = 0; i < psbt_merge.tx->vin.size(); ++i) {
91  (void)psbt_mut.AddInput(psbt_merge.tx->vin[i], psbt_merge.inputs[i]);
92  }
93  for (unsigned int i = 0; i < psbt_merge.tx->vout.size(); ++i) {
94  Assert(psbt_mut.AddOutput(psbt_merge.tx->vout[i], psbt_merge.outputs[i]));
95  }
96  psbt_mut.unknown.insert(psbt_merge.unknown.begin(), psbt_merge.unknown.end());
97 }
CombinePSBTs
TransactionError CombinePSBTs(PartiallySignedTransaction &out, const std::vector< PartiallySignedTransaction > &psbtxs)
Combines PSBTs with the same underlying transaction, resulting in a single PSBT with all partial sign...
Definition: psbt.cpp:364
FinalizePSBT
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
Definition: psbt.cpp:333
check.h
psbt.h
streams.h
PartiallySignedTransaction::IsNull
bool IsNull() const
Definition: psbt.cpp:17
PartiallySignedTransaction::inputs
std::vector< PSBTInput > inputs
Definition: psbt.h:674
PartiallySignedTransaction::AddInput
bool AddInput(const CTxIn &txin, PSBTInput &psbtin)
Definition: psbt.cpp:47
FinalizeAndExtractPSBT
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:348
pubkey.h
version.h
Assert
#define Assert(val)
Identity function.
Definition: check.h:57
CTxOut::IsNull
bool IsNull() const
Definition: transaction.h:149
node::AnalyzePSBT
PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx)
Provides helpful miscellaneous information about where a PSBT is in the signing workflow.
Definition: psbt.cpp:16
node::PSBTAnalysis
Holds the results of AnalyzePSBT (miscellaneous information about a PSBT)
Definition: psbt.h:30
PartiallySignedTransaction::Merge
bool Merge(const PartiallySignedTransaction &psbt)
Merge psbt into this.
Definition: psbt.cpp:22
CTxOut
An output of a transaction.
Definition: transaction.h:128
PartiallySignedTransaction::GetInputUTXO
bool GetInputUTXO(CTxOut &utxo, int input_index) const
Finds the UTXO for a given input index.
Definition: psbt.cpp:67
node::PSBTAnalysis::inputs
std::vector< PSBTInputAnalysis > inputs
More information about the individual inputs of the transaction.
Definition: psbt.h:34
PSBTOutput::IsNull
bool IsNull() const
Definition: psbt.cpp:195
FuzzedDataProvider.h
PartiallySignedTransaction::tx
std::optional< CMutableTransaction > tx
Definition: psbt.h:670
PSBTOutput
A structure for PSBTs which contains per output information.
Definition: psbt.h:531
PartiallySignedTransaction::AddOutput
bool AddOutput(const CTxOut &txout, const PSBTOutput &psbtout)
Definition: psbt.cpp:60
script.h
PSBTRoleName
std::string PSBTRoleName(PSBTRole role)
Definition: psbt.cpp:377
DecodeRawPSBT
bool DecodeRawPSBT(PartiallySignedTransaction &psbt, const std::string &tx_data, std::string &error)
Decode a raw (binary blob) PSBT into a PartiallySignedTransaction.
Definition: psbt.cpp:400
CountPSBTUnsignedInputs
size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction &psbt)
Counts the unsigned inputs of a PSBT.
Definition: psbt.cpp:213
node::PSBTInputAnalysis::next
PSBTRole next
Which of the BIP 174 roles needs to handle this input next.
Definition: psbt.h:19
PSBTInput
A structure for PSBTs which contain per-input information.
Definition: psbt.h:168
ECCVerifyHandle
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:332
fuzz.h
PartiallySignedTransaction
A version of CTransaction with the PSBT format.
Definition: psbt.h:668
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
CTxOut::ToString
std::string ToString() const
Definition: transaction.cpp:55
PartiallySignedTransaction::outputs
std::vector< PSBTOutput > outputs
Definition: psbt.h:675
PSBTInputSigned
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed.
Definition: psbt.cpp:208
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
PSBTInput::IsNull
bool IsNull() const
Definition: psbt.cpp:87
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
initialize_psbt
void initialize_psbt()
Definition: psbt.cpp:25
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(psbt, initialize_psbt)
Definition: psbt.cpp:30
node::PSBTInputAnalysis
Holds an analysis of one input from a PSBT.
Definition: psbt.h:16
node::PSBTAnalysis::next
PSBTRole next
Which of the BIP 174 roles needs to handle the transaction next.
Definition: psbt.h:35
PartiallySignedTransaction::unknown
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
Definition: psbt.h:676