use firma::log::debug;
use firma::online::{ConnectOptions, PathOptions, WalletNameOptions};
use firma::serde_json::Value;
use firma::*;
use std::convert::TryInto;
use structopt::StructOpt;
use FirmaOnlineSubcommands::*;
#[derive(StructOpt, Debug)]
#[structopt(name = "firma-online")]
struct FirmaOnlineCommands {
#[structopt(flatten)]
context: OnlineContext,
#[structopt(subcommand)]
subcommand: FirmaOnlineSubcommands,
#[structopt(short, long)]
encrypt: bool,
}
#[derive(StructOpt, Debug)]
enum FirmaOnlineSubcommands {
Connect(ConnectOptions),
CreateWallet(firma::online::create_wallet::CreateWalletOptions),
Rescan(firma::online::rescan::RescanOptions),
GetAddress(firma::online::get_address::GetAddressOptions),
CreateTx(firma::online::create_tx::CreateTxOptions),
SendTx(firma::online::send_tx::SendTxOptions),
Balance(WalletNameOptions),
ListCoins(WalletNameOptions),
Import(PathOptions),
}
fn main() -> Result<()> {
let output = match start() {
Ok(output) => output,
Err(e) => e.to_json(),
};
println!("{}", serde_json::to_string_pretty(&output)?);
Ok(())
}
fn start() -> Result<Value> {
init_logger();
debug!("firma-online start");
let FirmaOnlineCommands {
mut context,
subcommand,
encrypt,
} = FirmaOnlineCommands::from_args();
if encrypt {
context.read_encryption_key()?;
}
debug!(
"firma-online context:{:?} encrypt:{} subcommand:{:?}",
context, encrypt, subcommand
);
match subcommand {
Connect(opt) => {
let _ = opt.daemon_opts.make_client(None, context.network)?;
context.write_daemon_opts(opt.daemon_opts)?.try_into()
}
CreateWallet(opt) => context.create_wallet(&opt)?.try_into(),
GetAddress(opt) => context.get_address(&opt)?.try_into(),
CreateTx(opt) => context.create_tx(&opt)?.try_into(),
SendTx(opt) => context.send_tx(&opt)?.try_into(),
Balance(opt) => context.balance(&opt)?.try_into(),
Rescan(opt) => Ok(context.rescan(&opt)?),
ListCoins(opt) => context.list_coins(&opt)?.try_into(),
Import(opt) => context.import(&opt),
}
}