Coverage Report

Created: 2026-06-01 18:35

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 <primitives/transaction.h>
11
#include <script/signingprovider.h>
12
#include <util/check.h>
13
#include <util/result.h>
14
#include <util/strencodings.h>
15
16
using common::PSBTError;
17
18
0
PartiallySignedTransaction::PartiallySignedTransaction(const CMutableTransaction& tx, uint32_t version) : m_version(version)
19
0
{
20
0
    assert(m_version == 0 || m_version == 2);
  Branch (20:5): [True: 0, False: 0]
  Branch (20:5): [True: 0, False: 0]
  Branch (20:5): [True: 0, False: 0]
21
22
0
    tx_version = tx.version;
23
0
    fallback_locktime = tx.nLockTime;
24
0
    inputs.reserve(tx.vin.size());
25
0
    for (const CTxIn& input : tx.vin) {
  Branch (25:29): [True: 0, False: 0]
26
0
        inputs.emplace_back(GetVersion(), input.prevout.hash, input.prevout.n, input.nSequence);
27
0
    }
28
0
    outputs.reserve(tx.vout.size());
29
0
    for (const CTxOut& output : tx.vout) {
  Branch (29:31): [True: 0, False: 0]
30
0
        outputs.emplace_back(GetVersion(), output.nValue, output.scriptPubKey);
31
0
    }
32
0
}
33
34
bool PartiallySignedTransaction::IsNull() const
35
0
{
36
0
    return inputs.empty() && outputs.empty() && unknown.empty();
  Branch (36:12): [True: 0, False: 0]
  Branch (36:30): [True: 0, False: 0]
  Branch (36:49): [True: 0, False: 0]
37
0
}
38
39
bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
40
0
{
41
    // Prohibited to merge two PSBTs over different transactions
42
0
    std::optional<Txid> this_id = GetUniqueID();
43
0
    std::optional<Txid> psbt_id = psbt.GetUniqueID();
44
0
    if (!this_id || !psbt_id || this_id != psbt_id) {
  Branch (44:9): [True: 0, False: 0]
  Branch (44:21): [True: 0, False: 0]
  Branch (44:33): [True: 0, False: 0]
45
0
        return false;
46
0
    }
47
0
    if (GetVersion() != psbt.GetVersion()) {
  Branch (47:9): [True: 0, False: 0]
48
0
        return false;
49
0
    }
50
51
0
    for (unsigned int i = 0; i < inputs.size(); ++i) {
  Branch (51:30): [True: 0, False: 0]
52
0
        if (!inputs[i].Merge(psbt.inputs[i])) {
  Branch (52:13): [True: 0, False: 0]
53
0
            return false;
54
0
        }
55
0
    }
56
0
    for (unsigned int i = 0; i < outputs.size(); ++i) {
  Branch (56:30): [True: 0, False: 0]
57
0
        if (!outputs[i].Merge(psbt.outputs[i])) {
  Branch (57:13): [True: 0, False: 0]
58
0
            return false;
59
0
        }
60
0
    }
61
0
    for (auto& xpub_pair : psbt.m_xpubs) {
  Branch (61:26): [True: 0, False: 0]
62
0
        if (!m_xpubs.contains(xpub_pair.first)) {
  Branch (62:13): [True: 0, False: 0]
63
0
            m_xpubs[xpub_pair.first] = xpub_pair.second;
64
0
        } else {
65
0
            m_xpubs[xpub_pair.first].insert(xpub_pair.second.begin(), xpub_pair.second.end());
66
0
        }
67
0
    }
68
0
    if (fallback_locktime == std::nullopt && psbt.fallback_locktime != std::nullopt) fallback_locktime = psbt.fallback_locktime;
  Branch (68:9): [True: 0, False: 0]
  Branch (68:46): [True: 0, False: 0]
69
70
    // Set m_tx_modifiable only if either PSBT had it set
71
0
    if (m_tx_modifiable.has_value() || psbt.m_tx_modifiable.has_value()) {
  Branch (71:9): [True: 0, False: 0]
  Branch (71:40): [True: 0, False: 0]
72
        // In general, we AND the modifiable flags
73
0
        std::bitset<8> this_modifiable = m_tx_modifiable.value_or(0);
74
0
        std::bitset<8> psbt_modifiable = psbt.m_tx_modifiable.value_or(0);
75
0
        std::bitset<8> final_modifiable = this_modifiable & psbt_modifiable;
76
        // SIGHASH_SINGLE Modifiable (bit 2) needs to be bitwise OR'd
77
0
        final_modifiable.set(2, this_modifiable[2] || psbt_modifiable[2]);
  Branch (77:33): [True: 0, False: 0]
  Branch (77:55): [True: 0, False: 0]
78
79
0
        m_tx_modifiable = final_modifiable;
80
0
    }
81
82
0
    m_proprietary.insert(psbt.m_proprietary.begin(), psbt.m_proprietary.end());
83
0
    unknown.insert(psbt.unknown.begin(), psbt.unknown.end());
84
85
0
    return true;
86
0
}
87
88
std::optional<uint32_t> PartiallySignedTransaction::ComputeTimeLock() const
89
0
{
90
0
    if (GetVersion() >= 2) {
  Branch (90:9): [True: 0, False: 0]
91
0
        std::optional<uint32_t> time_lock{0};
92
0
        std::optional<uint32_t> height_lock{0};
93
0
        for (const PSBTInput& input : inputs) {
  Branch (93:37): [True: 0, False: 0]
94
0
            if (input.time_locktime.has_value() && !input.height_locktime.has_value()) {
  Branch (94:17): [True: 0, False: 0]
  Branch (94:52): [True: 0, False: 0]
95
0
                height_lock.reset(); // Transaction can no longer have a height locktime
96
0
                if (!time_lock.has_value()) {
  Branch (96:21): [True: 0, False: 0]
97
0
                    return std::nullopt;
98
0
                }
99
0
            } else if (!input.time_locktime.has_value() && input.height_locktime.has_value()) {
  Branch (99:24): [True: 0, False: 0]
  Branch (99:60): [True: 0, False: 0]
100
0
                time_lock.reset(); // Transaction can no longer have a time locktime
101
0
                if (!height_lock.has_value()) {
  Branch (101:21): [True: 0, False: 0]
102
0
                    return std::nullopt;
103
0
                }
104
0
            }
105
0
            if (input.time_locktime && time_lock.has_value()) {
  Branch (105:17): [True: 0, False: 0]
  Branch (105:40): [True: 0, False: 0]
106
0
                time_lock = std::max(time_lock, input.time_locktime);
107
0
            }
108
0
            if (input.height_locktime && height_lock.has_value()) {
  Branch (108:17): [True: 0, False: 0]
  Branch (108:42): [True: 0, False: 0]
109
0
                height_lock = std::max(height_lock, input.height_locktime);
110
0
            }
111
0
        }
112
0
        if (height_lock.has_value() && *height_lock > 0) {
  Branch (112:13): [True: 0, False: 0]
  Branch (112:40): [True: 0, False: 0]
113
0
            return *height_lock;
114
0
        }
115
0
        if (time_lock.has_value() && *time_lock > 0) {
  Branch (115:13): [True: 0, False: 0]
  Branch (115:38): [True: 0, False: 0]
116
0
            return *time_lock;
117
0
        }
118
0
    }
119
0
    return fallback_locktime.value_or(0);
120
0
}
121
122
std::optional<CMutableTransaction> PartiallySignedTransaction::GetUnsignedTx() const
123
0
{
124
0
    CMutableTransaction mtx;
125
0
    mtx.version = tx_version;
126
0
    std::optional<uint32_t> locktime = ComputeTimeLock();
127
0
    if (!locktime) {
  Branch (127:9): [True: 0, False: 0]
128
0
        return std::nullopt;
129
0
    }
130
0
    mtx.nLockTime = *locktime;
131
0
    uint32_t max_sequence = CTxIn::SEQUENCE_FINAL;
132
0
    for (const PSBTInput& input : inputs) {
  Branch (132:33): [True: 0, False: 0]
133
0
        CTxIn txin;
134
0
        txin.prevout.hash = input.prev_txid;
135
0
        txin.prevout.n = input.prev_out;
136
0
        txin.nSequence = input.sequence.value_or(max_sequence);
137
0
        mtx.vin.push_back(txin);
138
0
    }
139
0
    for (const PSBTOutput& output : outputs) {
  Branch (139:35): [True: 0, False: 0]
140
0
        CTxOut txout;
141
0
        txout.nValue = output.amount;
142
0
        txout.scriptPubKey = output.script;
143
0
        mtx.vout.push_back(txout);
144
0
    }
145
0
    return mtx;
146
0
}
147
148
std::optional<Txid> PartiallySignedTransaction::GetUniqueID() const
149
0
{
150
    // Get the unsigned transaction
151
0
    std::optional<CMutableTransaction> mtx = GetUnsignedTx();
152
0
    if (!mtx) {
  Branch (152:9): [True: 0, False: 0]
153
0
        return std::nullopt;
154
0
    }
155
0
    if (GetVersion() >= 2) {
  Branch (155:9): [True: 0, False: 0]
156
0
        for (CTxIn& txin : mtx->vin) {
  Branch (156:26): [True: 0, False: 0]
157
0
            txin.nSequence = 0;
158
0
        }
159
0
    }
160
0
    return mtx->GetHash();
161
0
}
162
163
bool PartiallySignedTransaction::AddInput(const PSBTInput& psbtin)
164
0
{
165
    // The input being added must be for this PSBT's version
166
0
    if (psbtin.GetVersion() != GetVersion()) {
  Branch (166:9): [True: 0, False: 0]
167
0
        return false;
168
0
    }
169
170
    // Prevent duplicate inputs
171
0
    if (std::find_if(inputs.begin(), inputs.end(),
  Branch (171:9): [True: 0, False: 0]
172
0
        [psbtin](const PSBTInput& psbt) {
173
0
            return psbt.prev_txid == psbtin.prev_txid && psbt.prev_out == psbtin.prev_out;
  Branch (173:20): [True: 0, False: 0]
  Branch (173:58): [True: 0, False: 0]
174
0
        }
175
0
    ) != inputs.end()) {
176
0
        return false;
177
0
    }
178
179
0
    if (GetVersion() < 2) {
  Branch (179:9): [True: 0, False: 0]
180
        // This is a v0 psbt, so do the v0 AddInput
181
0
        inputs.push_back(psbtin);
182
0
        inputs.back().partial_sigs.clear();
183
0
        inputs.back().final_script_sig.clear();
184
0
        inputs.back().final_script_witness.SetNull();
185
0
        return true;
186
0
    }
187
188
    // Check inputs modifiable flag
189
0
    if (!m_tx_modifiable.has_value() || !m_tx_modifiable->test(0)) {
  Branch (189:9): [True: 0, False: 0]
  Branch (189:41): [True: 0, False: 0]
190
0
        return false;
191
0
    }
192
193
    // Determine if we need to iterate the inputs.
194
    // For now, we only do this if the new input has a required time lock.
195
    // BIP 370 states that we should also do this if m_tx_modifiable's bit 2 is set
196
    // (Has SIGHASH_SINGLE flag) but since we are only adding inputs at the end of the vector,
197
    // we don't care about that.
198
0
    bool iterate_inputs = psbtin.time_locktime != std::nullopt || psbtin.height_locktime != std::nullopt;
  Branch (198:27): [True: 0, False: 0]
  Branch (198:67): [True: 0, False: 0]
199
0
    if (iterate_inputs) {
  Branch (199:9): [True: 0, False: 0]
200
0
        std::optional<uint32_t> old_timelock = ComputeTimeLock();
201
0
        if (!old_timelock) {
  Branch (201:13): [True: 0, False: 0]
202
0
            return false;
203
0
        }
204
205
0
        std::optional<uint32_t> time_lock = psbtin.time_locktime;
206
0
        std::optional<uint32_t> height_lock = psbtin.height_locktime;
207
0
        bool has_sigs = false;
208
0
        for (const PSBTInput& input : inputs) {
  Branch (208:37): [True: 0, False: 0]
209
0
            if (input.time_locktime.has_value() && !input.height_locktime.has_value()) {
  Branch (209:17): [True: 0, False: 0]
  Branch (209:52): [True: 0, False: 0]
210
0
                height_lock.reset(); // Transaction can no longer have a height locktime
211
0
                if (time_lock == std::nullopt) {
  Branch (211:21): [True: 0, False: 0]
212
0
                    return false;
213
0
                }
214
0
            } else if (!input.time_locktime.has_value() && input.height_locktime.has_value()) {
  Branch (214:24): [True: 0, False: 0]
  Branch (214:60): [True: 0, False: 0]
215
0
                time_lock.reset(); // Transaction can no longer have a time locktime
216
0
                if (height_lock == std::nullopt) {
  Branch (216:21): [True: 0, False: 0]
217
0
                    return false;
218
0
                }
219
0
            }
220
0
            if (input.time_locktime && time_lock.has_value()) {
  Branch (220:17): [True: 0, False: 0]
  Branch (220:40): [True: 0, False: 0]
221
0
                time_lock = std::max(time_lock, input.time_locktime);
222
0
            }
223
0
            if (input.height_locktime && height_lock.has_value()) {
  Branch (223:17): [True: 0, False: 0]
  Branch (223:42): [True: 0, False: 0]
224
0
                height_lock = std::max(height_lock, input.height_locktime);
225
0
            }
226
0
            if (input.HasSignatures()) {
  Branch (226:17): [True: 0, False: 0]
227
0
                has_sigs = true;
228
0
            }
229
0
        }
230
0
        uint32_t new_timelock = fallback_locktime.value_or(0);
231
0
        if (height_lock.has_value() && *height_lock > 0) {
  Branch (231:13): [True: 0, False: 0]
  Branch (231:40): [True: 0, False: 0]
232
0
            new_timelock = *height_lock;
233
0
        } else if (time_lock.has_value() && *time_lock > 0) {
  Branch (233:20): [True: 0, False: 0]
  Branch (233:45): [True: 0, False: 0]
234
0
            new_timelock = *time_lock;
235
0
        }
236
0
        if (has_sigs && *old_timelock != new_timelock) {
  Branch (236:13): [True: 0, False: 0]
  Branch (236:25): [True: 0, False: 0]
237
0
            return false;
238
0
        }
239
0
    }
240
241
    // Add the input to the end
242
0
    inputs.push_back(psbtin);
243
0
    return true;
244
0
}
245
246
bool PartiallySignedTransaction::AddOutput(const PSBTOutput& psbtout)
247
0
{
248
    // The output being added must be for this PSBT's version
249
0
    if (psbtout.GetVersion() != GetVersion()) {
  Branch (249:9): [True: 0, False: 0]
250
0
        return false;
251
0
    }
252
253
0
    if (GetVersion() < 2) {
  Branch (253:9): [True: 0, False: 0]
254
        // This is a v0 psbt, do the v0 AddOutput
255
0
        outputs.push_back(psbtout);
256
0
        return true;
257
0
    }
258
259
    // No global tx, must be PSBTv2
260
    // Check outputs are modifiable
261
0
    if (!m_tx_modifiable.has_value() || !m_tx_modifiable->test(1)) {
  Branch (261:9): [True: 0, False: 0]
  Branch (261:41): [True: 0, False: 0]
262
0
        return false;
263
0
    }
264
0
    outputs.push_back(psbtout);
265
266
0
    return true;
267
0
}
268
269
bool PSBTInput::GetUTXO(CTxOut& utxo) const
270
0
{
271
0
    if (non_witness_utxo) {
  Branch (271:9): [True: 0, False: 0]
272
0
        if (prev_out >= non_witness_utxo->vout.size()) {
  Branch (272:13): [True: 0, False: 0]
273
0
            return false;
274
0
        }
275
0
        if (non_witness_utxo->GetHash() != prev_txid) {
  Branch (275:13): [True: 0, False: 0]
276
0
            return false;
277
0
        }
278
0
        utxo = non_witness_utxo->vout[prev_out];
279
0
    } else if (!witness_utxo.IsNull()) {
  Branch (279:16): [True: 0, False: 0]
280
0
        utxo = witness_utxo;
281
0
    } else {
282
0
        return false;
283
0
    }
284
0
    return true;
285
0
}
286
287
COutPoint PSBTInput::GetOutPoint() const
288
0
{
289
0
    return COutPoint(prev_txid, prev_out);
290
0
}
291
292
bool PSBTInput::IsNull() const
293
0
{
294
0
    return !non_witness_utxo && witness_utxo.IsNull() && partial_sigs.empty() && unknown.empty() && hd_keypaths.empty() && redeem_script.empty() && witness_script.empty();
  Branch (294:12): [True: 0, False: 0]
  Branch (294:33): [True: 0, False: 0]
  Branch (294:58): [True: 0, False: 0]
  Branch (294:82): [True: 0, False: 0]
  Branch (294:101): [True: 0, False: 0]
  Branch (294:124): [True: 0, False: 0]
  Branch (294:149): [True: 0, False: 0]
295
0
}
296
297
void PSBTInput::FillSignatureData(SignatureData& sigdata) const
298
0
{
299
0
    if (!final_script_sig.empty()) {
  Branch (299:9): [True: 0, False: 0]
300
0
        sigdata.scriptSig = final_script_sig;
301
0
        sigdata.complete = true;
302
0
    }
303
0
    if (!final_script_witness.IsNull()) {
  Branch (303:9): [True: 0, False: 0]
304
0
        sigdata.scriptWitness = final_script_witness;
305
0
        sigdata.complete = true;
306
0
    }
307
0
    if (sigdata.complete) {
  Branch (307:9): [True: 0, False: 0]
308
0
        return;
309
0
    }
310
311
0
    sigdata.signatures.insert(partial_sigs.begin(), partial_sigs.end());
312
0
    if (!redeem_script.empty()) {
  Branch (312:9): [True: 0, False: 0]
313
0
        sigdata.redeem_script = redeem_script;
314
0
    }
315
0
    if (!witness_script.empty()) {
  Branch (315:9): [True: 0, False: 0]
316
0
        sigdata.witness_script = witness_script;
317
0
    }
318
0
    for (const auto& key_pair : hd_keypaths) {
  Branch (318:31): [True: 0, False: 0]
319
0
        sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
320
0
    }
321
0
    if (!m_tap_key_sig.empty()) {
  Branch (321:9): [True: 0, False: 0]
322
0
        sigdata.taproot_key_path_sig = m_tap_key_sig;
323
0
    }
324
0
    for (const auto& [pubkey_leaf, sig] : m_tap_script_sigs) {
  Branch (324:41): [True: 0, False: 0]
325
0
        sigdata.taproot_script_sigs.emplace(pubkey_leaf, sig);
326
0
    }
327
0
    if (!m_tap_internal_key.IsNull()) {
  Branch (327:9): [True: 0, False: 0]
328
0
        sigdata.tr_spenddata.internal_key = m_tap_internal_key;
329
0
    }
330
0
    if (!m_tap_merkle_root.IsNull()) {
  Branch (330:9): [True: 0, False: 0]
331
0
        sigdata.tr_spenddata.merkle_root = m_tap_merkle_root;
332
0
    }
333
0
    for (const auto& [leaf_script, control_block] : m_tap_scripts) {
  Branch (333:51): [True: 0, False: 0]
334
0
        sigdata.tr_spenddata.scripts.emplace(leaf_script, control_block);
335
0
    }
336
0
    for (const auto& [pubkey, leaf_origin] : m_tap_bip32_paths) {
  Branch (336:44): [True: 0, False: 0]
337
0
        sigdata.taproot_misc_pubkeys.emplace(pubkey, leaf_origin);
338
0
        sigdata.tap_pubkeys.emplace(Hash160(pubkey), pubkey);
339
0
    }
340
0
    for (const auto& [hash, preimage] : ripemd160_preimages) {
  Branch (340:39): [True: 0, False: 0]
341
0
        sigdata.ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
342
0
    }
343
0
    for (const auto& [hash, preimage] : sha256_preimages) {
  Branch (343:39): [True: 0, False: 0]
344
0
        sigdata.sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
345
0
    }
346
0
    for (const auto& [hash, preimage] : hash160_preimages) {
  Branch (346:39): [True: 0, False: 0]
347
0
        sigdata.hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
348
0
    }
349
0
    for (const auto& [hash, preimage] : hash256_preimages) {
  Branch (349:39): [True: 0, False: 0]
350
0
        sigdata.hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
351
0
    }
352
0
    sigdata.musig2_pubkeys.insert(m_musig2_participants.begin(), m_musig2_participants.end());
353
0
    for (const auto& [agg_key_lh, pubnonces] : m_musig2_pubnonces) {
  Branch (353:46): [True: 0, False: 0]
354
0
        sigdata.musig2_pubnonces[agg_key_lh].insert(pubnonces.begin(), pubnonces.end());
355
0
    }
356
0
    for (const auto& [agg_key_lh, psigs] : m_musig2_partial_sigs) {
  Branch (356:42): [True: 0, False: 0]
357
0
        sigdata.musig2_partial_sigs[agg_key_lh].insert(psigs.begin(), psigs.end());
358
0
    }
359
0
}
360
361
void PSBTInput::FromSignatureData(const SignatureData& sigdata)
362
0
{
363
0
    if (sigdata.complete) {
  Branch (363:9): [True: 0, False: 0]
364
0
        partial_sigs.clear();
365
0
        hd_keypaths.clear();
366
0
        redeem_script.clear();
367
0
        witness_script.clear();
368
369
0
        if (!sigdata.scriptSig.empty()) {
  Branch (369:13): [True: 0, False: 0]
370
0
            final_script_sig = sigdata.scriptSig;
371
0
        }
372
0
        if (!sigdata.scriptWitness.IsNull()) {
  Branch (372:13): [True: 0, False: 0]
373
0
            final_script_witness = sigdata.scriptWitness;
374
0
        }
375
0
        return;
376
0
    }
377
378
0
    partial_sigs.insert(sigdata.signatures.begin(), sigdata.signatures.end());
379
0
    if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
  Branch (379:9): [True: 0, False: 0]
  Branch (379:34): [True: 0, False: 0]
380
0
        redeem_script = sigdata.redeem_script;
381
0
    }
