bdk_chain::bitcoin::key

Struct Keypair

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§

§

impl Keypair

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())
);
§

impl Keypair

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.

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.

§

impl Keypair

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.

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.

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

Creates a Keypair directly from a Secp256k1 secret key.

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.

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.

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

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());

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

Returns the secret bytes for this key pair.

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");

pub fn secret_key(&self) -> SecretKey

Returns the SecretKey for this Keypair.

This is equivalent to using SecretKey::from_keypair.

pub fn public_key(&self) -> PublicKey

Returns the PublicKey for this Keypair.

This is equivalent to using PublicKey::from_keypair.

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.

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§

§

impl CPtr for Keypair

§

impl Clone for Keypair

§

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
§

impl Debug for Keypair

§

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

Formats the value using the given formatter. Read more
§

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

§

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
§

impl<'a> From<&'a Keypair> for PublicKey

§

fn from(pair: &'a Keypair) -> PublicKey

Converts to this type from the input type.
§

impl<'a> From<&'a Keypair> for SecretKey

§

fn from(pair: &'a Keypair) -> SecretKey

Converts to this type from the input type.
§

impl From<Keypair> for PublicKey

§

fn from(pair: Keypair) -> PublicKey

Converts to this type from the input type.
§

impl From<Keypair> for SecretKey

§

fn from(pair: Keypair) -> SecretKey

Converts to this type from the input type.
§

impl From<TweakedKeypair> for Keypair

§

fn from(pair: TweakedKeypair) -> Keypair

Converts to this type from the input type.
§

impl FromStr for Keypair

§

type Err = Error

The associated error which can be returned from parsing.
§

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

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

impl Hash for Keypair

§

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
§

impl Ord for Keypair

§

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
§

impl PartialEq for Keypair

§

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

impl PartialOrd for Keypair

§

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
§

impl Serialize for Keypair

§

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
§

impl TapTweak for Keypair

§

fn tap_tweak<C>( self, secp: &Secp256k1<C>, merkle_root: Option<TapNodeHash>, ) -> TweakedKeypair
where C: Verification,

Tweaks private and public keys within an untweaked Keypair with corresponding public key value and optional script tree merkle root.

This is done by tweaking private key within the pair using the equation q = p + H(P|c), where

  • q is the tweaked private key
  • p is the internal private key
  • H is the hash function
  • c is the commitment data

The public key is generated from a private key by multiplying with generator point, Q = qG.

§Returns

The tweaked key and its parity.

§

type TweakedAux = TweakedKeypair

Tweaked key type with optional auxiliary information
§

type TweakedKey = TweakedKeypair

Tweaked key type
§

fn dangerous_assume_tweaked(self) -> TweakedKeypair

§

impl Copy for Keypair

§

impl Eq for Keypair

§

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
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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.
§

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

§

fn vzip(self) -> V

Source§

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