瀏覽代碼

darkirc: list configured contacts

dasman 2 年之前
父節點
當前提交
2f5b10f472
共有 2 個文件被更改,包括 55 次插入12 次删除
  1. 26 0
      bin/darkirc/src/main.rs
  2. 29 12
      bin/darkirc/src/settings.rs

+ 26 - 0
bin/darkirc/src/main.rs

@@ -30,8 +30,10 @@ use darkfi::{
     util::path::{expand_path, get_config_path},
     Error, Result,
 };
+
 use log::{debug, error, info};
 use rand::rngs::OsRng;
+use settings::list_configured_contacts;
 use smol::{fs, lock::Mutex, stream::StreamExt, Executor};
 use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml};
 use url::Url;
@@ -130,6 +132,10 @@ struct Args {
     #[structopt(long)]
     encrypt_password: bool,
 
+    /// List configured contacts.
+    #[structopt(long)]
+    list_contacts: bool,
+
     /// P2P network settings
     #[structopt(flatten)]
     net: SettingsOpt,
@@ -215,6 +221,26 @@ async fn realmain(args: Args, ex: Arc<Executor<'static>>) -> Result<()> {
         return Ok(())
     }
 
+    if args.list_contacts {
+        let config_path = get_config_path(args.config, CONFIG_FILE)?;
+        let contents = fs::read_to_string(&config_path).await?;
+        let contents = match toml::from_str(&contents) {
+            Ok(v) => v,
+            Err(e) => {
+                error!("Failed parsing TOML config: {}", e);
+                return Err(Error::ParseFailed("Failed parsing TOML config"))
+            }
+        };
+
+        // Parse configured contacts
+        let contacts = list_configured_contacts(&contents)?;
+
+        for (name, public_key) in contacts {
+            println!("{}: {}", name, bs58::encode(public_key.as_bytes()).into_string())
+        }
+        return Ok(())
+    }
+
     if args.encrypt_password {
         let mut pw = String::new();
 

+ 29 - 12
bin/darkirc/src/settings.rs

@@ -21,6 +21,7 @@ use std::{
     sync::Arc,
 };
 
+use crypto_box::PublicKey;
 use darkfi::{Error::ParseFailed, Result};
 use log::info;
 
@@ -90,13 +91,7 @@ fn parse_dm_chacha_secret(data: &toml::Value) -> Result<Option<crypto_box::Secre
     Ok(Some(crypto_box::SecretKey::from(secret_bytes)))
 }
 
-/// Parse configured contacts from a TOML map.
-///
-/// ```toml
-/// [contact."anon"]
-/// dm_chacha_public = "7CkVuFgwTUpJn5Sv67Q3fyEDpa28yrSeL5Hg2GqQ4jfM"
-/// ```
-pub fn parse_configured_contacts(data: &toml::Value) -> Result<HashMap<String, IrcContact>> {
+pub fn list_configured_contacts(data: &toml::Value) -> Result<HashMap<String, PublicKey>> {
     let mut ret = HashMap::new();
 
     let Some(table) = data.as_table() else { return Err(ParseFailed("TOML not a map")) };
@@ -105,10 +100,6 @@ pub fn parse_configured_contacts(data: &toml::Value) -> Result<HashMap<String, I
         return Err(ParseFailed("`contact` not a map"))
     };
 
-    let Some(secret) = parse_dm_chacha_secret(data)? else {
-        return Err(ParseFailed("You have specified some contacts but you did not set up a valid chacha secret for yourself.  You can generate a keypair with: 'darkirc --gen-chacha-keypair' and then add that keypair to your config toml file."))
-    };
-
     for (name, items) in contacts {
         let Some(public_str) = items.get("dm_chacha_public") else {
             return Err(ParseFailed("Invalid contact configuration"))
@@ -129,12 +120,38 @@ pub fn parse_configured_contacts(data: &toml::Value) -> Result<HashMap<String, I
         let public_bytes: [u8; 32] = public_bytes.try_into().unwrap();
 
         let public = crypto_box::PublicKey::from(public_bytes);
-        let saltbox = Some(Arc::new(crypto_box::ChaChaBox::new(&public, &secret)));
 
         if ret.contains_key(name) {
             return Err(ParseFailed("Duplicate contact found"))
         }
 
+        info!("Instantiated ChaChaBox for contact \"{}\"", name);
+        ret.insert(name.to_string(), public);
+    }
+
+    Ok(ret)
+}
+
+/// Parse configured contacts from a TOML map.
+///
+/// ```toml
+/// [contact."anon"]
+/// dm_chacha_public = "7CkVuFgwTUpJn5Sv67Q3fyEDpa28yrSeL5Hg2GqQ4jfM"
+/// ```
+pub fn parse_configured_contacts(data: &toml::Value) -> Result<HashMap<String, IrcContact>> {
+    let mut ret = HashMap::new();
+
+    let contacts = list_configured_contacts(data)?;
+    let Some(secret) = parse_dm_chacha_secret(data)? else {
+        return Err(ParseFailed("You have specified some contacts but you did not set up a valid chacha secret for yourself.  You can generate a keypair with: 'darkirc --gen-chacha-keypair' and then add that keypair to your config toml file."))
+    };
+    for (name, public) in contacts {
+        let saltbox = Some(Arc::new(crypto_box::ChaChaBox::new(&public, &secret)));
+
+        if ret.contains_key(&name) {
+            return Err(ParseFailed("Duplicate contact found"))
+        }
+
         info!("Instantiated ChaChaBox for contact \"{}\"", name);
         ret.insert(name.to_string(), IrcContact { saltbox });
     }