From fd80a1ddbe7df493c326deec334d0c41d93e2bde Mon Sep 17 00:00:00 2001 From: rachel-rose Date: Sun, 4 Jul 2021 13:44:21 +0200 Subject: [PATCH] debugged key gen and wallet write. all drkcli functions now working --- src/bin/darkfid.rs | 14 +++++----- src/rpc/jsonserver.rs | 2 +- src/wallet/walletdb.rs | 61 +++++++++++++++++++++--------------------- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/bin/darkfid.rs b/src/bin/darkfid.rs index 10ebc9bae..7de2cdfb3 100644 --- a/src/bin/darkfid.rs +++ b/src/bin/darkfid.rs @@ -1,4 +1,5 @@ use drk::blockchain::{rocks::columns, Rocks, RocksColumn}; +use log::*; use drk::cli::{cli_config, WalletCli}; use drk::crypto::{ load_params, @@ -190,12 +191,14 @@ async fn start( let merkle_roots = RocksColumn::::new(rocks.clone()); let nullifiers = RocksColumn::::new(rocks); - //let wallet = adapter.wallet; let wallet = Arc::new(WalletDB::new("wallet.db")?); - //let wallet2 = wallet.clone(); let ex = executor.clone(); + let adapter = RpcAdapter::new(wallet.clone())?; + // start the rpc server + jsonserver::start(ex.clone(), config.clone(), adapter).await?; + let state = State { tree: CommitmentTree::empty(), merkle_roots, @@ -206,20 +209,19 @@ async fn start( }; // create gateway client + debug!(target: "Client", "Creating client"); let mut client = GatewayClient::new(connect_addr, slabstore)?; + debug!(target: "Gateway", "Start subscriber"); // start subscribing let gateway_slabs_sub: GatewaySlabsSubscriber = client.start_subscriber(sub_addr, executor.clone()).await?; let subscribe_task = executor.spawn(subscribe(gateway_slabs_sub, state)); // start gateway client + debug!(target: "fn::start client", "start() Client started"); client.start().await?; - let adapter = RpcAdapter::new(wallet.clone())?; - // start the rpc server - jsonserver::start(ex.clone(), config.clone(), adapter).await?; - subscribe_task.cancel().await; Ok(()) } diff --git a/src/rpc/jsonserver.rs b/src/rpc/jsonserver.rs index 47c21ab6b..d3be0cf00 100644 --- a/src/rpc/jsonserver.rs +++ b/src/rpc/jsonserver.rs @@ -203,7 +203,7 @@ impl RpcInterface { .key_gen() .await?; Ok(jsonrpc_core::Value::String( - "Attempted key generation".into(), + "Key generation successful".into(), )) } }); diff --git a/src/wallet/walletdb.rs b/src/wallet/walletdb.rs index 8565f1598..7bca0f74d 100644 --- a/src/wallet/walletdb.rs +++ b/src/wallet/walletdb.rs @@ -83,8 +83,10 @@ impl WalletDB { let valcom_blind = self.get_value_serialized(¬e.valcom_blind).await?; let value = self.get_value_serialized(¬e.value).await?; let asset_id = self.get_value_serialized(¬e.asset_id).await?; - let conn = Connection::open(&self.path)?; let witness = self.get_value_serialized(&witness).await?; + let conn = Connection::open(&self.path)?; + let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?; + let _rows = stmt.query([])?; conn.execute( "INSERT INTO coins(coin, serial, value, asset_id, coin_blind, valcom_blind, witness, key_id) VALUES (NULL, :coin, :serial, :value, :asset_id, :coin_blind, :valcom_blind, :witness, :key_id)", @@ -101,8 +103,6 @@ impl WalletDB { Ok(()) } - - pub async fn key_gen(&self) -> (Vec, Vec) { debug!(target: "key_gen", "Attempting to generate keys..."); let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng); @@ -121,34 +121,32 @@ impl WalletDB { (pubkey, privkey) } - pub async fn put_keypair(&self, pubkey: Vec, privkey: Vec) -> Result<()> { + pub async fn put_keypair(&self, key_public: Vec, key_private: Vec) -> Result<()> { let conn = Connection::open(&self.path)?; - //conn.execute( - // "INSERT INTO keys(key_id, key_private, key_public) - // VALUES (NULL, :privkey, :pubkey)", - // named_params! { - // ":privkey": privkey, - // ":pubkey": pubkey - // }, - //)?; + let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?; + let _rows = stmt.query([])?; + conn.execute( + "INSERT INTO keys(key_public, key_private) VALUES (?1, ?2)", + params![key_public, key_private])?; Ok(()) } - pub async fn put_cashier_pub(&self, pubkey: Vec) -> Result<()> { + pub async fn put_cashier_pub(&self, key_public: Vec) -> Result<()> { debug!(target: "save_cash_key", "Save cashier keys..."); let conn = Connection::open(&self.path)?; - // Write keys to database + let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?; + let _rows = stmt.query([])?; conn.execute( - "INSERT INTO cashier(key_id, key_public) - VALUES (NULL, :pubkey)", - named_params! {":pubkey": pubkey}, - )?; + "INSERT INTO cashier(key_public) VALUES (?1)", + params![key_public])?; Ok(()) } pub async fn get_public(&self) -> Result> { debug!(target: "get", "Returning keys..."); let conn = Connection::open(&self.path)?; + let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?; + let _rows = stmt.query([])?; let mut stmt = conn.prepare("SELECT key_public FROM keys")?; let key_iter = stmt.query_map::([], |row| row.get(0))?; let mut pub_keys = Vec::new(); @@ -161,6 +159,8 @@ impl WalletDB { pub async fn get_cashier_public(&self) -> Result> { debug!(target: "get_cashier_public", "Returning keys..."); let conn = Connection::open(&self.path)?; + let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?; + let _rows = stmt.query([])?; let mut stmt = conn.prepare("SELECT key_public FROM cashier")?; let key_iter = stmt.query_map::([], |row| row.get(0))?; let mut pub_keys = Vec::new(); @@ -173,6 +173,8 @@ impl WalletDB { pub fn get_private(&self) -> Result> { debug!(target: "get", "Returning keys..."); let conn = Connection::open(&self.path)?; + let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?; + let _rows = stmt.query([])?; let mut stmt = conn.prepare("SELECT key_private FROM keys")?; let key_iter = stmt.query_map::([], |row| row.get(0))?; let mut keys = Vec::new(); @@ -184,8 +186,10 @@ impl WalletDB { pub fn test_wallet(&self) -> Result<()> { let conn = Connection::open(&self.path)?; + let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?; + let _rows = stmt.query([])?; let mut stmt = conn.prepare("SELECT * FROM keys")?; - stmt.execute(["NULL"])?; + let _rows = stmt.query([])?; Ok(()) } @@ -210,18 +214,13 @@ use super::*; let conn = Connection::open(path)?; 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); - let mut stmt = conn.prepare("SELECT * FROM keys")?; - stmt.execute(rusqlite::params![1i32])?; - //conn.execute( - // "INSERT INTO keys(key_private, key_public) - // VALUES (NULL, :privkey, :pubkey)", - // named_params! { - // ":privkey": privkey, - // ":pubkey": pubkey - // }, - //)?; + let key_public = serial::serialize(&public); + let key_private = serial::serialize(&secret); + let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?; + let _rows = stmt.query([])?; + conn.execute( + "INSERT INTO keys(key_public, key_private) VALUES (?1, ?2)", + params![key_public, key_private])?; Ok(()) } }