use firma::import_export::ExportOptions;
use firma::log::debug;
use firma::online::{PathOptions, WalletNameOptions};
use firma::serde_json::{self, Value};
use firma::{common, init_logger, offline, OfflineContext, Result, ToJson};
use std::convert::TryInto;
use structopt::StructOpt;
use FirmaOfflineSubcommands::*;
#[derive(StructOpt, Debug)]
#[structopt(name = "firma-offline")]
struct FirmaOfflineCommands {
#[structopt(flatten)]
context: OfflineContext,
#[structopt(subcommand)]
subcommand: FirmaOfflineSubcommands,
#[structopt(short, long)]
encrypt: bool,
}
#[derive(StructOpt, Debug)]
enum FirmaOfflineSubcommands {
Dice(offline::dice::DiceOptions),
Random(offline::random::RandomOptions),
Sign(offline::sign::SignOptions),
Print(offline::print::PrintOptions),
Restore(offline::restore::RestoreOptions),
List(common::list::ListOptions),
SignWallet(WalletNameOptions),
VerifyWallet(WalletNameOptions),
Import(PathOptions),
Export(ExportOptions),
}
fn main() -> Result<()> {
init_logger();
debug!("firma-offline start");
let cmd = FirmaOfflineCommands::from_args();
let FirmaOfflineCommands {
mut context,
subcommand,
encrypt,
} = cmd;
if encrypt {
context.read_encryption_key()?;
}
debug!(
"firma-offline context:{:?} encrypt:{} subcommand:{:?}",
context, encrypt, subcommand
);
let value = match launch_subcommand(&context, subcommand) {
Ok(value) => value,
Err(e) => e.to_json(),
};
println!("{}", serde_json::to_string_pretty(&value)?);
Ok(())
}
fn launch_subcommand(
context: &OfflineContext,
subcommand: FirmaOfflineSubcommands,
) -> Result<Value> {
match &subcommand {
Dice(opt) => context.roll(opt)?.try_into(),
Sign(opt) => context.sign(opt)?.try_into(),
Random(opt) => context.create_key(opt)?.try_into(),
Print(opt) => context.print(opt)?.try_into(),
Restore(opt) => context.restore(opt)?.try_into(),
List(opt) => context.list(opt)?.try_into(),
SignWallet(opt) => context.sign_wallet(opt)?.try_into(),
VerifyWallet(opt) => context.verify_wallet(opt)?.try_into(),
Import(opt) => context.import(opt),
Export(opt) => context.export(opt),
}
}