Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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 <tinyformat.h>
6
#include <util/bip32.h>
7
#include <util/strencodings.h>
8
9
#include <cstdint>
10
#include <cstdio>
11
#include <sstream>
12
13
bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath)
14
0
{
15
0
    std::stringstream ss(keypath_str);
16
0
    std::string item;
17
0
    bool first = true;
18
0
    while (std::getline(ss, item, '/')) {
  Branch (18:12): [True: 0, False: 0]
19
0
        if (item.compare("m") == 0) {
  Branch (19:13): [True: 0, False: 0]
20
0
            if (first) {
  Branch (20:17): [True: 0, False: 0]
21
0
                first = false;
22
0
                continue;
23
0
            }
24
0
            return false;
25
0
        }
26
        // Finds whether it is hardened
27
0
        uint32_t path = 0;
28
0
        size_t pos = item.find('\'');
29
0
        if (pos != std::string::npos) {
  Branch (29:13): [True: 0, False: 0]
30
            // The hardened tick can only be in the last index of the string
31
0
            if (pos != item.size() - 1) {
  Branch (31:17): [True: 0, False: 0]
32
0
                return false;
33
0
            }
34
0
            path |= 0x80000000;
35
0
            item = item.substr(0, item.size() - 1); // Drop the last character which is the hardened tick
36
0
        }
37
38
        // Ensure this is only numbers
39
0
        const auto number{ToIntegral<uint32_t>(item)};
40
0
        if (!number) {
  Branch (40:13): [True: 0, False: 0]
41
0
            return false;
42
0
        }
43
0
        path |= *number;
44
45
0
        keypath.push_back(path);
46
0
        first = false;
47
0
    }
48
0
    return true;
49
0
}
50
51
std::string FormatHDKeypath(const std::vector<uint32_t>& path, bool apostrophe)
52
354k
{
53
354k
    std::string ret;
54
1.41M
    for (auto i : path) {
  Branch (54:17): [True: 1.41M, False: 354k]
55
1.41M
        ret += strprintf("/%i", (i << 1) >> 1);
56
1.41M
        if (i >> 31) ret += apostrophe ? '\'' : 'h';
  Branch (56:13): [True: 1.06M, False: 354k]
  Branch (56:29): [True: 266k, False: 798k]
57
1.41M
    }
58
354k
    return ret;
59
354k
}
60
61
std::string WriteHDKeypath(const std::vector<uint32_t>& keypath, bool apostrophe)
62
0
{
63
0
    return "m" + FormatHDKeypath(keypath, apostrophe);
64
0
}