382
0
    if (witness_script.empty() && !sigdata.witness_script.empty()) {
  Branch (382:9): [True: 0, False: 0]
  Branch (382:35): [True: 0, False: 0]
383
0
        witness_script = sigdata.witness_script;
384
0
    }
385
0
    for (const auto& entry : sigdata.misc_pubkeys) {
  Branch (385:28): [True: 0, False: 0]
386
0
        hd_keypaths.emplace(entry.second);
387
0
    }
388
0
    if (!sigdata.taproot_key_path_sig.empty()) {
  Branch (388:9): [True: 0, False: 0]
389
0
        m_tap_key_sig = sigdata.taproot_key_path_sig;
390
0
    }
391
0
    for (const auto& [pubkey_leaf, sig] : sigdata.taproot_script_sigs) {
  Branch (391:41): [True: 0, False: 0]
392
0
        m_tap_script_sigs.emplace(pubkey_leaf, sig);
393
0
    }
394
0
    if (!sigdata.tr_spenddata.internal_key.IsNull()) {
  Branch (394:9): [True: 0, False: 0]
395
0
        m_tap_internal_key = sigdata.tr_spenddata.internal_key;
396
0
    }
397
0
    if (!sigdata.tr_spenddata.merkle_root.IsNull()) {
  Branch (397:9): [True: 0, False: 0]
398
0
        m_tap_merkle_root = sigdata.tr_spenddata.merkle_root;
399
0
    }
