Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/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 <psbt.h>
6
7
#include <common/types.h>
8
#include <node/types.h>
9
#include <policy/policy.h>
10
#include <script/signingprovider.h>
11
#include <util/check.h>
12
#include <util/strencodings.h>
13
14
using common::PSBTError;
15
16
0
PartiallySignedTransaction::PartiallySignedTransaction(const CMutableTransaction& tx) : tx(tx)
17
0
{
18
0
    inputs.resize(tx.vin.size());
19
0
    outputs.resize(tx.vout.size());
20
0
}
21
22
bool PartiallySignedTransaction::IsNull() const
23
0
{
24
0
    return !tx && inputs.empty() && outputs.empty() && unknown.empty();
  Branch (24:12): [True: 0, False: 0]
  Branch (24:19): [True: 0, False: 0]
  Branch (24:37): [True: 0, False: 0]
  Branch (24:56): [True: 0, False: 0]
25
0
}
26
27
bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
28
0
{
29
    // Prohibited to merge two PSBTs over different transactions
30
0
    if (tx->GetHash() != psbt.tx->GetHash()) {
  Branch (30:9): [True: 0, False: 0]
31
0
        return false;
32
0
    }
33
34
0
    for (unsigned int i = 0; i < inputs.size(); ++i) {
  Branch (34:30): [True: 0, False: 0]
35
0
        inputs[i].Merge(psbt.inputs[i]);
36
0
    }
37
0
    for (unsigned int i = 0; i < outputs.size(); ++i) {
  Branch (37:30): [True: 0, False: 0]
38
0
        outputs[i].Merge(psbt.outputs[i]);
39
0
    }
40
0
    for (auto& xpub_pair : psbt.m_xpubs) {
  Branch (40:26): [True: 0, False: 0]
41
0
        if (m_xpubs.count(xpub_pair.first) == 0) {
  Branch (41:13): [True: 0, False: 0]
42
0
            m_xpubs[xpub_pair.first] = xpub_pair.second;
43
0
        } else {
44
0
            m_xpubs[xpub_pair.first].insert(xpub_pair.second.begin(), xpub_pair.second.end());
45
0
        }
46
0
    }
47
0
    unknown.insert(psbt.unknown.begin(), psbt.unknown.end());
48
49
0
    return true;
50
0
}
51
52
bool PartiallySignedTransaction::AddInput(const CTxIn& txin, PSBTInput& psbtin)
53
0
{
54
0
    if (std::find(tx->vin.begin(), tx->vin.end(), txin) != tx->vin.end()) {
  Branch (54:9): [True: 0, False: 0]
55
0
        return false;
56
0
    }
57
0
    tx->vin.push_back(txin);
58
0
    psbtin.partial_sigs.clear();
59
0
    psbtin.final_script_sig.clear();
60
0
    psbtin.final_script_witness.SetNull();
61
0
    inputs.push_back(psbtin);
62
0
    return true;
63
0
}
64
65
bool PartiallySignedTransaction::AddOutput(const CTxOut& txout, const PSBTOutput& psbtout)
66
0
{
67
0
    tx->vout.push_back(txout);
68
0
    outputs.push_back(psbtout);
69
0
    return true;
70
0
}
71
72
bool PartiallySignedTransaction::GetInputUTXO(CTxOut& utxo, int input_index) const
73
0
{
74
0
    const PSBTInput& input = inputs[input_index];
75
0
    uint32_t prevout_index = tx->vin[input_index].prevout.n;
76
0
    if (input.non_witness_utxo) {
  Branch (76:9): [True: 0, False: 0]
77
0
        if (prevout_index >= input.non_witness_utxo->vout.size()) {
  Branch (77:13): [True: 0, False: 0]
78
0
            return false;
79
0
        }
80
0
        if (input.non_witness_utxo->GetHash() != tx->vin[input_index].prevout.hash) {
  Branch (80:13): [True: 0, False: 0]
81
0
            return false;
82
0
        }
83
0
        utxo = input.non_witness_utxo->vout[prevout_index];
84
0
    } else if (!input.witness_utxo.IsNull()) {
  Branch (84:16): [True: 0, False: 0]
85
0
        utxo = input.witness_utxo;
86
0
    } else {
87
0
        return false;
88
0
    }
89
0
    return true;
90
0
}
91
92
bool PSBTInput::IsNull() const
93
0
{
94
0
    return !non_witness_utxo && witness_utxo.IsNull() && partial_sigs.empty() && unknown.empty() && hd_keypaths.empty() && redeem_script.empty() && witness_script.empty();
  Branch (94:12): [True: 0, False: 0]
  Branch (94:33): [True: 0, False: 0]
  Branch (94:58): [True: 0, False: 0]
  Branch (94:82): [True: 0, False: 0]
  Branch (94:101): [True: 0, False: 0]
  Branch (94:124): [True: 0, False: 0]
  Branch (94:149): [True: 0, False: 0]
95
0
}
96
97
void PSBTInput::FillSignatureData(SignatureData& sigdata) const
98
0
{
99
0
    if (!final_script_sig.empty()) {
  Branch (99:9): [True: 0, False: 0]
100
0
        sigdata.scriptSig = final_script_sig;
101
0
        sigdata.complete = true;
102
0
    }
103
0
    if (!final_script_witness.IsNull()) {
  Branch (103:9): [True: 0, False: 0]
104
0
        sigdata.scriptWitness = final_script_witness;
105
0
        sigdata.complete = true;
106
0
    }
107
0
    if (sigdata.complete) {
  Branch (107:9): [True: 0, False: 0]
108
0
        return;
109
0
    }
110
111
0
    sigdata.signatures.insert(partial_sigs.begin(), partial_sigs.end());
112
0
    if (!redeem_script.empty()) {
  Branch (112:9): [True: 0, False: 0]
113
0
        sigdata.redeem_script = redeem_script;
114
0
    }
115
0
    if (!witness_script.empty()) {
  Branch (115:9): [True: 0, False: 0]
116
0
        sigdata.witness_script = witness_script;
117
0
    }
118
0
    for (const auto& key_pair : hd_keypaths) {
  Branch (118:31): [True: 0, False: 0]
119
0
        sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
120
0
    }
121
0
    if (!m_tap_key_sig.empty()) {
  Branch (121:9): [True: 0, False: 0]
122
0
        sigdata.taproot_key_path_sig = m_tap_key_sig;
123
0
    }
124
0
    for (const auto& [pubkey_leaf, sig] : m_tap_script_sigs) {
  Branch (124:41): [True: 0, False: 0]
125
0
        sigdata.taproot_script_sigs.emplace(pubkey_leaf, sig);
126
0
    }
127
0
    if (!m_tap_internal_key.IsNull()) {
  Branch (127:9): [True: 0, False: 0]
128
0
        sigdata.tr_spenddata.internal_key = m_tap_internal_key;
129
0
    }
130
0
    if (!m_tap_merkle_root.IsNull()) {
  Branch (130:9): [True: 0, False: 0]
131
0
        sigdata.tr_spenddata.merkle_root = m_tap_merkle_root;
132
0
    }
133
0
    for (const auto& [leaf_script, control_block] : m_tap_scripts) {
  Branch (133:51): [True: 0, False: 0]
134
0
        sigdata.tr_spenddata.scripts.emplace(leaf_script, control_block);
135
0
    }
136
0
    for (const auto& [pubkey, leaf_origin] : m_tap_bip32_paths) {
  Branch (136:44): [True: 0, False: 0]
137
0
        sigdata.taproot_misc_pubkeys.emplace(pubkey, leaf_origin);
138
0
        sigdata.tap_pubkeys.emplace(Hash160(pubkey), pubkey);
139
0
    }
140
0
    for (const auto& [hash, preimage] : ripemd160_preimages) {
  Branch (140:39): [True: 0, False: 0]
141
0
        sigdata.ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
142
0
    }
143
0
    for (const auto& [hash, preimage] : sha256_preimages) {
  Branch (143:39): [True: 0, False: 0]
144
0
        sigdata.sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
145
0
    }
146
0
    for (const auto& [hash, preimage] : hash160_preimages) {
  Branch (146:39): [True: 0, False: 0]
147
0
        sigdata.hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
148
0
    }
149
0
    for (const auto& [hash, preimage] : hash256_preimages) {
  Branch (149:39): [True: 0, False: 0]
150
0
        sigdata.hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
151
0
    }
152
0
}
153
154
void PSBTInput::FromSignatureData(const SignatureData& sigdata)
155
0
{
156
0
    if (sigdata.complete) {
  Branch (156:9): [True: 0, False: 0]
157
0
        partial_sigs.clear();
158
0
        hd_keypaths.clear();
159
0
        redeem_script.clear();
160
0
        witness_script.clear();
161
162
0
        if (!sigdata.scriptSig.empty()) {
  Branch (162:13): [True: 0, False: 0]
163
0
            final_script_sig = sigdata.scriptSig;
164
0
        }
165
0
        if (!sigdata.scriptWitness.IsNull()) {
  Branch (165:13): [True: 0, False: 0]
166
0
            final_script_witness = sigdata.scriptWitness;
167
0
        }
168
0
        return;
169
0
    }
170
171
0
    partial_sigs.insert(sigdata.signatures.begin(), sigdata.signatures.end());
172
0
    if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
  Branch (172:9): [True: 0, False: 0]
  Branch (172:34): [True: 0, False: 0]
173
0
        redeem_script = sigdata.redeem_script;
174
0
    }
