/Users/brunogarcia/projects/bitcoin-core-dev/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 <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 | | bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath) |
16 | 0 | { |
17 | 0 | std::stringstream ss(keypath_str); |
18 | 0 | std::string item; |
19 | 0 | bool first = true; |
20 | 0 | while (std::getline(ss, item, '/')) { |
21 | 0 | if (item.compare("m") == 0) { |
22 | 0 | if (first) { |
23 | 0 | first = false; |
24 | 0 | continue; |
25 | 0 | } |
26 | 0 | return false; |
27 | 0 | } |
28 | | // Finds whether it is hardened |
29 | 0 | uint32_t path = 0; |
30 | 0 | size_t pos = item.find('\''); |
31 | 0 | if (pos != std::string::npos) { |
32 | | // The hardened tick can only be in the last index of the string |
33 | 0 | if (pos != item.size() - 1) { |
34 | 0 | return false; |
35 | 0 | } |
36 | 0 | path |= 0x80000000; |
37 | 0 | item = item.substr(0, item.size() - 1); // Drop the last character which is the hardened tick |
38 | 0 | } |
39 | | |
40 | | // Ensure this is only numbers |
41 | 0 | const auto number{ToIntegral<uint32_t>(item)}; |
42 | 0 | if (!number) { |
43 | 0 | return false; |
44 | 0 | } |
45 | 0 | path |= *number; |
46 | |
|
47 | 0 | keypath.push_back(path); |
48 | 0 | first = false; |
49 | 0 | } |
50 | 0 | return true; |
51 | 0 | } |
52 | | |
53 | | std::string FormatHDKeypath(const std::vector<uint32_t>& path, bool apostrophe) |
54 | 0 | { |
55 | 0 | std::string ret; |
56 | 0 | for (auto i : path) { |
57 | 0 | ret += strprintf("/%i", (i << 1) >> 1);Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
58 | 0 | if (i >> 31) ret += apostrophe ? '\'' : 'h'; |
59 | 0 | } |
60 | 0 | return ret; |
61 | 0 | } |
62 | | |
63 | | std::string WriteHDKeypath(const std::vector<uint32_t>& keypath, bool apostrophe) |
64 | 0 | { |
65 | 0 | return "m" + FormatHDKeypath(keypath, apostrophe); |
66 | 0 | } |