PrivateKey

Struct PrivateKey 

Source
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

Source

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();
Source

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();
Source

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));
Source

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
§Example
use rustywallet_keys::private_key::PrivateKey;

let hex = "0000000000000000000000000000000000000000000000000000000000000001";
let key = PrivateKey::from_hex(hex).unwrap();
Source

pub fn from_wif(wif_str: &str) -> Result<PrivateKey, PrivateKeyError>

Create a private key from a WIF (Wallet Import Format) string.

§Errors
§Example
use rustywallet_keys::private_key::PrivateKey;

let wif = "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ";
let key = PrivateKey::from_wif(wif).unwrap();
Source

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);
Source

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);
Source

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'));
Source

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");
Source

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

Source§

fn clone(&self) -> PrivateKey

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PrivateKey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Drop for PrivateKey

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl PartialEq for PrivateKey

Source§

fn eq(&self, other: &PrivateKey) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for PrivateKey

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V