2015-12-13 14:51:43 +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.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <test/data/base58_encode_decode.json.h>
|
|
|
|
|
Merge #11372: Address encoding cleanup
92f1f8b31 Split off key_io_tests from base58_tests (Pieter Wuille)
119b0f85e Split key_io (address/key encodings) off from base58 (Pieter Wuille)
ebfe217b1 Stop using CBase58Data for ext keys (Pieter Wuille)
32e69fa0d Replace CBitcoinSecret with {Encode,Decode}Secret (Pieter Wuille)
Pull request description:
This PR contains some of the changes left as TODO in #11167 (and built on top of that PR). They are not intended for backporting.
This removes the `CBase58`, `CBitcoinSecret`, `CBitcoinExtKey`, and `CBitcoinExtPubKey` classes, in favor of simple `Encode`/`Decode` functions. Furthermore, all Bitcoin-specific logic (addresses, WIF, BIP32) is moved to `key_io.{h,cpp}`, leaving `base58.{h,cpp}` as a pure utility that implements the base58 encoding/decoding logic.
Tree-SHA512: a5962c0ed27ad53cbe00f22af432cf11aa530e3efc9798e25c004bc9ed1b5673db5df3956e398ee2c085e3a136ac8da69fe7a7d97a05fb2eb3be0b60d0479655
Make linter happy
Dashify
2018-03-07 00:04:56 +01:00
|
|
|
#include <base58.h>
|
2022-02-25 18:19:25 +01:00
|
|
|
#include <test/util/setup_common.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/strencodings.h>
|
2019-12-12 10:55:37 +01:00
|
|
|
#include <util/vector.h>
|
2020-12-25 05:33:37 +01:00
|
|
|
|
|
|
|
#include <univalue.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
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(
|
2020-08-19 13:39:33 +02:00
|
|
|
EncodeBase58(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();
|
2019-12-12 10:55:37 +01:00
|
|
|
BOOST_CHECK_MESSAGE(DecodeBase58(base58string, result, 256), strTest);
|
2012-09-29 11:13:32 +02:00
|
|
|
BOOST_CHECK_MESSAGE(result.size() == expected.size() && std::equal(result.begin(), result.end(), expected.begin()), strTest);
|
|
|
|
}
|
|
|
|
|
2019-12-12 10:55:37 +01:00
|
|
|
BOOST_CHECK(!DecodeBase58("invalid", result, 100));
|
|
|
|
BOOST_CHECK(!DecodeBase58(std::string("invalid"), result, 100));
|
|
|
|
BOOST_CHECK(!DecodeBase58(std::string("\0invalid", 8), result, 100));
|
2019-12-11 12:00:52 +01:00
|
|
|
|
2019-12-12 10:55:37 +01:00
|
|
|
BOOST_CHECK(DecodeBase58(std::string("good", 4), result, 100));
|
|
|
|
BOOST_CHECK(!DecodeBase58(std::string("bad0IOl", 7), result, 100));
|
|
|
|
BOOST_CHECK(!DecodeBase58(std::string("goodbad0IOl", 11), result, 100));
|
|
|
|
BOOST_CHECK(!DecodeBase58(std::string("good\0bad0IOl", 12), result, 100));
|
2012-09-19 09:31:15 +02:00
|
|
|
|
|
|
|
// check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
|
2019-12-12 10:55:37 +01:00
|
|
|
BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result, 3));
|
|
|
|
BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result, 3));
|
2012-09-19 09:31:15 +02:00
|
|
|
std::vector<unsigned char> expected = ParseHex("971a55");
|
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
2019-12-11 12:00:52 +01:00
|
|
|
|
2019-12-12 10:55:37 +01:00
|
|
|
BOOST_CHECK(DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh", 21), result, 100));
|
|
|
|
BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oi", 21), result, 100));
|
|
|
|
BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh0IOl", 25), result, 100));
|
|
|
|
BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh\00IOl", 26), result, 100));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(base58_random_encode_decode)
|
|
|
|
{
|
|
|
|
for (int n = 0; n < 1000; ++n) {
|
|
|
|
unsigned int len = 1 + InsecureRandBits(8);
|
|
|
|
unsigned int zeroes = InsecureRandBool() ? InsecureRandRange(len + 1) : 0;
|
|
|
|
auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), g_insecure_rand_ctx.randbytes(len - zeroes));
|
|
|
|
auto encoded = EncodeBase58Check(data);
|
|
|
|
std::vector<unsigned char> decoded;
|
|
|
|
auto ok_too_small = DecodeBase58Check(encoded, decoded, InsecureRandRange(len));
|
|
|
|
BOOST_CHECK(!ok_too_small);
|
|
|
|
auto ok = DecodeBase58Check(encoded, decoded, len + InsecureRandRange(257 - len));
|
|
|
|
BOOST_CHECK(ok);
|
|
|
|
BOOST_CHECK(data == decoded);
|
|
|
|
}
|
2012-09-29 11:13:32 +02:00
|
|
|
}
|
|
|
|
|
2011-10-01 02:47:47 +02:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|