Explorar el Código

?got key generation from cli working

rachel-rose hace 5 años
padre
commit
fc36d9b95d
Se han modificado 2 ficheros con 27 adiciones y 8 borrados
  1. 25 3
      src/rpc/adapter.rs
  2. 2 5
      src/rpc/jsonserver.rs

+ 25 - 3
src/rpc/adapter.rs

@@ -3,7 +3,7 @@ use crate::Result;
 use ff::Field;
 use log::*;
 use rand::rngs::OsRng;
-use rusqlite::Connection;
+use rusqlite::{named_params, Connection};
 use std::fs::File;
 use std::io::prelude::*;
 use std::sync::Arc;
@@ -25,12 +25,34 @@ impl RpcAdapter {
         connector.expect("Failed to connect to database.")
     }
 
-    pub async fn key_gen() -> (Vec<u8>, Vec<u8>) {
+    pub async fn key_gen() -> Result<()> {
+        debug!(target: "adapter", "key_gen() [START]");
+        let path = dirs::home_dir()
+            .expect("Cannot find home directory.")
+            .as_path()
+            .join(".config/darkfi/wallet.db");
+        debug!(target: "adapter", "key_gen() [FOUND PATH]");
+        println!("Found path: {:?}", &path);
+        debug!(target: "adapter", "key_gen() [TRY DB CONNECT]");
+        let connect = Connection::open(&path).expect("Failed to connect to database.");
+        // TODO: assign new ID on each run
+        debug!(target: "adapter", "key_gen() [Assigning ID...]");
+        let id = 0;
+        debug!(target: "adapter", "key_gen() [Generating private key...]");
         let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
+        debug!(target: "adapter", "key_gen() [Generating public key...]");
         let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
         let pubkey = serial::serialize(&public);
         let privkey = serial::serialize(&secret);
-        (privkey, pubkey)
+        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 new_wallet() -> Result<()> {

+ 2 - 5
src/rpc/jsonserver.rs

@@ -160,11 +160,8 @@ impl RpcInterface {
             Ok(jsonrpc_core::Value::Null)
         });
         io.add_method("key_gen", move |_| async move {
-            println!("beep");
-            //let connection = RpcAdapter::db_connect().await;
-            //let (public, private) = RpcAdapter::key_gen().await;
-            // getting an error on the following:
-            // RpcAdapter::save_key(&connection, private, public).await;
+            println!("Key generation method called...");
+            RpcAdapter::key_gen().await.expect("Failed to generate key");
             Ok(jsonrpc_core::Value::String(
                 "Attempted key generation".into(),
             ))