LCOV - code coverage report
Current view: top level - src - psbt.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 86.3 % 495 427
Test Date: 2026-05-21 06:55:05 Functions: 97.1 % 34 33
Branches: 67.6 % 796 538

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

Generated by: LCOV version 2.0-1