Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2011-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <test/data/base58_encode_decode.json.h>7#include <base58.h>8#include <test/util/json.h>9#include <test/util/random.h>10#include <test/util/setup_common.h>11#include <util/strencodings.h>12#include <util/vector.h>14#include <univalue.h>16#include <boost/test/unit_test.hpp>17#include <string>19using namespace std::literals;20using namespace util::hex_literals;22BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup)24// Goal: test low-level base58 encoding functionality25BOOST_AUTO_TEST_CASE(base58_EncodeBase58)26{27UniValue tests = read_json(json_tests::base58_encode_decode);28for (unsigned int idx = 0; idx < tests.size(); idx++) {29const UniValue& test = tests[idx];30std::string strTest = test.write();31if (test.size() < 2) // Allow for extra stuff (useful for comments)32{33BOOST_ERROR("Bad test: " << strTest);34continue;35}36std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());37std::string base58string = test[1].get_str();38BOOST_CHECK_MESSAGE(39EncodeBase58(sourcedata) == base58string,40strTest);41}42}44// Goal: test low-level base58 decoding functionality45BOOST_AUTO_TEST_CASE(base58_DecodeBase58)46{47UniValue tests = read_json(json_tests::base58_encode_decode);48std::vector<unsigned char> result;50for (unsigned int idx = 0; idx < tests.size(); idx++) {51const UniValue& test = tests[idx];52std::string strTest = test.write();53if (test.size() < 2) // Allow for extra stuff (useful for comments)54{55BOOST_ERROR("Bad test: " << strTest);56continue;57}58std::vector<unsigned char> expected = ParseHex(test[0].get_str());59std::string base58string = test[1].get_str();60BOOST_CHECK_MESSAGE(DecodeBase58(base58string, result, 256), strTest);61BOOST_CHECK_MESSAGE(std::ranges::equal(result ,expected), strTest);62}64BOOST_CHECK(!DecodeBase58("invalid"s, result, 100));65BOOST_CHECK(!DecodeBase58("invalid\0"s, result, 100));66BOOST_CHECK(!DecodeBase58("\0invalid"s, result, 100));68BOOST_CHECK( DecodeBase58("good"s, result, 100));69BOOST_CHECK(!DecodeBase58("bad0IOl"s, result, 100));70BOOST_CHECK(!DecodeBase58("goodbad0IOl"s, result, 100));71BOOST_CHECK(!DecodeBase58("good\0bad0IOl"s, result, 100));73// check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.74BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result, 3));75BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result, 3));76constexpr auto expected{"971a55"_hex_u8};77BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());79BOOST_CHECK( DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh"s, result, 100));80BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oi"s, result, 100));81BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh0IOl"s, result, 100));82BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh\0" "0IOl"s, result, 100));83}85BOOST_AUTO_TEST_SUITE_END()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.