drk_cli.rs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //use super::cli_config::DrkCliConfig;
  2. use crate::Result;
  3. use clap::{App, Arg};
  4. use serde::Deserialize;
  5. use std::path::PathBuf;
  6. fn is_u64<'a>(v: &'a str) -> std::result::Result<(), String> {
  7. if v.parse::<u64>().is_ok() {
  8. Ok(())
  9. } else {
  10. Err(String::from("The value is not an integer of type u64"))
  11. }
  12. }
  13. #[derive(Deserialize, Debug)]
  14. pub struct TransferParams {
  15. pub pub_key: String,
  16. pub amount: u64,
  17. }
  18. impl TransferParams {
  19. pub fn new() -> Self {
  20. Self {
  21. pub_key: String::new(),
  22. amount: 0,
  23. }
  24. }
  25. }
  26. pub struct Deposit {
  27. pub asset: String,
  28. }
  29. impl Deposit {
  30. pub fn new() -> Self {
  31. Self {
  32. asset: String::new(),
  33. }
  34. }
  35. }
  36. #[derive(Deserialize, Debug)]
  37. pub struct WithdrawParams {
  38. pub pub_key: String,
  39. pub amount: u64,
  40. }
  41. impl WithdrawParams {
  42. pub fn new() -> Self {
  43. Self {
  44. pub_key: String::new(),
  45. amount: 0,
  46. }
  47. }
  48. }
  49. pub struct DrkCli {
  50. pub verbose: bool,
  51. pub cashier: bool,
  52. pub wallet: bool,
  53. pub key: bool,
  54. pub get_key: bool,
  55. pub info: bool,
  56. pub hello: bool,
  57. pub stop: bool,
  58. pub transfer: Option<TransferParams>,
  59. pub deposit: Option<Deposit>,
  60. pub withdraw: Option<WithdrawParams>,
  61. pub config: Box<Option<PathBuf>>,
  62. }
  63. impl DrkCli {
  64. pub fn load() -> Result<Self> {
  65. let app = App::new("Drk CLI")
  66. .version("0.1.0")
  67. .author("Amir Taaki <amir@dyne.org>")
  68. .about("Run Drk Client")
  69. .arg(
  70. Arg::new("verbose")
  71. .short('v')
  72. .help_heading(Some("Increase verbosity"))
  73. .long("verbose")
  74. .takes_value(false),
  75. )
  76. .arg(
  77. Arg::new("hello")
  78. .long("hello")
  79. .help_heading(Some("Test Hello"))
  80. .takes_value(false),
  81. )
  82. .arg(
  83. Arg::new("cashier")
  84. .short('c')
  85. .long("cashier")
  86. .help_heading(Some("Create a cashier wallet"))
  87. .takes_value(false),
  88. )
  89. .arg(
  90. Arg::new("key")
  91. .short('k')
  92. .long("key")
  93. .help_heading(Some("Test key"))
  94. .takes_value(false),
  95. )
  96. .arg(
  97. Arg::new("getkey")
  98. .long("getkey")
  99. .help_heading(Some("Get public key as base-58 encoded string"))
  100. .takes_value(false),
  101. )
  102. .arg(
  103. Arg::new("wallet")
  104. .short('w')
  105. .long("wallet")
  106. .help_heading(Some("Create a new wallet"))
  107. .takes_value(false),
  108. )
  109. .arg(
  110. Arg::new("info")
  111. .short('i')
  112. .long("info")
  113. .help_heading(Some("Request info from daemon"))
  114. .takes_value(false),
  115. )
  116. .arg(
  117. Arg::new("stop")
  118. .short('s')
  119. .long("stop")
  120. .help_heading(Some("Send a stop signal to the daemon"))
  121. .takes_value(false),
  122. )
  123. .arg(
  124. Arg::new("config")
  125. .help_heading(Some("Path for config file"))
  126. .long("config")
  127. .takes_value(true),
  128. )
  129. .subcommand(
  130. App::new("transfer")
  131. .about("Transfer DBTC between users")
  132. .arg(
  133. Arg::new("address")
  134. .value_name("RECEIVE_ADDRESS")
  135. .takes_value(true)
  136. .index(1)
  137. .help_heading(Some("Address of recipient"))
  138. .required(true),
  139. )
  140. .arg(
  141. Arg::new("amount")
  142. .value_name("AMOUNT")
  143. .takes_value(true)
  144. .index(2)
  145. .validator(is_u64)
  146. .help_heading(Some("Amount to send, in DBTC"))
  147. .required(true),
  148. ),
  149. )
  150. .subcommand(App::new("deposit").about("Deposit BTC for dBTC"))
  151. .subcommand(
  152. App::new("withdraw")
  153. .about("Withdraw BTC for dBTC")
  154. .arg(
  155. Arg::new("address")
  156. .value_name("RECEIVE_ADDRESS")
  157. .takes_value(true)
  158. .index(1)
  159. .help_heading(Some("Address of recipient"))
  160. .required(true),
  161. )
  162. .arg(
  163. Arg::new("amount")
  164. .value_name("AMOUNT")
  165. .takes_value(true)
  166. .index(2)
  167. .validator(is_u64)
  168. .help_heading(Some("Amount to send, in BTC"))
  169. .required(true),
  170. ),
  171. )
  172. .subcommand(App::new("deposit").about("Deposit BTC for dBTC"))
  173. .get_matches();
  174. let verbose = app.is_present("verbose");
  175. let cashier = app.is_present("cashier");
  176. let wallet = app.is_present("wallet");
  177. let key = app.is_present("key");
  178. let info = app.is_present("info");
  179. let hello = app.is_present("hello");
  180. let stop = app.is_present("stop");
  181. let get_key = app.is_present("getkey");
  182. let deposit = None;
  183. match app.subcommand_matches("deposit") {
  184. Some(_) => {
  185. //let deposit = Deposit::new();
  186. }
  187. None => {}
  188. }
  189. let mut transfer = None;
  190. match app.subcommand_matches("transfer") {
  191. Some(transfer_sub) => {
  192. let mut trn = TransferParams::new();
  193. if let Some(address) = transfer_sub.value_of("address") {
  194. trn.pub_key = address.to_string();
  195. }
  196. if let Some(amount) = transfer_sub.value_of("amount") {
  197. trn.amount = amount.parse()?;
  198. }
  199. transfer = Some(trn);
  200. }
  201. None => {}
  202. }
  203. let mut withdraw = None;
  204. match app.subcommand_matches("withdraw") {
  205. Some(withdraw_sub) => {
  206. let mut wdraw = WithdrawParams::new();
  207. if let Some(address) = withdraw_sub.value_of("address") {
  208. wdraw.pub_key = address.to_string();
  209. }
  210. if let Some(amount) = withdraw_sub.value_of("amount") {
  211. wdraw.amount = amount.parse()?;
  212. }
  213. withdraw = Some(wdraw);
  214. }
  215. None => {}
  216. }
  217. let config = Box::new(if let Some(config_path) = app.value_of("config") {
  218. Some(std::path::Path::new(config_path).to_path_buf())
  219. } else {
  220. None
  221. });
  222. Ok(Self {
  223. verbose,
  224. cashier,
  225. wallet,
  226. key,
  227. get_key,
  228. info,
  229. hello,
  230. stop,
  231. deposit,
  232. transfer,
  233. withdraw,
  234. config,
  235. })
  236. }
  237. }