made Asset type to handle drk cli inputs

This commit is contained in:
lunar-mining
2021-09-11 08:58:50 +02:00
parent 62292e2857
commit 1e96c744cc
3 changed files with 40 additions and 12 deletions

4
Cargo.lock generated
View File

@@ -3885,9 +3885,9 @@ checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d"
[[package]]
name = "sha2"
version = "0.9.6"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9204c41a1597a8c5af23c82d1c921cb01ec0a4c59e07a9c7306062829a3903f3"
checksum = "b69f9a4c9740d74c5baa3fd2e547f9525fa8088a8a958e0ca2409a514e33f5fa"
dependencies = [
"block-buffer 0.9.0",
"cfg-if 1.0.0",

View File

@@ -23,7 +23,7 @@ zcash_proofs = "0.5.0"
#bench-utils = { git = "https://github.com/scipr-lab/zexe", features = ["print-trace"]}
rand = "0.7.3"
rand_core = "0.5.1"
sha2 = "0.9.1"
sha2 = "0.9.8"
rand_xorshift = "0.2"
blake2s_simd = "0.5"
blake2b_simd = "0.5.11"

View File

@@ -1,9 +1,11 @@
//use super::cli_config::DrkCliConfig;
use crate::Result;
use blake2b_simd::Params;
use clap::{App, Arg};
use serde::Deserialize;
use crate::serial;
use std::path::PathBuf;
fn amount_f64(v: String) -> std::result::Result<(), String> {
@@ -16,7 +18,7 @@ fn amount_f64(v: String) -> std::result::Result<(), String> {
#[derive(Deserialize, Debug)]
pub struct TransferParams {
pub asset: String,
pub asset: Asset,
pub pub_key: String,
pub amount: f64,
}
@@ -24,7 +26,7 @@ pub struct TransferParams {
impl TransferParams {
pub fn new() -> Self {
Self {
asset: String::new(),
asset: Asset::new(),
pub_key: String::new(),
amount: 0.0,
}
@@ -32,20 +34,20 @@ impl TransferParams {
}
pub struct Deposit {
pub asset: String,
pub asset: Asset,
}
impl Deposit {
pub fn new() -> Self {
Self {
asset: String::new(),
asset: Asset::new(),
}
}
}
#[derive(Deserialize, Debug)]
pub struct WithdrawParams {
pub asset: String,
pub asset: Asset,
pub pub_key: String,
pub amount: f64,
}
@@ -53,13 +55,36 @@ pub struct WithdrawParams {
impl WithdrawParams {
pub fn new() -> Self {
Self {
asset: String::new(),
asset: Asset::new(),
pub_key: String::new(),
amount: 0.0,
}
}
}
#[derive(Deserialize, Debug)]
pub struct Asset {
pub ticker: String,
pub id: Vec<u8>,
}
impl Asset {
pub fn new() -> Self {
Self {
ticker: String::new(),
id: Vec::new(),
}
}
pub fn id_hash(&self, ticker: &String) -> Result<Vec<u8>> {
let mut hasher = Params::new().hash_length(64).to_state();
hasher.update(ticker.as_bytes());
let result = hasher.finalize();
let scalar = jubjub::Fr::from_bytes_wide(result.as_array());
let id = serial::serialize(&scalar);
Ok(id)
}
}
pub struct DrkCli {
pub verbose: bool,
pub wallet: bool,
@@ -218,7 +243,8 @@ impl DrkCli {
Some(deposit_sub) => {
let mut dep = Deposit::new();
if let Some(asset) = deposit_sub.value_of("asset") {
dep.asset = asset.to_string();
dep.asset.ticker = asset.to_string();
dep.asset.id = dep.asset.id_hash(&dep.asset.ticker)?;
}
deposit = Some(dep);
}
@@ -230,7 +256,8 @@ impl DrkCli {
Some(transfer_sub) => {
let mut trn = TransferParams::new();
if let Some(asset) = transfer_sub.value_of("asset") {
trn.asset = asset.to_string();
trn.asset.ticker = asset.to_string();
trn.asset.id = trn.asset.id_hash(&trn.asset.ticker)?;
}
if let Some(address) = transfer_sub.value_of("address") {
trn.pub_key = address.to_string();
@@ -248,7 +275,8 @@ impl DrkCli {
Some(withdraw_sub) => {
let mut wdraw = WithdrawParams::new();
if let Some(asset) = withdraw_sub.value_of("asset") {
wdraw.asset = asset.to_string();
wdraw.asset.ticker = asset.to_string();
wdraw.asset.id = wdraw.asset.id_hash(&wdraw.asset.ticker)?;
}
if let Some(address) = withdraw_sub.value_of("address") {
wdraw.pub_key = address.to_string();