cli_util.rs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::process::exit;
  19. use halo2_proofs::pasta::group::ff::PrimeField;
  20. use darkfi::{crypto::types::DrkTokenId, util::parse::decode_base10, Result};
  21. pub fn parse_value_pair(s: &str) -> Result<(u64, u64)> {
  22. let v: Vec<&str> = s.split(':').collect();
  23. if v.len() != 2 {
  24. eprintln!("Invalid value pair. Use a pair such as '13.37:11.0'.");
  25. exit(1);
  26. }
  27. // TODO: We shouldn't be hardcoding everything to 8 decimals.
  28. let val0 = decode_base10(v[0], 8, true);
  29. let val1 = decode_base10(v[1], 8, true);
  30. if val0.is_err() || val1.is_err() {
  31. eprintln!("Invalid value pair. Use a pair such as '13.37:11.0'.");
  32. exit(1);
  33. }
  34. Ok((val0.unwrap(), val1.unwrap()))
  35. }
  36. pub fn parse_token_pair(s: &str) -> Result<(String, String)> {
  37. let v: Vec<&str> = s.split(':').collect();
  38. if v.len() != 2 {
  39. eprintln!("Invalid token pair. Use a pair such as:");
  40. eprintln!("A7f1RKsCUUHrSXA7a9ogmwg8p3bs6F47ggsW826HD4yd:FCuoMii64H5Ee4eVWBjP18WTFS8iLUJmGi16Qti1xFQ2");
  41. exit(1);
  42. }
  43. let tok0 = bs58::decode(v[0]).into_vec();
  44. let tok1 = bs58::decode(v[1]).into_vec();
  45. if tok0.is_err() || tok1.is_err() {
  46. eprintln!("Invalid token pair. Use a pair such as:");
  47. eprintln!("A7f1RKsCUUHrSXA7a9ogmwg8p3bs6F47ggsW826HD4yd:FCuoMii64H5Ee4eVWBjP18WTFS8iLUJmGi16Qti1xFQ2");
  48. exit(1);
  49. }
  50. if tok0.as_ref().unwrap().len() != 32 ||
  51. DrkTokenId::from_repr(tok0.unwrap().try_into().unwrap()).is_some().unwrap_u8() == 0
  52. {
  53. eprintln!("Error: {} is not a valid token ID", v[0]);
  54. exit(1);
  55. }
  56. if tok1.as_ref().unwrap().len() != 32 ||
  57. DrkTokenId::from_repr(tok1.unwrap().try_into().unwrap()).is_some().unwrap_u8() == 0
  58. {
  59. eprintln!("Error: {} is not a valid token ID", v[1]);
  60. exit(1);
  61. }
  62. Ok((v[0].to_string(), v[1].to_string()))
  63. }