From 02487b8db2f4e971d8eec1cd0c8ed608c0128310 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Fri, 24 Sep 2021 13:03:34 +0200 Subject: [PATCH] Created 'initalized' boolean to check whether wallets are initialized. --- src/bin/cashierd.rs | 2 +- src/bin/darkfid.rs | 2 +- src/client/mod.rs | 2 ++ src/wallet/cashierdb.rs | 28 ++++++++++++++++++---------- src/wallet/walletdb.rs | 30 +++++++++++++++++++----------- 5 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/bin/cashierd.rs b/src/bin/cashierd.rs index 16932b67b..ade7b784e 100644 --- a/src/bin/cashierd.rs +++ b/src/bin/cashierd.rs @@ -112,7 +112,7 @@ impl Cashierd { } async fn start(&self, executor: Arc>) -> Result<()> { - self.cashier_wallet.init_db()?; + self.cashier_wallet.init_db().await?; let bridge = Bridge::new(); diff --git a/src/bin/darkfid.rs b/src/bin/darkfid.rs index 2bfa3996c..84e0711f4 100644 --- a/src/bin/darkfid.rs +++ b/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)) diff --git a/src/client/mod.rs b/src/client/mod.rs index fbd8aa1cd..801b36060 100644 --- a/src/client/mod.rs +++ b/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) } diff --git a/src/wallet/cashierdb.rs b/src/wallet/cashierdb.rs index 9c64cce80..fc389b235 100644 --- a/src/wallet/cashierdb.rs +++ b/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; pub struct CashierDb { pub path: PathBuf, pub password: String, + pub initialized: Mutex, } 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(()) } diff --git a/src/wallet/walletdb.rs b/src/wallet/walletdb.rs index b937eec1f..cb2ccf57c 100644 --- a/src/wallet/walletdb.rs +++ b/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, } 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(()) }