400
0
    for (const auto& [leaf_script, control_block] : sigdata.tr_spenddata.scripts) {
  Branch (400:51): [True: 0, False: 0]
401
0
        m_tap_scripts.emplace(leaf_script, control_block);
402
0
    }
403
0
    for (const auto& [pubkey, leaf_origin] : sigdata.taproot_misc_pubkeys) {
  Branch (403:44): [True: 0, False: 0]
404
0
        m_tap_bip32_paths.emplace(pubkey, leaf_origin);
405
0
    }
406
0
    m_musig2_participants.insert(sigdata.musig2_pubkeys.begin(), sigdata.musig2_pubkeys.end());
407
0
    for (const auto& [agg_key_lh, pubnonces] : sigdata.musig2_pubnonces) {
  Branch (407:46): [True: 0, False: 0]
408
0
        m_musig2_pubnonces[agg_key_lh].insert(pubnonces.begin(), pubnonces.end());
409
0
    }
410
0
    for (const auto& [agg_key_lh, psigs] : sigdata.musig2_partial_sigs) {
  Branch (410:42): [True: 0, False: 0]
411
0
        m_musig2_partial_sigs[agg_key_lh].insert(psigs.begin(), psigs.end());
412
0
    }
413
0
    for (const auto& [hash, preimage] : sigdata.ripemd160_preimages) {
  Branch (413:39): [True: 0, False: 0]
414
0
        ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
415
0
    }
