use crate::schemes::algorithms::Scheme;
use serde::Deserialize;
use serde::Serialize;
use std::env;
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct KeyPair<S: Scheme> {
pub(crate) public: S::PubKey,
pub(crate) private: S::PrivKey,
}
impl<S> KeyPair<S>
where
S: Scheme,
{
pub fn public_key(&self) -> &S::PubKey {
&self.public
}
pub fn private_key(&self) -> &S::PrivKey {
&self.private
}
pub fn into_parts(self) -> (S::PrivKey, S::PubKey) {
(self.private, self.public)
}
pub fn write_keypair_to_file(&self, file: Option<String>) {
println!("writhing to file...");
let file = file.unwrap_or(String::from("../fixtures/fixture_data/keyPair.json"));
let current_path = env::current_dir().unwrap();
let file_to_write = current_path.join(file);
std::fs::write(
&file_to_write,
serde_json::to_string_pretty(&self).expect("failed to serializing key pair"),
)
.expect(&format!(
"failed to write key pair to file: {}",
file_to_write.to_str().unwrap()
));
}
}