Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frost-client: allow removing contacts and groups #400

Open
wants to merge 1 commit into
base: key-auth-cleanup
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions frost-client/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ pub(crate) enum Command {
#[arg(short, long)]
config: Option<String>,
},
/// Remove a contact from the user's address book.
RemoveContact {
/// The path to the config file to manage. If not specified, it uses
/// $HOME/.local/frost/credentials.toml
#[arg(short, long)]
config: Option<String>,
/// The public key of the contact to remove (list with `contacts`).
#[arg(short, long)]
pubkey: String,
},
TrustedDealer {
/// The path to the config file to manage.
///
Expand Down Expand Up @@ -81,6 +91,17 @@ pub(crate) enum Command {
#[arg(short, long)]
config: Option<String>,
},
/// Remove a group from the config.
RemoveGroup {
/// The path to the config file to manage. If not specified, it uses
/// $HOME/.local/frost/credentials.toml
#[arg(short, long)]
config: Option<String>,
/// The group to remove, identified by the group public key (use
/// `groups` to list)
#[arg(short, long)]
group: String,
},
Coordinator {
/// The path to the config file to manage. If not specified, it uses
/// $HOME/.local/frost/credentials.toml
Expand Down
30 changes: 29 additions & 1 deletion frost-client/src/contact.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::error::Error;

use eyre::eyre;
use eyre::{eyre, OptionExt};
use serde::{Deserialize, Serialize};

use crate::{args::Command, config::Config};
Expand Down Expand Up @@ -124,3 +124,31 @@ pub(crate) fn list(args: &Command) -> Result<(), Box<dyn Error>> {

Ok(())
}

/// Remove a contact from the user's address book in the config file.
pub(crate) fn remove(args: &Command) -> Result<(), Box<dyn Error>> {
let Command::RemoveContact { config, pubkey } = (*args).clone() else {
panic!("invalid Command");
};

let mut config = Config::read(config)?;

let name = config
.contact
.iter()
.find_map(|(name, c)| {
if hex::encode(c.pubkey.clone()) == pubkey {
Some(name.clone())
} else {
None
}
})
.clone()
.ok_or_eyre("contact not found")?;

config.contact.remove(&name);

config.write()?;

Ok(())
}
17 changes: 17 additions & 0 deletions frost-client/src/group.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::error::Error;

use eyre::OptionExt;

use crate::{args::Command, config::Config};

pub(crate) fn list(args: &Command) -> Result<(), Box<dyn Error>> {
Expand All @@ -16,3 +18,18 @@ pub(crate) fn list(args: &Command) -> Result<(), Box<dyn Error>> {

Ok(())
}

/// Remove a group from the user's config file.
pub(crate) fn remove(args: &Command) -> Result<(), Box<dyn Error>> {
let Command::RemoveGroup { config, group } = (*args).clone() else {
panic!("invalid Command");
};

let mut config = Config::read(config)?;

config.group.remove(&group).ok_or_eyre("group not found")?;

config.write()?;

Ok(())
}
2 changes: 2 additions & 0 deletions frost-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
Command::Export { .. } => contact::export(&args.command),
Command::Import { .. } => contact::import(&args.command),
Command::Contacts { .. } => contact::list(&args.command),
Command::RemoveContact { .. } => contact::remove(&args.command),
Command::Groups { .. } => group::list(&args.command),
Command::RemoveGroup { .. } => group::remove(&args.command),
Command::TrustedDealer { .. } => trusted_dealer::trusted_dealer(&args.command),
Command::Coordinator { .. } => crate::coordinator::run(&args.command).await,
Command::Participant { .. } => crate::participant::run(&args.command).await,
Expand Down