416
0
    for (const auto& [hash, preimage] : sigdata.sha256_preimages) {
  Branch (416:39): [True: 0, False: 0]
417
0
        sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
418
0
    }
419
0
    for (const auto& [hash, preimage] : sigdata.hash160_preimages) {
  Branch (419:39): [True: 0, False: 0]
420
0
        hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
421
0
    }
422
0
    for (const auto& [hash, preimage] : sigdata.hash256_preimages) {
  Branch (422:39): [True: 0, False: 0]
423
0
        hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
424
0
    }
425
0
}
426
427
bool PSBTInput::Merge(const PSBTInput& input)
428
0
{
429
0
    if (!non_witness_utxo && input.non_witness_utxo) non_witness_utxo = input.non_witness_utxo;
  Branch (429:9): [True: 0, False: 0]
  Branch (429:30): [True: 0, False: 0]
430
0
    if (witness_utxo.IsNull() && !input.witness_utxo.IsNull()) {
  Branch (430:9): [True: 0, False: 0]
  Branch (430:34): [True: 0, False: 0]
431
0
        witness_utxo = input.witness_utxo;
432
0
    }
433
434
0
    partial_sigs.insert(input.partial_sigs.begin(), input.partial_sigs.end());
435
0
    ripemd160_preimages.insert(input.ripemd160_preimages.begin(), input.ripemd160_preimages.end());
436
0
    sha256_preimages.insert(input.sha256_preimages.begin(), input.sha256_preimages.end());
437
0
    hash160_preimages.insert(input.hash160_preimages.begin(), input.hash160_preimages.end());
438
0
    hash256_preimages.insert(input.hash256_preimages.begin(), input.hash256_preimages.end());
439
0
    hd_keypaths.insert(input.hd_keypaths.begin(), input.hd_keypaths.end());
440
0
    m_proprietary.insert(input.m_proprietary.begin(), input.m_proprietary.end());
441
0
    unknown.insert(input.unknown.begin(), input.unknown.end());
442
0
    m_tap_script_sigs.insert(input.m_tap_script_sigs.begin(), input.m_tap_script_sigs.end());
443
0
    m_tap_scripts.insert(input.m_tap_scripts.begin(), input.m_tap_scripts.end());
444
0
    m_tap_bip32_paths.insert(input.m_tap_bip32_paths.begin(), input.m_tap_bip32_paths.end());
445
446
0
    if (redeem_script.empty() && !input.redeem_script.empty()) redeem_script = input.redeem_script;
  Branch (446:9): [True: 0, False: 0]
  Branch (446:34): [True: 0, False: 0]
447
0
    if (witness_script.empty() && !input.witness_script.empty()) witness_script = input.witness_script;
  Branch (447:9): [True: 0, False: 0]
  Branch (447:35): [True: 0, False: 0]
448
0
    if (final_script_sig.empty() && !input.final_script_sig.empty()) final_script_sig = input.final_script_sig;
  Branch (448:9): [True: 0, False: 0]
  Branch (448:37): [True: 0, False: 0]
449
0
    if (final_script_witness.IsNull() && !input.final_script_witness.IsNull()) final_script_witness = input.final_script_witness;
  Branch (449:9): [True: 0, False: 0]
  Branch (449:42): [True: 0, False: 0]
450
0
    if (m_tap_key_sig.empty() && !input.m_tap_key_sig.empty()) m_tap_key_sig = input.m_tap_key_sig;
  Branch (450:9): [True: 0, False: 0]
  Branch (450:34): [True: 0, False: 0]
451
0
    if (m_tap_internal_key.IsNull() && !input.m_tap_internal_key.IsNull()) m_tap_internal_key = input.m_tap_internal_key;
  Branch (451:9): [True: 0, False: 0]
  Branch (451:40): [True: 0, False: 0]
452
0
    if (m_tap_merkle_root.IsNull() && !input.m_tap_merkle_root.IsNull()) m_tap_merkle_root = input.m_tap_merkle_root;
  Branch (452:9): [True: 0, False: 0]
  Branch (452:39): [True: 0, False: 0]
453
0
    m_musig2_participants.insert(input.m_musig2_participants.begin(), input.m_musig2_participants.end());
454
0
    for (const auto& [agg_key_lh, pubnonces] : input.m_musig2_pubnonces) {
  Branch (454:46): [True: 0, False: 0]
455
0
        m_musig2_pubnonces[agg_key_lh].insert(pubnonces.begin(), pubnonces.end());
456
0
    }
457
0
    for (const auto& [agg_key_lh, psigs] : input.m_musig2_partial_sigs) {
  Branch (457:42): [True: 0, False: 0]
458
0
        m_musig2_partial_sigs[agg_key_lh].insert(psigs.begin(), psigs.end());
459
0
    }
460
0
    if (sequence == std::nullopt && input.sequence != std::nullopt) sequence = input.sequence;
  Branch (460:9): [True: 0, False: 0]
  Branch (460:37): [True: 0, False: 0]
461
0
    if (time_locktime == std::nullopt && input.time_locktime != std::nullopt) time_locktime = input.time_locktime;
  Branch (461:9): [True: 0, False: 0]
  Branch (461:42): [True: 0, False: 0]
462
0
    if (height_locktime == std::nullopt && input.height_locktime != std::nullopt) height_locktime = input.height_locktime;
  Branch (462:9): [True: 0, False: 0]
  Branch (462:44): [True: 0, False: 0]
463
464
0
    return true;
465
0
}
466
467
bool PSBTInput::HasSignatures() const
468
0
{
469
0
    return !final_script_sig.empty()
  Branch (469:12): [True: 0, False: 0]
470
0
           || !final_script_witness.IsNull()
  Branch (470:15): [True: 0, False: 0]
471
0
           || !partial_sigs.empty()
  Branch (471:15): [True: 0, False: 0]
472
0
           || !m_tap_key_sig.empty()
  Branch (472:15): [True: 0, False: 0]
473
0
           || !m_tap_script_sigs.empty()
  Branch (473:15): [True: 0, False: 0]
474
0
           || !m_musig2_partial_sigs.empty();
  Branch (474:15): [True: 0, False: 0]
475
0
}
476
477
void PSBTOutput::FillSignatureData(SignatureData& sigdata) const
478
0
{
479
0
    if (!redeem_script.empty()) {
  Branch (479:9): [True: 0, False: 0]
480
0
        sigdata.redeem_script = redeem_script;
481
0
    }
482
0
    if (!witness_script.empty()) {
  Branch (482:9): [True: 0, False: 0]
483
0
        sigdata.witness_script = witness_script;
484
0
    }
485
0
    for (const auto& key_pair : hd_keypaths) {
  Branch (485:31): [True: 0, False: 0]
486
0
        sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
487
0
    }
488
0
    if (!m_tap_tree.empty() && m_tap_internal_key.IsFullyValid()) {
  Branch (488:9): [True: 0, False: 0]
  Branch (488:32): [True: 0, False: 0]
489
0
        TaprootBuilder builder;
490
0
        for (const auto& [depth, leaf_ver, script] : m_tap_tree) {
  Branch (490:52): [True: 0, False: 0]
491
0
            builder.Add((int)depth, script, (int)leaf_ver, /*track=*/true);
492
0
        }
493
0
        assert(builder.IsComplete());
  Branch (493:9): [True: 0, False: 0]
494
0
        builder.Finalize(m_tap_internal_key);
495
0
        TaprootSpendData spenddata = builder.GetSpendData();
496
497
0
        sigdata.tr_spenddata.internal_key = m_tap_internal_key;
498
0
        sigdata.tr_spenddata.Merge(spenddata);
499
0
        sigdata.tr_builder = builder;
500
0
    }
501
0
    for (const auto& [pubkey, leaf_origin] : m_tap_bip32_paths) {
  Branch (501:44): [True: 0, False: 0]
502
0
        sigdata.taproot_misc_pubkeys.emplace(pubkey, leaf_origin);
503
0
        sigdata.tap_pubkeys.emplace(Hash160(pubkey), pubkey);
504
0
    }
505
0
    sigdata.musig2_pubkeys.insert(m_musig2_participants.begin(), m_musig2_participants.end());
506
0
}
507
508
void PSBTOutput::FromSignatureData(const SignatureData& sigdata)
509
0
{
510
0
    if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
  Branch (510:9): [True: 0, False: 0]
  Branch (510:34): [True: 0, False: 0]
511
0
        redeem_script = sigdata.redeem_script;
512
0
    }
