use crate::utils::CommonConfigsWrapper;
use anyhow::bail;
use clap::ArgGroup;
use clap::Parser;
use nym_credential_storage::initialise_persistent_storage;
use nym_id::import_credential::import_master_verification_key;
use std::fs;
use std::path::PathBuf;
fn parse_encoded_key_data(raw: &str) -> bs58::decode::Result<Vec<u8>> {
bs58::decode(raw).into_vec()
}
#[derive(Debug, Parser)]
#[clap(
group(ArgGroup::new("key_data_group").required(true)),
)]
pub struct Args {
#[clap(long)]
pub(crate) client_config: PathBuf,
#[clap(long, group = "key_data_group", value_parser = parse_encoded_key_data)]
pub(crate) key_data: Option<Vec<u8>>,
#[clap(long, group = "key_data_group")]
pub(crate) key_path: Option<PathBuf>,
#[clap(long, hide = true)]
pub(crate) version: Option<u8>,
}
impl Args {
fn key_data(self) -> anyhow::Result<Vec<u8>> {
let data = match self.key_data {
Some(data) => data,
None => {
#[allow(clippy::unwrap_used)]
fs::read(self.key_path.unwrap())?
}
};
Ok(data)
}
}
pub async fn execute(args: Args) -> anyhow::Result<()> {
let loaded = CommonConfigsWrapper::try_load(&args.client_config)?;
if let Ok(id) = loaded.try_get_id() {
println!("loaded config file for client '{id}'");
}
let Ok(credentials_store) = loaded.try_get_credentials_store() else {
bail!("the loaded config does not have a credentials store information")
};
println!(
"using credentials store at '{}'",
credentials_store.display()
);
let credentials_store = initialise_persistent_storage(credentials_store).await;
let version = args.version;
let raw_key = args.key_data()?;
import_master_verification_key(credentials_store, raw_key, version).await?;
Ok(())
}