pub struct PrivateKey { /* private fields */ }Expand description
A secp256k1 private key for signing and key derivation.
Wraps a k256 SigningKey and provides Bitcoin-specific functionality
including WIF serialization, BRC-42 child derivation, and ECDH shared secrets.
Implementations§
Source§impl PrivateKey
impl PrivateKey
Sourcepub fn new() -> Self
pub fn new() -> Self
Generate a new random private key using the OS random number generator.
§Returns
A new randomly generated PrivateKey.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, PrimitivesError>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, PrimitivesError>
Sourcepub fn from_hex(hex_str: &str) -> Result<Self, PrimitivesError>
pub fn from_hex(hex_str: &str) -> Result<Self, PrimitivesError>
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.
Decodes the Base58Check-encoded string, validates the checksum, and extracts the 32-byte private key scalar.
§Arguments
wif- A Base58Check-encoded WIF string (compressed or uncompressed).
§Returns
Ok(PrivateKey) on success, or an error if the WIF is malformed or the checksum fails.
Sourcepub fn to_wif(&self) -> String
pub fn to_wif(&self) -> String
Encode the private key as a WIF string with the mainnet prefix (0x80).
Always encodes for compressed public key format.
§Returns
A Base58Check-encoded WIF string.
Sourcepub fn to_wif_prefix(&self, prefix: u8) -> String
pub fn to_wif_prefix(&self, prefix: u8) -> String
Sourcepub fn to_bytes(&self) -> [u8; 32]
pub fn to_bytes(&self) -> [u8; 32]
Serialize the private key as a 32-byte big-endian array.
§Returns
A 32-byte array containing the private key scalar.
Sourcepub fn to_hex(&self) -> String
pub fn to_hex(&self) -> String
Serialize the private key as a lowercase hexadecimal string.
§Returns
A 64-character hex string representing the 32-byte scalar.
Sourcepub fn pub_key(&self) -> PublicKey
pub fn pub_key(&self) -> PublicKey
Derive the corresponding public key for this private key.
§Returns
The PublicKey corresponding to this private key.
Compute an ECDH shared secret with another public key.
Multiplies the other party’s public key by this private key’s scalar, producing a shared EC point.
§Arguments
pub_key- The other party’s public key.
§Returns
Ok(PublicKey) representing the shared secret point, or an error if the
public key is not on the curve.
Sourcepub fn derive_child(
&self,
pub_key: &PublicKey,
invoice_number: &str,
) -> Result<PrivateKey, PrimitivesError>
pub fn derive_child( &self, pub_key: &PublicKey, invoice_number: &str, ) -> Result<PrivateKey, PrimitivesError>
Derive a child private key using BRC-42 key derivation.
Computes an ECDH shared secret with the provided public key, then uses HMAC-SHA256 with the invoice number to derive a new private key scalar.
See BRC-42 spec: https://github.com/bitcoin-sv/BRCs/blob/master/key-derivation/0042.md
§Arguments
pub_key- The counterparty’s public key.invoice_number- The invoice number string used as HMAC data.
§Returns
Ok(PrivateKey) with the derived child key, or an error if derivation fails.
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