pub struct KeyPair(_);
Expand description
Opaque data structure that holds a keypair consisting of a secret and a public key.
Serde support
Serialize
and Deserialize
are not implemented for this type, even with the serde
feature active. This is due to security considerations, see the [serde_keypair
] documentation
for details.
If the serde
and global-context
features are active KeyPair
s can be serialized and
deserialized by annotating them with #[serde(with = "secp256k1::serde_keypair")]
inside structs or enums for which Serialize
and Deserialize
are being derived.
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
sourceimpl KeyPair
impl KeyPair
sourcepub fn display_secret(&self) -> DisplaySecret
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())
);
sourceimpl KeyPair
impl KeyPair
sourcepub fn as_ptr(&self) -> *const KeyPair
pub fn as_ptr(&self) -> *const KeyPair
Obtains a raw const pointer suitable for use with FFI functions.
sourcepub fn as_mut_ptr(&mut self) -> *mut KeyPair
pub fn as_mut_ptr(&mut self) -> *mut KeyPair
Obtains a raw mutable pointer suitable for use with FFI functions.
sourcepub fn from_secret_key<C>(secp: &Secp256k1<C>, sk: SecretKey) -> KeyPairwhere
C: Signing,
pub fn from_secret_key<C>(secp: &Secp256k1<C>, sk: SecretKey) -> KeyPairwhere
C: Signing,
Creates a Schnorr KeyPair
directly from generic Secp256k1 secret key.
Panics
Panics if internal representation of the provided SecretKey
does not hold correct secret
key value obtained from Secp256k1 library previously, specifically when secret key value is
out-of-range (0 or in excess of the group order).
sourcepub fn from_seckey_slice<C>(
secp: &Secp256k1<C>,
data: &[u8]
) -> Result<KeyPair, Error>where
C: Signing,
pub fn from_seckey_slice<C>(
secp: &Secp256k1<C>,
data: &[u8]
) -> Result<KeyPair, Error>where
C: Signing,
Creates a Schnorr 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.
sourcepub fn from_seckey_str<C>(secp: &Secp256k1<C>, s: &str) -> Result<KeyPair, Error>where
C: Signing,
pub fn from_seckey_str<C>(secp: &Secp256k1<C>, s: &str) -> Result<KeyPair, Error>where
C: Signing,
Creates a Schnorr KeyPair
directly from a secret key string.
Errors
Error::InvalidSecretKey
if corresponding public key for the provided secret key is not even.
sourcepub fn new<R, C>(secp: &Secp256k1<C>, rng: &mut R) -> KeyPairwhere
R: Rng + ?Sized,
C: Signing,
pub fn new<R, C>(secp: &Secp256k1<C>, rng: &mut R) -> KeyPairwhere
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());
sourcepub fn secret_bytes(&self) -> [u8; 32]
pub fn secret_bytes(&self) -> [u8; 32]
Returns the secret bytes for this key pair.
sourcepub fn tweak_add_assign<C>(
&mut self,
secp: &Secp256k1<C>,
tweak: &[u8]
) -> Result<(), Error>where
C: Verification,
pub fn tweak_add_assign<C>(
&mut self,
secp: &Secp256k1<C>,
tweak: &[u8]
) -> Result<(), Error>where
C: Verification,
Tweaks a keypair by adding the given tweak to the secret key and updating the public key accordingly.
Errors
Returns an error if the resulting key would be invalid or if the tweak was not a 32-byte length slice.
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};
use secp256k1::rand::{RngCore, thread_rng};
let secp = Secp256k1::new();
let mut tweak = [0u8; 32];
thread_rng().fill_bytes(&mut tweak);
let mut key_pair = KeyPair::new(&secp, &mut thread_rng());
key_pair.tweak_add_assign(&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
sourcepub fn public_key(&self) -> XOnlyPublicKey
pub fn public_key(&self) -> XOnlyPublicKey
Gets the XOnlyPublicKey for this KeyPair.
Trait Implementations
sourceimpl<'de> Deserialize<'de> for KeyPair
impl<'de> Deserialize<'de> for KeyPair
sourcefn deserialize<D>(d: D) -> Result<KeyPair, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(d: D) -> Result<KeyPair, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
sourceimpl From<TweakedKeyPair> for KeyPair
impl From<TweakedKeyPair> for KeyPair
sourcefn from(pair: TweakedKeyPair) -> Self
fn from(pair: TweakedKeyPair) -> Self
sourceimpl Ord for KeyPair
impl Ord for KeyPair
1.21.0 · sourcefn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 · sourcefn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized + PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized + PartialOrd<Self>,
sourceimpl PartialOrd<KeyPair> for KeyPair
impl PartialOrd<KeyPair> for KeyPair
sourcefn partial_cmp(&self, other: &KeyPair) -> Option<Ordering>
fn partial_cmp(&self, other: &KeyPair) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more