use crate::{ generic_public_key::{GenericPublicKey, TPublicKey}, generic_secret_key::{GenericSecretKey, TSecretKey}, generic_signature::TSignature, }; use std::fmt; use std::marker::PhantomData; /// A simple wrapper around `PublicKey` and `GenericSecretKey`. #[derive(Clone)] pub struct GenericKeypair { pub pk: GenericPublicKey, pub sk: GenericSecretKey, _phantom: PhantomData, } impl GenericKeypair where Pub: TPublicKey, Sec: TSecretKey, Sig: TSignature, { /// Instantiate `Self` from a public and secret key. /// /// This function does not check to ensure that `pk` is derived from `sk`. It would be a logic /// error to supply such a `pk`. pub fn from_components(pk: GenericPublicKey, sk: GenericSecretKey) -> Self { Self { pk, sk, _phantom: PhantomData, } } /// Instantiates `Self` from a randomly generated secret key. pub fn random() -> Self { let sk = GenericSecretKey::random(); Self { pk: sk.public_key(), sk, _phantom: PhantomData, } } } impl fmt::Debug for GenericKeypair where Pub: TPublicKey, { /// Defers to `self.pk` to avoid leaking the secret key. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.pk.fmt(f) } }