From f42e9357009f84c659b093fc1d4abbbe39fe8a10 Mon Sep 17 00:00:00 2001 From: parazyd Date: Wed, 13 Apr 2022 12:43:10 +0200 Subject: [PATCH] lib: Move wallet module out of the node module. --- Cargo.lock | 1 + Cargo.toml | 5 ++++- src/error.rs | 14 ++++++++++++-- src/lib.rs | 3 +++ src/node/client.rs | 20 ++++++++++---------- src/node/mod.rs | 3 --- src/node/state.rs | 3 +-- src/node/wallet/mod.rs | 3 --- src/{node => }/wallet/cashierdb.rs | 16 ++++++++-------- src/wallet/mod.rs | 12 ++++++++++++ src/{node => }/wallet/wallet_api.rs | 0 src/{node => }/wallet/walletdb.rs | 16 ++++++++-------- 12 files changed, 59 insertions(+), 37 deletions(-) delete mode 100644 src/node/wallet/mod.rs rename src/{node => }/wallet/cashierdb.rs (97%) create mode 100644 src/wallet/mod.rs rename src/{node => }/wallet/wallet_api.rs (100%) rename src/{node => }/wallet/walletdb.rs (97%) diff --git a/Cargo.lock b/Cargo.lock index a859594db..b01543c51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6786,6 +6786,7 @@ dependencies = [ "idna", "matches", "percent-encoding", + "serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 37e920278..aa3a64344 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,7 +70,7 @@ serde_json = {version = "1.0.79", optional = true} serde = {version = "1.0.136", features = ["derive"], optional = true} # Utilities -url = {version = "2.2.2", optional = true} +url = {version = "2.2.2", features = ["serde"], optional = true} dirs = {version = "4.0.0", optional = true} subtle = {version = "2.4.1", optional = true} lazy_static = {version = "1.4.0", optional = true} @@ -270,6 +270,9 @@ crypto = [ wallet = [ "sqlx", "libsqlite3-sys", + + "crypto", + "util", ] wasm-runtime = [ diff --git a/src/error.rs b/src/error.rs index 8e0830804..eafddb476 100644 --- a/src/error.rs +++ b/src/error.rs @@ -104,6 +104,9 @@ pub enum Error { #[error("Client failed: `{0}`")] ClientFailed(String), + #[error("Wallet error: `{0}`")] + WalletError(String), + #[error("Cashier failed: `{0}`")] CashierError(String), @@ -114,7 +117,7 @@ pub enum Error { #[error("Rocksdb error: `{0}`")] RocksdbError(String), - #[cfg(feature = "node")] + #[cfg(feature = "wallet")] #[error("sqlx error: `{0}`")] SqlxError(String), @@ -252,7 +255,7 @@ impl From for Error { } } -#[cfg(feature = "node")] +#[cfg(feature = "wallet")] impl From for Error { fn from(err: sqlx::error::Error) -> Error { Error::SqlxError(err.to_string()) @@ -300,6 +303,13 @@ impl From for Error { } } +#[cfg(feature = "wallet")] +impl From for Error { + fn from(err: crate::wallet::WalletError) -> Error { + Error::WalletError(err.to_string()) + } +} + impl From for Error { fn from(_err: log::SetLoggerError) -> Error { Error::SetLoggerError diff --git a/src/lib.rs b/src/lib.rs index 8e3517c13..372a0a1b8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,3 +45,6 @@ pub mod zkas; #[cfg(feature = "raft")] pub mod raft; + +#[cfg(feature = "wallet")] +pub mod wallet; diff --git a/src/node/client.rs b/src/node/client.rs index 79b11c977..19273b6ed 100644 --- a/src/node/client.rs +++ b/src/node/client.rs @@ -5,6 +5,10 @@ use log::{debug, info, warn}; use smol::Executor; use url::Url; +use super::{ + service::GatewayClient, + state::{state_transition, State, StateUpdate}, +}; use crate::{ blockchain::{rocks::columns, Rocks, RocksColumn, Slab}, crypto::{ @@ -18,17 +22,12 @@ use crate::{ }, tx, util::serial::{Decodable, Encodable}, - zk::circuit::{MintContract, SpendContract}, - Result, -}; - -use super::{ - service::GatewayClient, - state::{state_transition, State, StateUpdate}, wallet::{ cashierdb::CashierDbPtr, walletdb::{Balances, WalletPtr}, }, + zk::circuit::{MintContract, SpendContract}, + Result, }; #[derive(Debug, Clone, thiserror::Error)] @@ -95,7 +94,7 @@ impl Client { if wallet.get_default_keypair().await.is_err() { // Generate a new keypair if we don't have any. if wallet.get_keypairs().await?.is_empty() { - wallet.key_gen().await?; + wallet.keygen().await?; } // set the first keypair as the default one wallet.set_default_keypair(&wallet.get_keypairs().await?[0].public).await?; @@ -397,8 +396,9 @@ impl Client { Ok(()) } - pub async fn key_gen(&self) -> Result<()> { - self.wallet.key_gen().await + pub async fn keygen(&self) -> Result<()> { + let _ = self.wallet.keygen().await?; + Ok(()) } pub async fn get_balances(&self) -> Result { diff --git a/src/node/mod.rs b/src/node/mod.rs index a94839e7a..1fabbcfd4 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -1,6 +1,3 @@ pub mod client; pub mod service; pub mod state; - -#[cfg(feature = "wallet")] -pub mod wallet; diff --git a/src/node/state.rs b/src/node/state.rs index 24a618dce..d425f1e22 100644 --- a/src/node/state.rs +++ b/src/node/state.rs @@ -14,11 +14,10 @@ use crate::{ }, error, tx::Transaction, + wallet::walletdb::WalletPtr, Result, }; -use super::wallet::walletdb::WalletPtr; - pub trait ProgramState { fn is_valid_cashier_public_key(&self, public: &PublicKey) -> bool; fn is_valid_merkle(&self, merkle: &MerkleNode) -> bool; diff --git a/src/node/wallet/mod.rs b/src/node/wallet/mod.rs deleted file mode 100644 index 8a1a521bc..000000000 --- a/src/node/wallet/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod cashierdb; -pub mod wallet_api; -pub mod walletdb; diff --git a/src/node/wallet/cashierdb.rs b/src/wallet/cashierdb.rs similarity index 97% rename from src/node/wallet/cashierdb.rs rename to src/wallet/cashierdb.rs index 1a6185530..634ab0b14 100644 --- a/src/node/wallet/cashierdb.rs +++ b/src/wallet/cashierdb.rs @@ -10,15 +10,15 @@ use sqlx::{ use super::wallet_api::WalletApi; +use super::WalletError; use crate::{ crypto::{ keypair::{Keypair, PublicKey, SecretKey}, merkle_node::MerkleNode, types::DrkTokenId, }, - node::client::ClientFailed, util::NetworkName, - Error, Result, + Result, }; pub type CashierDbPtr = Arc; @@ -54,7 +54,7 @@ impl CashierDb { 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)) + return Err(WalletError::EmptyPassword.into()) } if path != "sqlite::memory:" { @@ -80,9 +80,9 @@ impl CashierDb { } pub async fn init_db(&self) -> Result<()> { - let main_kps = include_str!("../../../script/sql/cashier_main_keypairs.sql"); - let deposit_kps = include_str!("../../../script/sql/cashier_deposit_keypairs.sql"); - let withdraw_kps = include_str!("../../../script/sql/cashier_withdraw_keypairs.sql"); + let main_kps = include_str!("../../script/sql/cashier_main_keypairs.sql"); + let deposit_kps = include_str!("../../script/sql/cashier_deposit_keypairs.sql"); + let withdraw_kps = include_str!("../../script/sql/cashier_withdraw_keypairs.sql"); let mut conn = self.conn.acquire().await?; @@ -103,8 +103,8 @@ impl CashierDb { match sqlx::query("SELECT * FROM tree").fetch_one(&mut conn).await { Ok(_) => { - error!("Tree already exists"); - Err(Error::from(ClientFailed::TreeExists)) + error!("Merkle tree already exists"); + Err(WalletError::TreeExists.into()) } Err(_) => { let tree = BridgeTree::::new(100); diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs new file mode 100644 index 000000000..f8da6e1e6 --- /dev/null +++ b/src/wallet/mod.rs @@ -0,0 +1,12 @@ +pub mod cashierdb; +pub mod wallet_api; +pub mod walletdb; + +#[derive(Debug, Clone, thiserror::Error)] +pub enum WalletError { + #[error("Empty password")] + EmptyPassword, + + #[error("Merkle tree already exists")] + TreeExists, +} diff --git a/src/node/wallet/wallet_api.rs b/src/wallet/wallet_api.rs similarity index 100% rename from src/node/wallet/wallet_api.rs rename to src/wallet/wallet_api.rs diff --git a/src/node/wallet/walletdb.rs b/src/wallet/walletdb.rs similarity index 97% rename from src/node/wallet/walletdb.rs rename to src/wallet/walletdb.rs index fbb3ae134..5b3294ffe 100644 --- a/src/node/wallet/walletdb.rs +++ b/src/wallet/walletdb.rs @@ -9,6 +9,7 @@ use sqlx::{ ConnectOptions, Row, SqlitePool, }; +use super::WalletError; use crate::{ crypto::{ coin::Coin, @@ -19,9 +20,8 @@ use crate::{ types::DrkTokenId, OwnCoin, OwnCoins, }, - node::client::ClientFailed, util::serial::serialize, - Error, Result, + Result, }; use super::wallet_api::WalletApi; @@ -50,7 +50,7 @@ impl WalletDb { pub async fn new(path: &str, password: &str) -> Result { if password.trim().is_empty() { error!("Password is empty. You must set a password to use the wallet."); - return Err(Error::from(ClientFailed::EmptyPassword)) + return Err(WalletError::EmptyPassword.into()) } if path != "sqlite::memory:" { @@ -77,9 +77,9 @@ impl WalletDb { pub async fn init_db(&self) -> Result<()> { info!("Initializing wallet database"); - let tree = include_str!("../../../script/sql/tree.sql"); - let keys = include_str!("../../../script/sql/keys.sql"); - let coins = include_str!("../../../script/sql/coins.sql"); + let tree = include_str!("../../script/sql/tree.sql"); + let keys = include_str!("../../script/sql/keys.sql"); + let coins = include_str!("../../script/sql/coins.sql"); let mut conn = self.conn.acquire().await?; @@ -175,8 +175,8 @@ impl WalletDb { match sqlx::query("SELECT * FROM tree").fetch_one(&mut conn).await { Ok(_) => { - error!("Tree already exists"); - Err(Error::from(ClientFailed::TreeExists)) + error!("Merkle tree already exists"); + Err(WalletError::TreeExists.into()) } Err(_) => { let tree = BridgeTree::::new(100);