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