adapter.rs 3.4 KB

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