Kaynağa Gözat

darkirc: minor cleanup nitpicks

skoupidi 1 yıl önce
ebeveyn
işleme
bfe1ec9285

+ 26 - 21
bin/darkirc/darkirc_config.toml

@@ -146,32 +146,37 @@ topic = "LunarDAO talk"
 ## ================
 ##
 ## In this section we configure our contacts and people we want to
-## have encrypted DMs with. Your contacts' public keys should be
-## retrieved manually. Whenever this is changed, you can send a
-## SIGHUP signal to the running darkirc instance to reload these.
+## have encrypted DMs with. Whenever something in the configuration
+## is changed, you can send a SIGHUP signal to the running darkirc
+## instance to reload these.
 ##
-## The secret key used to decrypt direct messages sent to your public
-## key (the counterpart to this secret key).
-## It is also recommended to paste the public key here as a comment in
-## order to be able to easily reference it for sharing.
+## The format is:
+## [contact."nickname"]
+## dm_chacha_public = "{the_contact_public_key}"
+## my_dm_chacha_secret = "{your_secret_key_for_this_contact}"
 ##
-## You can generate a keypair with: darkirc --gen-chacha-keypair
-## and replace the secret key below with the generated one.
-## **You should never share this secret key with anyone**
-
-## This is where you put other people's public keys. The format is:
-## [contact."nickname"]. "nickname" can be anything you want.
-## This is how they will appear in your IRC client when they send you a DM.
-## set their public key to dm_chacha_public and
-## set your secret key to dm_chacha_secret
-## you can set a separate secret key for each contact
-
+## "nickname" can be anything you want. This is how they will appear
+## in your IRC client when they send you a DM.
+##
+## "dm_chacha_public" is the contacts' public key, which should be
+## retrieved manually.
+##
+## "my_dm_chacha_secret" is the secret key used to decrypt direct
+## messages sent to the public key (the counterpart to this secret key)
+## you set for this contact. It is recommended to paste the public key
+## here as a comment in order to be able to easily reference it for
+## sharing. You can generate a keypair to use for a contact with:
+## ./darkirc --gen-chacha-keypair
+## Replace the secret key in the contact configuration with the
+## generated one. You can generate and set a separate secret key for
+## each contact, or reuse the same one in multiple contacts.
+## **You should never share secret keys with anyone**
 ##
-## Example (set as many as you want):
+## Examples (set as many as you want):
 #[contact."satoshi"]
 #dm_chacha_public = "C9vC6HNDfGQofWCapZfQK5MkV1JR8Cct839RDUCqbDGK"
-#dm_chacha_secret = "A3mLrq4aW9UkFVY4zCfR2aLdEEWVUdH4u8v4o2dgi4kC"
+#my_dm_chacha_secret = "A3mLrq4aW9UkFVY4zCfR2aLdEEWVUdH4u8v4o2dgi4kC"
 #
 #[contact."anon"]
 #dm_chacha_public = "7iTddcopP2pkvszFjbFUr7MwTcMSKZkYP6zUan22pxfX"
-#dm_chacha_secret = "E229CzXev335cxhHiJyuzSapz7HMfNzf6ipbginFTvtr"
+#my_dm_chacha_secret = "E229CzXev335cxhHiJyuzSapz7HMfNzf6ipbginFTvtr"

+ 5 - 3
bin/darkirc/src/irc/mod.rs

@@ -136,7 +136,9 @@ pub struct IrcChannel {
 /// IRC contact definition
 #[derive(Clone)]
 pub struct IrcContact {
-    /// the first one is the saltbox created with our contact pub key
-    /// the second one is the saltbox created with our own pub key
-    pub saltboxes: (Arc<ChaChaBox>, Arc<ChaChaBox>),
+    /// Saltbox created for our contact public key
+    pub saltbox: Arc<ChaChaBox>,
+    /// Saltbox used to encrypt our nick in direct messages,
+    /// created for our own public key.
+    pub self_saltbox: Arc<ChaChaBox>,
 }

+ 6 - 9
bin/darkirc/src/irc/server.rs

@@ -397,14 +397,13 @@ impl IrcServer {
         };
 
         if let Some((name, contact)) = self.contacts.read().await.get_key_value(privmsg.channel()) {
-            let (saltbox, self_saltbox) = &contact.saltboxes;
             // We will use dummy channel and nick values of MAX_NICK_LEN,
             // since they are not used, so all encrypted messages look the same.
-            *privmsg.channel() = saltbox::encrypt(saltbox, &[0x00; MAX_NICK_LEN]);
+            *privmsg.channel() = saltbox::encrypt(&contact.saltbox, &[0x00; MAX_NICK_LEN]);
             // We will encrypt the dummy nick value using our own self saltbox,
             // so we can identify our messages.
-            *privmsg.nick() = saltbox::encrypt(self_saltbox, &[0x00; MAX_NICK_LEN]);
-            *privmsg.msg() = saltbox::encrypt(saltbox, privmsg.msg().as_bytes());
+            *privmsg.nick() = saltbox::encrypt(&contact.self_saltbox, &[0x00; MAX_NICK_LEN]);
+            *privmsg.msg() = saltbox::encrypt(&contact.saltbox, privmsg.msg().as_bytes());
             debug!("Successfully encrypted message for {}", name);
         };
     }
