فهرست منبع

fixed key gen

rachel-rose 5 سال پیش
والد
کامیت
1ff8cfa793
3فایلهای تغییر یافته به همراه42 افزوده شده و 19 حذف شده
  1. 23 1
      src/bin/darkfid.rs
  2. 4 3
      src/rpc/adapter.rs
  3. 15 15
      src/wallet/walletdb.rs

+ 23 - 1
src/bin/darkfid.rs

@@ -54,6 +54,7 @@ impl ProgramState for State {
         let mut stmt = connect
             .prepare("SELECT key_public FROM cashier WHERE key_public IN (SELECT key_public)")
             .expect("Cannot generate statement.");
+        // test this
         stmt.exists([1i32]).unwrap()
     }
 
@@ -69,6 +70,7 @@ impl ProgramState for State {
             .expect("couldn't check if nullifier exists")
     }
 
+    // load from disk
     fn mint_pvk(&self) -> &groth16::PreparedVerifyingKey<Bls12> {
         &self.mint_pvk
     }
@@ -120,7 +122,26 @@ impl State {
 
     // sql
     fn try_decrypt_note(&self, _ciphertext: EncryptedNote) -> Option<(Note, jubjub::Fr)> {
-        // TODO
+        //let connect = Connection::open(&path).expect("Failed to connect to database.");
+        //let mut stmt = connect.prepare("SELECT key_private FROM keys").ok()?;
+        //let key_iter = stmt.query_map::<String, _, _>([], |row| row.get(0)).ok()?;
+        //for key in key_iter {
+        //    println!("Found key {:?}", key.unwrap());
+        //}
+        //
+        //// Loop through all our secret keys...
+
+        //for secret in &self.secrets {
+        //    // ... attempt to decrypt the note ...
+        //    match ciphertext.decrypt(secret) {
+        //        Ok(note) => {
+        //            // ... and return the decrypted note for this coin.
+        //            return Some((note, secret.clone()));
+        //        }
+        //        Err(_) => {}
+        //    }
+        //}
+        // We weren't able to decrypt the note with any of our keys.
         None
     }
 }
@@ -189,6 +210,7 @@ async fn start(executor: Arc<Executor<'_>>, options: ClientProgramOptions) -> Re
     let nullifiers = RocksColumn::<columns::Nullifiers>::new(rocks);
 
     let state = State {
+
         tree: CommitmentTree::empty(),
         merkle_roots,
         nullifiers,

+ 4 - 3
src/rpc/adapter.rs

@@ -12,11 +12,12 @@ impl RpcAdapter {
         Arc::new(Self {})
     }
 
-    pub async fn key_gen() -> Result<PathBuf> {
+    pub async fn key_gen() -> Result<()> {
         debug!(target: "adapter", "key_gen() [START]");
+        let (public, private) = WalletDB::create_key().await;
         let path = WalletDB::path("wallet.db").expect("Failed to get path");
-        //WalletDB::key_gen(path).await?;
-        Ok(path)
+        WalletDB::save_key(path, public, private).await.expect("Failed to save key");
+        Ok(())
     }
 
     pub async fn new_wallet() -> Result<()> {

+ 15 - 15
src/wallet/walletdb.rs

@@ -17,35 +17,25 @@ impl WalletDB {
         Ok(connect.execute_batch(&contents)?)
     }
 
-    //    pub async fn create_keypair() -> Result<String, String> {
-    //        let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
-    //        let pubkey = serial::serialize(&public);
-    //        let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
-    //        let privkey = serial::serialize(&secret);
-    //        Ok(pubkey, privkey)
-    //    }
     pub fn path(wallet: &str) -> Result<PathBuf> {
         let mut path = dirs::home_dir()
             .expect("cannot find home directory.")
             .as_path()
             .join(".config/darkfi/");
-        debug!(target: "walletdb", "CREATE PATH {:?}", path);
+        // add wallet specifier
         path.push(wallet);
+        debug!(target: "walletdb", "CREATE PATH {:?}", path);
         Ok(path)
     }
 
-    pub async fn key_gen(path: PathBuf, id: i32, pubkey: Vec<u8>, privkey: Vec<u8>) -> Result<()> {
+    // unique index in sql
+    // unique index attribute will generate new ID every new table
+    pub async fn save_key(path: PathBuf, pubkey: Vec<u8>, privkey: Vec<u8>) -> Result<()> {
         debug!(target: "key_gen", "Generating keys...");
         let connect = Connection::open(&path).expect("Failed to connect to database.");
         // TODO: ID should not be fixed
         let id = 0;
-        // Create keys
-        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);
-        // Write keys to database
         connect.execute(
             "INSERT INTO keys(key_id, key_private, key_public)
             VALUES (:id, :privkey, :pubkey)",
@@ -57,6 +47,16 @@ impl WalletDB {
         Ok(())
     }
 
+    pub async fn create_key() -> (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);
+        // Write keys to database
+        (pubkey, privkey)
+    }
+
     pub async fn get(path: PathBuf) -> Result<()> {
         debug!(target: "get_cash_public", "Returning cashier keys...");
         let connect = Connection::open(&path).expect("Failed to connect to database.");