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

This commit is contained in:
lunar-mining
2021-09-24 13:03:34 +02:00
parent da1fa2799d
commit 02487b8db2
5 changed files with 41 additions and 23 deletions

View File

@@ -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();

View File

@@ -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))

View File

@@ -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)
}

View File

@@ -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(())
}

View File

@@ -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(())
}