Coverage Report

Created: 2025-03-27 15:35

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