LCOV - code coverage report
Current view: top level - src/wallet/test - scriptpubkeyman_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 31 31
Test Date: 2025-01-19 05:08:01 Functions: 100.0 % 4 4
Branches: 50.4 % 226 114

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-2021 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 <key.h>
       6                 :             : #include <key_io.h>
       7                 :             : #include <test/util/setup_common.h>
       8                 :             : #include <script/solver.h>
       9                 :             : #include <wallet/scriptpubkeyman.h>
      10                 :             : #include <wallet/wallet.h>
      11                 :             : #include <wallet/test/util.h>
      12                 :             : 
      13                 :             : #include <boost/test/unit_test.hpp>
      14                 :             : 
      15                 :             : namespace wallet {
      16                 :             : BOOST_FIXTURE_TEST_SUITE(scriptpubkeyman_tests, BasicTestingSetup)
      17                 :             : 
      18                 :             : // Test LegacyScriptPubKeyMan::CanProvide behavior, making sure it returns true
      19                 :             : // for recognized scripts even when keys may not be available for signing.
      20   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(CanProvide)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      21                 :             : {
      22                 :             :     // Set up wallet and keyman variables.
      23   [ +  -  +  -  :           2 :     CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
      24         [ +  - ]:           1 :     LegacyScriptPubKeyMan& keyman = *wallet.GetOrCreateLegacyScriptPubKeyMan();
      25                 :             : 
      26                 :             :     // Make a 1 of 2 multisig script
      27         [ +  - ]:           1 :     std::vector<CKey> keys(2);
      28                 :           1 :     std::vector<CPubKey> pubkeys;
      29         [ +  + ]:           3 :     for (CKey& key : keys) {
      30         [ +  - ]:           2 :         key.MakeNewKey(true);
      31   [ +  -  +  - ]:           2 :         pubkeys.emplace_back(key.GetPubKey());
      32                 :             :     }
      33         [ +  - ]:           1 :     CScript multisig_script = GetScriptForMultisig(1, pubkeys);
      34   [ +  -  +  - ]:           1 :     CScript p2sh_script = GetScriptForDestination(ScriptHash(multisig_script));
      35                 :           1 :     SignatureData data;
      36                 :             : 
      37                 :             :     // Verify the p2sh(multisig) script is not recognized until the multisig
      38                 :             :     // script is added to the keystore to make it solvable
      39   [ +  -  +  -  :           1 :     BOOST_CHECK(!keyman.CanProvide(p2sh_script, data));
                   +  - ]
      40         [ +  - ]:           1 :     keyman.AddCScript(multisig_script);
      41   [ +  -  +  -  :           2 :     BOOST_CHECK(keyman.CanProvide(p2sh_script, data));
                   +  - ]
      42                 :           1 : }
      43                 :             : 
      44   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(DescriptorScriptPubKeyManTests)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      45                 :             : {
      46                 :           1 :     std::unique_ptr<interfaces::Chain>& chain = m_node.chain;
      47                 :             : 
      48   [ +  -  +  -  :           2 :     CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
      49                 :           1 :     auto key_scriptpath = GenerateRandomKey();
      50                 :             : 
      51                 :             :     // Verify that a SigningProvider for a pubkey is only returned if its corresponding private key is available
      52                 :           1 :     auto key_internal = GenerateRandomKey();
      53   [ +  -  +  -  :           3 :     std::string desc_str = "tr(" + EncodeSecret(key_internal) + ",pk(" + HexStr(key_scriptpath.GetPubKey()) + "))";
          +  -  +  -  +  
                      - ]
      54   [ +  -  +  - ]:           1 :     auto spk_man1 = dynamic_cast<DescriptorScriptPubKeyMan*>(CreateDescriptor(keystore, desc_str, true));
      55   [ +  -  +  -  :           2 :     BOOST_CHECK(spk_man1 != nullptr);
                   +  - ]
      56   [ +  -  +  - ]:           1 :     auto signprov_keypath_spendable = spk_man1->GetSigningProvider(key_internal.GetPubKey());
      57   [ +  -  +  -  :           2 :     BOOST_CHECK(signprov_keypath_spendable != nullptr);
                   +  - ]
      58                 :             : 
      59   [ +  -  +  -  :           3 :     desc_str = "tr(" + HexStr(XOnlyPubKey::NUMS_H) + ",pk(" + HexStr(key_scriptpath.GetPubKey()) + "))";
          +  -  +  -  +  
                      - ]
      60   [ +  -  +  - ]:           1 :     auto spk_man2 = dynamic_cast<DescriptorScriptPubKeyMan*>(CreateDescriptor(keystore, desc_str, true));
      61   [ +  -  +  -  :           2 :     BOOST_CHECK(spk_man2 != nullptr);
                   +  - ]
      62   [ +  -  +  - ]:           1 :     auto signprov_keypath_nums_h = spk_man2->GetSigningProvider(XOnlyPubKey::NUMS_H.GetEvenCorrespondingCPubKey());
      63   [ +  -  +  -  :           2 :     BOOST_CHECK(signprov_keypath_nums_h == nullptr);
                   -  + ]
      64         [ +  - ]:           2 : }
      65                 :             : 
      66                 :             : BOOST_AUTO_TEST_SUITE_END()
      67                 :             : } // namespace wallet
        

Generated by: LCOV version 2.0-1