Bitcoin Core 31.99.0
P2P Digital Currency
scriptpubkeyman_tests.cpp
Go to the documentation of this file.
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>
9#include <script/solver.h>
11#include <wallet/wallet.h>
12#include <wallet/test/util.h>
13
14#include <boost/test/unit_test.hpp>
15
16namespace wallet {
17BOOST_FIXTURE_TEST_SUITE(scriptpubkeyman_tests, BasicTestingSetup)
18
19BOOST_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
41BOOST_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
54} // namespace wallet
node::NodeContext m_node
Definition: bitcoin-gui.cpp:47
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: common.h:19
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:235
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:309
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:30
CKey GenerateRandomKey(bool compressed) noexcept
Definition: key.cpp:352
std::string EncodeSecret(const CKey &key)
Definition: key_io.cpp:232
std::string EncodeExtPubKey(const CExtPubKey &key)
Definition: key_io.cpp:258
wallet::DescriptorScriptPubKeyMan * CreateDescriptor(CWallet &keystore, const std::string &desc_str, const bool success)
Definition: util.cpp:127
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase()
Definition: util.cpp:122
BOOST_AUTO_TEST_CASE(bnb_test)
#define BOOST_CHECK(expr)
Definition: object.cpp:16
Basic testing setup.
Definition: setup_common.h:56
Definition: key.h:229
CExtPubKey Neuter() const
Definition: key.cpp:380
void SetSeed(std::span< const std::byte > seed)
Definition: key.cpp:368
std::unique_ptr< interfaces::Chain > chain
Definition: context.h:80