LCOV - code coverage report
Current view: top level - src/wallet/test - ismine_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 482 482
Test Date: 2024-11-04 05:10:19 Functions: 100.0 % 3 3
Branches: 50.2 % 2084 1046

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2017-2022 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 <node/context.h>
       8                 :             : #include <script/script.h>
       9                 :             : #include <script/solver.h>
      10                 :             : #include <script/signingprovider.h>
      11                 :             : #include <test/util/setup_common.h>
      12                 :             : #include <wallet/types.h>
      13                 :             : #include <wallet/wallet.h>
      14                 :             : #include <wallet/test/util.h>
      15                 :             : 
      16                 :             : #include <boost/test/unit_test.hpp>
      17                 :             : 
      18                 :             : using namespace util::hex_literals;
      19                 :             : 
      20                 :             : namespace wallet {
      21                 :             : BOOST_FIXTURE_TEST_SUITE(ismine_tests, BasicTestingSetup)
      22                 :             : 
      23                 :          18 : wallet::ScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success)
      24                 :             : {
      25                 :          18 :     keystore.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
      26                 :             : 
      27                 :          18 :     FlatSigningProvider keys;
      28         [ +  - ]:          18 :     std::string error;
      29         [ +  - ]:          18 :     auto parsed_descs = Parse(desc_str, keys, error, false);
      30   [ +  -  +  -  :          36 :     BOOST_CHECK(success == (!parsed_descs.empty()));
                   +  + ]
      31         [ +  + ]:          18 :     if (!success) return nullptr;
      32         [ +  - ]:          12 :     auto& desc = parsed_descs.at(0);
      33                 :             : 
      34                 :          12 :     const int64_t range_start = 0, range_end = 1, next_index = 0, timestamp = 1;
      35                 :             : 
      36   [ +  -  +  - ]:          12 :     WalletDescriptor w_desc(std::move(desc), timestamp, range_start, range_end, next_index);
      37                 :             : 
      38         [ +  - ]:          12 :     LOCK(keystore.cs_wallet);
      39                 :             : 
      40   [ +  -  +  -  :          12 :     return Assert(keystore.AddWalletDescriptor(w_desc, keys,/*label=*/"", /*internal=*/false));
             +  -  +  - ]
      41                 :          18 : };
      42                 :             : 
      43   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ismine_standard)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      44                 :             : {
      45                 :           2 :     CKey keys[2];
      46         [ +  + ]:           3 :     CPubKey pubkeys[2];
      47         [ +  + ]:           3 :     for (int i = 0; i < 2; i++) {
      48         [ +  - ]:           2 :         keys[i].MakeNewKey(true);
      49         [ +  - ]:           2 :         pubkeys[i] = keys[i].GetPubKey();
      50                 :             :     }
      51                 :             : 
      52                 :           1 :     CKey uncompressedKey = GenerateRandomKey(/*compressed=*/false);
      53         [ +  - ]:           1 :     CPubKey uncompressedPubkey = uncompressedKey.GetPubKey();
      54                 :           1 :     std::unique_ptr<interfaces::Chain>& chain = m_node.chain;
      55                 :             : 
      56                 :           1 :     CScript scriptPubKey;
      57                 :           1 :     isminetype result;
      58                 :             : 
      59                 :             :     // P2PK compressed - Legacy
      60                 :           1 :     {
      61   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
      62         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
      63   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
      64         [ +  - ]:           2 :         scriptPubKey = GetScriptForRawPubKey(pubkeys[0]);
      65                 :             : 
      66                 :             :         // Keystore does not have key
      67   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
      68   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
      69   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
      70                 :             : 
      71                 :             :         // Keystore has key
      72   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
          +  -  +  -  +  
                      - ]
      73   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
      74   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
      75   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
          +  -  +  -  +  
                      - ]
      76                 :           1 :     }
      77                 :             : 
      78                 :             :     // P2PK compressed - Descriptor
      79                 :           1 :     {
      80   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
      81   [ +  -  +  - ]:           2 :         std::string desc_str = "pk(" + EncodeSecret(keys[0]) + ")";
      82                 :             : 
      83         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, true);
      84                 :             : 
      85         [ +  - ]:           2 :         scriptPubKey = GetScriptForRawPubKey(pubkeys[0]);
      86         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
      87   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
      88                 :           1 :     }
      89                 :             : 
      90                 :             :     // P2PK uncompressed - Legacy
      91                 :           1 :     {
      92   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
      93         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
      94   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
      95         [ +  - ]:           2 :         scriptPubKey = GetScriptForRawPubKey(uncompressedPubkey);
      96                 :             : 
      97                 :             :         // Keystore does not have key
      98   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
      99   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     100   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     101                 :             : 
     102                 :             :         // Keystore has key
     103   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
          +  -  +  -  +  
                      - ]
     104   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     105   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     106   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
          +  -  +  -  +  
                      - ]
     107                 :           1 :     }
     108                 :             : 
     109                 :             :     // P2PK uncompressed - Descriptor
     110                 :           1 :     {
     111   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     112   [ +  -  +  - ]:           2 :         std::string desc_str = "pk(" + EncodeSecret(uncompressedKey) + ")";
     113                 :             : 
     114         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, true);
     115                 :             : 
     116         [ +  - ]:           2 :         scriptPubKey = GetScriptForRawPubKey(uncompressedPubkey);
     117         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
     118   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     119                 :           1 :     }
     120                 :             : 
     121                 :             :     // P2PKH compressed - Legacy
     122                 :           1 :     {
     123   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     124         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     125   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     126   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(PKHash(pubkeys[0]));
     127                 :             : 
     128                 :             :         // Keystore does not have key
     129   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     130   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     131   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     132                 :             : 
     133                 :             :         // Keystore has key
     134   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
          +  -  +  -  +  
                      - ]
     135   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     136   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     137   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
          +  -  +  -  +  
                      - ]
     138                 :           1 :     }
     139                 :             : 
     140                 :             :     // P2PKH compressed - Descriptor
     141                 :           1 :     {
     142   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     143   [ +  -  +  - ]:           2 :         std::string desc_str = "pkh(" + EncodeSecret(keys[0]) + ")";
     144                 :             : 
     145         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, true);
     146                 :             : 
     147   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(PKHash(pubkeys[0]));
     148         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
     149   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     150                 :           1 :     }
     151                 :             : 
     152                 :             :     // P2PKH uncompressed - Legacy
     153                 :           1 :     {
     154   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     155         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     156   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     157   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(PKHash(uncompressedPubkey));
     158                 :             : 
     159                 :             :         // Keystore does not have key
     160   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     161   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     162   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     163                 :             : 
     164                 :             :         // Keystore has key
     165   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
          +  -  +  -  +  
                      - ]
     166   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     167   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     168   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
          +  -  +  -  +  
                      - ]
     169                 :           1 :     }
     170                 :             : 
     171                 :             :     // P2PKH uncompressed - Descriptor
     172                 :           1 :     {
     173   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     174   [ +  -  +  - ]:           2 :         std::string desc_str = "pkh(" + EncodeSecret(uncompressedKey) + ")";
     175                 :             : 
     176         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, true);
     177                 :             : 
     178   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(PKHash(uncompressedPubkey));
     179         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
     180   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     181                 :           1 :     }
     182                 :             : 
     183                 :             :     // P2SH - Legacy
     184                 :           1 :     {
     185   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     186         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     187   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     188                 :             : 
     189   [ +  -  +  - ]:           1 :         CScript redeemScript = GetScriptForDestination(PKHash(pubkeys[0]));
     190   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
     191                 :             : 
     192                 :             :         // Keystore does not have redeemScript or key
     193   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     194   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     195   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     196                 :             : 
     197                 :             :         // Keystore has redeemScript but no key
     198   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemScript));
          +  -  +  -  +  
                      - ]
     199   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     200   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     201   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     202                 :             : 
     203                 :             :         // Keystore has redeemScript and key
     204   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
          +  -  +  -  +  
                      - ]
     205   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     206   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     207   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
             +  -  +  - ]
     208         [ +  - ]:           2 :     }
     209                 :             : 
     210                 :             :     // P2SH - Descriptor
     211                 :           1 :     {
     212   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     213   [ +  -  +  - ]:           2 :         std::string desc_str = "sh(pkh(" + EncodeSecret(keys[0]) + "))";
     214                 :             : 
     215         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, true);
     216                 :             : 
     217   [ +  -  +  - ]:           1 :         CScript redeemScript = GetScriptForDestination(PKHash(pubkeys[0]));
     218   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
     219         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
     220   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     221                 :           1 :     }
     222                 :             : 
     223                 :             :     // (P2PKH inside) P2SH inside P2SH (invalid) - Legacy
     224                 :           1 :     {
     225   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     226         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     227   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     228                 :             : 
     229   [ +  -  +  - ]:           1 :         CScript redeemscript_inner = GetScriptForDestination(PKHash(pubkeys[0]));
     230   [ +  -  +  - ]:           1 :         CScript redeemscript = GetScriptForDestination(ScriptHash(redeemscript_inner));
     231   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(ScriptHash(redeemscript));
     232                 :             : 
     233   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemscript));
          +  -  +  -  +  
                      - ]
     234   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemscript_inner));
          +  -  +  -  +  
                      - ]
     235   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
          +  -  +  -  +  
                      - ]
     236   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
          +  -  +  -  +  
                      - ]
     237   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     238   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     239   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
             +  -  +  - ]
     240         [ +  - ]:           2 :     }
     241                 :             : 
     242                 :             :     // (P2PKH inside) P2SH inside P2SH (invalid) - Descriptor
     243                 :           1 :     {
     244   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     245   [ +  -  +  - ]:           2 :         std::string desc_str = "sh(sh(" + EncodeSecret(keys[0]) + "))";
     246                 :             : 
     247         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, false);
     248   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(spk_manager, nullptr);
     249                 :           1 :     }
     250                 :             : 
     251                 :             :     // (P2PKH inside) P2SH inside P2WSH (invalid) - Legacy
     252                 :           1 :     {
     253   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     254         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     255   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     256                 :             : 
     257   [ +  -  +  - ]:           1 :         CScript redeemscript = GetScriptForDestination(PKHash(pubkeys[0]));
     258   [ +  -  +  - ]:           1 :         CScript witnessscript = GetScriptForDestination(ScriptHash(redeemscript));
     259   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessscript));
     260                 :             : 
     261   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(witnessscript));
          +  -  +  -  +  
                      - ]
     262   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemscript));
          +  -  +  -  +  
                      - ]
     263   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
          +  -  +  -  +  
                      - ]
     264   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
          +  -  +  -  +  
                      - ]
     265   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     266   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     267   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
             +  -  +  - ]
     268         [ +  - ]:           2 :     }
     269                 :             : 
     270                 :             :     // (P2PKH inside) P2SH inside P2WSH (invalid) - Descriptor
     271                 :           1 :     {
     272   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     273   [ +  -  +  - ]:           2 :         std::string desc_str = "wsh(sh(" + EncodeSecret(keys[0]) + "))";
     274                 :             : 
     275         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, false);
     276   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(spk_manager, nullptr);
     277                 :           1 :     }
     278                 :             : 
     279                 :             :     // P2WPKH inside P2WSH (invalid) - Legacy
     280                 :           1 :     {
     281   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     282         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     283   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     284                 :             : 
     285   [ +  -  +  - ]:           1 :         CScript witnessscript = GetScriptForDestination(WitnessV0KeyHash(pubkeys[0]));
     286   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessscript));
     287                 :             : 
     288   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(witnessscript));
          +  -  +  -  +  
                      - ]
     289   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
          +  -  +  -  +  
                      - ]
     290   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
          +  -  +  -  +  
                      - ]
     291   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     292   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     293   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
             +  -  +  - ]
     294         [ +  - ]:           2 :     }
     295                 :             : 
     296                 :             :     // P2WPKH inside P2WSH (invalid) - Descriptor
     297                 :           1 :     {
     298   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     299   [ +  -  +  - ]:           2 :         std::string desc_str = "wsh(wpkh(" + EncodeSecret(keys[0]) + "))";
     300                 :             : 
     301         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, false);
     302   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(spk_manager, nullptr);
     303                 :           1 :     }
     304                 :             : 
     305                 :             :     // (P2PKH inside) P2WSH inside P2WSH (invalid) - Legacy
     306                 :           1 :     {
     307   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     308         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     309   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     310                 :             : 
     311   [ +  -  +  - ]:           1 :         CScript witnessscript_inner = GetScriptForDestination(PKHash(pubkeys[0]));
     312   [ +  -  +  - ]:           1 :         CScript witnessscript = GetScriptForDestination(WitnessV0ScriptHash(witnessscript_inner));
     313   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessscript));
     314                 :             : 
     315   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(witnessscript_inner));
          +  -  +  -  +  
                      - ]
     316   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(witnessscript));
          +  -  +  -  +  
                      - ]
     317   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
          +  -  +  -  +  
                      - ]
     318   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
          +  -  +  -  +  
                      - ]
     319   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     320   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     321   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
             +  -  +  - ]
     322         [ +  - ]:           2 :     }
     323                 :             : 
     324                 :             :     // (P2PKH inside) P2WSH inside P2WSH (invalid) - Descriptor
     325                 :           1 :     {
     326   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     327   [ +  -  +  - ]:           2 :         std::string desc_str = "wsh(wsh(" + EncodeSecret(keys[0]) + "))";
     328                 :             : 
     329         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, false);
     330   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(spk_manager, nullptr);
     331                 :           1 :     }
     332                 :             : 
     333                 :             :     // P2WPKH compressed - Legacy
     334                 :           1 :     {
     335   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     336         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     337   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     338   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
          +  -  +  -  +  
                      - ]
     339                 :             : 
     340   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(pubkeys[0]));
     341                 :             : 
     342                 :             :         // Keystore implicitly has key and P2SH redeemScript
     343   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
          +  -  +  -  +  
                      - ]
     344   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     345   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     346   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
          +  -  +  -  +  
                      - ]
     347                 :           1 :     }
     348                 :             : 
     349                 :             :     // P2WPKH compressed - Descriptor
     350                 :           1 :     {
     351   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     352   [ +  -  +  - ]:           2 :         std::string desc_str = "wpkh(" + EncodeSecret(keys[0]) + ")";
     353                 :             : 
     354         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, true);
     355                 :             : 
     356   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(pubkeys[0]));
     357         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
     358   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     359                 :           1 :     }
     360                 :             : 
     361                 :             :     // P2WPKH uncompressed - Legacy
     362                 :           1 :     {
     363   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     364         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     365   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     366   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
          +  -  +  -  +  
                      - ]
     367                 :             : 
     368   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(uncompressedPubkey));
     369                 :             : 
     370                 :             :         // Keystore has key, but no P2SH redeemScript
     371   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     372   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     373   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     374                 :             : 
     375                 :             :         // Keystore has key and P2SH redeemScript
     376   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
          +  -  +  -  +  
                      - ]
     377   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     378   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     379   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     380                 :           1 :     }
     381                 :             : 
     382                 :             :     // P2WPKH uncompressed (invalid) - Descriptor
     383                 :           1 :     {
     384   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     385   [ +  -  +  - ]:           2 :         std::string desc_str = "wpkh(" + EncodeSecret(uncompressedKey) + ")";
     386                 :             : 
     387         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, false);
     388   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(spk_manager, nullptr);
     389                 :           1 :     }
     390                 :             : 
     391                 :             :     // scriptPubKey multisig - Legacy
     392                 :           1 :     {
     393   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     394         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     395   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     396                 :             : 
     397   [ +  -  +  -  :           2 :         scriptPubKey = GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
                   +  - ]
     398                 :             : 
     399                 :             :         // Keystore does not have any keys
     400   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     401   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     402   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     403                 :             : 
     404                 :             :         // Keystore has 1/2 keys
     405   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
          +  -  +  -  +  
                      - ]
     406                 :             : 
     407   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     408   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     409   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     410                 :             : 
     411                 :             :         // Keystore has 2/2 keys
     412   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
          +  -  +  -  +  
                      - ]
     413                 :             : 
     414   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     415   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     416   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     417                 :             : 
     418                 :             :         // Keystore has 2/2 keys and the script
     419   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
          +  -  +  -  +  
                      - ]
     420                 :             : 
     421   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     422   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     423   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     424                 :           1 :     }
     425                 :             : 
     426                 :             :     // scriptPubKey multisig - Descriptor
     427                 :           1 :     {
     428   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     429   [ +  -  +  -  :           3 :         std::string desc_str = "multi(2, " + EncodeSecret(uncompressedKey) + ", " + EncodeSecret(keys[1]) + ")";
             +  -  +  - ]
     430                 :             : 
     431         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, true);
     432                 :             : 
     433   [ +  -  +  -  :           2 :         scriptPubKey = GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
                   +  - ]
     434         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
     435   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     436                 :           1 :     }
     437                 :             : 
     438                 :             :     // P2SH multisig - Legacy
     439                 :           1 :     {
     440   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     441         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     442   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     443   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
          +  -  +  -  +  
                      - ]
     444   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
          +  -  +  -  +  
                      - ]
     445                 :             : 
     446   [ +  -  +  -  :           2 :         CScript redeemScript = GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
                   +  - ]
     447   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
     448                 :             : 
     449                 :             :         // Keystore has no redeemScript
     450   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     451   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     452   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     453                 :             : 
     454                 :             :         // Keystore has redeemScript
     455   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemScript));
          +  -  +  -  +  
                      - ]
     456   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     457   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     458   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
             +  -  +  - ]
     459         [ +  - ]:           2 :     }
     460                 :             : 
     461                 :             :     // P2SH multisig - Descriptor
     462                 :           1 :     {
     463   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     464                 :             : 
     465   [ +  -  +  -  :           3 :         std::string desc_str = "sh(multi(2, " + EncodeSecret(uncompressedKey) + ", " + EncodeSecret(keys[1]) + "))";
             +  -  +  - ]
     466                 :             : 
     467         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, true);
     468                 :             : 
     469   [ +  -  +  -  :           2 :         CScript redeemScript = GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
                   +  - ]
     470   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
     471         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
     472   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     473                 :           1 :     }
     474                 :             : 
     475                 :             :     // P2WSH multisig with compressed keys - Legacy
     476                 :           1 :     {
     477   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     478         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     479   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     480   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
          +  -  +  -  +  
                      - ]
     481   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
          +  -  +  -  +  
                      - ]
     482                 :             : 
     483   [ +  -  +  -  :           2 :         CScript witnessScript = GetScriptForMultisig(2, {pubkeys[0], pubkeys[1]});
                   +  - ]
     484   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
     485                 :             : 
     486                 :             :         // Keystore has keys, but no witnessScript or P2SH redeemScript
     487   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     488   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     489   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     490                 :             : 
     491                 :             :         // Keystore has keys and witnessScript, but no P2SH redeemScript
     492   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(witnessScript));
          +  -  +  -  +  
                      - ]
     493   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     494   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     495   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     496                 :             : 
     497                 :             :         // Keystore has keys, witnessScript, P2SH redeemScript
     498   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
          +  -  +  -  +  
                      - ]
     499   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     500   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     501   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
             +  -  +  - ]
     502         [ +  - ]:           2 :     }
     503                 :             : 
     504                 :             :     // P2WSH multisig with compressed keys - Descriptor
     505                 :           1 :     {
     506   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     507                 :             : 
     508   [ +  -  +  -  :           3 :         std::string desc_str = "wsh(multi(2, " + EncodeSecret(keys[0]) + ", " + EncodeSecret(keys[1]) + "))";
             +  -  +  - ]
     509                 :             : 
     510         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, true);
     511                 :             : 
     512   [ +  -  +  -  :           2 :         CScript redeemScript = GetScriptForMultisig(2, {pubkeys[0], pubkeys[1]});
                   +  - ]
     513   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(redeemScript));
     514         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
     515   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     516                 :           1 :     }
     517                 :             : 
     518                 :             :     // P2WSH multisig with uncompressed key - Legacy
     519                 :           1 :     {
     520   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     521         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     522   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     523   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
          +  -  +  -  +  
                      - ]
     524   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
          +  -  +  -  +  
                      - ]
     525                 :             : 
     526   [ +  -  +  -  :           2 :         CScript witnessScript = GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
                   +  - ]
     527   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
     528                 :             : 
     529                 :             :         // Keystore has keys, but no witnessScript or P2SH redeemScript
     530   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     531   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     532   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     533                 :             : 
     534                 :             :         // Keystore has keys and witnessScript, but no P2SH redeemScript
     535   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(witnessScript));
          +  -  +  -  +  
                      - ]
     536   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     537   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     538   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     539                 :             : 
     540                 :             :         // Keystore has keys, witnessScript, P2SH redeemScript
     541   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
          +  -  +  -  +  
                      - ]
     542   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     543   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     544   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
             +  -  +  - ]
     545         [ +  - ]:           2 :     }
     546                 :             : 
     547                 :             :     // P2WSH multisig with uncompressed key (invalid) - Descriptor
     548                 :           1 :     {
     549   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     550                 :             : 
     551   [ +  -  +  -  :           3 :         std::string desc_str = "wsh(multi(2, " + EncodeSecret(uncompressedKey) + ", " + EncodeSecret(keys[1]) + "))";
             +  -  +  - ]
     552                 :             : 
     553         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, false);
     554   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(spk_manager, nullptr);
     555                 :           1 :     }
     556                 :             : 
     557                 :             :     // P2WSH multisig wrapped in P2SH - Legacy
     558                 :           1 :     {
     559   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     560         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     561   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     562                 :             : 
     563   [ +  -  +  -  :           2 :         CScript witnessScript = GetScriptForMultisig(2, {pubkeys[0], pubkeys[1]});
                   +  - ]
     564   [ +  -  +  - ]:           1 :         CScript redeemScript = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
     565   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
     566                 :             : 
     567                 :             :         // Keystore has no witnessScript, P2SH redeemScript, or keys
     568   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     569   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     570   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     571                 :             : 
     572                 :             :         // Keystore has witnessScript and P2SH redeemScript, but no keys
     573   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemScript));
          +  -  +  -  +  
                      - ]
     574   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(witnessScript));
          +  -  +  -  +  
                      - ]
     575   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     576   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     577   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     578                 :             : 
     579                 :             :         // Keystore has keys, witnessScript, P2SH redeemScript
     580   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
          +  -  +  -  +  
                      - ]
     581   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
          +  -  +  -  +  
                      - ]
     582   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     583   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     584   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
             +  -  +  - ]
     585         [ +  - ]:           2 :     }
     586                 :             : 
     587                 :             :     // P2WSH multisig wrapped in P2SH - Descriptor
     588                 :           1 :     {
     589   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     590                 :             : 
     591   [ +  -  +  -  :           3 :         std::string desc_str = "sh(wsh(multi(2, " + EncodeSecret(keys[0]) + ", " + EncodeSecret(keys[1]) + ")))";
             +  -  +  - ]
     592                 :             : 
     593         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, true);
     594                 :             : 
     595   [ +  -  +  -  :           2 :         CScript witnessScript = GetScriptForMultisig(2, {pubkeys[0], pubkeys[1]});
                   +  - ]
     596   [ +  -  +  - ]:           1 :         CScript redeemScript = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
     597   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
     598         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
     599   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     600                 :           1 :     }
     601                 :             : 
     602                 :             :     // Combo - Descriptor
     603                 :           1 :     {
     604   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     605                 :             : 
     606   [ +  -  +  - ]:           2 :         std::string desc_str = "combo(" + EncodeSecret(keys[0]) + ")";
     607                 :             : 
     608         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, true);
     609                 :             : 
     610                 :             :         // Test P2PK
     611   [ +  -  +  - ]:           1 :         result = spk_manager->IsMine(GetScriptForRawPubKey(pubkeys[0]));
     612   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     613                 :             : 
     614                 :             :         // Test P2PKH
     615   [ +  -  +  -  :           1 :         result = spk_manager->IsMine(GetScriptForDestination(PKHash(pubkeys[0])));
                   +  - ]
     616   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     617                 :             : 
     618                 :             :         // Test P2SH (combo descriptor does not describe P2SH)
     619   [ +  -  +  - ]:           1 :         CScript redeemScript = GetScriptForDestination(PKHash(pubkeys[0]));
     620   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
     621         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
     622   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     623                 :             : 
     624                 :             :         // Test P2WPKH
     625   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(pubkeys[0]));
     626         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
     627   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     628                 :             : 
     629                 :             :         // P2SH-P2WPKH output
     630   [ +  -  +  - ]:           2 :         redeemScript = GetScriptForDestination(WitnessV0KeyHash(pubkeys[0]));
     631   [ +  -  +  - ]:           2 :         scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
     632         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
     633   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     634                 :             : 
     635                 :             :         // Test P2TR (combo descriptor does not describe P2TR)
     636                 :           1 :         XOnlyPubKey xpk(pubkeys[0]);
     637   [ +  -  +  - ]:           1 :         Assert(xpk.IsFullyValid());
     638         [ +  - ]:           1 :         TaprootBuilder builder;
     639         [ +  - ]:           1 :         builder.Finalize(xpk);
     640         [ +  - ]:           1 :         WitnessV1Taproot output = builder.GetOutput();
     641         [ +  - ]:           2 :         scriptPubKey = GetScriptForDestination(output);
     642         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
     643   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     644                 :           1 :     }
     645                 :             : 
     646                 :             :     // Taproot - Descriptor
     647                 :           1 :     {
     648   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     649                 :             : 
     650   [ +  -  +  - ]:           2 :         std::string desc_str = "tr(" + EncodeSecret(keys[0]) + ")";
     651                 :             : 
     652         [ +  - ]:           1 :         auto spk_manager = CreateDescriptor(keystore, desc_str, true);
     653                 :             : 
     654                 :           1 :         XOnlyPubKey xpk(pubkeys[0]);
     655   [ +  -  +  - ]:           1 :         Assert(xpk.IsFullyValid());
     656         [ +  - ]:           1 :         TaprootBuilder builder;
     657         [ +  - ]:           1 :         builder.Finalize(xpk);
     658         [ +  - ]:           1 :         WitnessV1Taproot output = builder.GetOutput();
     659         [ +  - ]:           2 :         scriptPubKey = GetScriptForDestination(output);
     660         [ +  - ]:           1 :         result = spk_manager->IsMine(scriptPubKey);
     661   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
     662                 :           1 :     }
     663                 :             : 
     664                 :             :     // OP_RETURN
     665                 :           1 :     {
     666   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     667         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     668   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     669   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
             +  -  +  - ]
     670                 :             : 
     671                 :           1 :         scriptPubKey.clear();
     672   [ +  -  +  - ]:           1 :         scriptPubKey << OP_RETURN << ToByteVector(pubkeys[0]);
     673                 :             : 
     674   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     675   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     676   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     677                 :           1 :     }
     678                 :             : 
     679                 :             :     // witness unspendable
     680                 :           1 :     {
     681   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     682         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     683   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     684   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
             +  -  +  - ]
     685                 :             : 
     686                 :           1 :         scriptPubKey.clear();
     687         [ +  - ]:           1 :         scriptPubKey << OP_0 << "aabb"_hex;
     688                 :             : 
     689   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     690   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     691   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     692                 :           1 :     }
     693                 :             : 
     694                 :             :     // witness unknown
     695                 :           1 :     {
     696   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     697         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     698   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     699   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
             +  -  +  - ]
     700                 :             : 
     701                 :           1 :         scriptPubKey.clear();
     702         [ +  - ]:           1 :         scriptPubKey << OP_16 << "aabb"_hex;
     703                 :             : 
     704   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     705   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     706   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     707                 :           1 :     }
     708                 :             : 
     709                 :             :     // Nonstandard
     710                 :           1 :     {
     711   [ +  -  +  -  :           2 :         CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
                   +  - ]
     712         [ +  - ]:           1 :         keystore.SetupLegacyScriptPubKeyMan();
     713   [ +  -  +  - ]:           1 :         LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
     714   [ +  -  +  -  :           2 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
             +  -  +  - ]
     715                 :             : 
     716                 :           1 :         scriptPubKey.clear();
     717   [ +  -  +  -  :           1 :         scriptPubKey << OP_9 << OP_ADD << OP_11 << OP_EQUAL;
             +  -  +  - ]
     718                 :             : 
     719   [ +  -  +  - ]:           1 :         result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
     720   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(result, ISMINE_NO);
     721   [ +  -  +  -  :           3 :         BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
          +  -  +  -  +  
                      - ]
     722                 :           1 :     }
     723   [ +  +  -  - ]:           4 : }
     724                 :             : 
     725                 :             : BOOST_AUTO_TEST_SUITE_END()
     726                 :             : } // namespace wallet
        

Generated by: LCOV version 2.0-1