user_adapter.rs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. use crate::cli::{TransferParams, WithdrawParams};
  2. use crate::client::ClientResult;
  3. use crate::serial::serialize;
  4. use crate::service::btc::PubAddress;
  5. use crate::wallet::WalletDb;
  6. use crate::{Error, Result};
  7. use log::*;
  8. use async_std::sync::Arc;
  9. use std::string::ToString;
  10. pub type UserAdapterPtr = Arc<UserAdapter>;
  11. pub type TransferChannel = (
  12. async_channel::Sender<TransferParams>,
  13. async_channel::Receiver<ClientResult<()>>,
  14. );
  15. pub type DepositChannel = (
  16. async_channel::Sender<jubjub::SubgroupPoint>,
  17. async_channel::Receiver<ClientResult<bitcoin::util::address::Address>>,
  18. );
  19. pub type WithdrawChannel = (
  20. async_channel::Sender<String>,
  21. async_channel::Receiver<ClientResult<jubjub::SubgroupPoint>>,
  22. );
  23. pub struct UserAdapter {
  24. pub wallet: Arc<WalletDb>,
  25. transfer_channel: TransferChannel,
  26. deposit_channel: DepositChannel,
  27. withdraw_channel: WithdrawChannel,
  28. }
  29. impl UserAdapter {
  30. pub fn new(
  31. wallet: Arc<WalletDb>,
  32. transfer_channel: TransferChannel,
  33. deposit_channel: DepositChannel,
  34. withdraw_channel: WithdrawChannel,
  35. ) -> Result<Self> {
  36. debug!(target: "ADAPTER", "new() [CREATING NEW WALLET]");
  37. Ok(Self {
  38. wallet,
  39. transfer_channel,
  40. deposit_channel,
  41. withdraw_channel,
  42. })
  43. }
  44. pub fn handle_input(self: Arc<Self>) -> Result<jsonrpc_core::IoHandler> {
  45. let mut io = jsonrpc_core::IoHandler::new();
  46. io.add_sync_method("say_hello", |_| {
  47. Ok(jsonrpc_core::Value::String("hello world!".into()))
  48. });
  49. let self1 = self.clone();
  50. io.add_method("get_key", move |_| {
  51. let self2 = self1.clone();
  52. async move {
  53. let pub_key = self2.get_key()?;
  54. Ok(jsonrpc_core::Value::String(pub_key))
  55. }
  56. });
  57. let self1 = self.clone();
  58. io.add_method("get_cash_public", move |_| {
  59. let self2 = self1.clone();
  60. async move {
  61. let cash_key = self2.get_cash_public()?;
  62. Ok(jsonrpc_core::Value::String(cash_key))
  63. }
  64. });
  65. let self1 = self.clone();
  66. io.add_method("get_info", move |_| {
  67. let self2 = self1.clone();
  68. async move {
  69. self2.get_info();
  70. Ok(jsonrpc_core::Value::Null)
  71. }
  72. });
  73. let self1 = self.clone();
  74. io.add_method("stop", move |_| {
  75. let self2 = self1.clone();
  76. async move {
  77. self2.stop();
  78. Ok(jsonrpc_core::Value::Null)
  79. }
  80. });
  81. let self1 = self.clone();
  82. io.add_method("create_wallet", move |_| {
  83. let self2 = self1.clone();
  84. async move {
  85. self2.init_db()?;
  86. Ok(jsonrpc_core::Value::String(
  87. "wallet creation successful".into(),
  88. ))
  89. }
  90. });
  91. let self1 = self.clone();
  92. io.add_method("key_gen", move |_| {
  93. let self2 = self1.clone();
  94. async move {
  95. self2.key_gen()?;
  96. Ok(jsonrpc_core::Value::String(
  97. "key generation successful".into(),
  98. ))
  99. }
  100. });
  101. let self1 = self.clone();
  102. io.add_method("deposit", move |_| {
  103. let self2 = self1.clone();
  104. async move {
  105. let btckey = self2.deposit().await?;
  106. Ok(jsonrpc_core::Value::String(format!("{}", btckey)))
  107. }
  108. });
  109. let self1 = self.clone();
  110. io.add_method("transfer", move |params: jsonrpc_core::Params| {
  111. let self2 = self1.clone();
  112. async move {
  113. let parsed: TransferParams = params.parse().unwrap();
  114. let amount = parsed.amount.clone();
  115. let address = parsed.pub_key.clone();
  116. self2.transfer(parsed).await?;
  117. Ok(jsonrpc_core::Value::String(format!(
  118. "transfered {} DRK to {}",
  119. amount, address
  120. )))
  121. }
  122. });
  123. let self1 = self.clone();
  124. io.add_method("withdraw", move |params: jsonrpc_core::Params| {
  125. let self2 = self1.clone();
  126. async move {
  127. let parsed: WithdrawParams = params.parse().unwrap();
  128. let amount = parsed.amount.clone();
  129. let address = parsed.pub_key.clone();
  130. self2.withdraw(parsed).await?;
  131. Ok(jsonrpc_core::Value::String(format!(
  132. "withdrawing {} BTC to {}...",
  133. amount, address
  134. )))
  135. }
  136. });
  137. Ok(io)
  138. }
  139. pub fn init_db(&self) -> Result<()> {
  140. debug!(target: "adapter", "init_db() [START]");
  141. self.wallet.init_db()?;
  142. Ok(())
  143. }
  144. pub fn key_gen(&self) -> Result<()> {
  145. debug!(target: "adapter", "key_gen() [START]");
  146. let (public, private) = self.wallet.key_gen();
  147. debug!(target: "adapter", "Created keypair...");
  148. debug!(target: "adapter", "Attempting to write to database...");
  149. self.wallet.put_keypair(public, private)?;
  150. Ok(())
  151. }
  152. pub fn get_key(&self) -> Result<String> {
  153. debug!(target: "adapter", "get_key() [START]");
  154. let key_public = self.wallet.get_public()?;
  155. let bs58_address = bs58::encode(serialize(&key_public)).into_string();
  156. Ok(bs58_address)
  157. }
  158. pub fn get_cash_public(&self) -> Result<String> {
  159. debug!(target: "adapter", "get_cash_public() [START]");
  160. let cashier_public = self.wallet.get_cashier_public()?;
  161. let bs58_address = bs58::encode(serialize(&cashier_public)).into_string();
  162. Ok(bs58_address)
  163. }
  164. pub async fn deposit(&self) -> Result<PubAddress> {
  165. debug!(target: "deposit", "deposit: START");
  166. let (public, private) = self.wallet.key_gen();
  167. self.wallet.put_keypair(public, private)?;
  168. let dkey = self.wallet.get_public()?;
  169. self.deposit_channel.0.send(dkey).await?;
  170. self.deposit_channel
  171. .1
  172. .recv()
  173. .await?
  174. .map_err(|err| Error::from(err))
  175. }
  176. async fn transfer(&self, transfer_params: TransferParams) -> Result<()> {
  177. self.transfer_channel.0.send(transfer_params).await?;
  178. self.transfer_channel
  179. .1
  180. .recv()
  181. .await?
  182. .map_err(|err| Error::from(err))
  183. }
  184. async fn withdraw(&self, withdraw_params: WithdrawParams) -> Result<()> {
  185. debug!(target: "withdraw", "withdraw: START");
  186. // do the key exchange
  187. self.withdraw_channel
  188. .0
  189. .send(withdraw_params.pub_key)
  190. .await?;
  191. // send the drk
  192. if let Ok(key) = self.withdraw_channel.1.recv().await? {
  193. let mut transfer_params = TransferParams::new();
  194. transfer_params.pub_key = key.to_string();
  195. transfer_params.amount = withdraw_params.amount;
  196. self.transfer_channel.0.send(transfer_params).await?;
  197. self.transfer_channel
  198. .1
  199. .recv()
  200. .await?
  201. .map_err(|err| Error::from(err))?;
  202. }
  203. Ok(())
  204. }
  205. pub fn get_info(&self) {}
  206. pub fn say_hello(&self) {}
  207. pub fn stop(&self) {}
  208. }