Bitcoin Core  24.99.0
P2P Digital Currency
Go to the documentation of this file.
1 // Copyright (c) 2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or
5 #include <wallet/wallet.h>
8 #include <boost/test/unit_test.hpp>
10 namespace wallet {
12 BOOST_AUTO_TEST_SUITE(walletload_tests)
14 class DummyDescriptor final : public Descriptor {
15 private:
16  std::string desc;
17 public:
18  explicit DummyDescriptor(const std::string& descriptor) : desc(descriptor) {};
19  ~DummyDescriptor() = default;
21  std::string ToString() const override { return desc; }
22  std::optional<OutputType> GetOutputType() const override { return OutputType::UNKNOWN; }
24  bool IsRange() const override { return false; }
25  bool IsSolvable() const override { return false; }
26  bool IsSingleType() const override { return true; }
27  bool ToPrivateString(const SigningProvider& provider, std::string& out) const override { return false; }
28  bool ToNormalizedString(const SigningProvider& provider, std::string& out, const DescriptorCache* cache = nullptr) const override { return false; }
29  bool Expand(int pos, const SigningProvider& provider, std::vector<CScript>& output_scripts, FlatSigningProvider& out, DescriptorCache* write_cache = nullptr) const override { return false; };
30  bool ExpandFromCache(int pos, const DescriptorCache& read_cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const override { return false; }
31  void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const override {}
32 };
34 BOOST_FIXTURE_TEST_CASE(wallet_load_unknown_descriptor, TestingSetup)
35 {
36  std::unique_ptr<WalletDatabase> database = CreateMockWalletDatabase();
37  {
38  // Write unknown active descriptor
39  WalletBatch batch(*database, false);
40  std::string unknown_desc = "trx(tpubD6NzVbkrYhZ4Y4S7m6Y5s9GD8FqEMBy56AGphZXuagajudVZEnYyBahZMgHNCTJc2at82YX6s8JiL1Lohu5A3v1Ur76qguNH4QVQ7qYrBQx/86'/1'/0'/0/*)#8pn8tzdt";
41  WalletDescriptor wallet_descriptor(std::make_shared<DummyDescriptor>(unknown_desc), 0, 0, 0, 0);
42  BOOST_CHECK(batch.WriteDescriptor(uint256(), wallet_descriptor));
43  BOOST_CHECK(batch.WriteActiveScriptPubKeyMan(static_cast<uint8_t>(OutputType::UNKNOWN), uint256(), false));
44  }
46  {
47  // Now try to load the wallet and verify the error.
48  const std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", m_args, std::move(database)));
50  }
51 }
54 } // namespace wallet
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
Cache for single descriptor's derived extended pubkeys.
Definition: descriptor.h:19
An interface to be implemented by keystores that support signing.
256-bit opaque blob.
Definition: uint256.h:119
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:236
bool IsSolvable() const override
Whether this descriptor has all information about signing ignoring lack of private keys.
bool ToNormalizedString(const SigningProvider &provider, std::string &out, const DescriptorCache *cache=nullptr) const override
Convert the descriptor to a normalized string.
void ExpandPrivate(int pos, const SigningProvider &provider, FlatSigningProvider &out) const override
Expand the private key for a descriptor at a specified position, if possible.
bool ToPrivateString(const SigningProvider &provider, std::string &out) const override
Convert the descriptor to a private string.
bool Expand(int pos, const SigningProvider &provider, std::vector< CScript > &output_scripts, FlatSigningProvider &out, DescriptorCache *write_cache=nullptr) const override
Expand a descriptor at a specified position.
std::optional< OutputType > GetOutputType() const override
bool ExpandFromCache(int pos, const DescriptorCache &read_cache, std::vector< CScript > &output_scripts, FlatSigningProvider &out) const override
Expand a descriptor at a specified position using cached expansion data.
bool IsRange() const override
Whether the expansion of this descriptor depends on the position.
bool IsSingleType() const override
Whether this descriptor will return one scriptPubKey or multiple (aka is or is not combo)
std::string ToString() const override
Convert the descriptor back to a string, undoing parsing.
DummyDescriptor(const std::string &descriptor)
Access to the wallet database.
Definition: walletdb.h:188
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:242
bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id, bool internal)
Definition: walletdb.cpp:210
Descriptor with some wallet metadata.
Definition: walletutil.h:77
Test Suite for CuckooCache.
Definition: node.h:39
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase(DatabaseOptions &options)
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1242
BOOST_FIXTURE_TEST_CASE(BasicOutputTypesTest, AvailableCoinsTestingSetup)
std::shared_ptr< CWallet > wallet
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
#define BOOST_CHECK(expr)
Definition: object.cpp:16
Interface for parsed descriptor objects.
Definition: descriptor.h:98
Testing setup that configures a complete environment.
Definition: setup_common.h:109
std::unique_ptr< interfaces::Chain > chain
Definition: context.h:57