rustywallet-keys
Secure secp256k1 private and public key management for cryptocurrency applications with batch generation capabilities.
Features
- 🔐 Secure Key Generation - CSPRNG-based random key generation
- 📥 Multiple Import Formats - Hex, WIF, raw bytes
- 📤 Multiple Export Formats - Hex, WIF, decimal, raw bytes
- 🔑 Public Key Derivation - Compressed and uncompressed formats
- 🛡️ Secure Memory - Automatic zeroization on drop
- ✅ Validation - Comprehensive key validation
- 🔄 Batch Generation - Efficient iterator-based batch key generation
- 🚀 High Performance - Optimized for speed and memory efficiency
- 🌐 Network Support - Bitcoin mainnet and testnet
Installation
Add to your Cargo.toml:
[]
= "0.1.1"
Quick Start
Basic Usage
use *;
// Generate a random private key
let private_key = random;
// Export to various formats
println!;
println!;
println!;
// Derive public key
let public_key = private_key.public_key;
println!;
Batch Generation Example
use *;
// Generate 1000 keys efficiently
let keys: = batch_generate.collect;
// Process keys in batches
for in batch_generate.enumerate
Import/Export Formats
Import Private Key
use *;
// From hex string (64 characters)
let key = from_hex?;
// From WIF (Wallet Import Format)
let key = from_wif?;
// From raw bytes
let bytes = ;
let key = from_bytes?;
// From decimal string
let key = from_decimal?;
Export Formats
use *;
let key = random;
// Hex format (64 characters, lowercase)
let hex = key.to_hex;
// WIF (Wallet Import Format)
let wif_mainnet = key.to_wif; // Starts with 'K' or 'L'
let wif_testnet = key.to_wif; // Starts with 'c'
// Decimal string representation
let decimal = key.to_decimal;
// Raw bytes (32 bytes)
let bytes: = key.to_bytes;
Public Key Operations
use *;
let private_key = random;
let public_key = private_key.public_key;
// Compressed format (33 bytes, starts with 02 or 03)
let compressed = public_key.to_compressed;
let compressed_hex = public_key.to_hex;
// Uncompressed format (65 bytes, starts with 04)
let uncompressed = public_key.to_uncompressed;
let uncompressed_hex = public_key.to_hex;
// Verify key pair
assert!;
Batch Generation
The batch generation feature provides an efficient iterator for generating multiple keys:
use *;
// Generate keys lazily with iterator
let mut key_iter = batch_generate;
// Take first 100 keys
let first_batch: = key_iter.take.collect;
// Generate keys with custom processing
for key in batch_generate
// Memory-efficient batch processing
const BATCH_SIZE: usize = 1000;
let total_keys = 100_000;
for batch_start in .step_by
API Reference
PrivateKey
Creation Methods
PrivateKey::random()- Generate random key using CSPRNGPrivateKey::from_hex(hex: &str)- Import from hex stringPrivateKey::from_wif(wif: &str)- Import from WIF formatPrivateKey::from_bytes(bytes: [u8; 32])- Import from raw bytesPrivateKey::from_decimal(decimal: &str)- Import from decimal stringPrivateKey::batch_generate(count: usize)- Create batch iterator
Export Methods
to_hex(&self) -> String- Export as hex stringto_wif(&self, network: Network) -> String- Export as WIFto_decimal(&self) -> String- Export as decimal stringto_bytes(&self) -> [u8; 32]- Export as raw bytes
Key Operations
public_key(&self) -> PublicKey- Derive public keyis_valid(&self) -> bool- Validate private key
PublicKey
Export Methods
to_hex(&self, format: PublicKeyFormat) -> String- Export as hexto_compressed(&self) -> [u8; 33]- Export compressed formatto_uncompressed(&self) -> [u8; 65]- Export uncompressed formatto_address(&self, network: Network) -> String- Generate address
Verification
verify_private_key(&self, private_key: &PrivateKey) -> bool- Verify key pair
Enums
Network
Network::Mainnet- Bitcoin mainnetNetwork::Testnet- Bitcoin testnet
PublicKeyFormat
PublicKeyFormat::Compressed- 33-byte compressed formatPublicKeyFormat::Uncompressed- 65-byte uncompressed format
Security Notes
Memory Security
- Private keys are automatically zeroized when dropped using the
zeroizecrate - Sensitive data is cleared from memory to prevent recovery
- No private key data is left in memory after use
Cryptographic Security
- Uses the battle-tested
secp256k1crate for all cryptographic operations - Random number generation uses OS-level CSPRNG (
OsRng) - All keys are validated according to secp256k1 curve parameters
Best Practices
- Always validate imported keys before use
- Use secure channels when transmitting keys
- Store private keys encrypted when persisting to disk
- Never log or print private keys in production
- Use testnet for development and testing
Threat Model
This library protects against:
- Memory dumps revealing private keys after use
- Invalid key generation or import
- Weak random number generation
This library does NOT protect against:
- Side-channel attacks during key operations
- Physical access to running memory
- Malware with memory access privileges
- Social engineering attacks
License
MIT License - see LICENSE for details.
Contributing
Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.
Part of rustywallet
This crate is part of the rustywallet ecosystem, providing secure and efficient cryptocurrency key management tools.