Struct breez_sdk_core::bitcoin::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 key_pair = 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::ONE_KEY;
use secp256k1::KeyPair;
use secp256k1::Secp256k1;

let secp = Secp256k1::new();
let key = ONE_KEY;
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 as_ptr(&self) -> *const KeyPair

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

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

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 key_pair = KeyPair::new(&secp, &mut rand::thread_rng());

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

Returns the secret bytes for this key pair.

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

👎Deprecated since 0.23.0: Use add_xonly_tweak instead

Tweaks a keypair by adding the given tweak to the secret key and updating the public key accordingly.

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};
use secp256k1::rand::{RngCore, thread_rng};

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

let mut key_pair = KeyPair::new(&secp, &mut thread_rng());
let tweaked = key_pair.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.

Trait Implementations§

§

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 + PartialOrd,

Restrict a value to a certain interval. Read more
§

impl PartialEq for KeyPair

§

fn eq(&self, other: &KeyPair) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method 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

This method 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

This method 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

This method 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<TapBranchHash>, ) -> 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
§

impl<T> Any for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

§

fn type_name(&self) -> &'static str

§

impl<T> AnySync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

§

impl<T> AsAny for T
where T: Any,

§

fn as_any(&self) -> &(dyn Any + 'static)

§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

§

fn type_name(&self) -> &'static str

Gets the type name of self
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

impl<T> Downcast for T
where T: AsAny + ?Sized,

§

fn is<T>(&self) -> bool
where T: AsAny,

Returns true if the boxed type is the same as T. Read more
§

fn downcast_ref<T>(&self) -> Option<&T>
where T: AsAny,

Forward to the method defined on the type Any.
§

fn downcast_mut<T>(&mut self) -> Option<&mut T>
where T: AsAny,

Forward to the method defined on the type Any.
source§

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

source§

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

Compare self to key and return true if they are equal.
§

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
§

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

§

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

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> Instrument for T

§

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

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

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

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

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

§

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

§

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

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> DartSafe for T

source§

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

§

impl<T> Sequence for T
where T: Eq + Hash,