From 60415b02c62098b9acfb1e21b91c1fbf0604db3d Mon Sep 17 00:00:00 2001 From: rachel-rose Date: Fri, 4 Jun 2021 10:53:06 +0200 Subject: [PATCH] renamed walletdb and passed a param to path --- src/bin/tx-test.rs | 66 +++++++++++++++---------------- src/rpc/adapter.rs | 52 ++++++++++++++++++++---- src/wallet/walletdb.rs | 89 +++++------------------------------------- 3 files changed, 87 insertions(+), 120 deletions(-) diff --git a/src/bin/tx-test.rs b/src/bin/tx-test.rs index 7c6762f39..b99114a74 100644 --- a/src/bin/tx-test.rs +++ b/src/bin/tx-test.rs @@ -21,7 +21,7 @@ use drk::crypto::{ }; use drk::serial::{Decodable, Encodable}; use drk::state::{state_transition, ProgramState, StateUpdate}; -use drk::wallet::walletdb::DBInterface; +use drk::wallet::walletdb::WalletDB; struct MemoryState { // The entire merkle tree state @@ -52,16 +52,16 @@ struct MemoryState { impl ProgramState for MemoryState { // Vec for keys fn is_valid_cashier_public_key(&self, public: &jubjub::SubgroupPoint) -> bool { - let path = DBInterface::wallet_path(); - let connect = Connection::open(&path).expect("Failed to connect to database."); - let mut stmt = connect.prepare("SELECT key_public FROM keys").unwrap(); - let key_iter = stmt - .query_map::, _, _>([], |row| row.get(0)) - .unwrap(); - // does not actually check whether the cashier key is valid - for key in key_iter { - key.unwrap() == self.cashier_public; - } + //let path = WalletDB::wallet_path(); + //let connect = Connection::open(&path).expect("Failed to connect to database."); + //let mut stmt = connect.prepare("SELECT key_public FROM keys").unwrap(); + //let key_iter = stmt + // .query_map::, _, _>([], |row| row.get(0)) + // .unwrap(); + //// does not actually check whether the cashier key is valid + //for key in key_iter { + // key.unwrap() == self.cashier_public; + //} true } // rocksdb @@ -126,29 +126,29 @@ impl MemoryState { // sql fn try_decrypt_note(&self, ciphertext: EncryptedNote) -> Option<(Note, jubjub::Fr)> { - debug!(target: "adapter", "try_decrypt_note() [START]"); - let path = DBInterface::wallet_path(); - debug!(target: "adapter", "try_decrypt_note() [FOUND PATH]"); - println!("Found path: {:?}", &path); - debug!(target: "adapter", "try_decrypt_note() [TRY DB CONNECT]"); - let connect = Connection::open(&path).expect("Failed to connect to database."); - let mut stmt = connect.prepare("SELECT key_private FROM keys").ok()?; - let key_iter = stmt.query_map::([], |row| row.get(0)).ok()?; - for key in key_iter { - println!("Found key {:?}", key.unwrap()); - } - // Loop through all our secret keys... + //debug!(target: "adapter", "try_decrypt_note() [START]"); + //let path = WalletDB::wallet_path(); + //debug!(target: "adapter", "try_decrypt_note() [FOUND PATH]"); + //println!("Found path: {:?}", &path); + //debug!(target: "adapter", "try_decrypt_note() [TRY DB CONNECT]"); + //let connect = Connection::open(&path).expect("Failed to connect to database."); + //let mut stmt = connect.prepare("SELECT key_private FROM keys").ok()?; + //let key_iter = stmt.query_map::([], |row| row.get(0)).ok()?; + //for key in key_iter { + // println!("Found key {:?}", key.unwrap()); + //} + //// Loop through all our secret keys... - for secret in &self.secrets { - // ... attempt to decrypt the note ... - match ciphertext.decrypt(secret) { - Ok(note) => { - // ... and return the decrypted note for this coin. - return Some((note, secret.clone())); - } - Err(_) => {} - } - } + //for secret in &self.secrets { + // // ... attempt to decrypt the note ... + // match ciphertext.decrypt(secret) { + // Ok(note) => { + // // ... and return the decrypted note for this coin. + // return Some((note, secret.clone())); + // } + // Err(_) => {} + // } + //} // We weren't able to decrypt the note with any of our keys. None } diff --git a/src/rpc/adapter.rs b/src/rpc/adapter.rs index 5e12e4b68..1d04311e1 100644 --- a/src/rpc/adapter.rs +++ b/src/rpc/adapter.rs @@ -1,7 +1,8 @@ use crate::Result; +use std::path::PathBuf; use log::*; use std::sync::Arc; -use crate::wallet::walletdb::DBInterface; +use crate::wallet::walletdb::WalletDB; // Dummy adapter for now pub struct RpcAdapter {} @@ -13,29 +14,66 @@ impl RpcAdapter { pub async fn key_gen() -> Result<()> { debug!(target: "adapter", "key_gen() [START]"); - DBInterface::own_key_gen().await?; + let path = dirs::home_dir() + .expect("cannot find home directory.") + .as_path() + .join(".config/darkfi/wallet.db"); + WalletDB::key_gen(path).await?; Ok(()) } + // user input should define wallet path pub async fn new_wallet() -> Result<()> { debug!(target: "adapter", "new_wallet() [START]"); - DBInterface::new_wallet().await?; + let path = dirs::home_dir() + .expect("cannot find home directory.") + .as_path() + .join(".config/darkfi/wallet.db"); + WalletDB::new(path).await?; Ok(()) } pub async fn new_cashier_wallet() -> Result<()> { + let path = dirs::home_dir() + .expect("cannot find home directory.") + .as_path() + .join(".config/darkfi/cashier.db"); debug!(target: "adapter", "new_wallet() [START]"); - DBInterface::new_cashier_wallet().await?; + WalletDB::new(path).await?; Ok(()) } - pub async fn save_key(pubkey: Vec) -> Result<()> { - debug!(target: "adapter", "save_key() [START]"); - DBInterface::save_key(pubkey).await?; + pub async fn save_cash_key(pubkey: Vec) -> Result<()> { + let path = dirs::home_dir() + .expect("cannot find home directory.") + .as_path() + .join(".config/darkfi/cashier.db"); + debug!(target: "adapter", "new_wallet() [START]"); + WalletDB::save(path, pubkey).await?; Ok(()) } + pub async fn save_key(pubkey: Vec) -> Result<()> { + let path = dirs::home_dir() + .expect("cannot find home directory.") + .as_path() + .join(".config/darkfi/wallet.db"); + debug!(target: "adapter", "new_wallet() [START]"); + WalletDB::save(path, pubkey).await?; + Ok(()) + + } + + pub fn wallet_path() -> PathBuf { + debug!(target: "wallet_path", "Finding wallet path..."); + let path = dirs::home_dir() + .expect("cannot find home directory.") + .as_path() + .join(".config/darkfi/wallet.db"); + path + } + pub async fn get_info() {} pub async fn say_hello() {} diff --git a/src/wallet/walletdb.rs b/src/wallet/walletdb.rs index 1cd69a1c6..4716c3e5b 100644 --- a/src/wallet/walletdb.rs +++ b/src/wallet/walletdb.rs @@ -8,52 +8,20 @@ use std::path::PathBuf; // TODO: make this more generic to remove boiler plate. e.g. create_wallet(cashier) instead of // create_cashier_wallet -pub struct DBInterface {} +pub struct WalletDB { +} -impl DBInterface { - pub fn wallet_path() -> PathBuf { - debug!(target: "wallet_path", "Finding wallet path..."); - let path = dirs::home_dir() - .expect("cannot find home directory.") - .as_path() - .join(".config/darkfi/wallet.db"); - path - } - - pub fn cashier_path() -> PathBuf { - debug!(target: "cashier_path", "Finding cashier path..."); - let path = dirs::home_dir() - .expect("Cannot find home directory.") - .as_path() - .join(".config/darkfi/cashier.db"); - path - } - - pub async fn new_wallet() -> Result<()> { - debug!(target: "new_wallet", "Creating new wallet..."); - let path = Self::wallet_path(); - debug!(target: "new_wallet", "Found path {:?}", path); - let connect = Connection::open(&path).expect("Failed to connect to database."); - debug!(target: "new_wallet", "Connection established"); - debug!(target: "new_wallet", "Attempting to load schema..."); - let contents = include_str!("../../res/schema.sql"); - debug!(target: "new_wallet", "Schema loaded"); - debug!(target: "new_wallet", "Executing schema"); - Ok(connect.execute_batch(&contents)?) - } - - pub async fn new_cashier_wallet() -> Result<()> { - debug!(target: "new_cashier_wallet", "Creating new cashier wallet..."); - let path = Self::cashier_path(); +impl WalletDB { + pub async fn new(path: PathBuf) -> Result<()> { let connect = Connection::open(&path).expect("Failed to connect to database."); let contents = include_str!("../../res/schema.sql"); Ok(connect.execute_batch(&contents)?) } - pub async fn own_key_gen() -> Result<()> { + pub async fn key_gen(path: PathBuf) -> Result<()> { debug!(target: "own_key_gen", "Generating keys..."); - let path = Self::wallet_path(); let connect = Connection::open(&path).expect("Failed to connect to database."); + // TODO: ID should not be fixed let id = 0; // Create keys let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng); @@ -73,31 +41,8 @@ impl DBInterface { Ok(()) } - pub async fn cash_key_gen() -> Result<()> { - debug!(target: "own_key_gen", "Generating cashier keys..."); - let path = Self::cashier_path(); - let connect = Connection::open(&path).expect("Failed to connect to database."); - let id = 0; - // Create keys - let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng); - let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret; - let pubkey = serial::serialize(&public); - let privkey = serial::serialize(&secret); - // Write keys to database - connect.execute( - "INSERT INTO keys(key_id, key_private, key_public) - VALUES (:id, :privkey, :pubkey)", - named_params! {":id": id, - ":privkey": privkey, - ":pubkey": pubkey - }, - )?; - Ok(()) - } - - pub async fn get_cash_public() -> Result<()> { + pub async fn get(path: PathBuf) -> Result<()> { debug!(target: "get_cash_public", "Returning cashier keys..."); - let path = Self::cashier_path(); let connect = Connection::open(&path).expect("Failed to connect to database."); let id = 0; let mut stmt = connect.prepare("SELECT key_public FROM keys").unwrap(); @@ -115,9 +60,9 @@ impl DBInterface { Ok(key) } - pub async fn save_cash_key(pubkey: Vec) -> Result<()> { + pub async fn save(path: PathBuf, pubkey: Vec) -> Result<()> { debug!(target: "save_cash_key", "Save cashier keys..."); - let path = Self::wallet_path(); + //let path = Self::wallet_path(); let connect = Connection::open(&path).expect("Failed to connect to database."); let id = 0; // Write keys to database @@ -130,22 +75,6 @@ impl DBInterface { )?; Ok(()) } - - pub async fn save_key(pubkey: Vec) -> Result<()> { - debug!(target: "save_key", "Save keys..."); - let path = Self::wallet_path(); - let connect = Connection::open(&path).expect("Failed to connect to database."); - let id = 0; - // Write keys to database - connect.execute( - "INSERT INTO keys(key_id, key_public) - VALUES (:id, :pubkey)", - named_params! {":id": id, - ":pubkey": pubkey - }, - )?; - Ok(()) - } } fn main() {}