| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- //use super::cli_config::DrkCliConfig;
- use crate::Result;
- use clap::{App, Arg};
- use serde::Deserialize;
- use std::path::PathBuf;
- fn is_u64<'a>(v: &'a str) -> std::result::Result<(), String> {
- if v.parse::<u64>().is_ok() {
- Ok(())
- } else {
- Err(String::from("The value is not an integer of type u64"))
- }
- }
- #[derive(Deserialize, Debug)]
- pub struct TransferParams {
- pub pub_key: String,
- pub amount: u64,
- }
- impl TransferParams {
- pub fn new() -> Self {
- Self {
- pub_key: String::new(),
- amount: 0,
- }
- }
- }
- pub struct Deposit {
- pub asset: String,
- }
- impl Deposit {
- pub fn new() -> Self {
- Self {
- asset: String::new(),
- }
- }
- }
- #[derive(Deserialize, Debug)]
- pub struct WithdrawParams {
- pub pub_key: String,
- pub amount: u64,
- }
- impl WithdrawParams {
- pub fn new() -> Self {
- Self {
- pub_key: String::new(),
- amount: 0,
- }
- }
- }
- pub struct DrkCli {
- pub verbose: bool,
- pub cashier: bool,
- pub wallet: bool,
- pub key: bool,
- pub get_key: bool,
- pub info: bool,
- pub hello: bool,
- pub stop: bool,
- pub transfer: Option<TransferParams>,
- pub deposit: Option<Deposit>,
- pub withdraw: Option<WithdrawParams>,
- pub config: Box<Option<PathBuf>>,
- }
- impl DrkCli {
- pub fn load() -> Result<Self> {
- let app = App::new("Drk CLI")
- .version("0.1.0")
- .author("Amir Taaki <amir@dyne.org>")
- .about("Run Drk Client")
- .arg(
- Arg::new("verbose")
- .short('v')
- .help_heading(Some("Increase verbosity"))
- .long("verbose")
- .takes_value(false),
- )
- .arg(
- Arg::new("hello")
- .long("hello")
- .help_heading(Some("Test Hello"))
- .takes_value(false),
- )
- .arg(
- Arg::new("cashier")
- .short('c')
- .long("cashier")
- .help_heading(Some("Create a cashier wallet"))
- .takes_value(false),
- )
- .arg(
- Arg::new("key")
- .short('k')
- .long("key")
- .help_heading(Some("Test key"))
- .takes_value(false),
- )
- .arg(
- Arg::new("getkey")
- .long("getkey")
- .help_heading(Some("Get public key as base-58 encoded string"))
- .takes_value(false),
- )
- .arg(
- Arg::new("wallet")
- .short('w')
- .long("wallet")
- .help_heading(Some("Create a new wallet"))
- .takes_value(false),
- )
- .arg(
- Arg::new("info")
- .short('i')
- .long("info")
- .help_heading(Some("Request info from daemon"))
- .takes_value(false),
- )
- .arg(
- Arg::new("stop")
- .short('s')
- .long("stop")
- .help_heading(Some("Send a stop signal to the daemon"))
- .takes_value(false),
- )
- .arg(
- Arg::new("config")
- .help_heading(Some("Path for config file"))
- .long("config")
- .takes_value(true),
- )
- .subcommand(
- App::new("transfer")
- .about("Transfer DBTC between users")
- .arg(
- Arg::new("address")
- .value_name("RECEIVE_ADDRESS")
- .takes_value(true)
- .index(1)
- .help_heading(Some("Address of recipient"))
- .required(true),
- )
- .arg(
- Arg::new("amount")
- .value_name("AMOUNT")
- .takes_value(true)
- .index(2)
- .validator(is_u64)
- .help_heading(Some("Amount to send, in DBTC"))
- .required(true),
- ),
- )
- .subcommand(App::new("deposit").about("Deposit BTC for dBTC"))
- .subcommand(
- App::new("withdraw")
- .about("Withdraw BTC for dBTC")
- .arg(
- Arg::new("address")
- .value_name("RECEIVE_ADDRESS")
- .takes_value(true)
- .index(1)
- .help_heading(Some("Address of recipient"))
- .required(true),
- )
- .arg(
- Arg::new("amount")
- .value_name("AMOUNT")
- .takes_value(true)
- .index(2)
- .validator(is_u64)
- .help_heading(Some("Amount to send, in BTC"))
- .required(true),
- ),
- )
- .subcommand(App::new("deposit").about("Deposit BTC for dBTC"))
- .get_matches();
- let verbose = app.is_present("verbose");
- let cashier = app.is_present("cashier");
- let wallet = app.is_present("wallet");
- let key = app.is_present("key");
- let info = app.is_present("info");
- let hello = app.is_present("hello");
- let stop = app.is_present("stop");
- let get_key = app.is_present("getkey");
- let deposit = None;
- match app.subcommand_matches("deposit") {
- Some(_) => {
- //let deposit = Deposit::new();
- }
- None => {}
- }
- let mut transfer = None;
- match app.subcommand_matches("transfer") {
- Some(transfer_sub) => {
- let mut trn = TransferParams::new();
- if let Some(address) = transfer_sub.value_of("address") {
- trn.pub_key = address.to_string();
- }
- if let Some(amount) = transfer_sub.value_of("amount") {
- trn.amount = amount.parse()?;
- }
- transfer = Some(trn);
- }
- None => {}
- }
- let mut withdraw = None;
- match app.subcommand_matches("withdraw") {
- Some(withdraw_sub) => {
- let mut wdraw = WithdrawParams::new();
- if let Some(address) = withdraw_sub.value_of("address") {
- wdraw.pub_key = address.to_string();
- }
- if let Some(amount) = withdraw_sub.value_of("amount") {
- wdraw.amount = amount.parse()?;
- }
- withdraw = Some(wdraw);
- }
- None => {}
- }
- let config = Box::new(if let Some(config_path) = app.value_of("config") {
- Some(std::path::Path::new(config_path).to_path_buf())
- } else {
- None
- });
- Ok(Self {
- verbose,
- cashier,
- wallet,
- key,
- get_key,
- info,
- hello,
- stop,
- deposit,
- transfer,
- withdraw,
- config,
- })
- }
- }
|