Coverage Report

Created: 2026-09-14 20:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/node/psbt.cpp
Line
Count
Source
1
// Copyright (c) 2009-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 <coins.h>
6
#include <consensus/amount.h>
7
#include <consensus/tx_verify.h>
8
#include <node/psbt.h>
9
#include <policy/policy.h>
10
#include <policy/settings.h>
11
#include <tinyformat.h>
12
13
#include <numeric>
14
15
namespace node {
16
PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx)
17
11
{
18
    // Go through each input and build status
19
11
    PSBTAnalysis result;
20
21
11
    std::optional<CMutableTransaction> unsigned_tx = psbtx.GetUnsignedTx();
22
11
    if (!unsigned_tx) {
23
0
        result.SetInvalid("PSBT cannot be made into a valid transaction");
24
0
        return result;
25
0
    }
26
11
    CMutableTransaction& mtx = *unsigned_tx;
27
28
11
    bool calc_fee = true;
29
30
11
    CAmount in_amt = 0;
31
32
11
    result.inputs.resize(psbtx.inputs.size());
33
34
    // PrecomputePSBTData calls GetUnsignedTx() which we checked already works
35
11
    const PrecomputedTransactionData txdata = *PrecomputePSBTData(psbtx);
36
37
21
    for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
38
12
        PSBTInput& input = psbtx.inputs[i];
39
12
        PSBTInputAnalysis& input_analysis = result.inputs[i];
40
41
        // We set next role here and ratchet backwards as required
42
12
        input_analysis.next = PSBTRole::EXTRACTOR;
43
44
        // Check for a UTXO
45
12
        CTxOut utxo;
46
12
        if (input.GetUTXO(utxo)) {
47
9
            if (!MoneyRange(utxo.nValue) || !MoneyRange(in_amt + utxo.nValue)) {
48
1
                result.SetInvalid(strprintf("PSBT is not valid. Input %u has invalid value", i));
49
1
                return result;
50
1
            }
51
8
            in_amt += utxo.nValue;
52
8
            input_analysis.has_utxo = true;
53
8
        } else {
54
3
            if (input.non_witness_utxo && input.prev_out >= input.non_witness_utxo->vout.size()) {
55
0
                result.SetInvalid(strprintf("PSBT is not valid. Input %u specifies invalid prevout", i));
56
0
                return result;
57
0
            }
58
3
            input_analysis.has_utxo = false;
59
3
            input_analysis.is_final = false;
60
3
            input_analysis.next = PSBTRole::UPDATER;
61
3
            calc_fee = false;
62
3
        }
63
64
11
        if (!utxo.IsNull() && utxo.scriptPubKey.IsUnspendable()) {
65
1
            result.SetInvalid(strprintf("PSBT is not valid. Input %u spends unspendable output", i));
66
1
            return result;
67
1
        }
68
69
        // Check if it is final
70
10
        if (!PSBTInputSignedAndVerified(psbtx, i, &txdata)) {
71
9
            input_analysis.is_final = false;
72
73
            // Figure out what is missing
74
9
            SignatureData outdata;
75
9
            const auto sign_result = SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, &txdata, /*options*/{}, &outdata);
76
9
            bool complete = sign_result.has_value();
77
78
            // Things are missing
79
9
            if (!complete) {
80
7
                input_analysis.missing_pubkeys = outdata.missing_pubkeys;
81
7
                input_analysis.missing_redeem_script = outdata.missing_redeem_script;
82
7
                input_analysis.missing_witness_script = outdata.missing_witness_script;
83
7
                input_analysis.missing_sigs = outdata.missing_sigs;
84
85
                // If we are only missing signatures and nothing else, then next is signer
86
7
                if (outdata.missing_pubkeys.empty() && outdata.missing_redeem_script.IsNull() && outdata.missing_witness_script.IsNull() && !outdata.missing_sigs.empty()) {
87
1
                    input_analysis.next = PSBTRole::SIGNER;
88
6
                } else {
89
6
                    input_analysis.next = PSBTRole::UPDATER;
90
6
                }
91
7
            } else {
92
2
                input_analysis.next = PSBTRole::FINALIZER;
93
2
            }
94
9
        } else if (!utxo.IsNull()){
95
1
            input_analysis.is_final = true;
96
1
        }
97
10
    }
98
99
    // Calculate next role for PSBT by grabbing "minimum" PSBTInput next role
100
9
    result.next = PSBTRole::EXTRACTOR;
101
19
    for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
102
10
        PSBTInputAnalysis& input_analysis = result.inputs[i];
103
10
        result.next = std::min(result.next, input_analysis.next);
104
10
    }
105
9
    assert(result.next > PSBTRole::CREATOR);
106
107
9
    if (calc_fee) {
108
        // Get the output amount
109
7
        CAmount out_amt = std::accumulate(psbtx.outputs.begin(), psbtx.outputs.end(), CAmount(0),
110
10
            [](CAmount a, const PSBTOutput& b) {
111
10
                if (!MoneyRange(a) || !MoneyRange(b.amount) || !MoneyRange(a + b.amount)) {
112
2
                    return CAmount(-1);
113
2
                }
114
8
                return a += b.amount;
115
10
            }
116
7
        );
117
7
        if (!MoneyRange(out_amt)) {
118
1
            result.SetInvalid("PSBT is not valid. Output amount invalid");
119
1
            return result;
120
1
        }
121
122
        // Get the fee
123
6
        CAmount fee = in_amt - out_amt;
124
6
        result.fee = fee;
125
126
        // Estimate the size
127
6
        CCoinsViewCache view{&CoinsViewEmpty::Get()};
128
6
        bool success = true;
129
130
12
        for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
131
6
            PSBTInput& input = psbtx.inputs[i];
132
6
            Coin newcoin;
133
134
6
            const auto sign_result = SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, nullptr, /*options=*/{});
135
6
            if (!sign_result.has_value() || !input.GetUTXO(newcoin.out)) {
136
0
                success = false;
137
0
                break;
138
6
            } else {
139
6
                mtx.vin[i].scriptSig = input.final_script_sig;
140
6
                mtx.vin[i].scriptWitness = input.final_script_witness;
141
6
                newcoin.nHeight = 1;
142
6
                view.AddCoin(input.GetOutPoint(), std::move(newcoin), true);
143
6
            }
144
6
        }
145
146
6
        if (success) {
147
6
            CTransaction ctx = CTransaction(mtx);
148
6
            size_t size(GetVirtualTransactionSize(ctx, GetTransactionSigOpCost(ctx, view, STANDARD_SCRIPT_VERIFY_FLAGS), ::nBytesPerSigOp));
149
6
            result.estimated_vsize = size;
150
            // Estimate fee rate
151
6
            CFeeRate feerate(fee, size);
152
6
            result.estimated_feerate = feerate;
153
6
        }
154
155
6
    }
156
157
8
    return result;
158
9
}
159
} // namespace node