Coverage Report

Created: 2026-11-13 17:38

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