pub struct PrivateKey { /* private fields */ }Expand description
A secp256k1 private key with secure memory handling.
This struct wraps a secp256k1::SecretKey and provides convenient methods
for key generation, import, and export. The key is automatically zeroized
when dropped for security.
§Example
use rustywallet_keys::private_key::PrivateKey;
use rustywallet_keys::network::Network;
// Generate a random key
let key = PrivateKey::random();
// Export to hex
let hex = key.to_hex();
assert_eq!(hex.len(), 64);
// Export to WIF
let wif = key.to_wif(Network::Mainnet);
assert!(wif.starts_with('K') || wif.starts_with('L'));Implementations§
Source§impl PrivateKey
impl PrivateKey
Sourcepub fn random() -> PrivateKey
pub fn random() -> PrivateKey
Generate a new random private key using a cryptographically secure RNG.
This method uses the operating system’s secure random number generator and automatically regenerates if an invalid key is produced (which is extremely unlikely).
§Example
use rustywallet_keys::private_key::PrivateKey;
let key = PrivateKey::random();Sourcepub fn from_bytes(bytes: [u8; 32]) -> Result<PrivateKey, PrivateKeyError>
pub fn from_bytes(bytes: [u8; 32]) -> Result<PrivateKey, PrivateKeyError>
Create a private key from a 32-byte array.
§Errors
Returns PrivateKeyError::OutOfRange if the bytes represent a value
that is zero or greater than or equal to the curve order.
§Example
use rustywallet_keys::private_key::PrivateKey;
let bytes = [1u8; 32];
let key = PrivateKey::from_bytes(bytes).unwrap();Sourcepub fn is_valid(bytes: &[u8; 32]) -> bool
pub fn is_valid(bytes: &[u8; 32]) -> bool
Check if a 32-byte array represents a valid private key.
A valid private key must be non-zero and less than the secp256k1 curve order.
§Example
use rustywallet_keys::private_key::PrivateKey;
let valid_bytes = [1u8; 32];
assert!(PrivateKey::is_valid(&valid_bytes));
let zero_bytes = [0u8; 32];
assert!(!PrivateKey::is_valid(&zero_bytes));Sourcepub fn from_hex(hex_str: &str) -> Result<PrivateKey, PrivateKeyError>
pub fn from_hex(hex_str: &str) -> Result<PrivateKey, PrivateKeyError>
Create a private key from a hex string.
The hex string must be exactly 64 characters (32 bytes). Both uppercase and lowercase characters are accepted.
§Errors
PrivateKeyError::InvalidLengthif the hex string is not 64 charactersPrivateKeyError::InvalidHexif the string contains invalid hex charactersPrivateKeyError::OutOfRangeif the decoded value is invalid
§Example
use rustywallet_keys::private_key::PrivateKey;
let hex = "0000000000000000000000000000000000000000000000000000000000000001";
let key = PrivateKey::from_hex(hex).unwrap();Sourcepub fn from_wif(wif_str: &str) -> Result<PrivateKey, PrivateKeyError>
pub fn from_wif(wif_str: &str) -> Result<PrivateKey, PrivateKeyError>
Create a private key from a WIF (Wallet Import Format) string.
§Errors
PrivateKeyError::InvalidWifif the WIF format is invalidPrivateKeyError::InvalidChecksumif the checksum doesn’t matchPrivateKeyError::OutOfRangeif the decoded key is invalid
§Example
use rustywallet_keys::private_key::PrivateKey;
let wif = "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ";
let key = PrivateKey::from_wif(wif).unwrap();Sourcepub fn to_bytes(&self) -> [u8; 32]
pub fn to_bytes(&self) -> [u8; 32]
Export the private key as a 32-byte array.
§Example
use rustywallet_keys::private_key::PrivateKey;
let key = PrivateKey::random();
let bytes = key.to_bytes();
assert_eq!(bytes.len(), 32);Sourcepub fn to_hex(&self) -> String
pub fn to_hex(&self) -> String
Export the private key as a lowercase hex string.
§Example
use rustywallet_keys::private_key::PrivateKey;
let key = PrivateKey::random();
let hex = key.to_hex();
assert_eq!(hex.len(), 64);Sourcepub fn to_wif(&self, network: Network) -> String
pub fn to_wif(&self, network: Network) -> String
Export the private key as a WIF (Wallet Import Format) string.
The WIF format includes the network version byte and uses compressed public key format by default.
§Example
use rustywallet_keys::private_key::PrivateKey;
use rustywallet_keys::network::Network;
let key = PrivateKey::random();
let wif = key.to_wif(Network::Mainnet);
assert!(wif.starts_with('K') || wif.starts_with('L'));Sourcepub fn to_decimal(&self) -> String
pub fn to_decimal(&self) -> String
Export the private key as a decimal string.
This converts the 256-bit key to its decimal representation.
§Example
use rustywallet_keys::private_key::PrivateKey;
let key = PrivateKey::from_hex(
"0000000000000000000000000000000000000000000000000000000000000001"
).unwrap();
assert_eq!(key.to_decimal(), "1");Sourcepub fn public_key(&self) -> PublicKey
pub fn public_key(&self) -> PublicKey
Derive the corresponding public key.
§Example
use rustywallet_keys::private_key::PrivateKey;
let private_key = PrivateKey::random();
let public_key = private_key.public_key();Trait Implementations§
Source§impl Clone for PrivateKey
impl Clone for PrivateKey
Source§fn clone(&self) -> PrivateKey
fn clone(&self) -> PrivateKey
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more