1 // Copyright (c) 2014-2018 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
5 #include <key_io.h>
6
7 #include <base58.h>
8 #include <bech32.h>
9 #include <script/script.h>
10 #include <util/strencodings.h>
11
12 #include <boost/variant/apply_visitor.hpp>
13 #include <boost/variant/static_visitor.hpp>
14
15 #include <assert.h>
16 #include <string.h>
17 #include <algorithm>
18
19 namespace
20 {
21 class DestinationEncoder : public boost::static_visitor<std::string>
22 {
23 private:
24 const CChainParams& m_params;
25
26 public:
DestinationEncoder(const CChainParams & params)27 explicit DestinationEncoder(const CChainParams& params) : m_params(params) {}
28
operator ()(const CKeyID & id) const29 std::string operator()(const CKeyID& id) const
30 {
31 std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
32 data.insert(data.end(), id.begin(), id.end());
33 return EncodeBase58Check(data);
34 }
35
operator ()(const CScriptID & id) const36 std::string operator()(const CScriptID& id) const
37 {
38 std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::SCRIPT_ADDRESS2);
39 data.insert(data.end(), id.begin(), id.end());
40 return EncodeBase58Check(data);
41 }
42
operator ()(const WitnessV0KeyHash & id) const43 std::string operator()(const WitnessV0KeyHash& id) const
44 {
45 std::vector<unsigned char> data = {0};
46 data.reserve(33);
47 ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end());
48 return bech32::Encode(m_params.Bech32HRP(), data);
49 }
50
operator ()(const WitnessV0ScriptHash & id) const51 std::string operator()(const WitnessV0ScriptHash& id) const
52 {
53 std::vector<unsigned char> data = {0};
54 data.reserve(53);
55 ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end());
56 return bech32::Encode(m_params.Bech32HRP(), data);
57 }
58
operator ()(const WitnessUnknown & id) const59 std::string operator()(const WitnessUnknown& id) const
60 {
61 if (id.version < 1 || id.version > 16 || id.length < 2 || id.length > 40) {
62 return {};
63 }
64 std::vector<unsigned char> data = {(unsigned char)id.version};
65 data.reserve(1 + (id.length * 8 + 4) / 5);
66 ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.program, id.program + id.length);
67 return bech32::Encode(m_params.Bech32HRP(), data);
68 }
69
operator ()(const CNoDestination & no) const70 std::string operator()(const CNoDestination& no) const { return {}; }
71 };
72
DecodeDestination(const std::string & str,const CChainParams & params)73 CTxDestination DecodeDestination(const std::string& str, const CChainParams& params)
74 {
75 std::vector<unsigned char> data;
76 uint160 hash;
77 if (DecodeBase58Check(str, data)) {
78 // base58-encoded Bitcoin addresses.
79 // Public-key-hash-addresses have version 0 (or 111 testnet).
80 // The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
81 const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
82 if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
83 std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin());
84 return CKeyID(hash);
85 }
86 // Script-hash-addresses have version 5 for 3 prefix (or 196 testnet).
87 // The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
88 const std::vector<unsigned char>& script_prefix = params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
89 if (data.size() == hash.size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
90 std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin());
91 return CScriptID(hash);
92 }
93 // Script-hash-addresses have version 5 for M prefix (or 196 testnet).
94 // The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
95 const std::vector<unsigned char>& script_prefix2 = params.Base58Prefix(CChainParams::SCRIPT_ADDRESS2);
96 if (data.size() == hash.size() + script_prefix2.size() && std::equal(script_prefix2.begin(), script_prefix2.end(), data.begin())) {
97 std::copy(data.begin() + script_prefix2.size(), data.end(), hash.begin());
98 return CScriptID(hash);
99 }
100 }
101 data.clear();
102 auto bech = bech32::Decode(str);
103 if (bech.second.size() > 0 && bech.first == params.Bech32HRP()) {
104 // Bech32 decoding
105 int version = bech.second[0]; // The first 5 bit symbol is the witness version (0-16)
106 // The rest of the symbols are converted witness program bytes.
107 data.reserve(((bech.second.size() - 1) * 5) / 8);
108 if (ConvertBits<5, 8, false>([&](unsigned char c) { data.push_back(c); }, bech.second.begin() + 1, bech.second.end())) {
109 if (version == 0) {
110 {
111 WitnessV0KeyHash keyid;
112 if (data.size() == keyid.size()) {
113 std::copy(data.begin(), data.end(), keyid.begin());
114 return keyid;
115 }
116 }
117 {
118 WitnessV0ScriptHash scriptid;
119 if (data.size() == scriptid.size()) {
120 std::copy(data.begin(), data.end(), scriptid.begin());
121 return scriptid;
122 }
123 }
124 return CNoDestination();
125 }
126 if (version > 16 || data.size() < 2 || data.size() > 40) {
127 return CNoDestination();
128 }
129 WitnessUnknown unk;
130 unk.version = version;
131 std::copy(data.begin(), data.end(), unk.program);
132 unk.length = data.size();
133 return unk;
134 }
135 }
136 return CNoDestination();
137 }
138 } // namespace
139
DecodeSecret(const std::string & str)140 CKey DecodeSecret(const std::string& str)
141 {
142 CKey key;
143 std::vector<unsigned char> data;
144 if (DecodeBase58Check(str, data)) {
145 const std::vector<unsigned char>& privkey_prefix = Params().Base58Prefix(CChainParams::SECRET_KEY);
146 if ((data.size() == 32 + privkey_prefix.size() || (data.size() == 33 + privkey_prefix.size() && data.back() == 1)) &&
147 std::equal(privkey_prefix.begin(), privkey_prefix.end(), data.begin())) {
148 bool compressed = data.size() == 33 + privkey_prefix.size();
149 key.Set(data.begin() + privkey_prefix.size(), data.begin() + privkey_prefix.size() + 32, compressed);
150 }
151 }
152 if (!data.empty()) {
153 memory_cleanse(data.data(), data.size());
154 }
155 return key;
156 }
157
EncodeSecret(const CKey & key)158 std::string EncodeSecret(const CKey& key)
159 {
160 assert(key.IsValid());
161 std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::SECRET_KEY);
162 data.insert(data.end(), key.begin(), key.end());
163 if (key.IsCompressed()) {
164 data.push_back(1);
165 }
166 std::string ret = EncodeBase58Check(data);
167 memory_cleanse(data.data(), data.size());
168 return ret;
169 }
170
DecodeExtPubKey(const std::string & str)171 CExtPubKey DecodeExtPubKey(const std::string& str)
172 {
173 CExtPubKey key;
174 std::vector<unsigned char> data;
175 if (DecodeBase58Check(str, data)) {
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
EncodeExtPubKey(const CExtPubKey & key)184 std::string EncodeExtPubKey(const CExtPubKey& key)
185 {
186 std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::EXT_PUBLIC_KEY);
187 size_t size = data.size();
188 data.resize(size + BIP32_EXTKEY_SIZE);
189 key.Encode(data.data() + size);
190 std::string ret = EncodeBase58Check(data);
191 return ret;
192 }
193
DecodeExtKey(const std::string & str)194 CExtKey DecodeExtKey(const std::string& str)
195 {
196 CExtKey key;
197 std::vector<unsigned char> data;
198 if (DecodeBase58Check(str, data)) {
199 const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
200 if (data.size() == BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
201 key.Decode(data.data() + prefix.size());
202 }
203 }
204 return key;
205 }
206
EncodeExtKey(const CExtKey & key)207 std::string EncodeExtKey(const CExtKey& key)
208 {
209 std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
210 size_t size = data.size();
211 data.resize(size + BIP32_EXTKEY_SIZE);
212 key.Encode(data.data() + size);
213 std::string ret = EncodeBase58Check(data);
214 memory_cleanse(data.data(), data.size());
215 return ret;
216 }
217
EncodeDestination(const CTxDestination & dest)218 std::string EncodeDestination(const CTxDestination& dest)
219 {
220 return boost::apply_visitor(DestinationEncoder(Params()), dest);
221 }
222
DecodeDestination(const std::string & str)223 CTxDestination DecodeDestination(const std::string& str)
224 {
225 return DecodeDestination(str, Params());
226 }
227
IsValidDestinationString(const std::string & str,const CChainParams & params)228 bool IsValidDestinationString(const std::string& str, const CChainParams& params)
229 {
230 return IsValidDestination(DecodeDestination(str, params));
231 }
232
IsValidDestinationString(const std::string & str)233 bool IsValidDestinationString(const std::string& str)
234 {
235 return IsValidDestinationString(str, Params());
236 }
237