From 3f7031eca2421ea0ba8e8ef03e2b6ec02c71c122 Mon Sep 17 00:00:00 2001 From: parazyd Date: Tue, 11 Jul 2023 13:24:15 +0200 Subject: [PATCH] wallet: Reintroduce sqlcipher. --- Cargo.toml | 3 --- bin/darkfid/src/main.rs | 2 +- bin/faucetd/src/main.rs | 2 +- src/consensus/state.rs | 2 +- src/contract/test-harness/src/lib.rs | 2 +- src/wallet/walletdb.rs | 28 ++++++++++++++++++++++++---- 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c812eff97..210e265c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -128,9 +128,6 @@ libsqlite3-sys = {version = "0.26.0", features = ["bundled-sqlcipher-vendored-op sled = {version = "0.34.7", optional = true} sled-overlay = {version = "0.0.7", optional = true} -# Temporary version lock -#curve25519-dalek = {version = "=4.0.0-rc.3", default-features = false, optional = true} - [dev-dependencies] clap = {version = "4.3.11", features = ["derive"]} halo2_proofs = {version = "0.3.0", features = ["dev-graph", "gadget-traces", "sanity-checks"]} diff --git a/bin/darkfid/src/main.rs b/bin/darkfid/src/main.rs index 292701b68..a6535b37d 100644 --- a/bin/darkfid/src/main.rs +++ b/bin/darkfid/src/main.rs @@ -301,7 +301,7 @@ async fn realmain(args: Args, ex: Arc>) -> Result<()> { } // Initialize or load wallet - let wallet = WalletDb::new(Some(expand_path(&args.wallet_path)?), &args.wallet_pass).await?; + let wallet = WalletDb::new(Some(expand_path(&args.wallet_path)?), Some(&args.wallet_pass))?; // Initialize or open sled database let db_path = diff --git a/bin/faucetd/src/main.rs b/bin/faucetd/src/main.rs index ea23eddb9..30b28cdbd 100644 --- a/bin/faucetd/src/main.rs +++ b/bin/faucetd/src/main.rs @@ -622,7 +622,7 @@ async fn prune_airdrop_maps(rate_map: AirdropMap, challenge_map: ChallengeMap, t async_daemonize!(realmain); async fn realmain(args: Args, ex: Arc>) -> Result<()> { // Initialize or load wallet - let wallet = WalletDb::new(Some(expand_path(&args.wallet_path)?), &args.wallet_pass).await?; + let wallet = WalletDb::new(Some(expand_path(&args.wallet_path)?), Some(&args.wallet_pass))?; // Initialize or open sled database let db_path = diff --git a/src/consensus/state.rs b/src/consensus/state.rs index f6169a827..c76f11505 100644 --- a/src/consensus/state.rs +++ b/src/consensus/state.rs @@ -859,7 +859,7 @@ mod tests { #[async_std::test] async fn calc_sigmas_test() -> Result<()> { // Generate dummy state - let wallet = WalletDb::new(None, "foo").await?; + let wallet = WalletDb::new(None, None)?; let sled_db = sled::Config::new().temporary(true).open()?; let blockchain = Blockchain::new(&sled_db)?; let state = ConsensusState::new( diff --git a/src/contract/test-harness/src/lib.rs b/src/contract/test-harness/src/lib.rs index 91f17da70..5ea1cbdc6 100644 --- a/src/contract/test-harness/src/lib.rs +++ b/src/contract/test-harness/src/lib.rs @@ -131,7 +131,7 @@ impl Wallet { genesis_block: &BlockInfo, faucet_pubkeys: &[PublicKey], ) -> Result { - let wallet = WalletDb::new(None, "foo").await?; + let wallet = WalletDb::new(None, None)?; let sled_db = sled::Config::new().temporary(true).open()?; // Use pregenerated vks diff --git a/src/wallet/walletdb.rs b/src/wallet/walletdb.rs index 46aa3cd7d..95073d9d4 100644 --- a/src/wallet/walletdb.rs +++ b/src/wallet/walletdb.rs @@ -27,7 +27,6 @@ use crate::Result; pub type WalletPtr = Arc; /// Types we want to allow to query from the SQL wallet -#[repr(u8)] pub enum QueryType { /// Integer gets decoded into `u64` Integer = 0x00, @@ -64,12 +63,15 @@ pub struct WalletDb { impl WalletDb { /// Create a new wallet. If `path` is `None`, create it in memory. - pub async fn new(path: Option, _password: &str) -> Result { + pub fn new(path: Option, password: Option<&str>) -> Result { let conn = match path.clone() { Some(p) => Connection::open(p)?, None => Connection::open_in_memory()?, }; + if let Some(password) = password { + conn.pragma_update(None, "key", password)?; + } conn.pragma_update(None, "foreign_keys", "ON")?; info!(target: "wallet::walletdb", "[WalletDb] Opened Sqlite connection at \"{:?}\"", path); @@ -80,8 +82,26 @@ impl WalletDb { /// Therefore it's best to use it for initializing a table or similar things. pub async fn exec_sql(&self, query: &str) -> Result<()> { info!(target: "wallet::walletdb", "[WalletDb] Executing SQL query"); - debug!(target: "wallet::walletdb", "\n{}", query); - self.conn.lock().await.execute(query, ())?; + debug!(target: "wallet::walletdb", "[WalletDb] Query:\n{}", query); + let _ = self.conn.lock().await.execute(query, ())?; Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[async_std::test] + async fn test_mem_wallet() { + let wallet = WalletDb::new(None, Some("foobar")).unwrap(); + wallet.exec_sql("CREATE TABLE mista ( numba INTEGER );").await.unwrap(); + wallet.exec_sql("INSERT INTO mista ( numba ) VALUES ( 42 );").await.unwrap(); + + let conn = wallet.conn.lock().await; + let mut stmt = conn.prepare("SELECT numba FROM mista").unwrap(); + let numba: u64 = stmt.query_row((), |row| Ok(row.get("numba").unwrap())).unwrap(); + stmt.finalize().unwrap(); + assert!(numba == 42); + } +}