Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/wallet/test/ismine_tests.cpp
Line
Count
Source
1
// Copyright (c) 2017-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 <node/context.h>
8
#include <script/script.h>
9
#include <script/solver.h>
10
#include <script/signingprovider.h>
11
#include <test/util/setup_common.h>
12
#include <wallet/types.h>
13
#include <wallet/wallet.h>
14
#include <wallet/test/util.h>
15
16
#include <boost/test/unit_test.hpp>
17
18
using namespace util::hex_literals;
19
20
namespace wallet {
21
BOOST_FIXTURE_TEST_SUITE(ismine_tests, BasicTestingSetup)
22
23
BOOST_AUTO_TEST_CASE(ismine_standard)
24
1
{
25
1
    CKey keys[2];
26
1
    CPubKey pubkeys[2];
27
3
    for (int i = 0; i < 2; i++) {
28
2
        keys[i].MakeNewKey(true);
29
2
        pubkeys[i] = keys[i].GetPubKey();
30
2
    }
31
32
1
    CKey uncompressedKey = GenerateRandomKey(/*compressed=*/false);
33
1
    CPubKey uncompressedPubkey = uncompressedKey.GetPubKey();
34
1
    std::unique_ptr<interfaces::Chain>& chain = m_node.chain;
35
36
1
    CScript scriptPubKey;
37
1
    bool result;
38
39
    // P2PK compressed - Descriptor
40
1
    {
41
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
42
1
        std::string desc_str = "pk(" + EncodeSecret(keys[0]) + ")";
43
44
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, true);
45
46
1
        scriptPubKey = GetScriptForRawPubKey(pubkeys[0]);
47
1
        result = spk_manager->IsMine(scriptPubKey);
48
1
        BOOST_CHECK(result);
49
1
    }
50
51
    // P2PK uncompressed - Descriptor
52
1
    {
53
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
54
1
        std::string desc_str = "pk(" + EncodeSecret(uncompressedKey) + ")";
55
56
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, true);
57
58
1
        scriptPubKey = GetScriptForRawPubKey(uncompressedPubkey);
59
1
        result = spk_manager->IsMine(scriptPubKey);
60
1
        BOOST_CHECK(result);
61
1
    }
62
63
    // P2PKH compressed - Descriptor
64
1
    {
65
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
66
1
        std::string desc_str = "pkh(" + EncodeSecret(keys[0]) + ")";
67
68
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, true);
69
70
1
        scriptPubKey = GetScriptForDestination(PKHash(pubkeys[0]));
71
1
        result = spk_manager->IsMine(scriptPubKey);
72
1
        BOOST_CHECK(result);
73
1
    }
74
75
    // P2PKH uncompressed - Descriptor
76
1
    {
77
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
78
1
        std::string desc_str = "pkh(" + EncodeSecret(uncompressedKey) + ")";
79
80
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, true);
81
82
1
        scriptPubKey = GetScriptForDestination(PKHash(uncompressedPubkey));
83
1
        result = spk_manager->IsMine(scriptPubKey);
84
1
        BOOST_CHECK(result);
85
1
    }
86
87
    // P2SH - Descriptor
88
1
    {
89
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
90
1
        std::string desc_str = "sh(pkh(" + EncodeSecret(keys[0]) + "))";
91
92
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, true);
93
94
1
        CScript redeemScript = GetScriptForDestination(PKHash(pubkeys[0]));
95
1
        scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
96
1
        result = spk_manager->IsMine(scriptPubKey);
97
1
        BOOST_CHECK(result);
98
1
    }
99
100
    // (P2PKH inside) P2SH inside P2SH (invalid) - Descriptor
101
1
    {
102
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
103
1
        std::string desc_str = "sh(sh(" + EncodeSecret(keys[0]) + "))";
104
105
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, false);
106
1
        BOOST_CHECK_EQUAL(spk_manager, nullptr);
107
1
    }
108
109
    // (P2PKH inside) P2SH inside P2WSH (invalid) - Descriptor
110
1
    {
111
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
112
1
        std::string desc_str = "wsh(sh(" + EncodeSecret(keys[0]) + "))";
113
114
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, false);
115
1
        BOOST_CHECK_EQUAL(spk_manager, nullptr);
116
1
    }
117
118
    // P2WPKH inside P2WSH (invalid) - Descriptor
119
1
    {
120
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
121
1
        std::string desc_str = "wsh(wpkh(" + EncodeSecret(keys[0]) + "))";
122
123
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, false);
124
1
        BOOST_CHECK_EQUAL(spk_manager, nullptr);
125
1
    }
126
127
    // (P2PKH inside) P2WSH inside P2WSH (invalid) - Descriptor
128
1
    {
129
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
130
1
        std::string desc_str = "wsh(wsh(" + EncodeSecret(keys[0]) + "))";
131
132
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, false);
133
1
        BOOST_CHECK_EQUAL(spk_manager, nullptr);
134
1
    }
135
136
    // P2WPKH compressed - Descriptor
137
1
    {
138
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
139
1
        std::string desc_str = "wpkh(" + EncodeSecret(keys[0]) + ")";
140
141
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, true);
142
143
1
        scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(pubkeys[0]));
144
1
        result = spk_manager->IsMine(scriptPubKey);
145
1
        BOOST_CHECK(result);
146
1
    }
147
148
    // P2WPKH uncompressed (invalid) - Descriptor
