Merge #12079: Improve prioritisetransaction test coverage
[bitcoinplatinum.git] / src / base58.h
blob39eb4eacc2cb917e9b6fc03f0fb66980359b9201
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 /**
7 * Why base-58 instead of standard base-64 encoding?
8 * - Don't want 0OIl characters that look the same in some fonts and
9 * could be used to create visually identical looking data.
10 * - A string with non-alphanumeric characters is not as easily accepted as input.
11 * - E-mail usually won't line-break if there's no punctuation to break at.
12 * - Double-clicking selects the whole string as one word if it's all alphanumeric.
14 #ifndef BITCOIN_BASE58_H
15 #define BITCOIN_BASE58_H
17 #include <chainparams.h>
18 #include <key.h>
19 #include <pubkey.h>
20 #include <script/standard.h>
21 #include <support/allocators/zeroafterfree.h>
23 #include <string>
24 #include <vector>
26 /**
27 * Encode a byte sequence as a base58-encoded string.
28 * pbegin and pend cannot be nullptr, unless both are.
30 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
32 /**
33 * Encode a byte vector as a base58-encoded string
35 std::string EncodeBase58(const std::vector<unsigned char>& vch);
37 /**
38 * Decode a base58-encoded string (psz) into a byte vector (vchRet).
39 * return true if decoding is successful.
40 * psz cannot be nullptr.
42 bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
44 /**
45 * Decode a base58-encoded string (str) into a byte vector (vchRet).
46 * return true if decoding is successful.
48 bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
50 /**
51 * Encode a byte vector into a base58-encoded string, including checksum
53 std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
55 /**
56 * Decode a base58-encoded string (psz) that includes a checksum into a byte
57 * vector (vchRet), return true if decoding is successful
59 inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
61 /**
62 * Decode a base58-encoded string (str) that includes a checksum into a byte
63 * vector (vchRet), return true if decoding is successful
65 inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
67 /**
68 * Base class for all base58-encoded data
70 class CBase58Data
72 protected:
73 //! the version byte(s)
74 std::vector<unsigned char> vchVersion;
76 //! the actually encoded data
77 typedef std::vector<unsigned char, zero_after_free_allocator<unsigned char> > vector_uchar;
78 vector_uchar vchData;
80 CBase58Data();
81 void SetData(const std::vector<unsigned char> &vchVersionIn, const void* pdata, size_t nSize);
82 void SetData(const std::vector<unsigned char> &vchVersionIn, const unsigned char *pbegin, const unsigned char *pend);
84 public:
85 bool SetString(const char* psz, unsigned int nVersionBytes = 1);
86 bool SetString(const std::string& str);
87 std::string ToString() const;
88 int CompareTo(const CBase58Data& b58) const;
90 bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; }
91 bool operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; }
92 bool operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; }
93 bool operator< (const CBase58Data& b58) const { return CompareTo(b58) < 0; }
94 bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
97 /**
98 * A base58-encoded secret key
100 class CBitcoinSecret : public CBase58Data
102 public:
103 void SetKey(const CKey& vchSecret);
104 CKey GetKey();
105 bool IsValid() const;
106 bool SetString(const char* pszSecret);
107 bool SetString(const std::string& strSecret);
109 CBitcoinSecret(const CKey& vchSecret) { SetKey(vchSecret); }
110 CBitcoinSecret() {}
113 template<typename K, int Size, CChainParams::Base58Type Type> class CBitcoinExtKeyBase : public CBase58Data
115 public:
116 void SetKey(const K &key) {
117 unsigned char vch[Size];
118 key.Encode(vch);
119 SetData(Params().Base58Prefix(Type), vch, vch+Size);
122 K GetKey() {
123 K ret;
124 if (vchData.size() == Size) {
125 // If base58 encoded data does not hold an ext key, return a !IsValid() key
126 ret.Decode(vchData.data());
128 return ret;
131 CBitcoinExtKeyBase(const K &key) {
132 SetKey(key);
135 CBitcoinExtKeyBase(const std::string& strBase58c) {
136 SetString(strBase58c.c_str(), Params().Base58Prefix(Type).size());
139 CBitcoinExtKeyBase() {}
142 typedef CBitcoinExtKeyBase<CExtKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_SECRET_KEY> CBitcoinExtKey;
143 typedef CBitcoinExtKeyBase<CExtPubKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_PUBLIC_KEY> CBitcoinExtPubKey;
145 std::string EncodeDestination(const CTxDestination& dest);
146 CTxDestination DecodeDestination(const std::string& str);
147 bool IsValidDestinationString(const std::string& str);
148 bool IsValidDestinationString(const std::string& str, const CChainParams& params);
150 #endif // BITCOIN_BASE58_H