LCOV - code coverage report
Current view: top level - src/util - bip32.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 96.6 % 29 28
Test Date: 2026-04-13 06:23:40 Functions: 100.0 % 3 3
Branches: 76.5 % 34 26

             Branch data     Line data    Source code
       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 <cstdint>
      11                 :             : #include <cstdio>
      12                 :             : #include <optional>
      13                 :             : #include <sstream>
      14                 :             : 
      15                 :          87 : bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath)
      16                 :             : {
      17                 :          87 :     std::stringstream ss(keypath_str);
      18                 :          87 :     std::string item;
      19                 :          87 :     bool first = true;
      20   [ +  -  +  + ]:      114023 :     while (std::getline(ss, item, '/')) {
      21         [ +  + ]:      114004 :         if (item.compare("m") == 0) {
      22         [ +  + ]:           4 :             if (first) {
      23                 :           2 :                 first = false;
      24                 :           2 :                 continue;
      25                 :             :             }
      26                 :             :             return false;
      27                 :             :         }
      28                 :             :         // Finds whether it is hardened
      29                 :      114000 :         uint32_t path = 0;
      30                 :      114000 :         size_t pos = item.find('\'');
      31         [ +  + ]:      114000 :         if (pos != std::string::npos) {
      32                 :             :             // The hardened tick can only be in the last index of the string
      33   [ -  +  +  + ]:        2710 :             if (pos != item.size() - 1) {
      34                 :             :                 return false;
      35                 :             :             }
      36                 :        2704 :             path |= 0x80000000;
      37         [ +  - ]:        2704 :             item = item.substr(0, item.size() - 1); // Drop the last character which is the hardened tick
      38                 :             :         }
      39                 :             : 
      40                 :             :         // Ensure this is only numbers
      41         [ -  + ]:      113994 :         const auto number{ToIntegral<uint32_t>(item)};
      42         [ +  + ]:      113994 :         if (!number) {
      43                 :             :             return false;
      44                 :             :         }
      45         [ +  - ]:      113934 :         path |= *number;
      46                 :             : 
      47         [ +  - ]:      113934 :         keypath.push_back(path);
      48                 :             :         first = false;
      49                 :             :     }
      50                 :             :     return true;
      51                 :          87 : }
      52                 :             : 
      53                 :     3794335 : std::string FormatHDKeypath(const std::vector<uint32_t>& path, bool apostrophe)
      54                 :             : {
      55                 :     3794335 :     std::string ret;
      56         [ +  + ]:     7569556 :     for (auto i : path) {
      57         [ +  - ]:     7550442 :         ret += strprintf("/%i", (i << 1) >> 1);
      58   [ +  +  +  + ]:     5479713 :         if (i >> 31) ret += apostrophe ? '\'' : 'h';
      59                 :             :     }
      60                 :     3794335 :     return ret;
      61                 :           0 : }
      62                 :             : 
      63                 :         692 : std::string WriteHDKeypath(const std::vector<uint32_t>& keypath, bool apostrophe)
      64                 :             : {
      65         [ +  - ]:        1384 :     return "m" + FormatHDKeypath(keypath, apostrophe);
      66                 :             : }
        

Generated by: LCOV version 2.0-1