scriptpubkeyman_tests.cpp raw
1 // Copyright (c) 2020-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 <key.h>
6 #include <key_io.h>
7 #include <test/util/common.h>
8 #include <test/util/setup_common.h>
9 #include <script/solver.h>
10 #include <wallet/scriptpubkeyman.h>
11 #include <wallet/wallet.h>
12 #include <wallet/test/util.h>
13
14 #include <boost/test/unit_test.hpp>
15
16 namespace wallet {
17 BOOST_FIXTURE_TEST_SUITE(scriptpubkeyman_tests, BasicTestingSetup)
18
19 BOOST_AUTO_TEST_CASE(DescriptorScriptPubKeyManTests)
20 {
21 std::unique_ptr<interfaces::Chain>& chain = m_node.chain;
22
23 CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
24 auto key_scriptpath = GenerateRandomKey();
25
26 // Verify that a SigningProvider for a pubkey is only returned if its corresponding private key is available
27 auto key_internal = GenerateRandomKey();
28 std::string desc_str = "tr(" + EncodeSecret(key_internal) + ",pk(" + HexStr(key_scriptpath.GetPubKey()) + "))";
29 auto spk_man1 = CreateDescriptor(keystore, desc_str, true);
30 BOOST_CHECK(spk_man1 != nullptr);
31 auto signprov_keypath_spendable = spk_man1->GetSigningProvider(key_internal.GetPubKey());
32 BOOST_CHECK(signprov_keypath_spendable != nullptr);
33
34 desc_str = "tr(" + HexStr(XOnlyPubKey::NUMS_H) + ",pk(" + HexStr(key_scriptpath.GetPubKey()) + "))";
35 auto spk_man2 = CreateDescriptor(keystore, desc_str, true);
36 BOOST_CHECK(spk_man2 != nullptr);
37 auto signprov_keypath_nums_h = spk_man2->GetSigningProvider(XOnlyPubKey::NUMS_H.GetEvenCorrespondingCPubKey());
38 BOOST_CHECK(signprov_keypath_nums_h == nullptr);
39 }
40
41 BOOST_AUTO_TEST_CASE(desc_spkm_topup_fail)
42 {
43 // Attempting to construct a DescriptorSPKM that cannot be topped up (hardened derivation without private keys)
44 // should throw even though it is valid and can be parsed
45 CExtKey extkey;
46 extkey.SetSeed(std::array<std::byte, 32>{});
47 CWallet keystore(m_node.chain.get(), "", CreateMockableWalletDatabase());
48 BOOST_CHECK_EXCEPTION(
49 CreateDescriptor(keystore, "wpkh(" + EncodeExtPubKey(extkey.Neuter()) + "/*h)", /*success=*/true),
50 std::runtime_error, HasReason("Could not top up scriptPubKeys"));
51 }
52
53 BOOST_AUTO_TEST_SUITE_END()
54 } // namespace wallet
55