1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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"
28 * Encode a byte sequence as a base58-encoded string.
29 * pbegin and pend cannot be nullptr, unless both are.
31 std::string
EncodeBase58(const unsigned char* pbegin
, const unsigned char* pend
);
34 * Encode a byte vector as a base58-encoded string
36 std::string
EncodeBase58(const std::vector
<unsigned char>& vch
);
39 * Decode a base58-encoded string (psz) into a byte vector (vchRet).
40 * return true if decoding is successful.
41 * psz cannot be nullptr.
43 bool DecodeBase58(const char* psz
, std::vector
<unsigned char>& vchRet
);
46 * Decode a base58-encoded string (str) into a byte vector (vchRet).
47 * return true if decoding is successful.
49 bool DecodeBase58(const std::string
& str
, std::vector
<unsigned char>& vchRet
);
52 * Encode a byte vector into a base58-encoded string, including checksum
54 std::string
EncodeBase58Check(const std::vector
<unsigned char>& vchIn
);
57 * Decode a base58-encoded string (psz) that includes a checksum into a byte
58 * vector (vchRet), return true if decoding is successful
60 inline bool DecodeBase58Check(const char* psz
, std::vector
<unsigned char>& vchRet
);
63 * Decode a base58-encoded string (str) that includes a checksum into a byte
64 * vector (vchRet), return true if decoding is successful
66 inline bool DecodeBase58Check(const std::string
& str
, std::vector
<unsigned char>& vchRet
);
69 * Base class for all base58-encoded data
74 //! the version byte(s)
75 std::vector
<unsigned char> vchVersion
;
77 //! the actually encoded data
78 typedef std::vector
<unsigned char, zero_after_free_allocator
<unsigned char> > vector_uchar
;
82 void SetData(const std::vector
<unsigned char> &vchVersionIn
, const void* pdata
, size_t nSize
);
83 void SetData(const std::vector
<unsigned char> &vchVersionIn
, const unsigned char *pbegin
, const unsigned char *pend
);
86 bool SetString(const char* psz
, unsigned int nVersionBytes
= 1);
87 bool SetString(const std::string
& str
);
88 std::string
ToString() const;
89 int CompareTo(const CBase58Data
& b58
) const;
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; }
95 bool operator> (const CBase58Data
& b58
) const { return CompareTo(b58
) > 0; }
99 * A base58-encoded secret key
101 class CBitcoinSecret
: public CBase58Data
104 void SetKey(const CKey
& vchSecret
);
106 bool IsValid() const;
107 bool SetString(const char* pszSecret
);
108 bool SetString(const std::string
& strSecret
);
110 CBitcoinSecret(const CKey
& vchSecret
) { SetKey(vchSecret
); }
114 template<typename K
, int Size
, CChainParams::Base58Type Type
> class CBitcoinExtKeyBase
: public CBase58Data
117 void SetKey(const K
&key
) {
118 unsigned char vch
[Size
];
120 SetData(Params().Base58Prefix(Type
), vch
, vch
+Size
);
125 if (vchData
.size() == Size
) {
126 // If base58 encoded data does not hold an ext key, return a !IsValid() key
127 ret
.Decode(vchData
.data());
132 CBitcoinExtKeyBase(const K
&key
) {
136 CBitcoinExtKeyBase(const std::string
& strBase58c
) {
137 SetString(strBase58c
.c_str(), Params().Base58Prefix(Type
).size());
140 CBitcoinExtKeyBase() {}
143 typedef CBitcoinExtKeyBase
<CExtKey
, BIP32_EXTKEY_SIZE
, CChainParams::EXT_SECRET_KEY
> CBitcoinExtKey
;
144 typedef CBitcoinExtKeyBase
<CExtPubKey
, BIP32_EXTKEY_SIZE
, CChainParams::EXT_PUBLIC_KEY
> CBitcoinExtPubKey
;
146 std::string
EncodeDestination(const CTxDestination
& dest
);
147 CTxDestination
DecodeDestination(const std::string
& str
);
148 bool IsValidDestinationString(const std::string
& str
);
149 bool IsValidDestinationString(const std::string
& str
, const CChainParams
& params
);
151 #endif // BITCOIN_BASE58_H