513
0
    if (witness_script.empty() && !sigdata.witness_script.empty()) {
  Branch (513:9): [True: 0, False: 0]
  Branch (513:35): [True: 0, False: 0]
514
0
        witness_script = sigdata.witness_script;
515
0
    }
516
0
    for (const auto& entry : sigdata.misc_pubkeys) {
  Branch (516:28): [True: 0, False: 0]
517
0
        hd_keypaths.emplace(entry.second);
518
0
    }
519
0
    if (!sigdata.tr_spenddata.internal_key.IsNull()) {
  Branch (519:9): [True: 0, False: 0]
520
0
        m_tap_internal_key = sigdata.tr_spenddata.internal_key;
521
0
    }
522
0
    if (sigdata.tr_builder.has_value() && sigdata.tr_builder->HasScripts()) {
  Branch (522:9): [True: 0, False: 0]
  Branch (522:43): [True: 0, False: 0]
523
0
        m_tap_tree = sigdata.tr_builder->GetTreeTuples();
524
0
    }
525
0
    for (const auto& [pubkey, leaf_origin] : sigdata.taproot_misc_pubkeys) {
  Branch (525:44): [True: 0, False: 0]
526
0
        m_tap_bip32_paths.emplace(pubkey, leaf_origin);
527
0
    }
528
0
    m_musig2_participants.insert(sigdata.musig2_pubkeys.begin(), sigdata.musig2_pubkeys.end());
