Bitcoin ABC  0.22.13
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  NodeContext node;
32  std::unique_ptr<interfaces::Chain> chain =
34 
35  CScript scriptPubKey;
36  isminetype result;
37 
38  // P2PK compressed
39  {
40  CWallet keystore(chain.get(), WalletLocation(),
42  keystore.SetupLegacyScriptPubKeyMan();
43  LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
44  scriptPubKey = GetScriptForRawPubKey(pubkeys[0]);
45 
46  // Keystore does not have key
47  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
49 
50  // Keystore has key
51  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
52  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
54  }
55 
56  // P2PK uncompressed
57  {
58  CWallet keystore(chain.get(), WalletLocation(),
60  keystore.SetupLegacyScriptPubKeyMan();
61  LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
62  scriptPubKey = GetScriptForRawPubKey(uncompressedPubkey);
63 
64  // Keystore does not have key
65  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
67 
68  // Keystore has key
70  keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
71  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
73  }
74 
75  // P2PKH compressed
76  {
77  CWallet keystore(chain.get(), WalletLocation(),
79  keystore.SetupLegacyScriptPubKeyMan();
80  LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
81  scriptPubKey = GetScriptForDestination(PKHash(pubkeys[0]));
82 
83  // Keystore does not have key
84  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
86 
87  // Keystore has key
88  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
89  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
91  }
92 
93  // P2PKH uncompressed
94  {
95  CWallet keystore(chain.get(), WalletLocation(),
97  keystore.SetupLegacyScriptPubKeyMan();
98  LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
99  scriptPubKey = GetScriptForDestination(PKHash(uncompressedPubkey));
100 
101  // Keystore does not have key
102  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
103  BOOST_CHECK_EQUAL(result, ISMINE_NO);
104 
105  // Keystore has key
106  BOOST_CHECK(
107  keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
108  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
110  }
111 
112  // P2SH
113  {
114  CWallet keystore(chain.get(), WalletLocation(),
116  keystore.SetupLegacyScriptPubKeyMan();
117  LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
118 
119  CScript redeemScript = GetScriptForDestination(PKHash(pubkeys[0]));
120  scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
121 
122  // Keystore does not have redeemScript or key
123  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
124  BOOST_CHECK_EQUAL(result, ISMINE_NO);
125 
126  // Keystore has redeemScript but no key
127  BOOST_CHECK(
128  keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemScript));
129  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
130  BOOST_CHECK_EQUAL(result, ISMINE_NO);
131 
132  // Keystore has redeemScript and key
133  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
134  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
136  }
137 
138  // (P2PKH inside) P2SH inside P2SH (invalid)
139  {
140  CWallet keystore(chain.get(), WalletLocation(),
142  keystore.SetupLegacyScriptPubKeyMan();
143  LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
144 
145  CScript redeemscript_inner =
146  GetScriptForDestination(PKHash(pubkeys[0]));
147  CScript redeemscript =
148  GetScriptForDestination(ScriptHash(redeemscript_inner));
149  scriptPubKey = GetScriptForDestination(ScriptHash(redeemscript));
150 
151  BOOST_CHECK(
152  keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemscript));
153  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(
154  redeemscript_inner));
155  BOOST_CHECK(
156  keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
157  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
158  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
159  BOOST_CHECK_EQUAL(result, ISMINE_NO);
160  }
161 
162  // scriptPubKey multisig
163  {
164  CWallet keystore(chain.get(), WalletLocation(),
166  keystore.SetupLegacyScriptPubKeyMan();
167  LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
168 
169  scriptPubKey =
170  GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
171 
172  // Keystore does not have any keys
173  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
174  BOOST_CHECK_EQUAL(result, ISMINE_NO);
175 
176  // Keystore has 1/2 keys
177  BOOST_CHECK(
178  keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
179 
180  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
181  BOOST_CHECK_EQUAL(result, ISMINE_NO);
182 
183  // Keystore has 2/2 keys
184  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
185 
186  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
187  BOOST_CHECK_EQUAL(result, ISMINE_NO);
188 
189  // Keystore has 2/2 keys and the script
190  BOOST_CHECK(
191  keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
192 
193  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
194  BOOST_CHECK_EQUAL(result, ISMINE_NO);
195  }
196 
197  // P2SH multisig
198  {
199  CWallet keystore(chain.get(), WalletLocation(),
201  keystore.SetupLegacyScriptPubKeyMan();
202  LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
203  BOOST_CHECK(
204  keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
205  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
206 
207  CScript redeemScript =
208  GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
209  scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
210 
211  // Keystore has no redeemScript
212  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
213  BOOST_CHECK_EQUAL(result, ISMINE_NO);
214 
215  // Keystore has redeemScript
216  BOOST_CHECK(
217  keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemScript));
218  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
220  }
221 
222  // OP_RETURN
223  {
224  CWallet keystore(chain.get(), WalletLocation(),
226  keystore.SetupLegacyScriptPubKeyMan();
227  LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
228  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
229 
230  scriptPubKey.clear();
231  scriptPubKey << OP_RETURN << ToByteVector(pubkeys[0]);
232 
233  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
234  BOOST_CHECK_EQUAL(result, ISMINE_NO);
235  }
236 
237  // Nonstandard
238  {
239  CWallet keystore(chain.get(), WalletLocation(),
241  keystore.SetupLegacyScriptPubKeyMan();
242  LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
243  BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
244 
245  scriptPubKey.clear();
246  scriptPubKey << OP_9 << OP_ADD << OP_11 << OP_EQUAL;
247 
248  result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
249  BOOST_CHECK_EQUAL(result, ISMINE_NO);
250  }
251 }
252 
static std::unique_ptr< BerkeleyDatabase > CreateDummy()
Return object for accessing dummy database with no read/write capabilities.
Definition: bdb.h:135
void SetupLegacyScriptPubKeyMan()
Make a LegacyScriptPubKeyMan and set it for all types, internal, and external.
Definition: wallet.cpp:4848
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:210
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:253
NodeContext struct containing references to chain state and connection state.
Definition: context.h:35
BOOST_AUTO_TEST_CASE(ismine_standard)
#define LOCK(cs)
Definition: sync.h:230
An encapsulated public key.
Definition: pubkey.h:31
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:183
isminetype
IsMine() return codes.
Definition: ismine.h:18
std::vector< uint8_t > ToByteVector(const T &in)
Definition: script.h:41
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:246
Definition: script.h:64
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
const CChainParams & Params()
Return the currently selected parameters.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:679
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:258
The WalletLocation class provides wallet information.
Definition: walletutil.h:80
An encapsulated secp256k1 private key.
Definition: key.h:25
Definition: script.h:66
void clear()
Definition: script.h:558
Definition: script.h:133
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::unique_ptr< Chain > MakeChain(NodeContext &node, const CChainParams &params)
Return implementation of Chain interface.
Definition: chain.cpp:448