use clap::Parser;
use sha2::{Digest, Sha256};
use crate::commands::global;
use crate::config::network::passphrase;
use crate::config::{locator, network};
pub mod public_key;
pub mod secret;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Config(#[from] locator::Error),
#[error(transparent)]
Network(#[from] network::Error),
#[error(transparent)]
Strkey(#[from] stellar_strkey::DecodeError),
}
#[derive(Debug, clap::Parser, Clone)]
#[group(skip)]
pub struct Args {
#[arg(long = "network-passphrase", env = "STELLAR_NETWORK_PASSPHRASE")]
pub network_passphrase: Option<String>,
#[arg(long, short = 'n', env = "STELLAR_NETWORK")]
pub network: Option<String>,
#[command(flatten)]
pub locator: locator::Args,
}
impl Args {
pub fn root_key(&self) -> Result<[u8; 32], Error> {
let network_passphrase = match (self.network.as_deref(), self.network_passphrase.clone()) {
(None, None) => passphrase::TESTNET.to_string(),
(Some(network), None) => self.locator.read_network(network)?.network_passphrase,
(_, Some(network_passphrase)) => network_passphrase,
};
Ok(Sha256::digest(network_passphrase.as_bytes()).into())
}
}
#[derive(Debug, Parser)]
pub enum Cmd {
#[command(visible_alias = "address")]
PublicKey(public_key::Cmd),
Secret(secret::Cmd),
}
impl Cmd {
pub fn run(&self, _global_args: &global::Args) -> Result<(), Error> {
match self {
Cmd::PublicKey(cmd) => cmd.run()?,
Cmd::Secret(cmd) => cmd.run()?,
}
Ok(())
}
}