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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2013 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. // Why base-58 instead of standard base-64 encoding?
  7. // - Don't want 0OIl characters that look the same in some fonts and
  8. // could be used to create visually identical looking account numbers.
  9. // - A string with non-alphanumeric characters is not as easily accepted as an account number.
  10. // - E-mail usually won't line-break if there's no punctuation to break at.
  11. // - Double-clicking selects the whole number as one word if it's all alphanumeric.
  12. //
  13. #ifndef BITCOIN_BASE58_H
  14. #define BITCOIN_BASE58_H
  15. #include "chainparams.h"
  16. #include "key.h"
  17. #include "pubkey.h"
  18. #include "script/script.h"
  19. #include "script/standard.h"
  20. #include <string>
  21. #include <vector>
  22. /**
  23. * Encode a byte sequence as a base58-encoded string.
  24. * pbegin and pend cannot be NULL, unless both are.
  25. */
  26. std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
  27. /**
  28. * Encode a byte vector as a base58-encoded string
  29. */
  30. std::string EncodeBase58(const std::vector<unsigned char>& vch);
  31. /**
  32. * Decode a base58-encoded string (psz) into a byte vector (vchRet).
  33. * return true if decoding is successful.
  34. * psz cannot be NULL.
  35. */
  36. bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
  37. /**
  38. * Decode a base58-encoded string (str) into a byte vector (vchRet).
  39. * return true if decoding is successful.
  40. */
  41. bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
  42. /**
  43. * Encode a byte vector into a base58-encoded string, including checksum
  44. */
  45. std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
  46. /**
  47. * Decode a base58-encoded string (psz) that includes a checksum into a byte
  48. * vector (vchRet), return true if decoding is successful
  49. */
  50. inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
  51. /**
  52. * Decode a base58-encoded string (str) that includes a checksum into a byte
  53. * vector (vchRet), return true if decoding is successful
  54. */
  55. inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
  56. /**
  57. * Base class for all base58-encoded data
  58. */
  59. class CBase58Data
  60. {
  61. protected:
  62. // the version byte(s)
  63. std::vector<unsigned char> vchVersion;
  64. // the actually encoded data
  65. typedef std::vector<unsigned char, zero_after_free_allocator<unsigned char> > vector_uchar;
  66. vector_uchar vchData;
  67. CBase58Data();
  68. void SetData(const std::vector<unsigned char> &vchVersionIn, const void* pdata, size_t nSize);
  69. void SetData(const std::vector<unsigned char> &vchVersionIn, const unsigned char *pbegin, const unsigned char *pend);
  70. public:
  71. bool SetString(const char* psz, unsigned int nVersionBytes = 1);
  72. bool SetString(const std::string& str);
  73. std::string ToString() 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; }
  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. */
  87. class CBitcoinAddress : public CBase58Data {
  88. public:
  89. bool Set(const CKeyID &id);
  90. bool Set(const CScriptID &id);
  91. bool Set(const CTxDestination &dest);
  92. bool IsValid() const;
  93. bool IsValid(const CChainParams &params) const;
  94. CBitcoinAddress() {}
  95. CBitcoinAddress(const CTxDestination &dest) { Set(dest); }
  96. CBitcoinAddress(const std::string& strAddress) { SetString(strAddress); }
  97. CBitcoinAddress(const char* pszAddress) { SetString(pszAddress); }
  98. CTxDestination Get() const;
  99. bool GetKeyID(CKeyID &keyID) const;
  100. bool IsScript() const;
  101. };
  102. /**
  103. * A base58-encoded secret key
  104. */
  105. class CBitcoinSecret : public CBase58Data
  106. {
  107. public:
  108. void SetKey(const CKey& vchSecret);
  109. CKey GetKey();
  110. bool IsValid() const;
  111. bool SetString(const char* pszSecret);
  112. bool SetString(const std::string& strSecret);
  113. CBitcoinSecret(const CKey& vchSecret) { SetKey(vchSecret); }
  114. CBitcoinSecret() {}
  115. };
  116. template<typename K, int Size, CChainParams::Base58Type Type> class CBitcoinExtKeyBase : public CBase58Data
  117. {
  118. public:
  119. void SetKey(const K &key) {
  120. unsigned char vch[Size];
  121. key.Encode(vch);
  122. SetData(Params().Base58Prefix(Type), vch, vch+Size);
  123. }
  124. K GetKey() {
  125. K ret;
  126. ret.Decode(&vchData[0], &vchData[Size]);
  127. return ret;
  128. }
  129. CBitcoinExtKeyBase(const K &key) {
  130. SetKey(key);
  131. }
  132. CBitcoinExtKeyBase() {}
  133. };
  134. typedef CBitcoinExtKeyBase<CExtKey, 74, CChainParams::EXT_SECRET_KEY> CBitcoinExtKey;
  135. typedef CBitcoinExtKeyBase<CExtPubKey, 74, CChainParams::EXT_PUBLIC_KEY> CBitcoinExtPubKey;
  136. #endif // BITCOIN_BASE58_H