Skip to main content

PrivateKey

Struct PrivateKey 

Source
pub struct PrivateKey(/* private fields */);
Expand description

A secret key used for signing and hashing.

This type has a safe Debug implementation that does not leak information. Additionally, it implements Drop to zeroize the memory to make leaking passwords less likely.

Implementations§

Source§

impl PrivateKey

Source

pub fn from_raw(raw: [u8; 32]) -> Result<Self, InvalidPrivateKey>

Creates a new private key from raw bytes.

Source

pub fn from_slice<B: AsRef<[u8]>>(raw: B) -> Result<Self, InvalidPrivateKey>

Creates a new private key from a slice of bytes.

Source

pub fn from_hex_str<S: AsRef<str>>(s: S) -> Result<Self, InvalidPrivateKey>

Creates a new private key from a hex string representation. Accepts hex string with or without leading "0x".

Source

pub fn public_address(&self) -> Address

Gets the public address for a given private key.

Methods from Deref<Target = SecretKey>§

Source

pub fn display_secret(&self) -> DisplaySecret

Formats the explicit byte value of the secret key kept inside the type as a little-endian hexadecimal string using the provided formatter.

This is the only method that outputs the actual secret key value, and, thus, should be used with extreme caution.

§Examples
use secp256k1::SecretKey;
let key = SecretKey::from_str("0000000000000000000000000000000000000000000000000000000000000001").unwrap();

// Normal debug hides value (`Display` is not implemented for `SecretKey`).
// E.g., `format!("{:?}", key)` prints "SecretKey(#2518682f7819fb2d)".

// Here we explicitly display the secret value:
assert_eq!(
    "0000000000000000000000000000000000000000000000000000000000000001",
    format!("{}", key.display_secret())
);
// Also, we can explicitly display with `Debug`:
assert_eq!(
    format!("{:?}", key.display_secret()),
    format!("DisplaySecret(\"{}\")", key.display_secret())
);
Source

pub fn secret_bytes(&self) -> [u8; 32]

Returns the secret key as a byte value.

Source

pub fn keypair<C>(&self, secp: &Secp256k1<C>) -> KeyPair
where C: Signing,

Returns the KeyPair for this SecretKey.

This is equivalent to using KeyPair::from_secret_key.

Source

pub fn public_key<C>(&self, secp: &Secp256k1<C>) -> PublicKey
where C: Signing,

Returns the PublicKey for this SecretKey.

This is equivalent to using PublicKey::from_secret_key.

Source

pub fn x_only_public_key<C>( &self, secp: &Secp256k1<C>, ) -> (XOnlyPublicKey, Parity)
where C: Signing,

Returns the XOnlyPublicKey (and it’s Parity) for this SecretKey.

This is equivalent to XOnlyPublicKey::from_keypair(self.keypair(secp)).

Trait Implementations§

Source§

impl Clone for PrivateKey

Source§

fn clone(&self) -> PrivateKey

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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 Deref for PrivateKey

Source§

type Target = SecretKey

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl FromStr for PrivateKey

Source§

type Err = InvalidPrivateKey

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Key for &PrivateKey

Source§

fn sign( &self, message: &[u8], chain_id: Option<u64>, ) -> Result<Signature, SigningError>

Sign given message and include chain-id replay protection. Read more
Source§

fn sign_message(&self, message: &[u8]) -> Result<Signature, SigningError>

Sign given message without manipulating V-value; used for typed transactions (AccessList and EIP-1559)
Source§

fn address(&self) -> Address

Get public address that this key represents.

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Key for T
where T: Deref<Target = SecretKey>,

Source§

fn sign( &self, message: &[u8], chain_id: Option<u64>, ) -> Result<Signature, SigningError>

Sign given message and include chain-id replay protection. Read more
Source§

fn sign_message(&self, message: &[u8]) -> Result<Signature, SigningError>

Sign given message without manipulating V-value; used for typed transactions (AccessList and EIP-1559)
Source§

fn address(&self) -> H160

Get public address that this key represents.
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more