Bitcoin Core  27.99.0
P2P Digital Currency
base_encode_decode.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2022 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 <test/fuzz/fuzz.h>
6 
7 #include <base58.h>
8 #include <psbt.h>
9 #include <util/strencodings.h>
10 #include <util/string.h>
11 
12 #include <cassert>
13 #include <cstdint>
14 #include <string>
15 #include <vector>
16 
17 using util::TrimString;
19 
20 FUZZ_TARGET(base_encode_decode)
21 {
22  const std::string random_encoded_string(buffer.begin(), buffer.end());
23 
24  std::vector<unsigned char> decoded;
25  if (DecodeBase58(random_encoded_string, decoded, 100)) {
26  const std::string encoded_string = EncodeBase58(decoded);
27  assert(encoded_string == TrimStringView(encoded_string));
28  assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
29  }
30 
31  if (DecodeBase58Check(random_encoded_string, decoded, 100)) {
32  const std::string encoded_string = EncodeBase58Check(decoded);
33  assert(encoded_string == TrimString(encoded_string));
34  assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
35  }
36 
37  auto result = DecodeBase32(random_encoded_string);
38  if (result) {
39  const std::string encoded_string = EncodeBase32(*result);
40  assert(encoded_string == TrimStringView(encoded_string));
41  assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
42  }
43 
44  result = DecodeBase64(random_encoded_string);
45  if (result) {
46  const std::string encoded_string = EncodeBase64(*result);
47  assert(encoded_string == TrimString(encoded_string));
48  assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
49  }
50 
52  std::string error;
53  (void)DecodeBase64PSBT(psbt, random_encoded_string, error);
54 }
std::string EncodeBase58(Span< const unsigned char > input)
Why base-58 instead of standard base-64 encoding?
Definition: base58.cpp:89
std::string EncodeBase58Check(Span< const unsigned char > input)
Encode a byte span into a base58-encoded string, including checksum.
Definition: base58.cpp:137
static bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet, int max_ret_len)
Definition: base58.cpp:146
static bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch, int max_ret_len)
Definition: base58.cpp:40
FUZZ_TARGET(base_encode_decode)
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:69
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:79
bool DecodeBase64PSBT(PartiallySignedTransaction &psbt, const std::string &base64_tx, std::string &error)
Decode a base64ed PSBT into a PartiallySignedTransaction.
Definition: psbt.cpp:536
A version of CTransaction with the PSBT format.
Definition: psbt.h:951
std::string EncodeBase64(Span< const unsigned char > input)
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.
std::optional< std::vector< unsigned char > > DecodeBase64(std::string_view str)
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
std::optional< std::vector< unsigned char > > DecodeBase32(std::string_view str)
assert(!tx.IsCoinBase())