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
22 #include <openssl/crypto.h> // for OPENSSL_cleanse()
27 // Encode a byte sequence as a base58-encoded string
28 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
30 // Encode a byte vector as a base58-encoded string
31 std::string EncodeBase58(const std::vector<unsigned char>& vch);
33 // Decode a base58-encoded string psz into byte vector vchRet
34 // returns true if decoding is successful
35 bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
37 // Decode a base58-encoded string str into byte vector vchRet
38 // returns true if decoding is successful
39 bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
41 // Encode a byte vector to a base58-encoded string, including checksum
42 std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
44 // Decode a base58-encoded string psz that includes a checksum, into byte vector vchRet
45 // returns true if decoding is successful
46 bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
48 // Decode a base58-encoded string str that includes a checksum, into byte vector vchRet
49 // returns true if decoding is successful
50 bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
52 /** Base class for all base58-encoded data */
57 unsigned char nVersion;
59 // the actually encoded data
60 std::vector<unsigned char> vchData;
65 void SetData(int nVersionIn, const void* pdata, size_t nSize);
66 void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend);
69 bool SetString(const char* psz);
70 bool SetString(const std::string& str);
71 std::string ToString() const;
72 const std::vector<unsigned char> &GetData() const;
74 int CompareTo(const CBase58Data& b58) const;
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; }
79 bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
82 /** base58-encoded Bitcoin addresses.
83 * Public-key-hash-addresses have version 0 (or 111 testnet).
84 * The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
85 * Script-hash-addresses have version 5 (or 196 testnet).
86 * The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
87 * Pubkey-pair-addresses have version 1 (or 6 testnet)
88 * The data vector contains a serialized copy of two compressed ECDSA secp256k1 public keys.
90 class CBitcoinAddress : public CBase58Data {
94 PUBKEY_PAIR_ADDRESS = 1,
97 PUBKEY_PAIR_ADDRESS_TEST = 6,
98 PUBKEY_ADDRESS_TEST = 111,
99 SCRIPT_ADDRESS_TEST = 196
102 bool Set(const CKeyID &id);
103 bool Set(const CScriptID &id);
104 bool Set(const CTxDestination &dest);
105 bool Set(const CMalleablePubKey &mpk);
106 bool Set(const CBitcoinAddress &dest);
107 bool IsValid() const;
110 CBitcoinAddress(const CTxDestination &dest) { Set(dest); }
111 CBitcoinAddress(const CMalleablePubKey &mpk) { Set(mpk); }
112 CBitcoinAddress(const std::string& strAddress) { SetString(strAddress); }
113 CBitcoinAddress(const char* pszAddress) { SetString(pszAddress); }
115 CTxDestination Get() const;
116 bool GetKeyID(CKeyID &keyID) const;
117 bool IsScript() const;
118 bool IsPubKey() const;
122 /** A base58-encoded secret key */
123 class CBitcoinSecret : public CBase58Data {
125 void SetSecret(const CSecret& vchSecret, bool fCompressed);
126 CSecret GetSecret(bool &fCompressedOut);
127 bool IsValid() const;
128 bool SetString(const char* pszSecret);
129 bool SetString(const std::string& strSecret);
131 CBitcoinSecret(const CSecret& vchSecret, bool fCompressed);