bitcoin::key

Struct Keypair

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

Opaque data structure that holds a keypair consisting of a secret and a public key.

§Serde support

Implements de/serialization with the serde and_global-context features enabled. Serializes the secret bytes only. We treat the byte value as a tuple of 32 u8s for non-human-readable formats. This representation is optimal for for some formats (e.g. bincode) however other formats may be less optimal (e.g. cbor). For human-readable formats we use a hex string.

§Examples

Basic usage:

use secp256k1::{rand, Keypair, Secp256k1};

let secp = Secp256k1::new();
let (secret_key, public_key) = secp.generate_keypair(&mut rand::thread_rng());
let keypair = Keypair::from_secret_key(&secp, &secret_key);

Implementations§

Source§

impl Keypair

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

§Example
use secp256k1::{Keypair, Secp256k1, SecretKey};

let secp = Secp256k1::new();
let key = SecretKey::from_str("0000000000000000000000000000000000000000000000000000000000000001").unwrap();
let key = Keypair::from_secret_key(&secp, &key);
// 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§

impl Keypair

Source

pub fn cmp_fast_unstable(&self, other: &Keypair) -> Ordering

Like cmp::Cmp but faster and with no guarantees across library versions.

The Cmp implementation for FFI types is stable but slow because it first serializes self and other before comparing them. This function provides a faster comparison if you know that your types come from the same library version.

Source

pub fn eq_fast_unstable(&self, other: &Keypair) -> bool

Like cmp::Eq but faster and with no guarantees across library versions.

The Eq implementation for FFI types is stable but slow because it first serializes self and other before comparing them. This function provides a faster equality check if you know that your types come from the same library version.

Source§

impl Keypair

Source

pub fn as_ptr(&self) -> *const Keypair

👎Deprecated since 0.25.0: Use Self::as_c_ptr if you need to access the FFI layer

Obtains a raw const pointer suitable for use with FFI functions.

Source

pub fn as_mut_ptr(&mut self) -> *mut Keypair

👎Deprecated since 0.25.0: Use Self::as_mut_c_ptr if you need to access the FFI layer

Obtains a raw mutable pointer suitable for use with FFI functions.

Source

pub fn from_secret_key<C>(secp: &Secp256k1<C>, sk: &SecretKey) -> Keypair
where C: Signing,

Creates a Keypair directly from a Secp256k1 secret key.

Source

pub fn from_seckey_slice<C>( secp: &Secp256k1<C>, data: &[u8], ) -> Result<Keypair, Error>
where C: Signing,

Creates a Keypair directly from a secret key slice.

§Errors

Error::InvalidSecretKey if the provided data has an incorrect length, exceeds Secp256k1 field p value or the corresponding public key is not even.

Source

pub fn from_seckey_str<C>( secp: &Secp256k1<C>, s: &str, ) -> Result<Keypair, Error>
where C: Signing,

Creates a Keypair directly from a secret key string.

§Errors

Error::InvalidSecretKey if corresponding public key for the provided secret key is not even.

Source

pub fn new<R, C>(secp: &Secp256k1<C>, rng: &mut R) -> Keypair
where R: Rng + ?Sized, C: Signing,

Available on crate feature rand only.

Generates a new random secret key.

§Examples
use secp256k1::{rand, Secp256k1, SecretKey, Keypair};

let secp = Secp256k1::new();
let keypair = Keypair::new(&secp, &mut rand::thread_rng());
Source

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

Returns the secret bytes for this key pair.

Source

pub fn add_xonly_tweak<C>( self, secp: &Secp256k1<C>, tweak: &Scalar, ) -> Result<Keypair, Error>
where C: Verification,

Tweaks a keypair by first converting the public key to an xonly key and tweaking it.

§Errors

Returns an error if the resulting key would be invalid.

NB: Will not error if the tweaked public key has an odd value and can’t be used for BIP 340-342 purposes.

§Examples
use secp256k1::{Secp256k1, Keypair, Scalar};

let secp = Secp256k1::new();
let tweak = Scalar::random();

let mut keypair = Keypair::new(&secp, &mut rand::thread_rng());
let tweaked = keypair.add_xonly_tweak(&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
Source

pub fn secret_key(&self) -> SecretKey

Returns the SecretKey for this Keypair.

This is equivalent to using SecretKey::from_keypair.

Source

pub fn public_key(&self) -> PublicKey

Returns the PublicKey for this Keypair.

This is equivalent to using PublicKey::from_keypair.

Source

pub fn x_only_public_key(&self) -> (XOnlyPublicKey, Parity)

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

This is equivalent to using XOnlyPublicKey::from_keypair.

Source

pub fn non_secure_erase(&mut self)

Attempts to erase the secret within the underlying array.

Note, however, that the compiler is allowed to freely copy or move the contents of this array to other places in memory. Preventing this behavior is very subtle. For more discussion on this, please see the documentation of the zeroize crate.

Trait Implementations§

Source§

impl CPtr for Keypair

Source§

impl Clone for Keypair

Source§

fn clone(&self) -> Keypair

Returns a copy 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 Keypair

Available on crate feature std only.
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

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

impl<'de> Deserialize<'de> for Keypair

Available on crate feature serde only.
Source§

fn deserialize<D>(d: D) -> Result<Keypair, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<TweakedKeypair> for Keypair

Source§

fn from(pair: TweakedKeypair) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Keypair

Source§

type Err = Error

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

fn from_str(s: &str) -> Result<Keypair, <Keypair as FromStr>::Err>

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

impl Hash for Keypair

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Keypair

Source§

fn cmp(&self, other: &Keypair) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Keypair

Source§

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

Source§

fn partial_cmp(&self, other: &Keypair) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Keypair

Available on crate feature serde only.
Source§

fn serialize<S>( &self, s: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for Keypair

Source§

impl Eq for Keypair

Source§

impl StructuralPartialEq for Keypair

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,