Bitcoin Core 28.99.0
P2P Digital Currency
psbt.cpp
Go to the documentation of this file.
1// Copyright (c) 2019-2022 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()};
29 std::string error;
30 auto str = fuzzed_data_provider.ConsumeRandomLengthString();
31 if (!DecodeRawPSBT(psbt_mut, MakeByteSpan(str), error)) {
32 return;
33 }
34 const PartiallySignedTransaction psbt = psbt_mut;
35
36 const PSBTAnalysis analysis = AnalyzePSBT(psbt);
37 (void)PSBTRoleName(analysis.next);
38 for (const PSBTInputAnalysis& input_analysis : analysis.inputs) {
39 (void)PSBTRoleName(input_analysis.next);
40 }
41
42 (void)psbt.IsNull();
43
44 std::optional<CMutableTransaction> tx = psbt.tx;
45 if (tx) {
46 const CMutableTransaction& mtx = *tx;
47 const PartiallySignedTransaction psbt_from_tx{mtx};
48 }
49
50 for (const PSBTInput& input : psbt.inputs) {
51 (void)PSBTInputSigned(input);
52 (void)input.IsNull();
53 }
54 (void)CountPSBTUnsignedInputs(psbt);
55
56 for (const PSBTOutput& output : psbt.outputs) {
57 (void)output.IsNull();
58 }
59
60 for (size_t i = 0; i < psbt.tx->vin.size(); ++i) {
61 CTxOut tx_out;
62 if (psbt.GetInputUTXO(tx_out, i)) {
63 (void)tx_out.IsNull();
64 (void)tx_out.ToString();
65 }
66 }
67
68 psbt_mut = psbt;
69 (void)FinalizePSBT(psbt_mut);
70
71 psbt_mut = psbt;
73 if (FinalizeAndExtractPSBT(psbt_mut, result)) {
74 const PartiallySignedTransaction psbt_from_tx{result};
75 }
76
78 str = fuzzed_data_provider.ConsumeRandomLengthString();
79 if (!DecodeRawPSBT(psbt_merge, MakeByteSpan(str), error)) {
80 psbt_merge = psbt;
81 }
82 psbt_mut = psbt;
83 (void)psbt_mut.Merge(psbt_merge);
84 psbt_mut = psbt;
85 (void)CombinePSBTs(psbt_mut, {psbt_mut, psbt_merge});
86 psbt_mut = psbt;
87 for (unsigned int i = 0; i < psbt_merge.tx->vin.size(); ++i) {
88 (void)psbt_mut.AddInput(psbt_merge.tx->vin[i], psbt_merge.inputs[i]);
89 }
90 for (unsigned int i = 0; i < psbt_merge.tx->vout.size(); ++i) {
91 Assert(psbt_mut.AddOutput(psbt_merge.tx->vout[i], psbt_merge.outputs[i]));
92 }
93 psbt_mut.unknown.insert(psbt_merge.unknown.begin(), psbt_merge.unknown.end());
94}
#define Assert(val)
Identity function.
Definition: check.h:85
An output of a transaction.
Definition: transaction.h:150
bool IsNull() const
Definition: transaction.h:170
std::string ToString() const
Definition: transaction.cpp:61
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:524
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:511
size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction &psbt)
Counts the unsigned inputs of a PSBT.
Definition: psbt.cpp:327
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:495
bool DecodeRawPSBT(PartiallySignedTransaction &psbt, Span< const std::byte > tx_data, std::string &error)
Decode a raw (binary blob) PSBT into a PartiallySignedTransaction.
Definition: psbt.cpp:546
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed by checking for non-null finalized fields.
Definition: psbt.cpp:293
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
Definition: psbt.cpp:480
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition: span.h:270
A mutable version of CTransaction.
Definition: transaction.h:378
A structure for PSBTs which contain per-input information.
Definition: psbt.h:198
bool IsNull() const
Definition: psbt.cpp:89
A structure for PSBTs which contains per output information.
Definition: psbt.h:715
bool IsNull() const
Definition: psbt.cpp:276
A version of CTransaction with the PSBT format.
Definition: psbt.h:951
bool Merge(const PartiallySignedTransaction &psbt)
Merge psbt into this.
Definition: psbt.cpp:24
bool IsNull() const
Definition: psbt.cpp:19
bool GetInputUTXO(CTxOut &utxo, int input_index) const
Finds the UTXO for a given input index.
Definition: psbt.cpp:69
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
Definition: psbt.h:958
bool AddOutput(const CTxOut &txout, const PSBTOutput &psbtout)
Definition: psbt.cpp:62
std::vector< PSBTInput > inputs
Definition: psbt.h:956
std::optional< CMutableTransaction > tx
Definition: psbt.h:952
bool AddInput(const CTxIn &txin, PSBTInput &psbtin)
Definition: psbt.cpp:49
std::vector< PSBTOutput > outputs
Definition: psbt.h:957
Holds the results of AnalyzePSBT (miscellaneous information about a PSBT)
Definition: psbt.h:30
std::vector< PSBTInputAnalysis > inputs
More information about the individual inputs of the transaction.
Definition: psbt.h:34
PSBTRole next
Which of the BIP 174 roles needs to handle the transaction next.
Definition: psbt.h:35
Holds an analysis of one input from a PSBT.
Definition: psbt.h:16
PSBTRole next
Which of the BIP 174 roles needs to handle this input next.
Definition: psbt.h:19
FUZZ_TARGET(psbt)
Definition: psbt.cpp:24
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.