Struct fedimint_core::core::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 u8
s 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
impl KeyPair
pub 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())
);
§impl KeyPair
impl KeyPair
pub fn as_ptr(&self) -> *const 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
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) -> KeyPairwhere
C: Signing,
pub fn from_secret_key<C>(secp: &Secp256k1<C>, sk: &SecretKey) -> KeyPairwhere 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,
pub fn from_seckey_slice<C>( secp: &Secp256k1<C>, data: &[u8] ) -> Result<KeyPair, Error>where C: Signing,
pub 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,
pub fn from_seckey_str_global(s: &str) -> Result<KeyPair, Error>
pub fn from_seckey_str_global(s: &str) -> Result<KeyPair, Error>
pub 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());
pub fn new_global<R>(rng: &mut R) -> KeyPairwhere
R: Rng + ?Sized,
pub fn new_global<R>(rng: &mut R) -> KeyPairwhere R: Rng + ?Sized,
Generates a new random secret key using the global [SECP256K1
] context.
pub fn secret_bytes(&self) -> [u8; 32]
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
pub fn tweak_add_assign<C>( &mut self, secp: &Secp256k1<C>, tweak: &Scalar ) -> Result<(), Error>where C: Verification,
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,
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
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
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)
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 sign_schnorr(&self, msg: Message) -> Signature
pub fn sign_schnorr(&self, msg: Message) -> Signature
Constructs an schnorr signature for msg
using the global [SECP256K1
] context.
Trait Implementations§
source§impl Decodable for KeyPair
impl Decodable for KeyPair
source§fn consensus_decode<D: Read>(
d: &mut D,
modules: &ModuleDecoderRegistry
) -> Result<Self, DecodeError>
fn consensus_decode<D: Read>( d: &mut D, modules: &ModuleDecoderRegistry ) -> Result<Self, DecodeError>
source§fn consensus_decode_hex(
hex: &str,
modules: &ModuleDecoderRegistry
) -> Result<Self, DecodeError>
fn consensus_decode_hex( hex: &str, modules: &ModuleDecoderRegistry ) -> Result<Self, DecodeError>
§impl<'de> Deserialize<'de> for KeyPair
impl<'de> Deserialize<'de> for KeyPair
§fn 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>,
source§impl Encodable for KeyPair
impl Encodable for KeyPair
source§fn consensus_encode<W: Write>(&self, writer: &mut W) -> Result<usize, Error>
fn consensus_encode<W: Write>(&self, writer: &mut W) -> Result<usize, Error>
source§fn consensus_encode_to_vec(&self) -> Result<Vec<u8>, Error>
fn consensus_encode_to_vec(&self) -> Result<Vec<u8>, Error>
Self::consensus_encode
to newly allocated Vec<u8>
fn consensus_encode_to_hex(&self) -> Result<String, Error>
source§impl From<TweakedKeyPair> for KeyPair
impl From<TweakedKeyPair> for KeyPair
source§fn from(pair: TweakedKeyPair) -> KeyPair
fn from(pair: TweakedKeyPair) -> KeyPair
§impl Ord for KeyPair
impl Ord for KeyPair
§impl PartialOrd for KeyPair
impl PartialOrd for KeyPair
§fn partial_cmp(&self, other: &KeyPair) -> Option<Ordering>
fn partial_cmp(&self, other: &KeyPair) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more§impl Serialize for KeyPair
impl Serialize for KeyPair
§fn serialize<S>(
&self,
s: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>( &self, s: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,
source§impl TapTweak for KeyPair
impl TapTweak for KeyPair
source§fn tap_tweak<C>(
self,
secp: &Secp256k1<C>,
merkle_root: Option<TapBranchHash>
) -> TweakedKeyPairwhere
C: Verification,
fn tap_tweak<C>( self, secp: &Secp256k1<C>, merkle_root: Option<TapBranchHash> ) -> TweakedKeyPairwhere 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
type TweakedAux = TweakedKeyPair
§type TweakedKey = TweakedKeyPair
type TweakedKey = TweakedKeyPair
source§fn dangerous_assume_tweaked(self) -> TweakedKeyPair
fn dangerous_assume_tweaked(self) -> TweakedKeyPair
impl Copy for KeyPair
impl Eq for KeyPair
impl StructuralEq for KeyPair
impl StructuralPartialEq for KeyPair
Auto Trait Implementations§
impl RefUnwindSafe for KeyPair
impl Send for KeyPair
impl Sync for KeyPair
impl Unpin for KeyPair
impl UnwindSafe for KeyPair
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
source§impl<T> DatabaseValue for Twhere
T: Debug + Encodable + Decodable,
impl<T> DatabaseValue for Twhere T: Debug + Encodable + Decodable,
fn from_bytes( data: &[u8], modules: &ModuleRegistry<Decoder, DecodingMode> ) -> Result<T, DecodingError>
fn to_bytes(&self) -> Vec<u8> ⓘ
source§impl<T> DynEncodable for Twhere
T: Encodable,
impl<T> DynEncodable for Twhere T: Encodable,
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where &'a Self: for<'a> IntoIterator,
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_mut()
into the pipe
function.source§impl<T> Serialize for Twhere
T: Serialize + ?Sized,
impl<T> Serialize for Twhere T: Serialize + ?Sized,
fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<Ok, Error>
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds.