From fc36d9b95daeebfc23361cf04b702ce58dd9331f Mon Sep 17 00:00:00 2001 From: rachel-rose Date: Sat, 29 May 2021 09:15:31 +0200 Subject: [PATCH] ?got key generation from cli working --- src/rpc/adapter.rs | 28 +++++++++++++++++++++++++--- src/rpc/jsonserver.rs | 7 ++----- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/rpc/adapter.rs b/src/rpc/adapter.rs index 7fd0a7e51..dcb8b0f49 100644 --- a/src/rpc/adapter.rs +++ b/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, Vec) { + 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<()> { diff --git a/src/rpc/jsonserver.rs b/src/rpc/jsonserver.rs index 85a55de97..9ae4bdefa 100644 --- a/src/rpc/jsonserver.rs +++ b/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(), ))