Overview
The HD key derivation APIs provide support for BIP32-compliant hierarchical deterministic wallets. These APIs allow you to derive child keys from parent keys using derivation paths, enabling deterministic key generation from a single seed.HDPrivateKey
Represents a hierarchical deterministic private key with derivation capabilities.Constructor Functions
NewHDPrivateKey
Creates a new HD private key from a seed.The seed bytes used to generate the master key
The network configuration (mainnet, testnet, or regtest)
The generated master HD private key at path “m”
Error if key generation fails
NewMasterHDPrivateKeyFromBytes
Builds an HD private key from raw key and chain code bytes. The data is assumed to correspond to a master key.The compressed private key bytes (32 bytes)
The chain code bytes (32 bytes)
The network configuration
The HD private key at path “m”
NewBasePathHDPrivateKeyFromBytes
Builds an HD private key from raw key and chain code bytes. The data is assumed to correspond to a key derived at Muun’s base pathm/schema:1'/recovery:1'.
The compressed private key bytes
The chain code bytes
The network configuration
The HD private key at path “m/schema:1’/recovery:1‘“
NewHDPrivateKeyFromString
Creates an HD private key from a base58-encoded string (xprv format).Base58-encoded extended private key (e.g., “xprv…”)
The derivation path for this key (e.g., “m/44’/0’/0’”)
The network configuration
The parsed HD private key
Error if the string is invalid or represents a public key
Key Derivation Methods
DerivedAt
Derives a child private key at a specific index. Supports both hardened and non-hardened derivation.The child index (0 to 2^31-1). Must not include the hardened bit
Whether to perform hardened derivation (adds 2^31 to index)
The derived child key with updated path
Error if index is out of bounds or derivation fails
DeriveTo
Derives a key to a specific derivation path. The target path must have the current key’s path as a prefix.The target derivation path (e.g., “m/44’/0’/0’/0/0” or “m/schema:1’/recovery:1’”)
The derived key at the target path
Error if path is invalid, not a prefix, or derivation fails
- Paths start with “m” (master key)
- Use
/to separate levels - Use
'suffix for hardened derivation (e.g.,44') - Named indices are supported (e.g.,
schema:1')
m/44'/0'/0'/0/0- BIP44 Bitcoin address derivationm/schema:1'/recovery:1'- Muun’s base derivation pathm/purpose:84'/coin:0'/account:0'- Named indices for clarity
Utility Methods
PublicKey
Returns the corresponding HD public key.The corresponding public key with the same path
String
Returns the base58-encoded extended private key (xprv format).Base58-encoded extended private key
ECPrivateKey
Returns the underlying EC private key for the current node.The underlying elliptic curve private key
ChainCode
Returns the chain code for this key.The 32-byte chain code
Sign
Signs data using the backing EC private key with SHA-256 hashing.The data to sign (will be SHA-256 hashed)
The DER-encoded ECDSA signature
Encryption Methods
See Encryption APIs forEncrypter(), EncrypterTo(), Decrypter(), and DecrypterFrom() methods.
HDPublicKey
Represents a hierarchical deterministic public key. Public keys can only perform non-hardened derivation.Constructor Functions
NewHDPublicKeyFromString
Creates an HD public key from a base58-encoded string (xpub format).Base58-encoded extended public key (e.g., “xpub…”)
The derivation path for this key
The network configuration
The parsed HD public key
Error if the string is invalid or represents a private key
Derivation Methods
DerivedAt
Derives a child public key at a specific index. Only non-hardened derivation is supported.The child index (0 to 2^31-1, non-hardened only)
The derived child public key
Error if attempting hardened derivation or index is out of bounds
Public keys cannot derive hardened children. Hardened derivation requires the private key.
DeriveTo
Derives a public key to a specific derivation path. All path components must be non-hardened.The target derivation path (must be non-hardened)
The derived public key at the target path
Error if path contains hardened derivation or is invalid
Utility Methods
String
Returns the base58-encoded extended public key (xpub format).Base58-encoded extended public key
Raw
Returns the compressed public key bytes.33-byte compressed public key (02 or 03 prefix + 32-byte x-coordinate)
Fingerprint
Returns the 4-byte fingerprint for this public key (first 4 bytes of HASH160).4-byte key fingerprint
ECPubKey
Returns the underlying EC public key.The underlying elliptic curve public key
ChainCode
Returns the chain code for this key.The 32-byte chain code
Derivation Path Specification
Standard Paths
Muun uses the following derivation paths:- Base Path:
m/schema:1'/recovery:1'- Muun’s base derivation path for wallet keys - BIP44:
m/44'/0'/0'- Standard Bitcoin derivation path - BIP84:
m/84'/0'/0'- Native SegWit (bech32) derivation path
Path Syntax
- Master:
mindicates the master key - Hardened:
'orhsuffix (e.g.,44'or44h) adds 2^31 to the index - Named Indices: Use format
name:index(e.g.,schema:1'is equivalent to1') - Separator:
/separates path levels
Examples
Error Handling
Common Errors
- Invalid Path: Path does not have current key’s path as prefix
- Hardened Derivation: Attempting hardened derivation on public key
- Index Out of Bounds: Index >= 2^31 or negative
- Parse Error: Invalid path syntax or base58 encoding
Best Practices
- Always check error returns from derivation functions
- Use hardened derivation (
') for account-level keys - Use non-hardened derivation for address generation
- Store and track derivation paths for key recovery
- Validate paths before derivation to avoid errors