user_adapter.rs 6.9 KB

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