Coverage Report

Created: 2026-04-29 19:21

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 <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
97.0k
    explicit DestinationEncoder(const CChainParams& params) : m_params(params) {}
30
31
    std::string operator()(const PKHash& id) const
32
30.5k
    {
33
30.5k
        std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
34
30.5k
        data.insert(data.end(), id.begin(), id.end());
35
30.5k
        return EncodeBase58Check(data);
36
30.5k
    }
37
38
    std::string operator()(const ScriptHash& id) const
39
14.6k
    {
40
14.6k
        std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
41
14.6k
        data.insert(data.end(), id.begin(), id.end());
42
14.6k
        return EncodeBase58Check(data);
43
14.6k
    }
44
45
    std::string operator()(const WitnessV0KeyHash& id) const
46
35.4k
    {
47
35.4k
        std::vector<unsigned char> data = {0};
48
35.4k
        data.reserve(33);
49
1.13M
        ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end());
50
35.4k
        return bech32::Encode(bech32::Encoding::BECH32, m_params.Bech32HRP(), data);
51
35.4k
    }
52
53
    std::string operator()(const WitnessV0ScriptHash& id) const
54
739
    {
55
739
        std::vector<unsigned char> data = {0};
56
739
        data.reserve(53);
57
38.4k
        ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end());
58
739
        return bech32::Encode(bech32::Encoding::BECH32, m_params.Bech32HRP(), data);
59
739
    }
60
61
    std::string operator()(const WitnessV1Taproot& tap) const
62
15.6k
    {
63
15.6k
        std::vector<unsigned char> data = {1};
64
15.6k
        data.reserve(53);
65
811k
        ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, tap.begin(), tap.end());
66
15.6k
        return bech32::Encode(bech32::Encoding::BECH32M, m_params.Bech32HRP(), data);
67
15.6k
    }
68
69
    std::string operator()(const WitnessUnknown& id) const
70
122
    {
71
122
        const std::vector<unsigned char>& program = id.GetWitnessProgram();
72
122
        if (id.GetWitnessVersion() < 1 || id.GetWitnessVersion() > 16 || program.size() < 2 || program.size() > 40) {
73
0
            return {};
74
0
        }
75
122
        std::vector<unsigned char> data = {(unsigned char)id.GetWitnessVersion()};
76
122
        data.reserve(1 + CeilDiv(program.size() * 8, 5u));
77
2.39k
        ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, program.begin(), program.end());
78
122
        return bech32::Encode(bech32::Encoding::BECH32M, m_params.Bech32HRP(), data);
79
122
    }
80
81
0
    std::string operator()(const CNoDestination& no) const { return {}; }
82
0
    std::string operator()(const PubKeyDestination& pk) const { return {}; }
83
};
84
85
CTxDestination DecodeDestination(const std::string& str, const CChainParams& params, std::string& error_str, std::vector<int>* error_locations)
86
23.1k
{
87
23.1k
    std::vector<unsigned char> data;
88
23.1k
    uint160 hash;
89
23.1k
    error_str = "";
90
91
    // Note this will be false if it is a valid Bech32 address for a different network
92
23.1k
    bool is_bech32 = (ToLower(str.substr(0, params.Bech32HRP().size())) == params.Bech32HRP());
93
94
23.1k
    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
9.43k
        const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
99
9.43k
        if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
100
8.06k
            std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin());
101
8.06k
            return PKHash(hash);
102
8.06k
        }
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
1.37k
        const std::vector<unsigned char>& script_prefix = params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
106
1.37k
        if (data.size() == hash.size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
107
1.35k
            std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin());
108
1.35k
            return ScriptHash(hash);
109
1.35k
        }
110
111
        // If the prefix of data matches either the script or pubkey prefix, the length must have been wrong
112
17
        if ((data.size() >= script_prefix.size() &&
113
17
                std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) ||
114
17
            (data.size() >= pubkey_prefix.size() &&
115
17
                std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin()))) {
116
0
            error_str = "Invalid length for Base58 address (P2PKH or P2SH)";
117
17
        } else {
118
17
            error_str = "Invalid or unsupported Base58-encoded address.";
119
17
        }
120
17
        return CNoDestination();
121
13.6k
    } else if (!is_bech32) {
122
        // Try Base58 decoding without the checksum, using a much larger max length
123
273
        if (!DecodeBase58(str, data, 100)) {
124
129
            error_str = "Invalid or unsupported Segwit (Bech32) or Base58 encoding.";
125
144
        } else {
126
144
            error_str = "Invalid checksum or length of Base58 address (P2PKH or P2SH)";
127
144
        }
128
273
        return CNoDestination();
129
273
    }
