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: 2025-06-01 05:27:21 Functions: 100.0 % 3 3
Branches: 80.0 % 30 24

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

Generated by: LCOV version 2.0-1