LCOV - code coverage report
Current view: top level - src/test - psbt_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 297 297
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 28 28
Branches: 50.5 % 1738 878

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or https://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <addresstype.h>
       6                 :             : #include <key.h>
       7                 :             : #include <psbt.h>
       8                 :             : #include <script/descriptor.h>
       9                 :             : #include <script/script.h>
      10                 :             : #include <script/signingprovider.h>
      11                 :             : #include <script/solver.h>
      12                 :             : #include <test/util/common.h>
      13                 :             : #include <test/util/setup_common.h>
      14                 :             : #include <util/strencodings.h>
      15                 :             : #include <util/string.h>
      16                 :             : 
      17                 :             : #include <boost/test/unit_test.hpp>
      18                 :             : 
      19                 :             : #include <cstddef>
      20                 :             : #include <string>
      21                 :             : #include <vector>
      22                 :             : 
      23                 :             : BOOST_FIXTURE_TEST_SUITE(psbt_tests, BasicTestingSetup)
      24                 :             : 
      25                 :             : // Bit positions in BIP 370's transaction modifiable flags.
      26                 :             : static constexpr std::size_t INPUTS_MODIFIABLE{0};
      27                 :             : static constexpr std::size_t OUTPUTS_MODIFIABLE{1};
      28                 :             : static constexpr std::size_t HAS_SIGHASH_SINGLE{2};
      29                 :             : 
      30                 :           2 : static PSBTProprietary MakeProprietary(uint64_t subtype, uint8_t key_data, uint8_t value)
      31                 :             : {
      32   [ +  -  +  - ]:           2 :     return PSBTProprietary{
      33                 :             :         .subtype = subtype,
      34                 :             :         .identifier = {'p', 's', 'b', 't'},
      35                 :             :         .key = {key_data},
      36                 :             :         .value = {value},
      37                 :           2 :     };
      38                 :             : }
      39                 :             : 
      40                 :          11 : void CheckTimeLock(const std::string& base64_psbt, std::optional<uint32_t> timelock)
      41                 :             : {
      42                 :          11 :     util::Result<PartiallySignedTransaction> psbt = DecodeBase64PSBT(base64_psbt);
      43   [ +  -  +  - ]:          22 :     BOOST_CHECK(psbt);
      44                 :             : 
      45         [ +  - ]:          11 :     std::optional<uint32_t> computed_timelock = psbt->ComputeTimeLock();
      46         [ +  - ]:          11 :     std::optional<CMutableTransaction> tx = psbt->GetUnsignedTx();
      47         [ +  + ]:          11 :     if (timelock) {
      48   [ +  -  +  -  :          18 :         BOOST_CHECK(computed_timelock);
                   +  - ]
      49   [ +  -  +  - ]:           9 :         BOOST_CHECK_EQUAL(*computed_timelock, *timelock);
      50   [ +  -  +  -  :          18 :         BOOST_CHECK(tx);
                   +  - ]
      51   [ +  -  +  - ]:           9 :         BOOST_CHECK_EQUAL(tx->nLockTime, *timelock);
      52                 :             :     } else {
      53   [ +  -  +  -  :           4 :         BOOST_CHECK(!computed_timelock);
                   +  - ]
      54   [ +  -  +  -  :          13 :         BOOST_CHECK(!tx);
                   +  + ]
      55                 :             :     }
      56                 :          11 : }
      57                 :             : 
      58   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(psbt2_timelock_test)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      59                 :             : {
      60         [ +  - ]:           1 :     CheckTimeLock("cHNidP8BAgQCAAAAAQQBAQEFAQIB+wQCAAAAAAEOIAsK2SFBnByHGXNdctxzn56p4GONH+TB7vD5lECEgV/IAQ8EAAAAAAABAwgACK8vAAAAAAEEFgAUxDD2TEdW2jENvRoIVXLvKZkmJywAAQMIi73rCwAAAAABBBYAFE3Rk6yWSlasG54cyoRU/i9HT4UTAA==", 0);
      61         [ +  - ]:           1 :     CheckTimeLock("cHNidP8BAgQCAAAAAQMEAAAAAAEEAQIBBQEBAfsEAgAAAAABDiAPdY2/vU2nwWyKMwnDyB4RAPVh6mRttbAXUsSF4b3enwEPBAEAAAAAAQ4gOhs7PIN9ZInqejHY5sfdUDwAG+8+BpWOdXSAjWjKeKUBDwQAAAAAAAEDCE+TNXcAAAAAAQQWABQLE1LKzQPPaqG388jWOIZxs0peEQA=", 0);
      62         [ +  - ]:           1 :     CheckTimeLock("cHNidP8BAgQCAAAAAQMEAAAAAAEEAQIBBQEBAfsEAgAAAAABDiAPdY2/vU2nwWyKMwnDyB4RAPVh6mRttbAXUsSF4b3enwEPBAEAAAABEgQQJwAAAAEOIDobOzyDfWSJ6nox2ObH3VA8ABvvPgaVjnV0gI1oynilAQ8EAAAAAAABAwhPkzV3AAAAAAEEFgAUCxNSys0Dz2qht/PI1jiGcbNKXhEA", 10000);
      63         [ +  - ]:           1 :     CheckTimeLock("cHNidP8BAgQCAAAAAQMEAAAAAAEEAQIBBQEBAfsEAgAAAAABDiAPdY2/vU2nwWyKMwnDyB4RAPVh6mRttbAXUsSF4b3enwEPBAEAAAABEgQQJwAAAAEOIDobOzyDfWSJ6nox2ObH3VA8ABvvPgaVjnV0gI1oynilAQ8EAAAAAAESBCgjAAAAAQMIT5M1dwAAAAABBBYAFAsTUsrNA89qobfzyNY4hnGzSl4RAA==", 10000);
      64         [ +  - ]:           1 :     CheckTimeLock("cHNidP8BAgQCAAAAAQMEAAAAAAEEAQIBBQEBAfsEAgAAAAABDiAPdY2/vU2nwWyKMwnDyB4RAPVh6mRttbAXUsSF4b3enwEPBAEAAAABEgQQJwAAAAEOIDobOzyDfWSJ6nox2ObH3VA8ABvvPgaVjnV0gI1oynilAQ8EAAAAAAERBIyNxGIBEgQoIwAAAAEDCE+TNXcAAAAAAQQWABQLE1LKzQPPaqG388jWOIZxs0peEQA=", 10000);
      65         [ +  - ]:           1 :     CheckTimeLock("cHNidP8BAgQCAAAAAQMEAAAAAAEEAQIBBQEBAfsEAgAAAAABDiAPdY2/vU2nwWyKMwnDyB4RAPVh6mRttbAXUsSF4b3enwEPBAEAAAABEQSLjcRiARIEECcAAAABDiA6Gzs8g31kiep6Mdjmx91QPAAb7z4GlY51dICNaMp4pQEPBAAAAAABEQSMjcRiARIEKCMAAAABAwhPkzV3AAAAAAEEFgAUCxNSys0Dz2qht/PI1jiGcbNKXhEA", 10000);
      66         [ +  - ]:           1 :     CheckTimeLock("cHNidP8BAgQCAAAAAQMEAAAAAAEEAQIBBQEBAfsEAgAAAAABDiAPdY2/vU2nwWyKMwnDyB4RAPVh6mRttbAXUsSF4b3enwEPBAEAAAABEQSLjcRiAAEOIDobOzyDfWSJ6nox2ObH3VA8ABvvPgaVjnV0gI1oynilAQ8EAAAAAAERBIyNxGIBEgQoIwAAAAEDCE+TNXcAAAAAAQQWABQLE1LKzQPPaqG388jWOIZxs0peEQA=", 1657048460);
      67         [ +  - ]:           1 :     CheckTimeLock("cHNidP8BAgQCAAAAAQMEAAAAAAEEAQIBBQEBAfsEAgAAAAABDiAPdY2/vU2nwWyKMwnDyB4RAPVh6mRttbAXUsSF4b3enwEPBAEAAAABEQSLjcRiARIEECcAAAABDiA6Gzs8g31kiep6Mdjmx91QPAAb7z4GlY51dICNaMp4pQEPBAAAAAABEQSMjcRiAAEDCE+TNXcAAAAAAQQWABQLE1LKzQPPaqG388jWOIZxs0peEQA=", 1657048460);
      68         [ +  - ]:           1 :     CheckTimeLock("cHNidP8BAgQCAAAAAQMEAAAAAAEEAQIBBQEBAfsEAgAAAAABDiAPdY2/vU2nwWyKMwnDyB4RAPVh6mRttbAXUsSF4b3enwEPBAEAAAAAAQ4gOhs7PIN9ZInqejHY5sfdUDwAG+8+BpWOdXSAjWjKeKUBDwQAAAAAAREEjI3EYgABAwhPkzV3AAAAAAEEFgAUCxNSys0Dz2qht/PI1jiGcbNKXhEA", 1657048460);
      69         [ +  - ]:           1 :     CheckTimeLock("cHNidP8BAgQCAAAAAQMEAAAAAAEEAQIBBQEBAfsEAgAAAAABDiAPdY2/vU2nwWyKMwnDyB4RAPVh6mRttbAXUsSF4b3enwEPBAEAAAABEgQQJwAAAAEOIDobOzyDfWSJ6nox2ObH3VA8ABvvPgaVjnV0gI1oynilAQ8EAAAAAAERBIyNxGIAAQMIT5M1dwAAAAABBBYAFAsTUsrNA89qobfzyNY4hnGzSl4RAA==", std::nullopt);
      70         [ +  - ]:           1 :     CheckTimeLock("cHNidP8BAgQCAAAAAQMEAAAAAAEEAQIBBQEBAfsEAgAAAAABDiA6Gzs8g31kiep6Mdjmx91QPAAb7z4GlY51dICNaMp4pQEPBAAAAAABEQSMjcRiAAEOIA91jb+9TafBbIozCcPIHhEA9WHqZG21sBdSxIXhvd6fAQ8EAQAAAAESBBAnAAAAAQMIT5M1dwAAAAABBBYAFAsTUsrNA89qobfzyNY4hnGzSl4RAA==", std::nullopt);
      71                 :           1 : }
      72                 :             : 
      73   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(psbt2_addinput)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      74                 :             : {
      75                 :           1 :     FastRandomContext rng(/*fDeterministic=*/true);
      76                 :             : 
      77         [ +  - ]:           1 :     CMutableTransaction mtx;
      78         [ +  - ]:           1 :     PartiallySignedTransaction psbt(mtx, /*version=*/2);
      79         [ -  + ]:           1 :     psbt.m_tx_modifiable.emplace();
      80         [ +  - ]:           1 :     psbt.m_tx_modifiable->set(INPUTS_MODIFIABLE);
      81   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 0);
                   +  - ]
      82                 :             : 
      83                 :             :     // Same PSBT version is required
      84                 :           1 :     uint256 txid;
      85                 :           1 :     rng.fillrand(MakeWritableByteSpan(txid));
      86                 :           1 :     PSBTInput psbtin_v0(/*psbt_version=*/0, Txid::FromUint256(txid), /*prev_out=*/0);
      87   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt.AddInput(psbtin_v0));
             +  -  +  - ]
      88   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 0);
                   +  - ]
      89                 :           1 :     rng.fillrand(MakeWritableByteSpan(txid));
      90                 :           1 :     PSBTInput psbtin(/*psbt_version=*/2, Txid::FromUint256(txid), /*prev_out=*/0);
      91   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt.AddInput(psbtin));
             +  -  +  - ]
      92   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 1);
                   +  - ]
      93                 :             : 
      94                 :             :     // Duplicates are not allowed
      95   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt.AddInput(psbtin));
             +  -  +  - ]
      96   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 1);
                   +  - ]
      97                 :             : 
      98                 :             :     // Input with a unique txid is allowed
      99                 :           1 :     rng.fillrand(MakeWritableByteSpan(txid));
     100                 :           1 :     PSBTInput psbtin2(/*psbt_version=*/2, Txid::FromUint256(txid), /*prev_out=*/0);
     101   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt.AddInput(psbtin2));
             +  -  +  - ]
     102   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 2);
                   +  - ]
     103                 :             : 
     104                 :             :     // Disabling inputs modifiable flag prevents adding new inputs
     105                 :           1 :     psbt.m_tx_modifiable->reset(INPUTS_MODIFIABLE);
     106                 :           1 :     rng.fillrand(MakeWritableByteSpan(txid));
     107                 :           1 :     PSBTInput psbtin3(/*psbt_version=*/2, Txid::FromUint256(txid), /*prev_out=*/0);
     108   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt.AddInput(psbtin3));
             +  -  +  - ]
     109   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 2);
                   +  - ]
     110                 :           1 :     psbt.m_tx_modifiable->set(INPUTS_MODIFIABLE);
     111                 :             : 
     112                 :             :     // Make sure that timelock compatibility checks are working
     113                 :             :     // No previous required timelocks, new input with both height and time timelocks is allowed
     114                 :           1 :     rng.fillrand(MakeWritableByteSpan(txid));
     115                 :           1 :     PSBTInput psbtin4(/*psbt_version=*/2, Txid::FromUint256(txid), /*prev_out=*/0);
     116                 :           1 :     psbtin4.time_locktime = LOCKTIME_THRESHOLD;
     117         [ -  + ]:           1 :     psbtin4.height_locktime = 100;
     118   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt.AddInput(psbtin4));
             +  -  +  - ]
     119   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 3);
                   +  - ]
     120                 :             : 
     121                 :             :     // Input with only a time timelock is allowed
     122                 :           1 :     rng.fillrand(MakeWritableByteSpan(txid));
     123                 :           1 :     PSBTInput psbtin5(/*psbt_version=*/2, Txid::FromUint256(txid), /*prev_out=*/0);
     124                 :           1 :     psbtin5.time_locktime = LOCKTIME_THRESHOLD + 1;
     125   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt.AddInput(psbtin5));
             +  -  +  - ]
     126   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 4);
                   +  - ]
     127                 :             : 
     128                 :             :     // Input with only a height timelock is not allowed because of previous
     129                 :           1 :     rng.fillrand(MakeWritableByteSpan(txid));
     130                 :           1 :     PSBTInput psbtin6(/*psbt_version=*/2, Txid::FromUint256(txid), /*prev_out=*/0);
     131         [ -  + ]:           1 :     psbtin6.height_locktime = 100;
     132   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt.AddInput(psbtin6));
             +  -  +  - ]
     133   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 4);
                   +  - ]
     134                 :             : 
     135                 :             :     // Adding an input that already has a signature is allowed
     136                 :           1 :     rng.fillrand(MakeWritableByteSpan(txid));
     137                 :           1 :     PSBTInput psbtin7(/*psbt_version=*/2, Txid::FromUint256(txid), /*prev_out=*/0);
     138         [ +  - ]:           1 :     psbtin7.final_script_sig << OP_1;
     139   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt.AddInput(psbtin7));
             +  -  +  - ]
     140   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 5);
                   +  - ]
     141                 :             : 
     142                 :             :     // Same thing, but with other things that have signatures
     143                 :           1 :     psbtin7.final_script_sig.clear();
     144   [ +  -  +  -  :           2 :     psbtin7.final_script_witness = CScriptWitness{std::vector<std::vector<unsigned char>>(1)};
             -  +  +  - ]
     145   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt.AddInput(psbtin7));
             +  -  +  - ]
     146   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 5);
                   +  - ]
     147   [ +  -  +  - ]:           2 :     psbtin7.final_script_witness = {};
     148         [ +  - ]:           1 :     psbtin7.partial_sigs.emplace();
     149   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt.AddInput(psbtin7));
             +  -  +  - ]
     150   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 5);
                   +  - ]
     151                 :           1 :     psbtin7.partial_sigs.clear();
     152         [ +  - ]:           1 :     psbtin7.m_tap_key_sig.push_back(0);
     153   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt.AddInput(psbtin7));
             +  -  +  - ]
     154   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 5);
                   +  - ]
     155         [ +  - ]:           1 :     psbtin7.m_tap_key_sig.clear();
     156         [ +  - ]:           1 :     psbtin7.m_tap_script_sigs.emplace();
     157   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt.AddInput(psbtin7));
             +  -  +  - ]
     158   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 5);
                   +  - ]
     159                 :           1 :     psbtin7.m_tap_script_sigs.clear();
     160         [ +  - ]:           1 :     psbtin7.m_musig2_partial_sigs.emplace();
     161   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt.AddInput(psbtin7));
             +  -  +  - ]
     162   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 5);
                   +  - ]
     163                 :             : 
     164                 :             :     // Adding an input that changes the timelock is no longer allowed
     165                 :           1 :     rng.fillrand(MakeWritableByteSpan(txid));
     166                 :           1 :     PSBTInput psbtin8(/*psbt_version=*/2, Txid::FromUint256(txid), /*prev_out=*/0);
     167                 :           1 :     psbtin8.time_locktime = LOCKTIME_THRESHOLD + 2;
     168   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt.AddInput(psbtin8));
             +  -  +  - ]
     169   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.inputs.size(), 5);
                   +  - ]
     170                 :           2 : }
     171                 :             : 
     172   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(psbt2_addoutput)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     173                 :             : {
     174                 :           1 :     CMutableTransaction mtx;
     175         [ +  - ]:           1 :     PartiallySignedTransaction psbt(mtx, /*version=*/2);
     176         [ -  + ]:           1 :     psbt.m_tx_modifiable.emplace();
     177         [ +  - ]:           1 :     psbt.m_tx_modifiable->set(OUTPUTS_MODIFIABLE);
     178   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.outputs.size(), 0);
                   +  - ]
     179                 :             : 
     180                 :             :     // Same PSBT version is required
     181                 :           1 :     PSBTOutput psbtout_v0(/*psbt_version=*/0, /*amount=*/1, CScript());
     182   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt.AddOutput(psbtout_v0));
             +  -  +  - ]
     183   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.outputs.size(), 0);
                   +  - ]
     184                 :           1 :     PSBTOutput psbtout(/*psbt_version=*/2, /*amount=*/1, CScript());
     185   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt.AddOutput(psbtout));
             +  -  +  - ]
     186   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.outputs.size(), 1);
                   +  - ]
     187                 :             : 
     188                 :             :     // Disabling outputs modifiable flag prevents adding new outputs
     189                 :           1 :     psbt.m_tx_modifiable->reset(OUTPUTS_MODIFIABLE);
     190                 :           1 :     PSBTOutput psbtout2(/*psbt_version=*/2, /*amount=*/1, CScript());
     191   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt.AddOutput(psbtout2));
             +  -  +  - ]
     192   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.outputs.size(), 1);
                   +  - ]
     193                 :           1 :     psbt.m_tx_modifiable->set(OUTPUTS_MODIFIABLE);
     194                 :           1 :     PSBTOutput psbtout3(/*psbt_version=*/2, /*amount=*/1, CScript());
     195   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt.AddOutput(psbtout3));
             +  -  +  - ]
     196   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(psbt.outputs.size(), 2);
                   +  - ]
     197                 :           2 : }
     198                 :             : 
     199   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(merge_proprietary_fields)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     200                 :             : {
     201                 :           1 :     CMutableTransaction tx;
     202         [ +  - ]:           1 :     tx.vin.emplace_back(COutPoint{});
     203         [ +  - ]:           1 :     tx.vout.emplace_back(0, CScript{});
     204                 :             : 
     205         [ +  - ]:           1 :     PartiallySignedTransaction left(tx, /*version=*/2);
     206         [ +  - ]:           1 :     PartiallySignedTransaction right(tx, /*version=*/2);
     207                 :             : 
     208         [ +  - ]:           1 :     const auto left_prop = MakeProprietary(/*subtype=*/1, /*key_data=*/0x01, /*value=*/0xaa);
     209         [ +  - ]:           1 :     const auto right_prop = MakeProprietary(/*subtype=*/2, /*key_data=*/0x02, /*value=*/0xbb);
     210                 :             : 
     211         [ +  - ]:           1 :     left.m_proprietary.insert(left_prop);
     212         [ +  - ]:           1 :     left.inputs[0].m_proprietary.insert(left_prop);
     213         [ +  - ]:           1 :     left.outputs[0].m_proprietary.insert(left_prop);
     214                 :             : 
     215         [ +  - ]:           1 :     right.m_proprietary.insert(right_prop);
     216         [ +  - ]:           1 :     right.inputs[0].m_proprietary.insert(right_prop);
     217         [ +  - ]:           1 :     right.outputs[0].m_proprietary.insert(right_prop);
     218                 :             : 
     219   [ +  -  +  -  :           2 :     BOOST_REQUIRE(left.Merge(right));
             +  -  +  - ]
     220                 :             : 
     221   [ +  -  +  - ]:           1 :     BOOST_REQUIRE_EQUAL(left.m_proprietary.size(), 2U);
     222   [ +  -  +  - ]:           1 :     BOOST_REQUIRE_EQUAL(left.inputs[0].m_proprietary.size(), 2U);
     223   [ +  -  +  - ]:           1 :     BOOST_REQUIRE_EQUAL(left.outputs[0].m_proprietary.size(), 2U);
     224                 :             : 
     225                 :           1 :     const auto global_it = left.m_proprietary.find(right_prop);
     226   [ +  -  +  -  :           2 :     BOOST_REQUIRE(global_it != left.m_proprietary.end());
                   +  - ]
     227   [ +  -  +  - ]:           2 :     BOOST_CHECK(global_it->value == right_prop.value);
     228                 :             : 
     229                 :           1 :     const auto input_it = left.inputs[0].m_proprietary.find(right_prop);
     230   [ +  -  +  -  :           2 :     BOOST_REQUIRE(input_it != left.inputs[0].m_proprietary.end());
                   +  - ]
     231   [ +  -  +  - ]:           2 :     BOOST_CHECK(input_it->value == right_prop.value);
     232                 :             : 
     233                 :           1 :     const auto output_it = left.outputs[0].m_proprietary.find(right_prop);
     234   [ +  -  +  -  :           2 :     BOOST_REQUIRE(output_it != left.outputs[0].m_proprietary.end());
                   +  - ]
     235   [ +  -  +  - ]:           2 :     BOOST_CHECK(output_it->value == right_prop.value);
     236                 :           2 : }
     237                 :             : 
     238                 :          12 : struct PSBTOutputTest {
     239                 :             :     CPubKey pubkey;
     240                 :             :     FlatSigningProvider provider;
     241                 :             :     CScript script_pubkey;
     242                 :             : 
     243                 :          12 :     explicit PSBTOutputTest(std::string descriptor)
     244                 :          12 :     {
     245                 :          12 :         CKey key{GenerateRandomKey()};
     246         [ +  - ]:          12 :         pubkey = key.GetPubKey();
     247   [ +  -  +  - ]:          12 :         provider.keys.emplace(pubkey.GetID(), key);
     248                 :             : 
     249   [ +  -  +  - ]:          24 :         util::ReplaceAll(descriptor, "<KEY>", HexStr(pubkey));
     250         [ -  + ]:          12 :         std::string error;
     251   [ -  +  +  - ]:          12 :         auto descriptors{Parse(descriptor, provider, error, /*require_checksum=*/false)};
     252   [ +  -  +  -  :          24 :         BOOST_REQUIRE_MESSAGE(!descriptors.empty(), error);
                   +  - ]
     253                 :          12 :         std::vector<CScript> output_scripts;
     254   [ +  -  +  -  :          24 :         BOOST_REQUIRE(descriptors[0]->Expand(/*pos=*/0, provider, output_scripts, provider));
             +  -  +  - ]
     255   [ +  -  -  +  :          12 :         BOOST_REQUIRE_EQUAL(output_scripts.size(), 1);
                   +  - ]
     256                 :          12 :         script_pubkey = output_scripts[0];
     257                 :          12 :     }
     258                 :             : 
     259                 :          12 :     PSBTOutput UpdateOutput(bool has_input) const
     260                 :             :     {
     261                 :          12 :         CMutableTransaction tx;
     262   [ +  +  +  - ]:          12 :         if (has_input) tx.vin.emplace_back();
     263         [ +  - ]:          12 :         tx.vout.emplace_back(0, script_pubkey);
     264         [ +  - ]:          12 :         PartiallySignedTransaction psbt{tx, /*version=*/2};
     265         [ +  - ]:          12 :         UpdatePSBTOutput(provider, psbt, 0);
     266         [ +  - ]:          12 :         return psbt.outputs[0];
     267                 :          24 :     }
     268                 :             : };
     269                 :             : 
     270   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(update_psbt_output_keypaths)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     271                 :             : {
     272         [ +  + ]:           3 :     for (bool has_input : {false, true}) {
     273         [ +  + ]:           6 :         for (const auto& descriptor : {"pkh(<KEY>)", "wpkh(<KEY>)"}) {
     274         [ +  - ]:           4 :             PSBTOutputTest test{descriptor};
     275         [ +  - ]:           4 :             auto out{test.UpdateOutput(has_input)};
     276   [ +  -  +  -  :           8 :             BOOST_CHECK(out.hd_keypaths.contains(test.pubkey));
                   +  - ]
     277   [ +  -  -  +  :           8 :             BOOST_CHECK(out.redeem_script.empty());
             +  -  +  - ]
     278   [ +  -  -  +  :           8 :             BOOST_CHECK(out.witness_script.empty());
                   +  - ]
     279                 :           8 :         }
     280                 :             :     }
     281                 :           1 : }
     282                 :             : 
     283   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(update_psbt_output_redeem_script)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     284                 :             : {
     285         [ +  + ]:           3 :     for (bool has_input : {false, true}) {
     286         [ +  - ]:           2 :         PSBTOutputTest test{"sh(wpkh(<KEY>))"};
     287         [ +  - ]:           2 :         auto out{test.UpdateOutput(has_input)};
     288   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(out.redeem_script, GetScriptForDestination(WitnessV0KeyHash{test.pubkey}));
             +  -  +  - ]
     289   [ +  -  +  - ]:           4 :         BOOST_CHECK(out.hd_keypaths.contains(test.pubkey));
     290                 :           4 :     }
     291                 :           1 : }
     292                 :             : 
     293   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(update_psbt_output_witness_script)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     294                 :             : {
     295         [ +  + ]:           3 :     for (bool has_input : {false, true}) {
     296         [ +  - ]:           2 :         PSBTOutputTest test{"wsh(pk(<KEY>))"};
     297         [ +  - ]:           2 :         auto out{test.UpdateOutput(has_input)};
     298   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(out.witness_script, GetScriptForRawPubKey(test.pubkey));
                   +  - ]
     299   [ +  -  +  - ]:           4 :         BOOST_CHECK(out.hd_keypaths.contains(test.pubkey));
     300                 :           4 :     }
     301                 :           1 : }
     302                 :             : 
     303   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(update_psbt_output_miniscript_timelock)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     304                 :             : {
     305         [ +  + ]:           3 :     for (bool has_input : {false, true}) {
     306         [ +  - ]:           2 :         PSBTOutputTest test{"wsh(and_v(v:pk(<KEY>),older(144)))"};
     307         [ +  - ]:           2 :         auto out{test.UpdateOutput(has_input)};
     308   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(GetScriptForDestination(WitnessV0ScriptHash{out.witness_script}), test.script_pubkey);
             +  -  +  - ]
     309   [ +  -  +  - ]:           4 :         BOOST_CHECK(out.hd_keypaths.contains(test.pubkey));
     310                 :           4 :     }
     311                 :           1 : }
     312                 :             : 
     313   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(update_psbt_output_taproot)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     314                 :             : {
     315         [ +  + ]:           3 :     for (bool has_input : {false, true}) {
     316         [ +  - ]:           2 :         PSBTOutputTest test{"rawtr(<KEY>)"};
     317         [ +  - ]:           2 :         auto out{test.UpdateOutput(has_input)};
     318   [ +  -  +  -  :           4 :         BOOST_CHECK(out.m_tap_bip32_paths.contains(XOnlyPubKey{test.pubkey}));
                   +  - ]
     319   [ +  -  +  - ]:           4 :         BOOST_CHECK(out.hd_keypaths.empty());
     320                 :           4 :     }
     321                 :           1 : }
     322                 :             : 
     323   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(psbt_merge_only_same_version)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     324                 :             : {
     325                 :           1 :     CMutableTransaction mtx;
     326         [ +  - ]:           1 :     PartiallySignedTransaction psbt_v0(mtx, /*version*/0);
     327         [ +  - ]:           1 :     PartiallySignedTransaction psbt_v2(mtx, /*version*/2);
     328   [ +  -  +  -  :           2 :     BOOST_REQUIRE(psbt_v0.GetUniqueID() == psbt_v2.GetUniqueID());
          +  -  +  -  +  
                      - ]
     329   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt_v0.Merge(psbt_v2));
             +  -  +  - ]
     330   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt_v2.Merge(psbt_v0));
                   +  - ]
     331                 :           2 : }
     332                 :             : 
     333   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(psbt_merge_fallback_locktime)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     334                 :             : {
     335                 :           1 :     CMutableTransaction mtx;
     336         [ +  - ]:           1 :     PartiallySignedTransaction psbt1(mtx, /*version*/2);
     337         [ +  - ]:           1 :     PartiallySignedTransaction psbt2(mtx, /*version*/2);
     338         [ -  + ]:           1 :     psbt1.m_tx_modifiable.emplace();
     339         [ +  - ]:           1 :     psbt1.m_tx_modifiable->set(INPUTS_MODIFIABLE);
     340   [ +  -  -  +  :           1 :     BOOST_REQUIRE_EQUAL(psbt1.inputs.size(), 0);
                   +  - ]
     341         [ -  + ]:           1 :     psbt2.m_tx_modifiable.emplace();
     342         [ +  - ]:           1 :     psbt2.m_tx_modifiable->set(INPUTS_MODIFIABLE);
     343   [ +  -  -  +  :           1 :     BOOST_REQUIRE_EQUAL(psbt2.inputs.size(), 0);
                   +  - ]
     344                 :             : 
     345                 :             : 
     346                 :           1 :     PSBTInput psbtin(/*psbt_version=*/2, Txid::FromUint256(uint256::ONE), /*prev_out=*/0);
     347         [ -  + ]:           1 :     psbtin.height_locktime = 100;
     348   [ +  -  +  -  :           2 :     BOOST_REQUIRE(psbt1.AddInput(psbtin));
             +  -  +  - ]
     349   [ +  -  +  -  :           2 :     BOOST_REQUIRE(psbt2.AddInput(psbtin));
             +  -  +  - ]
     350                 :             : 
     351         [ +  - ]:           1 :     psbt1.fallback_locktime = std::nullopt;
     352         [ +  - ]:           1 :     psbt2.fallback_locktime = 21;
     353   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt1.Merge(psbt2));
             +  -  +  - ]
     354   [ +  -  +  -  :           2 :     BOOST_REQUIRE(psbt1.fallback_locktime.has_value());
                   +  - ]
     355   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(*psbt1.fallback_locktime, 21);
     356                 :             : 
     357         [ +  - ]:           1 :     psbt1.fallback_locktime = 5;
     358         [ +  - ]:           1 :     psbt2.fallback_locktime = 19;
     359   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt1.Merge(psbt2));
             +  -  +  - ]
     360   [ +  -  +  -  :           2 :     BOOST_REQUIRE(psbt1.fallback_locktime.has_value());
                   +  - ]
     361   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(*psbt1.fallback_locktime, 5);
     362                 :             : 
     363         [ +  - ]:           1 :     psbt1.fallback_locktime = 21;
     364         [ +  - ]:           1 :     psbt2.fallback_locktime = std::nullopt;
     365   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt1.Merge(psbt2));
             +  -  +  - ]
     366   [ +  -  +  -  :           2 :     BOOST_REQUIRE(psbt1.fallback_locktime.has_value());
                   +  - ]
     367   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(*psbt1.fallback_locktime, 21);
     368                 :           2 : }
     369                 :             : 
     370   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(psbt_merge_modifiable)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     371                 :             : {
     372                 :           1 :     CMutableTransaction mtx;
     373         [ +  - ]:           1 :     PartiallySignedTransaction psbt1(mtx, /*version*/2);
     374         [ +  - ]:           1 :     PartiallySignedTransaction psbt2(mtx, /*version*/2);
     375                 :             : 
     376   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt1.Merge(psbt2));
             +  -  +  - ]
     377   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt1.m_tx_modifiable.has_value());
                   -  + ]
     378                 :             : 
     379         [ -  + ]:           1 :     psbt1.m_tx_modifiable.emplace();
     380         [ +  - ]:           1 :     psbt1.m_tx_modifiable->set(INPUTS_MODIFIABLE);
     381   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt1.Merge(psbt2));
             +  -  +  - ]
     382   [ +  -  +  -  :           2 :     BOOST_REQUIRE(psbt1.m_tx_modifiable.has_value());
                   +  - ]
     383   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt1.m_tx_modifiable->test(INPUTS_MODIFIABLE));
                   +  - ]
     384                 :             : 
     385         [ +  - ]:           1 :     psbt1.m_tx_modifiable.reset();
     386         [ -  + ]:           1 :     psbt2.m_tx_modifiable.emplace();
     387         [ +  - ]:           1 :     psbt2.m_tx_modifiable->set(INPUTS_MODIFIABLE);
     388                 :           1 :     psbt2.m_tx_modifiable->set(HAS_SIGHASH_SINGLE);
     389   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt1.Merge(psbt2));
             +  -  +  - ]
     390   [ +  -  +  -  :           2 :     BOOST_REQUIRE(psbt1.m_tx_modifiable.has_value());
                   +  - ]
     391   [ +  -  +  -  :           2 :     BOOST_CHECK(!psbt1.m_tx_modifiable->test(INPUTS_MODIFIABLE));
                   +  - ]
     392   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt1.m_tx_modifiable->test(HAS_SIGHASH_SINGLE));
                   +  - ]
     393                 :             : 
     394         [ +  - ]:           1 :     psbt1.m_tx_modifiable.emplace();
     395         [ +  - ]:           1 :     psbt1.m_tx_modifiable->set(HAS_SIGHASH_SINGLE);
     396         [ +  - ]:           1 :     psbt2.m_tx_modifiable.reset();
     397   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt1.Merge(psbt2));
             +  -  +  - ]
     398   [ +  -  +  -  :           2 :     BOOST_REQUIRE(psbt1.m_tx_modifiable.has_value());
                   +  - ]
     399   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt1.m_tx_modifiable->test(HAS_SIGHASH_SINGLE));
                   +  - ]
     400                 :             : 
     401         [ +  - ]:           1 :     psbt1.m_tx_modifiable.reset();
     402         [ -  + ]:           1 :     psbt2.m_tx_modifiable.emplace();
     403         [ +  - ]:           1 :     psbt2.m_tx_modifiable->set(HAS_SIGHASH_SINGLE);
     404   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt1.Merge(psbt2));
             +  -  +  - ]
     405   [ +  -  +  -  :           2 :     BOOST_REQUIRE(psbt1.m_tx_modifiable.has_value());
                   +  - ]
     406   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt1.m_tx_modifiable->test(HAS_SIGHASH_SINGLE));
                   +  - ]
     407                 :             : 
     408         [ +  - ]:           1 :     psbt1.m_tx_modifiable.emplace();
     409         [ +  - ]:           1 :     psbt1.m_tx_modifiable->set(INPUTS_MODIFIABLE);
     410                 :           1 :     psbt1.m_tx_modifiable->set(OUTPUTS_MODIFIABLE);
     411         [ +  - ]:           1 :     psbt2.m_tx_modifiable.emplace();
     412         [ +  - ]:           1 :     psbt2.m_tx_modifiable->set(INPUTS_MODIFIABLE);
     413                 :           1 :     psbt2.m_tx_modifiable->set(OUTPUTS_MODIFIABLE);
     414   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt1.Merge(psbt2));
             +  -  +  - ]
     415   [ +  -  +  -  :           2 :     BOOST_REQUIRE(psbt1.m_tx_modifiable.has_value());
                   +  - ]
     416   [ +  -  +  -  :           2 :     BOOST_CHECK(psbt1.m_tx_modifiable->test(INPUTS_MODIFIABLE) && psbt1.m_tx_modifiable->test(OUTPUTS_MODIFIABLE));
             -  +  +  - ]
     417                 :           2 : }
     418                 :             : 
     419                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1