You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base58.h 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // Why base-58 instead of standard base-64 encoding?
  6. // - Don't want 0OIl characters that look the same in some fonts and
  7. // could be used to create visually identical looking account numbers.
  8. // - A string with non-alphanumeric characters is not as easily accepted as an account number.
  9. // - E-mail usually won't line-break if there's no punctuation to break at.
  10. // - Doubleclicking selects the whole number as one word if it's all alphanumeric.
  11. //
  12. #ifndef BITCOIN_BASE58_H
  13. #define BITCOIN_BASE58_H
  14. #include <string>
  15. #include <vector>
  16. #include "bignum.h"
  17. static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
  18. inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
  19. {
  20. CAutoBN_CTX pctx;
  21. CBigNum bn58 = 58;
  22. CBigNum bn0 = 0;
  23. // Convert big endian data to little endian
  24. // Extra zero at the end make sure bignum will interpret as a positive number
  25. std::vector<unsigned char> vchTmp(pend-pbegin+1, 0);
  26. reverse_copy(pbegin, pend, vchTmp.begin());
  27. // Convert little endian data to bignum
  28. CBigNum bn;
  29. bn.setvch(vchTmp);
  30. // Convert bignum to std::string
  31. std::string str;
  32. str.reserve((pend - pbegin) * 138 / 100 + 1);
  33. CBigNum dv;
  34. CBigNum rem;
  35. while (bn > bn0)
  36. {
  37. if (!BN_div(&dv, &rem, &bn, &bn58, pctx))
  38. throw bignum_error("EncodeBase58 : BN_div failed");
  39. bn = dv;
  40. unsigned int c = rem.getulong();
  41. str += pszBase58[c];
  42. }
  43. // Leading zeroes encoded as base58 zeros
  44. for (const unsigned char* p = pbegin; p < pend && *p == 0; p++)
  45. str += pszBase58[0];
  46. // Convert little endian std::string to big endian
  47. reverse(str.begin(), str.end());
  48. return str;
  49. }
  50. inline std::string EncodeBase58(const std::vector<unsigned char>& vch)
  51. {
  52. return EncodeBase58(&vch[0], &vch[0] + vch.size());
  53. }
  54. inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
  55. {
  56. CAutoBN_CTX pctx;
  57. vchRet.clear();
  58. CBigNum bn58 = 58;
  59. CBigNum bn = 0;
  60. CBigNum bnChar;
  61. while (isspace(*psz))
  62. psz++;
  63. // Convert big endian string to bignum
  64. for (const char* p = psz; *p; p++)
  65. {
  66. const char* p1 = strchr(pszBase58, *p);
  67. if (p1 == NULL)
  68. {
  69. while (isspace(*p))
  70. p++;
  71. if (*p != '\0')
  72. return false;
  73. break;
  74. }
  75. bnChar.setulong(p1 - pszBase58);
  76. if (!BN_mul(&bn, &bn, &bn58, pctx))
  77. throw bignum_error("DecodeBase58 : BN_mul failed");
  78. bn += bnChar;
  79. }
  80. // Get bignum as little endian data
  81. std::vector<unsigned char> vchTmp = bn.getvch();
  82. // Trim off sign byte if present
  83. if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80)
  84. vchTmp.erase(vchTmp.end()-1);
  85. // Restore leading zeros
  86. int nLeadingZeros = 0;
  87. for (const char* p = psz; *p == pszBase58[0]; p++)
  88. nLeadingZeros++;
  89. vchRet.assign(nLeadingZeros + vchTmp.size(), 0);
  90. // Convert little endian data to big endian
  91. reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size());
  92. return true;
  93. }
  94. inline bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
  95. {
  96. return DecodeBase58(str.c_str(), vchRet);
  97. }
  98. inline std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
  99. {
  100. // add 4-byte hash check to the end
  101. std::vector<unsigned char> vch(vchIn);
  102. uint256 hash = Hash(vch.begin(), vch.end());
  103. vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
  104. return EncodeBase58(vch);
  105. }
  106. inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
  107. {
  108. if (!DecodeBase58(psz, vchRet))
  109. return false;
  110. if (vchRet.size() < 4)
  111. {
  112. vchRet.clear();
  113. return false;
  114. }
  115. uint256 hash = Hash(vchRet.begin(), vchRet.end()-4);
  116. if (memcmp(&hash, &vchRet.end()[-4], 4) != 0)
  117. {
  118. vchRet.clear();
  119. return false;
  120. }
  121. vchRet.resize(vchRet.size()-4);
  122. return true;
  123. }
  124. inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
  125. {
  126. return DecodeBase58Check(str.c_str(), vchRet);
  127. }
  128. #define ADDRESSVERSION ((unsigned char)(fTestNet ? 111 : 0))
  129. inline std::string Hash160ToAddress(uint160 hash160)
  130. {
  131. // add 1-byte version number to the front
  132. std::vector<unsigned char> vch(1, ADDRESSVERSION);
  133. vch.insert(vch.end(), UBEGIN(hash160), UEND(hash160));
  134. return EncodeBase58Check(vch);
  135. }
  136. inline bool AddressToHash160(const char* psz, uint160& hash160Ret)
  137. {
  138. std::vector<unsigned char> vch;
  139. if (!DecodeBase58Check(psz, vch))
  140. return false;
  141. if (vch.empty())
  142. return false;
  143. unsigned char nVersion = vch[0];
  144. if (vch.size() != sizeof(hash160Ret) + 1)
  145. return false;
  146. memcpy(&hash160Ret, &vch[1], sizeof(hash160Ret));
  147. return (nVersion <= ADDRESSVERSION);
  148. }
  149. inline bool AddressToHash160(const std::string& str, uint160& hash160Ret)
  150. {
  151. return AddressToHash160(str.c_str(), hash160Ret);
  152. }
  153. inline bool IsValidBitcoinAddress(const char* psz)
  154. {
  155. uint160 hash160;
  156. return AddressToHash160(psz, hash160);
  157. }
  158. inline bool IsValidBitcoinAddress(const std::string& str)
  159. {
  160. return IsValidBitcoinAddress(str.c_str());
  161. }
  162. inline std::string PubKeyToAddress(const std::vector<unsigned char>& vchPubKey)
  163. {
  164. return Hash160ToAddress(Hash160(vchPubKey));
  165. }
  166. #endif