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 FUZZ_TARGET(base_encode_decode)
18 {
19  const std::string random_encoded_string(buffer.begin(), buffer.end());
20 
21  std::vector<unsigned char> decoded;
22  if (DecodeBase58(random_encoded_string, decoded, 100)) {
23  const std::string encoded_string = EncodeBase58(decoded);
24  assert(encoded_string == TrimStringView(encoded_string));
25  assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
26  }
27 
28  if (DecodeBase58Check(random_encoded_string, decoded, 100)) {
29  const std::string encoded_string = EncodeBase58Check(decoded);
30  assert(encoded_string == TrimString(encoded_string));
31  assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
32  }
33 
34  auto result = DecodeBase32(random_encoded_string);
35  if (result) {
36  const std::string encoded_string = EncodeBase32(*result);
37  assert(encoded_string == TrimStringView(encoded_string));
38  assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
39  }
40 
41  result = DecodeBase64(random_encoded_string);
42  if (result) {
43  const std::string encoded_string = EncodeBase64(*result);
44  assert(encoded_string == TrimString(encoded_string));
45  assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
46  }
47 
49  std::string error;
50  (void)DecodeBase64PSBT(psbt, random_encoded_string, error);
51 }
std::string EncodeBase58(Span< const unsigned char > input)
Why base-58 instead of standard base-64 encoding?
Definition: base58.cpp:87
std::string EncodeBase58Check(Span< const unsigned char > input)
Encode a byte span into a base58-encoded string, including checksum.
Definition: base58.cpp:135
static bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet, int max_ret_len)
Definition: base58.cpp:144
static bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch, int max_ret_len)
Definition: base58.cpp:38
FUZZ_TARGET(base_encode_decode)
bool DecodeBase64PSBT(PartiallySignedTransaction &psbt, const std::string &base64_tx, std::string &error)
Decode a base64ed PSBT into a PartiallySignedTransaction.
Definition: psbt.cpp:536
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:41
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:31
A version of CTransaction with the PSBT format.
Definition: psbt.h:947
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())