175
0
    if (witness_script.empty() && !sigdata.witness_script.empty()) {
  Branch (175:9): [True: 0, False: 0]
  Branch (175:35): [True: 0, False: 0]
176
0
        witness_script = sigdata.witness_script;
177
0
    }
178
0
    for (const auto& entry : sigdata.misc_pubkeys) {
  Branch (178:28): [True: 0, False: 0]
179
0
        hd_keypaths.emplace(entry.second);
180
0
    }
181
0
    if (!sigdata.taproot_key_path_sig.empty()) {
  Branch (181:9): [True: 0, False: 0]
182
0
        m_tap_key_sig = sigdata.taproot_key_path_sig;
183
0
    }
184
0
    for (const auto& [pubkey_leaf, sig] : sigdata.taproot_script_sigs) {
  Branch (184:41): [True: 0, False: 0]
185
0
        m_tap_script_sigs.emplace(pubkey_leaf, sig);
186
0
    }
187
0
    if (!sigdata.tr_spenddata.internal_key.IsNull()) {
  Branch (187:9): [True: 0, False: 0]
188
0
        m_tap_internal_key = sigdata.tr_spenddata.internal_key;
189
0
    }
190
0
    if (!sigdata.tr_spenddata.merkle_root.IsNull()) {
  Branch (190:9): [True: 0, False: 0]
191
0
        m_tap_merkle_root = sigdata.tr_spenddata.merkle_root;
192
0
    }
