/src/key_io.cpp

https://github.com/syscoin/syscoin · C++ · 266 lines · 221 code · 31 blank · 14 comment · 55 complexity · eb08a99110d79caefdcb784b70bc8630 MD5 · raw file

  1. // Copyright (c) 2014-2019 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include <key_io.h>
  5. #include <base58.h>
  6. #include <bech32.h>
  7. #include <util/strencodings.h>
  8. #include <algorithm>
  9. #include <assert.h>
  10. #include <string.h>
  11. /// Maximum witness length for Bech32 addresses.
  12. static constexpr std::size_t BECH32_WITNESS_PROG_MAX_LEN = 40;
  13. namespace {
  14. class DestinationEncoder
  15. {
  16. private:
  17. const CChainParams& m_params;
  18. public:
  19. explicit DestinationEncoder(const CChainParams& params) : m_params(params) {}
  20. std::string operator()(const PKHash& id) const
  21. {
  22. std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
  23. data.insert(data.end(), id.begin(), id.end());
  24. return EncodeBase58Check(data);
  25. }
  26. std::string operator()(const ScriptHash& id) const
  27. {
  28. std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
  29. data.insert(data.end(), id.begin(), id.end());
  30. return EncodeBase58Check(data);
  31. }
  32. std::string operator()(const WitnessV0KeyHash& id) const
  33. {
  34. std::vector<unsigned char> data = {0};
  35. data.reserve(33);
  36. ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end());
  37. return bech32::Encode(bech32::Encoding::BECH32, m_params.Bech32HRP(), data);
  38. }
  39. std::string operator()(const WitnessV0ScriptHash& id) const
  40. {
  41. std::vector<unsigned char> data = {0};
  42. data.reserve(53);
  43. ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end());
  44. return bech32::Encode(bech32::Encoding::BECH32, m_params.Bech32HRP(), data);
  45. }
  46. std::string operator()(const WitnessUnknown& id) const
  47. {
  48. if (id.version < 1 || id.version > 16 || id.length < 2 || id.length > 40) {
  49. return {};
  50. }
  51. std::vector<unsigned char> data = {(unsigned char)id.version};
  52. data.reserve(1 + (id.length * 8 + 4) / 5);
  53. ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.program, id.program + id.length);
  54. return bech32::Encode(bech32::Encoding::BECH32M, m_params.Bech32HRP(), data);
  55. }
  56. std::string operator()(const CNoDestination& no) const { return {}; }
  57. };
  58. CTxDestination DecodeDestination(const std::string& str, const CChainParams& params, std::string& error_str)
  59. {
  60. std::vector<unsigned char> data;
  61. uint160 hash;
  62. error_str = "";
  63. if (DecodeBase58Check(str, data, 21)) {
  64. // base58-encoded Syscoin addresses.
  65. // Public-key-hash-addresses have version 0 (or 111 testnet).
  66. // The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
  67. const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
  68. if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
  69. std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin());
  70. return PKHash(hash);
  71. }
  72. // Script-hash-addresses have version 5 (or 196 testnet).
  73. // The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
  74. const std::vector<unsigned char>& script_prefix = params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
  75. if (data.size() == hash.size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
  76. std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin());
  77. return ScriptHash(hash);
  78. }
  79. // Set potential error message.
  80. // This message may be changed if the address can also be interpreted as a Bech32 address.
  81. error_str = "Invalid prefix for Base58-encoded address";
  82. }
  83. data.clear();
  84. const auto dec = bech32::Decode(str);
  85. if ((dec.encoding == bech32::Encoding::BECH32 || dec.encoding == bech32::Encoding::BECH32M) && dec.data.size() > 0) {
  86. // Bech32 decoding
  87. error_str = "";
  88. if (dec.hrp != params.Bech32HRP()) {
  89. error_str = "Invalid prefix for Bech32 address";
  90. return CNoDestination();
  91. }
  92. int version = dec.data[0]; // The first 5 bit symbol is the witness version (0-16)
  93. if (version == 0 && dec.encoding != bech32::Encoding::BECH32) {
  94. error_str = "Version 0 witness address must use Bech32 checksum";
  95. return CNoDestination();
  96. }
  97. if (version != 0 && dec.encoding != bech32::Encoding::BECH32M) {
  98. error_str = "Version 1+ witness address must use Bech32m checksum";
  99. return CNoDestination();
  100. }
  101. // The rest of the symbols are converted witness program bytes.
  102. data.reserve(((dec.data.size() - 1) * 5) / 8);
  103. if (ConvertBits<5, 8, false>([&](unsigned char c) { data.push_back(c); }, dec.data.begin() + 1, dec.data.end())) {
  104. if (version == 0) {
  105. {
  106. WitnessV0KeyHash keyid;
  107. if (data.size() == keyid.size()) {
  108. std::copy(data.begin(), data.end(), keyid.begin());
  109. return keyid;
  110. }
  111. }
  112. {
  113. WitnessV0ScriptHash scriptid;
  114. if (data.size() == scriptid.size()) {
  115. std::copy(data.begin(), data.end(), scriptid.begin());
  116. return scriptid;
  117. }
  118. }
  119. error_str = "Invalid Bech32 v0 address data size";
  120. return CNoDestination();
  121. }
  122. if (version > 16) {
  123. error_str = "Invalid Bech32 address witness version";
  124. return CNoDestination();
  125. }
  126. if (data.size() < 2 || data.size() > BECH32_WITNESS_PROG_MAX_LEN) {
  127. error_str = "Invalid Bech32 address data size";
  128. return CNoDestination();
  129. }
  130. WitnessUnknown unk;
  131. unk.version = version;
  132. std::copy(data.begin(), data.end(), unk.program);
  133. unk.length = data.size();
  134. return unk;
  135. }
  136. }
  137. // Set error message if address can't be interpreted as Base58 or Bech32.
  138. if (error_str.empty()) error_str = "Invalid address format";
  139. return CNoDestination();
  140. }
  141. } // namespace
  142. CKey DecodeSecret(const std::string& str)
  143. {
  144. CKey key;
  145. std::vector<unsigned char> data;
  146. if (DecodeBase58Check(str, data, 34)) {
  147. const std::vector<unsigned char>& privkey_prefix = Params().Base58Prefix(CChainParams::SECRET_KEY);
  148. if ((data.size() == 32 + privkey_prefix.size() || (data.size() == 33 + privkey_prefix.size() && data.back() == 1)) &&
  149. std::equal(privkey_prefix.begin(), privkey_prefix.end(), data.begin())) {
  150. bool compressed = data.size() == 33 + privkey_prefix.size();
  151. key.Set(data.begin() + privkey_prefix.size(), data.begin() + privkey_prefix.size() + 32, compressed);
  152. }
  153. }
  154. if (!data.empty()) {
  155. memory_cleanse(data.data(), data.size());
  156. }
  157. return key;
  158. }
  159. std::string EncodeSecret(const CKey& key)
  160. {
  161. assert(key.IsValid());
  162. std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::SECRET_KEY);
  163. data.insert(data.end(), key.begin(), key.end());
  164. if (key.IsCompressed()) {
  165. data.push_back(1);
  166. }
  167. std::string ret = EncodeBase58Check(data);
  168. memory_cleanse(data.data(), data.size());
  169. return ret;
  170. }
  171. CExtPubKey DecodeExtPubKey(const std::string& str)
  172. {
  173. CExtPubKey key;
  174. std::vector<unsigned char> data;
  175. if (DecodeBase58Check(str, data, 78)) {
  176. const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::EXT_PUBLIC_KEY);
  177. if (data.size() == BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
  178. key.Decode(data.data() + prefix.size());
  179. }
  180. }
  181. return key;
  182. }
  183. std::string EncodeExtPubKey(const CExtPubKey& key)
  184. {
  185. std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::EXT_PUBLIC_KEY);
  186. size_t size = data.size();
  187. data.resize(size + BIP32_EXTKEY_SIZE);
  188. key.Encode(data.data() + size);
  189. std::string ret = EncodeBase58Check(data);
  190. return ret;
  191. }
  192. CExtKey DecodeExtKey(const std::string& str)
  193. {
  194. CExtKey key;
  195. std::vector<unsigned char> data;
  196. if (DecodeBase58Check(str, data, 78)) {
  197. const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
  198. if (data.size() == BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
  199. key.Decode(data.data() + prefix.size());
  200. }
  201. }
  202. return key;
  203. }
  204. std::string EncodeExtKey(const CExtKey& key)
  205. {
  206. std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
  207. size_t size = data.size();
  208. data.resize(size + BIP32_EXTKEY_SIZE);
  209. key.Encode(data.data() + size);
  210. std::string ret = EncodeBase58Check(data);
  211. memory_cleanse(data.data(), data.size());
  212. return ret;
  213. }
  214. std::string EncodeDestination(const CTxDestination& dest)
  215. {
  216. return std::visit(DestinationEncoder(Params()), dest);
  217. }
  218. CTxDestination DecodeDestination(const std::string& str, std::string& error_msg)
  219. {
  220. return DecodeDestination(str, Params(), error_msg);
  221. }
  222. CTxDestination DecodeDestination(const std::string& str)
  223. {
  224. std::string error_msg;
  225. return DecodeDestination(str, error_msg);
  226. }
  227. bool IsValidDestinationString(const std::string& str, const CChainParams& params)
  228. {
  229. std::string error_msg;
  230. return IsValidDestination(DecodeDestination(str, params, error_msg));
  231. }
  232. bool IsValidDestinationString(const std::string& str)
  233. {
  234. return IsValidDestinationString(str, Params());
  235. }