1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin Developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
8 // Why base-58 instead of standard base-64 encoding?
9 // - Don't want 0OIl characters that look the same in some fonts and
10 // could be used to create visually identical looking account numbers.
11 // - A string with non-alphanumeric characters is not as easily accepted as an account number.
12 // - E-mail usually won't line-break if there's no punctuation to break at.
13 // - Double-clicking selects the whole number as one word if it's all alphanumeric.
15 #ifndef BITCOIN_BASE58_H
16 #define BITCOIN_BASE58_H
21 #include <openssl/crypto.h> // for OPENSSL_cleanse()
26 // Encode a byte sequence as a base58-encoded string
27 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
29 // Encode a byte vector as a base58-encoded string
30 std::string EncodeBase58(const std::vector<unsigned char>& vch);
32 // Decode a base58-encoded string psz into byte vector vchRet
33 // returns true if decoding is successful
34 bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
36 // Decode a base58-encoded string str into byte vector vchRet
37 // returns true if decoding is successful
38 bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
40 // Encode a byte vector to a base58-encoded string, including checksum
41 std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
43 // Decode a base58-encoded string psz that includes a checksum, into byte vector vchRet
44 // returns true if decoding is successful
45 bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
47 // Decode a base58-encoded string str that includes a checksum, into byte vector vchRet
48 // returns true if decoding is successful
49 bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
51 /** Base class for all base58-encoded data */
56 unsigned char nVersion;
58 // the actually encoded data
59 std::vector<unsigned char> vchData;
64 void SetData(int nVersionIn, const void* pdata, size_t nSize);
65 void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend);
68 bool SetString(const char* psz);
69 bool SetString(const std::string& str);
70 std::string ToString() const;
71 const std::vector<unsigned char> &GetData() const;
73 int CompareTo(const CBase58Data& b58) const;
74 bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; }
75 bool operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; }
76 bool operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; }
77 bool operator< (const CBase58Data& b58) const { return CompareTo(b58) < 0; }
78 bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
81 /** base58-encoded Bitcoin addresses.
82 * Public-key-hash-addresses have version 0 (or 111 testnet).
83 * The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
84 * Script-hash-addresses have version 5 (or 196 testnet).
85 * The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
86 * Pubkey-pair-addresses have version 1 (or 6 testnet)
87 * The data vector contains a serialized copy of two compressed ECDSA secp256k1 public keys.
89 class CBitcoinAddress : public CBase58Data {
93 PUBKEY_PAIR_ADDRESS = 1,
96 PUBKEY_PAIR_ADDRESS_TEST = 6,
97 PUBKEY_ADDRESS_TEST = 111,
98 SCRIPT_ADDRESS_TEST = 196
101 bool Set(const CKeyID &id);
102 bool Set(const CScriptID &id);
103 bool Set(const CTxDestination &dest);
104 bool Set(const CMalleablePubKey &mpk);
105 bool Set(const CBitcoinAddress &dest);
106 bool IsValid() const;
109 CBitcoinAddress(const CTxDestination &dest) { Set(dest); }
110 CBitcoinAddress(const CMalleablePubKey &mpk) { Set(mpk); }
111 CBitcoinAddress(const std::string& strAddress) { SetString(strAddress); }
112 CBitcoinAddress(const char* pszAddress) { SetString(pszAddress); }
114 CTxDestination Get() const;
115 bool GetKeyID(CKeyID &keyID) const;
116 bool IsScript() const;
117 bool IsPubKey() const;
121 /** A base58-encoded secret key */
122 class CBitcoinSecret : public CBase58Data {
124 void SetSecret(const CSecret& vchSecret, bool fCompressed);
125 CSecret GetSecret(bool &fCompressedOut);
126 bool IsValid() const;
127 bool SetString(const char* pszSecret);
128 bool SetString(const std::string& strSecret);
130 CBitcoinSecret(const CSecret& vchSecret, bool fCompressed);