193
0
    for (const auto& [leaf_script, control_block] : sigdata.tr_spenddata.scripts) {
  Branch (193:51): [True: 0, False: 0]
194
0
        m_tap_scripts.emplace(leaf_script, control_block);
195
0
    }
196
0
    for (const auto& [pubkey, leaf_origin] : sigdata.taproot_misc_pubkeys) {
  Branch (196:44): [True: 0, False: 0]
197
0
        m_tap_bip32_paths.emplace(pubkey, leaf_origin);
198
0
    }
199
0
}
200
201
void PSBTInput::Merge(const PSBTInput& input)
202
0
{
203
0
    if (!non_witness_utxo && input.non_witness_utxo) non_witness_utxo = input.non_witness_utxo;
  Branch (203:9): [True: 0, False: 0]
  Branch (203:30): [True: 0, False: 0]
204
0
    if (witness_utxo.IsNull() && !input.witness_utxo.IsNull()) {
  Branch (204:9): [True: 0, False: 0]
  Branch (204:34): [True: 0, False: 0]
205
0
        witness_utxo = input.witness_utxo;
206
0
    }
207
208
0
    partial_sigs.insert(input.partial_sigs.begin(), input.partial_sigs.end());
209
0
    ripemd160_preimages.insert(input.ripemd160_preimages.begin(), input.ripemd160_preimages.end());
210
0
    sha256_preimages.insert(input.sha256_preimages.begin(), input.sha256_preimages.end());
211
0
    hash160_preimages.insert(input.hash160_preimages.begin(), input.hash160_preimages.end());
212
0
    hash256_preimages.insert(input.hash256_preimages.begin(), input.hash256_preimages.end());
213
0
    hd_keypaths.insert(input.hd_keypaths.begin(), input.hd_keypaths.end());
214
0
    unknown.insert(input.unknown.begin(), input.unknown.end());
215
0
    m_tap_script_sigs.insert(input.m_tap_script_sigs.begin(), input.m_tap_script_sigs.end());
216
0
    m_tap_scripts.insert(input.m_tap_scripts.begin(), input.m_tap_scripts.end());
217
0
    m_tap_bip32_paths.insert(input.m_tap_bip32_paths.begin(), input.m_tap_bip32_paths.end());
218
219
0
    if (redeem_script.empty() && !input.redeem_script.empty()) redeem_script = input.redeem_script;
  Branch (219:9): [True: 0, False: 0]
  Branch (219:34): [True: 0, False: 0]
220
0
    if (witness_script.empty() && !input.witness_script.empty()) witness_script = input.witness_script;
  Branch (220:9): [True: 0, False: 0]
  Branch (220:35): [True: 0, False: 0]
221
0
    if (final_script_sig.empty() && !input.final_script_sig.empty()) final_script_sig = input.final_script_sig;
  Branch (221:9): [True: 0, False: 0]
  Branch (221:37): [True: 0, False: 0]
222
0
    if (final_script_witness.IsNull() && !input.final_script_witness.IsNull()) final_script_witness = input.final_script_witness;
  Branch (222:9): [True: 0, False: 0]
  Branch (222:42): [True: 0, False: 0]
223
0
    if (m_tap_key_sig.empty() && !input.m_tap_key_sig.empty()) m_tap_key_sig = input.m_tap_key_sig;
  Branch (223:9): [True: 0, False: 0]
  Branch (223:34): [True: 0, False: 0]
224
0
    if (m_tap_internal_key.IsNull() && !input.m_tap_internal_key.IsNull()) m_tap_internal_key = input.m_tap_internal_key;
  Branch (224:9): [True: 0, False: 0]
  Branch (224:40): [True: 0, False: 0]
225
0
    if (m_tap_merkle_root.IsNull() && !input.m_tap_merkle_root.IsNull()) m_tap_merkle_root = input.m_tap_merkle_root;
  Branch (225:9): [True: 0, False: 0]
  Branch (225:39): [True: 0, False: 0]
226
0
}
227
228
void PSBTOutput::FillSignatureData(SignatureData& sigdata) const
229
0
{
230
0
    if (!redeem_script.empty()) {
  Branch (230:9): [True: 0, False: 0]
231
0
        sigdata.redeem_script = redeem_script;
232
0
    }
233
0
    if (!witness_script.empty()) {
  Branch (233:9): [True: 0, False: 0]
234
0
        sigdata.witness_script = witness_script;
235
0
    }
236
0
    for (const auto& key_pair : hd_keypaths) {
  Branch (236:31): [True: 0, False: 0]
237
0
        sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
238
0
    }
239
0
    if (!m_tap_tree.empty() && m_tap_internal_key.IsFullyValid()) {
  Branch (239:9): [True: 0, False: 0]
  Branch (239:32): [True: 0, False: 0]
240
0
        TaprootBuilder builder;
241
0
        for (const auto& [depth, leaf_ver, script] : m_tap_tree) {
  Branch (241:52): [True: 0, False: 0]
242
0
            builder.Add((int)depth, script, (int)leaf_ver, /*track=*/true);
243
0
        }
244
0
        assert(builder.IsComplete());
  Branch (244:9): [True: 0, False: 0]
245
0
        builder.Finalize(m_tap_internal_key);
246
0
        TaprootSpendData spenddata = builder.GetSpendData();
247
248
0
        sigdata.tr_spenddata.internal_key = m_tap_internal_key;
249
0
        sigdata.tr_spenddata.Merge(spenddata);
250
0
    }
251
0
    for (const auto& [pubkey, leaf_origin] : m_tap_bip32_paths) {
  Branch (251:44): [True: 0, False: 0]
252
0
        sigdata.taproot_misc_pubkeys.emplace(pubkey, leaf_origin);
253
0
        sigdata.tap_pubkeys.emplace(Hash160(pubkey), pubkey);
254
0
    }
255
0
}
256
257
void PSBTOutput::FromSignatureData(const SignatureData& sigdata)
258
0
{
259
0
    if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
  Branch (259:9): [True: 0, False: 0]
  Branch (259:34): [True: 0, False: 0]
260
0
        redeem_script = sigdata.redeem_script;
261
0
    }