529
0
}
530
531
bool PSBTOutput::IsNull() const
532
0
{
533
0
    return redeem_script.empty() && witness_script.empty() && hd_keypaths.empty() && unknown.empty();
  Branch (533:12): [True: 0, False: 0]
  Branch (533:37): [True: 0, False: 0]
  Branch (533:63): [True: 0, False: 0]
  Branch (533:86): [True: 0, False: 0]
534
0
}
535
536
bool PSBTOutput::Merge(const PSBTOutput& output)
537
0
{
538
0
    hd_keypaths.insert(output.hd_keypaths.begin(), output.hd_keypaths.end());
539
0
    m_proprietary.insert(output.m_proprietary.begin(), output.m_proprietary.end());
540
0
    unknown.insert(output.unknown.begin(), output.unknown.end());
541
0
    m_tap_bip32_paths.insert(output.m_tap_bip32_paths.begin(), output.m_tap_bip32_paths.end());
542
543
0
    if (redeem_script.empty() && !output.redeem_script.empty()) redeem_script = output.redeem_script;
  Branch (543:9): [True: 0, False: 0]
  Branch (543:34): [True: 0, False: 0]
544
0
    if (witness_script.empty() && !output.witness_script.empty()) witness_script = output.witness_script;
  Branch (544:9): [True: 0, False: 0]
  Branch (544:35): [True: 0, False: 0]
545
0
    if (m_tap_internal_key.IsNull() && !output.m_tap_internal_key.IsNull()) m_tap_internal_key = output.m_tap_internal_key;
  Branch (545:9): [True: 0, False: 0]
  Branch (545:40): [True: 0, False: 0]
546
0
    if (m_tap_tree.empty() && !output.m_tap_tree.empty()) m_tap_tree = output.m_tap_tree;
  Branch (546:9): [True: 0, False: 0]
  Branch (546:31): [True: 0, False: 0]
547
0
    m_musig2_participants.insert(output.m_musig2_participants.begin(), output.m_musig2_participants.end());
548
549
0
    return true;
550
0
}
551
552
bool PSBTInputSigned(const PSBTInput& input)
553
0
{
554
0
    return !input.final_script_sig.empty() || !input.final_script_witness.IsNull();
  Branch (554:12): [True: 0, False: 0]
  Branch (554:47): [True: 0, False: 0]
555
0
}
556
557
bool PSBTInputSignedAndVerified(const PartiallySignedTransaction& psbt, unsigned int input_index, const PrecomputedTransactionData* txdata)
558
0
{
559
0
    CTxOut utxo;
560
0
    assert(input_index < psbt.inputs.size());
  Branch (560:5): [True: 0, False: 0]
561
0
    const PSBTInput& input = psbt.inputs[input_index];
562
563
0
    if (input.non_witness_utxo) {
  Branch (563:9): [True: 0, False: 0]
564
        // If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
565
0
        COutPoint prevout = input.GetOutPoint();
566
0
        if (prevout.n >= input.non_witness_utxo->vout.size()) {
  Branch (566:13): [True: 0, False: 0]
567
0
            return false;
568
0
        }
569
0
        if (input.non_witness_utxo->GetHash() != prevout.hash) {
  Branch (569:13): [True: 0, False: 0]
570
0
            return false;
571
0
        }
572
0
        utxo = input.non_witness_utxo->vout[prevout.n];
573
0
    } else if (!input.witness_utxo.IsNull()) {
  Branch (573:16): [True: 0, False: 0]
574
0
        utxo = input.witness_utxo;
575
0
    } else {
576
0
        return false;
577
0
    }
578
579
0
    std::optional<CMutableTransaction> unsigned_tx = psbt.GetUnsignedTx();
580
0
    if (!unsigned_tx) {
  Branch (580:9): [True: 0, False: 0]
581
0
        return false;
582
0
    }
583
0
    const CMutableTransaction& tx = *unsigned_tx;
584
0
    if (txdata) {
  Branch (584:9): [True: 0, False: 0]
585
0
        return VerifyScript(input.final_script_sig, utxo.scriptPubKey, &input.final_script_witness, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&tx, input_index, utxo.nValue, *txdata, MissingDataBehavior::FAIL});
586
0
    } else {
587
0
        return VerifyScript(input.final_script_sig, utxo.scriptPubKey, &input.final_script_witness, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&tx, input_index, utxo.nValue, MissingDataBehavior::FAIL});
588
0
    }
