Просмотр исходного кода

introduce CoinParams to money_xfer

x 2 лет назад
Родитель
Сommit
fd934a8e3a
2 измененных файлов с 46 добавлено и 28 удалено
  1. 18 26
      src/contract/money/src/client/transfer_v1/proof.rs
  2. 28 2
      src/contract/money/src/model.rs

+ 18 - 26
src/contract/money/src/client/transfer_v1/proof.rs

@@ -33,7 +33,7 @@ use log::debug;
 use rand::rngs::OsRng;
 
 use super::{TransferCallInput, TransferCallOutput};
-use crate::model::Coin;
+use crate::model::{Coin, CoinParams};
 
 pub struct TransferMintRevealed {
     pub coin: Coin,
@@ -92,23 +92,22 @@ pub fn create_transfer_burn_proof(
 ) -> Result<(Proof, TransferBurnRevealed)> {
     let nullifier = Nullifier::from(poseidon_hash([input.secret.inner(), input.note.serial]));
     let public_key = PublicKey::from_secret(input.secret);
-    let (pub_x, pub_y) = public_key.xy();
 
     let signature_public = PublicKey::from_secret(signature_secret);
 
-    let coin = poseidon_hash([
-        pub_x,
-        pub_y,
-        pallas::Base::from(input.note.value),
-        input.note.token_id.inner(),
-        input.note.serial,
-        input.note.spend_hook,
-        input.note.user_data,
-    ]);
+    let coin = CoinParams {
+        public_key,
+        value: input.note.value,
+        token_id: input.note.token_id,
+        serial: input.note.serial,
+        spend_hook: input.note.spend_hook,
+        user_data: input.note.user_data,
+    }
+    .to_coin();
 
     let merkle_root = {
         let position: u64 = input.leaf_position.into();
-        let mut current = MerkleNode::from(coin);
+        let mut current = MerkleNode::from(coin.inner());
         for (level, sibling) in input.merkle_path.iter().enumerate() {
             let level = level as u8;
             current = if position & (1 << level) == 0 {
@@ -170,23 +169,16 @@ pub fn create_transfer_mint_proof(
     let token_commit = poseidon_hash([output.token_id.inner(), token_blind]);
     let (pub_x, pub_y) = output.public_key.xy();
 
-    let coin = Coin::from(poseidon_hash([
-        pub_x,
-        pub_y,
-        pallas::Base::from(output.value),
-        output.token_id.inner(),
+    let coin = CoinParams {
+        public_key: output.public_key,
+        value: output.value,
+        token_id: output.token_id,
         serial,
         spend_hook,
         user_data,
-    ]));
-    debug!("Created coin {:?}", coin);
-    debug!("  pub_x: {:?}", pub_x);
-    debug!("  pub_y: {:?}", pub_y);
-    debug!("  value: {:?}", pallas::Base::from(output.value));
-    debug!("  token_id: {:?}", output.token_id.inner());
-    debug!("  serial: {:?}", serial);
-    debug!("  spend_hook: {:?}", spend_hook);
-    debug!("  user_data: {:?}", user_data);
+    };
+    debug!("Created coin: {:?}", coin);
+    let coin = coin.to_coin();
 
     let public_inputs = TransferMintRevealed { coin, value_commit, token_commit };
 

+ 28 - 2
src/contract/money/src/model.rs

@@ -18,8 +18,8 @@
 
 use darkfi_sdk::{
     crypto::{
-        ecvrf::VrfProof, note::AeadEncryptedNote, pasta_prelude::PrimeField, MerkleNode, Nullifier,
-        PublicKey, TokenId,
+        ecvrf::VrfProof, note::AeadEncryptedNote, pasta_prelude::PrimeField, poseidon_hash,
+        MerkleNode, Nullifier, PublicKey, TokenId,
     },
     error::ContractError,
     pasta::pallas,
@@ -56,6 +56,32 @@ impl Coin {
     }
 }
 
+#[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
+pub struct CoinParams {
+    pub public_key: PublicKey,
+    pub value: u64,
+    pub token_id: TokenId,
+    pub serial: pallas::Base,
+    pub spend_hook: pallas::Base,
+    pub user_data: pallas::Base,
+}
+
+impl CoinParams {
+    pub fn to_coin(&self) -> Coin {
+        let (pub_x, pub_y) = self.public_key.xy();
+        let coin = poseidon_hash([
+            pub_x,
+            pub_y,
+            pallas::Base::from(self.value),
+            self.token_id.inner(),
+            self.serial,
+            self.spend_hook,
+            self.user_data,
+        ]);
+        Coin(coin)
+    }
+}
+
 use core::str::FromStr;
 darkfi_sdk::fp_from_bs58!(Coin);
 darkfi_sdk::fp_to_bs58!(Coin);