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.
251constexpr unsigned int num_chars{T::size() * 2};252static_assert(num_chars <= 64); // this test needs to be modified to allow for more than 64 hex chars253const std::string valid_64char_input{"0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF"};254const auto valid_input{valid_64char_input.substr(0, num_chars)};255{256// check that lower and upper case hex characters are accepted257auto valid_result{T::FromHex(valid_input)};258BOOST_REQUIRE(valid_result);259BOOST_CHECK_EQUAL(valid_result->ToString(), ToLower(valid_input));260}261{262// check that only strings of size num_chars are accepted263BOOST_CHECK(!T::FromHex(""));264BOOST_CHECK(!T::FromHex("0"));265BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars / 2)));266BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars - 1)));267BOOST_CHECK(!T::FromHex(valid_input + "0"));268}269{270// check that non-hex characters are not accepted271std::string invalid_chars{R"( !"#$%&'()*+,-./:;<=>?@GHIJKLMNOPQRSTUVWXYZ[\]^_`ghijklmnopqrstuvwxyz{|}~)"};272for (auto c : invalid_chars) {273BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars - 1) + c));274}275// 0x prefixes are invalid276std::string invalid_prefix{"0x" + valid_input};277BOOST_CHECK(!T::FromHex(std::string_view(invalid_prefix.data(), num_chars)));278BOOST_CHECK(!T::FromHex(invalid_prefix));279}280{281// check that string_view length is respected282std::string chars_68{valid_64char_input + "0123"};283BOOST_CHECK_EQUAL(T::FromHex(std::string_view(chars_68.data(), num_chars)).value().ToString(), ToLower(valid_input));284BOOST_CHECK(!T::FromHex(std::string_view(chars_68.data(), num_chars - 1))); // too short285BOOST_CHECK(!T::FromHex(std::string_view(chars_68.data(), num_chars + 1))); // too long286}287}289BOOST_AUTO_TEST_CASE(from_hex)290{291TestFromHex<uint160>();292TestFromHex<uint256>();293TestFromHex<Txid>();294TestFromHex<Wtxid>();295}297BOOST_AUTO_TEST_CASE(from_user_hex)298{299BOOST_CHECK_EQUAL(uint256::FromUserHex(""), uint256::ZERO);300BOOST_CHECK_EQUAL(uint256::FromUserHex("0x"), uint256::ZERO);301BOOST_CHECK_EQUAL(uint256::FromUserHex("0"), uint256::ZERO);302BOOST_CHECK_EQUAL(uint256::FromUserHex("00"), uint256::ZERO);303BOOST_CHECK_EQUAL(uint256::FromUserHex("1"), uint256::ONE);304BOOST_CHECK_EQUAL(uint256::FromUserHex("0x10"), uint256{0x10});305BOOST_CHECK_EQUAL(uint256::FromUserHex("10"), uint256{0x10});306BOOST_CHECK_EQUAL(uint256::FromUserHex("0xFf"), uint256{0xff});307BOOST_CHECK_EQUAL(uint256::FromUserHex("Ff"), uint256{0xff});308const std::string valid_hex_64{"0x0123456789abcdef0123456789abcdef0123456789ABDCEF0123456789ABCDEF"};309BOOST_REQUIRE_EQUAL(valid_hex_64.size(), 2 + 64); // 0x prefix and 64 hex digits310BOOST_CHECK_EQUAL(uint256::FromUserHex(valid_hex_64.substr(2)).value().ToString(), ToLower(valid_hex_64.substr(2)));311BOOST_CHECK_EQUAL(uint256::FromUserHex(valid_hex_64.substr(0)).value().ToString(), ToLower(valid_hex_64.substr(2)));313BOOST_CHECK(!uint256::FromUserHex("0x0 ")); // no spaces at end,314BOOST_CHECK(!uint256::FromUserHex(" 0x0")); // or beginning,315BOOST_CHECK(!uint256::FromUserHex("0x 0")); // or middle,316BOOST_CHECK(!uint256::FromUserHex(" ")); // etc.317BOOST_CHECK(!uint256::FromUserHex("0x0ga")); // invalid character318BOOST_CHECK(!uint256::FromUserHex("x0")); // broken prefix319BOOST_CHECK(!uint256::FromUserHex("0x0x00")); // two prefixes not allowed320BOOST_CHECK(!uint256::FromUserHex(valid_hex_64.substr(2) + "0")); // 1 hex digit too many321BOOST_CHECK(!uint256::FromUserHex(valid_hex_64 + "a")); // 1 hex digit too many322BOOST_CHECK(!uint256::FromUserHex(valid_hex_64 + " ")); // whitespace after max length323BOOST_CHECK(!uint256::FromUserHex(valid_hex_64 + "z")); // invalid character after max length324}326BOOST_AUTO_TEST_CASE( check_ONE )327{328uint256 one = uint256{"0000000000000000000000000000000000000000000000000000000000000001"};329BOOST_CHECK_EQUAL(one, uint256::ONE);330}332BOOST_AUTO_TEST_CASE(FromHex_vs_uint256)333{334auto runtime_uint{uint256::FromHex("4A5E1E4BAAB89F3A32518A88C31BC87F618f76673e2cc77ab2127b7afdeda33b")};335constexpr uint256 consteval_uint{ "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"};336BOOST_CHECK_EQUAL(consteval_uint, runtime_uint);337}339BOOST_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.