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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2015 The Starwels developers
  3. // Distributed under the MIT 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 data.
  9. * - A string with non-alphanumeric characters is not as easily accepted as input.
  10. * - E-mail usually won't line-break if there's no punctuation to break at.
  11. * - Double-clicking selects the whole string as one word if it's all alphanumeric.
  12. */
  13. #ifndef STARWELS_BASE58_H
  14. #define STARWELS_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 "support/allocators/zeroafterfree.h"
  21. #include <string>
  22. #include <vector>
  23. /**
  24. * Encode a byte sequence as a base58-encoded string.
  25. * pbegin and pend cannot be nullptr, unless both are.
  26. */
  27. std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
  28. /**
  29. * Encode a byte vector as a base58-encoded string
  30. */
  31. std::string EncodeBase58(const std::vector<unsigned char>& vch);
  32. /**
  33. * Decode a base58-encoded string (psz) into a byte vector (vchRet).
  34. * return true if decoding is successful.
  35. * psz cannot be nullptr.
  36. */
  37. bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
  38. /**
  39. * Decode a base58-encoded string (str) into a byte vector (vchRet).
  40. * return true if decoding is successful.
  41. */
  42. bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
  43. /**
  44. * Encode a byte vector into a base58-encoded string, including checksum
  45. */
  46. std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
  47. /**
  48. * Decode a base58-encoded string (psz) that includes a checksum into a byte
  49. * vector (vchRet), return true if decoding is successful
  50. */
  51. inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
  52. /**
  53. * Decode a base58-encoded string (str) that includes a checksum into a byte
  54. * vector (vchRet), return true if decoding is successful
  55. */
  56. inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
  57. /**
  58. * Base class for all base58-encoded data
  59. */
  60. class CBase58Data
  61. {
  62. protected:
  63. //! the version byte(s)
  64. std::vector<unsigned char> vchVersion;
  65. //! the actually encoded data
  66. typedef std::vector<unsigned char, zero_after_free_allocator<unsigned char> > vector_uchar;
  67. vector_uchar vchData;
  68. CBase58Data();
  69. void SetData(const std::vector<unsigned char> &vchVersionIn, const void* pdata, size_t nSize);
  70. void SetData(const std::vector<unsigned char> &vchVersionIn, const unsigned char *pbegin, const unsigned char *pend);
  71. public:
  72. bool SetString(const char* psz, unsigned int nVersionBytes = 1);
  73. bool SetString(const std::string& str);
  74. std::string ToString() const;
  75. int CompareTo(const CBase58Data& b58) const;
  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. bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
  81. };
  82. /** base58-encoded Starwels addresses.
  83. * Public-key-hash-addresses have version 0 (or 111 ai).
  84. * The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
  85. * Script-hash-addresses have version 5 (or 196 ai).
  86. * The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
  87. */
  88. class CStarwelsAddress : public CBase58Data {
  89. public:
  90. bool Set(const CKeyID &id);
  91. bool Set(const CScriptID &id);
  92. bool Set(const CTxDestination &dest);
  93. bool IsValid() const;
  94. bool IsValid(const CChainParams &params) const;
  95. CStarwelsAddress() {}
  96. CStarwelsAddress(const CTxDestination &dest) { Set(dest); }
  97. CStarwelsAddress(const std::string& strAddress) { SetString(strAddress); }
  98. CStarwelsAddress(const char* pszAddress) { SetString(pszAddress); }
  99. CTxDestination Get() const;
  100. bool GetKeyID(CKeyID &keyID) const;
  101. bool IsScript() const;
  102. };
  103. /**
  104. * A base58-encoded secret key
  105. */
  106. class CStarwelsSecret : public CBase58Data
  107. {
  108. public:
  109. void SetKey(const CKey& vchSecret);
  110. CKey GetKey();
  111. bool IsValid() const;
  112. bool SetString(const char* pszSecret);
  113. bool SetString(const std::string& strSecret);
  114. CStarwelsSecret(const CKey& vchSecret) { SetKey(vchSecret); }
  115. CStarwelsSecret() {}
  116. };
  117. template<typename K, int Size, CChainParams::Base58Type Type> class CStarwelsExtKeyBase : public CBase58Data
  118. {
  119. public:
  120. void SetKey(const K &key) {
  121. unsigned char vch[Size];
  122. key.Encode(vch);
  123. SetData(Params().Base58Prefix(Type), vch, vch+Size);
  124. }
  125. K GetKey() {
  126. K ret;
  127. if (vchData.size() == Size) {
  128. // If base58 encoded data does not hold an ext key, return a !IsValid() key
  129. ret.Decode(vchData.data());
  130. }
  131. return ret;
  132. }
  133. CStarwelsExtKeyBase(const K &key) {
  134. SetKey(key);
  135. }
  136. CStarwelsExtKeyBase(const std::string& strBase58c) {
  137. SetString(strBase58c.c_str(), Params().Base58Prefix(Type).size());
  138. }
  139. CStarwelsExtKeyBase() {}
  140. };
  141. typedef CStarwelsExtKeyBase<CExtKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_SECRET_KEY> CStarwelsExtKey;
  142. typedef CStarwelsExtKeyBase<CExtPubKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_PUBLIC_KEY> CStarwelsExtPubKey;
  143. #endif // STARWELS_BASE58_H