use clap::{Args, Subcommand};
use kobe::DeriveExt;
use kobe::cosmos::{ChainConfig, Deriver};
use crate::commands::simple::SimpleArgs;
use crate::output::{self, HdWalletOutput};
#[derive(Args)]
pub(crate) struct CosmosCommand {
#[command(subcommand)]
command: CosmosSubcommand,
}
#[derive(Subcommand)]
enum CosmosSubcommand {
New {
#[command(flatten)]
args: CosmosArgs,
},
Import {
#[arg(short, long)]
mnemonic: String,
#[command(flatten)]
args: CosmosArgs,
},
}
#[derive(Args, Debug, Clone)]
struct CosmosArgs {
#[arg(long, default_value = "cosmos")]
hrp: String,
#[arg(long, default_value = "118")]
coin_type: u32,
#[command(flatten)]
common: SimpleArgs,
}
impl CosmosCommand {
pub(crate) fn execute(self, json: bool) -> Result<(), Box<dyn std::error::Error>> {
let (mnemonic, args) = match self.command {
CosmosSubcommand::New { args } => (None, args),
CosmosSubcommand::Import { mnemonic, args } => (Some(mnemonic), args),
};
let wallet = args.common.build_wallet(mnemonic.as_deref())?;
let deriver = Deriver::with_config(&wallet, ChainConfig::new(args.hrp, args.coin_type));
let accounts = deriver.derive_many(0, args.common.count)?;
let out = HdWalletOutput::simple("cosmos", &wallet, &accounts);
output::render_hd_wallet(&out, json, args.common.qr)?;
Ok(())
}
}