Skip to main content

PrivateKey

Struct PrivateKey 

Source
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

Source

pub fn new() -> Self

Generate a new random private key using the OS random number generator.

§Returns

A new randomly generated PrivateKey.

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self, PrimitivesError>

Create a private key from raw 32-byte scalar.

§Arguments
  • bytes - A 32-byte slice representing the private key scalar.
§Returns

Ok(PrivateKey) if the bytes represent a valid scalar on secp256k1, or an error if the scalar is zero or out of range.

Source

pub fn from_hex(hex_str: &str) -> Result<Self, PrimitivesError>

Create a private key from a hexadecimal string.

§Arguments
  • hex_str - A 64-character hex string representing the 32-byte scalar.
§Returns

Ok(PrivateKey) on success, or an error if the hex is invalid or the scalar is invalid.

Source

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.

Source

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.

Source

pub fn to_wif_prefix(&self, prefix: u8) -> String

Encode the private key as a WIF string with a custom network prefix.

Always encodes for compressed public key format.

§Arguments
  • prefix - The network prefix byte (0x80 for mainnet, 0xef for testnet).
§Returns

A Base58Check-encoded WIF string.

Source

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.

Source

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.

Source

pub fn pub_key(&self) -> PublicKey

Derive the corresponding public key for this private key.

§Returns

The PublicKey corresponding to this private key.

Source

pub fn sign(&self, hash: &[u8]) -> Result<Signature, PrimitivesError>

Sign a message hash using deterministic RFC6979 nonces.

The input should be a pre-computed hash (typically 32 bytes). Produces a low-S normalized signature per BIP-0062.

§Arguments
  • hash - The message hash to sign (should be 32 bytes).
§Returns

Ok(Signature) on success, or an error if signing fails.

Source

pub fn derive_shared_secret( &self, pub_key: &PublicKey, ) -> Result<PublicKey, PrimitivesError>

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.

Source

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

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

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

impl Default for PrivateKey

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for PrivateKey

Source§

fn eq(&self, other: &Self) -> 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