Bitcoin Core  31.0.0
P2P Digital Currency
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 
20 using node::AnalyzePSBT;
21 using node::PSBTAnalysis;
23 
25 {
27  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
29  std::string error;
31  if (!DecodeRawPSBT(psbt_mut, MakeByteSpan(str), error)) {
32  return;
33  }
34  const PartiallySignedTransaction psbt = psbt_mut;
35 
36  // A PSBT must roundtrip.
37  PartiallySignedTransaction psbt_roundtrip;
38  std::vector<uint8_t> psbt_ser;
39  VectorWriter{psbt_ser, 0, psbt};
40  SpanReader{psbt_ser} >> psbt_roundtrip;
41 
42  // And be stable across roundtrips.
43  std::vector<uint8_t> roundtrip_ser;
44  VectorWriter{roundtrip_ser, 0, psbt_roundtrip};
45  Assert(psbt_ser == roundtrip_ser);
46 
47  const PSBTAnalysis analysis = AnalyzePSBT(psbt);
48  (void)PSBTRoleName(analysis.next);
49  for (const PSBTInputAnalysis& input_analysis : analysis.inputs) {
50  (void)PSBTRoleName(input_analysis.next);
51  }
52 
53  (void)psbt.IsNull();
54 
55  std::optional<CMutableTransaction> tx = psbt.tx;
56  if (tx) {
57  const CMutableTransaction& mtx = *tx;
58  const PartiallySignedTransaction psbt_from_tx{mtx};
59  }
60 
61  for (const PSBTInput& input : psbt.inputs) {
62  (void)PSBTInputSigned(input);
63  (void)input.IsNull();
64  }
65  (void)CountPSBTUnsignedInputs(psbt);
66 
67  for (const PSBTOutput& output : psbt.outputs) {
68  (void)output.IsNull();
69  }
70 
71  for (size_t i = 0; i < psbt.tx->vin.size(); ++i) {
72  CTxOut tx_out;
73  if (psbt.GetInputUTXO(tx_out, i)) {
74  (void)tx_out.IsNull();
75  (void)tx_out.ToString();
76  }
77  }
78 
79  psbt_mut = psbt;
80  (void)FinalizePSBT(psbt_mut);
81 
82  psbt_mut = psbt;
84  if (FinalizeAndExtractPSBT(psbt_mut, result)) {
85  const PartiallySignedTransaction psbt_from_tx{result};
86  }
87 
88  PartiallySignedTransaction psbt_merge;
90  if (!DecodeRawPSBT(psbt_merge, MakeByteSpan(str), error)) {
91  psbt_merge = psbt;
92  }
93  psbt_mut = psbt;
94  (void)psbt_mut.Merge(psbt_merge);
95  psbt_mut = psbt;
96  (void)CombinePSBTs(psbt_mut, {psbt_mut, psbt_merge});
97  psbt_mut = psbt;
98  for (unsigned int i = 0; i < psbt_merge.tx->vin.size(); ++i) {
99  (void)psbt_mut.AddInput(psbt_merge.tx->vin[i], psbt_merge.inputs[i]);
100  }
101  for (unsigned int i = 0; i < psbt_merge.tx->vout.size(); ++i) {
102  Assert(psbt_mut.AddOutput(psbt_merge.tx->vout[i], psbt_merge.outputs[i]));
103  }
104  psbt_mut.unknown.insert(psbt_merge.unknown.begin(), psbt_merge.unknown.end());
105 }
bool AddInput(const CTxIn &txin, PSBTInput &psbtin)
Definition: psbt.cpp:52
bool IsNull() const
Definition: psbt.cpp:302
auto MakeByteSpan(const V &v) noexcept
Definition: span.h:84
FUZZ_TARGET(psbt)
Definition: psbt.cpp:24
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
Definition: psbt.cpp:551
A version of CTransaction with the PSBT format.
Definition: psbt.h:1138
std::string ToString() const
Definition: transaction.cpp:61
bool 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:583
PSBTRole next
Which of the BIP 174 roles needs to handle this input next.
Definition: psbt.h:19
bool AddOutput(const CTxOut &txout, const PSBTOutput &psbtout)
Definition: psbt.cpp:65
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:567
bool IsNull() const
Definition: transaction.h:160
std::string PSBTRoleName(PSBTRole role)
Definition: psbt.cpp:596
Minimal stream for reading from an existing byte array by std::span.
Definition: streams.h:82
A structure for PSBTs which contains per output information.
Definition: psbt.h:877
std::vector< PSBTOutput > outputs
Definition: psbt.h:1145
std::string ConsumeRandomLengthString(size_t max_length)
bool IsNull() const
Definition: psbt.cpp:22
bool DecodeRawPSBT(PartiallySignedTransaction &psbt, std::span< const std::byte > tx_data, std::string &error)
Decode a raw (binary blob) PSBT into a PartiallySignedTransaction.
Definition: psbt.cpp:618
A structure for PSBTs which contain per-input information.
Definition: psbt.h:261
An output of a transaction.
Definition: transaction.h:139
Holds an analysis of one input from a PSBT.
Definition: psbt.h:16
std::vector< PSBTInput > inputs
Definition: psbt.h:1144
std::vector< PSBTInputAnalysis > inputs
More information about the individual inputs of the transaction.
Definition: psbt.h:34
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction &psbt)
Counts the unsigned inputs of a PSBT.
Definition: psbt.cpp:354
bool Merge(const PartiallySignedTransaction &psbt)
Merge psbt into this.
Definition: psbt.cpp:27
auto result
Definition: common-types.h:74
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:38
Holds the results of AnalyzePSBT (miscellaneous information about a PSBT)
Definition: psbt.h:30
A mutable version of CTransaction.
Definition: transaction.h:357
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed by checking for non-null finalized fields.
Definition: psbt.cpp:320
std::optional< CMutableTransaction > tx
Definition: psbt.h:1140
Seed with a compile time constant of zeros.
PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx)
Provides helpful miscellaneous information about where a PSBT is in the signing workflow.
Definition: psbt.cpp:16
bool GetInputUTXO(CTxOut &utxo, int input_index) const
Finds the UTXO for a given input index.
Definition: psbt.cpp:72
PSBTRole next
Which of the BIP 174 roles needs to handle the transaction next.
Definition: psbt.h:35
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
Definition: psbt.h:1146
#define Assert(val)
Identity function.
Definition: check.h:113
bool IsNull() const
Definition: psbt.cpp:92