130
131
13.4k
    data.clear();
132
13.4k
    const auto dec = bech32::Decode(str);
133
13.4k
    if (dec.encoding == bech32::Encoding::BECH32 || dec.encoding == bech32::Encoding::BECH32M) {
134
13.3k
        if (dec.data.empty()) {
135
5
            error_str = "Empty Bech32 data section";
136
5
            return CNoDestination();
137
5
        }
138
        // Bech32 decoding
139
13.3k
        if (dec.hrp != params.Bech32HRP()) {
140
10
            error_str = strprintf("Invalid or unsupported prefix for Segwit (Bech32) address (expected %s, got %s).", params.Bech32HRP(), dec.hrp);
141
10
            return CNoDestination();
142
10
        }
143
13.3k
        int version = dec.data[0]; // The first 5 bit symbol is the witness version (0-16)
144
13.3k
        if (version == 0 && dec.encoding != bech32::Encoding::BECH32) {
145
8
            error_str = "Version 0 witness address must use Bech32 checksum";
146
8
            return CNoDestination();
147
8
        }
148
13.3k
        if (version != 0 && dec.encoding != bech32::Encoding::BECH32M) {
149
15
            error_str = "Version 1+ witness address must use Bech32m checksum";
150
15
            return CNoDestination();
151
15
        }
152
        // The rest of the symbols are converted witness program bytes.
153
13.3k
        data.reserve(((dec.data.size() - 1) * 5) / 8);
154
281k
        if (ConvertBits<5, 8, false>([&](unsigned char c) { data.push_back(c); }, dec.data.begin() + 1, dec.data.end())) {
155
156
13.3k
            std::string_view byte_str{data.size() == 1 ? "byte" : "bytes"};
157
158
13.3k
            if (version == 0) {
159
12.3k
                {
160
12.3k
                    WitnessV0KeyHash keyid;
161
12.3k
                    if (data.size() == keyid.size()) {
162
12.0k
                        std::copy(data.begin(), data.end(), keyid.begin());
163
12.0k
                        return keyid;
164
12.0k
                    }
165
12.3k
                }
166
316
                {
167
316
                    WitnessV0ScriptHash scriptid;
168
316
                    if (data.size() == scriptid.size()) {
169
307
                        std::copy(data.begin(), data.end(), scriptid.begin());
170
307
                        return scriptid;
171
307
                    }
172
316
                }
173
174
9
                error_str = strprintf("Invalid Bech32 v0 address program size (%d %s), per BIP141", data.size(), byte_str);
175
9
                return CNoDestination();
176
316
            }
177
178
985
            if (version == 1 && data.size() == WITNESS_V1_TAPROOT_SIZE) {
179
933
                static_assert(WITNESS_V1_TAPROOT_SIZE == WitnessV1Taproot::size());
180
933
                WitnessV1Taproot tap;
181
933
                std::copy(data.begin(), data.end(), tap.begin());
182
933
                return tap;
183
933
            }
184
185
52
            if (CScript::IsPayToAnchor(version, data)) {
186
5
                return PayToAnchor();
187
5
            }
188
189
47
            if (version > 16) {
190
8
                error_str = "Invalid Bech32 address witness version";
191
8
                return CNoDestination();
192
8
            }
193
194
39
            if (data.size() < 2 || data.size() > BECH32_WITNESS_PROG_MAX_LEN) {
195
12
                error_str = strprintf("Invalid Bech32 address program size (%d %s)", data.size(), byte_str);
196
12
                return CNoDestination();
197
12
            }
198
199
27
            return WitnessUnknown{version, data};
200
39
        } else {
201
6
            error_str = strprintf("Invalid padding in Bech32 data section");
202
6
            return CNoDestination();
203
6
        }
204
13.3k
    }
205
206
    // Perform Bech32 error location
207
21
    auto res = bech32::LocateErrors(str);
208
21
    error_str = res.first;
209
21
    if (error_locations) *error_locations = std::move(res.second);
210
21
    return CNoDestination();
