LCOV - code coverage report
Current view: top level - src/util - bip32.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 97.5 % 40 39
Test Date: 2024-09-01 05:20:30 Functions: 100.0 % 3 3
Branches: 75.0 % 40 30

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2019-2022 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                 :         149 : bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath)
      14                 :             : {
      15                 :         149 :     std::stringstream ss(keypath_str);
      16                 :         149 :     std::string item;
      17                 :         149 :     bool first = true;
      18   [ +  -  +  -  :      401479 :     while (std::getline(ss, item, '/')) {
                   +  + ]
      19         [ +  + ]:      401443 :         if (item.compare("m") == 0) {
      20         [ +  + ]:           3 :             if (first) {
      21                 :           2 :                 first = false;
      22                 :           2 :                 continue;
      23                 :             :             }
      24                 :           1 :             return false;
      25                 :             :         }
      26                 :             :         // Finds whether it is hardened
      27                 :      401440 :         uint32_t path = 0;
      28                 :      401440 :         size_t pos = item.find('\'');
      29         [ +  + ]:      401440 :         if (pos != std::string::npos) {
      30                 :             :             // The hardened tick can only be in the last index of the string
      31         [ +  + ]:        5678 :             if (pos != item.size() - 1) {
      32                 :          13 :                 return false;
      33                 :             :             }
      34                 :        5665 :             path |= 0x80000000;
      35         [ -  + ]:        5665 :             item = item.substr(0, item.size() - 1); // Drop the last character which is the hardened tick
      36                 :        5665 :         }
      37                 :             : 
      38                 :             :         // Ensure this is only numbers
      39         [ +  + ]:      401427 :         if (item.find_first_not_of( "0123456789" ) != std::string::npos) {
      40                 :          48 :             return false;
      41                 :             :         }
      42                 :      401379 :         uint32_t number;
      43   [ +  -  +  + ]:      401379 :         if (!ParseUInt32(item, &number)) {
      44                 :          51 :             return false;
      45                 :             :         }
      46                 :      401328 :         path |= number;
      47                 :             : 
      48         [ +  - ]:      401328 :         keypath.push_back(path);
      49                 :      401328 :         first = false;
      50         [ +  + ]:      401440 :     }
      51                 :          36 :     return true;
      52                 :         149 : }
      53                 :             : 
      54                 :     4669811 : std::string FormatHDKeypath(const std::vector<uint32_t>& path, bool apostrophe)
      55                 :             : {
      56                 :     4669811 :     std::string ret;
      57         [ +  + ]:    13762158 :     for (auto i : path) {
      58   [ +  -  -  + ]:     9092347 :         ret += strprintf("/%i", (i << 1) >> 1);
      59   [ +  +  -  + ]:     9092347 :         if (i >> 31) ret += apostrophe ? '\'' : 'h';
      60                 :     9092347 :     }
      61                 :     4669811 :     return ret;
      62         [ +  - ]:     4669811 : }
      63                 :             : 
      64                 :         568 : std::string WriteHDKeypath(const std::vector<uint32_t>& keypath, bool apostrophe)
      65                 :             : {
      66         [ +  - ]:         568 :     return "m" + FormatHDKeypath(keypath, apostrophe);
      67                 :           0 : }
        

Generated by: LCOV version 2.0-1