149
1
    {
150
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
151
1
        std::string desc_str = "wpkh(" + EncodeSecret(uncompressedKey) + ")";
152
153
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, false);
154
1
        BOOST_CHECK_EQUAL(spk_manager, nullptr);
155
1
    }
156
157
    // scriptPubKey multisig - Descriptor
158
1
    {
159
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
160
1
        std::string desc_str = "multi(2," + EncodeSecret(uncompressedKey) + "," + EncodeSecret(keys[1]) + ")";
161
162
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, true);
163
164
1
        scriptPubKey = GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
165
1
        result = spk_manager->IsMine(scriptPubKey);
166
1
        BOOST_CHECK(result);
167
1
    }
168
169
    // P2SH multisig - Descriptor
170
1
    {
171
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
172
173
1
        std::string desc_str = "sh(multi(2," + EncodeSecret(uncompressedKey) + "," + EncodeSecret(keys[1]) + "))";
174
175
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, true);
176
177
1
        CScript redeemScript = GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
178
1
        scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
179
1
        result = spk_manager->IsMine(scriptPubKey);
180
1
        BOOST_CHECK(result);
181
1
    }
182
183
    // P2WSH multisig with compressed keys - Descriptor
184
1
    {
185
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
186
187
1
        std::string desc_str = "wsh(multi(2," + EncodeSecret(keys[0]) + "," + EncodeSecret(keys[1]) + "))";
188
189
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, true);
190
191
1
        CScript redeemScript = GetScriptForMultisig(2, {pubkeys[0], pubkeys[1]});
192
1
        scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(redeemScript));
193
1
        result = spk_manager->IsMine(scriptPubKey);
194
1
        BOOST_CHECK(result);
195
1
    }
196
197
    // P2WSH multisig with uncompressed key (invalid) - Descriptor
198
1
    {
199
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
200
201
1
        std::string desc_str = "wsh(multi(2," + EncodeSecret(uncompressedKey) + "," + EncodeSecret(keys[1]) + "))";
202
203
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, false);
204
1
        BOOST_CHECK_EQUAL(spk_manager, nullptr);
205
1
    }
206
207
    // P2WSH multisig wrapped in P2SH - Descriptor
208
1
    {
209
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
210
211
1
        std::string desc_str = "sh(wsh(multi(2," + EncodeSecret(keys[0]) + "," + EncodeSecret(keys[1]) + ")))";
212
213
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, true);
214
215
1
        CScript witnessScript = GetScriptForMultisig(2, {pubkeys[0], pubkeys[1]});
216
1
        CScript redeemScript = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
217
1
        scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
218
1
        result = spk_manager->IsMine(scriptPubKey);
219
1
        BOOST_CHECK(result);
220
1
    }
221
222
    // Combo - Descriptor
223
1
    {
224
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
225
226
1
        std::string desc_str = "combo(" + EncodeSecret(keys[0]) + ")";
227
228
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, true);
229
230
        // Test P2PK
231
1
        result = spk_manager->IsMine(GetScriptForRawPubKey(pubkeys[0]));
232
1
        BOOST_CHECK(result);
233
234
        // Test P2PKH
235
1
        result = spk_manager->IsMine(GetScriptForDestination(PKHash(pubkeys[0])));
236
1
        BOOST_CHECK(result);
237
238
        // Test P2SH (combo descriptor does not describe P2SH)
239
1
        CScript redeemScript = GetScriptForDestination(PKHash(pubkeys[0]));
240
1
        scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
241
1
        result = spk_manager->IsMine(scriptPubKey);
242
1
        BOOST_CHECK(!result);
243
244
        // Test P2WPKH
245
1
        scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(pubkeys[0]));
246
1
        result = spk_manager->IsMine(scriptPubKey);
247
1
        BOOST_CHECK(result);
248
249
        // P2SH-P2WPKH output
250
1
        redeemScript = GetScriptForDestination(WitnessV0KeyHash(pubkeys[0]));
251
1
        scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
252
1
        result = spk_manager->IsMine(scriptPubKey);
253
1
        BOOST_CHECK(result);
254
255
        // Test P2TR (combo descriptor does not describe P2TR)
256
1
        XOnlyPubKey xpk(pubkeys[0]);
257
1
        Assert(xpk.IsFullyValid());
258
1
        TaprootBuilder builder;
259
1
        builder.Finalize(xpk);
260
1
        WitnessV1Taproot output = builder.GetOutput();
261
1
        scriptPubKey = GetScriptForDestination(output);
262
1
        result = spk_manager->IsMine(scriptPubKey);
263
1
        BOOST_CHECK(!result);
264
1
    }
265
266
    // Taproot - Descriptor
267
1
    {
268
1
        CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
269
270
1
        std::string desc_str = "tr(" + EncodeSecret(keys[0]) + ")";
271
272
1
        auto spk_manager = CreateDescriptor(keystore, desc_str, true);
273
274
1
        XOnlyPubKey xpk(pubkeys[0]);
275
1
        Assert(xpk.IsFullyValid());
276
1
        TaprootBuilder builder;
277
1
        builder.Finalize(xpk);
278
1
        WitnessV1Taproot output = builder.GetOutput();
279
1
        scriptPubKey = GetScriptForDestination(output);
280
1
        result = spk_manager->IsMine(scriptPubKey);
281
        BOOST_CHECK(result);
282
1
    }
283
1
}
284
285
BOOST_AUTO_TEST_SUITE_END()
286
} // namespace wallet