Coverage Report

Created: 2026-11-13 17:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/util/bip32.cpp
Line
Count
Source
1
// Copyright (c) 2019-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 <util/bip32.h>
6
7
#include <tinyformat.h>
8
#include <util/strencodings.h>
9
10
#include <algorithm>
11
#include <cstdint>
12
#include <optional>
13
#include <span>
14
#include <sstream>
15
#include <string_view>
16
17
util::Expected<KeyPathElement, std::string> ParseKeyPathElement(std::span<const char> elem)
18
33.9k
{
19
33.9k
    const std::string_view raw{elem.begin(), elem.end()};
20
33.9k
    if (elem.empty()) {
21
10
        return util::Unexpected{strprintf("Key path value '%s' is not valid", raw)};
22
10
    }
23
24
33.9k
    bool is_hardened = false;
25
33.9k
    const char last = elem.back();
26
33.9k
    if (last == '\'' || last == 'h') {
27
24.4k
        elem = elem.first(elem.size() - 1);
28
24.4k
        is_hardened = true;
29
24.4k
    }
30
31
33.9k
    const auto number{ToIntegral<uint32_t>(std::string_view{elem.begin(), elem.end()})};
32
33.9k
    if (!number) {
33
38
        return util::Unexpected{strprintf("Key path value '%s' is not a valid uint32", raw)};
34
38
    }
35
33.8k
    if (*number >= BIP32_HARDENED_FLAG) {
36
11
        return util::Unexpected{strprintf("Key path value %u is out of range", *number)};
37
11
    }
38
33.8k
    return KeyPathElement{*number, is_hardened};
39
33.8k
}
40
41
bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath)
42
174
{
43
174
    std::stringstream ss(keypath_str);
44
174
    std::string item;
45
174
    bool first = true;
46
1.05k
    while (std::getline(ss, item, '/') || std::getline(ss, item, 'h')) {
47
923
        if (item.compare("m") == 0) {
48
112
            if (first) {
49
112
                first = false;
50
112
                continue;
51
112
            }
52
0
            return false;
53
112
        }
54
811
        const auto parsed{ParseKeyPathElement(std::span<const char>{item.data(), item.size()})};
55
811
        if (!parsed) return false;
56
770
        keypath.push_back(parsed->ChildNumber());
57
770
        first = false;
58
770
    }
59
133
    return true;
60
174
}
61
62
std::string FormatHDKeypath(const std::vector<uint32_t>& path, bool apostrophe)
63
221k
{
64
221k
    std::string ret;
65
662k
    for (auto i : path) {
66
662k
        ret += strprintf("/%i", (i << 1) >> 1);
67
662k
        if (i >> 31) ret += apostrophe ? '\'' : 'h';
68
662k
    }
69
221k
    return ret;
70
221k
}
71
72
std::string WriteHDKeypath(const std::vector<uint32_t>& keypath, bool apostrophe)
73
2.96k
{
74
2.96k
    return "m" + FormatHDKeypath(keypath, apostrophe);
75
2.96k
}
76
77
bool HasHardenedDerivation(std::span<const uint32_t> keypath)
78
29.1k
{
79
29.1k
    return std::any_of(keypath.begin(), keypath.end(), [](uint32_t index) {
80
28.9k
        return index >> 31;
81
28.9k
    });
82
29.1k
}