|
@@ -1,6 +1,6 @@
|
|
|
use crate::crypto::{coin::Coin, merkle::IncrementalWitness, merkle_node::MerkleNode, note::Note};
|
|
use crate::crypto::{coin::Coin, merkle::IncrementalWitness, merkle_node::MerkleNode, note::Note};
|
|
|
use crate::serial;
|
|
use crate::serial;
|
|
|
-use crate::serial::{deserialize, Decodable};
|
|
|
|
|
|
|
+use crate::serial::{deserialize, serialize, Decodable, Encodable};
|
|
|
use crate::Error;
|
|
use crate::Error;
|
|
|
use crate::Result;
|
|
use crate::Result;
|
|
|
use async_std::sync::Arc;
|
|
use async_std::sync::Arc;
|
|
@@ -40,6 +40,31 @@ impl WalletDB {
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ pub async fn put_own_coins(&self) -> Result<()> {
|
|
|
|
|
+ let note = &self.own_coins[0].1;
|
|
|
|
|
+ let coin = self.get_value_serialized(&self.own_coins[0].0.repr).await?;
|
|
|
|
|
+ let serial = self.get_value_serialized(¬e.serial).await?;
|
|
|
|
|
+ let coin_blind = self.get_value_serialized(¬e.coin_blind).await?;
|
|
|
|
|
+ let valcom_blind = self.get_value_serialized(¬e.valcom_blind).await?;
|
|
|
|
|
+ let value = self.get_value_serialized(¬e.value).await?;
|
|
|
|
|
+ let conn = Connection::open(&self.path)?;
|
|
|
|
|
+ // witness deserialization not implemented
|
|
|
|
|
+ conn.execute(
|
|
|
|
|
+ "INSERT INTO coins(coin, serial, value, coin_blind, valcom_blind, witness, key_id)
|
|
|
|
|
+ VALUES (NULL, :coin, :serial, :value, :coin_blind, :valcom_blind, :witness, :key_id)",
|
|
|
|
|
+ named_params! {
|
|
|
|
|
+ ":coin": coin,
|
|
|
|
|
+ ":serial": serial,
|
|
|
|
|
+ ":value": value,
|
|
|
|
|
+ ":coin_blind": coin_blind,
|
|
|
|
|
+ ":valcom_blind": valcom_blind,
|
|
|
|
|
+ //":privkey": privkey,
|
|
|
|
|
+ //":pubkey": pubkey
|
|
|
|
|
+ },
|
|
|
|
|
+ )?;
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
fn create_path(wallet: &str) -> Result<PathBuf> {
|
|
fn create_path(wallet: &str) -> Result<PathBuf> {
|
|
|
let mut path = dirs::home_dir()
|
|
let mut path = dirs::home_dir()
|
|
|
.ok_or(Error::PathNotFound)?
|
|
.ok_or(Error::PathNotFound)?
|
|
@@ -50,11 +75,16 @@ impl WalletDB {
|
|
|
Ok(path)
|
|
Ok(path)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //fn get_path() -> Result<PathBuf> {
|
|
|
|
|
- // Ok(self.path)
|
|
|
|
|
- //}
|
|
|
|
|
|
|
+ pub async fn key_gen(&self) -> (Vec<u8>, Vec<u8>) {
|
|
|
|
|
+ debug!(target: "key_gen", "Generating keys...");
|
|
|
|
|
+ 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);
|
|
|
|
|
+ (pubkey, privkey)
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- pub async fn put_key(&self, pubkey: Vec<u8>, privkey: Vec<u8>) -> Result<()> {
|
|
|
|
|
|
|
+ pub async fn put_keypair(&self, pubkey: Vec<u8>, privkey: Vec<u8>) -> Result<()> {
|
|
|
//debug!(target: "key_gen", "Generating keys...");
|
|
//debug!(target: "key_gen", "Generating keys...");
|
|
|
let conn = Connection::open(&self.path)?;
|
|
let conn = Connection::open(&self.path)?;
|
|
|
//debug!(target: "adapter", "key_gen() [Saving public key...]");
|
|
//debug!(target: "adapter", "key_gen() [Saving public key...]");
|
|
@@ -69,13 +99,16 @@ impl WalletDB {
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- pub async fn key_gen(&self) -> (Vec<u8>, Vec<u8>) {
|
|
|
|
|
- debug!(target: "key_gen", "Generating keys...");
|
|
|
|
|
- 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);
|
|
|
|
|
- (pubkey, privkey)
|
|
|
|
|
|
|
+ pub async fn put_cashier_pub(&self, pubkey: Vec<u8>) -> Result<()> {
|
|
|
|
|
+ debug!(target: "save_cash_key", "Save cashier keys...");
|
|
|
|
|
+ let conn = Connection::open(&self.path)?;
|
|
|
|
|
+ // Write keys to database
|
|
|
|
|
+ conn.execute(
|
|
|
|
|
+ "INSERT INTO cashier(key_id, key_public)
|
|
|
|
|
+ VALUES (NULL, :pubkey)",
|
|
|
|
|
+ named_params! {":pubkey": pubkey},
|
|
|
|
|
+ )?;
|
|
|
|
|
+ Ok(())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_public(&self) -> Result<Vec<u8>> {
|
|
pub async fn get_public(&self) -> Result<Vec<u8>> {
|
|
@@ -102,28 +135,12 @@ impl WalletDB {
|
|
|
Ok(keys)
|
|
Ok(keys)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ pub async fn get_value_serialized<T: Encodable>(&self, data: &T) -> Result<Vec<u8>> {
|
|
|
|
|
+ let v = serialize(data);
|
|
|
|
|
+ Ok(v)
|
|
|
|
|
+ }
|
|
|
pub async fn get_value_deserialized<D: Decodable>(&self, key: Vec<u8>) -> Result<D> {
|
|
pub async fn get_value_deserialized<D: Decodable>(&self, key: Vec<u8>) -> Result<D> {
|
|
|
let v: D = deserialize(&key)?;
|
|
let v: D = deserialize(&key)?;
|
|
|
Ok(v)
|
|
Ok(v)
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- pub async fn put_cashier_pub(&self, pubkey: Vec<u8>) -> Result<()> {
|
|
|
|
|
- debug!(target: "save_cash_key", "Save cashier keys...");
|
|
|
|
|
- let conn = Connection::open(&self.path)?;
|
|
|
|
|
- // Write keys to database
|
|
|
|
|
- conn.execute(
|
|
|
|
|
- "INSERT INTO cashier(key_id, key_public)
|
|
|
|
|
- VALUES (NULL, :pubkey)",
|
|
|
|
|
- named_params! {":pubkey": pubkey},
|
|
|
|
|
- )?;
|
|
|
|
|
- Ok(())
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- pub async fn is_valid_cashier_pub(&self, public: &jubjub::SubgroupPoint) -> Result<bool> {
|
|
|
|
|
- let conn = Connection::open(&self.path)?;
|
|
|
|
|
- let mut stmt = conn
|
|
|
|
|
- .prepare("SELECT key_public FROM cashier WHERE key_public IN (SELECT key_public)")
|
|
|
|
|
- .expect("Cannot generate statement.");
|
|
|
|
|
- Ok(stmt.exists([1i32])?)
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|