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

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

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

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

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

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.

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.

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

Returns the secret bytes for this key pair.

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

Gets the XOnlyPublicKey for this KeyPair.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

Converts to this type from the input type.

The associated error which can be returned from parsing.

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

Feeds this value into the given Hasher. Read more

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

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.