2015-12-13 17:58:29 +01:00
|
|
|
// Copyright (c) 2011-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-03-18 10:11:00 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "base58.h"
|
|
|
|
|
2013-09-10 21:18:09 +02:00
|
|
|
#include "data/base58_encode_decode.json.h"
|
|
|
|
#include "data/base58_keys_invalid.json.h"
|
|
|
|
#include "data/base58_keys_valid.json.h"
|
2011-10-01 02:47:47 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "key.h"
|
2014-08-20 17:37:40 +02:00
|
|
|
#include "script/script.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "uint256.h"
|
2012-09-29 11:13:32 +02:00
|
|
|
#include "util.h"
|
2014-09-25 05:32:36 +02:00
|
|
|
#include "utilstrencodings.h"
|
2015-03-12 09:34:42 +01:00
|
|
|
#include "test/test_bitcoin.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2015-09-04 16:11:34 +02:00
|
|
|
#include <univalue.h>
|
2015-05-18 14:02:18 +02:00
|
|
|
|
2015-05-13 21:29:19 +02:00
|
|
|
extern UniValue read_json(const std::string& jsondata);
|
2011-10-01 02:47:47 +02:00
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup)
|
2011-10-01 02:47:47 +02:00
|
|
|
|
2012-09-29 11:13:32 +02:00
|
|
|
// Goal: test low-level base58 encoding functionality
|
|
|
|
BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
|
|
|
|
{
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode)));
|
2014-08-20 21:15:16 +02:00
|
|
|
for (unsigned int idx = 0; idx < tests.size(); idx++) {
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue test = tests[idx];
|
2014-08-20 21:15:16 +02:00
|
|
|
std::string strTest = test.write();
|
2012-09-29 11:13:32 +02:00
|
|
|
if (test.size() < 2) // Allow for extra stuff (useful for comments)
|
|
|
|
{
|
|
|
|
BOOST_ERROR("Bad test: " << strTest);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());
|
|
|
|
std::string base58string = test[1].get_str();
|
|
|
|
BOOST_CHECK_MESSAGE(
|
2014-09-23 19:30:27 +02:00
|
|
|
EncodeBase58(begin_ptr(sourcedata), end_ptr(sourcedata)) == base58string,
|
2012-09-29 11:13:32 +02:00
|
|
|
strTest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Goal: test low-level base58 decoding functionality
|
|
|
|
BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
|
|
|
|
{
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode)));
|
2012-09-29 11:13:32 +02:00
|
|
|
std::vector<unsigned char> result;
|
|
|
|
|
2014-08-20 21:15:16 +02:00
|
|
|
for (unsigned int idx = 0; idx < tests.size(); idx++) {
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue test = tests[idx];
|
2014-08-20 21:15:16 +02:00
|
|
|
std::string strTest = test.write();
|
2012-09-29 11:13:32 +02:00
|
|
|
if (test.size() < 2) // Allow for extra stuff (useful for comments)
|
|
|
|
{
|
|
|
|
BOOST_ERROR("Bad test: " << strTest);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::vector<unsigned char> expected = ParseHex(test[0].get_str());
|
|
|
|
std::string base58string = test[1].get_str();
|
|
|
|
BOOST_CHECK_MESSAGE(DecodeBase58(base58string, result), strTest);
|
|
|
|
BOOST_CHECK_MESSAGE(result.size() == expected.size() && std::equal(result.begin(), result.end(), expected.begin()), strTest);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_CHECK(!DecodeBase58("invalid", result));
|
2012-09-19 09:31:15 +02:00
|
|
|
|
|
|
|
// check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
|
|
|
|
BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result));
|
|
|
|
BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result));
|
|
|
|
std::vector<unsigned char> expected = ParseHex("971a55");
|
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
2012-09-29 11:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Visitor to check address type
|
|
|
|
class TestAddrTypeVisitor : public boost::static_visitor<bool>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::string exp_addrType;
|
|
|
|
public:
|
|
|
|
TestAddrTypeVisitor(const std::string &exp_addrType) : exp_addrType(exp_addrType) { }
|
|
|
|
bool operator()(const CKeyID &id) const
|
|
|
|
{
|
|
|
|
return (exp_addrType == "pubkey");
|
|
|
|
}
|
|
|
|
bool operator()(const CScriptID &id) const
|
|
|
|
{
|
|
|
|
return (exp_addrType == "script");
|
|
|
|
}
|
|
|
|
bool operator()(const CNoDestination &no) const
|
|
|
|
{
|
|
|
|
return (exp_addrType == "none");
|
|
|
|
}
|
2011-10-01 02:47:47 +02:00
|
|
|
};
|
|
|
|
|
2012-09-29 11:13:32 +02:00
|
|
|
// Visitor to check address payload
|
|
|
|
class TestPayloadVisitor : public boost::static_visitor<bool>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::vector<unsigned char> exp_payload;
|
|
|
|
public:
|
|
|
|
TestPayloadVisitor(std::vector<unsigned char> &exp_payload) : exp_payload(exp_payload) { }
|
|
|
|
bool operator()(const CKeyID &id) const
|
|
|
|
{
|
|
|
|
uint160 exp_key(exp_payload);
|
|
|
|
return exp_key == id;
|
|
|
|
}
|
|
|
|
bool operator()(const CScriptID &id) const
|
|
|
|
{
|
|
|
|
uint160 exp_key(exp_payload);
|
|
|
|
return exp_key == id;
|
|
|
|
}
|
|
|
|
bool operator()(const CNoDestination &no) const
|
|
|
|
{
|
|
|
|
return exp_payload.size() == 0;
|
|
|
|
}
|
2011-10-01 02:47:47 +02:00
|
|
|
};
|
|
|
|
|
2012-09-29 11:13:32 +02:00
|
|
|
// Goal: check that parsed keys match test payload
|
|
|
|
BOOST_AUTO_TEST_CASE(base58_keys_valid_parse)
|
2011-10-01 02:47:47 +02:00
|
|
|
{
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue tests = read_json(std::string(json_tests::base58_keys_valid, json_tests::base58_keys_valid + sizeof(json_tests::base58_keys_valid)));
|
2012-09-29 11:13:32 +02:00
|
|
|
std::vector<unsigned char> result;
|
|
|
|
CBitcoinSecret secret;
|
|
|
|
CBitcoinAddress addr;
|
2015-03-09 16:04:43 +01:00
|
|
|
SelectParams(CBaseChainParams::MAIN);
|
2012-09-29 11:13:32 +02:00
|
|
|
|
2014-08-20 21:15:16 +02:00
|
|
|
for (unsigned int idx = 0; idx < tests.size(); idx++) {
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue test = tests[idx];
|
2014-08-20 21:15:16 +02:00
|
|
|
std::string strTest = test.write();
|
2012-09-29 11:13:32 +02:00
|
|
|
if (test.size() < 3) // Allow for extra stuff (useful for comments)
|
|
|
|
{
|
|
|
|
BOOST_ERROR("Bad test: " << strTest);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::string exp_base58string = test[0].get_str();
|
|
|
|
std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str());
|
2015-05-13 21:29:19 +02:00
|
|
|
const UniValue &metadata = test[2].get_obj();
|
2012-09-29 11:13:32 +02:00
|
|
|
bool isPrivkey = find_value(metadata, "isPrivkey").get_bool();
|
|
|
|
bool isTestnet = find_value(metadata, "isTestnet").get_bool();
|
2013-05-07 15:16:25 +02:00
|
|
|
if (isTestnet)
|
2014-06-19 15:10:04 +02:00
|
|
|
SelectParams(CBaseChainParams::TESTNET);
|
2013-05-07 15:16:25 +02:00
|
|
|
else
|
2014-06-19 15:10:04 +02:00
|
|
|
SelectParams(CBaseChainParams::MAIN);
|
2012-09-29 11:13:32 +02:00
|
|
|
if(isPrivkey)
|
|
|
|
{
|
|
|
|
bool isCompressed = find_value(metadata, "isCompressed").get_bool();
|
|
|
|
// Must be valid private key
|
|
|
|
// Note: CBitcoinSecret::SetString tests isValid, whereas CBitcoinAddress does not!
|
|
|
|
BOOST_CHECK_MESSAGE(secret.SetString(exp_base58string), "!SetString:"+ strTest);
|
|
|
|
BOOST_CHECK_MESSAGE(secret.IsValid(), "!IsValid:" + strTest);
|
2013-05-01 06:52:05 +02:00
|
|
|
CKey privkey = secret.GetKey();
|
|
|
|
BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + strTest);
|
2012-09-29 11:13:32 +02:00
|
|
|
BOOST_CHECK_MESSAGE(privkey.size() == exp_payload.size() && std::equal(privkey.begin(), privkey.end(), exp_payload.begin()), "key mismatch:" + strTest);
|
|
|
|
|
|
|
|
// Private key must be invalid public key
|
|
|
|
addr.SetString(exp_base58string);
|
|
|
|
BOOST_CHECK_MESSAGE(!addr.IsValid(), "IsValid privkey as pubkey:" + strTest);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string exp_addrType = find_value(metadata, "addrType").get_str(); // "script" or "pubkey"
|
|
|
|
// Must be valid public key
|
|
|
|
BOOST_CHECK_MESSAGE(addr.SetString(exp_base58string), "SetString:" + strTest);
|
|
|
|
BOOST_CHECK_MESSAGE(addr.IsValid(), "!IsValid:" + strTest);
|
|
|
|
BOOST_CHECK_MESSAGE(addr.IsScript() == (exp_addrType == "script"), "isScript mismatch" + strTest);
|
|
|
|
CTxDestination dest = addr.Get();
|
|
|
|
BOOST_CHECK_MESSAGE(boost::apply_visitor(TestAddrTypeVisitor(exp_addrType), dest), "addrType mismatch" + strTest);
|
|
|
|
|
|
|
|
// Public key must be invalid private key
|
|
|
|
secret.SetString(exp_base58string);
|
|
|
|
BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid pubkey as privkey:" + strTest);
|
|
|
|
}
|
2011-10-01 02:47:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-29 11:13:32 +02:00
|
|
|
// Goal: check that generated keys match test vectors
|
|
|
|
BOOST_AUTO_TEST_CASE(base58_keys_valid_gen)
|
2011-10-01 02:47:47 +02:00
|
|
|
{
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue tests = read_json(std::string(json_tests::base58_keys_valid, json_tests::base58_keys_valid + sizeof(json_tests::base58_keys_valid)));
|
2011-10-01 02:47:47 +02:00
|
|
|
std::vector<unsigned char> result;
|
2014-08-20 21:15:16 +02:00
|
|
|
|
|
|
|
for (unsigned int idx = 0; idx < tests.size(); idx++) {
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue test = tests[idx];
|
2014-08-20 21:15:16 +02:00
|
|
|
std::string strTest = test.write();
|
2012-09-29 11:13:32 +02:00
|
|
|
if (test.size() < 3) // Allow for extra stuff (useful for comments)
|
|
|
|
{
|
|
|
|
BOOST_ERROR("Bad test: " << strTest);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::string exp_base58string = test[0].get_str();
|
|
|
|
std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str());
|
2015-05-13 21:29:19 +02:00
|
|
|
const UniValue &metadata = test[2].get_obj();
|
2012-09-29 11:13:32 +02:00
|
|
|
bool isPrivkey = find_value(metadata, "isPrivkey").get_bool();
|
|
|
|
bool isTestnet = find_value(metadata, "isTestnet").get_bool();
|
2013-05-07 15:16:25 +02:00
|
|
|
if (isTestnet)
|
2014-06-19 15:10:04 +02:00
|
|
|
SelectParams(CBaseChainParams::TESTNET);
|
2013-05-07 15:16:25 +02:00
|
|
|
else
|
2014-06-19 15:10:04 +02:00
|
|
|
SelectParams(CBaseChainParams::MAIN);
|
2012-09-29 11:13:32 +02:00
|
|
|
if(isPrivkey)
|
|
|
|
{
|
|
|
|
bool isCompressed = find_value(metadata, "isCompressed").get_bool();
|
2013-05-01 06:52:05 +02:00
|
|
|
CKey key;
|
|
|
|
key.Set(exp_payload.begin(), exp_payload.end(), isCompressed);
|
|
|
|
assert(key.IsValid());
|
2012-09-29 11:13:32 +02:00
|
|
|
CBitcoinSecret secret;
|
2013-05-01 06:52:05 +02:00
|
|
|
secret.SetKey(key);
|
2012-09-29 11:13:32 +02:00
|
|
|
BOOST_CHECK_MESSAGE(secret.ToString() == exp_base58string, "result mismatch: " + strTest);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string exp_addrType = find_value(metadata, "addrType").get_str();
|
|
|
|
CTxDestination dest;
|
|
|
|
if(exp_addrType == "pubkey")
|
|
|
|
{
|
|
|
|
dest = CKeyID(uint160(exp_payload));
|
|
|
|
}
|
|
|
|
else if(exp_addrType == "script")
|
|
|
|
{
|
|
|
|
dest = CScriptID(uint160(exp_payload));
|
|
|
|
}
|
|
|
|
else if(exp_addrType == "none")
|
|
|
|
{
|
|
|
|
dest = CNoDestination();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BOOST_ERROR("Bad addrtype: " << strTest);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
CBitcoinAddress addrOut;
|
2014-05-09 23:42:20 +02:00
|
|
|
BOOST_CHECK_MESSAGE(addrOut.Set(dest), "encode dest: " + strTest);
|
2012-09-29 11:13:32 +02:00
|
|
|
BOOST_CHECK_MESSAGE(addrOut.ToString() == exp_base58string, "mismatch: " + strTest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visiting a CNoDestination must fail
|
|
|
|
CBitcoinAddress dummyAddr;
|
|
|
|
CTxDestination nodest = CNoDestination();
|
2014-05-09 23:42:20 +02:00
|
|
|
BOOST_CHECK(!dummyAddr.Set(nodest));
|
2012-09-29 11:13:32 +02:00
|
|
|
|
2015-03-09 16:04:43 +01:00
|
|
|
SelectParams(CBaseChainParams::MAIN);
|
2012-09-29 11:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Goal: check that base58 parsing code is robust against a variety of corrupted data
|
|
|
|
BOOST_AUTO_TEST_CASE(base58_keys_invalid)
|
|
|
|
{
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue tests = read_json(std::string(json_tests::base58_keys_invalid, json_tests::base58_keys_invalid + sizeof(json_tests::base58_keys_invalid))); // Negative testcases
|
2012-09-29 11:13:32 +02:00
|
|
|
std::vector<unsigned char> result;
|
|
|
|
CBitcoinSecret secret;
|
|
|
|
CBitcoinAddress addr;
|
|
|
|
|
2014-08-20 21:15:16 +02:00
|
|
|
for (unsigned int idx = 0; idx < tests.size(); idx++) {
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue test = tests[idx];
|
2014-08-20 21:15:16 +02:00
|
|
|
std::string strTest = test.write();
|
2012-09-29 11:13:32 +02:00
|
|
|
if (test.size() < 1) // Allow for extra stuff (useful for comments)
|
|
|
|
{
|
|
|
|
BOOST_ERROR("Bad test: " << strTest);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::string exp_base58string = test[0].get_str();
|
|
|
|
|
|
|
|
// must be invalid as public and as private key
|
|
|
|
addr.SetString(exp_base58string);
|
|
|
|
BOOST_CHECK_MESSAGE(!addr.IsValid(), "IsValid pubkey:" + strTest);
|
|
|
|
secret.SetString(exp_base58string);
|
|
|
|
BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid privkey:" + strTest);
|
2011-10-01 02:47:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-29 11:13:32 +02:00
|
|
|
|
2011-10-01 02:47:47 +02:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|