diff --git a/src/service/bitcoin_bridge.rs b/src/service/bitcoin_bridge.rs index 247ff47e6..0da04f0ac 100644 --- a/src/service/bitcoin_bridge.rs +++ b/src/service/bitcoin_bridge.rs @@ -132,14 +132,11 @@ impl CashierService { let peer = msg.0; match request.get_command() { 0 => { - + // Exchange zk_pubkey for bitcoin address } 1 => { - - } - 2 => { - + // Withdraw } _ => { return Err(Error::ServicesError("received wrong command")); @@ -148,3 +145,81 @@ impl CashierService { Ok(()) } } + +pub struct CashierClient { + protocol: ReqProtocol, + cashierstore: Arc, +} + +impl CashierClient { + pub fn new(addr: SocketAddr, rocks: RocksColumn) -> Result { + let protocol = ReqProtocol::new(addr, String::from("CASHIER CLIENT")); + + let cashierstore = CashierStore::new(rocks)?; + + Ok(CashierClient { + protocol, + cashierstore, + }) + } + + pub async fn start(&mut self) -> Result<()> { + self.protocol.start().await?; + self.sync().await?; + + Ok(()) + } + + pub async fn get_keys(&mut self, index: u64) -> Result> { + let rep = self + .protocol + .request( + //CashierCommand::GetKeys as u8, + serialize(&index), + &handle_error, + ) + .await?; + + if let Some(keys) = rep { + let keys: CashierKeys = deserialize(&keys)?; + //self.gateway_slabs_sub_s.send(slab.clone()).await?; + self.cashierstore.put(keys.clone())?; + return Ok(Some(keys)); + } + Ok(None) + } + + pub async fn put_keys(&mut self, mut keys: CashierKeys) -> Result<()> { + loop { + let last_index = self.sync().await?; + //keys.set_index(last_index + 1); + let keys = serialize(&keys); + + let rep = self + .protocol + .request(GatewayCommand::PutSlab as u8, slab.clone(), &handle_error) + .await?; + + if let Some(_) = rep { + break; + } + } + Ok(()) + } + + pub async fn get_last_index(&mut self) -> Result { + let rep = self + .protocol + .request(CashierCommand::GetLastIndex as u8, vec![], &handle_error) + .await?; + if let Some(index) = rep { + return Ok(deserialize(&index)?); + } + Ok(0) + } + + pub fn get_cashierstore(&self) -> Arc { + self.cashierstore.clone() + } + +}