use clap::{arg, command};
use super::super::config::{
locator, network,
secret::{self, Secret},
};
use crate::{commands::global, print::Print};
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Config(#[from] locator::Error),
#[error(transparent)]
Secret(#[from] secret::Error),
#[error(transparent)]
Network(#[from] network::Error),
#[error("An identity with the name '{0}' already exists")]
IdentityAlreadyExists(String),
}
#[derive(Debug, clap::Parser, Clone)]
#[group(skip)]
#[allow(clippy::struct_excessive_bools)]
pub struct Cmd {
pub name: String,
#[arg(long)]
pub no_fund: bool,
#[arg(long, conflicts_with = "default_seed")]
pub seed: Option<String>,
#[arg(long, short = 's')]
pub as_secret: bool,
#[command(flatten)]
pub config_locator: locator::Args,
#[arg(long)]
pub hd_path: Option<usize>,
#[arg(long, short = 'd', conflicts_with = "seed")]
pub default_seed: bool,
#[command(flatten)]
pub network: network::Args,
#[arg(long, default_value = "false")]
pub fund: bool,
#[arg(long)]
pub overwrite: bool,
}
impl Cmd {
pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
let print = Print::new(global_args.quiet);
if self.config_locator.read_identity(&self.name).is_ok() {
if !self.overwrite {
return Err(Error::IdentityAlreadyExists(self.name.clone()));
}
print.exclaimln(format!("Overwriting identity '{}'", &self.name));
}
if !self.fund {
print.warnln(
"Behavior of `generate` will change in the \
future, and it will no longer fund by default. If you want to fund please \
provide `--fund` flag. If you don't need to fund your keys in the future, ignore this \
warning. It can be suppressed with -q flag.",
);
}
let seed_phrase = if self.default_seed {
Secret::test_seed_phrase()
} else {
Secret::from_seed(self.seed.as_deref())
}?;
let secret = if self.as_secret {
seed_phrase.private_key(self.hd_path)?.into()
} else {
seed_phrase
};
self.config_locator.write_identity(&self.name, &secret)?;
if !self.no_fund {
let addr = secret.public_key(self.hd_path)?;
let network = self.network.get(&self.config_locator)?;
network
.fund_address(&addr)
.await
.map_err(|e| {
tracing::warn!("fund_address failed: {e}");
})
.unwrap_or_default();
}
Ok(())
}
}