1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#pragma once
8

9
#include "Data.h"
10
#include "Hash.h"
11

12
#include <array>
13
#include <string>
14

15
namespace TW {
16

17
class Base58 {
18
  public:
19
    /// Base58 coder with Bitcoin character map.
20
    static Base58 bitcoin;
21

22
    /// Base58 coder with Ripple character map.
23
    static Base58 ripple;
24

25
  public:
26
    /// Ordered list of valid characters.
27
    const std::array<char, 58> digits;
28

29
    /// Maps characters to base58 values.
30
    const std::array<signed char, 128> characterMap;
31

32
    /// Initializes a Base58 class with custom digit mapping.
33 1
    Base58(const std::array<char, 58>& digits, const std::array<signed char, 128>& characterMap)
34 1
        : digits(digits), characterMap(characterMap) {}
35

36
    /// Decodes a base 58 string verifying the checksum, returns empty on failure.
37 1
    Data decodeCheck(const std::string& string, Hash::Hasher hasher = Hash::sha256d) const {
38 1
        return decodeCheck(string.data(), string.data() + string.size(), hasher);
39
    }
40

41
    /// Decodes a base 58 string verifying the checksum, returns empty on failure.
42
    Data decodeCheck(const char* begin, const char* end, Hash::Hasher hasher = Hash::sha256d) const;
43

44
    /// Decodes a base 58 string into `result`, returns `false` on failure.
45 1
    Data decode(const std::string& string) const {
46 1
        return decode(string.data(), string.data() + string.size());
47
    }
48

49
    /// Decodes a base 58 string into `result`, returns `false` on failure.
50
    Data decode(const char* begin, const char* end) const;
51

52
    /// Encodes data as a base 58 string with a checksum.
53
    template <typename T>
54 1
    std::string encodeCheck(const T& data, Hash::Hasher hasher = Hash::sha256d) const {
55 1
        return encodeCheck(data.data(), data.data() + data.size(), hasher);
56
    }
57

58
    /// Encodes data as a base 58 string with a checksum.
59
    std::string encodeCheck(const byte* pbegin, const byte* pend, Hash::Hasher hasher = Hash::sha256d) const;
60

61
    /// Encodes data as a base 58 string.
62
    template <typename T>
63 1
    std::string encode(const T& data) const {
64 1
        return encode(data.data(), data.data() + data.size());
65
    }
66

67
    /// Encodes data as a base 58 string.
68
    std::string encode(const byte* pbegin, const byte* pend) const;
69
};
70

71
} // namespace TW

Read our documentation on viewing source code .

Loading