HD Wallets
Overview
The HD Wallets component implements the BSV Key Derivation Scheme (BKDS) as defined in BRC-42 and BRC-43. It provides privacy-enhanced key derivation for generating unique keys per transaction, counterparty, and protocol while maintaining a single master key for recovery.
The KeyDeriver class is the core implementation that enables deterministic key generation using protocol IDs, key IDs, and counterparty information, ensuring that keys are never reused and maintaining privacy across different contexts.
Purpose
HD Wallets in the BSV SDK solve several critical problems:
Privacy: Generate unique keys for each transaction context without address reuse
Deterministic Recovery: Recover all keys from a single master key using BIP-32 derivation
Protocol Isolation: Separate keys by security level and protocol to prevent cross-contamination
Counterparty-Specific Keys: Derive unique keys for each counterparty relationship
Symmetric Key Support: Generate shared encryption keys for secure communications
This component is essential for building privacy-respecting BSV applications that comply with modern key management standards.
Basic Usage
Initialize Key Deriver
import { KeyDeriver, PrivateKey } from '@bsv/sdk';
// Initialize with a master private key
const rootKey = PrivateKey.fromRandom();
const keyDeriver = new KeyDeriver(rootKey);
// Or use an existing master key
const masterKey = PrivateKey.fromWif('L5EY1SbTvvPNSdCYQe1EJHfXCBBT4PmnF6CDbzCm9iifZptUvDGB');
const deriver = new KeyDeriver(masterKey);Derive Public Keys
Derive Private Keys
Derive Symmetric Keys
Key Features
1. BRC-42 Compliant Key Derivation
The SDK implements the complete BRC-42 specification for hierarchical deterministic key derivation:
2. Counterparty-Specific Derivation
Generate unique keys for each relationship or counterparty:
3. Protocol Isolation
Separate keys by protocol to prevent cross-contamination:
4. Master Key Recovery
Recover all derived keys from a single master key:
API Reference
KeyDeriver Class
The main class for hierarchical deterministic key derivation.
Constructor
Parameters:
rootKey: The master private key for all derivationsidentityKey(optional): Custom identity key for specialized derivation paths
derivePublicKey()
Derives a public key for a specific protocol, key ID, and counterparty context.
Parameters:
protocolID: Tuple of[securityLevel, protocolName]securityLevel: 0-4 per BRC-43protocolName: String identifier for the protocol
keyID: Unique identifier within the protocol contextcounterparty: Public key of counterparty, or 'self'/'anyone'forSelf: Whether the key is for yourself (true) or counterparty (false)
Returns: Derived PublicKey
Example:
derivePrivateKey()
Derives a private key for a specific context. Only derive private keys for keys you control.
Parameters:
protocolID: Tuple of[securityLevel, protocolName]keyID: Unique identifier within the protocol contextcounterparty: Public key of counterparty, or 'self'/'anyone'
Returns: Derived PrivateKey
Example:
deriveSymmetricKey()
Derives a symmetric encryption key using ECDH key agreement.
Parameters:
protocolID: Tuple of[securityLevel, protocolName]keyID: Unique identifier within the protocol contextcounterparty: Public key of the other party
Returns: Array of numbers representing the symmetric key bytes
Example:
revealCounterpartySecret()
Reveals the shared secret with a counterparty (use carefully for debugging or specific protocols).
Parameters:
counterparty: Public key of the counterparty
Returns: Array of numbers representing the shared secret
revealSpecificSecret()
Reveals the specific secret for a given derivation path.
Parameters:
protocolID: Tuple of[securityLevel, protocolName]keyID: Unique identifier within the protocol contextcounterparty: Public key of counterparty, or 'self'/'anyone'
Returns: Array of numbers representing the specific secret
Common Patterns
Pattern 1: Payment Address Generation
Generate unique payment addresses per invoice without address reuse:
Pattern 2: Secure Messaging with Symmetric Keys
Implement end-to-end encrypted messaging using derived symmetric keys:
Pattern 3: Multi-Protocol Wallet with Change Addresses
Manage multiple protocols with proper change address handling:
Security Considerations
Master Key Protection
The master key is the single point of failure for HD wallets:
Security Level Selection
Choose appropriate security levels per BRC-43:
Counterparty Validation
Always validate counterparty public keys:
Key ID Uniqueness
Ensure key IDs are truly unique:
Performance Considerations
Key Derivation Caching
Cache derived keys to avoid repeated computation:
Batch Key Derivation
Derive multiple keys efficiently:
Related Components
Transactions: Use derived keys in transactions
P2PKH: P2PKH template for derived addresses
Signatures: Sign with derived private keys
Best Practices
1. Master Key Management
2. Protocol Versioning
3. Key ID Documentation
4. Counterparty Context
5. Backup and Recovery
Troubleshooting
Issue: Derived Keys Don't Match After Recovery
Problem: Keys derived after recovery don't match original keys.
Solution:
Issue: Performance Degradation with Many Derivations
Problem: Application slows down when deriving many keys.
Solution:
Issue: Incorrect Counterparty Key Type
Problem: Error when passing counterparty key to derivation.
Solution:
Issue: Protocol Name Collision
Problem: Different protocols accidentally use the same name.
Solution:
Further Reading
BRC Standards
BRC-42: BSV Key Derivation Scheme (BKDS)
BRC-43: Security Levels and Protocol IDs
BRC-44: Protocol Security Requirements
Related Documentation
SDK Wallet Documentation: Official KeyDeriver API
BIP-32 Specification: Hierarchical Deterministic Wallets (original)
BIP-39 Specification: Mnemonic code for generating deterministic keys
Tutorials and Examples
BSV SDK Examples: Complete working examples
Key Derivation Guide: Step-by-step tutorial
Privacy Best Practices: Using HD wallets for privacy
Status
✅ Complete - Comprehensive documentation with BRC-42/43 implementation details, security considerations, and production-ready examples.
Last updated
