From 59c78ad2d9bf1bc405fda43d6b3af8664578a48a Mon Sep 17 00:00:00 2001 From: rachel-rose Date: Mon, 10 May 2021 11:14:00 +0200 Subject: [PATCH] added key gen and wallet connect functions --- src/bin/wallet/darkd.rs | 48 ++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/bin/wallet/darkd.rs b/src/bin/wallet/darkd.rs index 2f2013f08..88ae6156d 100644 --- a/src/bin/wallet/darkd.rs +++ b/src/bin/wallet/darkd.rs @@ -11,11 +11,15 @@ use rand::rngs::OsRng; use sapvi::serial; use serde_json::json; use smol::Async; +use std::fs::File; +use std::io::prelude::*; +use std::io::BufReader; use std::net::SocketAddr; use std::net::TcpListener; use std::sync::Arc; +use rusqlite::Connection; -use sapvi::{net, Result}; +use sapvi::{net, Result, Error}; /// Listens for incoming connections and serves them. async fn listen( @@ -95,6 +99,31 @@ impl RpcInterface { }) } + 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.") + } + + async fn generate_key() -> (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: fix this + async fn store_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)?) + } + // add new methods to handle wallet commands async fn serve(self: Arc, mut req: Request) -> http_types::Result { info!("RPC serving {}", req.url()); @@ -125,18 +154,11 @@ impl RpcInterface { Ok(jsonrpc_core::Value::Null) } }); - io.add_method("key_gen", move |_| { - //let stop_send = stop_send.clone(); - async move { - //let _ = stop_send.send(()).await; - 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); - //println!("{:?}", pubkey); - //println!("{:?}", privkey); - Ok(jsonrpc_core::Value::Null) - } + io.add_method("key_gen", move |_| async move { + RpcInterface::db_connect().await; + let (pubkey, privkey) = RpcInterface::generate_key().await; + //println!("{}", pubkey, "{}", privkey); + Ok(jsonrpc_core::Value::Null) }); let response = io