10
10
11
11
#include " key.h"
12
12
#include " script/script.h"
13
+ #include " test/test_bitcoin.h"
13
14
#include " uint256.h"
14
15
#include " util.h"
15
16
#include " utilstrencodings.h"
16
- #include " test/test_bitcoin.h"
17
+
18
+ #include < univalue.h>
17
19
18
20
#include < boost/test/unit_test.hpp>
19
21
20
- #include < univalue.h>
21
22
22
23
extern UniValue read_json (const std::string& jsondata);
23
24
@@ -72,50 +73,6 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
72
73
BOOST_CHECK_EQUAL_COLLECTIONS (result.begin (), result.end (), expected.begin (), expected.end ());
73
74
}
74
75
75
- // Visitor to check address type
76
- class TestAddrTypeVisitor : public boost ::static_visitor<bool >
77
- {
78
- private:
79
- std::string exp_addrType;
80
- public:
81
- explicit TestAddrTypeVisitor (const std::string &_exp_addrType) : exp_addrType(_exp_addrType) { }
82
- bool operator ()(const CKeyID &id) const
83
- {
84
- return (exp_addrType == " pubkey" );
85
- }
86
- bool operator ()(const CScriptID &id) const
87
- {
88
- return (exp_addrType == " script" );
89
- }
90
- bool operator ()(const CNoDestination &no) const
91
- {
92
- return (exp_addrType == " none" );
93
- }
94
- };
95
-
96
- // Visitor to check address payload
97
- class TestPayloadVisitor : public boost ::static_visitor<bool >
98
- {
99
- private:
100
- std::vector<unsigned char > exp_payload;
101
- public:
102
- explicit TestPayloadVisitor (std::vector<unsigned char > &_exp_payload) : exp_payload(_exp_payload) { }
103
- bool operator ()(const CKeyID &id) const
104
- {
105
- uint160 exp_key (exp_payload);
106
- return exp_key == id;
107
- }
108
- bool operator ()(const CScriptID &id) const
109
- {
110
- uint160 exp_key (exp_payload);
111
- return exp_key == id;
112
- }
113
- bool operator ()(const CNoDestination &no) const
114
- {
115
- return exp_payload.size () == 0 ;
116
- }
117
- };
118
-
119
76
// Goal: check that parsed keys match test payload
120
77
BOOST_AUTO_TEST_CASE (base58_keys_valid_parse)
121
78
{
@@ -127,22 +84,21 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_parse)
127
84
for (unsigned int idx = 0 ; idx < tests.size (); idx++) {
128
85
UniValue test = tests[idx];
129
86
std::string strTest = test.write ();
130
- if (test.size () < 3 ) // Allow for extra stuff (useful for comments)
131
- {
87
+ if (test.size () < 3 ) { // Allow for extra stuff (useful for comments)
132
88
BOOST_ERROR (" Bad test: " << strTest);
133
89
continue ;
134
90
}
135
91
std::string exp_base58string = test[0 ].get_str ();
136
92
std::vector<unsigned char > exp_payload = ParseHex (test[1 ].get_str ());
137
93
const UniValue &metadata = test[2 ].get_obj ();
138
94
bool isPrivkey = find_value (metadata, " isPrivkey" ).get_bool ();
139
- bool isTestnet = find_value (metadata, " isTestnet " ).get_bool () ;
140
- if (isTestnet)
95
+ bool isTestnet = find_value (metadata, " chain " ).get_str () == " testnet " ;
96
+ if (isTestnet) {
141
97
SelectParams (CBaseChainParams::TESTNET);
142
- else
98
+ } else {
143
99
SelectParams (CBaseChainParams::MAIN);
144
- if (isPrivkey)
145
- {
100
+ }
101
+ if (isPrivkey) {
146
102
bool isCompressed = find_value (metadata, " isCompressed" ).get_bool ();
147
103
// Must be valid private key
148
104
BOOST_CHECK_MESSAGE (secret.SetString (exp_base58string), " !SetString:" + strTest);
@@ -154,15 +110,12 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_parse)
154
110
// Private key must be invalid public key
155
111
destination = DecodeDestination (exp_base58string);
156
112
BOOST_CHECK_MESSAGE (!IsValidDestination (destination), " IsValid privkey as pubkey:" + strTest);
157
- }
158
- else
159
- {
160
- std::string exp_addrType = find_value (metadata, " addrType" ).get_str (); // "script" or "pubkey"
113
+ } else {
161
114
// Must be valid public key
162
115
destination = DecodeDestination (exp_base58string);
116
+ CScript script = GetScriptForDestination (destination);
163
117
BOOST_CHECK_MESSAGE (IsValidDestination (destination), " !IsValid:" + strTest);
164
- BOOST_CHECK_MESSAGE ((boost::get<CScriptID>(&destination) != nullptr ) == (exp_addrType == " script" ), " isScript mismatch" + strTest);
165
- BOOST_CHECK_MESSAGE (boost::apply_visitor (TestAddrTypeVisitor (exp_addrType), destination), " addrType mismatch" + strTest);
118
+ BOOST_CHECK_EQUAL (HexStr (script), HexStr (exp_payload));
166
119
167
120
// Public key must be invalid private key
168
121
secret.SetString (exp_base58string);
@@ -188,44 +141,26 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_gen)
188
141
std::vector<unsigned char > exp_payload = ParseHex (test[1 ].get_str ());
189
142
const UniValue &metadata = test[2 ].get_obj ();
190
143
bool isPrivkey = find_value (metadata, " isPrivkey" ).get_bool ();
191
- bool isTestnet = find_value (metadata, " isTestnet " ).get_bool () ;
192
- if (isTestnet)
144
+ bool isTestnet = find_value (metadata, " chain " ).get_str () == " testnet " ;
145
+ if (isTestnet) {
193
146
SelectParams (CBaseChainParams::TESTNET);
194
- else
147
+ } else {
195
148
SelectParams (CBaseChainParams::MAIN);
196
- if (isPrivkey)
197
- {
149
+ }
150
+ if (isPrivkey) {
198
151
bool isCompressed = find_value (metadata, " isCompressed" ).get_bool ();
199
152
CKey key;
200
153
key.Set (exp_payload.begin (), exp_payload.end (), isCompressed);
201
154
assert (key.IsValid ());
202
155
CBitcoinSecret secret;
203
156
secret.SetKey (key);
204
157
BOOST_CHECK_MESSAGE (secret.ToString () == exp_base58string, " result mismatch: " + strTest);
205
- }
206
- else
207
- {
208
- std::string exp_addrType = find_value (metadata, " addrType" ).get_str ();
158
+ } else {
209
159
CTxDestination dest;
210
- if (exp_addrType == " pubkey" )
211
- {
212
- dest = CKeyID (uint160 (exp_payload));
213
- }
214
- else if (exp_addrType == " script" )
215
- {
216
- dest = CScriptID (uint160 (exp_payload));
217
- }
218
- else if (exp_addrType == " none" )
219
- {
220
- dest = CNoDestination ();
221
- }
222
- else
223
- {
224
- BOOST_ERROR (" Bad addrtype: " << strTest);
225
- continue ;
226
- }
160
+ CScript exp_script (exp_payload.begin (), exp_payload.end ());
161
+ ExtractDestination (exp_script, dest);
227
162
std::string address = EncodeDestination (dest);
228
- BOOST_CHECK_MESSAGE (address == exp_base58string, " mismatch: " + strTest );
163
+ BOOST_CHECK_EQUAL (address, exp_base58string );
229
164
}
230
165
}
231
166
0 commit comments