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

wallet: Support arbitrary strings for database path.

This enforces handling of the path on the client side, but also
allows us easier handling of in-memory databases.
parazyd 4 лет назад
Родитель
Сommit
e155a8583e
4 измененных файлов с 27 добавлено и 40 удалено
  1. 15 20
      src/bin/cashierd.rs
  2. 5 9
      src/bin/darkfid.rs
  3. 3 5
      src/wallet/cashierdb.rs
  4. 4 6
      src/wallet/walletdb.rs

+ 15 - 20
src/bin/cashierd.rs

@@ -85,11 +85,10 @@ impl Cashierd {
     async fn new(config: CashierdConfig) -> Result<Self> {
         debug!(target: "CASHIER DAEMON", "Initialize");
 
-        let cashier_wallet = CashierDb::new(
-            expand_path(&config.cashier_wallet_path)?.as_path(),
-            config.cashier_wallet_password.clone(),
-        )
-        .await?;
+        let wallet_path =
+            format!("sqlite://{}", expand_path(&config.cashier_wallet_path)?.to_str().unwrap());
+        let cashier_wallet =
+            CashierDb::new(&wallet_path, config.cashier_wallet_password.clone()).await?;
 
         let mut networks = Vec::new();
 
@@ -673,11 +672,10 @@ async fn start(
 ) -> Result<()> {
     let mut cashierd = Cashierd::new(config.clone()).await?;
 
-    let client_wallet = WalletDb::new(
-        expand_path(&config.client_wallet_path.clone())?.as_path(),
-        config.client_wallet_password.clone(),
-    )
-    .await?;
+    let client_wallet_path =
+        format!("sqlite://{}", expand_path(&config.client_wallet_path)?.to_str().unwrap());
+    let client_wallet =
+        WalletDb::new(&client_wallet_path, config.client_wallet_password.clone()).await?;
 
     let rocks = Rocks::new(expand_path(&config.database_path.clone())?.as_path())?;
 
@@ -757,19 +755,16 @@ async fn main() -> Result<()> {
     if args.is_present("refresh") {
         debug!(target: "CASHIER DAEMON", "Refresh the wallet and the database");
 
-        let client_wallet = WalletDb::new(
-            expand_path(&config.client_wallet_path)?.as_path(),
-            config.client_wallet_password.clone(),
-        )
-        .await?;
+        let client_wallet_path =
+            format!("sqlite://{}", expand_path(&config.client_wallet_path)?.to_str().unwrap());
+        let client_wallet =
+            WalletDb::new(&client_wallet_path, config.client_wallet_password.clone()).await?;
 
         client_wallet.remove_own_coins().await?;
 
-        let wallet = CashierDb::new(
-            expand_path(&config.cashier_wallet_path)?.as_path(),
-            config.cashier_wallet_password.clone(),
-        )
-        .await?;
+        let wallet_path =
+            format!("sqlite://{}", expand_path(&config.cashier_wallet_path)?.to_str().unwrap());
+        let wallet = CashierDb::new(&wallet_path, config.cashier_wallet_password.clone()).await?;
 
         wallet.remove_withdraw_and_deposit_keys().await?;
 

+ 5 - 9
src/bin/darkfid.rs

@@ -536,9 +536,8 @@ async fn start(
     local_cashier: Option<&str>,
     config: &DarkfidConfig,
 ) -> Result<()> {
-    let wallet =
-        WalletDb::new(expand_path(&config.wallet_path)?.as_path(), config.wallet_password.clone())
-            .await?;
+    let wallet_path = format!("sqlite://{}", expand_path(&config.wallet_path)?.to_str().unwrap());
+    let wallet = WalletDb::new(&wallet_path, config.wallet_password.clone()).await?;
 
     let rocks = Rocks::new(expand_path(&config.database_path.clone())?.as_path())?;
 
@@ -637,12 +636,9 @@ async fn main() -> Result<()> {
 
     if args.is_present("refresh") {
         debug!(target: "DARKFI DAEMON", "Refresh the wallet and the database");
-
-        let wallet = WalletDb::new(
-            expand_path(&config.wallet_path)?.as_path(),
-            config.wallet_password.clone(),
-        )
-        .await?;
+        let wallet_path =
+            format!("sqlite://{}", expand_path(&config.wallet_path)?.to_str().unwrap());
+        let wallet = WalletDb::new(&wallet_path, config.wallet_password.clone()).await?;
 
         wallet.remove_own_coins().await?;
 

+ 3 - 5
src/wallet/cashierdb.rs

@@ -1,4 +1,4 @@
-use std::{path::Path, str::FromStr};
+use std::str::FromStr;
 
 use async_std::sync::Arc;
 use log::{debug, error, info};
@@ -45,16 +45,14 @@ pub struct CashierDb {
 impl WalletApi for CashierDb {}
 
 impl CashierDb {
-    pub async fn new(path: &Path, password: String) -> Result<CashierDbPtr> {
+    pub async fn new(path: &str, password: String) -> Result<CashierDbPtr> {
         debug!("new() Constructor called");
         if password.trim().is_empty() {
             error!("Password is empty. You must set a password to use the wallet.");
             return Err(Error::from(ClientFailed::EmptyPassword))
         }
 
-        let p = format!("sqlite://{}", path.to_str().unwrap());
-
-        let connect_opts = SqliteConnectOptions::from_str(&p)?
+        let connect_opts = SqliteConnectOptions::from_str(path)?
             .pragma("key", password)
             .create_if_missing(true)
             .journal_mode(SqliteJournalMode::Off);

+ 4 - 6
src/wallet/walletdb.rs

@@ -1,4 +1,4 @@
-use std::{path::Path, str::FromStr};
+use std::str::FromStr;
 
 use async_std::sync::Arc;
 use log::{debug, error, info};
@@ -44,23 +44,21 @@ pub struct WalletDb {
 impl WalletApi for WalletDb {}
 
 impl WalletDb {
-    pub async fn new(path: &Path, password: String) -> Result<WalletPtr> {
+    pub async fn new(path: &str, password: String) -> Result<WalletPtr> {
         debug!("new() Constructor called");
         if password.trim().is_empty() {
             error!("Password is empty. You must set a password to use the wallet.");
             return Err(Error::from(ClientFailed::EmptyPassword))
         }
 
-        let p = format!("sqlite://{}", path.to_str().unwrap());
-
-        let connect_opts = SqliteConnectOptions::from_str(&p)?
+        let connect_opts = SqliteConnectOptions::from_str(path)?
             .pragma("key", password)
             .create_if_missing(true)
             .journal_mode(SqliteJournalMode::Off);
 
         let conn = SqlitePool::connect_with(connect_opts).await?;
 
-        info!("Opened connection at path sqlite://{:?}", path);
+        info!("Opened connection at path {}", path);
         Ok(Arc::new(WalletDb { conn }))
     }