Bitcoin ABC 0.30.7
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>
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
20BOOST_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());
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());
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
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
95 scriptPubKey = GetScriptForDestination(PKHash(uncompressedPubkey));
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
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);
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
140
141 CScript redeemscript_inner =
142 GetScriptForDestination(PKHash(pubkeys[0]));
143 CScript redeemscript =
144 GetScriptForDestination(ScriptHash(redeemscript_inner));
145 scriptPubKey = GetScriptForDestination(ScriptHash(redeemscript));
146
148 keystore.GetLegacyScriptPubKeyMan()->AddCScript(redeemscript));
150 redeemscript_inner));
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
164
165 scriptPubKey =
166 GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]});
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
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);
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
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
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
249BOOST_AUTO_TEST_SUITE_END()
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
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_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
@ 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.
Definition: walletdb.cpp:1170