Parcourir la source

lib: Move wallet module out of the node module.

parazyd il y a 4 ans
Parent
commit
f42e935700
12 fichiers modifiés avec 59 ajouts et 37 suppressions
  1. 1 0
      Cargo.lock
  2. 4 1
      Cargo.toml
  3. 12 2
      src/error.rs
  4. 3 0
      src/lib.rs
  5. 10 10
      src/node/client.rs
  6. 0 3
      src/node/mod.rs
  7. 1 2
      src/node/state.rs
  8. 0 3
      src/node/wallet/mod.rs
  9. 8 8
      src/wallet/cashierdb.rs
  10. 12 0
      src/wallet/mod.rs
  11. 0 0
      src/wallet/wallet_api.rs
  12. 8 8
      src/wallet/walletdb.rs

+ 1 - 0
Cargo.lock

@@ -6786,6 +6786,7 @@ dependencies = [
  "idna",
  "matches",
  "percent-encoding",
+ "serde",
 ]
 
 [[package]]

+ 4 - 1
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 = [

+ 12 - 2
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<rocksdb::Error> for Error {
     }
 }
 
-#[cfg(feature = "node")]
+#[cfg(feature = "wallet")]
 impl From<sqlx::error::Error> for Error {
     fn from(err: sqlx::error::Error) -> Error {
         Error::SqlxError(err.to_string())
@@ -300,6 +303,13 @@ impl From<crate::node::client::ClientFailed> for Error {
     }
 }
 
+#[cfg(feature = "wallet")]
+impl From<crate::wallet::WalletError> for Error {
+    fn from(err: crate::wallet::WalletError) -> Error {
+        Error::WalletError(err.to_string())
+    }
+}
+
 impl From<log::SetLoggerError> for Error {
     fn from(_err: log::SetLoggerError) -> Error {
         Error::SetLoggerError

+ 3 - 0
src/lib.rs

@@ -45,3 +45,6 @@ pub mod zkas;
 
 #[cfg(feature = "raft")]
 pub mod raft;
+
+#[cfg(feature = "wallet")]
+pub mod wallet;

+ 10 - 10
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<Balances> {

+ 0 - 3
src/node/mod.rs

@@ -1,6 +1,3 @@
 pub mod client;
 pub mod service;
 pub mod state;
-
-#[cfg(feature = "wallet")]
-pub mod wallet;

+ 1 - 2
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;

+ 0 - 3
src/node/wallet/mod.rs

@@ -1,3 +0,0 @@
-pub mod cashierdb;
-pub mod wallet_api;
-pub mod walletdb;

+ 8 - 8
src/node/wallet/cashierdb.rs → 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<CashierDb>;
@@ -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::<MerkleNode, 32>::new(100);

+ 12 - 0
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,
+}

+ 0 - 0
src/node/wallet/wallet_api.rs → src/wallet/wallet_api.rs


+ 8 - 8
src/node/wallet/walletdb.rs → 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<WalletPtr> {
         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::<MerkleNode, 32>::new(100);