Bitcoin Core  27.99.0
P2P Digital Currency
hex.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <core_io.h>
6 #include <primitives/block.h>
7 #include <pubkey.h>
8 #include <rpc/util.h>
9 #include <test/fuzz/fuzz.h>
10 #include <uint256.h>
11 #include <univalue.h>
12 #include <util/strencodings.h>
13 
14 #include <cassert>
15 #include <cstdint>
16 #include <string>
17 #include <vector>
18 
20 {
21  const std::string random_hex_string(buffer.begin(), buffer.end());
22  const std::vector<unsigned char> data = ParseHex(random_hex_string);
23  const std::vector<std::byte> bytes{ParseHex<std::byte>(random_hex_string)};
24  assert(AsBytes(Span{data}) == Span{bytes});
25  const std::string hex_data = HexStr(data);
26  if (IsHex(random_hex_string)) {
27  assert(ToLower(random_hex_string) == hex_data);
28  }
29  (void)IsHexNumber(random_hex_string);
30  uint256 result;
31  (void)ParseHashStr(random_hex_string, result);
32  (void)uint256S(random_hex_string);
33  try {
34  (void)HexToPubKey(random_hex_string);
35  } catch (const UniValue&) {
36  }
37  CBlockHeader block_header;
38  (void)DecodeHexBlockHeader(block_header, random_hex_string);
39  CBlock block;
40  (void)DecodeHexBlk(block, random_hex_string);
41 }
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
Definition: block.h:69
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:98
256-bit opaque blob.
Definition: uint256.h:106
bool DecodeHexBlk(CBlock &, const std::string &strHexBlk)
Definition: core_read.cpp:218
bool ParseHashStr(const std::string &strHex, uint256 &result)
Parse a hex string into 256 bits.
Definition: core_read.cpp:235
bool DecodeHexBlockHeader(CBlockHeader &, const std::string &hex_header)
Definition: core_read.cpp:204
FUZZ_TARGET(hex)
Definition: hex.cpp:19
CPubKey HexToPubKey(const std::string &hex_in)
Definition: util.cpp:191
Span< const std::byte > AsBytes(Span< T > s) noexcept
Definition: span.h:266
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
Definition: strencodings.h:65
uint256 uint256S(const char *str)
Definition: uint256.h:119
bool IsHexNumber(std::string_view str)
Return true if the string is a hex number, optionally prefixed with "0x".
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
bool IsHex(std::string_view str)
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
assert(!tx.IsCoinBase())