adapter.rs 3.1 KB

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