LCOV - code coverage report
Current view: top level - src - psbt.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 92.1 % 492 453
Test Date: 2026-05-17 07:22:50 Functions: 88.2 % 34 30
Branches: 68.0 % 796 541

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

Generated by: LCOV version 2.0-1