Moved keys submodules

This commit is contained in:
Dave Hrycyszyn
2024-06-20 17:13:34 +01:00
parent 1ad7c99283
commit a29a0fca04
6 changed files with 10 additions and 11 deletions

View File

@@ -0,0 +1,27 @@
use std::{
fs::{self, File},
io::Write,
path::PathBuf,
};
use bft_json_crdt::keypair::{make_keypair, Ed25519KeyPair};
use fastcrypto::traits::EncodeDecodeBase64;
/// Writes a new Ed25519 keypair to the file at key_path.
pub(crate) fn write(key_path: &PathBuf) -> Result<(), std::io::Error> {
let keys = make_keypair();
let mut file = File::create(key_path)?;
let out = keys.encode_base64();
file.write(out.as_bytes())?;
Ok(())
}
pub(crate) fn load_from_file(side_dir: &PathBuf) -> Ed25519KeyPair {
let key_path = crate::utils::side_paths(side_dir.clone()).0;
let data = fs::read_to_string(key_path).expect("couldn't read bft-crdt key file");
println!("data: {:?}", data);
Ed25519KeyPair::decode_base64(&data).expect("couldn't load keypair from file")
}

View File

@@ -0,0 +1,40 @@
use bitcoin::secp256k1::{rand, Keypair, Secp256k1, SecretKey};
use std::{
fs::{self, File},
io::Write,
path::PathBuf,
str::FromStr,
};
pub fn make_keypair() -> bitcoin::secp256k1::Keypair {
let secp = Secp256k1::new();
let (secret_key, _) = secp.generate_keypair(&mut rand::thread_rng());
Keypair::from_secret_key(&secp, &secret_key)
}
pub(crate) fn write(key_path: &PathBuf) -> Result<(), std::io::Error> {
let key_pair = make_keypair();
let mut file = File::create(key_path)?;
let pub_str = key_pair.public_key().to_string();
let priv_str = key_pair.display_secret().to_string();
println!("private key: {priv_str}");
let out = format!("{pub_str}/{priv_str}");
println!("out: {out}");
file.write(out.as_bytes())?;
Ok(())
}
pub(crate) fn load_from_file(side_dir: &PathBuf) -> bitcoin::secp256k1::Keypair {
let bitcoin_key_path = crate::utils::side_paths(side_dir.clone()).1; // TODO: this tuple stinks
let data = fs::read_to_string(bitcoin_key_path).expect("couldn't read bitcoin key file");
println!("bitcoin keys: {:?}", data);
let secret_key_data = data.split("/").collect::<Vec<&str>>()[1];
let secp = Secp256k1::new();
let secret_key =
SecretKey::from_str(secret_key_data).expect("couldn't load secret key from file");
Keypair::from_secret_key(&secp, &secret_key)
}

View File

@@ -0,0 +1,2 @@
pub mod bft_crdt;
pub mod bitcoin;