Bitcoin Core  24.99.0
P2P Digital Currency
ismine_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2021 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 <node/context.h>
7 #include <script/script.h>
8 #include <script/standard.h>
10 #include <wallet/ismine.h>
11 #include <wallet/wallet.h>
12 
13 #include <boost/test/unit_test.hpp>
14 
15 
16 namespace wallet {
17 BOOST_FIXTURE_TEST_SUITE(ismine_tests, BasicTestingSetup)
18 
19 BOOST_AUTO_TEST_CASE(ismine_standard)
20 {
21  CKey keys[2];
22  CPubKey pubkeys[2];
23  for (int i = 0; i < 2; i++) {
24  keys[i].MakeNewKey(true);
25  pubkeys[i] = keys[i].GetPubKey();
26  }
27 
28  CKey uncompressedKey;
29  uncompressedKey.MakeNewKey(false);
30  CPubKey uncompressedPubkey = uncompressedKey.GetPubKey();
31  std::unique_ptr<interfaces::Chain>& chain = m_node.chain;
32 
33  CScript scriptPubKey;
34  isminetype result;
35 
36  // P2PK compressed
37  {
38  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
39  keystore.SetupLegacyScriptPubKeyMan();
41  scriptPubKey = GetScriptForRawPubKey(pubkeys[0]);
42 
43  // Keystore does not have key
44  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
46  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
47 
48  // Keystore has key
49  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
50  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
52  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
53  }
54 
55  // P2PK uncompressed
56  {
57  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
58  keystore.SetupLegacyScriptPubKeyMan();
60  scriptPubKey = GetScriptForRawPubKey(uncompressedPubkey);
61 
62  // Keystore does not have key
63  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
65  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
66 
67  // Keystore has key
68  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
69  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
71  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
72  }
73 
74  // P2PKH compressed
75  {
76  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
77  keystore.SetupLegacyScriptPubKeyMan();
79  scriptPubKey = GetScriptForDestination(PKHash(pubkeys[0]));
80 
81  // Keystore does not have key
82  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
84  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
85 
86  // Keystore has key
87  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
88  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
90  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
91  }
92 
93  // P2PKH uncompressed
94  {
95  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
96  keystore.SetupLegacyScriptPubKeyMan();
98  scriptPubKey = GetScriptForDestination(PKHash(uncompressedPubkey));
99 
100  // Keystore does not have key
101  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
102  BOOST_CHECK_EQUAL(result, ISMINE_NO);
103  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
104 
105  // Keystore has key
106  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
107  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
109  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
110  }
111 
112  // P2SH
113  {
114  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
115  keystore.SetupLegacyScriptPubKeyMan();
117 
118  CScript redeemScript = GetScriptForDestination(PKHash(pubkeys[0]));
119  scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
120 
121  // Keystore does not have redeemScript or key
122  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
123  BOOST_CHECK_EQUAL(result, ISMINE_NO);
124  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
125 
126  // Keystore has redeemScript but no key
127  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemScript));
128  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
129  BOOST_CHECK_EQUAL(result, ISMINE_NO);
130  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
131 
132  // Keystore has redeemScript and key
133  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
134  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
136  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
137  }
138 
139  // (P2PKH inside) P2SH inside P2SH (invalid)
140  {
141  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
142  keystore.SetupLegacyScriptPubKeyMan();
144 
145  CScript redeemscript_inner = GetScriptForDestination(PKHash(pubkeys[0]));
146  CScript redeemscript = GetScriptForDestination(ScriptHash(redeemscript_inner));
147  scriptPubKey = GetScriptForDestination(ScriptHash(redeemscript));
148 
149  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemscript));
150  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemscript_inner));
151  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
152  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
153  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
154  BOOST_CHECK_EQUAL(result, ISMINE_NO);
155  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
156  }
157 
158  // (P2PKH inside) P2SH inside P2WSH (invalid)
159  {
160  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
161  keystore.SetupLegacyScriptPubKeyMan();
163 
164  CScript redeemscript = GetScriptForDestination(PKHash(pubkeys[0]));
165  CScript witnessscript = GetScriptForDestination(ScriptHash(redeemscript));
166  scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessscript));
167 
168  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(witnessscript));
169  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemscript));
170  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
171  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
172  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
173  BOOST_CHECK_EQUAL(result, ISMINE_NO);
174  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
175  }
176 
177  // P2WPKH inside P2WSH (invalid)
178  {
179  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
180  keystore.SetupLegacyScriptPubKeyMan();
182 
183  CScript witnessscript = GetScriptForDestination(WitnessV0KeyHash(pubkeys[0]));
184  scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessscript));
185 
186  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(witnessscript));
187  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
188  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
189  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
190  BOOST_CHECK_EQUAL(result, ISMINE_NO);
191  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
192  }
193 
194  // (P2PKH inside) P2WSH inside P2WSH (invalid)
195  {
196  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
197  keystore.SetupLegacyScriptPubKeyMan();
199 
200  CScript witnessscript_inner = GetScriptForDestination(PKHash(pubkeys[0]));
201  CScript witnessscript = GetScriptForDestination(WitnessV0ScriptHash(witnessscript_inner));
202  scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessscript));
203 
204  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(witnessscript_inner));
205  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(witnessscript));
206  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
207  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
208  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
209  BOOST_CHECK_EQUAL(result, ISMINE_NO);
210  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
211  }
212 
213  // P2WPKH compressed
214  {
215  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
216  keystore.SetupLegacyScriptPubKeyMan();
218  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
219 
220  scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(pubkeys[0]));
221 
222  // Keystore implicitly has key and P2SH redeemScript
223  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
224  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
226  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
227  }
228 
229  // P2WPKH uncompressed
230  {
231  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
232  keystore.SetupLegacyScriptPubKeyMan();
234  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
235 
236  scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(uncompressedPubkey));
237 
238  // Keystore has key, but no P2SH redeemScript
239  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
240  BOOST_CHECK_EQUAL(result, ISMINE_NO);
241  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
242 
243  // Keystore has key and P2SH redeemScript
244  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
245  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
246  BOOST_CHECK_EQUAL(result, ISMINE_NO);
247  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
248  }
249 
250  // scriptPubKey multisig
251  {
252  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
253  keystore.SetupLegacyScriptPubKeyMan();
255 
256  scriptPubKey = GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
257 
258  // Keystore does not have any keys
259  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
260  BOOST_CHECK_EQUAL(result, ISMINE_NO);
261  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
262 
263  // Keystore has 1/2 keys
264  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
265 
266  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
267  BOOST_CHECK_EQUAL(result, ISMINE_NO);
268  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
269 
270  // Keystore has 2/2 keys
271  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
272 
273  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
274  BOOST_CHECK_EQUAL(result, ISMINE_NO);
275  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
276 
277  // Keystore has 2/2 keys and the script
278  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
279 
280  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
281  BOOST_CHECK_EQUAL(result, ISMINE_NO);
282  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
283  }
284 
285  // P2SH multisig
286  {
287  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
288  keystore.SetupLegacyScriptPubKeyMan();
290  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
291  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
292 
293  CScript redeemScript = GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
294  scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
295 
296  // Keystore has no redeemScript
297  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
298  BOOST_CHECK_EQUAL(result, ISMINE_NO);
299  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
300 
301  // Keystore has redeemScript
302  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemScript));
303  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
305  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
306  }
307 
308  // P2WSH multisig with compressed keys
309  {
310  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
311  keystore.SetupLegacyScriptPubKeyMan();
313  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
314  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
315 
316  CScript witnessScript = GetScriptForMultisig(2, {pubkeys[0], pubkeys[1]});
317  scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
318 
319  // Keystore has keys, but no witnessScript or P2SH redeemScript
320  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
321  BOOST_CHECK_EQUAL(result, ISMINE_NO);
322  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
323 
324  // Keystore has keys and witnessScript, but no P2SH redeemScript
325  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(witnessScript));
326  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
327  BOOST_CHECK_EQUAL(result, ISMINE_NO);
328  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
329 
330  // Keystore has keys, witnessScript, P2SH redeemScript
331  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
332  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
334  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
335  }
336 
337  // P2WSH multisig with uncompressed key
338  {
339  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
340  keystore.SetupLegacyScriptPubKeyMan();
342  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
343  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
344 
345  CScript witnessScript = GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
346  scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
347 
348  // Keystore has keys, but no witnessScript or P2SH redeemScript
349  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
350  BOOST_CHECK_EQUAL(result, ISMINE_NO);
351  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
352 
353  // Keystore has keys and witnessScript, but no P2SH redeemScript
354  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(witnessScript));
355  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
356  BOOST_CHECK_EQUAL(result, ISMINE_NO);
357  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
358 
359  // Keystore has keys, witnessScript, P2SH redeemScript
360  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
361  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
362  BOOST_CHECK_EQUAL(result, ISMINE_NO);
363  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
364  }
365 
366  // P2WSH multisig wrapped in P2SH
367  {
368  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
369  keystore.SetupLegacyScriptPubKeyMan();
371 
372  CScript witnessScript = GetScriptForMultisig(2, {pubkeys[0], pubkeys[1]});
373  CScript redeemScript = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
374  scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
375 
376  // Keystore has no witnessScript, P2SH redeemScript, or keys
377  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
378  BOOST_CHECK_EQUAL(result, ISMINE_NO);
379  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
380 
381  // Keystore has witnessScript and P2SH redeemScript, but no keys
382  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemScript));
383  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(witnessScript));
384  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
385  BOOST_CHECK_EQUAL(result, ISMINE_NO);
386  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
387 
388  // Keystore has keys, witnessScript, P2SH redeemScript
389  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
390  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
391  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
393  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 1);
394  }
395 
396  // OP_RETURN
397  {
398  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
399  keystore.SetupLegacyScriptPubKeyMan();
401  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
402 
403  scriptPubKey.clear();
404  scriptPubKey << OP_RETURN << ToByteVector(pubkeys[0]);
405 
406  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
407  BOOST_CHECK_EQUAL(result, ISMINE_NO);
408  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
409  }
410 
411  // witness unspendable
412  {
413  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
414  keystore.SetupLegacyScriptPubKeyMan();
416  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
417 
418  scriptPubKey.clear();
419  scriptPubKey << OP_0 << ToByteVector(ParseHex("aabb"));
420 
421  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
422  BOOST_CHECK_EQUAL(result, ISMINE_NO);
423  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
424  }
425 
426  // witness unknown
427  {
428  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
429  keystore.SetupLegacyScriptPubKeyMan();
431  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
432 
433  scriptPubKey.clear();
434  scriptPubKey << OP_16 << ToByteVector(ParseHex("aabb"));
435 
436  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
437  BOOST_CHECK_EQUAL(result, ISMINE_NO);
438  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
439  }
440 
441  // Nonstandard
442  {
443  CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase());
444  keystore.SetupLegacyScriptPubKeyMan();
446  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
447 
448  scriptPubKey.clear();
449  scriptPubKey << OP_9 << OP_ADD << OP_11 << OP_EQUAL;
450 
451  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
452  BOOST_CHECK_EQUAL(result, ISMINE_NO);
453  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->GetScriptPubKeys().count(scriptPubKey) == 0);
454  }
455 }
456 
458 } // namespace wallet
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
An encapsulated private key.
Definition: key.h:27
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:160
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
An encapsulated public key.
Definition: pubkey.h:34
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
void clear()
Definition: script.h:554
virtual bool AddKey(const CKey &key)
RecursiveMutex cs_KeyStore
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:236
LegacyScriptPubKeyMan * GetLegacyScriptPubKeyMan() const
Get the LegacyScriptPubKeyMan which is used for all types, internal, and external.
Definition: wallet.cpp:3385
void SetupLegacyScriptPubKeyMan()
Make a LegacyScriptPubKeyMan and set it for all types, internal, and external.
Definition: wallet.cpp:3403
isminetype IsMine(const CScript &script) const override
bool AddCScript(const CScript &redeemScript) override
const std::unordered_set< CScript, SaltedSipHasher > GetScriptPubKeys() const override
Returns a set of all the scriptPubKeys that this ScriptPubKeyMan watches.
BOOST_AUTO_TEST_SUITE_END()
Definition: node.h:39
isminetype
IsMine() return codes, which depend on ScriptPubKeyMan implementation.
Definition: ismine.h:41
@ ISMINE_NO
Definition: ismine.h:42
@ ISMINE_SPENDABLE
Definition: ismine.h:44
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1236
BOOST_AUTO_TEST_CASE(bnb_search_test)
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
#define BOOST_CHECK(expr)
Definition: object.cpp:16
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:63
@ OP_16
Definition: script.h:95
@ OP_EQUAL
Definition: script.h:142
@ OP_ADD
Definition: script.h:157
@ OP_9
Definition: script.h:88
@ OP_11
Definition: script.h:90
@ OP_0
Definition: script.h:72
@ OP_RETURN
Definition: script.h:107
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:344
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:339
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:334
Basic testing setup.
Definition: setup_common.h:83
std::unique_ptr< interfaces::Chain > chain
Definition: context.h:57
#define LOCK(cs)
Definition: sync.h:261
std::vector< Byte > ParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).