@@ -457,21 +456,19 @@ impl IrcServer {
         }
 
         for (name, contact) in self.contacts.read().await.iter() {
-            let (saltbox, self_saltbox) = &contact.saltboxes;
-
-            if saltbox::try_decrypt(saltbox, &channel_ciphertext).is_none() {
+            if saltbox::try_decrypt(&contact.saltbox, &channel_ciphertext).is_none() {
                 continue
             };
 
             // Since everyone encrypts the dummy nick value with their self saltbox,
             // we try to decrypt using our, to identify our messages.
-            let nick = if saltbox::try_decrypt(self_saltbox, &nick_ciphertext).is_some() {
+            let nick = if saltbox::try_decrypt(&contact.self_saltbox, &nick_ciphertext).is_some() {
                 String::from(self_nickname)
             } else {
                 name.to_string()
             };
 
-            let Some(msg_dec) = saltbox::try_decrypt(saltbox, &msg_ciphertext) else {
+            let Some(msg_dec) = saltbox::try_decrypt(&contact.saltbox, &msg_ciphertext) else {
                 warn!(target: "darkirc::irc::server::try_decrypt", "Could not decrypt message ciphertext for contact: {name}");
                 continue
             };

+ 8 - 4
bin/darkirc/src/main.rs

@@ -193,11 +193,11 @@ async fn realmain(args: Args, ex: Arc<Executor<'static>>) -> Result<()> {
         let public = bs58::encode(public.to_bytes()).into_string();
 
         println!(
-            "Place this in your config file under your contact, you can reuse this key for multiple contacts\n"
+            "Place this in your config file under your contact, you can reuse this keypair for multiple contacts\n"
         );
         println!("[contact.\"satoshi\"]");
         println!("dm_chacha_public = \"YOUR_CONTACT_PUBLIC_KEY\"");
-        println!("dm_chacha_secret = \"{}\"", secret);
+        println!("my_dm_chacha_secret = \"{}\"", secret);
         println!("#my_dm_chacha_public = \"{}\"", public);
         return Ok(())
     }
@@ -255,8 +255,12 @@ async fn realmain(args: Args, ex: Arc<Executor<'static>>) -> Result<()> {
         // 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())
+        for (name, (public_key, my_secret_key)) in contacts {
+            let public_key = bs58::encode(public_key.to_bytes()).into_string();
+            let my_public_key = my_secret_key.public_key();
+            let my_secret_key = bs58::encode(my_secret_key.to_bytes()).into_string();
+            let my_public_key = bs58::encode(my_public_key.to_bytes()).into_string();
+            println!("{name}: {public_key} using key {my_secret_key}({my_public_key})")
         }
         return Ok(())
     }

+ 17 - 17
bin/darkirc/src/settings.rs

@@ -100,40 +100,40 @@ pub fn list_configured_contacts(
             return Err(ParseFailed("Duplicate contact found"))
         }
 
-        // parse the secret key for that specific contact
-        let Some(contact_secret) = items.get("dm_chacha_secret") else {
-            return Err(ParseFailed("Invalid contact configuration dm_chacha_secret missing. \
+        // Parse the secret key for that specific contact
+        let Some(my_secret) = items.get("my_dm_chacha_secret") else {
+            return Err(ParseFailed("Invalid contact configuration my_dm_chacha_secret missing. \
             You can generate a keypair with: 'darkirc --gen-chacha-keypair' and then add that keypair to your config toml file."))
         };
 
-        let Some(contact_secret_str) = contact_secret.as_str() else {
-            return Err(ParseFailed("dm_chacha_secret not a string"))
+        let Some(my_secret_str) = my_secret.as_str() else {
+            return Err(ParseFailed("my_dm_chacha_secret not a string"))
         };
 
-        let Ok(contact_secret_bytes) = bs58::decode(contact_secret_str).into_vec() else {
-            return Err(ParseFailed("dm_chacha_secret not valid base58"))
+        let Ok(my_secret_bytes) = bs58::decode(my_secret_str).into_vec() else {
+            return Err(ParseFailed("my_dm_chacha_secret not valid base58"))
         };
 
-        if contact_secret_bytes.len() != 32 {
-            return Err(ParseFailed("dm_chacha_secret not 32 bytes long"))
+        if my_secret_bytes.len() != 32 {
+            return Err(ParseFailed("my_dm_chacha_secret not 32 bytes long"))
         }
 
-        let contact_secret_bytes: [u8; 32] = contact_secret_bytes.try_into().unwrap();
+        let my_secret_bytes: [u8; 32] = my_secret_bytes.try_into().unwrap();
 
-        let contact_secret = crypto_box::SecretKey::from(contact_secret_bytes);
+        let my_secret = crypto_box::SecretKey::from(my_secret_bytes);
 
-        ret.insert(name.to_string(), (public, contact_secret));
+        ret.insert(name.to_string(), (public, my_secret));
     }
 
     Ok(ret)
 }
 
 /// Parse configured contacts from a TOML map.
-/// If contacts exist and our secret key is valid, also return its saltbox.
 ///
 /// ```toml
 /// [contact."anon"]
 /// dm_chacha_public = "7CkVuFgwTUpJn5Sv67Q3fyEDpa28yrSeL5Hg2GqQ4jfM"
+/// my_dm_chacha_secret = "A3mLrq4aW9UkFVY4zCfR2aLdEEWVUdH4u8v4o2dgi4kC"
 /// ```
 #[allow(clippy::type_complexity)]
 pub fn parse_configured_contacts(data: &toml::Value) -> Result<HashMap<String, IrcContact>> {
@@ -144,18 +144,18 @@ pub fn parse_configured_contacts(data: &toml::Value) -> Result<HashMap<String, I
         return Ok(ret);
     }
 
-    for (name, (public, contact_secret)) in contacts {
+    for (name, (public, my_secret)) in contacts {
         let saltbox: Arc<crypto_box::ChaChaBox> =
-            Arc::new(crypto_box::ChaChaBox::new(&public, &contact_secret));
+            Arc::new(crypto_box::ChaChaBox::new(&public, &my_secret));
         let self_saltbox: Arc<crypto_box::ChaChaBox> =
-            Arc::new(crypto_box::ChaChaBox::new(&contact_secret.public_key(), &contact_secret));
+            Arc::new(crypto_box::ChaChaBox::new(&my_secret.public_key(), &my_secret));
 
         if ret.contains_key(&name) {
             return Err(ParseFailed("Duplicate contact found"))
         }
 
         info!("Instantiated ChaChaBox for contact \"{}\"", name);
-        ret.insert(name.to_string(), IrcContact { saltboxes: (saltbox, self_saltbox) });
+        ret.insert(name.to_string(), IrcContact { saltbox, self_saltbox });
     }
 
     Ok(ret)

+ 1 - 1
contrib/localnet/darkirc-four-nodes/darkirc_full_node1.toml

@@ -52,5 +52,5 @@ secret = "HoLfk8vXBvNsBZ73e2xRPBZ9vEbnF9FVi4fDdyU9AZLU"
 
 [contact."node2"]
 dm_chacha_public = "ACaVU2uf4n2cheRHFP7RB9PASLweBJNdp5QJ4PGwVz67"
-dm_chacha_secret = "Bwdhe48rKsqFwGUquQSSGZZS5NQP1szrdJUhyDE77Br6"
+my_dm_chacha_secret = "Bwdhe48rKsqFwGUquQSSGZZS5NQP1szrdJUhyDE77Br6"
 #my_dm_chacha_public = "BmUo88RJ5TyR7gUWnr6Mk7tqZE5xPuCqJdJV8KWzsagh"

+ 2 - 2
contrib/localnet/darkirc-four-nodes/darkirc_full_node2.toml

@@ -52,5 +52,5 @@ secret = "HoLfk8vXBvNsBZ73e2xRPBZ9vEbnF9FVi4fDdyU9AZLU"
 
 [contact."node1"]
 dm_chacha_public = "BmUo88RJ5TyR7gUWnr6Mk7tqZE5xPuCqJdJV8KWzsagh"
-dm_chacha_secret = "JwsF8D2xvr9ff3Z5j1rhUCBL8odhdZR7qvR2qJ1ZV6P"
-#my_dm_chacha_public = "ACaVU2uf4n2cheRHFP7RB9PASLweBJNdp5QJ4PGwVz67"
+my_dm_chacha_secret = "JwsF8D2xvr9ff3Z5j1rhUCBL8odhdZR7qvR2qJ1ZV6P"
+#my_dm_chacha_public = "ACaVU2uf4n2cheRHFP7RB9PASLweBJNdp5QJ4PGwVz67"

+ 16 - 16
doc/src/misc/darkirc/private_message.md

@@ -17,12 +17,12 @@ Generate a keypair using the following command:
 This will generate a Public Key and a Private Key.
 
 Save the Private key safely & add it to the `darkirc_config.toml` 
-file under your contact. you can reuse this secret key for multiple
-contacts
+file under your contact(s). You may reuse this keypair for multiple
+contacts, or generate a new one each time.
 ```toml
 [contact.“satoshi”]
-dm_chacha_public = “your_contact_public_key_goes_here”
-dm_chacha_secret = “your_private_key_for_the_contact_goes_here”
+dm_chacha_public = “the_contact_public_key_goes_here”
+my_dm_chacha_secret = “your_private_key_for_this_contact_goes_here”
 ```
 
 To share your Public Key with a user over `darkirc` you can use one of the 
@@ -42,45 +42,45 @@ See the [example darkirc_config.toml](https://codeberg.org/darkrenaissance/darkf
 ## Example
 Lets start by configuring our contacts list in the generated 
 `darkirc_config.toml` file (you can also refer to the examples written 
-in the comments of the toml file), let's assume alice and bob want to
+in the comments of the toml file), let's assume Alice and Bob want to
 privately chat after they have each other's public keys:
 
 Alice would add bob to her contact list in her own config file:
 ```toml
-[contact.”bob”]
+[contact.”Bob”]
 dm_chacha_public = “D6UzKA6qCG5Mep16i6pJYkUCQcnp46E1jPBsUhyJiXhb”
-dm_chacha_secret = “A3mLrq4aW9UkFVY4zCfR2aLdEEWVUdH4u8v4o2dgi4kC”
+my_dm_chacha_secret = “A3mLrq4aW9UkFVY4zCfR2aLdEEWVUdH4u8v4o2dgi4kC”
 ```
 
 And Bob would do the same:
 ```toml
-[contact.”alice”]
+[contact.”Alice”]
 dm_chacha_public = “9sfMEVLphJ4dTX3SEvm6NBhTbWDqfsxu7R2bo88CtV8g”
-dm_chacha_secret = “E229CzXev335cxhHiJyuzSapz7HMfNzf6ipbginFTvtr”
+my_dm_chacha_secret = “E229CzXev335cxhHiJyuzSapz7HMfNzf6ipbginFTvtr”
 ```
 
-Lets see an Example where 'alice' sends “Hi” message to 'bob' using 
+Lets see an Example where 'Alice' sends “Hi” message to 'Bob' using 
 the /msg command
 ```
-/msg bob Hi
+/msg Bob Hi
 ```
 
 <u>Note for Weechat Client Users:</u>\
 When you private message someone as shown above, the buffer will not 
 pop in weechat client until you receive a reply from that person.
 
-For example here 'alice' will not see any new buffer on her irc interface for 
-the recent message which she just send to 'bob' until 'bob' replies,
-but 'bob' will get a buffer shown on his irc client with the message 'Hi'.
+For example here 'Alice' will not see any new buffer on her irc interface for 
+the recent message which she just send to 'Bob' until 'Bob' replies,
+but 'Bob' will get a buffer shown on his irc client with the message 'Hi'.
 
-Reply from 'bob' to 'alice' 
+Reply from 'Bob' to 'Alice'
 ```
 /msg alice welcome!
 ```
 
 Or instead of `/msg` command, you can use:
 ```
-/query bob hello
+/query Bob hello
 ```
 This works exactly the same as `/msg` except it will open a new buffer 
 with Bob in your client regardless of sending a msg or not.