2014-03-18 02:11:00 -07:00
|
|
|
// Copyright (c) 2011-2014 The Bitcoin Core developers
|
2014-12-12 20:09:33 -08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2019-07-18 07:16:09 -07:00
|
|
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
2014-03-18 02:11:00 -07:00
|
|
|
|
2013-04-12 22:13:08 -07:00
|
|
|
#include "base58.h"
|
|
|
|
|
2013-09-10 12:18:09 -07:00
|
|
|
#include "data/base58_encode_decode.json.h"
|
|
|
|
#include "data/base58_keys_invalid.json.h"
|
|
|
|
#include "data/base58_keys_valid.json.h"
|
2011-09-30 17:47:47 -07:00
|
|
|
|
2013-04-12 22:13:08 -07:00
|
|
|
#include "key.h"
|
2017-09-19 18:12:25 -07:00
|
|
|
#include "key_io.h"
|
2014-08-20 08:37:40 -07:00
|
|
|
#include "script/script.h"
|
2017-09-19 15:30:11 -07:00
|
|
|
#include "test/test_bitcoin.h"
|
2013-04-12 22:13:08 -07:00
|
|
|
#include "uint256.h"
|
2012-09-29 02:13:32 -07:00
|
|
|
#include "util.h"
|
2014-09-24 20:32:36 -07:00
|
|
|
#include "utilstrencodings.h"
|
2017-09-19 15:30:11 -07:00
|
|
|
|
|
|
|
#include <univalue.h>
|
2013-04-12 22:13:08 -07:00
|
|
|
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2015-05-18 05:02:18 -07:00
|
|
|
|
2015-05-13 12:29:19 -07:00
|
|
|
extern UniValue read_json(const std::string& jsondata);
|
2011-09-30 17:47:47 -07:00
|
|
|
|
2015-03-12 01:34:42 -07:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup)
|
2011-09-30 17:47:47 -07:00
|
|
|
|
2012-09-29 02:13:32 -07:00
|
|
|
// Goal: test low-level base58 encoding functionality
|
|
|
|
BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
|
|
|
|
{
|
2015-05-13 12:29:19 -07:00
|
|
|
UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode)));
|
2017-02-06 16:43:10 -08:00
|
|
|
for (size_t idx = 0; idx < tests.size(); idx++) {
|
2015-05-13 12:29:19 -07:00
|
|
|
UniValue test = tests[idx];
|
2014-08-20 12:15:16 -07:00
|
|
|
std::string strTest = test.write();
|
2012-09-29 02:13:32 -07: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 10:30:27 -07:00
|
|
|
EncodeBase58(begin_ptr(sourcedata), end_ptr(sourcedata)) == base58string,
|
2012-09-29 02:13:32 -07:00
|
|
|
strTest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Goal: test low-level base58 decoding functionality
|
|
|
|
BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
|
|
|
|
{
|
2015-05-13 12:29:19 -07: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 02:13:32 -07:00
|
|
|
std::vector<unsigned char> result;
|
|
|
|
|
2017-02-06 16:43:10 -08:00
|
|
|
for (size_t idx = 0; idx < tests.size(); idx++) {
|
2015-05-13 12:29:19 -07:00
|
|
|
UniValue test = tests[idx];
|
2014-08-20 12:15:16 -07:00
|
|
|
std::string strTest = test.write();
|
2012-09-29 02:13:32 -07: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 00:31:15 -07: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 02:13:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Goal: check that parsed keys match test payload
|
|
|
|
BOOST_AUTO_TEST_CASE(base58_keys_valid_parse)
|
2011-09-30 17:47:47 -07:00
|
|
|
{
|
2015-05-13 12:29:19 -07:00
|
|
|
UniValue tests = read_json(std::string(json_tests::base58_keys_valid, json_tests::base58_keys_valid + sizeof(json_tests::base58_keys_valid)));
|
2017-09-19 16:49:52 -07:00
|
|
|
CKey privkey;
|
2017-08-22 18:02:33 -07:00
|
|
|
CTxDestination destination;
|
2015-03-09 08:04:43 -07:00
|
|
|
SelectParams(CBaseChainParams::MAIN);
|
2012-09-29 02:13:32 -07:00
|
|
|
|
2017-02-06 16:43:10 -08:00
|
|
|
for (size_t idx = 0; idx < tests.size(); idx++) {
|
2015-05-13 12:29:19 -07:00
|
|
|
UniValue test = tests[idx];
|
2014-08-20 12:15:16 -07:00
|
|
|
std::string strTest = test.write();
|
2017-09-19 15:30:11 -07:00
|
|
|
if (test.size() < 3) { // Allow for extra stuff (useful for comments)
|
2012-09-29 02:13:32 -07:00
|
|
|
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 12:29:19 -07:00
|
|
|
const UniValue &metadata = test[2].get_obj();
|
2012-09-29 02:13:32 -07:00
|
|
|
bool isPrivkey = find_value(metadata, "isPrivkey").get_bool();
|
2017-09-19 15:30:11 -07:00
|
|
|
bool isTestnet = find_value(metadata, "chain").get_str() == "testnet";
|
|
|
|
if (isTestnet) {
|
2014-06-19 06:10:04 -07:00
|
|
|
SelectParams(CBaseChainParams::TESTNET);
|
2017-09-19 15:30:11 -07:00
|
|
|
} else {
|
2014-06-19 06:10:04 -07:00
|
|
|
SelectParams(CBaseChainParams::MAIN);
|
2017-09-19 15:30:11 -07:00
|
|
|
}
|
|
|
|
if (isPrivkey) {
|
2012-09-29 02:13:32 -07:00
|
|
|
bool isCompressed = find_value(metadata, "isCompressed").get_bool();
|
|
|
|
// Must be valid private key
|
2017-09-19 16:49:52 -07:00
|
|
|
privkey = DecodeSecret(exp_base58string);
|
|
|
|
BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest);
|
2013-04-30 21:52:05 -07:00
|
|
|
BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + strTest);
|
2012-09-29 02:13:32 -07: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
|
2017-08-22 18:02:33 -07:00
|
|
|
destination = DecodeDestination(exp_base58string);
|
|
|
|
BOOST_CHECK_MESSAGE(!IsValidDestination(destination), "IsValid privkey as pubkey:" + strTest);
|
2017-09-19 15:30:11 -07:00
|
|
|
} else {
|
2012-09-29 02:13:32 -07:00
|
|
|
// Must be valid public key
|
2017-08-22 18:02:33 -07:00
|
|
|
destination = DecodeDestination(exp_base58string);
|
2017-09-19 15:30:11 -07:00
|
|
|
CScript script = GetScriptForDestination(destination);
|
2017-08-22 18:02:33 -07:00
|
|
|
BOOST_CHECK_MESSAGE(IsValidDestination(destination), "!IsValid:" + strTest);
|
2017-09-19 15:30:11 -07:00
|
|
|
BOOST_CHECK_EQUAL(HexStr(script), HexStr(exp_payload));
|
2012-09-29 02:13:32 -07:00
|
|
|
|
|
|
|
// Public key must be invalid private key
|
2017-09-19 16:49:52 -07:00
|
|
|
privkey = DecodeSecret(exp_base58string);
|
|
|
|
BOOST_CHECK_MESSAGE(!privkey.IsValid(), "IsValid pubkey as privkey:" + strTest);
|
2012-09-29 02:13:32 -07:00
|
|
|
}
|
2011-09-30 17:47:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-29 02:13:32 -07:00
|
|
|
// Goal: check that generated keys match test vectors
|
|
|
|
BOOST_AUTO_TEST_CASE(base58_keys_valid_gen)
|
2011-09-30 17:47:47 -07:00
|
|
|
{
|
2015-05-13 12:29:19 -07:00
|
|
|
UniValue tests = read_json(std::string(json_tests::base58_keys_valid, json_tests::base58_keys_valid + sizeof(json_tests::base58_keys_valid)));
|
2014-08-20 12:15:16 -07:00
|
|
|
|
2017-02-06 16:43:10 -08:00
|
|
|
for (size_t idx = 0; idx < tests.size(); idx++) {
|
2015-05-13 12:29:19 -07:00
|
|
|
UniValue test = tests[idx];
|
2014-08-20 12:15:16 -07:00
|
|
|
std::string strTest = test.write();
|
2012-09-29 02:13:32 -07: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 12:29:19 -07:00
|
|
|
const UniValue &metadata = test[2].get_obj();
|
2012-09-29 02:13:32 -07:00
|
|
|
bool isPrivkey = find_value(metadata, "isPrivkey").get_bool();
|
2017-09-19 15:30:11 -07:00
|
|
|
bool isTestnet = find_value(metadata, "chain").get_str() == "testnet";
|
|
|
|
if (isTestnet) {
|
2014-06-19 06:10:04 -07:00
|
|
|
SelectParams(CBaseChainParams::TESTNET);
|
2017-09-19 15:30:11 -07:00
|
|
|
} else {
|
2014-06-19 06:10:04 -07:00
|
|
|
SelectParams(CBaseChainParams::MAIN);
|
2017-09-19 15:30:11 -07:00
|
|
|
}
|
|
|
|
if (isPrivkey) {
|
2012-09-29 02:13:32 -07:00
|
|
|
bool isCompressed = find_value(metadata, "isCompressed").get_bool();
|
2013-04-30 21:52:05 -07:00
|
|
|
CKey key;
|
|
|
|
key.Set(exp_payload.begin(), exp_payload.end(), isCompressed);
|
|
|
|
assert(key.IsValid());
|
2017-09-19 16:49:52 -07:00
|
|
|
BOOST_CHECK_MESSAGE(EncodeSecret(key) == exp_base58string, "result mismatch: " + strTest);
|
2017-09-19 15:30:11 -07:00
|
|
|
} else {
|
2012-09-29 02:13:32 -07:00
|
|
|
CTxDestination dest;
|
2017-09-19 15:30:11 -07:00
|
|
|
CScript exp_script(exp_payload.begin(), exp_payload.end());
|
|
|
|
ExtractDestination(exp_script, dest);
|
2017-08-22 18:02:33 -07:00
|
|
|
std::string address = EncodeDestination(dest);
|
2017-09-19 15:30:11 -07:00
|
|
|
BOOST_CHECK_EQUAL(address, exp_base58string);
|
2012-09-29 02:13:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 08:04:43 -07:00
|
|
|
SelectParams(CBaseChainParams::MAIN);
|
2012-09-29 02:13:32 -07: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 12:29:19 -07: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
|
2017-09-19 16:49:52 -07:00
|
|
|
CKey privkey;
|
2017-08-22 18:02:33 -07:00
|
|
|
CTxDestination destination;
|
2012-09-29 02:13:32 -07:00
|
|
|
|
2017-02-06 16:43:10 -08:00
|
|
|
for (size_t idx = 0; idx < tests.size(); idx++) {
|
2015-05-13 12:29:19 -07:00
|
|
|
UniValue test = tests[idx];
|
2014-08-20 12:15:16 -07:00
|
|
|
std::string strTest = test.write();
|
2012-09-29 02:13:32 -07: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
|
2017-08-22 18:02:33 -07:00
|
|
|
destination = DecodeDestination(exp_base58string);
|
|
|
|
BOOST_CHECK_MESSAGE(!IsValidDestination(destination), "IsValid pubkey:" + strTest);
|
2017-09-19 16:49:52 -07:00
|
|
|
privkey = DecodeSecret(exp_base58string);
|
|
|
|
BOOST_CHECK_MESSAGE(!privkey.IsValid(), "IsValid privkey:" + strTest);
|
2011-09-30 17:47:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-29 02:13:32 -07:00
|
|
|
|
2011-09-30 17:47:47 -07:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|