adapter.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 log::*;
  7. use async_std::sync::Arc;
  8. pub type AdapterPtr = Arc<RpcAdapter>;
  9. pub type DepositChannel = (
  10. async_channel::Sender<jubjub::SubgroupPoint>,
  11. async_channel::Receiver<Option<bitcoin::util::address::Address>>,
  12. );
  13. pub type WithdrawChannel = (
  14. async_channel::Sender<WithdrawParams>,
  15. async_channel::Receiver<jubjub::SubgroupPoint>,
  16. );
  17. pub struct RpcAdapter {
  18. pub wallet: Arc<WalletDb>,
  19. publish_tx_send: async_channel::Sender<TransferParams>,
  20. deposit_channel: DepositChannel,
  21. withdraw_channel: WithdrawChannel,
  22. }
  23. impl RpcAdapter {
  24. pub fn new(
  25. wallet: Arc<WalletDb>,
  26. publish_tx_send: async_channel::Sender<TransferParams>,
  27. deposit_channel: DepositChannel,
  28. withdraw_channel: WithdrawChannel,
  29. ) -> Result<Self> {
  30. debug!(target: "ADAPTER", "new() [CREATING NEW WALLET]");
  31. Ok(Self {
  32. wallet,
  33. publish_tx_send,
  34. deposit_channel,
  35. withdraw_channel,
  36. })
  37. }
  38. pub fn init_db(&self) -> Result<()> {
  39. debug!(target: "adapter", "init_db() [START]");
  40. self.wallet.init_db()?;
  41. Ok(())
  42. }
  43. pub fn init_cashier_db(&self) -> Result<()> {
  44. debug!(target: "adapter", "init_cashier_db() [START]");
  45. self.wallet.init_cashier_db()?;
  46. Ok(())
  47. }
  48. pub fn key_gen(&self) -> Result<()> {
  49. debug!(target: "adapter", "key_gen() [START]");
  50. let (public, private) = self.wallet.key_gen();
  51. debug!(target: "adapter", "Created keypair...");
  52. debug!(target: "adapter", "Attempting to write to database...");
  53. self.wallet.put_keypair(public, private)?;
  54. Ok(())
  55. }
  56. pub fn cash_key_gen(&self) -> Result<()> {
  57. debug!(target: "adapter", "key_gen() [START]");
  58. let (public, private) = self.wallet.cash_key_gen();
  59. self.wallet.put_keypair(public, private)?;
  60. Ok(())
  61. }
  62. pub fn get_key(&self) -> Result<String> {
  63. debug!(target: "adapter", "get_key() [START]");
  64. let key_public = self.wallet.get_public()?;
  65. let bs58_address = bs58::encode(serialize(&key_public)).into_string();
  66. Ok(bs58_address)
  67. }
  68. pub fn get_cash_key(&self) -> Result<String> {
  69. debug!(target: "adapter", "get_cash_key() [START]");
  70. let cashier_public = self.wallet.get_cashier_public()?;
  71. let bs58_address = bs58::encode(serialize(&cashier_public)).into_string();
  72. Ok(bs58_address)
  73. }
  74. pub async fn deposit(&self) -> Result<PubAddress> {
  75. debug!(target: "deposit", "deposit: START");
  76. let (public, private) = self.wallet.key_gen();
  77. self.wallet.put_keypair(public, private)?;
  78. let dkey = self.wallet.get_public()?;
  79. self.deposit_channel.0.send(dkey).await?;
  80. match self.deposit_channel.1.recv().await? {
  81. Some(key) => Ok(key),
  82. None => Err(Error::CashierNoReply),
  83. }
  84. }
  85. pub async fn transfer(&self, transfer_params: TransferParams) -> Result<()> {
  86. self.publish_tx_send.send(transfer_params).await?;
  87. Ok(())
  88. }
  89. pub async fn withdraw(&self, withdraw_params: WithdrawParams) -> Result<()> {
  90. self.withdraw_channel.0.send(withdraw_params).await?;
  91. Ok(())
  92. }
  93. pub fn get_info(&self) {}
  94. pub fn say_hello(&self) {}
  95. pub fn stop(&self) {}
  96. }