From 6a025fe17c23240a9833b46561df8f39746ecde9 Mon Sep 17 00:00:00 2001 From: parazyd Date: Wed, 29 Sep 2021 18:49:34 +0200 Subject: [PATCH] Remove old client adapter code. --- src/rpc/adapter_old/client_adapter.rs | 228 -------------------------- src/rpc/adapter_old/mod.rs | 3 - 2 files changed, 231 deletions(-) delete mode 100644 src/rpc/adapter_old/client_adapter.rs delete mode 100644 src/rpc/adapter_old/mod.rs diff --git a/src/rpc/adapter_old/client_adapter.rs b/src/rpc/adapter_old/client_adapter.rs deleted file mode 100644 index b29b4cf41..000000000 --- a/src/rpc/adapter_old/client_adapter.rs +++ /dev/null @@ -1,228 +0,0 @@ -//use crate::cli::jubjub::Fr; -use crate::client::{Client, ClientFailed}; -use crate::serial::{deserialize, serialize, Decodable}; -use crate::service::CashierClient; -use crate::{Error, Result}; - -use jsonrpc_core::BoxFuture; -use jsonrpc_derive::rpc; -use log::*; - -use async_std::sync::{Arc, Mutex}; -use futures::FutureExt; - -/// Rpc trait -#[rpc(server)] -pub trait RpcClient { - /// say hello - #[rpc(name = "say_hello")] - fn say_hello(&self) -> Result; - - /// get key - #[rpc(name = "get_key")] - fn get_key(&self) -> BoxFuture>; - - /// create wallet - #[rpc(name = "create_wallet")] - fn create_wallet(&self) -> BoxFuture>; - - /// key gen - #[rpc(name = "key_gen")] - fn key_gen(&self) -> BoxFuture>; - - /// transfer - #[rpc(name = "transfer")] - fn transfer( - &self, - asset_id: Vec, - pub_key: Vec, - amount: f64, - ) -> BoxFuture>; - - /// withdraw - #[rpc(name = "withdraw")] - fn withdraw( - &self, - asset_id: Vec, - pub_key: Vec, - amount: f64, - ) -> BoxFuture>; - - /// deposit - #[rpc(name = "deposit")] - fn deposit(&self, asset_id: Vec) -> BoxFuture>; -} - -pub struct RpcClientAdapter { - client: Arc>, - cashier_client: Arc>, -} - -impl RpcClientAdapter { - pub fn new(client: Arc>, cashier_client: Arc>) -> Self { - Self { - client, - cashier_client, - } - } - - async fn get_key_process(client: Arc>) -> Result { - let key_public = client.lock().await.state.wallet.get_keypairs()?[0].public; - let bs58_address = bs58::encode(serialize(&key_public)).into_string(); - Ok(bs58_address) - } - - async fn create_wallet_process(client: Arc>) -> Result { - client.lock().await.state.wallet.init_db()?; - Ok("wallet creation successful".into()) - } - - async fn key_gen_process(client: Arc>) -> Result { - debug!(target: "RPC USER ADAPTER", "Generating keypair..."); - debug!(target: "RPC USER ADAPTER", "Attempting to write to database..."); - client.lock().await.state.wallet.key_gen()?; - Ok("key generation successful".into()) - } - - async fn transfer_process( - client: Arc>, - asset_id: Vec, - address: Vec, - amount: f64, - ) -> Result { - let pub_key: String = deserialize(&address)?; - let address = bs58::decode(pub_key.clone()) - .into_vec() - .map_err(|_| ClientFailed::InvalidAddress(pub_key.clone()))?; - - let address: jubjub::SubgroupPoint = - deserialize(&address).map_err(|_| ClientFailed::InvalidAddress(pub_key))?; - - let asset_id: jubjub::Fr = deserialize(&asset_id)?; - - client - .lock() - .await - .transfer(asset_id, address.clone(), amount) - .await?; - - Ok(format!("transfered {} DRK to {}", amount, address)) - } - - async fn withdraw_process( - client: Arc>, - cashier_client: Arc>, - asset_id: Vec, - address: Vec, - amount: f64, - ) -> Result { - let asset_id: jubjub::Fr = deserialize(&asset_id)?; - - let drk_public = cashier_client - .lock() - .await - .withdraw(asset_id, address) - .await - .map_err(|err| ClientFailed::from(err))?; - - if let Some(drk_addr) = drk_public { - client - .lock() - .await - .transfer(asset_id, drk_addr.clone(), amount) - .await?; - - return Ok(format!( - "sending {} drk to provided address for withdrawing: {} ", - amount, drk_addr - )); - } else { - return Err(Error::from(ClientFailed::UnableToGetWithdrawAddress)); - } - } - - async fn deposit_process( - client: Arc>, - cashier_client: Arc>, - asset_id: Vec, - ) -> Result - where - T: Decodable + ToString, - { - let asset_id: jubjub::Fr = deserialize(&asset_id)?; - let deposit_addr = client.lock().await.state.wallet.get_keypairs()?[0].public; - let coin_public = cashier_client - .lock() - .await - .get_address(asset_id, deposit_addr) - .await - .map_err(|err| ClientFailed::from(err))?; - - if let Some(coin_addr) = coin_public { - let pub_k: T = deserialize(&coin_addr)?; - return Ok(pub_k.to_string()); - } else { - return Err(Error::from(ClientFailed::UnableToGetDepositAddress)); - } - } -} - -impl RpcClient for RpcClientAdapter { - fn say_hello(&self) -> Result { - debug!(target: "RPC USER ADAPTER", "say_hello() [START]"); - Ok(String::from("hello world")) - } - - fn get_key(&self) -> BoxFuture> { - debug!(target: "RPC USER ADAPTER", "get_key() [START]"); - Self::get_key_process(self.client.clone()).boxed() - } - - fn create_wallet(&self) -> BoxFuture> { - debug!(target: "RPC USER ADAPTER", "create_wallet() [START]"); - Self::create_wallet_process(self.client.clone()).boxed() - } - - fn key_gen(&self) -> BoxFuture> { - debug!(target: "RPC USER ADAPTER", "key_gen() [START]"); - Self::key_gen_process(self.client.clone()).boxed() - } - - fn transfer( - &self, - asset_id: Vec, - pub_key: Vec, - amount: f64, - ) -> BoxFuture> { - debug!(target: "RPC USER ADAPTER", "transfer() [START]"); - Self::transfer_process(self.client.clone(), asset_id, pub_key, amount).boxed() - } - - fn withdraw( - &self, - asset_id: Vec, - pub_key: Vec, - amount: f64, - ) -> BoxFuture> { - debug!(target: "RPC USER ADAPTER", "withdraw() [START]"); - Self::withdraw_process( - self.client.clone(), - self.cashier_client.clone(), - asset_id, - pub_key, - amount, - ) - .boxed() - } - - fn deposit(&self, asset_id: Vec) -> BoxFuture> { - debug!(target: "RPC USER ADAPTER", "deposit() [START]"); - #[cfg(feature = "default")] - Self::deposit_process::( - self.client.clone(), - self.cashier_client.clone(), - asset_id, - ) - .boxed() - } -} diff --git a/src/rpc/adapter_old/mod.rs b/src/rpc/adapter_old/mod.rs deleted file mode 100644 index ee8fc80b8..000000000 --- a/src/rpc/adapter_old/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod client_adapter; - -pub use client_adapter::{RpcClient, RpcClientAdapter};