PageRenderTime 73ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/src/key_io.cpp

https://github.com/denis2342/bitcoin
C++ | 281 lines | 234 code | 33 blank | 14 comment | 60 complexity | bdababf56e96da6ff6ceccf629b45aec 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 WitnessV1Taproot& tap) const
  47. {
  48. std::vector<unsigned char> data = {1};
  49. data.reserve(53);
  50. ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, tap.begin(), tap.end());
  51. return bech32::Encode(bech32::Encoding::BECH32M, m_params.Bech32HRP(), data);
  52. }
  53. std::string operator()(const WitnessUnknown& id) const
  54. {
  55. if (id.version < 1 || id.version > 16 || id.length < 2 || id.length > 40) {
  56. return {};
  57. }
  58. std::vector<unsigned char> data = {(unsigned char)id.version};
  59. data.reserve(1 + (id.length * 8 + 4) / 5);
  60. ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.program, id.program + id.length);
  61. return bech32::Encode(bech32::Encoding::BECH32M, m_params.Bech32HRP(), data);
  62. }
  63. std::string operator()(const CNoDestination& no) const { return {}; }
  64. };
  65. CTxDestination DecodeDestination(const std::string& str, const CChainParams& params, std::string& error_str)
  66. {
  67. std::vector<unsigned char> data;
  68. uint160 hash;
  69. error_str = "";
  70. if (DecodeBase58Check(str, data, 21)) {
  71. // base58-encoded Bitcoin addresses.
  72. // Public-key-hash-addresses have version 0 (or 111 testnet).
  73. // The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
  74. const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
  75. if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
  76. std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin());
  77. return PKHash(hash);
  78. }
  79. // Script-hash-addresses have version 5 (or 196 testnet).
  80. // The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
  81. const std::vector<unsigned char>& script_prefix = params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
  82. if (data.size() == hash.size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
  83. std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin());
  84. return ScriptHash(hash);
  85. }
  86. // Set potential error message.
  87. // This message may be changed if the address can also be interpreted as a Bech32 address.
  88. error_str = "Invalid prefix for Base58-encoded address";
  89. }
  90. data.clear();
  91. const auto dec = bech32::Decode(str);
  92. if ((dec.encoding == bech32::Encoding::BECH32 || dec.encoding == bech32::Encoding::BECH32M) && dec.data.size() > 0) {
  93. // Bech32 decoding
  94. error_str = "";
  95. if (dec.hrp != params.Bech32HRP()) {
  96. error_str = "Invalid prefix for Bech32 address";
  97. return CNoDestination();
  98. }
  99. int version = dec.data[0]; // The first 5 bit symbol is the witness version (0-16)
  100. if (version == 0 && dec.encoding != bech32::Encoding::BECH32) {
  101. error_str = "Version 0 witness address must use Bech32 checksum";
  102. return CNoDestination();
  103. }
  104. if (version != 0 && dec.encoding != bech32::Encoding::BECH32M) {
  105. error_str = "Version 1+ witness address must use Bech32m checksum";
  106. return CNoDestination();
  107. }
  108. // The rest of the symbols are converted witness program bytes.
  109. data.reserve(((dec.data.size() - 1) * 5) / 8);
  110. if (ConvertBits<5, 8, false>([&](unsigned char c) { data.push_back(c); }, dec.data.begin() + 1, dec.data.end())) {
  111. if (version == 0) {
  112. {
  113. WitnessV0KeyHash keyid;
  114. if (data.size() == keyid.size()) {
  115. std::copy(data.begin(), data.end(), keyid.begin());
  116. return keyid;
  117. }
  118. }
  119. {
  120. WitnessV0ScriptHash scriptid;
  121. if (data.size() == scriptid.size()) {
  122. std::copy(data.begin(), data.end(), scriptid.begin());
  123. return scriptid;
  124. }
  125. }
  126. error_str = "Invalid Bech32 v0 address data size";
  127. return CNoDestination();
  128. }
  129. if (version == 1 && data.size() == WITNESS_V1_TAPROOT_SIZE) {
  130. static_assert(WITNESS_V1_TAPROOT_SIZE == WitnessV1Taproot::size());
  131. WitnessV1Taproot tap;
  132. std::copy(data.begin(), data.end(), tap.begin());
  133. return tap;
  134. }
  135. if (version > 16) {
  136. error_str = "Invalid Bech32 address witness version";
  137. return CNoDestination();
  138. }
  139. if (data.size() < 2 || data.size() > BECH32_WITNESS_PROG_MAX_LEN) {
  140. error_str = "Invalid Bech32 address data size";
  141. return CNoDestination();
  142. }
  143. WitnessUnknown unk;
  144. unk.version = version;
  145. std::copy(data.begin(), data.end(), unk.program);
  146. unk.length = data.size();
  147. return unk;
  148. }
  149. }
  150. // Set error message if address can't be interpreted as Base58 or Bech32.
  151. if (error_str.empty()) error_str = "Invalid address format";
  152. return CNoDestination();
  153. }
  154. } // namespace
  155. CKey DecodeSecret(const std::string& str)
  156. {
  157. CKey key;
  158. std::vector<unsigned char> data;
  159. if (DecodeBase58Check(str, data, 34)) {
  160. const std::vector<unsigned char>& privkey_prefix = Params().Base58Prefix(CChainParams::SECRET_KEY);
  161. if ((data.size() == 32 + privkey_prefix.size() || (data.size() == 33 + privkey_prefix.size() && data.back() == 1)) &&
  162. std::equal(privkey_prefix.begin(), privkey_prefix.end(), data.begin())) {
  163. bool compressed = data.size() == 33 + privkey_prefix.size();
  164. key.Set(data.begin() + privkey_prefix.size(), data.begin() + privkey_prefix.size() + 32, compressed);
  165. }
  166. }
  167. if (!data.empty()) {
  168. memory_cleanse(data.data(), data.size());
  169. }
  170. return key;
  171. }
  172. std::string EncodeSecret(const CKey& key)
  173. {
  174. assert(key.IsValid());
  175. std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::SECRET_KEY);
  176. data.insert(data.end(), key.begin(), key.end());
  177. if (key.IsCompressed()) {
  178. data.push_back(1);
  179. }
  180. std::string ret = EncodeBase58Check(data);
  181. memory_cleanse(data.data(), data.size());
  182. return ret;
  183. }
  184. CExtPubKey DecodeExtPubKey(const std::string& str)
  185. {
  186. CExtPubKey key;
  187. std::vector<unsigned char> data;
  188. if (DecodeBase58Check(str, data, 78)) {
  189. const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::EXT_PUBLIC_KEY);
  190. if (data.size() == BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
  191. key.Decode(data.data() + prefix.size());
  192. }
  193. }
  194. return key;
  195. }
  196. std::string EncodeExtPubKey(const CExtPubKey& key)
  197. {
  198. std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::EXT_PUBLIC_KEY);
  199. size_t size = data.size();
  200. data.resize(size + BIP32_EXTKEY_SIZE);
  201. key.Encode(data.data() + size);
  202. std::string ret = EncodeBase58Check(data);
  203. return ret;
  204. }
  205. CExtKey DecodeExtKey(const std::string& str)
  206. {
  207. CExtKey key;
  208. std::vector<unsigned char> data;
  209. if (DecodeBase58Check(str, data, 78)) {
  210. const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
  211. if (data.size() == BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
  212. key.Decode(data.data() + prefix.size());
  213. }
  214. }
  215. return key;
  216. }
  217. std::string EncodeExtKey(const CExtKey& key)
  218. {
  219. std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
  220. size_t size = data.size();
  221. data.resize(size + BIP32_EXTKEY_SIZE);
  222. key.Encode(data.data() + size);
  223. std::string ret = EncodeBase58Check(data);
  224. memory_cleanse(data.data(), data.size());
  225. return ret;
  226. }
  227. std::string EncodeDestination(const CTxDestination& dest)
  228. {
  229. return std::visit(DestinationEncoder(Params()), dest);
  230. }
  231. CTxDestination DecodeDestination(const std::string& str, std::string& error_msg)
  232. {
  233. return DecodeDestination(str, Params(), error_msg);
  234. }
  235. CTxDestination DecodeDestination(const std::string& str)
  236. {
  237. std::string error_msg;
  238. return DecodeDestination(str, error_msg);
  239. }
  240. bool IsValidDestinationString(const std::string& str, const CChainParams& params)
  241. {
  242. std::string error_msg;
  243. return IsValidDestination(DecodeDestination(str, params, error_msg));
  244. }
  245. bool IsValidDestinationString(const std::string& str)
  246. {
  247. return IsValidDestinationString(str, Params());
  248. }