589
0
}
590
591
0
size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction& psbt) {
592
0
    size_t count = 0;
593
0
    for (const auto& input : psbt.inputs) {
  Branch (593:28): [True: 0, False: 0]
594
0
        if (!PSBTInputSigned(input)) {
  Branch (594:13): [True: 0, False: 0]
595
0
            count++;
596
0
        }
597
0
    }
598
599
0
    return count;
600
0
}
601
602
void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index)
603
0
{
604
0
    std::optional<CMutableTransaction> unsigned_tx = psbt.GetUnsignedTx();
605
0
    if (!unsigned_tx) {
  Branch (605:9): [True: 0, False: 0]
606
0
        return;
607
0
    }
608
0
    CMutableTransaction& tx = *unsigned_tx;
609
0
    const CTxOut& out = tx.vout.at(index);
610
0
    PSBTOutput& psbt_out = psbt.outputs.at(index);
611
612
    // Fill a SignatureData with output info
613
0
    SignatureData sigdata;
614
0
    psbt_out.FillSignatureData(sigdata);
615
616
    // Construct a would-be spend of this output, to update sigdata with.
617
    // Note that ProduceSignature is used to fill in metadata (not actual signatures),
618
    // so provider does not need to provide any private keys (it can be a HidingSigningProvider).
619
0
    MutableTransactionSignatureCreator creator(tx, /*input_idx=*/0, out.nValue, {.sighash_type = SIGHASH_ALL});
620
0
    ProduceSignature(provider, creator, out.scriptPubKey, sigdata);
621
622
    // Put redeem_script, witness_script, key paths, into PSBTOutput.
623
0
    psbt_out.FromSignatureData(sigdata);
624
0
}
625
626
std::optional<PrecomputedTransactionData> PrecomputePSBTData(const PartiallySignedTransaction& psbt)
627
0
{
628
0
    std::optional<CMutableTransaction> unsigned_tx = psbt.GetUnsignedTx();
629
0
    if (!unsigned_tx) {
  Branch (629:9): [True: 0, False: 0]
630
0
        return std::nullopt;
631
0
    }
632
0
    const CMutableTransaction& tx = *unsigned_tx;
633
0
    bool have_all_spent_outputs = true;
634
0
    std::vector<CTxOut> utxos;
635
0
    for (const PSBTInput& input : psbt.inputs) {
  Branch (635:33): [True: 0, False: 0]
636
0
        if (!input.GetUTXO(utxos.emplace_back())) have_all_spent_outputs = false;
  Branch (636:13): [True: 0, False: 0]
637
0
    }
638
0
    PrecomputedTransactionData txdata;
639
0
    if (have_all_spent_outputs) {
  Branch (639:9): [True: 0, False: 0]
640
0
        txdata.Init(tx, std::move(utxos), true);
641
0
    } else {
642
0
        txdata.Init(tx, {}, true);
643
0
    }
644
0
    return txdata;
645
0
}
646
647
PSBTError SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, const PrecomputedTransactionData* txdata, const common::PSBTFillOptions& options,  SignatureData* out_sigdata)
648
0
{
649
0
    PSBTInput& input = psbt.inputs.at(index);
650
0
    std::optional<CMutableTransaction> unsigned_tx = psbt.GetUnsignedTx();
651
0
    if (!unsigned_tx) {
  Branch (651:9): [True: 0, False: 0]
652
0
        return PSBTError::INVALID_TX;
653
0
    }
654
0
    const CMutableTransaction& tx = *unsigned_tx;
655
656
0
    if (PSBTInputSignedAndVerified(psbt, index, txdata)) {
  Branch (656:9): [True: 0, False: 0]
657
0
        return PSBTError::OK;
658
0
    }
659
660
    // Fill SignatureData with input info
661
0
    SignatureData sigdata;
662
0
    input.FillSignatureData(sigdata);
663
664
    // Get UTXO
665
0
    bool require_witness_sig = false;
666
0
    CTxOut utxo;
667
668
0
    if (input.non_witness_utxo) {
  Branch (668:9): [True: 0, False: 0]
669
        // If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
670
0
        COutPoint prevout = input.GetOutPoint();
671
0
        if (prevout.n >= input.non_witness_utxo->vout.size()) {
  Branch (671:13): [True: 0, False: 0]
672
0
            return PSBTError::MISSING_INPUTS;
673
0
        }
674
0
        if (input.non_witness_utxo->GetHash() != prevout.hash) {
  Branch (674:13): [True: 0, False: 0]
675
0
            return PSBTError::MISSING_INPUTS;
676
0
        }
677
0
        utxo = input.non_witness_utxo->vout[prevout.n];
678
0
    } else if (!input.witness_utxo.IsNull()) {
  Branch (678:16): [True: 0, False: 0]
679
0
        utxo = input.witness_utxo;
680
        // When we're taking our information from a witness UTXO, we can't verify it is actually data from
681
        // the output being spent. This is safe in case a witness signature is produced (which includes this
682
        // information directly in the hash), but not for non-witness signatures. Remember that we require
683
        // a witness signature in this situation.
684
0
        require_witness_sig = true;
685
0
    } else {
686
0
        return PSBTError::MISSING_INPUTS;
687
0
    }
688
689
    // Get the sighash type
690
    // If both the field and the parameter are provided, they must match
691
    // If only the parameter is provided, use it and add it to the PSBT if it is other than SIGHASH_DEFAULT
692
    // for all input types, and not SIGHASH_ALL for non-taproot input types.
693
    // If neither are provided, use SIGHASH_DEFAULT if it is taproot, and SIGHASH_ALL for everything else.
694
0
    int sighash{options.sighash_type.value_or(utxo.scriptPubKey.IsPayToTaproot() ? SIGHASH_DEFAULT : SIGHASH_ALL)};
  Branch (694:47): [True: 0, False: 0]
695
696
    // For user safety, the desired sighash must be provided if the PSBT wants something other than the default set in the previous line.
697
0
    if (input.sighash_type && input.sighash_type != sighash) {
  Branch (697:9): [True: 0, False: 0]
  Branch (697:31): [True: 0, False: 0]
698
0
        return PSBTError::SIGHASH_MISMATCH;
699
0
    }
700
    // Set the PSBT sighash field when sighash is not DEFAULT or ALL
701
    // DEFAULT is allowed for non-taproot inputs since DEFAULT may be passed for them (e.g. the psbt being signed also has taproot inputs)
702
    // Note that signing already aliases DEFAULT to ALL for non-taproot inputs.
703
0
    if (utxo.scriptPubKey.IsPayToTaproot() ? sighash != SIGHASH_DEFAULT :
  Branch (703:9): [True: 0, False: 0]
  Branch (703:9): [True: 0, False: 0]
704
0
                                            (sighash != SIGHASH_DEFAULT && sighash != SIGHASH_ALL)) {
  Branch (704:46): [True: 0, False: 0]
  Branch (704:76): [True: 0, False: 0]
705
0
        input.sighash_type = sighash;
706
0
    }
707
708
    // Check all existing signatures use the sighash type
709
0
    if (sighash == SIGHASH_DEFAULT) {
  Branch (709:9): [True: 0, False: 0]
710
0
        if (!input.m_tap_key_sig.empty() && input.m_tap_key_sig.size() != 64) {
  Branch (710:13): [True: 0, False: 0]
  Branch (710:45): [True: 0, False: 0]
711
0
            return PSBTError::SIGHASH_MISMATCH;
712
0
        }
713
0
        for (const auto& [_, sig] : input.m_tap_script_sigs) {
  Branch (713:35): [True: 0, False: 0]
714
0
            if (sig.size() != 64) return PSBTError::SIGHASH_MISMATCH;
  Branch (714:17): [True: 0, False: 0]
715
0
        }
716
0
    } else {
717
0
        if (!input.m_tap_key_sig.empty() && (input.m_tap_key_sig.size() != 65 || input.m_tap_key_sig.back() != sighash)) {
  Branch (717:13): [True: 0, False: 0]
  Branch (717:46): [True: 0, False: 0]
  Branch (717:82): [True: 0, False: 0]
718
0
            return PSBTError::SIGHASH_MISMATCH;
719
0
        }
720
0
        for (const auto& [_, sig] : input.m_tap_script_sigs) {
  Branch (720:35): [True: 0, False: 0]
721
0
            if (sig.size() != 65 || sig.back() != sighash) return PSBTError::SIGHASH_MISMATCH;
  Branch (721:17): [True: 0, False: 0]
  Branch (721:37): [True: 0, False: 0]
722
0
        }
723
0
        for (const auto& [_, sig] : input.partial_sigs) {
  Branch (723:35): [True: 0, False: 0]
724
0
            if (sig.second.back() != sighash) return PSBTError::SIGHASH_MISMATCH;
  Branch (724:17): [True: 0, False: 0]
725
0
        }
726
0
    }
727
728
0
    sigdata.witness = false;
729
0
    bool sig_complete;
730
0
    if (txdata == nullptr) {
  Branch (730:9): [True: 0, False: 0]
731
0
        sig_complete = ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, utxo.scriptPubKey, sigdata);
732
0
    } else {
733
0
        MutableTransactionSignatureCreator creator(tx, index, utxo.nValue, txdata, {.sighash_type = sighash});
734
0
        sig_complete = ProduceSignature(provider, creator, utxo.scriptPubKey, sigdata);
735
0
    }
736
    // Verify that a witness signature was produced in case one was required.
737
0
    if (require_witness_sig && !sigdata.witness) return PSBTError::INCOMPLETE;
  Branch (737:9): [True: 0, False: 0]
  Branch (737:32): [True: 0, False: 0]
738
739
    // If we are not finalizing, set sigdata.complete to false to not set the scriptWitness
740
0
    if (!options.finalize && sigdata.complete) sigdata.complete = false;
  Branch (740:9): [True: 0, False: 0]
  Branch (740:30): [True: 0, False: 0]
741
742
0
    input.FromSignatureData(sigdata);
743
744
    // If we have a witness signature, put a witness UTXO.
745
0
    if (sigdata.witness) {
  Branch (745:9): [True: 0, False: 0]
746
0
        input.witness_utxo = utxo;
747
        // We can remove the non_witness_utxo if and only if there are no non-segwit or segwit v0
748
        // inputs in this transaction. Since this requires inspecting the entire transaction, this
749
        // is something for the caller to deal with (i.e. FillPSBT).
750
0
    }
751
752
    // Fill in the missing info
753
0
    if (out_sigdata) {
  Branch (753:9): [True: 0, False: 0]
754
0
        out_sigdata->missing_pubkeys = sigdata.missing_pubkeys;
755
0
        out_sigdata->missing_sigs = sigdata.missing_sigs;
756
0
        out_sigdata->missing_redeem_script = sigdata.missing_redeem_script;
757
0
        out_sigdata->missing_witness_script = sigdata.missing_witness_script;
758
0
    }