262
0
    if (witness_script.empty() && !sigdata.witness_script.empty()) {
  Branch (262:9): [True: 0, False: 0]
  Branch (262:35): [True: 0, False: 0]
263
0
        witness_script = sigdata.witness_script;
264
0
    }
265
0
    for (const auto& entry : sigdata.misc_pubkeys) {
  Branch (265:28): [True: 0, False: 0]
266
0
        hd_keypaths.emplace(entry.second);
267
0
    }
268
0
    if (!sigdata.tr_spenddata.internal_key.IsNull()) {
  Branch (268:9): [True: 0, False: 0]
269
0
        m_tap_internal_key = sigdata.tr_spenddata.internal_key;
270
0
    }
271
0
    if (sigdata.tr_builder.has_value() && sigdata.tr_builder->HasScripts()) {
  Branch (271:9): [True: 0, False: 0]
  Branch (271:43): [True: 0, False: 0]
272
0
        m_tap_tree = sigdata.tr_builder->GetTreeTuples();
273
0
    }
274
0
    for (const auto& [pubkey, leaf_origin] : sigdata.taproot_misc_pubkeys) {
  Branch (274:44): [True: 0, False: 0]
275
0
        m_tap_bip32_paths.emplace(pubkey, leaf_origin);
276
0
    }
