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

added password param to darkfid and wallet unlock support to sqlite

lunar-mining 5 лет назад
Родитель
Сommit
a566e4c1ae
4 измененных файлов с 57 добавлено и 22 удалено
  1. 0 1
      res/schema.sql
  2. 1 1
      src/bin/darkfid.rs
  3. 11 0
      src/cli/client_cli/darkfid_cli.rs
  4. 45 20
      src/wallet/walletdb.rs

+ 0 - 1
res/schema.sql

@@ -1,4 +1,3 @@
-PRAGMA key = 'testkey';
 CREATE TABLE IF NOT EXISTS keys(
     key_id INTEGER PRIMARY KEY NOT NULL,
     key_public BLOB NOT NULL,

+ 1 - 1
src/bin/darkfid.rs

@@ -189,7 +189,7 @@ async fn start(
     let merkle_roots = RocksColumn::<columns::MerkleRoots>::new(rocks.clone());
     let nullifiers = RocksColumn::<columns::Nullifiers>::new(rocks);
 
-    let wallet = Arc::new(WalletDB::new("wallet.db")?);
+    let wallet = Arc::new(WalletDB::new("wallet.db", config.password.clone())?);
 
     let ex = executor.clone();
 

+ 11 - 0
src/cli/client_cli/darkfid_cli.rs

@@ -51,6 +51,10 @@ impl DarkfidCli {
                                     .about("Set Log Path")
                                     .long("log-path")
                                     .takes_value(true),
+                                Arg::new("password")
+                                    .about("Set password")
+                                    .long("password")
+                                    .takes_value(true),
                             ])
                             .setting(AppSettings::ArgRequiredElseHelp),
                     ),
@@ -71,6 +75,9 @@ impl DarkfidCli {
                         println!("RPC Url: {}", config.rpc_url);
                         println!("Database path: {}", config.database_path);
                         println!("Log Path: {}", config.log_path);
+                        if config.password.trim().is_empty() {
+                            println!("Password is empty. Please set a password.")
+                        };
                     }
                     ("set", c) => {
                         change_config = true;
@@ -94,6 +101,10 @@ impl DarkfidCli {
                             config.log_path = v.to_string();
                             println!("Change Log Path To {}", config.log_path);
                         }
+                        if let Some(v) = c.value_of("passport") {
+                            config.log_path = v.to_string();
+                            println!("Password updated successfully.");
+                        }
                     }
                     _ => {}
                 },

+ 45 - 20
src/wallet/walletdb.rs

