Transaction View: LastMonth calculation fixed
[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 <string>
19 #include <vector>
20 #include <openssl/crypto.h> // for OPENSSL_cleanse()
21 #include "bignum.h"
22 #include "key.h"
23 #include "script.h"
24
25 // Encode a byte sequence as a base58-encoded string
26 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
27
28 // Encode a byte vector as a base58-encoded string
29 std::string EncodeBase58(const std::vector<unsigned char>& vch);
30
31 // Decode a base58-encoded string psz into byte vector vchRet
32 // returns true if decoding is successful
33 bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
34
35 // Decode a base58-encoded string str into byte vector vchRet
36 // returns true if decoding is successful
37 bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
38
39 // Encode a byte vector to a base58-encoded string, including checksum
40 std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
41
42 // Decode a base58-encoded string psz that includes a checksum, into byte vector vchRet
43 // returns true if decoding is successful
44 bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
45
46 // Decode a base58-encoded string str that includes a checksum, into byte vector vchRet
47 // returns true if decoding is successful
48 bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
49
50 /** Base class for all base58-encoded data */
51 class CBase58Data
52 {
53 protected:
54     // the version byte
55     unsigned char nVersion;
56
57     // the actually encoded data
58     std::vector<unsigned char> vchData;
59
60     CBase58Data();
61     ~CBase58Data();
62
63     void SetData(int nVersionIn, const void* pdata, size_t nSize);
64     void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend);
65
66 public:
67     bool SetString(const char* psz);
68     bool SetString(const std::string& str);
69     std::string ToString() const;
70     const std::vector<unsigned char> &GetData() const;
71
72     int CompareTo(const CBase58Data& b58) const;
73     bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; }
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 };
79
80 /** base58-encoded Bitcoin addresses.
81  * Public-key-hash-addresses have version 0 (or 111 testnet).
82  * The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
83  * Script-hash-addresses have version 5 (or 196 testnet).
84  * The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
85  * Pubkey-pair-addresses have version 1 (or 6 testnet)
86  * The data vector contains a serialized copy of two compressed ECDSA secp256k1 public keys.
87  */
88 class CBitcoinAddress;
89 class CBitcoinAddressVisitor : public boost::static_visitor<bool>
90 {
91 private:
92     CBitcoinAddress *addr;
93 public:
94     CBitcoinAddressVisitor(CBitcoinAddress *addrIn) : addr(addrIn) { }
95     bool operator()(const CKeyID &id) const;
96     bool operator()(const CScriptID &id) const;
97     bool operator()(const CMalleablePubKey &mpk) const;
98     bool operator()(const CNoDestination &no) const;
99 };
100
101 class CBitcoinAddress : public CBase58Data
102 {
103 public:
104     enum
105     {
106         PUBKEY_PAIR_ADDRESS = 1,
107         PUBKEY_ADDRESS = 8,
108         SCRIPT_ADDRESS = 20,
109         PUBKEY_PAIR_ADDRESS_TEST = 6,
110         PUBKEY_ADDRESS_TEST = 111,
111         SCRIPT_ADDRESS_TEST = 196
112     };
113
114     bool Set(const CKeyID &id);
115     bool Set(const CScriptID &id);
116     bool Set(const CTxDestination &dest);
117     bool Set(const CMalleablePubKey &mpk);
118     bool Set(const CBitcoinAddress &dest);
119     bool IsValid() const;
120
121     CBitcoinAddress()
122     {
123     }
124
125     CBitcoinAddress(const CTxDestination &dest)
126     {
127         Set(dest);
128     }
129
130     CBitcoinAddress(const CMalleablePubKey &mpk)
131     {
132         Set(mpk);
133     }
134
135     CBitcoinAddress(const std::string& strAddress)
136     {
137         SetString(strAddress);
138     }
139
140     CBitcoinAddress(const char* pszAddress)
141     {
142         SetString(pszAddress);
143     }
144
145     CTxDestination Get() const;
146     bool GetKeyID(CKeyID &keyID) const;
147     bool IsScript() const;
148     bool IsPubKey() const;
149     bool IsPair() const;
150 };
151
152 bool inline CBitcoinAddressVisitor::operator()(const CKeyID &id) const            { return addr->Set(id); }
153 bool inline CBitcoinAddressVisitor::operator()(const CScriptID &id) const         { return addr->Set(id); }
154 bool inline CBitcoinAddressVisitor::operator()(const CMalleablePubKey &mpk) const { return addr->Set(mpk); }
155 bool inline CBitcoinAddressVisitor::operator()(const CNoDestination &id) const    { return false; }
156
157 /** A base58-encoded secret key */
158 class CBitcoinSecret : public CBase58Data
159 {
160 public:
161     void SetSecret(const CSecret& vchSecret, bool fCompressed);
162     CSecret GetSecret(bool &fCompressedOut);
163
164     bool IsValid() const;
165
166     bool SetString(const char* pszSecret);
167     bool SetString(const std::string& strSecret);
168
169     CBitcoinSecret(const CSecret& vchSecret, bool fCompressed);
170     CBitcoinSecret()
171     {
172     }
173 };
174
175 #endif