/tmp/bitcoin/src/wallet/test/walletload_tests.cpp
Line | Count | Source |
1 | | // Copyright (c) 2022-present The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or https://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #include <wallet/test/util.h> |
6 | | #include <wallet/wallet.h> |
7 | | #include <test/util/common.h> |
8 | | #include <test/util/logging.h> |
9 | | #include <test/util/setup_common.h> |
10 | | |
11 | | #include <boost/test/unit_test.hpp> |
12 | | |
13 | | namespace wallet { |
14 | | |
15 | | BOOST_AUTO_TEST_SUITE(walletload_tests) |
16 | | |
17 | | class DummyDescriptor final : public Descriptor { |
18 | | private: |
19 | | std::string desc; |
20 | | public: |
21 | 2 | explicit DummyDescriptor(const std::string& descriptor) : desc(descriptor) {}; |
22 | 2 | ~DummyDescriptor() = default; |
23 | | |
24 | 2 | std::string ToString(bool compat_format) const override { return desc; } |
25 | 0 | std::string ToCanonicalString() const override { return desc; } |
26 | 0 | std::optional<OutputType> GetOutputType() const override { return OutputType::UNKNOWN; } |
27 | | |
28 | 4 | bool IsRange() const override { return true; } |
29 | 0 | bool IsSolvable() const override { return false; } |
30 | 0 | bool IsSingleType() const override { return true; } |
31 | 0 | bool HavePrivateKeys(const SigningProvider&) const override { return false; } |
32 | 0 | bool ToPrivateString(const SigningProvider& provider, std::string& out) const override { return false; } |
33 | 0 | bool ToNormalizedString(const SigningProvider& provider, std::string& out, const DescriptorCache* cache = nullptr) const override { return false; } |
34 | 0 | bool Expand(int pos, const SigningProvider& provider, std::vector<CScript>& output_scripts, FlatSigningProvider& out, DescriptorCache* write_cache = nullptr) const override { return false; }; |
35 | 0 | bool ExpandFromCache(int pos, const DescriptorCache& read_cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const override { return false; } |
36 | 0 | void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const override {} |
37 | 0 | std::optional<int64_t> ScriptSize() const override { return {}; } |
38 | 0 | std::optional<int64_t> MaxSatisfactionWeight(bool) const override { return {}; } |
39 | 0 | std::optional<int64_t> MaxSatisfactionElems() const override { return {}; } |
40 | 0 | void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs) const override {} |
41 | 0 | bool HasScripts() const override { return true; } |
42 | 0 | std::vector<std::string> Warnings() const override { return {}; } |
43 | 0 | uint32_t GetMaxKeyExpr() const override { return 0; } |
44 | 0 | size_t GetKeyCount() const override { return 0; } |
45 | 0 | bool CanSelfExpand() const final { return false; } |
46 | | }; |
47 | | |
48 | | BOOST_FIXTURE_TEST_CASE(wallet_load_descriptors, TestingSetup) |
49 | 1 | { |
50 | 1 | bilingual_str _error; |
51 | 1 | std::vector<bilingual_str> _warnings; |
52 | 1 | std::unique_ptr<WalletDatabase> database = CreateMockableWalletDatabase(); |
53 | 1 | { |
54 | | // Write unknown active descriptor |
55 | 1 | WalletBatch batch(*database); |
56 | 1 | std::string unknown_desc = "trx(tpubD6NzVbkrYhZ4Y4S7m6Y5s9GD8FqEMBy56AGphZXuagajudVZEnYyBahZMgHNCTJc2at82YX6s8JiL1Lohu5A3v1Ur76qguNH4QVQ7qYrBQx/86'/1'/0'/0/*)#8pn8tzdt"; |
57 | 1 | WalletDescriptor wallet_descriptor(std::make_shared<DummyDescriptor>(unknown_desc), 0, 0, 0, 0); |
58 | 1 | BOOST_CHECK(batch.WriteDescriptor(uint256(), wallet_descriptor)); |
59 | 1 | BOOST_CHECK(batch.WriteActiveScriptPubKeyMan(static_cast<uint8_t>(OutputType::UNKNOWN), uint256(), false)); |
60 | 1 | } |
61 | | |
62 | 1 | { |
63 | | // Now try to load the wallet and verify the error. |
64 | 1 | const std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", std::move(database))); |
65 | 1 | BOOST_CHECK_EQUAL(wallet->PopulateWalletFromDB(_error, _warnings), DBErrors::UNKNOWN_DESCRIPTOR); |
66 | 1 | } |
67 | | |
68 | | // Test 2 |
69 | | // Now write a valid descriptor with a different ID which must be accepted |
70 | 1 | database = CreateMockableWalletDatabase(); |
71 | | |
72 | 1 | { |
73 | | // Write valid descriptor with arbitrary ID |
74 | 1 | WalletBatch batch(*database); |
75 | 1 | std::string desc = "wpkh([d34db33f/84h/0h/0h]xpub6DJ2dNUysrn5Vt36jH2KLBT2i1auw1tTSSomg8PhqNiUtx8QX2SvC9nrHu81fT41fvDUnhMjEzQgXnQjKEu3oaqMSzhSrHMxyyoEAmUHQbY/0/*)#cjjspncu"; |
76 | 1 | WalletDescriptor wallet_descriptor(std::make_shared<DummyDescriptor>(desc), 0, 0, 0, 0); |
77 | 1 | BOOST_CHECK(batch.WriteDescriptor(uint256::ONE, wallet_descriptor)); |
78 | 1 | } |
79 | | |
80 | 1 | { |
81 | | // Now try to load the wallet and verify the result. |
82 | 1 | const std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", std::move(database))); |
83 | | BOOST_CHECK_EQUAL(wallet->PopulateWalletFromDB(_error, _warnings), DBErrors::LOAD_OK); |
84 | 1 | } |
85 | 1 | } |
86 | | |
87 | | BOOST_AUTO_TEST_SUITE_END() |
88 | | } // namespace wallet |