Răsfoiți Sursa

Created 'initalized' boolean to check whether wallets are initialized.

lunar-mining 4 ani în urmă
părinte
comite
02487b8db2
5 a modificat fișierele cu 41 adăugiri și 23 ștergeri
  1. 1 1
      src/bin/cashierd.rs
  2. 1 1
      src/bin/darkfid.rs
  3. 2 0
      src/client/mod.rs
  4. 18 10
      src/wallet/cashierdb.rs
  5. 19 11
      src/wallet/walletdb.rs

+ 1 - 1
src/bin/cashierd.rs

@@ -112,7 +112,7 @@ impl Cashierd {
     }
 
     async fn start(&self, executor: Arc<Executor<'static>>) -> Result<()> {
-        self.cashier_wallet.init_db()?;
+        self.cashier_wallet.init_db().await?;
 
         let bridge = Bridge::new();
 

+ 1 - 1
src/bin/darkfid.rs

@@ -77,7 +77,7 @@ impl Darkfid {
     // --> {"method": "create_wallet", "params": []}
     // <-- {"result": true}
     async fn create_wallet(&self, id: Value, _params: Value) -> JsonResult {
-        match self.wallet.init_db() {
+        match self.wallet.init_db().await {
             Ok(()) => return JsonResult::Resp(jsonresp(json!(true), id)),
             Err(e) => {
                 return JsonResult::Err(jsonerr(ServerError(-32001), Some(e.to_string()), id))

+ 2 - 0
src/client/mod.rs

@@ -14,6 +14,7 @@ pub enum ClientFailed {
     DoNotHaveCashierPublicKey,
     DoNotHaveKeypair,
     EmptyPassword,
+    WalletInitialized,
     ClientError(String),
 }
 
@@ -38,6 +39,7 @@ impl fmt::Display for ClientFailed {
             ClientFailed::DoNotHaveCashierPublicKey => f.write_str("Don't have cashier public key"),
             ClientFailed::DoNotHaveKeypair => f.write_str("Don't have keypair"),
             ClientFailed::EmptyPassword => f.write_str("Password is empty. Cannot create database"),
+            ClientFailed::WalletInitialized => f.write_str("Wallet already initalized"),
             ClientFailed::ClientError(i) => {
                 write!(f, "ClientError: {}", i)
             }

+ 18 - 10
src/wallet/cashierdb.rs

@@ -2,7 +2,7 @@ use super::{Keypair, WalletApi};
 use crate::client::ClientFailed;
 use crate::{Error, Result};
 
-use async_std::sync::Arc;
+use async_std::sync::{Arc, Mutex};
 use log::*;
 use rusqlite::{named_params, params, Connection};
 
@@ -13,6 +13,7 @@ pub type CashierDbPtr = Arc<CashierDb>;
 pub struct CashierDb {
     pub path: PathBuf,
     pub password: String,
+    pub initialized: Mutex<bool>,
 }
 
 impl WalletApi for CashierDb {
@@ -30,19 +31,26 @@ impl CashierDb {
         Ok(Arc::new(Self {
             path: path.to_owned(),
             password,
+            initialized: Mutex::new(false),
         }))
     }
 
-    pub fn init_db(&self) -> Result<()> {
-        if !self.password.trim().is_empty() {
-            let contents = include_str!("../../sql/cashier.sql");
-            let conn = Connection::open(&self.path)?;
-            debug!(target: "CASHIERDB", "Opened connection at path {:?}", self.path);
-            conn.pragma_update(None, "key", &self.password)?;
-            conn.execute_batch(&contents)?;
+    pub async fn init_db(&self) -> Result<()> {
+        if *self.initialized.lock().await == false {
+            if !self.password.trim().is_empty() {
+                let contents = include_str!("../../sql/cashier.sql");
+                let conn = Connection::open(&self.path)?;
+                debug!(target: "CASHIERDB", "Opened connection at path {:?}", self.path);
+                conn.pragma_update(None, "key", &self.password)?;
+                conn.execute_batch(&contents)?;
+                *self.initialized.lock().await = true;
+            } else {
+                debug!(target: "CASHIERDB", "Password is empty. You must set a password to use the wallet.");
+                return Err(Error::from(ClientFailed::EmptyPassword));
+            }
         } else {
-            debug!(target: "CASHIERDB", "Password is empty. You must set a password to use the wallet.");
-            return Err(Error::from(ClientFailed::EmptyPassword));
+            debug!(target: "WALLETDB", "Wallet already initialized.");
+            return Err(Error::from(ClientFailed::WalletInitialized));
         }
         Ok(())
     }

+ 19 - 11
src/wallet/walletdb.rs

@@ -6,7 +6,7 @@ use crate::crypto::{
 use crate::serial;
 use crate::{Error, Result};
 
-use async_std::sync::Arc;
+use async_std::sync::{Arc, Mutex};
 use ff::Field;
 use log::*;
 use rand::rngs::OsRng;
@@ -22,10 +22,11 @@ pub struct Keypair {
     pub private: jubjub::Fr,
 }
 
-#[derive(Clone)]
+//#[derive(Clone)]
 pub struct WalletDb {
     pub path: PathBuf,
     pub password: String,
+    pub initialized: Mutex<bool>,
 }
 
 impl WalletApi for WalletDb {
@@ -43,19 +44,26 @@ impl WalletDb {
         Ok(Arc::new(Self {
             path: path.to_owned(),
             password,
+            initialized: Mutex::new(false),
         }))
     }
 
-    pub fn init_db(&self) -> Result<()> {
-        if !self.password.trim().is_empty() {
-            let contents = include_str!("../../sql/schema.sql");
-            let conn = Connection::open(&self.path)?;
-            debug!(target: "WALLETDB", "OPENED CONNECTION AT PATH {:?}", self.path);
-            conn.pragma_update(None, "key", &self.password)?;
-            conn.execute_batch(&contents)?;
+    pub async fn init_db(&self) -> Result<()> {
+        if *self.initialized.lock().await == false {
+            if !self.password.trim().is_empty() {
+                let contents = include_str!("../../sql/schema.sql");
+                let conn = Connection::open(&self.path)?;
+                debug!(target: "WALLETDB", "OPENED CONNECTION AT PATH {:?}", self.path);
+                conn.pragma_update(None, "key", &self.password)?;
+                conn.execute_batch(&contents)?;
+                *self.initialized.lock().await = true;
+            } else {
+                debug!(target: "WALLETDB", "Password is empty. You must set a password to use the wallet.");
+                return Err(Error::from(ClientFailed::EmptyPassword));
+            }
         } else {
-            debug!(target: "WALLETDB", "Password is empty. You must set a password to use the wallet.");
-            return Err(Error::from(ClientFailed::EmptyPassword));
+            debug!(target: "WALLETDB", "Wallet already initialized.");
+            return Err(Error::from(ClientFailed::WalletInitialized));
         }
         Ok(())
     }