Bitcoin Core  29.1.0
P2P Digital Currency
scriptpubkeyman_tests.cpp
Go to the documentation of this file.
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>
8 #include <script/solver.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.
21 {
22  // Set up wallet and keyman variables.
24  LegacyScriptPubKeyMan& keyman = *wallet.GetOrCreateLegacyScriptPubKeyMan();
25 
26  // Make a 1 of 2 multisig script
27  std::vector<CKey> keys(2);
28  std::vector<CPubKey> pubkeys;
29  for (CKey& key : keys) {
30  key.MakeNewKey(true);
31  pubkeys.emplace_back(key.GetPubKey());
32  }
33  CScript multisig_script = GetScriptForMultisig(1, pubkeys);
34  CScript p2sh_script = GetScriptForDestination(ScriptHash(multisig_script));
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  BOOST_CHECK(!keyman.CanProvide(p2sh_script, data));
40  keyman.AddCScript(multisig_script);
41  BOOST_CHECK(keyman.CanProvide(p2sh_script, data));
42 }
43 
44 BOOST_AUTO_TEST_CASE(DescriptorScriptPubKeyManTests)
45 {
46  std::unique_ptr<interfaces::Chain>& chain = m_node.chain;
47 
48  CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
49  auto key_scriptpath = GenerateRandomKey();
50 
51  // Verify that a SigningProvider for a pubkey is only returned if its corresponding private key is available
52  auto key_internal = GenerateRandomKey();
53  std::string desc_str = "tr(" + EncodeSecret(key_internal) + ",pk(" + HexStr(key_scriptpath.GetPubKey()) + "))";
54  auto spk_man1 = dynamic_cast<DescriptorScriptPubKeyMan*>(CreateDescriptor(keystore, desc_str, true));
55  BOOST_CHECK(spk_man1 != nullptr);
56  auto signprov_keypath_spendable = spk_man1->GetSigningProvider(key_internal.GetPubKey());
57  BOOST_CHECK(signprov_keypath_spendable != nullptr);
58 
59  desc_str = "tr(" + HexStr(XOnlyPubKey::NUMS_H) + ",pk(" + HexStr(key_scriptpath.GetPubKey()) + "))";
60  auto spk_man2 = dynamic_cast<DescriptorScriptPubKeyMan*>(CreateDescriptor(keystore, desc_str, true));
61  BOOST_CHECK(spk_man2 != nullptr);
62  auto signprov_keypath_nums_h = spk_man2->GetSigningProvider(XOnlyPubKey::NUMS_H.GetEvenCorrespondingCPubKey());
63  BOOST_CHECK(signprov_keypath_nums_h == nullptr);
64 }
65 
67 } // namespace wallet
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: solver.cpp:218
std::unique_ptr< interfaces::Chain > chain
Definition: context.h:76
wallet::ScriptPubKeyMan * CreateDescriptor(CWallet &keystore, const std::string &desc_str, const bool success)
Definition: util.cpp:196
node::NodeContext m_node
Definition: bitcoin-gui.cpp:42
static const XOnlyPubKey NUMS_H
Nothing Up My Sleeve point H Used as an internal key for provably disabling the key path spend see BI...
Definition: pubkey.h:239
Basic testing setup.
Definition: setup_common.h:64
CKey GenerateRandomKey(bool compressed) noexcept
Definition: key.cpp:352
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:299
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:186
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:29
An encapsulated private key.
Definition: key.h:34
BOOST_AUTO_TEST_CASE(bnb_search_test)
std::string EncodeSecret(const CKey &key)
Definition: key_io.cpp:231
#define BOOST_CHECK(expr)
Definition: object.cpp:17