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

changed wallet unlock to proper pragma syntax

lunar-mining 5 лет назад
Родитель
Сommit
61a45931c1
1 измененных файлов с 94 добавлено и 94 удалено
  1. 94 94
      src/wallet/walletdb.rs

+ 94 - 94
src/wallet/walletdb.rs

@@ -8,7 +8,7 @@ use async_std::sync::{Arc, Mutex};
 use ff::Field;
 use ff::Field;
 use log::*;
 use log::*;
 use rand::rngs::OsRng;
 use rand::rngs::OsRng;
-use rusqlite::{named_params, params, Connection};
+use rusqlite::{named_params, params, OptionalExtension, DatabaseName, Connection};
 
 
 use std::path::PathBuf;
 use std::path::PathBuf;
 
 
@@ -56,7 +56,7 @@ impl WalletDB {
             let contents = include_str!("../../res/schema.sql");
             let contents = include_str!("../../res/schema.sql");
             let conn = Connection::open(&self.path)?;
             let conn = Connection::open(&self.path)?;
             debug!(target: "walletdb", "OPENED CONNECTION AT PATH {:?}", self.path);
             debug!(target: "walletdb", "OPENED CONNECTION AT PATH {:?}", self.path);
-            //conn.execute("PRAGMA key=(?1)", params![self.password])?;
+            conn.pragma_update(None, "key", &self.password)?;
             conn.execute_batch(&contents)?
             conn.execute_batch(&contents)?
         } else {
         } else {
             println!("Password is empty. You must set a password to use the wallet.");
             println!("Password is empty. You must set a password to use the wallet.");
@@ -93,7 +93,7 @@ impl WalletDB {
         // open connection
         // open connection
         let conn = Connection::open(&self.path)?;
         let conn = Connection::open(&self.path)?;
         // unlock database
         // unlock database
-        conn.execute("PRAGMA key=(?1)", params![self.password])?;
+        conn.pragma_update(None, "key", &self.password)?;
         // return key_id from key_private
         // return key_id from key_private
         let mut get_id =
         let mut get_id =
             conn.prepare("SELECT key_id FROM keys WHERE key_private = :key_private")?;
             conn.prepare("SELECT key_id FROM keys WHERE key_private = :key_private")?;
@@ -140,7 +140,7 @@ impl WalletDB {
     pub fn put_keypair(&self, key_public: Vec<u8>, key_private: Vec<u8>) -> Result<()> {
     pub fn put_keypair(&self, key_public: Vec<u8>, key_private: Vec<u8>) -> Result<()> {
         let conn = Connection::open(&self.path)?;
         let conn = Connection::open(&self.path)?;
         println!("{}", self.password);
         println!("{}", self.password);
-        conn.execute("PRAGMA key=(?1)", params![self.password])?;
+        conn.pragma_update(None, "key", &self.password)?;
         conn.execute(
         conn.execute(
             "INSERT INTO keys(key_public, key_private) VALUES (?1, ?2)",
             "INSERT INTO keys(key_public, key_private) VALUES (?1, ?2)",
             params![key_public, key_private],
             params![key_public, key_private],
@@ -151,7 +151,7 @@ impl WalletDB {
     pub fn put_cashier_pub(&self, key_public: Vec<u8>) -> Result<()> {
     pub fn put_cashier_pub(&self, key_public: Vec<u8>) -> Result<()> {
         debug!(target: "save_cash_key", "Save cashier keys...");
         debug!(target: "save_cash_key", "Save cashier keys...");
         let conn = Connection::open(&self.path)?;
         let conn = Connection::open(&self.path)?;
-        conn.execute("PRAGMA key=(?1)", params![self.password])?;
+        conn.pragma_update(None, "key", &self.password)?;
         conn.execute(
         conn.execute(
             "INSERT INTO cashier(key_public) VALUES (?1)",
             "INSERT INTO cashier(key_public) VALUES (?1)",
             params![key_public],
             params![key_public],
@@ -162,7 +162,7 @@ impl WalletDB {
     pub fn get_public(&self) -> Result<Vec<u8>> {
     pub fn get_public(&self) -> Result<Vec<u8>> {
         debug!(target: "get", "Returning keys...");
         debug!(target: "get", "Returning keys...");
         let conn = Connection::open(&self.path)?;
         let conn = Connection::open(&self.path)?;
-        conn.execute("PRAGMA key=(?1)", params![self.password])?;
+        conn.pragma_update(None, "key", &self.password)?;
         let mut stmt = conn.prepare("SELECT key_public FROM keys")?;
         let mut stmt = conn.prepare("SELECT key_public FROM keys")?;
         let key_iter = stmt.query_map::<u8, _, _>([], |row| row.get(0))?;
         let key_iter = stmt.query_map::<u8, _, _>([], |row| row.get(0))?;
         let mut pub_keys = Vec::new();
         let mut pub_keys = Vec::new();
@@ -175,7 +175,7 @@ impl WalletDB {
     pub fn get_cashier_public(&self) -> Result<Vec<u8>> {
     pub fn get_cashier_public(&self) -> Result<Vec<u8>> {
         debug!(target: "get_cashier_public", "Returning keys...");
         debug!(target: "get_cashier_public", "Returning keys...");
         let conn = Connection::open(&self.path)?;
         let conn = Connection::open(&self.path)?;
-        conn.execute("PRAGMA key=(?1)", params![self.password])?;
+        conn.pragma_update(None, "key", &self.password)?;
         let mut stmt = conn.prepare("SELECT key_public FROM cashier")?;
         let mut stmt = conn.prepare("SELECT key_public FROM cashier")?;
         let key_iter = stmt.query_map::<u8, _, _>([], |row| row.get(0))?;
         let key_iter = stmt.query_map::<u8, _, _>([], |row| row.get(0))?;
         let mut pub_keys = Vec::new();
         let mut pub_keys = Vec::new();
@@ -188,7 +188,7 @@ impl WalletDB {
     pub fn get_private(&self) -> Result<Vec<u8>> {
     pub fn get_private(&self) -> Result<Vec<u8>> {
         debug!(target: "get", "Returning keys...");
         debug!(target: "get", "Returning keys...");
         let conn = Connection::open(&self.path)?;
         let conn = Connection::open(&self.path)?;
-        conn.execute("PRAGMA key=(?1)", params![self.password])?;
+        conn.pragma_update(None, "key", &self.password)?;
         let mut stmt = conn.prepare("SELECT key_private FROM keys")?;
         let mut stmt = conn.prepare("SELECT key_private FROM keys")?;
         let key_iter = stmt.query_map::<u8, _, _>([], |row| row.get(0))?;
         let key_iter = stmt.query_map::<u8, _, _>([], |row| row.get(0))?;
         let mut keys = Vec::new();
         let mut keys = Vec::new();
@@ -200,7 +200,7 @@ impl WalletDB {
 
 
     pub fn test_wallet(&self) -> Result<()> {
     pub fn test_wallet(&self) -> Result<()> {
         let conn = Connection::open(&self.path)?;
         let conn = Connection::open(&self.path)?;
-        conn.execute("PRAGMA key=(?1)", params![self.password])?;
+        conn.pragma_update(None, "key", &self.password)?;
         let mut stmt = conn.prepare("SELECT * FROM keys")?;
         let mut stmt = conn.prepare("SELECT * FROM keys")?;
         let _rows = stmt.query([])?;
         let _rows = stmt.query([])?;
         Ok(())
         Ok(())
@@ -228,95 +228,95 @@ mod tests {
         let contents = include_str!("../../res/schema.sql");
         let contents = include_str!("../../res/schema.sql");
         let conn = Connection::open(&path)?;
         let conn = Connection::open(&path)?;
         debug!(target: "walletdb", "OPENED CONNECTION AT PATH {:?}", path);
         debug!(target: "walletdb", "OPENED CONNECTION AT PATH {:?}", path);
-        conn.execute("PRAGMA key=(?1)", params![password])?;
+        conn.pragma_update(None, "key", &password)?;
         conn.execute_batch(&contents)?;
         conn.execute_batch(&contents)?;
         Ok(())
         Ok(())
     }
     }
 
 
-    //#[test]
-    //pub fn test_keypair() -> Result<()> {
-    //    let path = join_config_path(&PathBuf::from("wallet.db"))?;
-    //    let conn = Connection::open(path)?;
-    //    let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
-    //    let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
-    //    let key_public = serial::serialize(&public);
-    //    let key_private = serial::serialize(&secret);
-    //    let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?;
-    //    let _rows = stmt.query([])?;
-    //    conn.execute(
-    //        "INSERT INTO keys(key_public, key_private) VALUES (?1, ?2)",
-    //        params![key_public, key_private],
-    //    )?;
-    //    Ok(())
-    //}
+    #[test]
+    pub fn test_keypair() -> Result<()> {
+        let path = join_config_path(&PathBuf::from("wallet.db"))?;
+        let conn = Connection::open(path)?;
+        let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
+        let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
+        let key_public = serial::serialize(&public);
+        let key_private = serial::serialize(&secret);
+        let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?;
+        let _rows = stmt.query([])?;
+        conn.execute(
+            "INSERT INTO keys(key_public, key_private) VALUES (?1, ?2)",
+            params![key_public, key_private],
+        )?;
+        Ok(())
+    }
 
 
-    //#[test]
-    //pub fn test_get_id() -> Result<()> {
-    //    let path = join_config_path(&PathBuf::from("wallet.db"))?;
-    //    let conn = Connection::open(path)?;
-    //    let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
-    //    let key_private = serial::serialize(&secret);
-    //    let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
-    //    let key_public = serial::serialize(&public);
-    //    let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?;
-    //    let _rows = stmt.query([])?;
-    //    conn.execute(
-    //        "INSERT INTO keys(key_public, key_private) VALUES (?1, ?2)",
-    //        params![key_public, key_private],
-    //    )?;
-    //    let mut get_id =
-    //        conn.prepare("SELECT key_id FROM keys WHERE key_private = :key_private")?;
-    //    let rows =
-    //        get_id.query_map::<u8, _, _>(&[(":key_private", &key_private)], |row| row.get(0))?;
-    //    let mut key_id = Vec::new();
-    //    for id in rows {
-    //        key_id.push(id?)
-    //    }
-    //    println!("FOUND ID: {:?}", key_id.pop().unwrap());
-    //    Ok(())
-    //}
+    #[test]
+    pub fn test_get_id() -> Result<()> {
+        let path = join_config_path(&PathBuf::from("wallet.db"))?;
+        let conn = Connection::open(path)?;
+        let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
+        let key_private = serial::serialize(&secret);
+        let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
+        let key_public = serial::serialize(&public);
+        let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?;
+        let _rows = stmt.query([])?;
+        conn.execute(
+            "INSERT INTO keys(key_public, key_private) VALUES (?1, ?2)",
+            params![key_public, key_private],
+        )?;
+        let mut get_id =
+            conn.prepare("SELECT key_id FROM keys WHERE key_private = :key_private")?;
+        let rows =
+            get_id.query_map::<u8, _, _>(&[(":key_private", &key_private)], |row| row.get(0))?;
+        let mut key_id = Vec::new();
+        for id in rows {
+            key_id.push(id?)
+        }
+        println!("FOUND ID: {:?}", key_id.pop().unwrap());
+        Ok(())
+    }
 
 
-    //#[test]
-    //pub fn test_own_coins() -> Result<()> {
-    //    let key_private = Vec::new();
-    //    let coin = Vec::new();
-    //    let serial = Vec::new();
-    //    let coin_blind = Vec::new();
-    //    let valcom_blind = Vec::new();
-    //    let value = Vec::new();
-    //    let asset_id = Vec::new();
-    //    let witness = Vec::new();
-    //    let path = join_config_path(&PathBuf::from("wallet.db"))?;
-    //    let conn = Connection::open(path)?;
-    //    let contents = include_str!("../../res/schema.sql");
-    //    match conn.execute_batch(&contents) {
-    //        Ok(v) => println!("Database initalized successfully {:?}", v),
-    //        Err(err) => println!("Error: {}", err),
-    //    };
-    //    //let mut unlock = conn.prepare("PRAGMA key = 'testkey'")?;
-    //    //let _rows = unlock.query([])?;
-    //    let mut get_id =
-    //        conn.prepare("SELECT key_id FROM keys WHERE key_private = :key_private")?;
-    //    let rows =
-    //        get_id.query_map::<u8, _, _>(&[(":key_private", &key_private)], |row| row.get(0))?;
-    //    let mut key_id = Vec::new();
-    //    for id in rows {
-    //        key_id.push(id?)
-    //    }
-    //    conn.execute(
-    //        "INSERT INTO coins(coin, serial, value, asset_id, coin_blind, valcom_blind, witness, key_id)
-    //        VALUES (:coin, :serial, :value, :asset_id, :coin_blind, :valcom_blind, :witness, :key_id)",
-    //        named_params! {
-    //        ":coin": coin,
-    //        ":serial": serial,
-    //        ":value": value,
-    //        ":asset_id": asset_id,
-    //        ":coin_blind": coin_blind,
-    //        ":valcom_blind": valcom_blind,
-    //        ":witness": witness,
-    //        ":key_id": key_id.pop().expect("key_id not found!"),
-    //        },
-    //    )?;
-    //    Ok(())
-    //}
+    #[test]
+    pub fn test_own_coins() -> Result<()> {
+        let key_private = Vec::new();
+        let coin = Vec::new();
+        let serial = Vec::new();
+        let coin_blind = Vec::new();
+        let valcom_blind = Vec::new();
+        let value = Vec::new();
+        let asset_id = Vec::new();
+        let witness = Vec::new();
+        let path = join_config_path(&PathBuf::from("wallet.db"))?;
+        let conn = Connection::open(path)?;
+        let contents = include_str!("../../res/schema.sql");
+        match conn.execute_batch(&contents) {
+            Ok(v) => println!("Database initalized successfully {:?}", v),
+            Err(err) => println!("Error: {}", err),
+        };
+        //let mut unlock = conn.prepare("PRAGMA key = 'testkey'")?;
+        //let _rows = unlock.query([])?;
+        let mut get_id =
+            conn.prepare("SELECT key_id FROM keys WHERE key_private = :key_private")?;
+        let rows =
+            get_id.query_map::<u8, _, _>(&[(":key_private", &key_private)], |row| row.get(0))?;
+        let mut key_id = Vec::new();
+        for id in rows {
+            key_id.push(id?)
+        }
+        conn.execute(
+            "INSERT INTO coins(coin, serial, value, asset_id, coin_blind, valcom_blind, witness, key_id)
+            VALUES (:coin, :serial, :value, :asset_id, :coin_blind, :valcom_blind, :witness, :key_id)",
+            named_params! {
+            ":coin": coin,
+            ":serial": serial,
+            ":value": value,
+            ":asset_id": asset_id,
+            ":coin_blind": coin_blind,
+            ":valcom_blind": valcom_blind,
+            ":witness": witness,
+            ":key_id": key_id.pop().expect("key_id not found!"),
+            },
+        )?;
+        Ok(())
+    }
 }
 }