@@ -23,10 +23,11 @@ pub struct WalletDB {
     pub witnesses: Mutex<Vec<IncrementalWitness<MerkleNode>>>,
     pub cashier_public: jubjub::SubgroupPoint,
     pub public: jubjub::SubgroupPoint,
+    pub password: String,
 }
 
 impl WalletDB {
-    pub fn new(wallet: &str) -> Result<Self> {
+    pub fn new(wallet: &str, password: String) -> Result<Self> {
         debug!(target: "walletdb", "new() Constructor called");
         let path = join_config_path(&PathBuf::from(wallet))?;
         let cashier_secret = jubjub::Fr::random(&mut OsRng);
@@ -45,6 +46,7 @@ impl WalletDB {
             coins,
             notes,
             witnesses,
+            password,
             //conn,
         })
     }
@@ -53,9 +55,18 @@ impl WalletDB {
         let conn = Connection::open(&self.path)?;
         debug!(target: "walletdb", "OPENED CONNECTION AT PATH {:?}", self.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),
+        if !self.password.trim().is_empty() {
+            conn.execute(
+                "PRAGMA key=(?1)",
+                params![self.password],
+            )?;
+            match conn.execute_batch(&contents) {
+                Ok(v) => println!("Database initalized successfully {:?}", v),
+                Err(err) => println!("Error: {}", err),
+            };
+        }
+        else {
+            println!("Password is empty. You must set a password to use the wallet.")
         };
         Ok(())
     }
@@ -90,8 +101,10 @@ impl WalletDB {
         // open connection
         let conn = Connection::open(&self.path)?;
         // unlock database
-        let mut unlock = conn.prepare("PRAGMA key = 'testkey'")?;
-        let _rows = unlock.query([])?;
+        conn.execute(
+            "PRAGMA key=(?1)",
+            params![self.password],
+        )?;
         // return key_id from key_private
         let mut get_id =
             conn.prepare("SELECT key_id FROM keys WHERE key_private = :key_private")?;
@@ -137,8 +150,10 @@ impl WalletDB {
 
     pub fn put_keypair(&self, key_public: Vec<u8>, key_private: Vec<u8>) -> Result<()> {
         let conn = Connection::open(&self.path)?;
-        let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?;
-        let _rows = stmt.query([])?;
+        conn.execute(
+            "PRAGMA key=(?1)",
+            params![self.password],
+        )?;
         conn.execute(
             "INSERT INTO keys(key_public, key_private) VALUES (?1, ?2)",
             params![key_public, key_private],
@@ -149,8 +164,10 @@ impl WalletDB {
     pub fn put_cashier_pub(&self, key_public: Vec<u8>) -> Result<()> {
         debug!(target: "save_cash_key", "Save cashier keys...");
         let conn = Connection::open(&self.path)?;
-        let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?;
-        let _rows = stmt.query([])?;
+        conn.execute(
+            "PRAGMA key=(?1)",
+            params![self.password],
+        )?;
         conn.execute(
             "INSERT INTO cashier(key_public) VALUES (?1)",
             params![key_public],
@@ -161,8 +178,10 @@ impl WalletDB {
     pub fn get_public(&self) -> Result<Vec<u8>> {
         debug!(target: "get", "Returning keys...");
         let conn = Connection::open(&self.path)?;
-        let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?;
-        let _rows = stmt.query([])?;
+        conn.execute(
+            "PRAGMA key=(?1)",
+            params![self.password],
+        )?;
         let mut stmt = conn.prepare("SELECT key_public FROM keys")?;
         let key_iter = stmt.query_map::<u8, _, _>([], |row| row.get(0))?;
         let mut pub_keys = Vec::new();
@@ -175,8 +194,10 @@ impl WalletDB {
     pub fn get_cashier_public(&self) -> Result<Vec<u8>> {
         debug!(target: "get_cashier_public", "Returning keys...");
         let conn = Connection::open(&self.path)?;
-        let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?;
-        let _rows = stmt.query([])?;
+        conn.execute(
+            "PRAGMA key=(?1)",
+            params![self.password],
+        )?;
         let mut stmt = conn.prepare("SELECT key_public FROM cashier")?;
         let key_iter = stmt.query_map::<u8, _, _>([], |row| row.get(0))?;
         let mut pub_keys = Vec::new();
@@ -189,8 +210,10 @@ impl WalletDB {
     pub fn get_private(&self) -> Result<Vec<u8>> {
         debug!(target: "get", "Returning keys...");
         let conn = Connection::open(&self.path)?;
-        let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?;
-        let _rows = stmt.query([])?;
+        conn.execute(
+            "PRAGMA key=(?1)",
+            params![self.password],
+        )?;
         let mut stmt = conn.prepare("SELECT key_private FROM keys")?;
         let key_iter = stmt.query_map::<u8, _, _>([], |row| row.get(0))?;
         let mut keys = Vec::new();
@@ -202,8 +225,10 @@ impl WalletDB {
 
     pub fn test_wallet(&self) -> Result<()> {
         let conn = Connection::open(&self.path)?;
-        let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?;
-        let _rows = stmt.query([])?;
+        conn.execute(
+            "PRAGMA key=(?1)",
+            params![self.password],
+        )?;
         let mut stmt = conn.prepare("SELECT * FROM keys")?;
         let _rows = stmt.query([])?;
         Ok(())
@@ -284,8 +309,8 @@ mod tests {
             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 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 =