LCOV - code coverage report
Current view: top level - src/util - bip32.cpp (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 33 37 89.2 %
Date: 2026-06-25 07:23:51 Functions: 2 3 66.7 %

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

Generated by: LCOV version 1.16