Bitcoin Core 22.99.0
P2P Digital Currency
base_encode_decode.cpp
Go to the documentation of this file.
1// Copyright (c) 2019-2020 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 == TrimString(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 bool pf_invalid;
40 std::string decoded_string = DecodeBase32(random_encoded_string, &pf_invalid);
41 if (!pf_invalid) {
42 const std::string encoded_string = EncodeBase32(decoded_string);
43 assert(encoded_string == TrimString(encoded_string));
44 assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
45 }
46
47 decoded_string = DecodeBase64(random_encoded_string, &pf_invalid);
48 if (!pf_invalid) {
49 const std::string encoded_string = EncodeBase64(decoded_string);
50 assert(encoded_string == TrimString(encoded_string));
51 assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
52 }
53
55 std::string error;
56 (void)DecodeBase64PSBT(psbt, random_encoded_string, error);
57}
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_INIT(base_encode_decode, initialize_base_encode_decode)
void initialize_base_encode_decode()
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:316
bool DecodeBase64PSBT(PartiallySignedTransaction &psbt, const std::string &base64_tx, std::string &error)
Decode a base64ed PSBT into a PartiallySignedTransaction.
Definition: psbt.cpp:374
std::vector< unsigned char > DecodeBase32(const char *p, bool *pf_invalid)
std::vector< unsigned char > DecodeBase64(const char *p, bool *pf_invalid)
std::string EncodeBase64(Span< const unsigned char > input)
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.
std::string TrimString(const std::string &str, const std::string &pattern=" \f\n\r\t\v")
Definition: string.h:18
A version of CTransaction with the PSBT format.
Definition: psbt.h:392
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
assert(!tx.IsCoinBase())