277
0
}
278
279
bool PSBTOutput::IsNull() const
280
0
{
281
0
    return redeem_script.empty() && witness_script.empty() && hd_keypaths.empty() && unknown.empty();
  Branch (281:12): [True: 0, False: 0]
  Branch (281:37): [True: 0, False: 0]
  Branch (281:63): [True: 0, False: 0]
  Branch (281:86): [True: 0, False: 0]
282
0
}
283
284
void PSBTOutput::Merge(const PSBTOutput& output)
285
0
{
286
0
    hd_keypaths.insert(output.hd_keypaths.begin(), output.hd_keypaths.end());
287
0
    unknown.insert(output.unknown.begin(), output.unknown.end());
288
0
    m_tap_bip32_paths.insert(output.m_tap_bip32_paths.begin(), output.m_tap_bip32_paths.end());
289
290
0
    if (redeem_script.empty() && !output.redeem_script.empty()) redeem_script = output.redeem_script;
  Branch (290:9): [True: 0, False: 0]
  Branch (290:34): [True: 0, False: 0]
291
0
    if (witness_script.empty() && !output.witness_script.empty()) witness_script = output.witness_script;
  Branch (291:9): [True: 0, False: 0]
  Branch (291:35): [True: 0, False: 0]
292
0
    if (m_tap_internal_key.IsNull() && !output.m_tap_internal_key.IsNull()) m_tap_internal_key = output.m_tap_internal_key;
  Branch (292:9): [True: 0, False: 0]
  Branch (292:40): [True: 0, False: 0]
293
0
    if (m_tap_tree.empty() && !output.m_tap_tree.empty()) m_tap_tree = output.m_tap_tree;
  Branch (293:9): [True: 0, False: 0]
  Branch (293:31): [True: 0, False: 0]
294
0
}
295
296
bool PSBTInputSigned(const PSBTInput& input)
297
0
{
298
0
    return !input.final_script_sig.empty() || !input.final_script_witness.IsNull();
  Branch (298:12): [True: 0, False: 0]
  Branch (298:47): [True: 0, False: 0]
299
0
}
300
301
bool PSBTInputSignedAndVerified(const PartiallySignedTransaction psbt, unsigned int input_index, const PrecomputedTransactionData* txdata)
302
0
{
303
0
    CTxOut utxo;
304
0
    assert(psbt.inputs.size() >= input_index);
  Branch (304:5): [True: 0, False: 0]
305
0
    const PSBTInput& input = psbt.inputs[input_index];
306
307
0
    if (input.non_witness_utxo) {
  Branch (307:9): [True: 0, False: 0]
308
        // If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
309
0
        COutPoint prevout = psbt.tx->vin[input_index].prevout;
310
0
        if (prevout.n >= input.non_witness_utxo->vout.size()) {
  Branch (310:13): [True: 0, False: 0]
311
0
            return false;
312
0
        }
313
0
        if (input.non_witness_utxo->GetHash() != prevout.hash) {
  Branch (313:13): [True: 0, False: 0]
314
0
            return false;
315
0
        }
316
0
        utxo = input.non_witness_utxo->vout[prevout.n];
317
0
    } else if (!input.witness_utxo.IsNull()) {
  Branch (317:16): [True: 0, False: 0]
318
0
        utxo = input.witness_utxo;
319
0
    } else {
320
0
        return false;
321
0
    }
322
323
0
    if (txdata) {
  Branch (323:9): [True: 0, False: 0]
324
0
        return VerifyScript(input.final_script_sig, utxo.scriptPubKey, &input.final_script_witness, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&(*psbt.tx), input_index, utxo.nValue, *txdata, MissingDataBehavior::FAIL});
325
0
    } else {
326
0
        return VerifyScript(input.final_script_sig, utxo.scriptPubKey, &input.final_script_witness, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&(*psbt.tx), input_index, utxo.nValue, MissingDataBehavior::FAIL});
327
0
    }
