Branch data Line data Source code
1 : : // Copyright (c) 2014-present 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/interpreter.h>
10 : : #include <script/solver.h>
11 : : #include <tinyformat.h>
12 : : #include <util/overflow.h>
13 : : #include <util/strencodings.h>
14 : :
15 : : #include <algorithm>
16 : : #include <cassert>
17 : : #include <cstring>
18 : :
19 : : /// Maximum witness length for Bech32 addresses.
20 : : static constexpr std::size_t BECH32_WITNESS_PROG_MAX_LEN = 40;
21 : :
22 : : namespace {
23 : : class DestinationEncoder
24 : : {
25 : : private:
26 : : const CChainParams& m_params;
27 : :
28 : : public:
29 : 914861 : explicit DestinationEncoder(const CChainParams& params) : m_params(params) {}
30 : :
31 : 99373 : std::string operator()(const PKHash& id) const
32 : : {
33 : 99373 : std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
34 [ + - ]: 99373 : data.insert(data.end(), id.begin(), id.end());
35 [ - + + - ]: 99373 : return EncodeBase58Check(data);
36 : 99373 : }
37 : :
38 : 302441 : std::string operator()(const ScriptHash& id) const
39 : : {
40 : 302441 : std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
41 [ + - ]: 302441 : data.insert(data.end(), id.begin(), id.end());
42 [ - + + - ]: 302441 : return EncodeBase58Check(data);
43 : 302441 : }
44 : :
45 : 151659 : std::string operator()(const WitnessV0KeyHash& id) const
46 : : {
47 : 151659 : std::vector<unsigned char> data = {0};
48 [ + - ]: 151659 : data.reserve(33);
49 [ + - ]: 5004747 : ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end());
50 [ + - ]: 151659 : return bech32::Encode(bech32::Encoding::BECH32, m_params.Bech32HRP(), data);
51 : 151659 : }
52 : :
53 : 151462 : std::string operator()(const WitnessV0ScriptHash& id) const
54 : : {
55 : 151462 : std::vector<unsigned char> data = {0};
56 [ + - ]: 151462 : data.reserve(53);
57 [ + - ]: 8027486 : ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end());
58 [ + - ]: 151462 : return bech32::Encode(bech32::Encoding::BECH32, m_params.Bech32HRP(), data);
59 : 151462 : }
60 : :
61 : 129307 : std::string operator()(const WitnessV1Taproot& tap) const
62 : : {
63 : 129307 : std::vector<unsigned char> data = {1};
64 [ + - ]: 129307 : data.reserve(53);
65 [ + - ]: 6853271 : ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, tap.begin(), tap.end());
66 [ + - ]: 129307 : return bech32::Encode(bech32::Encoding::BECH32M, m_params.Bech32HRP(), data);
67 : 129307 : }
68 : :
69 : 72705 : std::string operator()(const WitnessUnknown& id) const
70 : : {
71 [ + - ]: 72705 : const std::vector<unsigned char>& program = id.GetWitnessProgram();
72 [ + - + - : 145410 : if (id.GetWitnessVersion() < 1 || id.GetWitnessVersion() > 16 || program.size() < 2 || program.size() > 40) {
+ - - + ]
73 : 0 : return {};
74 : : }
75 : 72705 : std::vector<unsigned char> data = {(unsigned char)id.GetWitnessVersion()};
76 [ - + + - ]: 72705 : data.reserve(1 + CeilDiv(program.size() * 8, 5u));
77 [ + - ]: 1594143 : ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, program.begin(), program.end());
78 [ + - ]: 72705 : return bech32::Encode(bech32::Encoding::BECH32M, m_params.Bech32HRP(), data);
79 : 72705 : }
80 : :
81 : 5240 : std::string operator()(const CNoDestination& no) const { return {}; }
82 : 2674 : std::string operator()(const PubKeyDestination& pk) const { return {}; }
83 : : };
84 : :
85 : 13418 : CTxDestination DecodeDestination(const std::string& str, const CChainParams& params, std::string& error_str, std::vector<int>* error_locations)
86 : : {
87 : 13418 : std::vector<unsigned char> data;
88 : 13418 : uint160 hash;
89 [ + - ]: 13418 : error_str = "";
90 : :
91 : : // Note this will be false if it is a valid Bech32 address for a different network
92 [ - + + - : 26836 : bool is_bech32 = (ToLower(str.substr(0, params.Bech32HRP().size())) == params.Bech32HRP());
+ - ]
93 : :
94 [ + + + - : 13418 : if (!is_bech32 && DecodeBase58Check(str, data, 21)) {
+ + ]
95 : : // base58-encoded Bitcoin addresses.
96 : : // Public-key-hash-addresses have version 0 (or 111 testnet).
97 : : // The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
98 [ - + ]: 4236 : const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
99 [ - + - + : 4236 : if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
+ + + + ]
100 : 4018 : std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin());
101 : 4018 : return PKHash(hash);
102 : : }
103 : : // Script-hash-addresses have version 5 (or 196 testnet).
104 : : // The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
105 [ - + ]: 218 : const std::vector<unsigned char>& script_prefix = params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
106 [ - + + + : 218 : if (data.size() == hash.size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
+ + ]
107 : 165 : std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin());
108 : 165 : return ScriptHash(hash);
109 : : }
110 : :
111 : : // If the prefix of data matches either the script or pubkey prefix, the length must have been wrong
112 [ + + ]: 51 : if ((data.size() >= script_prefix.size() &&
113 [ + + + + ]: 53 : std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) ||
114 [ + + + + ]: 52 : (data.size() >= pubkey_prefix.size() &&
115 [ + + ]: 50 : std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin()))) {
116 [ + - ]: 2 : error_str = "Invalid length for Base58 address (P2PKH or P2SH)";
117 : : } else {
118 [ + - ]: 51 : error_str = "Invalid or unsupported Base58-encoded address.";
119 : : }
120 : 53 : return CNoDestination();
121 [ + + ]: 9182 : } else if (!is_bech32) {
122 : : // Try Base58 decoding without the checksum, using a much larger max length
123 [ + - + + ]: 7491 : if (!DecodeBase58(str, data, 100)) {
124 [ + - ]: 1404 : error_str = "Invalid or unsupported Segwit (Bech32) or Base58 encoding.";
125 : : } else {
126 [ + - ]: 6087 : error_str = "Invalid checksum or length of Base58 address (P2PKH or P2SH)";
127 : : }
128 : 7491 : return CNoDestination();
129 : : }
130 : :
131 [ - + ]: 1691 : data.clear();
132 [ + - ]: 1691 : const auto dec = bech32::Decode(str);
133 [ + + ]: 1691 : if (dec.encoding == bech32::Encoding::BECH32 || dec.encoding == bech32::Encoding::BECH32M) {
134 [ + + ]: 675 : if (dec.data.empty()) {
135 [ + - ]: 7 : error_str = "Empty Bech32 data section";
136 : 7 : return CNoDestination();
137 : : }
138 : : // Bech32 decoding
139 [ + + ]: 668 : if (dec.hrp != params.Bech32HRP()) {
140 [ + - ]: 13 : error_str = strprintf("Invalid or unsupported prefix for Segwit (Bech32) address (expected %s, got %s).", params.Bech32HRP(), dec.hrp);
141 : 13 : return CNoDestination();
142 : : }
143 [ + + ]: 655 : int version = dec.data[0]; // The first 5 bit symbol is the witness version (0-16)
144 [ + + + + ]: 655 : if (version == 0 && dec.encoding != bech32::Encoding::BECH32) {
145 [ + - ]: 2 : error_str = "Version 0 witness address must use Bech32 checksum";
146 : 2 : return CNoDestination();
147 : : }
148 [ + + ]: 255 : if (version != 0 && dec.encoding != bech32::Encoding::BECH32M) {
149 [ + - ]: 6 : error_str = "Version 1+ witness address must use Bech32m checksum";
150 : 6 : return CNoDestination();
151 : : }
152 : : // The rest of the symbols are converted witness program bytes.
153 [ - + + - ]: 647 : data.reserve(((dec.data.size() - 1) * 5) / 8);
154 [ + - + + ]: 13227 : if (ConvertBits<5, 8, false>([&](unsigned char c) { data.push_back(c); }, dec.data.begin() + 1, dec.data.end())) {
155 : :
156 [ - + + - ]: 631 : std::string_view byte_str{data.size() == 1 ? "byte" : "bytes"};
157 : :
158 [ + + ]: 631 : if (version == 0) {
159 : 391 : {
160 [ + + ]: 391 : WitnessV0KeyHash keyid;
161 [ + + ]: 391 : if (data.size() == keyid.size()) {
162 : 319 : std::copy(data.begin(), data.end(), keyid.begin());
163 : 319 : return keyid;
164 : : }
165 : : }
166 : 72 : {
167 [ + + ]: 72 : WitnessV0ScriptHash scriptid;
168 [ + + ]: 72 : if (data.size() == scriptid.size()) {
169 : 52 : std::copy(data.begin(), data.end(), scriptid.begin());
170 : 52 : return scriptid;
171 : : }
172 : : }
173 : :
174 [ + - ]: 20 : error_str = strprintf("Invalid Bech32 v0 address program size (%d %s), per BIP141", data.size(), byte_str);
175 : 20 : return CNoDestination();
176 : : }
177 : :
178 [ + + + + ]: 240 : if (version == 1 && data.size() == WITNESS_V1_TAPROOT_SIZE) {
179 : 36 : static_assert(WITNESS_V1_TAPROOT_SIZE == WitnessV1Taproot::size());
180 : 36 : WitnessV1Taproot tap;
181 : 36 : std::copy(data.begin(), data.end(), tap.begin());
182 : 36 : return tap;
183 : : }
184 : :
185 [ + - + + ]: 204 : if (CScript::IsPayToAnchor(version, data)) {
186 [ + - ]: 32 : return PayToAnchor();
187 : : }
188 : :
189 [ - + ]: 172 : if (version > 16) {
190 [ # # ]: 0 : error_str = "Invalid Bech32 address witness version";
191 : 0 : return CNoDestination();
192 : : }
193 : :
194 [ - + + - : 172 : if (data.size() < 2 || data.size() > BECH32_WITNESS_PROG_MAX_LEN) {
+ + ]
195 [ + - ]: 6 : error_str = strprintf("Invalid Bech32 address program size (%d %s)", data.size(), byte_str);
196 : 6 : return CNoDestination();
197 : : }
198 : :
199 [ + - ]: 166 : return WitnessUnknown{version, data};
200 : : } else {
201 [ + - ]: 16 : error_str = strprintf("Invalid padding in Bech32 data section");
202 : 16 : return CNoDestination();
203 : : }
204 : : }
205 : :
206 : : // Perform Bech32 error location
207 [ + - ]: 1016 : auto res = bech32::LocateErrors(str);
208 [ + - ]: 1016 : error_str = res.first;
209 [ + + ]: 1016 : if (error_locations) *error_locations = std::move(res.second);
210 : 1016 : return CNoDestination();
211 : 16125 : }
212 : : } // namespace
213 : :
214 : 137125 : CKey DecodeSecret(const std::string& str)
215 : : {
216 : 137125 : CKey key;
217 : 137125 : std::vector<unsigned char> data;
218 [ + - + + ]: 137125 : if (DecodeBase58Check(str, data, 34)) {
219 [ + - - + ]: 82631 : const std::vector<unsigned char>& privkey_prefix = Params().Base58Prefix(CChainParams::SECRET_KEY);
220 [ - + - + : 82631 : if ((data.size() == 32 + privkey_prefix.size() || (data.size() == 33 + privkey_prefix.size() && data.back() == 1)) &&
+ + + + +
+ + + ]
221 [ + + ]: 82620 : std::equal(privkey_prefix.begin(), privkey_prefix.end(), data.begin())) {
222 : 82612 : bool compressed = data.size() == 33 + privkey_prefix.size();
223 [ + - ]: 82612 : key.Set(data.begin() + privkey_prefix.size(), data.begin() + privkey_prefix.size() + 32, compressed);
224 : : }
225 : : }
226 [ + + ]: 137125 : if (!data.empty()) {
227 [ - + + - ]: 82631 : memory_cleanse(data.data(), data.size());
228 : : }
229 : 137125 : return key;
230 : 137125 : }
231 : :
232 : 143439 : std::string EncodeSecret(const CKey& key)
233 : : {
234 [ - + ]: 143439 : assert(key.IsValid());
235 : 143439 : std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::SECRET_KEY);
236 [ + - + - : 430317 : data.insert(data.end(), UCharCast(key.begin()), UCharCast(key.end()));
+ - ]
237 [ + + ]: 143439 : if (key.IsCompressed()) {
238 [ + - ]: 140713 : data.push_back(1);
239 : : }
240 [ - + + - ]: 143439 : std::string ret = EncodeBase58Check(data);
241 [ - + + - ]: 143439 : memory_cleanse(data.data(), data.size());
242 : 143439 : return ret;
243 : 143439 : }
244 : :
245 : 173705 : CExtPubKey DecodeExtPubKey(const std::string& str)
246 : : {
247 [ + - ]: 173705 : CExtPubKey key;
248 : 173705 : std::vector<unsigned char> data;
249 [ + - + + ]: 173705 : if (DecodeBase58Check(str, data, 78)) {
250 [ + - - + ]: 173020 : const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::EXT_PUBLIC_KEY);
251 [ - + - + : 173020 : if (data.size() == BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
+ + + + ]
252 [ + - ]: 34861 : key.Decode(data.data() + prefix.size());
253 : : }
254 : : }
255 : 173705 : return key;
256 : 173705 : }
257 : :
258 : 2668201 : std::string EncodeExtPubKey(const CExtPubKey& key)
259 : : {
260 : 2668201 : std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::EXT_PUBLIC_KEY);
261 [ - + ]: 2668201 : size_t size = data.size();
262 [ + - ]: 2668201 : data.resize(size + BIP32_EXTKEY_SIZE);
263 [ + - ]: 2668201 : key.Encode(data.data() + size);
264 [ - + + - ]: 2668201 : std::string ret = EncodeBase58Check(data);
265 : 2668201 : return ret;
266 : 2668201 : }
267 : :
268 : 173703 : CExtKey DecodeExtKey(const std::string& str)
269 : : {
270 [ + - ]: 173703 : CExtKey key;
271 : 173703 : std::vector<unsigned char> data;
272 [ + - + + ]: 173703 : if (DecodeBase58Check(str, data, 78)) {
273 [ + - - + ]: 173018 : const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
274 [ - + - + : 173018 : if (data.size() == BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
+ + + + ]
275 [ + - ]: 138113 : key.Decode(data.data() + prefix.size());
276 : : }
277 : : }
278 [ + + ]: 173703 : if (!data.empty()) {
279 [ - + + - ]: 173018 : memory_cleanse(data.data(), data.size());
280 : : }
281 : 173703 : return key;
282 : 173703 : }
283 : :
284 : 556752 : std::string EncodeExtKey(const CExtKey& key)
285 : : {
286 : 556752 : std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
287 [ - + ]: 556752 : size_t size = data.size();
288 [ + - ]: 556752 : data.resize(size + BIP32_EXTKEY_SIZE);
289 [ + - ]: 556752 : key.Encode(data.data() + size);
290 [ - + + - ]: 556752 : std::string ret = EncodeBase58Check(data);
291 [ - + + - ]: 556752 : memory_cleanse(data.data(), data.size());
292 : 556752 : return ret;
293 : 556752 : }
294 : :
295 : 914861 : std::string EncodeDestination(const CTxDestination& dest)
296 : : {
297 : 914861 : return std::visit(DestinationEncoder(Params()), dest);
298 : : }
299 : :
300 : 10949 : CTxDestination DecodeDestination(const std::string& str, std::string& error_msg, std::vector<int>* error_locations)
301 : : {
302 : 10949 : return DecodeDestination(str, Params(), error_msg, error_locations);
303 : : }
304 : :
305 : 10786 : CTxDestination DecodeDestination(const std::string& str)
306 : : {
307 [ + - ]: 10786 : std::string error_msg;
308 [ + - ]: 10786 : return DecodeDestination(str, error_msg);
309 : 10786 : }
310 : :
311 : 2469 : bool IsValidDestinationString(const std::string& str, const CChainParams& params)
312 : : {
313 [ + - ]: 2469 : std::string error_msg;
314 [ + - + - ]: 2469 : return IsValidDestination(DecodeDestination(str, params, error_msg, nullptr));
315 : 2469 : }
316 : :
317 : 2469 : bool IsValidDestinationString(const std::string& str)
318 : : {
319 : 2469 : return IsValidDestinationString(str, Params());
320 : : }
|