Эх сурвалжийг харах

New btc keys create a connection to existing Electrum Client

Janus 5 жил өмнө
parent
commit
e440409d81

+ 33 - 9
src/service/btc.rs

@@ -22,20 +22,14 @@ pub type PrivKey = PrivateKey;
 pub struct BitcoinKeys {
     secret_key: SecretKey,
     bitcoin_private_key: PrivateKey,
-    btc_client: ElectrumClient,
+    btc_client: Arc<ElectrumClient>,
     pub bitcoin_public_key: PublicKey,
     pub pub_address: Address,
     pub script: Script,
 }
 
 impl BitcoinKeys {
-    pub fn new() -> Result<Arc<BitcoinKeys>> {
-        // Pull address from config later
-        let client_address = "";
-
-        // create client
-        let mut btc_client = ElectrumClient::new(&client_address).unwrap();
-
+    pub fn new(btc_client: Arc<ElectrumClient>) -> Result<Arc<BitcoinKeys>> {
         let context = secp256k1::Secp256k1::new();
 
         // Probably not good enough for release
@@ -65,13 +59,43 @@ impl BitcoinKeys {
         Ok(Arc::new(BitcoinKeys {
             secret_key,
             bitcoin_private_key,
-            bitcoin_public_key,
             btc_client,
+            bitcoin_public_key,
             pub_address,
             script,
         }))
     }
 
+    pub async fn start_subscribe(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
+        debug!(target: "BTC", "Subscribe");
+
+        // Check if script is already subscribed
+        let status_start = self.btc_client.script_subscribe(&self.script).unwrap();
+        let subscribe_status_task =
+            executor.spawn(self.subscribe_status_loop(status_start, executor.clone()));
+        debug!(target: "BTC", "Subscribed to scripthash");
+        let _ = subscribe_status_task.cancel().await;
+
+        Ok(())
+    }
+
+    async fn subscribe_status_loop(
+        self: Arc<Self>,
+        status_start: Option<electrum_client::ScriptStatus>,
+        executor: Arc<Executor<'_>>,
+    ) -> Result<Option<String>> {
+        loop {
+            let check = self.btc_client.script_pop(&self.script);
+            match check {
+                // Script has a notification update
+                Ok(status) => {}
+                // No update, repeat
+                Err(_) => break,
+            }
+        }
+        Ok(None)
+    }
+
     // This should do a db lookup to return the same obj
     pub fn address_from_slice(key: &[u8]) -> Result<Address> {
         let pub_key = PublicKey::from_slice(key).unwrap();

+ 6 - 41
src/service/cashier.rs

@@ -125,40 +125,6 @@ impl CashierService {
 
         Ok(())
     }
-    async fn start_subscribe(
-        self: Arc<Self>,
-        script: Script,
-        executor: Arc<Executor<'_>>,
-    ) -> Result<()> {
-        debug!(target: "BTC", "Subscribe");
-
-        // Check if script is already subscribed
-        let status_start = self.btc_client.script_subscribe(&script).unwrap();
-        let subscribe_status_task =
-            executor.spawn(self.subscribe_status_loop(status_start, script, executor.clone()));
-        debug!(target: "BTC", "Subscribed to scripthash");
-        let _ = subscribe_status_task.cancel().await;
-
-        Ok(())
-    }
-
-    async fn subscribe_status_loop(
-        self: Arc<Self>,
-        status_start: Option<electrum_client::ScriptStatus>,
-        script: Script,
-        executor: Arc<Executor<'_>>,
-    ) -> Result<()> {
-        loop {
-            let check = self.btc_client.script_pop(&script);
-            match check {
-                // Script has a notification update
-                Ok(status) => {}
-                // No update, repeat
-                Err(_) => break,
-            }
-        }
-        Ok(())
-    }
 
     async fn handle_request_loop(
         self: Arc<Self>,
@@ -204,10 +170,10 @@ impl CashierService {
                 let zkpub = request.get_payload();
 
                 //check if key has already been issued
-                //let check = cashier_wallet.get_keys_by_dkey(&zkpub);
+                let check = cashier_wallet.get_keys_by_dkey(&zkpub);
 
                 // Generate bitcoin Address
-                let btc_keys = BitcoinKeys::new().unwrap();
+                let btc_keys = BitcoinKeys::new(btc_client).unwrap();
 
                 let btc_pub = btc_keys.get_pubkey();
                 let btc_priv = btc_keys.get_privkey();
@@ -215,7 +181,7 @@ impl CashierService {
                 let script = btc_keys.get_script();
 
                 // add pairings to db
-                //let _result = cashier_wallet.put_exchange_keys(zkpub, *btc_priv, *btc_pub);
+                let result = cashier_wallet.put_exchange_keys(zkpub, *btc_priv, *btc_pub);
 
                 let mut reply = Reply::from(&request, CashierError::NoError as u32, vec![]);
 
@@ -228,10 +194,9 @@ impl CashierService {
                 // start scheduler for checking balance
                 debug!(target: "BTC", "Subscribing");
 
-                //let _ = self.btc_client.start_subscribe(script, executor.clone()).await?;
-                //let _ = self.btc_client.script_subscribe(&script).unwrap();
-                //self.mint_dbtc(zkpub, 100);
-                //let _ = self.subscribe_to_address(script);
+                let _ = btc_keys.start_subscribe(executor.clone()).await?;
+
+                //self.mint_dbtc(deserialize(&zkpub).unwrap(), 100);
 
                 info!("Waiting for address balance");
             }