328
0
}
329
330
0
size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction& psbt) {
331
0
    size_t count = 0;
332
0
    for (const auto& input : psbt.inputs) {
  Branch (332:28): [True: 0, False: 0]
333
0
        if (!PSBTInputSigned(input)) {
  Branch (333:13): [True: 0, False: 0]
334
0
            count++;
335
0
        }
336
0
    }
337
338
0
    return count;
339
0
}
340
341
void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index)
342
0
{
343
0
    CMutableTransaction& tx = *Assert(psbt.tx);
344
0
    const CTxOut& out = tx.vout.at(index);
345
0
    PSBTOutput& psbt_out = psbt.outputs.at(index);
346
347
    // Fill a SignatureData with output info
348
0
    SignatureData sigdata;
349
0
    psbt_out.FillSignatureData(sigdata);
350
351
    // Construct a would-be spend of this output, to update sigdata with.
352
    // Note that ProduceSignature is used to fill in metadata (not actual signatures),
353
    // so provider does not need to provide any private keys (it can be a HidingSigningProvider).
354
0
    MutableTransactionSignatureCreator creator(tx, /*input_idx=*/0, out.nValue, SIGHASH_ALL);
355
0
    ProduceSignature(provider, creator, out.scriptPubKey, sigdata);
356
357
    // Put redeem_script, witness_script, key paths, into PSBTOutput.
358
0
    psbt_out.FromSignatureData(sigdata);
359
0
}
360
361
PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction& psbt)
362
0
{
363
0
    const CMutableTransaction& tx = *psbt.tx;
364
0
    bool have_all_spent_outputs = true;
365
0
    std::vector<CTxOut> utxos(tx.vin.size());
366
0
    for (size_t idx = 0; idx < tx.vin.size(); ++idx) {
  Branch (366:26): [True: 0, False: 0]
367
0
        if (!psbt.GetInputUTXO(utxos[idx], idx)) have_all_spent_outputs = false;
  Branch (367:13): [True: 0, False: 0]
368
0
    }
369
0
    PrecomputedTransactionData txdata;
370
0
    if (have_all_spent_outputs) {
  Branch (370:9): [True: 0, False: 0]
371
0
        txdata.Init(tx, std::move(utxos), true);
372
0
    } else {
373
0
        txdata.Init(tx, {}, true);
374
0
    }
375
0
    return txdata;
376
0
}
377
378
PSBTError SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, const PrecomputedTransactionData* txdata, std::optional<int> sighash,  SignatureData* out_sigdata, bool finalize)
379
0
{
380
0
    PSBTInput& input = psbt.inputs.at(index);
381
0
    const CMutableTransaction& tx = *psbt.tx;
382
383
0
    if (PSBTInputSignedAndVerified(psbt, index, txdata)) {
  Branch (383:9): [True: 0, False: 0]
384
0
        return PSBTError::OK;
385
0
    }
386
387
    // Fill SignatureData with input info
388
0
    SignatureData sigdata;
389
0
    input.FillSignatureData(sigdata);
390
391
    // Get UTXO
392
0
    bool require_witness_sig = false;
393
0
    CTxOut utxo;
394
395
0
    if (input.non_witness_utxo) {
  Branch (395:9): [True: 0, False: 0]
396
        // If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
397
0
        COutPoint prevout = tx.vin[index].prevout;
398
0
        if (prevout.n >= input.non_witness_utxo->vout.size()) {
  Branch (398:13): [True: 0, False: 0]
399
0
            return PSBTError::MISSING_INPUTS;
400
0
        }
401
0
        if (input.non_witness_utxo->GetHash() != prevout.hash) {
  Branch (401:13): [True: 0, False: 0]
402
0
            return PSBTError::MISSING_INPUTS;
403
0
        }
404
0
        utxo = input.non_witness_utxo->vout[prevout.n];
405
0
    } else if (!input.witness_utxo.IsNull()) {
  Branch (405:16): [True: 0, False: 0]
406
0
        utxo = input.witness_utxo;
407
        // When we're taking our information from a witness UTXO, we can't verify it is actually data from
408
        // the output being spent. This is safe in case a witness signature is produced (which includes this
409
        // information directly in the hash), but not for non-witness signatures. Remember that we require
410
        // a witness signature in this situation.
411
0
        require_witness_sig = true;
412
0
    } else {
413
0
        return PSBTError::MISSING_INPUTS;
414
0
    }
415
416
    // Get the sighash type
417
    // If both the field and the parameter are provided, they must match
418
    // If only the parameter is provided, use it and add it to the PSBT if it is other than SIGHASH_DEFAULT
419
    // for all input types, and not SIGHASH_ALL for non-taproot input types.
420
    // If neither are provided, use SIGHASH_DEFAULT if it is taproot, and SIGHASH_ALL for everything else.
421
0
    if (!sighash) sighash = utxo.scriptPubKey.IsPayToTaproot() ? SIGHASH_DEFAULT : SIGHASH_ALL;
  Branch (421:9): [True: 0, False: 0]
  Branch (421:29): [True: 0, False: 0]
422
0
    Assert(sighash.has_value());
423
    // For user safety, the desired sighash must be provided if the PSBT wants something other than the default set in the previous line.
424
0
    if (input.sighash_type && input.sighash_type != sighash) {
  Branch (424:9): [True: 0, False: 0]
  Branch (424:31): [True: 0, False: 0]
425
0
        return PSBTError::SIGHASH_MISMATCH;
426
0
    }
427
    // Set the PSBT sighash field when sighash is not DEFAULT or ALL
428
    // DEFAULT is allowed for non-taproot inputs since DEFAULT may be passed for them (e.g. the psbt being signed also has taproot inputs)
429
    // Note that signing already aliases DEFAULT to ALL for non-taproot inputs.
430
0
    if (utxo.scriptPubKey.IsPayToTaproot() ? sighash != SIGHASH_DEFAULT :
  Branch (430:9): [True: 0, False: 0]
  Branch (430:9): [True: 0, False: 0]
431
0
                                            (sighash != SIGHASH_DEFAULT && sighash != SIGHASH_ALL)) {
  Branch (431:46): [True: 0, False: 0]
  Branch (431:76): [True: 0, False: 0]
432
0
        input.sighash_type = sighash;
433
0
    }
434
435
    // Check all existing signatures use the sighash type
436
0
    if (sighash == SIGHASH_DEFAULT) {
  Branch (436:9): [True: 0, False: 0]
437
0
        if (!input.m_tap_key_sig.empty() && input.m_tap_key_sig.size() != 64) {
  Branch (437:13): [True: 0, False: 0]
  Branch (437:45): [True: 0, False: 0]
438
0
            return PSBTError::SIGHASH_MISMATCH;
439
0
        }
440
0
        for (const auto& [_, sig] : input.m_tap_script_sigs) {
  Branch (440:35): [True: 0, False: 0]
441
0
            if (sig.size() != 64) return PSBTError::SIGHASH_MISMATCH;
  Branch (441:17): [True: 0, False: 0]
442
0
        }
443
0
    } else {
444
0
        if (!input.m_tap_key_sig.empty() && (input.m_tap_key_sig.size() != 65 || input.m_tap_key_sig.back() != *sighash)) {
  Branch (444:13): [True: 0, False: 0]
  Branch (444:46): [True: 0, False: 0]
  Branch (444:82): [True: 0, False: 0]
445
0
            return PSBTError::SIGHASH_MISMATCH;
446
0
        }
447
0
        for (const auto& [_, sig] : input.m_tap_script_sigs) {
  Branch (447:35): [True: 0, False: 0]
448
0
            if (sig.size() != 65 || sig.back() != *sighash) return PSBTError::SIGHASH_MISMATCH;
  Branch (448:17): [True: 0, False: 0]
  Branch (448:37): [True: 0, False: 0]
449
0
        }
450
0
        for (const auto& [_, sig] : input.partial_sigs) {
  Branch (450:35): [True: 0, False: 0]
451
0
            if (sig.second.back() != *sighash) return PSBTError::SIGHASH_MISMATCH;
  Branch (451:17): [True: 0, False: 0]
452
0
        }
453
0
    }
454
455
0
    sigdata.witness = false;
456
0
    bool sig_complete;
457
0
    if (txdata == nullptr) {
  Branch (457:9): [True: 0, False: 0]
458
0
        sig_complete = ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, utxo.scriptPubKey, sigdata);
459
0
    } else {
460
0
        MutableTransactionSignatureCreator creator(tx, index, utxo.nValue, txdata, *sighash);
461
0
        sig_complete = ProduceSignature(provider, creator, utxo.scriptPubKey, sigdata);
462
0
    }
