浏览代码

check from walletdb if the cashier public key is valid

ghassmo 4 年之前
父节点
当前提交
78832bed3f
共有 2 个文件被更改,包括 19 次插入33 次删除
  1. 14 25
      src/client/client.rs
  2. 5 8
      src/wallet/walletdb.rs

+ 14 - 25
src/client/client.rs

@@ -72,7 +72,7 @@ impl Client {
             nullifiers,
             mint_pvk,
             spend_pvk,
-            wallet
+            wallet,
         };
 
         // create gateway client
@@ -111,8 +111,7 @@ impl Client {
 
         let mut io = IoHandler::new();
 
-        let rpc_client_adapter =
-            RpcClientAdapter::new(client_mutex.clone(), cashier_mutex.clone());
+        let rpc_client_adapter = RpcClientAdapter::new(client_mutex.clone(), cashier_mutex.clone());
 
         io.extend_with(rpc_client_adapter.to_delegate());
 
@@ -123,17 +122,12 @@ impl Client {
         let _ = jsonserver::start(executor.clone(), rpc_url, io).await?;
 
         // start subscriber
-        Client::connect_to_subscriber(client_mutex.clone(), executor.clone())
-            .await?;
+        Client::connect_to_subscriber(client_mutex.clone(), executor.clone()).await?;
 
         Ok(())
     }
 
-    pub async fn transfer(
-        self: &mut Client,
-        pub_key: String,
-        amount: f64,
-    ) -> Result<()> {
+    pub async fn transfer(self: &mut Client, pub_key: String, amount: f64) -> Result<()> {
         let address = bs58::decode(pub_key.clone())
             .into_vec()
             .map_err(|_| ClientFailed::UnvalidAddress(pub_key.clone()))?;
@@ -225,15 +219,11 @@ pub struct State {
 }
 
 impl ProgramState for State {
-    fn is_valid_cashier_public_key(&self, _public: &jubjub::SubgroupPoint) -> bool {
-        // TODO create a function in walletdb to check if it's a valid cashier public key
-        //let conn = Connection::open(self.wallet_path.clone()).expect("Connect to database");
-        //let mut stmt = conn
-        //    .prepare("SELECT key_public FROM cashier WHERE key_public IN (SELECT key_public)")
-        //    .expect("Generate statement");
-        //stmt.exists([1i32]).expect("Read database")
-        // do actual validity check
-        true
+    fn is_valid_cashier_public_key(&self, public: &jubjub::SubgroupPoint) -> bool {
+        self.wallet
+            .get_cashier_public_keys()
+            .expect("Get cashier public keys")
+            .contains(public)
     }
 
     fn is_valid_merkle(&self, merkle_root: &MerkleNode) -> bool {
@@ -277,7 +267,8 @@ impl State {
             // Also update all the coin witnesses
             for (coin_id, witness) in self.wallet.get_witnesses()?.iter_mut() {
                 witness.append(node).expect("Append to witness");
-                self.wallet.update_witness(coin_id.clone(), witness.clone())?;
+                self.wallet
+                    .update_witness(coin_id.clone(), witness.clone())?;
             }
 
             if let Some((note, secret)) = self.try_decrypt_note(enc_note).await {
@@ -293,16 +284,14 @@ impl State {
                 // Make a new witness for this coin
                 let witness = IncrementalWitness::from_tree(&self.tree);
 
-                self.wallet.put_own_coins(coin.clone(), note.clone(),  secret, witness.clone())?;
+                self.wallet
+                    .put_own_coins(coin.clone(), note.clone(), secret, witness.clone())?;
             }
         }
         Ok(())
     }
 
-    async fn try_decrypt_note(
-        &self,
-        ciphertext: EncryptedNote,
-    ) -> Option<(Note, jubjub::Fr)> {
+    async fn try_decrypt_note(&self, ciphertext: EncryptedNote) -> Option<(Note, jubjub::Fr)> {
         let secret = self.wallet.get_private().ok()?;
         match ciphertext.decrypt(&secret) {
             Ok(note) => {

+ 5 - 8
src/wallet/walletdb.rs

@@ -233,7 +233,7 @@ impl WalletDb {
         Ok(public)
     }
 
-    pub fn get_cashier_public(&self) -> Result<jubjub::SubgroupPoint> {
+    pub fn get_cashier_public_keys(&self) -> Result<Vec<jubjub::SubgroupPoint>> {
         debug!(target: "WALLETDB", "Returning keys...");
         let conn = Connection::open(&self.path)?;
         conn.pragma_update(None, "key", &self.password)?;
@@ -241,14 +241,11 @@ impl WalletDb {
         let key_iter = stmt.query_map([], |row| row.get(0))?;
         let mut pub_keys = Vec::new();
         for key in key_iter {
-            pub_keys.push(key?);
+            let public: jubjub::SubgroupPoint = self.get_value_deserialized(key?)?;
+            pub_keys.push(public);
         }
-        let public: jubjub::SubgroupPoint = self.get_value_deserialized(
-            pub_keys
-                .pop()
-                .expect("Load cashier public_key from walletdb"),
-        )?;
-        Ok(public)
+
+        Ok(pub_keys)
     }
 
     pub fn get_private(&self) -> Result<jubjub::Fr> {