Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2019-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_UTIL_BIP32_H6#define BITCOIN_UTIL_BIP32_H8#include <cstdint>9#include <span>10#include <string>11#include <util/expected.h>12#include <vector>14/** BIP32 unhardened derivation index (no high bit set) */15static constexpr uint32_t BIP32_UNHARDENED_FLAG = 0x0;16/** BIP32 hardened derivation flag (2^31) */17static constexpr uint32_t BIP32_HARDENED_FLAG = 0x80000000;19struct KeyPathElement {20/** Derivation index, without the hardened flag */21uint32_t index;22bool is_hardened;24/** Derivation index with the hardened flag applied */25uint32_t ChildNumber() const { return index | (is_hardened ? BIP32_HARDENED_FLAG : BIP32_UNHARDENED_FLAG); }26};28/** Parse a single key path element like "0", "0'", or "0h".29* Returns the derivation index and hardened status, or an error message. */30util::Expected<KeyPathElement, std::string> ParseKeyPathElement(std::span<const char> elem);32/** Parse an HD keypaths like "m/7/0'/2000". */33[[nodiscard]] bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath);35/** Write HD keypaths as strings */36std::string WriteHDKeypath(const std::vector<uint32_t>& keypath, bool apostrophe = false);37std::string FormatHDKeypath(const std::vector<uint32_t>& path, bool apostrophe = false);39/** Whether a parsed HD keypath contains at least one hardened derivation step. */40bool HasHardenedDerivation(std::span<const uint32_t> keypath);42#endif // BITCOIN_UTIL_BIP32_H