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

added deserialize to walletdb.rs 'get' functions. ran cargo fmt

lunar-mining 5 лет назад
Родитель
Сommit
a6b4791feb

+ 1 - 2
src/bin/cashierd.rs

@@ -6,8 +6,8 @@ use std::{path::Path, path::PathBuf};
 
 use drk::blockchain::{rocks::columns, Rocks, RocksColumn};
 use drk::cli::{CashierdCli, CashierdConfig};
-use drk::wallet::{WalletDb, WalletPtr};
 use drk::service::CashierService;
+use drk::wallet::{WalletDb, WalletPtr};
 
 use drk::util::join_config_path;
 use drk::Result;
@@ -71,7 +71,6 @@ fn main() -> Result<()> {
     ])
     .unwrap();
 
-
     let ex2 = ex.clone();
 
     let (_, result) = Parallel::new()

+ 3 - 3
src/bin/darkfid.rs

@@ -18,21 +18,21 @@ use drk::wallet::{WalletDb, WalletPtr};
 use drk::{tx, Result};
 use log::*;
 
-use std::net::TcpStream;
-use futures::AsyncWriteExt;
 use async_executor::Executor;
 use bellman::groth16;
 use bls12_381::Bls12;
 use easy_parallel::Parallel;
 use ff::Field;
+use futures::AsyncWriteExt;
 use rand::rngs::OsRng;
 use rusqlite::Connection;
+use std::net::TcpStream;
 
 use async_std::sync::Arc;
+use smol::Async;
 use std::net::SocketAddr;
 use std::path::Path;
 use std::path::PathBuf;