759
760
0
    return sig_complete ? PSBTError::OK : PSBTError::INCOMPLETE;
  Branch (760:12): [True: 0, False: 0]
761
0
}
762
763
void RemoveUnnecessaryTransactions(PartiallySignedTransaction& psbtx)
764
0
{
765
    // Figure out if any non_witness_utxos should be dropped
766
0
    std::vector<unsigned int> to_drop;
767
0
    for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
  Branch (767:30): [True: 0, False: 0]
768
0
        const auto& input = psbtx.inputs.at(i);
769
0
        int wit_ver;
770
0
        std::vector<unsigned char> wit_prog;
771
0
        if (input.witness_utxo.IsNull() || !input.witness_utxo.scriptPubKey.IsWitnessProgram(wit_ver, wit_prog)) {
  Branch (771:13): [True: 0, False: 0]
  Branch (771:44): [True: 0, False: 0]
772
            // There's a non-segwit input, so we cannot drop any non_witness_utxos
773
0
            to_drop.clear();
774
0
            break;
775
0
        }
776
0
        if (wit_ver == 0) {
  Branch (776:13): [True: 0, False: 0]
777
            // Segwit v0, so we cannot drop any non_witness_utxos
778
0
            to_drop.clear();
779
0
            break;
780
0
        }
781
        // non_witness_utxos cannot be dropped if the sighash type includes SIGHASH_ANYONECANPAY
782
        // Since callers should have called SignPSBTInput which updates the sighash type in the PSBT, we only
783
        // need to look at that field. If it is not present, then we can assume SIGHASH_DEFAULT or SIGHASH_ALL.
784
0
        if (input.sighash_type != std::nullopt && (*input.sighash_type & 0x80) == SIGHASH_ANYONECANPAY) {
  Branch (784:13): [True: 0, False: 0]
  Branch (784:51): [True: 0, False: 0]
785
0
            to_drop.clear();
786
0
            break;
787
0
        }
788
789
0
        if (input.non_witness_utxo) {
  Branch (789:13): [True: 0, False: 0]
790
0
            to_drop.push_back(i);
791
0
        }
792
0
    }
793
794
    // Drop the non_witness_utxos that we can drop
795
0
    for (unsigned int i : to_drop) {
  Branch (795:25): [True: 0, False: 0]
796
0
        psbtx.inputs.at(i).non_witness_utxo = nullptr;
797
0
    }
798
0
}
799
800
bool FinalizePSBT(PartiallySignedTransaction& psbtx)
801
0
{
802
    // Finalize input signatures -- in case we have partial signatures that add up to a complete
803
    //   signature, but have not combined them yet (e.g. because the combiner that created this
804
    //   PartiallySignedTransaction did not understand them), this will combine them into a final
805
    //   script.
806
0
    bool complete = true;
807
0
    std::optional<PrecomputedTransactionData> txdata_res = PrecomputePSBTData(psbtx);
808
0
    if (!txdata_res) {
  Branch (808:9): [True: 0, False: 0]
809
0
        return false;
810
0
    }
811
0
    const PrecomputedTransactionData& txdata = *txdata_res;
812
0
    for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
  Branch (812:30): [True: 0, False: 0]
813
0
        PSBTInput& input = psbtx.inputs.at(i);
814
0
        complete &= (SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, &txdata, {.sighash_type = input.sighash_type, .finalize = true}, /*out_sigdata=*/nullptr) == PSBTError::OK);
815
0
    }
816
817
0
    return complete;
818
0
}
819
820
bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransaction& result)
821
0
{
822
    // It's not safe to extract a PSBT that isn't finalized, and there's no easy way to check
823
    //   whether a PSBT is finalized without finalizing it, so we just do this.
824
0
    if (!FinalizePSBT(psbtx)) {
  Branch (824:9): [True: 0, False: 0]
825
0
        return false;
826
0
    }
827
828
0
    std::optional<CMutableTransaction> unsigned_tx = psbtx.GetUnsignedTx();
829
0
    if (!unsigned_tx) {
  Branch (829:9): [True: 0, False: 0]
830
0
        return false;
831
0
    }
832
0
    result = *unsigned_tx;
833
0
    for (unsigned int i = 0; i < result.vin.size(); ++i) {
  Branch (833:30): [True: 0, False: 0]
834
0
        result.vin[i].scriptSig = psbtx.inputs[i].final_script_sig;
835
0
        result.vin[i].scriptWitness = psbtx.inputs[i].final_script_witness;
836
0
    }
837
0
    return true;
838
0
}
839
840
std::optional<PartiallySignedTransaction> CombinePSBTs(const std::vector<PartiallySignedTransaction>& psbtxs)
841
0
{
842
0
    PartiallySignedTransaction out = psbtxs[0]; // Copy the first one
843
844
    // Merge
845
0
    for (auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
  Branch (845:47): [True: 0, False: 0]
846
0
        if (!out.Merge(*it)) {
  Branch (846:13): [True: 0, False: 0]
847
0
            return std::nullopt;
848
0
        }
849
0
    }
850
0
    return out;
851
0
}
852
853
0
std::string PSBTRoleName(PSBTRole role) {
854
0
    switch (role) {
  Branch (854:13): [True: 0, False: 0]
855
0
    case PSBTRole::CREATOR: return "creator";
  Branch (855:5): [True: 0, False: 0]
856
0
    case PSBTRole::UPDATER: return "updater";
  Branch (856:5): [True: 0, False: 0]
857
0
    case PSBTRole::SIGNER: return "signer";
  Branch (857:5): [True: 0, False: 0]
858
0
    case PSBTRole::FINALIZER: return "finalizer";
  Branch (858:5): [True: 0, False: 0]
859
0
    case PSBTRole::EXTRACTOR: return "extractor";
  Branch (859:5): [True: 0, False: 0]
860
0
    } // no default case, so the compiler can warn about missing cases
861
0
    assert(false);
  Branch (861:5): [Folded - Ignored]
862
0
}
863
864
util::Result<PartiallySignedTransaction> DecodeBase64PSBT(const std::string& base64_tx)
865
0
{
866
0
    auto tx_data = DecodeBase64(base64_tx);
867
0
    if (!tx_data) {
  Branch (867:9): [True: 0, False: 0]
868
0
        return util::Error{Untranslated("invalid base64")};
869
0
    }
870
0
    return DecodeRawPSBT(MakeByteSpan(*tx_data));
871
0
}
872
873
util::Result<PartiallySignedTransaction> DecodeRawPSBT(std::span<const std::byte> tx_data)
874
0
{
875
0
    SpanReader ss_data{tx_data};
876
0
    try {
877
0
        PartiallySignedTransaction psbt(deserialize, ss_data);
878
0
        if (!ss_data.empty()) {
  Branch (878:13): [True: 0, False: 0]
879
0
            return util::Error{Untranslated("extra data after PSBT")};
880
0
        }
881
0
        return psbt;
882
0
    } catch (const std::exception& e) {
883
0
        return util::Error{Untranslated(e.what())};
884
0
    }
885
0
}
886
887
uint32_t PartiallySignedTransaction::GetVersion() const
888
0
{
889
0
    if (m_version != std::nullopt) {
  Branch (889:9): [True: 0, False: 0]
890
0
        return *m_version;
891
0
    }
892
0
    return 0;
893
0
}