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