LCOV - code coverage report
Current view: top level - src - psbt.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 98.3 % 352 346
Test Date: 2026-01-05 15:33:09 Functions: 100.0 % 28 28
Branches: 79.4 % 558 443

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

Generated by: LCOV version 2.0-1