Przeglądaj źródła

contract/dao: Use AeadEncryptedNote from darkfi-sdk.

parazyd 3 lat temu
rodzic
commit
0feda5f8e5

+ 4 - 5
bin/drk/src/wallet_dao.rs

@@ -40,13 +40,13 @@ use darkfi_dao_contract::{
         DAO_VOTES_COL_VOTE_OPTION, DAO_VOTES_COL_YES_VOTE_BLIND, DAO_VOTES_TABLE,
     },
     dao_model::{DaoBulla, DaoMintParams, DaoProposeParams, DaoVoteParams},
-    note::EncryptedNote2,
     DaoFunction,
 };
 use darkfi_sdk::{
     bridgetree,
     crypto::{
-        poseidon_hash, MerkleNode, MerkleTree, PublicKey, SecretKey, TokenId, DAO_CONTRACT_ID,
+        note::AeadEncryptedNote, poseidon_hash, MerkleNode, MerkleTree, PublicKey, SecretKey,
+        TokenId, DAO_CONTRACT_ID,
     },
     pasta::pallas,
 };
@@ -1037,8 +1037,7 @@ impl Drk {
 
             for proposal in new_dao_proposals {
                 proposals_tree.append(MerkleNode::from(proposal.0.proposal_bulla));
-                // FIXME: EncryptedNote2 should perhaps be something generic?
-                let enc_note = EncryptedNote2 {
+                let enc_note = AeadEncryptedNote {
                     ciphertext: proposal.0.ciphertext,
                     ephem_public: proposal.0.ephem_public,
                 };
@@ -1075,7 +1074,7 @@ impl Drk {
             }
 
             for vote in new_dao_votes {
-                let enc_note = EncryptedNote2 {
+                let enc_note = AeadEncryptedNote {
                     ciphertext: vote.0.ciphertext,
                     ephem_public: vote.0.ephem_public,
                 };

+ 4 - 7
src/contract/dao/src/dao_client/propose.rs

@@ -20,8 +20,8 @@ use darkfi_sdk::{
     bridgetree,
     bridgetree::Hashable,
     crypto::{
-        pasta_prelude::*, pedersen::pedersen_commitment_u64, poseidon_hash, MerkleNode, PublicKey,
-        SecretKey, TokenId,
+        note::AeadEncryptedNote, pasta_prelude::*, pedersen::pedersen_commitment_u64,
+        poseidon_hash, MerkleNode, PublicKey, SecretKey, TokenId,
     },
     pasta::pallas,
 };
@@ -34,10 +34,7 @@ use darkfi::{
     Result,
 };
 
-use crate::{
-    dao_model::{DaoProposeParams, DaoProposeParamsInput},
-    note,
-};
+use crate::dao_model::{DaoProposeParams, DaoProposeParamsInput};
 
 use super::DaoInfo;
 
@@ -247,7 +244,7 @@ impl DaoProposeCall {
         proofs.push(main_proof);
 
         let note = DaoProposeNote { proposal: self.proposal };
-        let enc_note = note::encrypt(&note, &self.dao.public_key).unwrap();
+        let enc_note = AeadEncryptedNote::encrypt(&note, &self.dao.public_key, &mut OsRng).unwrap();
         let params = DaoProposeParams {
             dao_merkle_root: self.dao_merkle_root,
             proposal_bulla,

+ 5 - 7
src/contract/dao/src/dao_client/vote.rs

@@ -20,8 +20,8 @@ use darkfi_sdk::{
     bridgetree,
     bridgetree::Hashable,
     crypto::{
-        pasta_prelude::*, pedersen_commitment_u64, poseidon_hash, Keypair, MerkleNode, Nullifier,
-        PublicKey, SecretKey,
+        note::AeadEncryptedNote, pasta_prelude::*, pedersen_commitment_u64, poseidon_hash, Keypair,
+        MerkleNode, Nullifier, PublicKey, SecretKey,
     },
     pasta::pallas,
 };
@@ -36,10 +36,7 @@ use darkfi::{
 };
 
 use super::{DaoInfo, DaoProposalInfo};
-use crate::{
-    dao_model::{DaoVoteParams, DaoVoteParamsInput},
-    note,
-};
+use crate::dao_model::{DaoVoteParams, DaoVoteParamsInput};
 
 #[derive(SerialEncodable, SerialDecodable)]
 pub struct DaoVoteNote {
@@ -270,7 +267,8 @@ impl DaoVoteCall {
             all_vote_value,
             all_vote_blind,
         };
-        let enc_note = note::encrypt(&note, &self.vote_keypair.public).unwrap();
+        let enc_note =
+            AeadEncryptedNote::encrypt(&note, &self.vote_keypair.public, &mut OsRng).unwrap();
 
         let params = DaoVoteParams {
             token_commit,

+ 0 - 3
src/contract/dao/src/lib.rs

@@ -21,9 +21,6 @@ use darkfi_sdk::error::ContractError;
 #[cfg(not(feature = "no-entrypoint"))]
 pub mod entrypoint;
 
-#[cfg(feature = "client")]
-pub mod note;
-
 #[cfg(feature = "client")]
 /// Transaction building API for clients interacting with DAO contract
 pub mod dao_client;

+ 0 - 121
src/contract/dao/src/note.rs

@@ -1,121 +0,0 @@
-/* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2020-2023 Dyne.org foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- */
-
-use chacha20poly1305::{AeadInPlace, ChaCha20Poly1305, KeyInit};
-use darkfi_sdk::crypto::{diffie_hellman, PublicKey, SecretKey};
-use darkfi_serial::{Decodable, Encodable, SerialDecodable, SerialEncodable};
-use rand::rngs::OsRng;
-
-use darkfi::{Error, Result};
-
-pub const AEAD_TAG_SIZE: usize = 16;
-
-pub fn encrypt<T: Encodable>(note: &T, public: &PublicKey) -> Result<EncryptedNote2> {
-    let ephem_secret = SecretKey::random(&mut OsRng);
-    let ephem_public = PublicKey::from_secret(ephem_secret);
-    let shared_secret = diffie_hellman::sapling_ka_agree(&ephem_secret, public);
-    let key = diffie_hellman::kdf_sapling(&shared_secret, &ephem_public);
-
-    let mut input = Vec::new();
-    note.encode(&mut input)?;
-    let input_len = input.len();
-
-    let mut ciphertext = vec![0_u8; input_len + AEAD_TAG_SIZE];
-    ciphertext[..input_len].copy_from_slice(&input);
-
-    ChaCha20Poly1305::new(key.as_ref().into())
-        .encrypt_in_place([0u8; 12][..].into(), &[], &mut ciphertext)
-        .unwrap();
-
-    Ok(EncryptedNote2 { ciphertext, ephem_public })
-}
-
-#[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
-pub struct EncryptedNote2 {
-    pub ciphertext: Vec<u8>,
-    pub ephem_public: PublicKey,
-}
-
-impl EncryptedNote2 {
-    pub fn decrypt<T: Decodable>(&self, secret: &SecretKey) -> Result<T> {
-        let shared_secret = diffie_hellman::sapling_ka_agree(secret, &self.ephem_public);
-        let key = diffie_hellman::kdf_sapling(&shared_secret, &self.ephem_public);
-
-        let ciphertext_len = self.ciphertext.len();
-        let mut plaintext = vec![0_u8; ciphertext_len];
-        plaintext.copy_from_slice(&self.ciphertext);
-
-        match ChaCha20Poly1305::new(key.as_ref().into()).decrypt_in_place(
-            [0u8; 12][..].into(),
-            &[],
-            &mut plaintext,
-        ) {
-            Ok(()) => {
-                Ok(T::decode(&plaintext[..ciphertext_len - AEAD_TAG_SIZE]).map_err(Error::from)?)
-            }
-            Err(e) => Err(Error::NoteDecryptionFailed(e.to_string())),
-        }
-    }
-}
-
-// TODO: This test is broken:
-//       1. no darkfi::crypto::type
-//       2. SerialDecodable cannot infer type for type parameter `D`
-/*
-#[cfg(test)]
-mod tests {
-    use super::*;
-    use darkfi::crypto::types::{DrkCoinBlind, DrkSerial, DrkValueBlind};
-    use darkfi_sdk::{
-        crypto::{Keypair, TokenId},
-        pasta::{group::ff::Field, pallas},
-    };
-
-    #[test]
-    fn test_note_encdec() {
-        #[derive(SerialEncodable, SerialDecodable)]
-        struct MyNote {
-            serial: DrkSerial,
-            value: u64,
-            token_id: TokenId,
-            coin_blind: DrkCoinBlind,
-            value_blind: DrkValueBlind,
-            token_blind: DrkValueBlind,
-            memo: Vec<u8>,
-        }
-        let note = MyNote {
-            serial: DrkSerial::random(&mut OsRng),
-            value: 110,
-            token_id: TokenId::from(pallas::Base::random(&mut OsRng)),
-            coin_blind: DrkCoinBlind::random(&mut OsRng),
-            value_blind: DrkValueBlind::random(&mut OsRng),
-            token_blind: DrkValueBlind::random(&mut OsRng),
-            memo: vec![32, 223, 231, 3, 1, 1],
-        };
-
-        let keypair = Keypair::random(&mut OsRng);
-
-        let encrypted_note = encrypt(&note, &keypair.public).unwrap();
-        let note2: MyNote = encrypted_note.decrypt(&keypair.secret).unwrap();
-        assert_eq!(note.value, note2.value);
-        assert_eq!(note.token_id, note2.token_id);
-        assert_eq!(note.token_blind, note2.token_blind);
-        assert_eq!(note.memo, note2.memo);
-    }
-}
-*/

+ 12 - 20
src/contract/dao/tests/integration.rs

@@ -21,8 +21,9 @@ use std::time::{Duration, Instant};
 use darkfi::{tx::Transaction, Result};
 use darkfi_sdk::{
     crypto::{
-        pasta_prelude::*, pedersen_commitment_u64, poseidon_hash, Keypair, MerkleNode, MerkleTree,
-        SecretKey, TokenId, DAO_CONTRACT_ID, DARK_TOKEN_ID, MONEY_CONTRACT_ID,
+        note::AeadEncryptedNote, pasta_prelude::*, pedersen_commitment_u64, poseidon_hash, Keypair,
+        MerkleNode, MerkleTree, SecretKey, TokenId, DAO_CONTRACT_ID, DARK_TOKEN_ID,
+        MONEY_CONTRACT_ID,
     },
     pasta::pallas,
     ContractCall,
@@ -32,7 +33,7 @@ use log::debug;
 use rand::rngs::OsRng;
 
 use darkfi_dao_contract::{
-    dao_client, dao_model, money_client, note, wallet_cache::WalletCache, DaoFunction,
+    dao_client, dao_model, money_client, wallet_cache::WalletCache, DaoFunction,
 };
 
 use darkfi_money_contract::{
@@ -460,11 +461,8 @@ async fn integration_test() -> Result<()> {
 
     // Read received proposal
     let (proposal, proposal_bulla) = {
-        // TODO: EncryptedNote should be accessible by wasm and put in the structs directly
-        let enc_note = note::EncryptedNote2 {
-            ciphertext: params.ciphertext,
-            ephem_public: params.ephem_public,
-        };
+        let enc_note =
+            AeadEncryptedNote { ciphertext: params.ciphertext, ephem_public: params.ephem_public };
         let note: dao_client::DaoProposeNote = enc_note.decrypt(&dao_th.dao_kp.secret).unwrap();
 
         // TODO: check it belongs to DAO bulla
@@ -572,10 +570,8 @@ async fn integration_test() -> Result<()> {
     // TODO: look into verifiable encryption for notes
     // TODO: look into timelock puzzle as a possibility
     let vote_note_1 = {
-        let enc_note = note::EncryptedNote2 {
-            ciphertext: params.ciphertext,
-            ephem_public: params.ephem_public,
-        };
+        let enc_note =
+            AeadEncryptedNote { ciphertext: params.ciphertext, ephem_public: params.ephem_public };
         let note: dao_client::DaoVoteNote = enc_note.decrypt(&vote_keypair_1.secret).unwrap();
         note
     };
@@ -644,10 +640,8 @@ async fn integration_test() -> Result<()> {
     vote_verify_times.push(timer.elapsed());
 
     let vote_note_2 = {
-        let enc_note = note::EncryptedNote2 {
-            ciphertext: params.ciphertext,
-            ephem_public: params.ephem_public,
-        };
+        let enc_note =
+            AeadEncryptedNote { ciphertext: params.ciphertext, ephem_public: params.ephem_public };
         let note: dao_client::DaoVoteNote = enc_note.decrypt(&vote_keypair_2.secret).unwrap();
         note
     };
@@ -719,10 +713,8 @@ async fn integration_test() -> Result<()> {
     // TODO: look into verifiable encryption for notes
     // TODO: look into timelock puzzle as a possibility
     let vote_note_3 = {
-        let enc_note = note::EncryptedNote2 {
-            ciphertext: params.ciphertext,
-            ephem_public: params.ephem_public,
-        };
+        let enc_note =
+            AeadEncryptedNote { ciphertext: params.ciphertext, ephem_public: params.ephem_public };
         let note: dao_client::DaoVoteNote = enc_note.decrypt(&vote_keypair_3.secret).unwrap();
         note
     };