debugged key gen and wallet write. all drkcli functions now working

This commit is contained in:
rachel-rose
2021-07-04 13:44:21 +02:00
parent d54af89ea6
commit fd80a1ddbe
3 changed files with 39 additions and 38 deletions

View File

@@ -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::<columns::MerkleRoots>::new(rocks.clone());
let nullifiers = RocksColumn::<columns::Nullifiers>::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(())
}

View File

@@ -203,7 +203,7 @@ impl RpcInterface {
.key_gen()
.await?;
Ok(jsonrpc_core::Value::String(
"Attempted key generation".into(),
"Key generation successful".into(),
))
}
});

View File

@@ -83,8 +83,10 @@ impl WalletDB {
let valcom_blind = self.get_value_serialized(&note.valcom_blind).await?;
let value = self.get_value_serialized(&note.value).await?;
let asset_id = self.get_value_serialized(&note.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<u8>, Vec<u8>) {
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<u8>, privkey: Vec<u8>) -> Result<()> {
pub async fn put_keypair(&self, key_public: Vec<u8>, key_private: Vec<u8>) -> 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<u8>) -> Result<()> {
pub async fn put_cashier_pub(&self, key_public: Vec<u8>) -> 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<Vec<u8>> {
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::<u8, _, _>([], |row| row.get(0))?;
let mut pub_keys = Vec::new();
@@ -161,6 +159,8 @@ impl WalletDB {
pub async fn get_cashier_public(&self) -> Result<Vec<u8>> {
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::<u8, _, _>([], |row| row.get(0))?;
let mut pub_keys = Vec::new();
@@ -173,6 +173,8 @@ impl WalletDB {
pub fn get_private(&self) -> Result<Vec<u8>> {
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::<u8, _, _>([], |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(())
}
}