-use smol::Async;
 
 #[allow(dead_code)]
 pub struct State {

+ 2 - 3
src/blockchain/cashierstore.rs

@@ -3,8 +3,8 @@ use std::sync::Arc;
 use crate::serial::{deserialize, serialize};
 use crate::Result;
 
-use super::rocks::{columns, IteratorMode, RocksColumn};
 use super::cashier_keypair::CashierKeypair;
+use super::rocks::{columns, IteratorMode, RocksColumn};
 
 pub struct CashierStore {
     rocks: RocksColumn<columns::CashierKeys>,
@@ -21,7 +21,6 @@ impl CashierStore {
     }
 
     pub fn put(&self, keypair: CashierKeypair) -> Result<Option<jubjub::SubgroupPoint>> {
-
         let index = keypair.get_index();
 
         match self.get(index) {
@@ -29,7 +28,7 @@ impl CashierStore {
             Err(_e) => {
                 self.rocks.put(index.clone(), keypair)?;
                 Ok(Some(index))
-            },
+            }
         }
     }
 

+ 4 - 4
src/blockchain/mod.rs

@@ -1,11 +1,11 @@
+pub mod cashier_keypair;
+pub mod cashierstore;
 pub mod rocks;
 pub mod slab;
 pub mod slabstore;
-pub mod cashier_keypair;
-pub mod cashierstore;
 
+pub use cashier_keypair::CashierKeypair;
+pub use cashierstore::CashierStore;
 pub use rocks::{Rocks, RocksColumn};
 pub use slab::Slab;
 pub use slabstore::SlabStore;
-pub use cashier_keypair::CashierKeypair;
-pub use cashierstore::CashierStore;

+ 9 - 2
src/blockchain/rocks.rs

@@ -54,12 +54,19 @@ impl Rocks {
         // nullifiers column family
         let nullifiers_cf = ColumnFamilyDescriptor::new(columns::Nullifiers::NAME, cf_opts.clone());
         // merkleroots column family
-        let merkleroots_cf = ColumnFamilyDescriptor::new(columns::MerkleRoots::NAME, cf_opts.clone());
+        let merkleroots_cf =
+            ColumnFamilyDescriptor::new(columns::MerkleRoots::NAME, cf_opts.clone());
         // cashierkeypair column family
         let cashierkeys_cf = ColumnFamilyDescriptor::new(columns::CashierKeys::NAME, cf_opts);
 
         // column families
-        let cfs = vec![default_cf, slab_cf, nullifiers_cf, merkleroots_cf, cashierkeys_cf];
+        let cfs = vec![
+            default_cf,
+            slab_cf,
+            nullifiers_cf,
+            merkleroots_cf,
+            cashierkeys_cf,
+        ];
 
         // database options
         let mut opt = Options::default();

+ 1 - 3
src/cli/cashierd_cli.rs

@@ -16,8 +16,6 @@ impl CashierdCli {
 
         let verbose = app.is_present("VERBOSE");
 
-        Ok(Self {
-            verbose,
-        })
+        Ok(Self { verbose })
     }
 }

+ 6 - 1
src/cli/cli_config.rs

@@ -202,6 +202,11 @@ impl Default for CashierdConfig {
         let database_path = String::from("cashierd.db");
         let log_path = String::from("/tmp/cashierd.log");
         let password = String::new();
-        Self { accept_url, database_path, log_path, password }
+        Self {
+            accept_url,
+            database_path,
+            log_path,
+            password,
+        }
     }
 }

+ 3 - 3
src/cli/mod.rs

@@ -1,12 +1,12 @@
+pub mod cashierd_cli;
 pub mod cli_config;
 pub mod darkfid_cli;
 pub mod drk_cli;
 pub mod gatewayd_cli;
-pub mod cashierd_cli;
 
-pub use cli_config::{DarkfidConfig, DrkConfig, CashierdConfig, GatewaydConfig};
+pub use cashierd_cli::CashierdCli;
+pub use cli_config::{CashierdConfig, DarkfidConfig, DrkConfig, GatewaydConfig};
 pub use darkfid_cli::DarkfidCli;
 pub use drk_cli::DrkCli;
 pub use drk_cli::Transfer;
 pub use gatewayd_cli::GatewaydCli;
-pub use cashierd_cli::CashierdCli;

+ 1 - 3
src/rpc/jsonserver.rs

@@ -247,9 +247,7 @@ impl RpcInterface {
         let self1 = self.clone();
         io.add_method("deposit", move |_| {
             let self2 = self1.clone();
-            async move {
-                Ok(jsonrpc_core::Value::String("Initiating deposit... ".into()))
-            }
+            async move { Ok(jsonrpc_core::Value::String("Initiating deposit... ".into())) }
         });
 
         io.add_method("transfer", |params: jsonrpc_core::Params| async move {

+ 12 - 24
src/service/cashier.rs

@@ -1,22 +1,22 @@
-use rand::{thread_rng, Rng};
+use bitcoin::util::address::Address;
+use bitcoin::util::ecdsa::{PrivateKey, PublicKey as BitcoinPubKey};
 use rand::distributions::Alphanumeric;
+use rand::{thread_rng, Rng};
 use secp256k1::key::SecretKey;
-use bitcoin::util::ecdsa::{PrivateKey, PublicKey as BitcoinPubKey};
-use bitcoin::util::address::Address;
 
 use bitcoin::network::constants::Network;
 
-use bitcoin::hash_types::PubkeyHash;
 use super::reqrep::{PeerId, RepProtocol, Reply, ReqProtocol, Request};
-use crate::blockchain::{rocks::columns, RocksColumn, CashierKeypair, CashierStore};
+use crate::blockchain::{rocks::columns, CashierKeypair, CashierStore, RocksColumn};
 use crate::{serial::deserialize, serial::serialize, Error, Result};
+use bitcoin::hash_types::PubkeyHash;
 
 use crate::wallet::{WalletDb, WalletPtr};
 
-use std::net::SocketAddr;
-use async_std::sync::Arc;
 use async_executor::Executor;
+use async_std::sync::Arc;
 use log::*;
+use std::net::SocketAddr;
 
 #[repr(u8)]
 enum CashierError {
@@ -39,10 +39,7 @@ pub struct BitcoinKeys {
 }
 
 impl BitcoinKeys {
-    pub fn new(
-
-    ) -> Result<BitcoinKeys> {
-
+    pub fn new() -> Result<BitcoinKeys> {
         let context = secp256k1::Secp256k1::new();
 
         // Probably not good enough for release
@@ -93,7 +90,7 @@ impl CashierService {
         addr: SocketAddr,
         rocks: RocksColumn<columns::CashierKeys>,
         wallet: Arc<WalletDb>,
-    )-> Result<Arc<CashierService>> {
+    ) -> Result<Arc<CashierService>> {
         let cashierstore = CashierStore::new(rocks)?;
 
         Ok(Arc::new(CashierService {
@@ -109,11 +106,8 @@ impl CashierService {
 
         let (send, recv) = protocol.start().await?;
 
-        let handle_request_task = executor.spawn(self.handle_request_loop(
-            send.clone(),
-            recv.clone(),
-            executor.clone(),
-        ));
+        let handle_request_task =
+            executor.spawn(self.handle_request_loop(send.clone(), recv.clone(), executor.clone()));
 
         protocol.run(executor.clone()).await?;
 
@@ -132,11 +126,7 @@ impl CashierService {
                 Ok(msg) => {
                     let cashierstore = self.cashierstore.clone();
                     let _ = executor
-                        .spawn(Self::handle_request(
-                            msg,
-                            cashierstore,
-                            send_queue.clone(),
-                        ))
+                        .spawn(Self::handle_request(msg, cashierstore, send_queue.clone()))
                         .detach();
                 }
                 Err(_) => {
@@ -174,7 +164,6 @@ impl CashierService {
                 send_queue.send((peer, reply)).await?;
 
                 info!("Received dkey->btc msg");
-
             }
             1 => {
                 // Withdraw
@@ -232,7 +221,6 @@ impl CashierClient {
     pub fn get_cashierstore(&self) -> Arc<CashierStore> {
         self.cashierstore.clone()
     }
-
 }
 
 fn handle_error(status_code: u32) {

+ 2 - 2
src/service/mod.rs

@@ -1,7 +1,7 @@
+pub mod cashier;
 pub mod gateway;
 pub mod reqrep;
-pub mod cashier;
 
 pub use gateway::{GatewayClient, GatewayService, GatewaySlabsSubscriber};
 
-pub use cashier::{BitcoinKeys, CashierService, CashierClient};
+pub use cashier::{BitcoinKeys, CashierClient, CashierService};

+ 11 - 6
src/wallet/walletdb.rs

@@ -159,20 +159,22 @@ impl WalletDb {
         Ok(())
     }
 
-    pub fn get_public(&self) -> Result<Vec<u8>> {
+    pub fn get_public(&self) -> Result<jubjub::SubgroupPoint> {
         debug!(target: "get", "Returning keys...");
         let conn = Connection::open(&self.path)?;
         conn.pragma_update(None, "key", &self.password)?;
         let mut stmt = conn.prepare("SELECT key_public FROM keys")?;
+        // this just gets the first key. maybe we should randomize this
         let key_iter = stmt.query_map::<u8, _, _>([], |row| row.get(0))?;
         let mut pub_keys = Vec::new();
         for key in key_iter {
             pub_keys.push(key?);
         }
-        Ok(pub_keys)
+        let public: jubjub::SubgroupPoint = self.get_value_deserialized(pub_keys)?;
+        Ok(public)
     }
 
-    pub fn get_cashier_public(&self) -> Result<Vec<u8>> {
+    pub fn get_cashier_public(&self) -> Result<jubjub::SubgroupPoint> {
         debug!(target: "get_cashier_public", "Returning keys...");
         let conn = Connection::open(&self.path)?;
         conn.pragma_update(None, "key", &self.password)?;
@@ -182,10 +184,11 @@ impl WalletDb {
         for key in key_iter {
             pub_keys.push(key?);
         }
-        Ok(pub_keys)
+        let public: jubjub::SubgroupPoint = self.get_value_deserialized(pub_keys)?;
+        Ok(public)
     }
 
-    pub fn get_private(&self) -> Result<Vec<u8>> {
+    pub fn get_private(&self) -> Result<jubjub::Fr> {
         debug!(target: "get", "Returning keys...");
         let conn = Connection::open(&self.path)?;
         conn.pragma_update(None, "key", &self.password)?;
@@ -195,7 +198,8 @@ impl WalletDb {
         for key in key_iter {
             keys.push(key?);
         }
-        Ok(keys)
+        let private: jubjub::Fr = self.get_value_deserialized(keys)?;
+        Ok(private)
     }
 
     pub fn test_wallet(&self) -> Result<()> {
@@ -210,6 +214,7 @@ impl WalletDb {
         let v = serialize(data);
         Ok(v)
     }
+
     pub fn get_value_deserialized<D: Decodable>(&self, key: Vec<u8>) -> Result<D> {
         let v: D = deserialize(&key)?;
         Ok(v)