Просмотр исходного кода

Check for existing dkey_pub to matched btc

Janus 5 лет назад
Родитель
Сommit
916a679a81
4 измененных файлов с 48 добавлено и 13 удалено
  1. 1 1
      res/cashier.sql
  2. 3 0
      src/service/btc.rs
  3. 14 4
      src/service/cashier.rs
  4. 30 8
      src/wallet/cashierdb.rs

+ 1 - 1
res/cashier.sql

@@ -7,5 +7,5 @@ CREATE TABLE IF NOT EXISTS keypairs(
     dkey_id INTEGER PRIMARY KEY NOT NULL,
     dkey_id INTEGER PRIMARY KEY NOT NULL,
     btc_key_private BLOB NOT NULL,
     btc_key_private BLOB NOT NULL,
     btc_key_public BLOB NOT NULL,
     btc_key_public BLOB NOT NULL,
-    txid BLOB NOT NULL
+    txid BLOB
 );
 );

+ 3 - 0
src/service/btc.rs

@@ -72,5 +72,8 @@ impl BitcoinKeys {
     pub fn get_pubkey(&self) -> &PublicKey {
     pub fn get_pubkey(&self) -> &PublicKey {
         &self.bitcoin_public_key
         &self.bitcoin_public_key
     }
     }
+    pub fn get_privkey(&self) -> &PrivateKey {
+        &self.bitcoin_private_key
+    }
 
 
 }
 }

+ 14 - 4
src/service/cashier.rs

@@ -132,23 +132,28 @@ impl CashierService {
     }
     }
     async fn handle_request(
     async fn handle_request(
         msg: (PeerId, Request),
         msg: (PeerId, Request),
-        _cashier_wallet: CashierDbPtr,
+        cashier_wallet: CashierDbPtr,
         send_queue: async_channel::Sender<(PeerId, Reply)>,
         send_queue: async_channel::Sender<(PeerId, Reply)>,
     ) -> Result<()> {
     ) -> Result<()> {
         let request = msg.1;
         let request = msg.1;
         let peer = msg.0;
         let peer = msg.0;
         match request.get_command() {
         match request.get_command() {
             0 => {
             0 => {
+                debug!(target: "Cashier", "Get command");
                 // Exchange zk_pubkey for bitcoin address
                 // Exchange zk_pubkey for bitcoin address
-                let _zkpub = request.get_payload();
+                let zkpub = request.get_payload();
+
+                //check if key has already been issued
+                let _check = cashier_wallet.get_keys_by_dkey(&zkpub);
 
 
                 // Generate bitcoin Address
                 // Generate bitcoin Address
                 let btc_keys = BitcoinKeys::new().unwrap();
                 let btc_keys = BitcoinKeys::new().unwrap();
 
 
                 let btc_pub = btc_keys.get_pubkey();
                 let btc_pub = btc_keys.get_pubkey();
+                let btc_priv = btc_keys.get_privkey();
 
 
-                // add to watchlist
-
+                // add pairings to db
+                let _result = cashier_wallet.put_exchange_keys(zkpub, *btc_priv, *btc_pub);
 
 
                 let mut reply = Reply::from(&request, CashierError::NoError as u32, vec![]);
                 let mut reply = Reply::from(&request, CashierError::NoError as u32, vec![]);
 
 
@@ -157,11 +162,15 @@ impl CashierService {
                 // send reply
                 // send reply
                 send_queue.send((peer, reply)).await?;
                 send_queue.send((peer, reply)).await?;
 
 
+                // add to watchlist
+
+
                 info!("Received dkey->btc msg");
                 info!("Received dkey->btc msg");
 
 
             }
             }
             1 => {
             1 => {
                 // Withdraw
                 // Withdraw
+                info!("Received withdraw request");
             }
             }
             _ => {
             _ => {
                 return Err(Error::ServicesError("received wrong command"));
                 return Err(Error::ServicesError("received wrong command"));
@@ -185,6 +194,7 @@ impl CashierClient {
     }
     }
 
 
     pub async fn start(&mut self) -> Result<()> {
     pub async fn start(&mut self) -> Result<()> {
+        debug!(target: "Cashier", "Start CashierClient");
         self.protocol.start().await?;
         self.protocol.start().await?;
 
 
         Ok(())
         Ok(())

+ 30 - 8
src/wallet/cashierdb.rs

@@ -49,20 +49,43 @@ impl CashierDb {
         }
         }
         Ok(())
         Ok(())
     }
     }
+
+    pub fn get_keys_by_dkey(&self, dkey_pub: &Vec<u8>) -> Result<()> {
+        println!("get keys...");
+        debug!(target: "CashierDB", "Check for existing dkey");
+        //let dkey_id = self.get_value_deserialized(dkey_pub)?;
+        // open connection
+        let conn = Connection::open(&self.path)?;
+        // unlock database
+        conn.pragma_update(None, "key", &self.password)?;
+
+        // let mut keypairs = conn.prepare("SELECT dkey_id FROM keypairs WHERE dkey_id = :dkey_id")?;
+        // let rows = keypairs.query_map::<Vec<u8>, _, _>(&[(":dkey_id", &secret)], |row| row.get(0))?;
+
+        let mut stmt = conn.prepare("SELECT * FROM keypairs where dkey_id = ?")?;
+        let mut rows = stmt.query([dkey_pub])?;
+        if let Some(_row) = rows.next()? {
+            println!("Got something");
+        } else {
+            println!("Did not get something");
+        }
+
+        Ok(())
+    }
+
     // Update to take BitcoinKeys instance instead
     // Update to take BitcoinKeys instance instead
     pub fn put_exchange_keys(
     pub fn put_exchange_keys(
         &self,
         &self,
-        dkey_pub: jubjub::SubgroupPoint,
+        dkey_pub: Vec<u8>,
         btc_private: PrivKey,
         btc_private: PrivKey,
         btc_public: PubKey,
         btc_public: PubKey,
-        // Successful btc tx id
-        txid: String,
+        //txid will be updated when exists
     ) -> Result<()> {
     ) -> Result<()> {
+        debug!(target: "CashierDB", "Put exchange keys");
         // prepare the values
         // prepare the values
-        let dkey_pub = self.get_value_serialized(&dkey_pub)?;
+        //let dkey_pub = self.get_value_serialized(&dkey_pub)?;
         let btc_private = btc_private.to_bytes();
         let btc_private = btc_private.to_bytes();
         let btc_public = btc_public.to_bytes();
         let btc_public = btc_public.to_bytes();
-        let txid = self.get_value_serialized(&txid)?;
 
 
         // open connection
         // open connection
         let conn = Connection::open(&self.path)?;
         let conn = Connection::open(&self.path)?;
@@ -70,13 +93,12 @@ impl CashierDb {
         conn.pragma_update(None, "key", &self.password)?;
         conn.pragma_update(None, "key", &self.password)?;
 
 
         conn.execute(
         conn.execute(
-            "INSERT INTO keypairs(dkey_id, btc_key_private, btc_key_public, txid)
-            VALUES (:dkey_id, :btc_key_private, :btc_key_public, :txid)",
+            "INSERT INTO keypairs(dkey_id, btc_key_private, btc_key_public)
+            VALUES (:dkey_id, :btc_key_private, :btc_key_public)",
             named_params! {
             named_params! {
             ":dkey_id": dkey_pub,
             ":dkey_id": dkey_pub,
             ":btc_key_private": btc_private,
             ":btc_key_private": btc_private,
             ":btc_key_private": btc_public,
             ":btc_key_private": btc_public,
-            ":txid": txid,
             },
             },
         )?;
         )?;
         Ok(())
         Ok(())