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

re-implemented drk cli function calls

rachel-rose 5 лет назад
Родитель
Сommit
cf160aafb0
4 измененных файлов с 88 добавлено и 63 удалено
  1. 2 2
      scripts/dark_client/util.py
  2. 20 26
      src/rpc/adapter.rs
  3. 45 33
      src/rpc/jsonserver.rs
  4. 21 2
      src/wallet/walletdb.rs

+ 2 - 2
scripts/dark_client/util.py

@@ -23,7 +23,7 @@ async def arg_parser(client):
             await client.key_gen(client.payload)
 
         if args.wallet:
-            print("Attemping to generate a create wallet...")
+            print("Attemping to create a wallet...")
             await client.create_wallet(client.payload)
 
         if args.info:
@@ -41,7 +41,7 @@ async def arg_parser(client):
             await client.say_hello(client.payload)
 
         if args.cashier:
-            print("Cash was entered")
+            print("Attempting to generate a cashier wallet...")
             await client.create_cashier_wallet(client.payload)
 
     except Exception:

+ 20 - 26
src/rpc/adapter.rs

@@ -3,7 +3,6 @@ use crate::Result;
 use log::*;
 //use std::sync::Arc;
 
-// there should
 // Dummy adapter for now
 pub struct RpcAdapter {
     pub wallet: WalletDB,
@@ -16,9 +15,6 @@ impl RpcAdapter {
         Ok(Self { wallet })
     }
 
-    //pub async fn get_path(&self) -> Result<PathBuf> {
-    //}
-
     pub async fn key_gen(&self) -> Result<()> {
         debug!(target: "adapter", "key_gen() [START]");
         let (public, private) = self.wallet.key_gen().await;
@@ -28,41 +24,39 @@ impl RpcAdapter {
 
     pub async fn cash_key_gen(&self) -> Result<()> {
         debug!(target: "adapter", "key_gen() [START]");
-        //let (public, private) = WalletDB::create_key().await;
-        //let path = WalletDB::path("cashier.db")?;
-        //WalletDB::save_key(path, public, private)
-        //    .await
-        //    .expect("Failed to save key");
+        let (public, private) = self.wallet.key_gen().await;
+        //self.wallet.put_keypair(public, private).await?;
         Ok(())
     }
 
     pub async fn get_key(&self) -> Result<()> {
         debug!(target: "adapter", "get_key() [START]");
-        //let path = WalletDB::path("wallet.db")?;
-        //WalletDB::get_public(path).await?;
+        let key_public = self.wallet.get_public().await?;
+        println!("{:?}", key_public);
         Ok(())
     }
 
     pub async fn get_cash_key(&self) -> Result<()> {
         debug!(target: "adapter", "get_cash_key() [START]");
-        //let path = WalletDB::path("cashier.db")?;
-        //let key = WalletDB::get_public(path).await?;
-        //println!("{:?}", key);
-        Ok(())
-    }
-    pub async fn save_key(&self, pubkey: Vec<u8>) -> Result<()> {
-        debug!(target: "adapter", "save_key() [START]");
-        //let path = WalletDB::path("wallet.db")?;
-        //WalletDB::save(path, pubkey).await?;
+        let cashier_public = self.wallet.get_public().await?;
+        println!("{:?}", cashier_public);
         Ok(())
     }
 
-    pub async fn save_cash_key(&self, pubkey: Vec<u8>) -> Result<()> {
-        debug!(target: "adapter", "save_cash_key() [START]");
-        //let path = WalletDB::path("cashier.db")?;
-        //WalletDB::save(path, pubkey).await?;
-        Ok(())
-    }
+    //pub async fn create_
+    //pub async fn save_key(&self, pubkey: Vec<u8>) -> Result<()> {
+    //    debug!(target: "adapter", "save_key() [START]");
+    //    //let path = WalletDB::path("wallet.db")?;
+    //    //WalletDB::save(path, pubkey).await?;
+    //    Ok(())
+    //}
+
+    //pub async fn save_cash_key(&self, pubkey: Vec<u8>) -> Result<()> {
+    //    debug!(target: "adapter", "save_cash_key() [START]");
+    //    //let path = WalletDB::path("cashier.db")?;
+    //    //WalletDB::save(path, pubkey).await?;
+    //    Ok(())
+    //}
 
     pub async fn get_info(&self) {}
 

+ 45 - 33
src/rpc/jsonserver.rs

@@ -151,64 +151,76 @@ impl RpcInterface {
             Ok(jsonrpc_core::Value::String("TEST PATH!".into()))
         });
 
+        let self1 = self.clone();
         io.add_method("get_cash_key", move |_| {
+            let self2 = self1.clone();
             async move {
-                //RpcAdapter::get_cash_key().await.expect("Failed to get key");
+                self2.adapter.get_cash_key().await.expect("Failed to get key");
                 Ok(jsonrpc_core::Value::String("Getting cashier key...".into()))
             }
         });
 
+        let self1 = self.clone();
         io.add_method("get_info", move |_| {
+        let self2 = self1.clone();
             async move {
-                //RpcAdapter::get_info().await;
+                self2.adapter.get_info().await;
                 Ok(jsonrpc_core::Value::Null)
             }
         });
 
+        let self1 = self.clone();
         io.add_method("stop", move |_| {
+        let self2 = self1.clone();
             async move {
-                //self.adapter.stop().await;
+                self2.adapter.stop().await;
                 Ok(jsonrpc_core::Value::Null)
             }
         });
-        io.add_method("new_wallet", move |_| async move {
+        let self1 = self.clone();
+        io.add_method("create_wallet", move |_| {
+            let self2 = self1.clone();
+            async move {
             println!("New wallet method called...");
             RpcAdapter::new("wallet.db").expect("Failed to create wallet");
+            println!("Wallet created at path {:?}", self2.adapter.wallet.path);
             Ok(jsonrpc_core::Value::String(
-                "Attempted wallet generation".into(),
-            ))
+                "Created wallet".into(),))
+            }
+        });
+        let self1 = self.clone();
+        io.add_method("key_gen", move |_| {
+            let self2 = self1.clone();
+            async move {
+                println!("Key generation method called...");
+                self2.adapter.key_gen().await.expect("Failed to generate key");
+                Ok(jsonrpc_core::Value::String(
+                    "Attempted key generation".into(),
+                ))
+            }
         });
-        //let self3 = self.clone();
-        //io.add_method("key_gen", move |_| {
-        //    let self4 = self3.clone();
-        //    async move {
-        //        println!("Key generation method called...");
-        //        self4.adapter.key_gen().await.expect("Failed to generate key");
-        //        //RpcAdapter::key_gen().await.expect("Failed to generate key");
-        //        Ok(jsonrpc_core::Value::String(
-        //            "Attempted key generation".into(),
-        //        ))
-        //    }
-        //});
-        //let self5 = self.clone();
-        //io.add_method("cash_key_gen", move |_| {
-        //    let self6 = self5.clone();
-        //    async move {
-        //        println!("Key generation method called...");
-        //        //RpcAdapter::cash_key_gen()
-        //            //.await
-        //            //.expect("Failed to generate key");
-        //        Ok(jsonrpc_core::Value::String(
-        //            "Attempted key generation".into(),
-        //        ))
-        //    }
-        //});
-        io.add_method("new_cashier_wallet", move |_| async move {
+        let self1 = self.clone();
+        io.add_method("cash_key_gen", move |_| {
+            let self2 = self1.clone();
+            async move {
+                println!("Key generation method called...");
+                self2.adapter.cash_key_gen().await.expect("Failed to generate key");
+                Ok(jsonrpc_core::Value::String(
+                    "Attempted key generation".into(),
+                ))
+            }
+        });
+        let self1 = self.clone();
+        io.add_method("create_cashier_wallet", move |_| {
+            let self2 = self1.clone();
+            async move {
             println!("New wallet method called...");
             RpcAdapter::new("cashier.db").expect("Failed to create wallet");
+            println!("Wallet created at path {:?}", self2.adapter.wallet.path);
             Ok(jsonrpc_core::Value::String(
-                "Attempted wallet generation".into(),
+                "Created cashier wallet".into(),
             ))
+            }
         });
         debug!(target: "rpc", "JsonRpcInterface::handle_input() [END]");
         Ok(io)

+ 21 - 2
src/wallet/walletdb.rs

@@ -90,10 +90,17 @@ impl WalletDB {
         (pubkey, privkey)
     }
 
+    pub async fn cash_key_gen(&self) -> (Vec<u8>, Vec<u8>) {
+        debug!(target: "cash key_gen", "Generating cashier 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);
+        (pubkey, privkey)
+    }
+
     pub async fn put_keypair(&self, pubkey: Vec<u8>, privkey: Vec<u8>) -> Result<()> {
-        //debug!(target: "key_gen", "Generating keys...");
         let conn = Connection::open(&self.path)?;
-        //debug!(target: "adapter", "key_gen() [Saving public key...]");
         conn.execute(
             "INSERT INTO keys(key_id, key_private, key_public)
             VALUES (NULL, :privkey, :pubkey)",
@@ -129,6 +136,18 @@ impl WalletDB {
         Ok(pub_keys)
     }
 
+    pub async fn get_cashier_public(&self) -> Result<Vec<u8>> {
+        debug!(target: "get_cashier_public", "Returning keys...");
+        let conn = Connection::open(&self.path)?;
+        let mut stmt = conn.prepare("SELECT key_public FROM cashier")?;
+        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)
+    }
+
     pub fn get_private(&self) -> Result<Vec<u8>> {
         debug!(target: "get", "Returning keys...");
         let conn = Connection::open(&self.path)?;