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

contract/money: switched auth_token_mint and token_mint execution order

skoupidi 2 лет назад
Родитель
Сommit
f52b4573e9

+ 15 - 20
bin/drk/src/token.rs

@@ -281,22 +281,10 @@ impl Drk {
             blind: Blind::random(&mut OsRng),
         };
 
-        // Create the minting call
-        let builder = TokenMintCallBuilder {
-            coin_attrs: coin_attrs.clone(),
-            token_attrs: token_attrs.clone(),
-            mint_zkbin,
-            mint_pk,
-        };
-        let mint_debris = builder.build()?;
-        let mut data = vec![MoneyFunction::TokenMintV1 as u8];
-        mint_debris.params.encode_async(&mut data).await?;
-        let mint_call = ContractCall { contract_id: *MONEY_CONTRACT_ID, data };
-
         // Create the auth call
         let builder = AuthTokenMintCallBuilder {
-            coin_attrs,
-            token_attrs,
+            coin_attrs: coin_attrs.clone(),
+            token_attrs: token_attrs.clone(),
             mint_keypair: mint_authority,
             auth_mint_zkbin,
             auth_mint_pk,
@@ -306,11 +294,18 @@ impl Drk {
         auth_debris.params.encode_async(&mut data).await?;
         let auth_call = ContractCall { contract_id: *MONEY_CONTRACT_ID, data };
 
+        // Create the minting call
+        let builder = TokenMintCallBuilder { coin_attrs, token_attrs, mint_zkbin, mint_pk };
+        let mint_debris = builder.build()?;
+        let mut data = vec![MoneyFunction::TokenMintV1 as u8];
+        mint_debris.params.encode_async(&mut data).await?;
+        let mint_call = ContractCall { contract_id: *MONEY_CONTRACT_ID, data };
+
         // Create the TransactionBuilder containing above calls
         let mut tx_builder = TransactionBuilder::new(
-            ContractCallLeaf { call: auth_call, proofs: auth_debris.proofs },
+            ContractCallLeaf { call: mint_call, proofs: mint_debris.proofs },
             vec![DarkTree::new(
-                ContractCallLeaf { call: mint_call, proofs: mint_debris.proofs },
+                ContractCallLeaf { call: auth_call, proofs: auth_debris.proofs },
                 vec![],
                 None,
                 None,
@@ -320,9 +315,9 @@ impl Drk {
         // We first have to execute the fee-less tx to gather its used gas, and then we feed
         // it into the fee-creating function.
         let mut tx = tx_builder.build()?;
-        let mint_sigs = tx.create_sigs(&[])?;
         let auth_sigs = tx.create_sigs(&[mint_authority.secret])?;
-        tx.signatures = vec![mint_sigs, auth_sigs];
+        let mint_sigs = tx.create_sigs(&[])?;
+        tx.signatures = vec![auth_sigs, mint_sigs];
 
         let tree = self.get_money_tree().await?;
         let secret = self.default_secret().await?;
@@ -335,10 +330,10 @@ impl Drk {
 
         // Now build the actual transaction and sign it with all necessary keys.
         let mut tx = tx_builder.build()?;
-        let sigs = tx.create_sigs(&[])?;
-        tx.signatures.push(sigs);
         let sigs = tx.create_sigs(&[mint_authority.secret])?;
         tx.signatures.push(sigs);
+        let sigs = tx.create_sigs(&[])?;
+        tx.signatures.push(sigs);
         let sigs = tx.create_sigs(&fee_secrets)?;
         tx.signatures.push(sigs);
 

+ 2 - 11
src/contract/money/src/entrypoint/auth_token_mint_v1.rs

@@ -38,16 +38,7 @@ pub(crate) fn money_auth_token_mint_get_metadata_v1(
     call_idx: usize,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
-    let self_ = &calls[call_idx];
-    if self_.children_indexes.len() != 1 {
-        msg!(
-            "[MintV1] Error: Children indexes length is not expected(1): {}",
-            self_.children_indexes.len()
-        );
-        return Err(MoneyError::ChildrenIndexesLengthMismatch.into())
-    }
-
-    let params: MoneyAuthTokenMintParamsV1 = deserialize(&self_.data.data[1..])?;
+    let params: MoneyAuthTokenMintParamsV1 = deserialize(&calls[call_idx].data.data[1..])?;
 
     // Public inputs for the ZK proofs we have to verify
     let mut zk_public_inputs: Vec<(String, Vec<pallas::Base>)> = vec![];
@@ -81,7 +72,7 @@ pub(crate) fn money_auth_token_mint_process_instruction_v1(
 
     // Check that the mint is not frozen
     if wasm::db::db_contains_key(token_freeze_db, &serialize(&params.token_id))? {
-        msg!("[MintV1] Error: Token mint for {} is frozen", params.token_id);
+        msg!("[AuthTokenMintV1] Error: Token mint for {} is frozen", params.token_id);
         return Err(MoneyError::TokenMintFrozen.into())
     }
 

+ 18 - 9
src/contract/money/src/entrypoint/token_mint_v1.rs

@@ -41,25 +41,34 @@ pub(crate) fn money_token_mint_get_metadata_v1(
     call_idx: usize,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
-    let self_ = &calls[call_idx].data;
-    let params: MoneyTokenMintParamsV1 = deserialize(&self_.data[1..])?;
+    let self_ = &calls[call_idx];
+
+    // Grab the auth call info
+    if self_.children_indexes.len() != 1 {
+        msg!(
+            "[MintV1] Error: Children indexes length is not expected(1): {}",
+            self_.children_indexes.len()
+        );
+        return Err(MoneyError::ChildrenIndexesLengthMismatch.into())
+    }
+    let child_idx = self_.children_indexes[0];
+    let child_call = &calls[child_idx].data;
+    let child_contract_id = child_call.contract_id;
+    let child_func_code = child_call.data[0];
 
-    let parent_idx = calls[call_idx as usize].parent_index.unwrap();
-    let parent_call = &calls[parent_idx].data;
-    let parent_contract_id = parent_call.contract_id;
-    let parent_func_code = parent_call.data[0];
+    let params: MoneyTokenMintParamsV1 = deserialize(&self_.data.data[1..])?;
 
     // Public inputs for the ZK proofs we have to verify
     let mut zk_public_inputs: Vec<(String, Vec<pallas::Base>)> = vec![];
     // Public keys for the transaction signatures we have to verify.
     let signature_pubkeys: Vec<PublicKey> = vec![];
 
-    let parent_func_id =
-        FuncRef { contract_id: parent_contract_id, func_code: parent_func_code }.to_func_id();
+    let child_func_id =
+        FuncRef { contract_id: child_contract_id, func_code: child_func_code }.to_func_id();
 
     zk_public_inputs.push((
         MONEY_CONTRACT_ZKAS_TOKEN_MINT_NS_V1.to_string(),
-        vec![parent_func_id.inner(), params.coin.inner()],
+        vec![child_func_id.inner(), params.coin.inner()],
     ));
 
     // Serialize everything gathered and return it

+ 20 - 20
src/contract/test-harness/src/money_token.rs

@@ -99,22 +99,10 @@ impl TestHarness {
             blind: Blind::random(&mut OsRng),
         };
 
-        // Create the minting call
-        let builder = TokenMintCallBuilder {
-            coin_attrs: coin_attrs.clone(),
-            token_attrs: token_attrs.clone(),
-            mint_zkbin: token_mint_zkbin.clone(),
-            mint_pk: token_mint_pk.clone(),
-        };
-        let mint_debris = builder.build()?;
-        let mut data = vec![MoneyFunction::TokenMintV1 as u8];
-        mint_debris.params.encode_async(&mut data).await?;
-        let mint_call = ContractCall { contract_id: *MONEY_CONTRACT_ID, data };
-
         // Create the auth call
         let builder = AuthTokenMintCallBuilder {
-            coin_attrs,
-            token_attrs,
+            coin_attrs: coin_attrs.clone(),
+            token_attrs: token_attrs.clone(),
             mint_keypair: mint_authority,
             auth_mint_zkbin: auth_mint_zkbin.clone(),
             auth_mint_pk: auth_mint_pk.clone(),
@@ -124,11 +112,23 @@ impl TestHarness {
         auth_debris.params.encode_async(&mut data).await?;
         let auth_call = ContractCall { contract_id: *MONEY_CONTRACT_ID, data };
 
+        // Create the minting call
+        let builder = TokenMintCallBuilder {
+            coin_attrs,
+            token_attrs,
+            mint_zkbin: token_mint_zkbin.clone(),
+            mint_pk: token_mint_pk.clone(),
+        };
+        let mint_debris = builder.build()?;
+        let mut data = vec![MoneyFunction::TokenMintV1 as u8];
+        mint_debris.params.encode_async(&mut data).await?;
+        let mint_call = ContractCall { contract_id: *MONEY_CONTRACT_ID, data };
+
         // Create the TransactionBuilder containing above calls
         let mut tx_builder = TransactionBuilder::new(
-            ContractCallLeaf { call: auth_call, proofs: auth_debris.proofs },
+            ContractCallLeaf { call: mint_call, proofs: mint_debris.proofs },
             vec![DarkTree::new(
-                ContractCallLeaf { call: mint_call, proofs: mint_debris.proofs },
+                ContractCallLeaf { call: auth_call, proofs: auth_debris.proofs },
                 vec![],
                 None,
                 None,
@@ -140,9 +140,9 @@ impl TestHarness {
         let mut fee_signature_secrets = None;
         if self.verify_fees {
             let mut tx = tx_builder.build()?;
-            let mint_sigs = tx.create_sigs(&[])?;
             let auth_sigs = tx.create_sigs(&[mint_authority.secret])?;
-            tx.signatures = vec![mint_sigs, auth_sigs];
+            let mint_sigs = tx.create_sigs(&[])?;
+            tx.signatures = vec![auth_sigs, mint_sigs];
 
             let (fee_call, fee_proofs, fee_secrets, _spent_fee_coins, fee_call_params) =
                 self.append_fee_call(holder, tx, block_height, &[]).await?;
@@ -155,9 +155,9 @@ impl TestHarness {
 
         // Now build the actual transaction and sign it with necessary keys.
         let mut tx = tx_builder.build()?;
-        let mint_sigs = tx.create_sigs(&[])?;
         let auth_sigs = tx.create_sigs(&[mint_authority.secret])?;
-        tx.signatures = vec![mint_sigs, auth_sigs];
+        let mint_sigs = tx.create_sigs(&[])?;
+        tx.signatures = vec![auth_sigs, mint_sigs];
         if let Some(fee_signature_secrets) = fee_signature_secrets {
             let sigs = tx.create_sigs(&fee_signature_secrets)?;
             tx.signatures.push(sigs);