rustywallet_keys/lib.rs
1//! # rustywallet-keys
2//!
3//! Type-safe private and public key management for cryptocurrency wallets.
4//!
5//! This crate provides ergonomic APIs for working with secp256k1 keys,
6//! with a focus on security and developer experience.
7//!
8//! ## Features
9//!
10//! - **Key Generation**: Generate cryptographically secure random private keys
11//! - **Multiple Formats**: Import/export keys in hex, WIF, and raw bytes
12//! - **Public Key Derivation**: Derive public keys in compressed or uncompressed format
13//! - **Secure by Default**: Private keys are zeroized on drop, debug output is masked
14//! - **Type Safety**: Strong typing prevents common mistakes at compile time
15//!
16//! ## Quick Start
17//!
18//! ```rust
19//! use rustywallet_keys::prelude::*;
20//!
21//! // Generate a random private key
22//! let private_key = PrivateKey::random();
23//!
24//! // Export to various formats
25//! let hex = private_key.to_hex();
26//! let wif = private_key.to_wif(Network::Mainnet);
27//! let bytes = private_key.to_bytes();
28//!
29//! // Derive public key
30//! let public_key = private_key.public_key();
31//! let compressed = public_key.to_hex(PublicKeyFormat::Compressed);
32//! let uncompressed = public_key.to_hex(PublicKeyFormat::Uncompressed);
33//! ```
34//!
35//! ## Import Existing Keys
36//!
37//! ```rust
38//! use rustywallet_keys::prelude::*;
39//!
40//! // From hex string
41//! let hex = "0000000000000000000000000000000000000000000000000000000000000001";
42//! let key = PrivateKey::from_hex(hex).unwrap();
43//!
44//! // From WIF (Wallet Import Format)
45//! let wif = "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ";
46//! let key = PrivateKey::from_wif(wif).unwrap();
47//!
48//! // From raw bytes
49//! let bytes = [1u8; 32];
50//! let key = PrivateKey::from_bytes(bytes).unwrap();
51//! ```
52//!
53//! ## Error Handling
54//!
55//! All fallible operations return `Result` types with descriptive errors:
56//!
57//! ```rust
58//! use rustywallet_keys::prelude::*;
59//!
60//! // Invalid hex string
61//! let result = PrivateKey::from_hex("invalid");
62//! assert!(result.is_err());
63//!
64//! // Zero key (invalid)
65//! let zero_bytes = [0u8; 32];
66//! assert!(!PrivateKey::is_valid(&zero_bytes));
67//! ```
68//!
69//! ## Security
70//!
71//! This crate takes security seriously:
72//!
73//! - Private keys are automatically zeroized when dropped
74//! - Debug output shows `PrivateKey(****)` instead of actual key data
75//! - Uses the battle-tested `secp256k1` crate for cryptographic operations
76//!
77//! ## Modules
78//!
79//! - [`private_key`] - Private key generation, import, and export
80//! - [`public_key`] - Public key derivation and format conversion
81//! - [`network`] - Network type (Mainnet/Testnet) for WIF encoding
82//! - [`error`] - Error types for all operations
83//! - [`prelude`] - Convenient re-exports for common types
84
85pub mod error;
86pub mod network;
87pub mod prelude;
88pub mod private_key;
89pub mod public_key;
90
91mod encoding;