Struct secp256k1::KeyPair

source ·
pub struct KeyPair(_);
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§

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 KeyPair directly from a Secp256k1 secret key.

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.

Creates a KeyPair directly from a secret key string.

Errors

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

Creates a KeyPair directly from a secret key string and the global SECP256K1 context.

Errors

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

Returns the secret bytes for this key pair.

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

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

Returns the SecretKey for this KeyPair.

This is equivalent to using SecretKey::from_keypair.

Returns the PublicKey for this KeyPair.

This is equivalent to using PublicKey::from_keypair.

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

This is equivalent to using XOnlyPublicKey::from_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
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
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 !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
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

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