Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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>
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
18BOOST_FIXTURE_TEST_SUITE(ismine_tests, BasicTestingSetup)
19
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
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();
40 LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
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();
57 LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
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();
76 LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
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();
94 LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
96
97 // Keystore does not have key
98 result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
100
101 // Keystore has key
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();
113 LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
114
117
118 // Keystore does not have redeemScript or key
119 result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
121
122 // Keystore has redeemScript but no key
124 keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemScript));
125 result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
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();
139 LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
140
142 GetScriptForDestination(PKHash(pubkeys[0]));
143 CScript redeemscript =
145 scriptPubKey = GetScriptForDestination(ScriptHash(redeemscript));
146
148 keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemscript));
149 BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddCScript(
152 keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
153 BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
154 result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
156 }
157
158 // scriptPubKey multisig
159 {
160 CWallet keystore(chain.get(), "", CreateDummyWalletDatabase());
161
162 keystore.SetupLegacyScriptPubKeyMan();
163 LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
164
165 scriptPubKey =
167
168 // Keystore does not have any keys
169 result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
171
172 // Keystore has 1/2 keys
174 keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
175
176 result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
178
179 // Keystore has 2/2 keys
180 BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
181
182 result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
184
185 // Keystore has 2/2 keys and the script
187 keystore.GetLegacyScriptPubKeyMan()->AddCScript(scriptPubKey));
188
189 result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
191 }
192
193 // P2SH multisig
194 {
195 CWallet keystore(chain.get(), "", CreateDummyWalletDatabase());
196
197 keystore.SetupLegacyScriptPubKeyMan();
198 LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
200 keystore.GetLegacyScriptPubKeyMan()->AddKey(uncompressedKey));
201 BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[1]));
202
206
207 // Keystore has no redeemScript
208 result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
210
211 // Keystore has redeemScript
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();
223 LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
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);
231 }
232
233 // Nonstandard
234 {
235 CWallet keystore(chain.get(), "", CreateDummyWalletDatabase());
236
237 keystore.SetupLegacyScriptPubKeyMan();
238 LOCK(keystore.GetLegacyScriptPubKeyMan()->cs_KeyStore);
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);
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
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
#define BOOST_CHECK_EQUAL(v1, v2)
Definition object.cpp:18
#define BOOST_CHECK(expr)
Definition object.cpp:17
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
@ 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
std::vector< uint8_t > ToByteVector(const T &in)
Definition script.h:42
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.