user_adapter.rs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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>, mut io: jsonrpc_core::IoHandler) -> Result<jsonrpc_core::IoHandler> {
  40. io.add_sync_method("say_hello", |_| {
  41. Ok(jsonrpc_core::Value::String("hello world!".into()))
  42. });
  43. let self1 = self.clone();
  44. io.add_method("get_key", move |_| {
  45. let self2 = self1.clone();
  46. async move {
  47. let pub_key = self2.get_key()?;
  48. Ok(jsonrpc_core::Value::String(pub_key))
  49. }
  50. });
  51. let self1 = self.clone();
  52. io.add_method("get_cash_public", move |_| {
  53. let self2 = self1.clone();
  54. async move {
  55. let cash_key = self2.get_cash_public()?;
  56. Ok(jsonrpc_core::Value::String(cash_key))
  57. }
  58. });
  59. let self1 = self.clone();
  60. io.add_method("get_info", move |_| {
  61. let self2 = self1.clone();
  62. async move {
  63. self2.get_info();
  64. Ok(jsonrpc_core::Value::Null)
  65. }
  66. });
  67. let self1 = self.clone();
  68. io.add_method("stop", move |_| {
  69. let self2 = self1.clone();
  70. async move {
  71. self2.stop();
  72. Ok(jsonrpc_core::Value::Null)
  73. }
  74. });
  75. let self1 = self.clone();
  76. io.add_method("create_wallet", move |_| {
  77. let self2 = self1.clone();
  78. async move {
  79. self2.init_db()?;
  80. Ok(jsonrpc_core::Value::String(
  81. "wallet creation successful".into(),
  82. ))
  83. }
  84. });
  85. let self1 = self.clone();
  86. io.add_method("key_gen", move |_| {
  87. let self2 = self1.clone();
  88. async move {
  89. self2.key_gen()?;
  90. Ok(jsonrpc_core::Value::String(
  91. "key generation successful".into(),
  92. ))
  93. }
  94. });
  95. let self1 = self.clone();
  96. io.add_method("deposit", move |_| {
  97. let self2 = self1.clone();
  98. async move {
  99. let btckey = self2.deposit().await?;
  100. Ok(jsonrpc_core::Value::String(format!("{}", btckey)))
  101. }
  102. });
  103. let self1 = self.clone();
  104. io.add_method("transfer", move |params: jsonrpc_core::Params| {
  105. let self2 = self1.clone();
  106. async move {
  107. let parsed: TransferParams = params.parse().unwrap();
  108. let amount = parsed.amount.clone();
  109. let address = parsed.pub_key.clone();
  110. self2.transfer(parsed).await?;
  111. Ok(jsonrpc_core::Value::String(format!(
  112. "transfered {} DRK to {}",
  113. amount, address
  114. )))
  115. }
  116. });
  117. let self1 = self.clone();
  118. io.add_method("withdraw", move |params: jsonrpc_core::Params| {
  119. let self2 = self1.clone();
  120. async move {
  121. let parsed: WithdrawParams = params.parse().unwrap();
  122. let amount = parsed.amount.clone();
  123. let address = parsed.pub_key.clone();
  124. self2.withdraw(parsed).await?;
  125. Ok(jsonrpc_core::Value::String(format!(
  126. "withdrawing {} BTC to {}...",
  127. amount, address
  128. )))
  129. }
  130. });
  131. Ok(io)
  132. }
  133. pub fn init_db(&self) -> Result<()> {
  134. debug!(target: "adapter", "init_db() [START]");
  135. self.wallet.init_db()?;
  136. Ok(())
  137. }
  138. pub fn key_gen(&self) -> Result<()> {
  139. debug!(target: "adapter", "key_gen() [START]");
  140. let (public, private) = self.wallet.key_gen();
  141. debug!(target: "adapter", "Created keypair...");
  142. debug!(target: "adapter", "Attempting to write to database...");
  143. self.wallet.put_keypair(public, private)?;
  144. Ok(())
  145. }
  146. pub fn get_key(&self) -> Result<String> {
  147. debug!(target: "adapter", "get_key() [START]");
  148. let key_public = self.wallet.get_public()?;
  149. let bs58_address = bs58::encode(serialize(&key_public)).into_string();
  150. Ok(bs58_address)
  151. }
  152. pub fn get_cash_public(&self) -> Result<String> {
  153. debug!(target: "adapter", "get_cash_public() [START]");
  154. let cashier_public = self.wallet.get_cashier_public()?;
  155. let bs58_address = bs58::encode(serialize(&cashier_public)).into_string();
  156. Ok(bs58_address)
  157. }
  158. pub async fn deposit(&self) -> Result<PubAddress> {
  159. debug!(target: "deposit", "deposit: START");
  160. let (public, private) = self.wallet.key_gen();
  161. self.wallet.put_keypair(public, private)?;
  162. let dkey = self.wallet.get_public()?;
  163. self.deposit_channel.0.send(dkey).await?;
  164. match self.deposit_channel.1.recv().await? {
  165. Some(key) => Ok(key),
  166. None => Err(Error::CashierNoReply),
  167. }
  168. }
  169. pub async fn transfer(&self, transfer_params: TransferParams) -> Result<()> {
  170. self.publish_tx_send.send(transfer_params).await?;
  171. Ok(())
  172. }
  173. pub async fn withdraw(&self, withdraw_params: WithdrawParams) -> Result<()> {
  174. debug!(target: "withdraw", "withdraw: START");
  175. // do the key exchange
  176. self.withdraw_channel.0.send(withdraw_params.pub_key).await?;
  177. // send the drk
  178. if let Some(key) = self.withdraw_channel.1.recv().await? {
  179. let mut transfer_params = TransferParams::new();
  180. transfer_params.pub_key = key.to_string();
  181. transfer_params.amount = withdraw_params.amount;
  182. self.publish_tx_send.send(transfer_params).await?;
  183. }
  184. Ok(())
  185. }
  186. pub fn get_info(&self) {}
  187. pub fn say_hello(&self) {}
  188. pub fn stop(&self) {}
  189. }