/tmp/bitcoin/src/wallet/test/wallet_interfaces_tests.cpp
Line | Count | Source |
1 | | // Copyright (c) 2026-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 <interfaces/wallet.h> |
6 | | #include <key_io.h> |
7 | | #include <test/util/setup_common.h> |
8 | | #include <wallet/context.h> |
9 | | #include <wallet/test/util.h> |
10 | | #include <wallet/wallet.h> |
11 | | |
12 | | #include <boost/test/unit_test.hpp> |
13 | | #include <optional> |
14 | | |
15 | | namespace wallet { |
16 | | |
17 | | BOOST_FIXTURE_TEST_SUITE(wallet_interfaces_tests, BasicTestingSetup) |
18 | | |
19 | | BOOST_AUTO_TEST_CASE(addhdkey) |
20 | 1 | { |
21 | 1 | WalletContext context; |
22 | 1 | context.args = &m_args; |
23 | 1 | auto wallet = TestCreateWallet(context); |
24 | 1 | auto interface = interfaces::MakeWallet(context, wallet); |
25 | | |
26 | 1 | auto result = interface->addHDKey(std::nullopt); |
27 | 1 | BOOST_REQUIRE(result); |
28 | 1 | BOOST_CHECK(result->pubkey.IsValid()); |
29 | 1 | } |
30 | | |
31 | | BOOST_AUTO_TEST_CASE(addhdkey_with_key) |
32 | 1 | { |
33 | 1 | WalletContext context; |
34 | 1 | context.args = &m_args; |
35 | 1 | auto wallet = TestCreateWallet(context); |
36 | 1 | auto interface = interfaces::MakeWallet(context, wallet); |
37 | | |
38 | 1 | CKey seed_key = GenerateRandomKey(); |
39 | 1 | CExtKey key; |
40 | 1 | key.SetSeed(seed_key); |
41 | | |
42 | 1 | auto result = interface->addHDKey(key); |
43 | 1 | BOOST_REQUIRE(result); |
44 | 1 | BOOST_CHECK(result->pubkey.IsValid()); |
45 | 1 | BOOST_CHECK_EQUAL(EncodeExtPubKey(*result), EncodeExtPubKey(key.Neuter())); |
46 | 1 | } |
47 | | |
48 | | BOOST_AUTO_TEST_CASE(addhdkey_with_malformed_key) |
49 | 1 | { |
50 | 1 | WalletContext context; |
51 | 1 | context.args = &m_args; |
52 | 1 | auto wallet = TestCreateWallet(context); |
53 | 1 | auto interface = interfaces::MakeWallet(context, wallet); |
54 | | |
55 | 1 | CExtKey key; |
56 | 1 | key.SetSeed(GenerateRandomKey()); |
57 | 1 | key.nChild = 1; |
58 | | |
59 | 1 | auto result = interface->addHDKey(key); |
60 | 1 | BOOST_REQUIRE(!result); |
61 | 1 | BOOST_CHECK(result.error().code == WalletErrorCode::GenericError); |
62 | 1 | BOOST_CHECK_EQUAL(result.error().message.original, "Invalid HD key"); |
63 | 1 | } |
64 | | |
65 | | BOOST_AUTO_TEST_CASE(addhdkey_wallet_locked) |
66 | 1 | { |
67 | 1 | WalletContext context; |
68 | 1 | context.args = &m_args; |
69 | 1 | auto wallet = TestCreateWallet(context); |
70 | 1 | BOOST_REQUIRE(wallet->EncryptWallet("hunter2")); |
71 | 1 | BOOST_REQUIRE(wallet->Lock()); |
72 | | |
73 | 1 | auto interface = interfaces::MakeWallet(context, wallet); |
74 | 1 | auto result = interface->addHDKey(std::nullopt); |
75 | 1 | BOOST_REQUIRE(!result); |
76 | | BOOST_CHECK(result.error().code == WalletErrorCode::UnlockNeeded); |
77 | 1 | } |
78 | | |
79 | | BOOST_AUTO_TEST_SUITE_END() |
80 | | |
81 | | } // namespace wallet |