Finish defining bridgedb, THEN output keys

This commit is contained in:
Vecna 2022-11-24 22:48:18 -05:00
parent a117e79e18
commit bd14499f3b
1 changed files with 13 additions and 12 deletions

View File

@ -4,27 +4,31 @@ use std::io::Write;
use std::path::Path;
fn main() {
let bridgedb_privkey_filename = "bridgedb.json";
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_privkey_filename).exists() {
let bridgedb = if Path::new(bridgedb_filename).exists() {
// read in file
let bridgedb_infile = File::open(bridgedb_privkey_filename).unwrap();
let bridgedb_infile = File::open(bridgedb_filename).unwrap();
serde_json::from_reader(bridgedb_infile).unwrap()
} else {
// create new bridgedb (implicitly generates keys)
let bridgedb = BridgeDb::new();
BridgeDb::new()
};
// output full serialized bridgedb
let mut bridgedb_outfile = File::create(bridgedb_privkey_filename)
.expect("Failed to create bridgedb privkey file");
// 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
// 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!(
@ -33,8 +37,5 @@ fn main() {
serde_json::to_string(&bridgedb.pubkey).unwrap()
)
.expect("Failed to write to bridgedb pubkey file");
// return bridgedb
bridgedb
};
}
}