463
    // Verify that a witness signature was produced in case one was required.
464
0
    if (require_witness_sig && !sigdata.witness) return PSBTError::INCOMPLETE;
  Branch (464:9): [True: 0, False: 0]
  Branch (464:32): [True: 0, False: 0]
465
466
    // If we are not finalizing, set sigdata.complete to false to not set the scriptWitness
467
0
    if (!finalize && sigdata.complete) sigdata.complete = false;
  Branch (467:9): [True: 0, False: 0]
  Branch (467:22): [True: 0, False: 0]
468
469
0
    input.FromSignatureData(sigdata);
470
471
    // If we have a witness signature, put a witness UTXO.
472
0
    if (sigdata.witness) {
  Branch (472:9): [True: 0, False: 0]
473
0
        input.witness_utxo = utxo;
474
        // We can remove the non_witness_utxo if and only if there are no non-segwit or segwit v0
475
        // inputs in this transaction. Since this requires inspecting the entire transaction, this
476
        // is something for the caller to deal with (i.e. FillPSBT).
477
0
    }
478
479
    // Fill in the missing info
480
0
    if (out_sigdata) {
  Branch (480:9): [True: 0, False: 0]
481
0
        out_sigdata->missing_pubkeys = sigdata.missing_pubkeys;
482
0
        out_sigdata->missing_sigs = sigdata.missing_sigs;
483
0
        out_sigdata->missing_redeem_script = sigdata.missing_redeem_script;
484
0
        out_sigdata->missing_witness_script = sigdata.missing_witness_script;
485
0
    }
486
487
0
    return sig_complete ? PSBTError::OK : PSBTError::INCOMPLETE;
  Branch (487:12): [True: 0, False: 0]
488
0
}
489
490
void RemoveUnnecessaryTransactions(PartiallySignedTransaction& psbtx)
491
0
{
492
    // Figure out if any non_witness_utxos should be dropped
493
0
    std::vector<unsigned int> to_drop;
494
0
    for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
  Branch (494:30): [True: 0, False: 0]
495
0
        const auto& input = psbtx.inputs.at(i);
496
0
        int wit_ver;
497
0
        std::vector<unsigned char> wit_prog;
498
0
        if (input.witness_utxo.IsNull() || !input.witness_utxo.scriptPubKey.IsWitnessProgram(wit_ver, wit_prog)) {
  Branch (498:13): [True: 0, False: 0]
  Branch (498:44): [True: 0, False: 0]
499
            // There's a non-segwit input, so we cannot drop any non_witness_utxos
500
0
            to_drop.clear();
501
0
            break;
502
0
        }
503
0
        if (wit_ver == 0) {
  Branch (503:13): [True: 0, False: 0]
504
            // Segwit v0, so we cannot drop any non_witness_utxos
505
0
            to_drop.clear();
506
0
            break;
507
0
        }
508
        // non_witness_utxos cannot be dropped if the sighash type includes SIGHASH_ANYONECANPAY
509
        // Since callers should have called SignPSBTInput which updates the sighash type in the PSBT, we only
510
        // need to look at that field. If it is not present, then we can assume SIGHASH_DEFAULT or SIGHASH_ALL.
511
0
        if (input.sighash_type != std::nullopt && (*input.sighash_type & 0x80) == SIGHASH_ANYONECANPAY) {
  Branch (511:13): [True: 0, False: 0]
  Branch (511:51): [True: 0, False: 0]
512
0
            to_drop.clear();
513
0
            break;
514
0
        }
515
516
0
        if (input.non_witness_utxo) {
  Branch (516:13): [True: 0, False: 0]
517
0
            to_drop.push_back(i);
518
0
        }
519
0
    }
