UltrafastSecp256k1 3.50.0
Ultra high-performance secp256k1 elliptic curve cryptography library
Loading...
Searching...
No Matches
coin_hd.hpp
Go to the documentation of this file.
1#ifndef SECP256K1_COINS_COIN_HD_HPP
2#define SECP256K1_COINS_COIN_HD_HPP
3#pragma once
4
5// ============================================================================
6// Coin HD -- BIP-44 Hierarchical Deterministic Derivation per Coin
7// ============================================================================
8// Wraps BIP-32 derivation with coin-aware path construction:
9// m / purpose' / coin_type' / account' / change / address_index
10// (where ' denotes hardened derivation)
11//
12// Standard purposes:
13// 44' -- P2PKH (BIP-44)
14// 49' -- P2SH-P2WPKH (BIP-49)
15// 84' -- P2WPKH Native SegWit (BIP-84)
16// 86' -- P2TR Taproot (BIP-86)
17//
18// Usage:
19// auto master = bip32_master_key(seed, 64);
20// auto key = coin_derive_key(master.first, Bitcoin, 0, false, 0);
21// auto addr = coin_address(key.first.public_key(), Bitcoin);
22//
23// // Ethereum at m/44'/60'/0'/0/0
24// auto eth_key = coin_derive_key(master.first, Ethereum, 0, false, 0);
25// auto eth_addr = coin_address(eth_key.first.public_key(), Ethereum);
26// ============================================================================
27
28#include <string>
29#include <cstdint>
30#include <utility>
31#include "secp256k1/bip32.hpp"
33
34namespace secp256k1::coins {
35
36// -- BIP-44 Purposes ----------------------------------------------------------
37
38enum class DerivationPurpose : std::uint32_t {
39 BIP44 = 44, // P2PKH (legacy)
40 BIP49 = 49, // P2SH-P2WPKH (nested SegWit)
41 BIP84 = 84, // P2WPKH (native SegWit)
42 BIP86 = 86, // P2TR (Taproot)
43};
44
45// -- Path Construction --------------------------------------------------------
46
47// Build BIP-44 derivation path string.
48// Example: coin_derive_path(Bitcoin, 0, false, 0) -> "m/44'/0'/0'/0/0"
49// Example: coin_derive_path(Ethereum, 0, false, 0, BIP44) -> "m/44'/60'/0'/0/0"
50std::string coin_derive_path(const CoinParams& coin,
51 std::uint32_t account = 0,
52 bool change = false,
53 std::uint32_t address_index = 0,
55
56// Select best derivation purpose for a coin.
57// Bitcoin -> BIP84 (native SegWit), Taproot -> BIP86, Legacy -> BIP44
59
60// -- Key Derivation -----------------------------------------------------------
61
62// Derive a coin-specific child key from master key.
63// Automatically selects the best purpose for the coin.
64// Returns {ExtendedKey, success}
65std::pair<ExtendedKey, bool>
67 const CoinParams& coin,
68 std::uint32_t account = 0,
69 bool change = false,
70 std::uint32_t address_index = 0);
71
72// Derive with explicit purpose override.
73std::pair<ExtendedKey, bool>
75 const CoinParams& coin,
76 DerivationPurpose purpose,
77 std::uint32_t account = 0,
78 bool change = false,
79 std::uint32_t address_index = 0);
80
81// -- Convenience: Seed -> Address ----------------------------------------------
82
83// Full pipeline: seed bytes -> BIP-44 derived address string.
84// Returns {address, success}
85std::pair<std::string, bool>
86coin_address_from_seed(const std::uint8_t* seed, std::size_t seed_len,
87 const CoinParams& coin,
88 std::uint32_t account = 0,
89 std::uint32_t address_index = 0);
90
91} // namespace secp256k1::coins
92
93#endif // SECP256K1_COINS_COIN_HD_HPP
std::pair< std::string, bool > coin_address_from_seed(const std::uint8_t *seed, std::size_t seed_len, const CoinParams &coin, std::uint32_t account=0, std::uint32_t address_index=0)
std::pair< ExtendedKey, bool > coin_derive_key_with_purpose(const ExtendedKey &master, const CoinParams &coin, DerivationPurpose purpose, std::uint32_t account=0, bool change=false, std::uint32_t address_index=0)
DerivationPurpose best_purpose(const CoinParams &coin)
std::string coin_derive_path(const CoinParams &coin, std::uint32_t account=0, bool change=false, std::uint32_t address_index=0, DerivationPurpose purpose=DerivationPurpose::BIP44)
std::pair< ExtendedKey, bool > coin_derive_key(const ExtendedKey &master, const CoinParams &coin, std::uint32_t account=0, bool change=false, std::uint32_t address_index=0)