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 : : #ifndef BITCOIN_UTIL_BIP32_H
6 : : #define BITCOIN_UTIL_BIP32_H
7 : :
8 : : #include <cstdint>
9 : : #include <span>
10 : : #include <string>
11 : : #include <util/expected.h>
12 : : #include <vector>
13 : :
14 : : /** BIP32 unhardened derivation index (no high bit set) */
15 : : static constexpr uint32_t BIP32_UNHARDENED_FLAG = 0x0;
16 : : /** BIP32 hardened derivation flag (2^31) */
17 : : static constexpr uint32_t BIP32_HARDENED_FLAG = 0x80000000;
18 : :
19 : : struct KeyPathElement {
20 : : /** Derivation index, without the hardened flag */
21 : : uint32_t index;
22 : : bool is_hardened;
23 : :
24 : : /** Derivation index with the hardened flag applied */
25 [ + + + - ]: 39524 : uint32_t ChildNumber() const { return index | (is_hardened ? BIP32_HARDENED_FLAG : BIP32_UNHARDENED_FLAG); }
[ + + ]
26 : : };
27 : :
28 : : /** Parse a single key path element like "0", "0'", or "0h".
29 : : * Returns the derivation index and hardened status, or an error message. */
30 : : util::Expected<KeyPathElement, std::string> ParseKeyPathElement(std::span<const char> elem);
31 : :
32 : : /** Parse an HD keypaths like "m/7/0'/2000". */
33 : : [[nodiscard]] bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath);
34 : :
35 : : /** Write HD keypaths as strings */
36 : : std::string WriteHDKeypath(const std::vector<uint32_t>& keypath, bool apostrophe = false);
37 : : std::string FormatHDKeypath(const std::vector<uint32_t>& path, bool apostrophe = false);
38 : :
39 : : /** Whether a parsed HD keypath contains at least one hardened derivation step. */
40 : : bool HasHardenedDerivation(std::span<const uint32_t> keypath);
41 : :
42 : : #endif // BITCOIN_UTIL_BIP32_H
|