Skip to main content

PrivateKey

Struct PrivateKey 

Source
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

Source

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].

Source

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].

Source

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].

Source

pub fn from_string(s: &str) -> Result<Self, PrimitivesError>

Create a private key from a string (alias for from_hex).

Source

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).

Source

pub fn to_hex(&self) -> String

Export the private key as a 64-character zero-padded hex string.

Source

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.

Source

pub fn to_public_key(&self) -> PublicKey

Derive the corresponding public key.

Computes pubkey = inner * G using the precomputed base point.

Source

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.

Source

pub fn to_bytes(&self) -> Vec<u8>

Get the private key as 32 big-endian bytes.

Source

pub fn bn(&self) -> &BigNumber

Access the underlying BigNumber.

Source

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

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.

Source

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

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 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.

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> 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.