Vecna 1 год назад
Родитель
Сommit
a117e79e18
1 измененных файлов с 40 добавлено и 21 удалено
  1. 40 21
      src/bin/lox_auth.rs

+ 40 - 21
src/bin/lox_auth.rs

@@ -1,10 +1,11 @@
 use lox::BridgeAuth;
 use std::fs::File;
 use std::io::Write;
+use std::path::Path;
 
 fn main() {
     let bridgedb_pubkey_filename = "bridgedb_pubkey.json";
-    let lox_auth_privkeys_filename = "lox_auth.json";
+    let lox_auth_filename = "lox_auth.json";
     let lox_auth_pubkeys_filename = "lox_auth_pubkeys.json";
 
     // import bridgedb pubkey
@@ -12,27 +13,45 @@ fn main() {
     let bridgedb_pubkey_infile = File::open(bridgedb_pubkey_filename).unwrap();
     let bridgedb_pubkey = serde_json::from_reader(bridgedb_pubkey_infile).unwrap();
 
-    // create new bridge authority (implicitly generates keys)
-    let lox_auth = BridgeAuth::new(bridgedb_pubkey);
+    // If lox_auth has already been created, recreate it from file.
+    // Otherwise, create new lox authority.
+    let lox_auth = if Path::new(lox_auth_filename).exists() {
+        // read in file
+        let lox_auth_infile = File::open(lox_auth_filename).unwrap();
+        serde_json::from_reader(lox_auth_infile).unwrap()
+    } else {
+        // create new lox_auth (implicitly generates keys)
+        BridgeAuth::new(bridgedb_pubkey)
+    };
 
-    // TODO: serialize lox_auth private keys
+    // output full serialized lox_auth if it doesn't exist
+    if !Path::new(lox_auth_filename).exists() {
+        let mut lox_auth_outfile =
+            File::create(lox_auth_filename).expect("Failed to create lox_auth file");
+        let lox_auth_outfile_json = serde_json::to_string(&lox_auth).unwrap();
+        write!(lox_auth_outfile, "{}", lox_auth_outfile_json)
+            .expect("Failed to write to lox_auth file");
+    }
 
-    // vector of public keys (to serialize)
-    let lox_auth_pubkeys = vec![
-        lox_auth.lox_pub,
-        lox_auth.migration_pub,
-        lox_auth.migrationkey_pub,
-        lox_auth.reachability_pub,
-        lox_auth.invitation_pub,
-    ];
+    // output lox_auth pubkeys if the file doesn't exist
+    if !Path::new(lox_auth_pubkeys_filename).exists() {
+        // vector of public keys (to serialize)
+        let lox_auth_pubkeys = vec![
+            &lox_auth.lox_pub,
+            &lox_auth.migration_pub,
+            &lox_auth.migrationkey_pub,
+            &lox_auth.reachability_pub,
+            &lox_auth.invitation_pub,
+        ];
 
-    // output lox_auth public keys
-    let mut lox_auth_pubkeys_outfile =
-        File::create(lox_auth_pubkeys_filename).expect("Failed to create lox_auth pubkeys file");
-    write!(
-        lox_auth_pubkeys_outfile,
-        "{}",
-        serde_json::to_string(&lox_auth_pubkeys).unwrap()
-    )
-    .expect("Failed to write to lox_auth pubkeys file");
+        // output lox_auth public keys
+        let mut lox_auth_pubkeys_outfile = File::create(lox_auth_pubkeys_filename)
+            .expect("Failed to create lox_auth pubkeys file");
+        write!(
+            lox_auth_pubkeys_outfile,
+            "{}",
+            serde_json::to_string(&lox_auth_pubkeys).unwrap()
+        )
+        .expect("Failed to write to lox_auth pubkeys file");
+    }
 }