Skip to content

Commit 6565c55

Browse files
committed
Convert base58_tests from type/payload to scriptPubKey comparison
1 parent 8fd2267 commit 6565c55

File tree

2 files changed

+294
-385
lines changed

2 files changed

+294
-385
lines changed

src/test/base58_tests.cpp

Lines changed: 21 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010

1111
#include "key.h"
1212
#include "script/script.h"
13+
#include "test/test_bitcoin.h"
1314
#include "uint256.h"
1415
#include "util.h"
1516
#include "utilstrencodings.h"
16-
#include "test/test_bitcoin.h"
17+
18+
#include <univalue.h>
1719

1820
#include <boost/test/unit_test.hpp>
1921

20-
#include <univalue.h>
2122

2223
extern UniValue read_json(const std::string& jsondata);
2324

@@ -72,50 +73,6 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
7273
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
7374
}
7475

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-
11976
// Goal: check that parsed keys match test payload
12077
BOOST_AUTO_TEST_CASE(base58_keys_valid_parse)
12178
{
@@ -127,22 +84,21 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_parse)
12784
for (unsigned int idx = 0; idx < tests.size(); idx++) {
12885
UniValue test = tests[idx];
12986
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)
13288
BOOST_ERROR("Bad test: " << strTest);
13389
continue;
13490
}
13591
std::string exp_base58string = test[0].get_str();
13692
std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str());
13793
const UniValue &metadata = test[2].get_obj();
13894
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) {
14197
SelectParams(CBaseChainParams::TESTNET);
142-
else
98+
} else {
14399
SelectParams(CBaseChainParams::MAIN);
144-
if(isPrivkey)
145-
{
100+
}
101+
if (isPrivkey) {
146102
bool isCompressed = find_value(metadata, "isCompressed").get_bool();
147103
// Must be valid private key
148104
BOOST_CHECK_MESSAGE(secret.SetString(exp_base58string), "!SetString:"+ strTest);
@@ -154,15 +110,12 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_parse)
154110
// Private key must be invalid public key
155111
destination = DecodeDestination(exp_base58string);
156112
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 {
161114
// Must be valid public key
162115
destination = DecodeDestination(exp_base58string);
116+
CScript script = GetScriptForDestination(destination);
163117
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));
166119

167120
// Public key must be invalid private key
168121
secret.SetString(exp_base58string);
@@ -188,44 +141,26 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_gen)
188141
std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str());
189142
const UniValue &metadata = test[2].get_obj();
190143
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) {
193146
SelectParams(CBaseChainParams::TESTNET);
194-
else
147+
} else {
195148
SelectParams(CBaseChainParams::MAIN);
196-
if(isPrivkey)
197-
{
149+
}
150+
if (isPrivkey) {
198151
bool isCompressed = find_value(metadata, "isCompressed").get_bool();
199152
CKey key;
200153
key.Set(exp_payload.begin(), exp_payload.end(), isCompressed);
201154
assert(key.IsValid());
202155
CBitcoinSecret secret;
203156
secret.SetKey(key);
204157
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 {
209159
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);
227162
std::string address = EncodeDestination(dest);
228-
BOOST_CHECK_MESSAGE(address == exp_base58string, "mismatch: " + strTest);
163+
BOOST_CHECK_EQUAL(address, exp_base58string);
229164
}
230165
}
231166

0 commit comments

Comments
 (0)