211
13.4k
}
212
} // namespace
213
214
CKey DecodeSecret(const std::string& str)
215
1.96k
{
216
1.96k
    CKey key;
217
1.96k
    std::vector<unsigned char> data;
218
1.96k
    if (DecodeBase58Check(str, data, 34)) {
219
1.37k
        const std::vector<unsigned char>& privkey_prefix = Params().Base58Prefix(CChainParams::SECRET_KEY);
220
1.37k
        if ((data.size() == 32 + privkey_prefix.size() || (data.size() == 33 + privkey_prefix.size() && data.back() == 1)) &&
221
1.37k
            std::equal(privkey_prefix.begin(), privkey_prefix.end(), data.begin())) {
222
1.33k
            bool compressed = data.size() == 33 + privkey_prefix.size();
223
1.33k
            key.Set(data.begin() + privkey_prefix.size(), data.begin() + privkey_prefix.size() + 32, compressed);
224
1.33k
        }
225
1.37k
    }
226
1.96k
    if (!data.empty()) {
227
1.37k
        memory_cleanse(data.data(), data.size());
228
1.37k
    }
229
1.96k
    return key;
230
1.96k
}
231
232
std::string EncodeSecret(const CKey& key)
233
256
{
234
256
    assert(key.IsValid());
235
256
    std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::SECRET_KEY);
236
256
    data.insert(data.end(), UCharCast(key.begin()), UCharCast(key.end()));
237
256
    if (key.IsCompressed()) {
238
230
        data.push_back(1);
239
230
    }
240
256
    std::string ret = EncodeBase58Check(data);
241
256
    memory_cleanse(data.data(), data.size());
242
256
    return ret;
243
256
}
244
245
CExtPubKey DecodeExtPubKey(const std::string& str)
246
8.00k
{
247
8.00k
    CExtPubKey key;
248
8.00k
    std::vector<unsigned char> data;
249
8.00k
    if (DecodeBase58Check(str, data, 78)) {
250
8.00k
        const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::EXT_PUBLIC_KEY);
251
8.00k
        if (data.size() == BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
252
7.15k
            key.Decode(data.data() + prefix.size());
253
7.15k
        }
254
8.00k
    }
255
8.00k
    return key;
256
8.00k
}
257
258
std::string EncodeExtPubKey(const CExtPubKey& key)
259
143k
{
260
143k
    std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::EXT_PUBLIC_KEY);
261
143k
    size_t size = data.size();
262
143k
    data.resize(size + BIP32_EXTKEY_SIZE);
263
143k
    key.Encode(data.data() + size);
264
143k
    std::string ret = EncodeBase58Check(data);
265
143k
    return ret;
266
143k
}
267
268
CExtKey DecodeExtKey(const std::string& str)
269
8.00k
{
270
8.00k
    CExtKey key;
271
8.00k
    std::vector<unsigned char> data;
272
8.00k
    if (DecodeBase58Check(str, data, 78)) {
273
7.99k
        const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
274
7.99k
        if (data.size() == BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
275
859
            key.Decode(data.data() + prefix.size());
276
859
        }
277
7.99k
    }
278
8.00k
    if (!data.empty()) {
279
7.99k
        memory_cleanse(data.data(), data.size());
280
7.99k
    }
281
8.00k
    return key;
282
8.00k
}
283
284
std::string EncodeExtKey(const CExtKey& key)
285
995
{
286
995
    std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
287
995
    size_t size = data.size();
288
995
    data.resize(size + BIP32_EXTKEY_SIZE);
289
995
    key.Encode(data.data() + size);
290
995
    std::string ret = EncodeBase58Check(data);
291
995
    memory_cleanse(data.data(), data.size());
292
995
    return ret;
293
995
}
294
295
std::string EncodeDestination(const CTxDestination& dest)
296
97.0k
{
297
97.0k
    return std::visit(DestinationEncoder(Params()), dest);
298
97.0k
}
299
300
CTxDestination DecodeDestination(const std::string& str, std::string& error_msg, std::vector<int>* error_locations)
301
23.0k
{
302
23.0k
    return DecodeDestination(str, Params(), error_msg, error_locations);
303
23.0k
}
304
305
CTxDestination DecodeDestination(const std::string& str)
306
22.1k
{
307
22.1k
    std::string error_msg;
308
22.1k
    return DecodeDestination(str, error_msg);
309
22.1k
}
310
311
bool IsValidDestinationString(const std::string& str, const CChainParams& params)
312
11
{
313
11
    std::string error_msg;
314
11
    return IsValidDestination(DecodeDestination(str, params, error_msg, nullptr));
315
11
}
316
317
bool IsValidDestinationString(const std::string& str)
318
11
{
319
11
    return IsValidDestinationString(str, Params());
320
11
}