LCOV - code coverage report
Current view: top level - src/test - bip352_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 250 250
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 16 16
Branches: 51.0 % 1660 847

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2026-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 <common/bip352.h>
       6                 :             : #include <chainparams.h>
       7                 :             : #include <coins.h>
       8                 :             : #include <span.h>
       9                 :             : #include <addresstype.h>
      10                 :             : #include <policy/policy.h>
      11                 :             : #include <script/interpreter.h>
      12                 :             : #include <script/script.h>
      13                 :             : #include <script/script_error.h>
      14                 :             : #include <script/solver.h>
      15                 :             : #include <test/data/bip352_send_and_receive_vectors.json.h>
      16                 :             : 
      17                 :             : #include <test/util/common.h>
      18                 :             : #include <test/util/setup_common.h>
      19                 :             : 
      20                 :             : #include <boost/test/unit_test.hpp>
      21                 :             : #include <test/util/json.h>
      22                 :             : #include <vector>
      23                 :             : #include <util/chaintype.h>
      24                 :             : #include <util/strencodings.h>
      25                 :             : #include <streams.h>
      26                 :             : 
      27                 :             : namespace bip352 {
      28                 :             : BOOST_FIXTURE_TEST_SUITE(bip352_tests, BasicTestingSetup)
      29                 :             : 
      30                 :         117 : CKey ParseHexToCKey(std::string_view hex) {
      31                 :         117 :     CKey output;
      32         [ +  - ]:         117 :     std::vector<unsigned char> hex_data = ParseHex(hex);
      33         [ +  - ]:         117 :     output.Set(hex_data.begin(), hex_data.end(), true);
      34                 :         117 :     return output;
      35                 :         117 : };
      36                 :             : 
      37   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(bip352_send_and_receive_test_vectors)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      38                 :             : {
      39         [ +  - ]:           1 :     UniValue tests;
      40   [ +  -  +  -  :           2 :     BOOST_REQUIRE(tests.read(json_tests::bip352_send_and_receive_vectors));
             +  -  +  - ]
      41                 :             : 
      42   [ +  -  +  + ]:          29 :     for (const auto& vec : tests.getValues()) {
      43                 :             :         // run sending tests
      44   [ +  -  +  -  :          28 :         BOOST_TEST_MESSAGE(vec["comment"].get_str());
          +  -  +  -  +  
                -  +  - ]
      45                 :             : 
      46   [ +  -  +  -  :          56 :         for (const auto& sender : vec["sending"].getValues()) {
             +  -  +  + ]
      47   [ +  -  +  - ]:          28 :             const UniValue& given = sender["given"];
      48   [ +  -  +  - ]:          28 :             const UniValue& expected = sender["expected"];
      49                 :             : 
      50                 :          28 :             std::vector<COutPoint> outpoints;
      51                 :          28 :             std::vector<CKey> keys;
      52                 :          28 :             std::vector<KeyPair> taproot_keys;
      53   [ +  -  +  -  :          88 :             for (const auto& input : given["vin"].getValues()) {
             +  -  +  + ]
      54   [ +  -  +  -  :         120 :                 COutPoint outpoint{Txid::FromHex(input["txid"].get_str()).value(), input["vout"].getInt<uint32_t>()};
          +  -  -  +  +  
          -  +  -  +  -  
                   +  - ]
      55         [ +  - ]:          60 :                 outpoints.push_back(outpoint);
      56   [ +  -  +  -  :          60 :                 const auto& spk_bytes = ParseHex(input["prevout"]["scriptPubKey"]["hex"].get_str());
          +  -  +  -  +  
          -  +  -  +  -  
             -  +  +  - ]
      57                 :          60 :                 CScript spk = CScript(spk_bytes.begin(), spk_bytes.end());
      58   [ +  -  +  -  :          60 :                 const auto& script_sig_bytes = ParseHex(input["scriptSig"].get_str());
          +  -  -  +  +  
                      - ]
      59                 :          60 :                 CScript script_sig = CScript(script_sig_bytes.begin(), script_sig_bytes.end());
      60         [ +  - ]:          60 :                 CTxIn txin{outpoint, script_sig};
      61                 :             :                 // Deserialize the txinwitness field into txin.scriptWitness.
      62   [ +  -  +  -  :          60 :                 const auto witness_str = ParseHex(input["txinwitness"].get_str());
          +  -  -  +  +  
                      - ]
      63         [ +  + ]:          60 :                 if (!witness_str.empty()) {
      64   [ -  +  +  - ]:          19 :                     SpanReader(witness_str) >> txin.scriptWitness;
      65                 :             :                 }
      66                 :             : 
      67                 :             :                 // check if this is a silent payments input by trying to extract the public key
      68         [ +  - ]:          60 :                 const auto& pubkey = GetPubKeyFromInput(txin, spk);
      69         [ +  + ]:          60 :                 if (pubkey.has_value()) {
      70                 :          52 :                     std::vector<std::vector<unsigned char>> solutions;
      71         [ +  - ]:          52 :                     TxoutType type = Solver(spk, solutions);
      72         [ +  + ]:          52 :                     if (type == TxoutType::WITNESS_V1_TAPROOT) {
      73   [ +  -  +  -  :          18 :                         taproot_keys.emplace_back(ParseHexToCKey(input["private_key"].get_str()).ComputeKeyPair(nullptr));
          +  -  -  +  +  
             -  +  -  +  
                      - ]
      74                 :             :                     } else {
      75   [ +  -  +  -  :          86 :                         keys.emplace_back(ParseHexToCKey(input["private_key"].get_str()));
          +  -  -  +  +  
                -  +  - ]
      76                 :             :                     }
      77                 :          52 :                 }
      78                 :          60 :             }
      79   [ +  +  +  + ]:          28 :             if (taproot_keys.empty() && keys.empty()) {
      80   [ +  -  +  -  :           2 :                 BOOST_CHECK(expected["outputs"].getValues()[0].empty());
          +  -  +  -  -  
                +  +  - ]
      81                 :           1 :                 continue;
      82                 :             :             }
      83                 :             :             // silent payments logic
      84                 :          27 :             auto smallest_outpoint = std::min_element(outpoints.begin(), outpoints.end(), BIP352Comparator());
      85         [ +  - ]:          27 :             std::map<size_t, SilentPaymentsDestination> sp_dests;
      86   [ +  -  +  -  :          27 :             const std::vector<UniValue>& silent_payments_addresses = given["recipients"].getValues();
                   +  - ]
      87                 :          27 :             size_t sp_index = 0;
      88   [ -  +  +  + ]:          63 :             for (size_t i = 0; i < silent_payments_addresses.size(); ++i) {
      89   [ +  -  +  -  :          36 :                 auto sp = DecodeSilentPaymentsAddress(silent_payments_addresses[i]["address"].get_str(), Params());
          +  -  +  -  +  
                      - ]
      90   [ +  -  +  -  :          72 :                 BOOST_REQUIRE(sp.has_value());
                   +  - ]
      91   [ +  -  +  -  :          36 :                 if (!silent_payments_addresses[i]["scan_pub_key"].isNull()) {
                   +  - ]
      92   [ +  -  +  -  :          36 :                     BOOST_CHECK_EQUAL(HexStr(sp->GetScanPubKey()), silent_payments_addresses[i]["scan_pub_key"].get_str());
          +  -  +  -  +  
                -  +  - ]
      93   [ +  -  +  -  :          36 :                     BOOST_CHECK_EQUAL(HexStr(sp->GetSpendPubKey()), silent_payments_addresses[i]["spend_pub_key"].get_str());
          +  -  +  -  +  
                -  +  - ]
      94                 :             :                 }
      95   [ +  -  +  -  :          36 :                 size_t count = silent_payments_addresses[i]["count"].isNull() ? 1 : (size_t)silent_payments_addresses[i]["count"].getInt<int>();
          +  +  +  -  +  
             -  +  -  -  
                      - ]
      96         [ +  + ]:        2395 :                 for (size_t j = 0; j < count; ++j) {
      97         [ +  - ]:        2359 :                     sp_dests.emplace(sp_index++, *sp);
      98                 :             :                 }
      99                 :          36 :             }
     100         [ +  - ]:          27 :             auto sp_tr_dests = GenerateSilentPaymentsTaprootDestinations(sp_dests, keys, taproot_keys, *smallest_outpoint);
     101                 :             :             // This means the inputs summed to zero, which realistically would only happen maliciously. In this case, just move on
     102         [ +  + ]:          27 :             if (!sp_tr_dests.has_value()) {
     103                 :             :                 // Check that we actually expect zero outputs to be generated for this test
     104   [ +  -  +  -  :           4 :                 BOOST_CHECK(expected["outputs"].getValues()[0].empty());
          +  -  +  -  -  
                +  +  - ]
     105                 :           2 :                 continue;
     106                 :             :             }
     107                 :          25 :             bool match = false;
     108   [ +  -  +  -  :          30 :             for (const auto& candidate_set : expected["outputs"].getValues()) {
             +  -  +  - ]
     109   [ +  -  -  +  :          60 :                 BOOST_CHECK(sp_tr_dests->size() == candidate_set.size());
             +  -  +  - ]
     110                 :          30 :                 std::vector<WitnessV1Taproot> expected_spks;
     111   [ +  -  +  + ]:          82 :                 for (const auto& output : candidate_set.getValues()) {
     112   [ +  -  -  +  :         104 :                     const WitnessV1Taproot tap{XOnlyPubKey(ParseHex(output.get_str()))};
                   +  - ]
     113         [ +  - ]:          52 :                     expected_spks.push_back(tap);
     114                 :             :                 }
     115                 :          69 :                 match = std::all_of(sp_tr_dests->begin(), sp_tr_dests->end(), [&](const auto& entry) {
     116                 :          39 :                     return std::find(expected_spks.begin(), expected_spks.end(), entry.second) != expected_spks.end();
     117                 :             :                 });
     118         [ +  + ]:          30 :                 if (match) break;
     119                 :          30 :             }
     120   [ +  -  +  - ]:          50 :             BOOST_CHECK(match);
     121                 :          28 :         }
     122                 :             : 
     123                 :             :         // Test receiving
     124   [ +  -  +  -  :          57 :         for (const auto& recipient : vec["receiving"].getValues()) {
             +  -  +  + ]
     125                 :             : 
     126   [ +  -  +  - ]:          29 :             const UniValue& given = recipient["given"];
     127   [ +  -  +  - ]:          29 :             const UniValue& expected = recipient["expected"];
     128                 :             : 
     129                 :          29 :             std::vector<CTxIn> vin;
     130         [ +  - ]:          29 :             std::map<COutPoint, Coin> coins;
     131   [ +  -  +  -  :          91 :             for (const auto& input : given["vin"].getValues()) {
             +  -  +  + ]
     132   [ +  -  +  -  :         124 :                 COutPoint outpoint{Txid::FromHex(input["txid"].get_str()).value(), input["vout"].getInt<uint32_t>()};
          +  -  -  +  +  
          -  +  -  +  -  
                   +  - ]
     133   [ +  -  +  -  :          62 :                 const auto& spk_bytes = ParseHex(input["prevout"]["scriptPubKey"]["hex"].get_str());
          +  -  +  -  +  
          -  +  -  +  -  
             -  +  +  - ]
     134                 :          62 :                 CScript spk = CScript(spk_bytes.begin(), spk_bytes.end());
     135   [ +  -  +  -  :          62 :                 const auto& script_sig_bytes = ParseHex(input["scriptSig"].get_str());
          +  -  -  +  +  
                      - ]
     136                 :          62 :                 CScript script_sig = CScript(script_sig_bytes.begin(), script_sig_bytes.end());
     137         [ +  - ]:          62 :                 CTxIn txin{outpoint, script_sig};
     138                 :             :                 // Deserialize the txinwitness field into txin.scriptWitness.
     139   [ +  -  +  -  :          62 :                 const auto witness_str = ParseHex(input["txinwitness"].get_str());
          +  -  -  +  +  
                      - ]
     140         [ +  + ]:          62 :                 if (!witness_str.empty()) {
     141   [ -  +  +  - ]:          19 :                     SpanReader(witness_str) >> txin.scriptWitness;
     142                 :             :                 }
     143         [ +  - ]:          62 :                 vin.push_back(txin);
     144   [ +  -  +  - ]:          62 :                 coins[outpoint] = Coin{CTxOut{{}, spk}, 0, false};
     145                 :          62 :             }
     146         [ +  - ]:          29 :             auto pub_tweak_data = GetSilentPaymentsPrevoutsSummary(vin, coins);
     147                 :             :             // If we don't get any tweak data from the transaction inputs, it is not a silent payment
     148                 :             :             // transaction, so we skip it.
     149         [ +  + ]:          29 :             if (!pub_tweak_data.has_value()) {
     150                 :             :                 // Make sure this is expected and not just a failure of the GetSilentPaymentsPrevoutsSummary func
     151   [ +  -  +  -  :           4 :                 BOOST_CHECK(expected["outputs"].empty());
          +  -  -  +  +  
                      - ]
     152                 :           2 :                 continue;
     153                 :             :             }
     154                 :          27 :             std::vector<XOnlyPubKey> output_pub_keys;
     155   [ +  -  +  -  :        2388 :             for (const auto& pubkey : given["outputs"].getValues()) {
             +  -  +  + ]
     156   [ +  -  -  +  :        4722 :                 output_pub_keys.emplace_back(ParseHex(pubkey.get_str()));
             +  -  +  - ]
     157                 :             :             }
     158                 :             : 
     159   [ +  -  +  -  :          54 :             CKey scan_priv_key = ParseHexToCKey(given["key_material"]["scan_priv_key"].get_str());
          +  -  +  -  +  
             -  -  +  +  
                      - ]
     160   [ +  -  +  -  :          54 :             CKey spend_priv_key = ParseHexToCKey(given["key_material"]["spend_priv_key"].get_str());
          +  -  +  -  +  
             -  -  +  +  
                      - ]
     161   [ +  -  +  -  :          54 :             SilentPaymentsDestination sp_address{SilentPaymentsDestination::From(scan_priv_key.GetPubKey(), spend_priv_key.GetPubKey()).value()};
                   +  - ]
     162   [ +  -  +  -  :          27 :             auto expected_address = DecodeSilentPaymentsAddress(expected["addresses"][0].get_str(), Params());
          +  -  +  -  +  
                -  +  - ]
     163   [ +  -  +  -  :          54 :             BOOST_REQUIRE(expected_address.has_value());
                   +  - ]
     164   [ +  -  +  -  :          54 :             BOOST_CHECK(sp_address == *expected_address);
                   +  - ]
     165                 :             : 
     166                 :             :             // The change label is registered automatically; only non-change labels need registering.
     167         [ +  - ]:          27 :             SilentPaymentsReceiver receiver{scan_priv_key, sp_address.GetSpendPubKey()};
     168   [ +  -  +  -  :          27 :             auto given_labels{given["labels"].getValues()};
             +  -  +  - ]
     169   [ -  +  +  + ]:          42 :             for (size_t i = 0; i < given_labels.size(); i++) {
     170         [ +  - ]:          15 :                 const uint32_t m = given_labels[i].getInt<uint32_t>();
     171   [ +  +  +  -  :          15 :                 const SilentPaymentsDestination labeled_addr = (m == 0) ? receiver.GetChangeDestination() : receiver.GenerateLabeledAddress(m);
                   +  - ]
     172                 :             :                 // expected["addresses"] contains the base silent payments address (at index 0)
     173                 :             :                 // followed by the labeled addresses
     174   [ +  -  +  -  :          15 :                 auto sp = DecodeSilentPaymentsAddress(expected["addresses"][i+1].get_str(), Params());
          +  -  +  -  +  
                -  +  - ]
     175   [ +  -  +  -  :          30 :                 BOOST_REQUIRE(sp.has_value());
                   +  - ]
     176   [ +  -  +  - ]:          30 :                 BOOST_CHECK(labeled_addr == *sp);
     177                 :          15 :             }
     178                 :             : 
     179                 :             :             // Scanning
     180         [ +  - ]:          27 :             const auto& found_outputs = receiver.Scan(*pub_tweak_data, output_pub_keys);
     181   [ +  -  +  -  :          54 :             BOOST_REQUIRE(found_outputs.has_value());
                   +  + ]
     182                 :             :             // The transaction may be a silent payments transaction, but it does not contain any outputs for us,
     183                 :             :             // so we continue to the next transaction.
     184         [ +  + ]:          27 :             if (found_outputs->empty()) {
     185   [ +  -  +  -  :           2 :                 BOOST_CHECK(expected["outputs"].empty());
          +  -  -  +  +  
                      - ]
     186                 :           1 :                 continue;
     187                 :             :             }
     188   [ +  -  +  -  :          26 :             if (!expected["n_outputs"].isNull()) {
                   +  + ]
     189   [ +  -  +  -  :           1 :                 BOOST_CHECK_EQUAL(found_outputs->size(), (size_t)expected["n_outputs"].getInt<int>());
          +  -  +  -  -  
                +  +  - ]
     190                 :             :             } else {
     191         [ +  - ]:          25 :                 std::map<XOnlyPubKey, uint256> expected_outputs;
     192   [ +  -  +  -  :          57 :                 for (const auto& output : expected["outputs"].getValues()) {
             +  -  +  + ]
     193                 :          64 :                     expected_outputs.emplace(
     194   [ +  -  +  -  :          96 :                         XOnlyPubKey{ParseHex(output["pub_key"].get_str())},
          -  +  +  -  +  
                      - ]
     195   [ +  -  +  -  :          96 :                         uint256{ParseHex(output["priv_key_tweak"].get_str())});
          +  -  -  +  +  
                -  +  - ]
     196                 :             :                 }
     197   [ +  -  +  -  :          25 :                 BOOST_TEST_MESSAGE(found_outputs->size());
             -  +  +  - ]
     198   [ +  -  -  +  :          25 :                 BOOST_REQUIRE_EQUAL(found_outputs->size(), expected_outputs.size());
                   +  - ]
     199         [ +  + ]:          57 :                 for (const auto& output : *found_outputs) {
     200                 :          32 :                     const auto expected_output = expected_outputs.find(output.output);
     201   [ +  -  +  -  :          64 :                     BOOST_REQUIRE(expected_output != expected_outputs.end());
                   +  - ]
     202   [ +  -  +  - ]:          64 :                     BOOST_CHECK(output.tweak == expected_output->second);
     203                 :             :                 }
     204                 :          25 :             }
     205                 :          30 :         }
     206                 :             :     }
     207                 :           1 : }
     208                 :             : 
     209   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(bip352_preserves_requested_output_indexes)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     210                 :             : {
     211                 :           1 :     CKey sender_key = ParseHexToCKey("0000000000000000000000000000000000000000000000000000000000000001");
     212         [ +  - ]:           1 :     CKey scan_key = ParseHexToCKey("0000000000000000000000000000000000000000000000000000000000000002");
     213         [ +  - ]:           1 :     CKey spend_key = ParseHexToCKey("0000000000000000000000000000000000000000000000000000000000000003");
     214   [ +  -  +  -  :           2 :     SilentPaymentsDestination sp_dest{SilentPaymentsDestination::From(scan_key.GetPubKey(), spend_key.GetPubKey()).value()};
                   +  - ]
     215   [ +  -  +  +  :           4 :     std::map<size_t, SilentPaymentsDestination> sp_dests{{2, sp_dest}, {5, sp_dest}};
                   -  - ]
     216   [ +  -  +  - ]:           2 :     COutPoint smallest_outpoint{Txid::FromHex("0000000000000000000000000000000000000000000000000000000000000001").value(), 0};
     217                 :             : 
     218   [ +  -  +  -  :           2 :     auto generated = GenerateSilentPaymentsTaprootDestinations(sp_dests, {sender_key}, {}, smallest_outpoint);
             +  +  -  - ]
     219                 :             : 
     220   [ +  -  +  -  :           2 :     BOOST_REQUIRE(generated.has_value());
                   +  - ]
     221   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(generated->size(), sp_dests.size());
     222   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(generated->count(0), 0);
     223   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(generated->count(1), 0);
     224   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(generated->count(2), 1);
     225   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(generated->count(5), 1);
     226   [ +  -  -  +  :           4 : }
             +  -  -  - ]
     227                 :             : 
     228   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(bip352_skips_transactions_spending_unknown_segwit_versions)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     229                 :             : {
     230                 :           1 :     CKey key = ParseHexToCKey("0000000000000000000000000000000000000000000000000000000000000001");
     231         [ +  - ]:           1 :     CPubKey pubkey = key.GetPubKey();
     232   [ +  -  +  - ]:           2 :     COutPoint eligible_outpoint{Txid::FromHex("0000000000000000000000000000000000000000000000000000000000000001").value(), 0};
     233   [ +  -  +  - ]:           2 :     COutPoint unknown_segwit_outpoint{Txid::FromHex("0000000000000000000000000000000000000000000000000000000000000002").value(), 0};
     234                 :             : 
     235         [ +  - ]:           1 :     CTxIn eligible_input{eligible_outpoint};
     236   [ +  -  +  -  :           4 :     eligible_input.scriptWitness = CScriptWitness{{std::vector<unsigned char>(64, 0), ToByteVector(pubkey)}};
          -  +  +  +  -  
                      - ]
     237                 :             : 
     238         [ +  - ]:           1 :     std::map<COutPoint, Coin> coins;
     239   [ +  -  +  -  :           1 :     coins[eligible_outpoint] = Coin{CTxOut{{}, GetScriptForDestination(WitnessV0KeyHash{pubkey})}, 0, false};
             +  -  +  - ]
     240   [ +  -  +  -  :           2 :     coins[unknown_segwit_outpoint] = Coin{CTxOut{{}, GetScriptForDestination(WitnessUnknown{2, std::vector<unsigned char>(32, 1)})}, 0, false};
          +  -  +  -  +  
                      - ]
     241                 :             : 
     242   [ +  -  +  -  :           3 :     BOOST_REQUIRE(GetSilentPaymentsPrevoutsSummary({eligible_input}, coins).has_value());
          +  -  +  -  +  
             -  +  +  -  
                      - ]
     243   [ +  -  +  -  :           6 :     BOOST_CHECK(!GetSilentPaymentsPrevoutsSummary({eligible_input, CTxIn{unknown_segwit_outpoint}}, coins).has_value());
          +  -  +  -  +  
             -  +  +  -  
                      - ]
     244   [ +  -  +  -  :           4 : }
          +  -  +  -  +  
             -  -  -  -  
                      - ]
     245                 :             : 
     246   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(bip352_scan_skips_invalid_taproot_outputs)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     247                 :             : {
     248                 :           1 :     CKey sender_key = ParseHexToCKey("0000000000000000000000000000000000000000000000000000000000000001");
     249         [ +  - ]:           1 :     CKey scan_key = ParseHexToCKey("0000000000000000000000000000000000000000000000000000000000000002");
     250         [ +  - ]:           1 :     CKey spend_key = ParseHexToCKey("0000000000000000000000000000000000000000000000000000000000000003");
     251   [ +  -  +  - ]:           2 :     const COutPoint outpoint{Txid::FromHex("0000000000000000000000000000000000000000000000000000000000000001").value(), 0};
     252                 :             : 
     253         [ +  - ]:           1 :     std::map<size_t, SilentPaymentsDestination> sp_dests;
     254   [ +  -  +  -  :           2 :     sp_dests.emplace(0, SilentPaymentsDestination::From(scan_key.GetPubKey(), spend_key.GetPubKey()).value());
             +  -  +  - ]
     255   [ +  -  +  -  :           2 :     const auto sp_tr_dests = GenerateSilentPaymentsTaprootDestinations(sp_dests, {sender_key}, {}, outpoint);
             +  +  -  - ]
     256   [ +  -  +  -  :           2 :     BOOST_REQUIRE(sp_tr_dests.has_value());
                   +  - ]
     257         [ +  - ]:           1 :     const XOnlyPubKey expected_output{sp_tr_dests->begin()->second};
     258                 :             : 
     259         [ +  - ]:           1 :     CTxIn txin{outpoint};
     260         [ +  - ]:           1 :     const CPubKey sender_pubkey{sender_key.GetPubKey()};
     261   [ +  -  +  -  :           4 :     txin.scriptWitness = CScriptWitness{{{}, ToByteVector(sender_pubkey)}};
          -  +  +  +  -  
                      - ]
     262                 :             : 
     263         [ +  - ]:           1 :     std::map<COutPoint, Coin> coins;
     264   [ +  -  +  -  :           1 :     coins[outpoint] = Coin{CTxOut{{}, GetScriptForDestination(WitnessV0KeyHash{sender_pubkey})}, 0, false};
             +  -  +  - ]
     265   [ +  -  +  -  :           2 :     const auto prevouts_summary = GetSilentPaymentsPrevoutsSummary({txin}, coins);
             +  +  -  - ]
     266   [ +  -  +  -  :           2 :     BOOST_REQUIRE(prevouts_summary.has_value());
                   +  - ]
     267                 :             : 
     268                 :           1 :     std::vector<XOnlyPubKey> output_pub_keys;
     269   [ +  -  +  - ]:           1 :     output_pub_keys.emplace_back(ParseHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
     270         [ +  - ]:           1 :     output_pub_keys.push_back(expected_output);
     271                 :             : 
     272   [ +  -  +  - ]:           1 :     SilentPaymentsReceiver receiver{scan_key, spend_key.GetPubKey()};
     273         [ +  - ]:           1 :     const auto found_outputs = receiver.Scan(*prevouts_summary, output_pub_keys);
     274   [ +  -  +  -  :           2 :     BOOST_REQUIRE(found_outputs.has_value());
                   +  - ]
     275   [ +  -  -  +  :           1 :     BOOST_REQUIRE_EQUAL(found_outputs->size(), 1);
                   +  - ]
     276   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(found_outputs->front().output, expected_output);
     277   [ +  -  +  -  :           4 : }
             +  -  -  - ]
     278                 :             : 
     279   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(bip352_p2pkh_pubkey_extraction_with_checksig_in_scriptsig)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     280                 :             : {
     281                 :             :     // P2PKH scriptSigs are third-party malleable. Construct one that contains an OP_CHECKSIG with a
     282                 :             :     // bogus (but strictly DER-encoded) signature, followed by a conditional push of a wrong pubkey:
     283                 :             :     //
     284                 :             :     //   <sig> <pubkey> <bogus_sig> OP_OVER OP_CHECKSIG OP_IF <wrong_pubkey> OP_ENDIF
     285                 :             :     //
     286                 :             :     // In real validation the OP_CHECKSIG fails, the OP_IF branch is skipped and the final stack is
     287                 :             :     // <sig> <pubkey>, i.e. the spend is still valid and the pubkey must be extracted from it.
     288                 :           1 :     const CKey key = ParseHexToCKey("0000000000000000000000000000000000000000000000000000000000000001");
     289         [ +  - ]:           1 :     const CPubKey pubkey = key.GetPubKey();
     290   [ +  -  +  - ]:           1 :     const CPubKey wrong_pubkey = ParseHexToCKey("0000000000000000000000000000000000000000000000000000000000000002").GetPubKey();
     291   [ +  -  +  - ]:           1 :     const CScript spk{GetScriptForDestination(PKHash{pubkey})};
     292                 :             : 
     293         [ +  - ]:           1 :     CMutableTransaction tx;
     294   [ +  -  +  - ]:           2 :     tx.vin.emplace_back(COutPoint{Txid::FromHex("0000000000000000000000000000000000000000000000000000000000000001").value(), 0});
     295                 :           1 :     std::vector<unsigned char> sig;
     296   [ +  -  +  -  :           2 :     BOOST_REQUIRE(key.Sign(SignatureHash(spk, tx, 0, SIGHASH_ALL, /*amount=*/0, SigVersion::BASE), sig));
          +  -  +  -  +  
                      - ]
     297         [ +  - ]:           1 :     sig.push_back(SIGHASH_ALL);
     298         [ +  - ]:           1 :     const std::vector<unsigned char> bogus_sig{ParseHex("300602010102010101")}; // r=1, s=1, SIGHASH_ALL
     299   [ -  +  +  -  :           4 :     tx.vin[0].scriptSig = CScript() << sig << ToByteVector(pubkey) << bogus_sig << OP_OVER << OP_CHECKSIG
          -  +  +  -  +  
                      - ]
     300   [ +  -  +  -  :           2 :                                     << OP_IF << ToByteVector(wrong_pubkey) << OP_ENDIF;
                   +  - ]
     301         [ +  - ]:           1 :     const CTxIn& txin{tx.vin[0]};
     302                 :             : 
     303                 :             :     // Verify that the malleated spend is consensus-valid
     304                 :           1 :     ScriptError serror = SCRIPT_ERR_OK;
     305   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(VerifyScript(txin.scriptSig, spk, nullptr, MANDATORY_SCRIPT_VERIFY_FLAGS,
             +  -  +  - ]
     306                 :             :                                      MutableTransactionSignatureChecker(&tx, 0, /*amountIn=*/0, MissingDataBehavior::ASSERT_FAIL), &serror),
     307                 :             :                         ScriptErrorString(serror));
     308                 :             : 
     309         [ +  - ]:           1 :     const auto extracted_pubkey = GetPubKeyFromInput(txin, spk);
     310   [ +  -  +  -  :           2 :     BOOST_REQUIRE(extracted_pubkey.has_value());
                   +  - ]
     311   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(std::get<CPubKey>(*extracted_pubkey), pubkey);
                   +  - ]
     312                 :           2 : }
     313                 :             : 
     314   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(bip352_label_serialize_roundtrip)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     315                 :             : {
     316                 :           1 :     CKey scan_key = ParseHexToCKey("0000000000000000000000000000000000000000000000000000000000000002");
     317   [ +  -  +  - ]:           1 :     CPubKey spend_pubkey = ParseHexToCKey("0000000000000000000000000000000000000000000000000000000000000003").GetPubKey();
     318         [ +  - ]:           1 :     SilentPaymentsReceiver receiver{scan_key, spend_pubkey};
     319                 :             : 
     320   [ +  -  +  -  :           1 :     BOOST_REQUIRE_EQUAL(receiver.GetLabels().size(), 1);
                   +  - ]
     321   [ +  -  +  - ]:           1 :     const SilentPaymentsLabel& label = receiver.GetLabels().begin()->first;
     322                 :             : 
     323                 :           1 :     DataStream stream;
     324         [ +  - ]:           1 :     label.Serialize(stream);
     325         [ +  - ]:           1 :     auto roundtripped = SilentPaymentsLabel::Unserialize(stream);
     326   [ +  -  +  -  :           2 :     BOOST_REQUIRE(roundtripped.has_value());
                   +  - ]
     327   [ +  -  +  -  :           2 :     BOOST_CHECK(*roundtripped == label);
                   +  - ]
     328   [ +  -  -  +  :           2 :     BOOST_CHECK(stream.empty());
                   +  - ]
     329                 :           1 : }
     330                 :             : 
     331   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(bip352_decode_address)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     332                 :             : {
     333                 :           1 :     struct ValidVector {
     334                 :             :         ChainType chain;
     335                 :             :         std::string address;
     336                 :             :         std::string scan_pubkey;
     337                 :             :         std::string spend_pubkey;
     338                 :             :         std::string extension_data;
     339                 :             :         uint8_t version;
     340                 :             :     };
     341                 :           1 :     const ValidVector valid_vectors[]{
     342                 :             :         {ChainType::MAIN,
     343                 :             :          "sp1qq22l5s6l9460ww6t4tkzsy2a7zejurcmzz35pt0ffrzk5erlaykdcqugecjjnjqf7ggq39vl6wexjlm00n66z94v675n7wcux6d2krr68gdjvfn2",
     344                 :             :          "0295fa435f2d74f73b4baaec28115df0b32e0f1b10a340ade948c56a647fe92cdc",
     345                 :             :          "0388ce2529c809f21008959fd3b2697f6f7cf5a116acd7a93f3b1c369aab0c7a3a",
     346                 :             :          "", 0},
     347                 :             :         {ChainType::MAIN,
     348                 :             :          "sp1pq22l5s6l9460ww6t4tkzsy2a7zejurcmzz35pt0ffrzk5erlaykdcqugecjjnjqf7ggq39vl6wexjlm00n66z94v675n7wcux6d2krr68t02m0h0s8cwhy",
     349                 :             :          "0295fa435f2d74f73b4baaec28115df0b32e0f1b10a340ade948c56a647fe92cdc",
     350                 :             :          "0388ce2529c809f21008959fd3b2697f6f7cf5a116acd7a93f3b1c369aab0c7a3a",
     351                 :             :          "deadbeef", 1},
     352                 :             :         {ChainType::TESTNET4,
     353                 :             :          "tsp1qqthpye3hdcnydp9temp7yduy6uw5h2nw8u9fz677ccrna280qwj3uq60zeqs3zfpj3age62h4ljq2lyawwdecmk8a545yysk4x3tu3skjqm2thu6",
     354                 :             :          "02ee1266376e264684abcec3e23784d71d4baa6e3f0a916bdec6073ea8ef03a51e",
     355                 :             :          "034f1641088921947a8ce957afe4057c9d739b9c6ec7ed2b421216a9a2be461690",
     356                 :             :          "", 0},
     357                 :             :         {ChainType::SIGNET,
     358                 :             :          "tsp1qqvrl2accqtatgtkllv5fdsfapq6vqnrr0uf2whhmjyas4teg7ljfqqlpec0ze90m97t3stv92p5ekzkcwzmpx0x8p7ljj9xqcjjpvnfurc00tjn9",
     359                 :             :          "0307f5771802fab42edffb2896c13d0834c04c637f12a75efb913b0aaf28f7e490",
     360                 :             :          "03e1ce1e2c95fb2f97182d8550699b0ad870b6133cc70fbf2914c0c4a4164d3c1e",
     361                 :             :          "", 0},
     362                 :             :         {ChainType::REGTEST,
     363                 :             :          "sprt1qq04xgllnqqfdlxjkr355rmsahfn9t8l6sd0k20t4uka4c73nvvpnwqu4kvg0nm62d5jtmqp54lkc7tul0dzt25ejtn4f80505z3u0h5jag59rdg4",
     364                 :             :          "03ea647ff30012df9a561c6941ee1dba66559ffa835f653d75e5bb5c7a33630337",
     365                 :             :          "0395b310f9ef4a6d24bd8034afed8f2f9f7b44b553325cea93be8fa0a3c7de92ea",
     366                 :             :          "", 0},
     367   [ +  -  +  -  :           6 :     };
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
             -  -  -  - ]
     368                 :             : 
     369         [ +  + ]:           6 :     for (const auto& vec : valid_vectors) {
     370         [ +  - ]:           5 :         SelectParams(vec.chain);
     371                 :             : 
     372   [ +  -  +  - ]:           5 :         auto sp = DecodeSilentPaymentsAddress(vec.address, Params());
     373   [ +  -  +  -  :          10 :         BOOST_REQUIRE_MESSAGE(sp.has_value(), vec.address);
                   +  - ]
     374   [ +  -  +  - ]:           5 :         BOOST_CHECK_EQUAL(sp->GetVersion(), vec.version);
     375   [ +  -  +  -  :           5 :         BOOST_CHECK_EQUAL(HexStr(sp->GetScanPubKey()), vec.scan_pubkey);
                   +  - ]
     376   [ +  -  +  -  :           5 :         BOOST_CHECK_EQUAL(HexStr(sp->GetSpendPubKey()), vec.spend_pubkey);
                   +  - ]
     377   [ +  -  -  +  :           5 :         BOOST_CHECK_EQUAL(HexStr(sp->GetExtensionData()), vec.extension_data);
             +  -  +  - ]
     378                 :             : 
     379                 :             :         // Bech32(m) is case-insensitive as a whole; an all-uppercase address must decode identically.
     380   [ -  +  +  - ]:           5 :         std::string flipped = ToUpper(vec.address);
     381   [ +  -  +  - ]:           5 :         auto sp_flipped = DecodeSilentPaymentsAddress(flipped, Params());
     382   [ +  -  +  -  :          10 :         BOOST_REQUIRE_MESSAGE(sp_flipped.has_value(), flipped);
                   +  - ]
     383   [ +  -  +  - ]:          10 :         BOOST_CHECK(*sp_flipped == *sp);
     384                 :           5 :     }
     385                 :             : 
     386                 :             :     // `chain` is the one chain (if any) whose HRP matches the address, where `expected_error`
     387                 :             :     // applies; on every other chain the HRP check itself is expected to fail first.
     388                 :             :     // `chain_independent` addresses fail bech32m checksum verification before the HRP is even
     389                 :             :     // compared, so `expected_error` applies on every chain instead.
     390                 :           9 :     struct InvalidVector {
     391                 :             :         std::string address;
     392                 :             :         std::string expected_error;
     393                 :             :         std::optional<ChainType> chain = std::nullopt;
     394                 :             :         bool chain_independent = false;
     395                 :             :     };
     396                 :           1 :     const InvalidVector invalid_vectors[]{
     397                 :             :         {"spx1qq22l5s6l9460ww6t4tkzsy2a7zejurcmzz35pt0ffrzk5erlaykdcqugecjjnjqf7ggq39vl6wexjlm00n66z94v675n7wcux6d2krr68g37pn04",
     398                 :             :          "Invalid or unsupported prefix for Silent Payments address (expected sp, got spx)."}, // wrong HRP; never matches any chain
     399                 :             :         {"sp1qq22l5s6l9460ww6t4tkzsy2a7zejurcmzz35pt0ffrzk5erlaykdcqugecjjnjqf7ggq39vl6wexjlm00n66z94v675n7wcux6d2krr68gcwu9kg",
     400                 :             :          "Silent Payments address must use Bech32m checksum", std::nullopt, /*chain_independent=*/true}, // bad checksum
     401                 :             :         {"sp1qqgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq2qugecjjnjqf7ggq39vl6wexjlm00n66z94v675n7wcux6d2krr68g25havg",
     402                 :             :          "Invalid Silent payments address", ChainType::MAIN}, // invalid scan pubkey
     403                 :             :         {"sp1qq22l5s6l9460ww6t4tkzsy2a7zejurcmzz35pt0ffrzk5erlaykdcqsqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq57qc9nf",
     404                 :             :          "Invalid Silent payments address", ChainType::MAIN}, // invalid spend pubkey
     405                 :             :         {"sp1lq22l5s6l9460ww6t4tkzsy2a7zejurcmzz35pt0ffrzk5erlaykdcqugecjjnjqf7ggq39vl6wexjlm00n66z94v675n7wcux6d2krr68gaafydt",
     406                 :             :          "This implementation only supports Silent payments addresses v0 through v30 (got 31).", ChainType::MAIN}, // reserved version 31
     407                 :             :         {"sp1qq22l5s6l9460ww6t4tkzsy2a7zejurcmzz35pt0ffrzk5erlaykdcznsxsn",
     408                 :             :          "Silent payments data payload is too small (expected at least 66, got 33).", ChainType::MAIN}, // payload too small
     409                 :             :         {"tsp1qqthpye3hdcnydp9temp7yduy6uw5h2nw8u9fz677ccrna280qwj3uq60zeqs3zfpj3age62h4ljq2lyawwdecmk8a545yysk4x3tu3skjqm2thum",
     410                 :             :          "Silent Payments address must use Bech32m checksum", std::nullopt, /*chain_independent=*/true}, // bad checksum
     411                 :             :         {"sp1qqgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqsqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqf26rn7",
     412                 :             :          "Silent Payments address must use Bech32m checksum", std::nullopt, /*chain_independent=*/true}, // bad checksum
     413                 :             :         {"sprt1qqgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq2qu4kvg0nm62d5jtmqp54lkc7tul0dzt25ejtn4f80505z3u0h5jagf9zkp5",
     414                 :             :          "Invalid Silent payments address", ChainType::REGTEST}, // invalid scan pubkey
     415   [ +  -  +  -  :          19 :     };
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  - ]
     416                 :             : 
     417         [ +  + ]:          10 :     for (const auto& vec : invalid_vectors) {
     418         [ +  + ]:          54 :         for (const auto chain : {ChainType::MAIN, ChainType::TESTNET, ChainType::TESTNET4, ChainType::SIGNET, ChainType::REGTEST}) {
     419         [ +  - ]:          45 :             SelectParams(chain);
     420   [ +  -  +  - ]:          45 :             auto sp = DecodeSilentPaymentsAddress(vec.address, Params());
     421   [ +  -  +  -  :          90 :             BOOST_REQUIRE_MESSAGE(!sp.has_value(), vec.address);
                   +  + ]
     422   [ +  +  +  + ]:          45 :             if (vec.chain_independent || chain == vec.chain) {
     423   [ +  -  +  - ]:          20 :                 BOOST_CHECK_EQUAL(sp.error(), vec.expected_error);
     424                 :             :             } else {
     425   [ +  -  -  +  :          70 :                 BOOST_CHECK_MESSAGE(sp.error().starts_with("Invalid or unsupported prefix for Silent Payments address"), sp.error());
                   +  - ]
     426                 :             :             }
     427                 :          45 :         }
     428                 :             :     }
     429                 :             : 
     430         [ +  - ]:           1 :     SelectParams(ChainType::MAIN);
     431   [ +  +  +  +  :          17 : }
             -  -  -  - ]
     432                 :             : 
     433                 :             : BOOST_AUTO_TEST_SUITE_END()
     434                 :             : } // namespace bip352
        

Generated by: LCOV version 2.0-1