Bitcoin Core  24.1.0
P2P Digital Currency
base_encode_decode.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 <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 
18 {
19  static const ECCVerifyHandle verify_handle;
20 }
21 
23 {
24  const std::string random_encoded_string(buffer.begin(), buffer.end());
25 
26  std::vector<unsigned char> decoded;
27  if (DecodeBase58(random_encoded_string, decoded, 100)) {
28  const std::string encoded_string = EncodeBase58(decoded);
29  assert(encoded_string == TrimStringView(encoded_string));
30  assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
31  }
32 
33  if (DecodeBase58Check(random_encoded_string, decoded, 100)) {
34  const std::string encoded_string = EncodeBase58Check(decoded);
35  assert(encoded_string == TrimString(encoded_string));
36  assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
37  }
38 
39  auto result = DecodeBase32(random_encoded_string);
40  if (result) {
41  const std::string encoded_string = EncodeBase32(*result);
42  assert(encoded_string == TrimStringView(encoded_string));
43  assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
44  }
45 
46  result = DecodeBase64(random_encoded_string);
47  if (result) {
48  const std::string encoded_string = EncodeBase64(*result);
49  assert(encoded_string == TrimString(encoded_string));
50  assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
51  }
52 
54  std::string error;
55  (void)DecodeBase64PSBT(psbt, random_encoded_string, error);
56 }
FUZZ_TARGET_INIT(base_encode_decode, initialize_base_encode_decode)
assert(!tx.IsCoinBase())
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \\\)
Definition: string.h:31
std::string EncodeBase64(Span< const unsigned char > input)
void initialize_base_encode_decode()
std::string EncodeBase58(Span< const unsigned char > input)
Why base-58 instead of standard base-64 encoding?
Definition: base58.cpp:87
A version of CTransaction with the PSBT format.
Definition: psbt.h:946
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:335
bool DecodeBase64PSBT(PartiallySignedTransaction &psbt, const std::string &base64_tx, std::string &error)
Decode a base64ed PSBT into a PartiallySignedTransaction.
Definition: psbt.cpp:458
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::string EncodeBase58Check(Span< const unsigned char > input)
Encode a byte span into a base58-encoded string, including checksum.
Definition: base58.cpp:135
static bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch, int max_ret_len)
Definition: base58.cpp:38
std::optional< std::vector< unsigned char > > DecodeBase32(std::string_view str)
static bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet, int max_ret_len)
Definition: base58.cpp:144
std::string TrimString(std::string_view str, std::string_view pattern=" \\\)
Definition: string.h:41
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.