Somewhat confident now, tested on GNOME+KDE, with all types of transactions. Next...
[novacoin.git] / core / include / base58.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
4
5
6 //
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 account numbers.
10 // - A string with non-alphanumeric characters is not as easily accepted as an account number.
11 // - E-mail usually won't line-break if there's no punctuation to break at.
12 // - Doubleclicking selects the whole number as one word if it's all alphanumeric.
13 //
14 #ifndef BITCOIN_BASE58_H
15 #define BITCOIN_BASE58_H
16
17 #include <string>
18 #include <vector>
19 #include "bignum.h"
20
21 static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
22
23
24 inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
25 {
26     CAutoBN_CTX pctx;
27     CBigNum bn58 = 58;
28     CBigNum bn0 = 0;
29
30     // Convert big endian data to little endian
31     // Extra zero at the end make sure bignum will interpret as a positive number
32     std::vector<unsigned char> vchTmp(pend-pbegin+1, 0);
33     reverse_copy(pbegin, pend, vchTmp.begin());
34
35     // Convert little endian data to bignum
36     CBigNum bn;
37     bn.setvch(vchTmp);
38
39     // Convert bignum to std::string
40     std::string str;
41     str.reserve((pend - pbegin) * 138 / 100 + 1);
42     CBigNum dv;
43     CBigNum rem;
44     while (bn > bn0)
45     {
46         if (!BN_div(&dv, &rem, &bn, &bn58, pctx))
47             throw bignum_error("EncodeBase58 : BN_div failed");
48         bn = dv;
49         unsigned int c = rem.getulong();
50         str += pszBase58[c];
51     }
52
53     // Leading zeroes encoded as base58 zeros
54     for (const unsigned char* p = pbegin; p < pend && *p == 0; p++)
55         str += pszBase58[0];
56
57     // Convert little endian std::string to big endian
58     reverse(str.begin(), str.end());
59     return str;
60 }
61
62 inline std::string EncodeBase58(const std::vector<unsigned char>& vch)
63 {
64     return EncodeBase58(&vch[0], &vch[0] + vch.size());
65 }
66
67 inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
68 {
69     CAutoBN_CTX pctx;
70     vchRet.clear();
71     CBigNum bn58 = 58;
72     CBigNum bn = 0;
73     CBigNum bnChar;
74     while (isspace(*psz))
75         psz++;
76
77     // Convert big endian string to bignum
78     for (const char* p = psz; *p; p++)
79     {
80         const char* p1 = strchr(pszBase58, *p);
81         if (p1 == NULL)
82         {
83             while (isspace(*p))
84                 p++;
85             if (*p != '\0')
86                 return false;
87             break;
88         }
89         bnChar.setulong(p1 - pszBase58);
90         if (!BN_mul(&bn, &bn, &bn58, pctx))
91             throw bignum_error("DecodeBase58 : BN_mul failed");
92         bn += bnChar;
93     }
94
95     // Get bignum as little endian data
96     std::vector<unsigned char> vchTmp = bn.getvch();
97
98     // Trim off sign byte if present
99     if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80)
100         vchTmp.erase(vchTmp.end()-1);
101
102     // Restore leading zeros
103     int nLeadingZeros = 0;
104     for (const char* p = psz; *p == pszBase58[0]; p++)
105         nLeadingZeros++;
106     vchRet.assign(nLeadingZeros + vchTmp.size(), 0);
107
108     // Convert little endian data to big endian
109     reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size());
110     return true;
111 }
112
113 inline bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
114 {
115     return DecodeBase58(str.c_str(), vchRet);
116 }
117
118
119
120
121
122 inline std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
123 {
124     // add 4-byte hash check to the end
125     std::vector<unsigned char> vch(vchIn);
126     uint256 hash = Hash(vch.begin(), vch.end());
127     vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
128     return EncodeBase58(vch);
129 }
130
131 inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
132 {
133     if (!DecodeBase58(psz, vchRet))
134         return false;
135     if (vchRet.size() < 4)
136     {
137         vchRet.clear();
138         return false;
139     }
140     uint256 hash = Hash(vchRet.begin(), vchRet.end()-4);
141     if (memcmp(&hash, &vchRet.end()[-4], 4) != 0)
142     {
143         vchRet.clear();
144         return false;
145     }
146     vchRet.resize(vchRet.size()-4);
147     return true;
148 }
149
150 inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
151 {
152     return DecodeBase58Check(str.c_str(), vchRet);
153 }
154
155
156
157
158
159
160 #define ADDRESSVERSION   ((unsigned char)(fTestNet ? 111 : 0))
161
162 inline std::string Hash160ToAddress(uint160 hash160)
163 {
164     // add 1-byte version number to the front
165     std::vector<unsigned char> vch(1, ADDRESSVERSION);
166     vch.insert(vch.end(), UBEGIN(hash160), UEND(hash160));
167     return EncodeBase58Check(vch);
168 }
169
170 inline bool AddressToHash160(const char* psz, uint160& hash160Ret)
171 {
172     std::vector<unsigned char> vch;
173     if (!DecodeBase58Check(psz, vch))
174         return false;
175     if (vch.empty())
176         return false;
177     unsigned char nVersion = vch[0];
178     if (vch.size() != sizeof(hash160Ret) + 1)
179         return false;
180     memcpy(&hash160Ret, &vch[1], sizeof(hash160Ret));
181     return (nVersion <= ADDRESSVERSION);
182 }
183
184 inline bool AddressToHash160(const std::string& str, uint160& hash160Ret)
185 {
186     return AddressToHash160(str.c_str(), hash160Ret);
187 }
188
189 inline bool IsValidBitcoinAddress(const char* psz)
190 {
191     uint160 hash160;
192     return AddressToHash160(psz, hash160);
193 }
194
195 inline bool IsValidBitcoinAddress(const std::string& str)
196 {
197     return IsValidBitcoinAddress(str.c_str());
198 }
199
200
201
202
203 inline std::string PubKeyToAddress(const std::vector<unsigned char>& vchPubKey)
204 {
205     return Hash160ToAddress(Hash160(vchPubKey));
206 }
207
208 #endif