Update CMakeLists.txt - play with openssl
[novacoin.git] / src / base58.h
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.
5
6
7 //
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.
14 //
15 #ifndef BITCOIN_BASE58_H
16 #define BITCOIN_BASE58_H
17
18 #include "key.h"
19 #include "script.h"
20
21 #include <openssl/crypto.h> // for OPENSSL_cleanse()
22
23 #include <string>
24 #include <vector>
25
26 // Encode a byte sequence as a base58-encoded string
27 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
28
29 // Encode a byte vector as a base58-encoded string
30 std::string EncodeBase58(const std::vector<unsigned char>& vch);
31
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);
35
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);
39
40 // Encode a byte vector to a base58-encoded string, including checksum
41 std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
42
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);
46
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);
50
51 /** Base class for all base58-encoded data */
52 class CBase58Data
53 {
54 protected:
55     // the version byte
56     unsigned char nVersion;
57
58     // the actually encoded data
59     std::vector<unsigned char> vchData;
60
61     CBase58Data();
62     ~CBase58Data();
63
64     void SetData(int nVersionIn, const void* pdata, size_t nSize);
65     void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend);
66
67 public:
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;
72
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; }
79 };
80
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.
88  */
89 class CBitcoinAddress : public CBase58Data {
90 public:
91     enum
92     {
93         PUBKEY_PAIR_ADDRESS = 1,
94         PUBKEY_ADDRESS = 8,
95         SCRIPT_ADDRESS = 20,
96         PUBKEY_PAIR_ADDRESS_TEST = 6,
97         PUBKEY_ADDRESS_TEST = 111,
98         SCRIPT_ADDRESS_TEST = 196
99     };
100
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;
107
108     CBitcoinAddress() {}
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); }
113
114     CTxDestination Get() const;
115     bool GetKeyID(CKeyID &keyID) const;
116     bool IsScript() const;
117     bool IsPubKey() const;
118     bool IsPair() const;
119 };
120
121 /** A base58-encoded secret key */
122 class CBitcoinSecret : public CBase58Data {
123 public:
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);
129
130     CBitcoinSecret(const CSecret& vchSecret, bool fCompressed);
131     CBitcoinSecret() {}
132 };
133
134 #endif