diff --git a/src/rpc/adapter.rs b/src/rpc/adapter.rs index 5efe3a188..b23e02138 100644 --- a/src/rpc/adapter.rs +++ b/src/rpc/adapter.rs @@ -1,6 +1,13 @@ -// Adapter class goes here -//use crate::rpc::jsonserver::JsonRpcInterface; +#[macro_use] use std::sync::Arc; +use rusqlite::Connection; +use ff::Field; +use rand::rngs::OsRng; +use std::fs::File; +use std::io::prelude::*; +use crate::serial; +use crate::Result; +use smol::Async; // Dummy adapter for now pub struct RpcAdapter {} @@ -10,9 +17,32 @@ impl RpcAdapter { Arc::new(Self {}) } - pub async fn get_info() {} + pub async fn db_connect() -> Connection { + let path = dirs::home_dir() + .expect("Cannot find home directory.") + .as_path() + .join(".config/darkfi/wallet.db"); + let connector = Connection::open(&path); + connector.expect("Failed to connect to database.") + } - pub async fn key_gen() {} + pub async fn key_gen() -> (Vec, Vec) { + 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); + (privkey, pubkey) + } + + // TODO: getting an error when i call this function- does not implement send + pub async fn save_key(conn: &Connection, pubkey: Vec, privkey: Vec) -> Result<()> { + let mut db_file = File::open("wallet.sql")?; + let mut contents = String::new(); + db_file.read_to_string(&mut contents)?; + Ok(conn.execute_batch(&mut contents)?) + } + + pub async fn get_info() {} pub async fn say_hello() {} diff --git a/src/rpc/jsonserver.rs b/src/rpc/jsonserver.rs index d2ceefd76..5d7c9577f 100644 --- a/src/rpc/jsonserver.rs +++ b/src/rpc/jsonserver.rs @@ -155,7 +155,10 @@ impl RpcInterface { Ok(jsonrpc_core::Value::Null) }); io.add_method("key_gen", move |_| async move { - RpcAdapter::key_gen().await; + //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; Ok(jsonrpc_core::Value::Null) }); debug!(target: "rpc", "JsonRpcInterface::handle_input() [END]");