1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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.
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"
20 #include "script/script.h"
21 #include "script/standard.h"
22 #include "support/allocators/zeroafterfree.h"
23 #include "zcash/Address.hpp"
29 * Encode a byte sequence as a base58-encoded string.
30 * pbegin and pend cannot be NULL, unless both are.
32 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
35 * Encode a byte vector as a base58-encoded string
37 std::string EncodeBase58(const std::vector<unsigned char>& vch);
40 * Decode a base58-encoded string (psz) into a byte vector (vchRet).
41 * return true if decoding is successful.
44 bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
47 * Decode a base58-encoded string (str) into a byte vector (vchRet).
48 * return true if decoding is successful.
50 bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
53 * Encode a byte vector into a base58-encoded string, including checksum
55 std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
58 * Decode a base58-encoded string (psz) that includes a checksum into a byte
59 * vector (vchRet), return true if decoding is successful
61 bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
64 * Decode a base58-encoded string (str) that includes a checksum into a byte
65 * vector (vchRet), return true if decoding is successful
67 bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
70 * Base class for all base58-encoded data
75 //! the version byte(s)
76 std::vector<unsigned char> vchVersion;
78 //! the actually encoded data
79 typedef std::vector<unsigned char, zero_after_free_allocator<unsigned char> > vector_uchar;
83 void SetData(const std::vector<unsigned char> &vchVersionIn, const void* pdata, size_t nSize);
84 void SetData(const std::vector<unsigned char> &vchVersionIn, const unsigned char *pbegin, const unsigned char *pend);
87 bool SetString(const char* psz, unsigned int nVersionBytes);
88 bool SetString(const std::string& str, unsigned int nVersionBytes);
89 std::string ToString() const;
90 int CompareTo(const CBase58Data& b58) const;
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; }
95 bool operator< (const CBase58Data& b58) const { return CompareTo(b58) < 0; }
96 bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
99 template<class DATA_TYPE, CChainParams::Base58Type PREFIX, size_t SER_SIZE>
100 class CZCEncoding : public CBase58Data {
102 virtual std::string PrependName(const std::string& s) const = 0;
105 bool Set(const DATA_TYPE& addr);
107 DATA_TYPE Get() const;
110 /** base58-encoded Bitcoin addresses.
111 * Public-key-hash-addresses have version 0 (or 111 testnet).
112 * The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
113 * Script-hash-addresses have version 5 (or 196 testnet).
114 * The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
116 class CBitcoinAddress : public CBase58Data {
118 bool Set(const CKeyID &id);
119 bool Set(const CPubKey &key);
120 bool Set(const CScriptID &id);
121 bool Set(const CTxDestination &dest);
122 bool IsValid() const;
123 bool IsValid(const CChainParams ¶ms) const;
124 bool SetString(const char* pszSecret);
125 bool SetString(const std::string& strSecret);
128 CBitcoinAddress(const CTxDestination &dest) { Set(dest); }
129 CBitcoinAddress(const std::string& strAddress) { SetString(strAddress); }
130 CBitcoinAddress(const char* pszAddress) { SetString(pszAddress); }
132 CTxDestination Get() const;
133 bool GetKeyID(CKeyID &keyID) const;
134 bool GetKeyID_NoCheck(CKeyID& keyID) const;
135 bool GetIndexKey(uint160& hashBytes, int& type) const;
136 bool IsScript() const;
140 * A base58-encoded secret key
142 class CBitcoinSecret : public CBase58Data
145 void SetKey(const CKey& vchSecret);
147 bool IsValid() const;
148 bool SetString(const char* pszSecret);
149 bool SetString(const std::string& strSecret);
151 CBitcoinSecret(const CKey& vchSecret) { SetKey(vchSecret); }
155 template<typename K, int Size, CChainParams::Base58Type Type> class CBitcoinExtKeyBase : public CBase58Data
158 void SetKey(const K &key) {
159 unsigned char vch[Size];
161 SetData(Params().Base58Prefix(Type), vch, vch+Size);
166 if (vchData.size() == Size) {
167 //if base58 encoded data not holds a ext key, return a !IsValid() key
168 ret.Decode(&vchData[0]);
173 CBitcoinExtKeyBase(const K &key) {
177 CBitcoinExtKeyBase(const std::string& strBase58c) {
178 SetString(strBase58c.c_str(), Params().Base58Prefix(Type).size());
181 CBitcoinExtKeyBase() {}
184 typedef CBitcoinExtKeyBase<CExtKey, 74, CChainParams::EXT_SECRET_KEY> CBitcoinExtKey;
185 typedef CBitcoinExtKeyBase<CExtPubKey, 74, CChainParams::EXT_PUBLIC_KEY> CBitcoinExtPubKey;
187 #endif // BITCOIN_BASE58_H