36 lines
1.1 KiB
Rust
36 lines
1.1 KiB
Rust
|
|
use bitcoin::secp256k1::{rand, Keypair, Secp256k1};
|
||
|
|
use std::{
|
||
|
|
fs::{self, File},
|
||
|
|
io::Write,
|
||
|
|
path::PathBuf,
|
||
|
|
};
|
||
|
|
|
||
|
|
pub(crate) fn write(key_path: &PathBuf) -> Result<(), std::io::Error> {
|
||
|
|
let secp = Secp256k1::new();
|
||
|
|
let (secret_key, public_key) = secp.generate_keypair(&mut rand::thread_rng());
|
||
|
|
|
||
|
|
let mut file = File::create(key_path)?;
|
||
|
|
|
||
|
|
let pub_str = public_key.to_string();
|
||
|
|
let priv_str = secret_key.display_secret().to_string();
|
||
|
|
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 key_path = crate::utils::side_paths(side_dir.clone()).0;
|
||
|
|
|
||
|
|
let data = fs::read_to_string(key_path).expect("couldn't read bitcoin key file");
|
||
|
|
println!("data: {:?}", data);
|
||
|
|
|
||
|
|
let secret_key_data = data.split("/").collect::<Vec<&str>>()[1];
|
||
|
|
|
||
|
|
let secp = Secp256k1::new();
|
||
|
|
let secret_key = bitcoin::secp256k1::SecretKey::from_slice(secret_key_data.as_bytes()).unwrap();
|
||
|
|
|
||
|
|
Keypair::from_secret_key(&secp, &secret_key)
|
||
|
|
}
|