pub struct PrivateKey { /* private fields */ }Expand description
A secp256k1 private key (256-bit scalar in [1, n-1]).
Uses composition (not inheritance) with BigNumber as the internal representation, following Rust conventions.
Implementations§
Source§impl PrivateKey
impl PrivateKey
Sourcepub fn from_random() -> Result<Self, PrimitivesError>
pub fn from_random() -> Result<Self, PrimitivesError>
Generate a random private key using OS entropy.
Generates 32 random bytes and reduces mod n, ensuring the result is in [1, n-1].
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, PrimitivesError>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, PrimitivesError>
Create a private key from raw bytes (big-endian).
Must be a valid 32-byte scalar in [1, n-1].
Sourcepub fn from_hex(hex: &str) -> Result<Self, PrimitivesError>
pub fn from_hex(hex: &str) -> Result<Self, PrimitivesError>
Create a private key from a hexadecimal string.
The hex string is parsed as a big-endian 256-bit integer. Must be in [1, n-1].
Sourcepub fn from_string(s: &str) -> Result<Self, PrimitivesError>
pub fn from_string(s: &str) -> Result<Self, PrimitivesError>
Create a private key from a string (alias for from_hex).
Sourcepub fn from_wif(wif: &str) -> Result<Self, PrimitivesError>
pub fn from_wif(wif: &str) -> Result<Self, PrimitivesError>
Create a private key from a WIF (Wallet Import Format) string.
WIF format: Base58Check(prefix(1) || key(32) || compression_flag(1)) The compression flag must be 0x01 (we only support compressed keys).
Sourcepub fn to_hex(&self) -> String
pub fn to_hex(&self) -> String
Export the private key as a 64-character zero-padded hex string.
Sourcepub fn to_wif(&self, prefix: &[u8]) -> String
pub fn to_wif(&self, prefix: &[u8]) -> String
Export the private key in WIF (Wallet Import Format).
WIF format: Base58Check(prefix || key(32) || 0x01)
Default prefix is [0x80] for mainnet.
Sourcepub fn to_public_key(&self) -> PublicKey
pub fn to_public_key(&self) -> PublicKey
Derive the corresponding public key.
Computes pubkey = inner * G using the precomputed base point.
Sourcepub fn sign(
&self,
message: &[u8],
force_low_s: bool,
) -> Result<Signature, PrimitivesError>
pub fn sign( &self, message: &[u8], force_low_s: bool, ) -> Result<Signature, PrimitivesError>
Sign a message (raw bytes) using ECDSA.
The message is first hashed with SHA-256, then signed using RFC 6979 deterministic nonce generation.
Compute ECDH shared secret: self.bn * public_key.point.
Returns the resulting point on the curve. The public key must be a valid point on secp256k1.
Sourcepub fn derive_child(
&self,
counterparty: &PublicKey,
invoice_number: &str,
) -> Result<PrivateKey, PrimitivesError>
pub fn derive_child( &self, counterparty: &PublicKey, invoice_number: &str, ) -> Result<PrivateKey, PrimitivesError>
Derive a child private key using Type-42 key derivation (BRC-42).
Computes: child = (self + HMAC-SHA256(shared_secret_compressed, invoice_number)) mod n where shared_secret = self * counterparty.
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