Bitcoin ABC  0.26.3
P2P Digital Currency
ismine_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2019 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 <chainparams.h>
6 #include <key.h>
7 #include <node/context.h>
8 #include <script/script.h>
9 #include <script/script_error.h>
10 #include <script/standard.h>
11 #include <wallet/ismine.h>
12 #include <wallet/wallet.h>
13 
14 #include <test/util/setup_common.h>
15 
16 #include <boost/test/unit_test.hpp>
17 
18 BOOST_FIXTURE_TEST_SUITE(ismine_tests, BasicTestingSetup)
19 
20 BOOST_AUTO_TEST_CASE(ismine_standard) {
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(), "", CreateDummyWalletDatabase());
39  keystore.SetupLegacyScriptPubKeyMan();
41  scriptPubKey = GetScriptForRawPubKey(pubkeys[0]);
42 
43  // Keystore does not have key
44  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
46 
47  // Keystore has key
48  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
49  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
51  }
52 
53  // P2PK uncompressed
54  {
55  CWallet keystore(chain.get(), "", CreateDummyWalletDatabase());
56  keystore.SetupLegacyScriptPubKeyMan();
58  scriptPubKey = GetScriptForRawPubKey(uncompressedPubkey);
59 
60  // Keystore does not have key
61  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
63 
64  // Keystore has key
66  keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
67  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
69  }
70 
71  // P2PKH compressed
72  {
73  CWallet keystore(chain.get(), "", CreateDummyWalletDatabase());
74 
75  keystore.SetupLegacyScriptPubKeyMan();
77  scriptPubKey = GetScriptForDestination(PKHash(pubkeys[0]));
78 
79  // Keystore does not have key
80  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
82 
83  // Keystore has key
84  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
85  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
87  }
88 
89  // P2PKH uncompressed
90  {
91  CWallet keystore(chain.get(), "", CreateDummyWalletDatabase());
92 
93  keystore.SetupLegacyScriptPubKeyMan();
95  scriptPubKey = GetScriptForDestination(PKHash(uncompressedPubkey));
96 
97  // Keystore does not have key
98  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
100 
101  // Keystore has key
102  BOOST_CHECK(
103  keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
104  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
106  }
107 
108  // P2SH
109  {
110  CWallet keystore(chain.get(), "", CreateDummyWalletDatabase());
111 
112  keystore.SetupLegacyScriptPubKeyMan();
114 
115  CScript redeemScript = GetScriptForDestination(PKHash(pubkeys[0]));
116  scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
117 
118  // Keystore does not have redeemScript or key
119  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
120  BOOST_CHECK_EQUAL(result, ISMINE_NO);
121 
122  // Keystore has redeemScript but no key
123  BOOST_CHECK(
124  keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemScript));
125  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
126  BOOST_CHECK_EQUAL(result, ISMINE_NO);
127 
128  // Keystore has redeemScript and key
129  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
130  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
132  }
133 
134  // (P2PKH inside) P2SH inside P2SH (invalid)
135  {
136  CWallet keystore(chain.get(), "", CreateDummyWalletDatabase());
137 
138  keystore.SetupLegacyScriptPubKeyMan();
140 
141  CScript redeemscript_inner =
142  GetScriptForDestination(PKHash(pubkeys[0]));
143  CScript redeemscript =
144  GetScriptForDestination(ScriptHash(redeemscript_inner));
145  scriptPubKey = GetScriptForDestination(ScriptHash(redeemscript));
146 
147  BOOST_CHECK(
148  keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemscript));
150  redeemscript_inner));
151  BOOST_CHECK(
152  keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
153  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
154  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
155  BOOST_CHECK_EQUAL(result, ISMINE_NO);
156  }
157 
158  // scriptPubKey multisig
159  {
160  CWallet keystore(chain.get(), "", CreateDummyWalletDatabase());
161 
162  keystore.SetupLegacyScriptPubKeyMan();
164 
165  scriptPubKey =
166  GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
167 
168  // Keystore does not have any keys
169  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
170  BOOST_CHECK_EQUAL(result, ISMINE_NO);
171 
172  // Keystore has 1/2 keys
173  BOOST_CHECK(
174  keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
175 
176  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
177  BOOST_CHECK_EQUAL(result, ISMINE_NO);
178 
179  // Keystore has 2/2 keys
180  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
181 
182  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
183  BOOST_CHECK_EQUAL(result, ISMINE_NO);
184 
185  // Keystore has 2/2 keys and the script
186  BOOST_CHECK(
187  keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
188 
189  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
190  BOOST_CHECK_EQUAL(result, ISMINE_NO);
191  }
192 
193  // P2SH multisig
194  {
195  CWallet keystore(chain.get(), "", CreateDummyWalletDatabase());
196 
197  keystore.SetupLegacyScriptPubKeyMan();
199  BOOST_CHECK(
200  keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
201  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
202 
203  CScript redeemScript =
204  GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
205  scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
206 
207  // Keystore has no redeemScript
208  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
209  BOOST_CHECK_EQUAL(result, ISMINE_NO);
210 
211  // Keystore has redeemScript
212  BOOST_CHECK(
213  keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemScript));
214  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
216  }
217 
218  // OP_RETURN
219  {
220  CWallet keystore(chain.get(), "", CreateDummyWalletDatabase());
221 
222  keystore.SetupLegacyScriptPubKeyMan();
224  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
225 
226  scriptPubKey.clear();
227  scriptPubKey << OP_RETURN << ToByteVector(pubkeys[0]);
228 
229  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
230  BOOST_CHECK_EQUAL(result, ISMINE_NO);
231  }
232 
233  // Nonstandard
234  {
235  CWallet keystore(chain.get(), "", CreateDummyWalletDatabase());
236 
237  keystore.SetupLegacyScriptPubKeyMan();
239  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
240 
241  scriptPubKey.clear();
242  scriptPubKey << OP_9 << OP_ADD << OP_11 << OP_EQUAL;
243 
244  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
245  BOOST_CHECK_EQUAL(result, ISMINE_NO);
246  }
247 }
248 
An encapsulated secp256k1 private key.
Definition: key.h:28
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:183
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:210
An encapsulated public key.
Definition: pubkey.h:31
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
void clear()
Definition: script.h:553
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:254
void SetupLegacyScriptPubKeyMan()
Make a LegacyScriptPubKeyMan and set it for all types, internal, and external.
Definition: wallet.cpp:3290
LegacyScriptPubKeyMan * GetLegacyScriptPubKeyMan() const
Get the LegacyScriptPubKeyMan which is used for all types, internal, and external.
Definition: wallet.cpp:3271
virtual bool AddKey(const CKey &key)
RecursiveMutex cs_KeyStore
isminetype IsMine(const CScript &script) const override
bool AddCScript(const CScript &redeemScript) override
isminetype
IsMine() return codes.
Definition: ismine.h:18
@ ISMINE_SPENDABLE
Definition: ismine.h:21
@ ISMINE_NO
Definition: ismine.h:19
BOOST_AUTO_TEST_CASE(ismine_standard)
NodeContext & m_node
Definition: interfaces.cpp:785
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::vector< uint8_t > ToByteVector(const T &in)
Definition: script.h:42
@ OP_EQUAL
Definition: script.h:119
@ OP_ADD
Definition: script.h:134
@ OP_9
Definition: script.h:65
@ OP_11
Definition: script.h:67
@ OP_RETURN
Definition: script.h:84
BOOST_FIXTURE_TEST_SUITE(stakingrewards_tests, StakingRewardsActivationTestingSetup) BOOST_AUTO_TEST_CASE(isstakingrewardsactivated)
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:249
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:244
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:240
#define LOCK(cs)
Definition: sync.h:306
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1170