lox_cli/src/bin/bridgedb.rs

42 lines
1.5 KiB
Rust

use lox::BridgeDb;
use std::fs::File;
use std::io::Write;
use std::path::Path;
fn main() {
let bridgedb_filename = "bridgedb.json";
let bridgedb_pubkey_filename = "bridgedb_pubkey.json";
// If bridgedb has already been created, recreate it from file.
// Otherwise, create new bridgedb.
let bridgedb = if Path::new(bridgedb_filename).exists() {
// read in file
let bridgedb_infile = File::open(bridgedb_filename).unwrap();
serde_json::from_reader(bridgedb_infile).unwrap()
} else {
// create new bridgedb (implicitly generates keys)
BridgeDb::new()
};
// output full serialized bridgedb if file doesn't already exist
if !Path::new(bridgedb_filename).exists() {
let mut bridgedb_outfile =
File::create(bridgedb_filename).expect("Failed to create bridgedb privkey file");
let bridgedb_outfile_json = serde_json::to_string(&bridgedb).unwrap();
write!(bridgedb_outfile, "{}", bridgedb_outfile_json)
.expect("Failed to write to bridgedb.json");
}
// output bridgedb public key if file doesn't already exist
if !Path::new(bridgedb_pubkey_filename).exists() {
let mut bridgedb_pubkey_outfile =
File::create(bridgedb_pubkey_filename).expect("Failed to create bridgedb pubkey file");
write!(
bridgedb_pubkey_outfile,
"{}",
serde_json::to_string(&bridgedb.pubkey).unwrap()
)
.expect("Failed to write to bridgedb pubkey file");
}
}