520
521
    // Drop the non_witness_utxos that we can drop
522
0
    for (unsigned int i : to_drop) {
  Branch (522:25): [True: 0, False: 0]
523
0
        psbtx.inputs.at(i).non_witness_utxo = nullptr;
524
0
    }
525
0
}
526
527
bool FinalizePSBT(PartiallySignedTransaction& psbtx)
528
0
{
529
    // Finalize input signatures -- in case we have partial signatures that add up to a complete
530
    //   signature, but have not combined them yet (e.g. because the combiner that created this
531
    //   PartiallySignedTransaction did not understand them), this will combine them into a final
532
    //   script.
533
0
    bool complete = true;
534
0
    const PrecomputedTransactionData txdata = PrecomputePSBTData(psbtx);
535
0
    for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
  Branch (535:30): [True: 0, False: 0]
536
0
        PSBTInput& input = psbtx.inputs.at(i);
537
0
        complete &= (SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, &txdata, input.sighash_type, nullptr, true) == PSBTError::OK);
538
0
    }
539
540
0
    return complete;
541
0
}
542
543
bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransaction& result)
544
0
{
545
    // It's not safe to extract a PSBT that isn't finalized, and there's no easy way to check
546
    //   whether a PSBT is finalized without finalizing it, so we just do this.
547
0
    if (!FinalizePSBT(psbtx)) {
  Branch (547:9): [True: 0, False: 0]
548
0
        return false;
549
0
    }
550
551
0
    result = *psbtx.tx;
552
0
    for (unsigned int i = 0; i < result.vin.size(); ++i) {
  Branch (552:30): [True: 0, False: 0]
553
0
        result.vin[i].scriptSig = psbtx.inputs[i].final_script_sig;
554
0
        result.vin[i].scriptWitness = psbtx.inputs[i].final_script_witness;
555
0
    }
556
0
    return true;
557
0
}
558
559
bool CombinePSBTs(PartiallySignedTransaction& out, const std::vector<PartiallySignedTransaction>& psbtxs)
560
0
{
561
0
    out = psbtxs[0]; // Copy the first one
562
563
    // Merge
564
0
    for (auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
  Branch (564:47): [True: 0, False: 0]
565
0
        if (!out.Merge(*it)) {
  Branch (565:13): [True: 0, False: 0]
566
0
            return false;
567
0
        }
568
0
    }
569
0
    return true;
570
0
}
571
572
0
std::string PSBTRoleName(PSBTRole role) {
573
0
    switch (role) {
  Branch (573:13): [True: 0, False: 0]
574
0
    case PSBTRole::CREATOR: return "creator";
  Branch (574:5): [True: 0, False: 0]
575
0
    case PSBTRole::UPDATER: return "updater";
  Branch (575:5): [True: 0, False: 0]
576
0
    case PSBTRole::SIGNER: return "signer";
  Branch (576:5): [True: 0, False: 0]
577
0
    case PSBTRole::FINALIZER: return "finalizer";
  Branch (577:5): [True: 0, False: 0]
578
0
    case PSBTRole::EXTRACTOR: return "extractor";
  Branch (578:5): [True: 0, False: 0]
579
        // no default case, so the compiler can warn about missing cases
580
0
    }
581
0
    assert(false);
  Branch (581:5): [Folded - Ignored]
582
0
}
583
584
bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, std::string& error)
585
0
{
586
0
    auto tx_data = DecodeBase64(base64_tx);
587
0
    if (!tx_data) {
  Branch (587:9): [True: 0, False: 0]
588
0
        error = "invalid base64";
589
0
        return false;
590
0
    }
591
0
    return DecodeRawPSBT(psbt, MakeByteSpan(*tx_data), error);
592
0
}
593
594
bool DecodeRawPSBT(PartiallySignedTransaction& psbt, std::span<const std::byte> tx_data, std::string& error)
595
0
{
596
0
    DataStream ss_data{tx_data};
597
0
    try {
598
0
        ss_data >> psbt;
599
0
        if (!ss_data.empty()) {
  Branch (599:13): [True: 0, False: 0]
600
0
            error = "extra data after PSBT";
601
0
            return false;
602
0
        }
603
0
    } catch (const std::exception& e) {
604
0
        error = e.what();
605
0
        return false;
606
0
    }
607
0
    return true;
608
0
}
609
610
uint32_t PartiallySignedTransaction::GetVersion() const
611
0
{
612
0
    if (m_version != std::nullopt) {
  Branch (612:9): [True: 0, False: 0]
613
0
        return *m_version;
614
0
    }
615
0
    return 0;
616
0
}