rustywallet-keys
Type-safe private and public key management for cryptocurrency wallets.
This crate provides ergonomic APIs for working with secp256k1 keys, with a focus on security and developer experience.
Features
- Key Generation: Generate cryptographically secure random private keys
- Multiple Formats: Import/export keys in hex, WIF, and raw bytes
- Public Key Derivation: Derive public keys in compressed or uncompressed format
- Secure by Default: Private keys are zeroized on drop, debug output is masked
- Type Safety: Strong typing prevents common mistakes at compile time
Quick Start
use *;
// Generate a random private key
let private_key = random;
// Export to various formats
let hex = private_key.to_hex;
let wif = private_key.to_wif;
let bytes = private_key.to_bytes;
// Derive public key
let public_key = private_key.public_key;
let compressed = public_key.to_hex;
let uncompressed = public_key.to_hex;
Import Existing Keys
use *;
// From hex string
let hex = "0000000000000000000000000000000000000000000000000000000000000001";
let key = from_hex.unwrap;
// From WIF (Wallet Import Format)
let wif = "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ";
let key = from_wif.unwrap;
// From raw bytes
let bytes = ;
let key = from_bytes.unwrap;
Error Handling
All fallible operations return Result types with descriptive errors:
use *;
// Invalid hex string
let result = from_hex;
assert!;
// Zero key (invalid)
let zero_bytes = ;
assert!;
Security
This crate takes security seriously:
- Private keys are automatically zeroized when dropped
- Debug output shows
PrivateKey(****)instead of actual key data - Uses the battle-tested
secp256k1crate for cryptographic operations
Modules
- [
private_key] - Private key generation, import, and export - [
public_key] - Public key derivation and format conversion - [
network] - Network type (Mainnet/Testnet) for WIF encoding - [
error] - Error types for all operations - [
prelude] - Convenient re-exports for common types