use std::{path::PathBuf, str::FromStr};
use anyhow::{bail, Result};
use clap::Args;
use crate::{Network, PrivateKey};
#[derive(Debug, Args, Clone)]
#[group(required = true, multiple = false)]
pub struct Key<N: Network> {
#[clap(long = "private-key")]
pub private_key: Option<PrivateKey<N>>,
#[clap(long = "private-key-file")]
pub private_key_file: Option<PathBuf>,
}
impl<N: Network> Key<N> {
pub fn try_get(self) -> Result<PrivateKey<N>> {
match (self.private_key, self.private_key_file) {
(Some(key), None) => Ok(key),
(None, Some(file)) => {
let raw = std::fs::read_to_string(file)?.trim().to_string();
Ok(PrivateKey::from_str(&raw)?)
}
_ => bail!("Either `private-key` or `private-key-file` must be set"),
}
}
}