From 3046602da7accac64dd6782cc056900202a4a045 Mon Sep 17 00:00:00 2001 From: Murisi Tarusenga Date: Fri, 28 Jun 2024 17:45:17 +0200 Subject: [PATCH 1/3] MASP section hash is now based on Transaction bytes. --- crates/tx/src/types.rs | 2 +- wasm/checksums.json | 55 +++++++++++++++++++++--------------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/crates/tx/src/types.rs b/crates/tx/src/types.rs index 3156b584d5..221f81acb4 100644 --- a/crates/tx/src/types.rs +++ b/crates/tx/src/types.rs @@ -802,7 +802,7 @@ impl Section { Self::Authorization(signature) => signature.hash(hasher), Self::MaspBuilder(mb) => mb.hash(hasher), Self::MaspTx(tx) => { - hasher.update(tx.txid().as_ref()); + hasher.update(tx.serialize_to_vec()); hasher } Self::Header(header) => header.hash(hasher), diff --git a/wasm/checksums.json b/wasm/checksums.json index ae9470452a..615281fee8 100644 --- a/wasm/checksums.json +++ b/wasm/checksums.json @@ -1,29 +1,30 @@ { - "tx_become_validator.wasm": "tx_become_validator.9499cd491944c2a1eca4544d529c2ac133b504c4ac10b121d30d092b13fb486f.wasm", - "tx_bond.wasm": "tx_bond.732f85dd8ef096a1e358296287f2457f362663e0f23f68333fa209d8d97ae494.wasm", - "tx_bridge_pool.wasm": "tx_bridge_pool.e940f9695742c8967811115bc28f9eb4458d500a8522657174997d4c9d946198.wasm", - "tx_change_consensus_key.wasm": "tx_change_consensus_key.b5bf392325e332f19901eb9237cb32766cbee0e64c9cc47d65dd62882f716337.wasm", - "tx_change_validator_commission.wasm": "tx_change_validator_commission.243491629bc622b6ca7a0e9179b635f1d269b58a3597121fab2e3d135749b016.wasm", - "tx_change_validator_metadata.wasm": "tx_change_validator_metadata.0e7d7586c52c29c51256020058a93665203af00da985371a9df1702f208d9cd1.wasm", - "tx_claim_rewards.wasm": "tx_claim_rewards.ae5b42e465e46c68c5f185321a34dceeffa8e56b0994ea680b6cb70a8880aed5.wasm", - "tx_deactivate_validator.wasm": "tx_deactivate_validator.fe46741d2440a2a5b9e01ece72a0938e6e1727ce97a2753951aac101fc667d88.wasm", - "tx_ibc.wasm": "tx_ibc.c630d1b1ca76a6c877d0d0fb7c91ebeed99f263cec1c9cd37ce9fce1513ba347.wasm", - "tx_init_account.wasm": "tx_init_account.afe7d7be1b9781f5fa66855b8195bc2bea897c9ee20d729d0728145716da5305.wasm", - "tx_init_proposal.wasm": "tx_init_proposal.10bfbf2c4d87db3d1ae3c319f140db85f1ae44ea54c92aa3ece4b9bf8dbdefa9.wasm", - "tx_reactivate_validator.wasm": "tx_reactivate_validator.a71e116b6ab4ac37940dd32361e922ed43e8d0b9bb9129f4fd0e89dae8f3c902.wasm", - "tx_redelegate.wasm": "tx_redelegate.bcd3f7332de530df6407a6d6cb77046d43904609bea467065028c45f50e775ef.wasm", - "tx_resign_steward.wasm": "tx_resign_steward.d60c11ddfef084421ed46444f1c0165125e29ac2305cc8d82b6cbc579a03803d.wasm", - "tx_reveal_pk.wasm": "tx_reveal_pk.9cc8f2fec826ddf606b1f0244ce485f63a8928adf18174f55a656db6423cbf73.wasm", - "tx_shielded_transfer.wasm": "tx_shielded_transfer.f77c6fc4bcf121977e00a4ebc24b5c60fbb1a182062b51ec96df21701cee5ffc.wasm", - "tx_shielding_transfer.wasm": "tx_shielding_transfer.0bcb41dbc0bceafccc65bc5262510a40ee0c0450c0c08303b676b37de6514647.wasm", - "tx_transparent_transfer.wasm": "tx_transparent_transfer.cf989d4d445340e7c98e446f414a4c7c9d353638a34b40198b2b63a05fbbf69a.wasm", - "tx_unbond.wasm": "tx_unbond.16130834f9cbb300e50ce5405b3531618c733156430fa6788a086fb303eb61c9.wasm", - "tx_unjail_validator.wasm": "tx_unjail_validator.088c40fa51dc5cbab896e5464daa71bb205e999ba2aa24e8775a63c2ebf10084.wasm", - "tx_unshielding_transfer.wasm": "tx_unshielding_transfer.7a7e3e048ff135642a6694437cc6e0a4c7c6943e9c149a4f81363f063fe28dbf.wasm", - "tx_update_account.wasm": "tx_update_account.bcd9bc9f531015719cfe1009af1c5bcfd6567cea55f4cec053bd45c96a2143df.wasm", - "tx_update_steward_commission.wasm": "tx_update_steward_commission.ee6739d438764e8aaaa91cace5ca7fa70b06a4cd4d021f3764fa28d94bd0d202.wasm", - "tx_vote_proposal.wasm": "tx_vote_proposal.45cda2bb54d6101206297be8e97a437806a24fb47fe7f91b61f05dbe8314bb53.wasm", - "tx_withdraw.wasm": "tx_withdraw.bca31434c620eee98fc1394a9dfb071033e87830152f0585ec82cc9c68ad310a.wasm", - "vp_implicit.wasm": "vp_implicit.1d6894f6b285a65561cf5ff0599deb5b5e7ac6227a074bf571cba28f63b4813f.wasm", - "vp_user.wasm": "vp_user.3e7c5f520227e3b49b8a66218ef9ce18976400d70a6b36e0878638ed9912e147.wasm" + "tx_become_validator.wasm": "tx_become_validator.8b478f55a70a4513898d95a7ab66585f9a86b95e2b4824782e61c80a7f79c706.wasm", + "tx_bond.wasm": "tx_bond.ea935db08694f240f3329ff828395ac615ad87f3358dfce156e71ac567ad42f2.wasm", + "tx_bridge_pool.wasm": "tx_bridge_pool.c84d0b2ccf48d3e587dca788b0b159c1cab7eba5990734cd48ae9ace07112922.wasm", + "tx_change_consensus_key.wasm": "tx_change_consensus_key.235b3f0f7a459f65341a62ae77d90b665a7498f8e4c76c8102ab6184d511b834.wasm", + "tx_change_validator_commission.wasm": "tx_change_validator_commission.9195035b42fa34dd96793afd49725dbbcb1c4f927eaba3212251496c20401fc5.wasm", + "tx_change_validator_metadata.wasm": "tx_change_validator_metadata.7ba5938bb666259f3d8fcdccd4f31a0604360ec1f6b3ca51e6bb9be137b3ad17.wasm", + "tx_claim_rewards.wasm": "tx_claim_rewards.c0dae92db5367c6bcba112e95eb0e8d0a6d248d9b2901cf1129e97ab655c957c.wasm", + "tx_deactivate_validator.wasm": "tx_deactivate_validator.b03231cb1fcf7b02279292edf93eec47c8feda5e043e9cecf4d74e5ef08c89cb.wasm", + "tx_ibc.wasm": "tx_ibc.3b59d6295e506a6f62d5fda3af07e9e169fcff3267eb170a1304f8403a8c7c64.wasm", + "tx_init_account.wasm": "tx_init_account.e03f75e7be38ff2a709108b8f8cb66a86d9abb16f2dc7b26a4a3285b9789f319.wasm", + "tx_init_proposal.wasm": "tx_init_proposal.1cd06e990777757900fcf5ee8ab306421cc9fd34526a3990de7f468a05a8994a.wasm", + "tx_reactivate_validator.wasm": "tx_reactivate_validator.308b8189d365fd8ca03072e280d37fe92e179f1254978aab20009caf9901dece.wasm", + "tx_redelegate.wasm": "tx_redelegate.cddf6e9b64df8ae39b0b087d4b6d84448e46077860c2f3ed5d8b1de860f9d5ab.wasm", + "tx_resign_steward.wasm": "tx_resign_steward.be37dc2ad480fce55b76a115704d3c240a8c6db37f75b81577c3976578da818b.wasm", + "tx_reveal_pk.wasm": "tx_reveal_pk.48d1aa82ef612a820cfe589fae0bae4d34c9ed98f282a8fe2aefbeff6f3fe533.wasm", + "tx_shielded_transfer.wasm": "tx_shielded_transfer.3f92e650c7f5800211d1af877d230fe34d055cf73a6ab80291d49f64a94e0852.wasm", + "tx_shielding_transfer.wasm": "tx_shielding_transfer.a1c07ccf830489d99fc816e293d77b5beb565b0b5bafb7e6b2c4df9f54602ffd.wasm", + "tx_transfer.wasm": "tx_transfer.98b685f3785001f0bd891c8d33b883da35edbb2298aba438de52478be9a907f1.wasm", + "tx_transparent_transfer.wasm": "tx_transparent_transfer.80707873f3f6168b42ac026d12cd9a282e54c1d86560a412309f8af3ea33aed6.wasm", + "tx_unbond.wasm": "tx_unbond.8fea5c9f27b383d7ea8c8adb279e81398a097c58b7ac935bff184ecb76ec07c0.wasm", + "tx_unjail_validator.wasm": "tx_unjail_validator.7dd675d4b9b04dcce279ba8641678f4201856a1e3aaadf8bfe0e47ad4cbbdffa.wasm", + "tx_unshielding_transfer.wasm": "tx_unshielding_transfer.7245bde191dec1d7c17ead60126067cb956ddcc4c86299fc1a582e28949a8884.wasm", + "tx_update_account.wasm": "tx_update_account.3da87050456f8bdaab621b4284834e116117cbf8c765d4eb7278f20a9e25fc99.wasm", + "tx_update_steward_commission.wasm": "tx_update_steward_commission.54ba8f4aac57ae670931686201475cfe978ad90df76a24a306d10378a38f4e5e.wasm", + "tx_vote_proposal.wasm": "tx_vote_proposal.6465948984980857130318b130798d60d2f70c2841612f6b82aa72c02451fa86.wasm", + "tx_withdraw.wasm": "tx_withdraw.c907e047e303a35989a5070f14c0ee8db38dbaa5649b3e37161b3c8be3604438.wasm", + "vp_implicit.wasm": "vp_implicit.d056f22e5cafeab19f44bdd8076885a96f06c2240cc9c440a70edeb87e1265b5.wasm", + "vp_user.wasm": "vp_user.a2ca2e93f35a5f104b869a02283127459bc3b51bcf0c67ee362f3d5291371e2f.wasm" } \ No newline at end of file From 50d92466d9b81d000009672acb8bf7e3679be850 Mon Sep 17 00:00:00 2001 From: Murisi Tarusenga Date: Fri, 28 Jun 2024 20:36:29 +0200 Subject: [PATCH 2/3] Use txids to reference MASP Transactions from Transfers and MaspBuilders. --- Cargo.lock | 6 +- Cargo.toml | 4 +- crates/core/src/masp.rs | 61 +++++++++++++++++++- crates/light_sdk/src/transaction/transfer.rs | 7 ++- crates/namada/src/ledger/native_vp/masp.rs | 4 +- crates/node/src/bench_utils.rs | 20 +++---- crates/sdk/src/masp.rs | 15 ++--- crates/sdk/src/tx.rs | 13 +++-- crates/token/src/lib.rs | 8 +-- crates/tx/src/action.rs | 6 +- crates/tx/src/types.rs | 23 ++++++-- wasm/Cargo.lock | 6 +- wasm/checksums.json | 54 ++++++++--------- wasm/tx_ibc/src/lib.rs | 4 +- wasm/tx_shielded_transfer/src/lib.rs | 4 +- wasm/tx_shielding_transfer/src/lib.rs | 4 +- wasm/tx_unshielding_transfer/src/lib.rs | 4 +- wasm_for_tests/Cargo.lock | 6 +- 18 files changed, 155 insertions(+), 94 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c5f83204df..59b38ebc95 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4448,7 +4448,7 @@ dependencies = [ [[package]] name = "masp_note_encryption" version = "1.0.0" -source = "git+https://github.com/anoma/masp?rev=4ede1c42d76d6348af8224bc8bfac4404321fe82#4ede1c42d76d6348af8224bc8bfac4404321fe82" +source = "git+https://github.com/anoma/masp?rev=8d83b172698098fba393006016072bc201ed9ab7#8d83b172698098fba393006016072bc201ed9ab7" dependencies = [ "borsh 1.2.1", "chacha20", @@ -4461,7 +4461,7 @@ dependencies = [ [[package]] name = "masp_primitives" version = "1.0.0" -source = "git+https://github.com/anoma/masp?rev=4ede1c42d76d6348af8224bc8bfac4404321fe82#4ede1c42d76d6348af8224bc8bfac4404321fe82" +source = "git+https://github.com/anoma/masp?rev=8d83b172698098fba393006016072bc201ed9ab7#8d83b172698098fba393006016072bc201ed9ab7" dependencies = [ "aes", "bip0039", @@ -4493,7 +4493,7 @@ dependencies = [ [[package]] name = "masp_proofs" version = "1.0.0" -source = "git+https://github.com/anoma/masp?rev=4ede1c42d76d6348af8224bc8bfac4404321fe82#4ede1c42d76d6348af8224bc8bfac4404321fe82" +source = "git+https://github.com/anoma/masp?rev=8d83b172698098fba393006016072bc201ed9ab7#8d83b172698098fba393006016072bc201ed9ab7" dependencies = [ "bellman", "blake2b_simd", diff --git a/Cargo.toml b/Cargo.toml index 6821a58e31..9b3adf8278 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -125,8 +125,8 @@ libc = "0.2.97" libloading = "0.7.2" linkme = "0.3.24" # branch = "main" -masp_primitives = { git = "https://github.com/anoma/masp", rev = "4ede1c42d76d6348af8224bc8bfac4404321fe82" } -masp_proofs = { git = "https://github.com/anoma/masp", rev = "4ede1c42d76d6348af8224bc8bfac4404321fe82", default-features = false, features = ["local-prover"] } +masp_primitives = { git = "https://github.com/anoma/masp", rev = "8d83b172698098fba393006016072bc201ed9ab7" } +masp_proofs = { git = "https://github.com/anoma/masp", rev = "8d83b172698098fba393006016072bc201ed9ab7", default-features = false, features = ["local-prover"] } num256 = "0.3.5" num_cpus = "1.13.0" num-derive = "0.4" diff --git a/crates/core/src/masp.rs b/crates/core/src/masp.rs index c31734a803..efb095b736 100644 --- a/crates/core/src/masp.rs +++ b/crates/core/src/masp.rs @@ -11,11 +11,10 @@ use masp_primitives::asset_type::AssetType; use namada_macros::BorshDeserializer; #[cfg(feature = "migrations")] use namada_migrations::*; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; use sha2::{Digest, Sha256}; use crate::address::{Address, DecodeError, HASH_HEX_LEN, MASP}; -use crate::hash::Hash; use crate::impl_display_and_from_str_via_format; use crate::storage::Epoch; use crate::string_encoding::{ @@ -24,6 +23,62 @@ use crate::string_encoding::{ }; use crate::token::{Denomination, MaspDigitPos}; +/// Serialize the given TxId +pub fn serialize_txid( + txid: &masp_primitives::transaction::TxId, + s: S, +) -> Result +where + S: Serializer, +{ + s.serialize_bytes(txid.as_ref()) +} + +/// Deserialize the given TxId +pub fn deserialize_txid<'de, D>( + deserializer: D, +) -> Result +where + D: Deserializer<'de>, +{ + Ok(masp_primitives::transaction::TxId::from_bytes( + Deserialize::deserialize(deserializer)?, + )) +} + +/// Wrapper for masp_primitive's TxId +#[derive( + Serialize, + Deserialize, + Clone, + BorshSerialize, + BorshDeserialize, + BorshSchema, + Debug, + Eq, + PartialEq, + Copy, + Ord, + PartialOrd, + Hash, +)] +pub struct MaspTxId( + #[serde( + serialize_with = "serialize_txid", + deserialize_with = "deserialize_txid" + )] + masp_primitives::transaction::TxId, +); + +impl From for MaspTxId { + fn from(txid: masp_primitives::transaction::TxId) -> Self { + Self(txid) + } +} + +/// Wrapper for masp_primitive's TxId +pub type TxId = MaspTxId; + /// Wrapper type around `Epoch` for type safe operations involving the masp /// epoch #[derive( @@ -603,7 +658,7 @@ impl FromStr for MaspValue { /// The masp transactions' references of a given batch #[derive(Default, Clone, Serialize, Deserialize)] -pub struct MaspTxRefs(pub Vec); +pub struct MaspTxRefs(pub Vec); impl Display for MaspTxRefs { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { diff --git a/crates/light_sdk/src/transaction/transfer.rs b/crates/light_sdk/src/transaction/transfer.rs index 84475660ea..2d8b5fc147 100644 --- a/crates/light_sdk/src/transaction/transfer.rs +++ b/crates/light_sdk/src/transaction/transfer.rs @@ -1,6 +1,7 @@ use namada_sdk::address::Address; use namada_sdk::hash::Hash; use namada_sdk::key::common; +use namada_sdk::masp::TxId; use namada_sdk::token::DenominatedAmount; use namada_sdk::tx::data::GasLimit; use namada_sdk::tx::{ @@ -40,7 +41,7 @@ impl Transfer { } /// Build a shielded transfer transaction from the given parameters - pub fn shielded(shielded_section_hash: Hash, args: GlobalArgs) -> Self { + pub fn shielded(shielded_section_hash: TxId, args: GlobalArgs) -> Self { let data = namada_sdk::token::ShieldedTransfer { section_hash: shielded_section_hash, }; @@ -57,7 +58,7 @@ impl Transfer { source: Address, token: Address, amount: DenominatedAmount, - shielded_section_hash: Hash, + shielded_section_hash: TxId, args: GlobalArgs, ) -> Self { let data = namada_sdk::token::ShieldingTransfer { @@ -79,7 +80,7 @@ impl Transfer { target: Address, token: Address, amount: DenominatedAmount, - shielded_section_hash: Hash, + shielded_section_hash: TxId, args: GlobalArgs, ) -> Self { let data = namada_sdk::token::UnshieldingTransfer { diff --git a/crates/namada/src/ledger/native_vp/masp.rs b/crates/namada/src/ledger/native_vp/masp.rs index 51e86f018b..61457c6b5c 100644 --- a/crates/namada/src/ledger/native_vp/masp.rs +++ b/crates/namada/src/ledger/native_vp/masp.rs @@ -371,8 +371,8 @@ where })?; let shielded_tx = tx_data .tx - .get_section(&masp_section_ref) - .and_then(|section| section.masp_tx()) + .get_masp_section(&masp_section_ref) + .cloned() .ok_or_else(|| { native_vp::Error::new_const( "Missing MASP section in transaction", diff --git a/crates/node/src/bench_utils.rs b/crates/node/src/bench_utils.rs index ef80036715..2fb2989712 100644 --- a/crates/node/src/bench_utils.rs +++ b/crates/node/src/bench_utils.rs @@ -114,7 +114,6 @@ use namada_sdk::wallet::Wallet; use namada_sdk::{Namada, NamadaImpl}; use namada_test_utils::tx_data::TxWriteData; use rand_core::OsRng; -use sha2::{Digest, Sha256}; use tempfile::TempDir; use crate::config; @@ -936,8 +935,10 @@ impl Client for BenchShell { tx.sections .iter() .find_map(|section| { - if let Section::MaspTx(_) = section { - Some(section.get_hash()) + if let Section::MaspTx(transaction) = + section + { + Some(transaction.txid().into()) } else { None } @@ -1102,13 +1103,7 @@ impl BenchShieldedCtx { ) .expect("MASP must have shielded part"); - let mut hasher = Sha256::new(); - let shielded_section_hash = namada::core::hash::Hash( - Section::MaspTx(shielded.clone()) - .hash(&mut hasher) - .finalize_reset() - .into(), - ); + let shielded_section_hash = shielded.txid().into(); let tx = if source.effective_address() == MASP && target.effective_address() == MASP { @@ -1212,10 +1207,9 @@ impl BenchShieldedCtx { .unwrap(); let masp_tx = tx .tx - .get_section(&transfer.shielded_section_hash) + .get_masp_section(&transfer.shielded_section_hash) .unwrap() - .masp_tx() - .unwrap(); + .clone(); let msg = MsgTransfer { message: msg, transfer: Some(transfer), diff --git a/crates/sdk/src/masp.rs b/crates/sdk/src/masp.rs index 516a7ad611..5f7d87ee02 100644 --- a/crates/sdk/src/masp.rs +++ b/crates/sdk/src/masp.rs @@ -54,7 +54,7 @@ use namada_core::collections::{HashMap, HashSet}; use namada_core::dec::Dec; pub use namada_core::masp::{ encode_asset_type, AssetData, BalanceOwner, ExtendedViewingKey, - PaymentAddress, TransferSource, TransferTarget, + PaymentAddress, TransferSource, TransferTarget, TxId, }; use namada_core::masp::{MaspEpoch, MaspTxRefs}; use namada_core::storage::{BlockHeight, TxIndex}; @@ -851,14 +851,11 @@ impl ShieldedContext { .0 .iter() .try_fold(vec![], |mut acc, hash| { - match tx - .get_section(hash) - .and_then(|section| section.masp_tx()) - .ok_or_else(|| { - Error::Other( - "Missing expected masp transaction".to_string(), - ) - }) { + match tx.get_masp_section(hash).cloned().ok_or_else(|| { + Error::Other( + "Missing expected masp transaction".to_string(), + ) + }) { Ok(transaction) => { acc.push(transaction); Ok(acc) diff --git a/crates/sdk/src/tx.rs b/crates/sdk/src/tx.rs index b9daabeca2..7d66a6fe82 100644 --- a/crates/sdk/src/tx.rs +++ b/crates/sdk/src/tx.rs @@ -17,7 +17,9 @@ use masp_primitives::transaction::components::transparent::fees::{ InputView as TransparentInputView, OutputView as TransparentOutputView, }; use masp_primitives::transaction::components::I128Sum; -use masp_primitives::transaction::{builder, Transaction as MaspTransaction}; +use masp_primitives::transaction::{ + builder, Transaction as MaspTransaction, TxId, +}; use masp_primitives::zip32::ExtendedFullViewingKey; use namada_account::{InitAccount, UpdateAccount}; use namada_core::address::{Address, InternalAddress, MASP}; @@ -2970,7 +2972,7 @@ pub async fn build_shielded_transfer( // Construct the tx data with a placeholder shielded section hash let data = token::ShieldedTransfer { - section_hash: Hash::zero(), + section_hash: TxId::from_bytes(Default::default()).into(), }; let tx = build_pow_flag( context, @@ -3081,7 +3083,7 @@ pub async fn build_shielding_transfer( source: source.clone(), token: args.token.clone(), amount: validated_amount, - shielded_section_hash: Hash::zero(), + shielded_section_hash: TxId::from_bytes(Default::default()).into(), }; let tx = build_pow_flag( @@ -3167,7 +3169,7 @@ pub async fn build_unshielding_transfer( target: args.target.clone(), token: args.token.clone(), amount: validated_amount, - shielded_section_hash: Hash::zero(), + shielded_section_hash: TxId::from_bytes(Default::default()).into(), }; let tx = build_pow_flag( context, @@ -3539,8 +3541,7 @@ pub async fn gen_ibc_shielding_transfer( .map_err(|err| TxSubmitError::MaspError(err.to_string()))?; if let Some(shielded_transfer) = shielded_transfer { - let masp_tx_hash = - Section::MaspTx(shielded_transfer.masp_tx.clone()).get_hash(); + let masp_tx_hash = shielded_transfer.masp_tx.txid().into(); let transfer = token::ShieldingTransfer { source: source.clone(), token: token.clone(), diff --git a/crates/token/src/lib.rs b/crates/token/src/lib.rs index c14d93f430..d913cc005c 100644 --- a/crates/token/src/lib.rs +++ b/crates/token/src/lib.rs @@ -19,7 +19,6 @@ )] use namada_core::borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; -use namada_core::hash::Hash; use namada_macros::BorshDeserializer; pub use namada_shielded_token::*; pub use namada_trans_token::*; @@ -32,6 +31,7 @@ pub mod storage_key { } use namada_core::address::Address; +use namada_core::masp::TxId; use namada_events::EmitEvents; use namada_storage::{Result, StorageRead, StorageWrite}; @@ -110,7 +110,7 @@ pub struct TransparentTransfer { )] pub struct ShieldedTransfer { /// Hash of tx section that contains the MASP transaction - pub section_hash: Hash, + pub section_hash: TxId, } /// Arguments for a shielding transfer (from a transparent token to a shielded @@ -137,7 +137,7 @@ pub struct ShieldingTransfer { /// The amount of tokens pub amount: DenominatedAmount, /// Hash of tx section that contains the MASP transaction - pub shielded_section_hash: Hash, + pub shielded_section_hash: TxId, } /// Arguments for an unshielding transfer (from a shielded token to a @@ -164,7 +164,7 @@ pub struct UnshieldingTransfer { /// The amount of tokens pub amount: DenominatedAmount, /// Hash of tx section that contains the MASP transaction - pub shielded_section_hash: Hash, + pub shielded_section_hash: TxId, } #[cfg(any(test, feature = "testing"))] diff --git a/crates/tx/src/action.rs b/crates/tx/src/action.rs index 6748e181aa..f8826b47c6 100644 --- a/crates/tx/src/action.rs +++ b/crates/tx/src/action.rs @@ -8,7 +8,7 @@ use std::fmt; use namada_core::address::Address; use namada_core::borsh::{BorshDeserialize, BorshSerialize}; -use namada_core::hash::Hash; +use namada_core::masp::TxId; use namada_core::storage::KeySeg; use namada_core::{address, storage}; @@ -67,7 +67,7 @@ pub enum PgfAction { #[derive(Clone, Debug, BorshDeserialize, BorshSerialize)] pub struct MaspAction { /// The hash of the masp [`crate::types::Section`] - pub masp_section_ref: Hash, + pub masp_section_ref: TxId, } /// Read actions from temporary storage @@ -123,7 +123,7 @@ fn storage_key() -> storage::Key { /// first one pub fn get_masp_section_ref( reader: &T, -) -> Result, ::Err> { +) -> Result, ::Err> { Ok(reader.read_actions()?.into_iter().find_map(|action| { // In case of multiple masp actions we get the first one if let Action::Masp(MaspAction { masp_section_ref }) = action { diff --git a/crates/tx/src/types.rs b/crates/tx/src/types.rs index 221f81acb4..14b94c87a6 100644 --- a/crates/tx/src/types.rs +++ b/crates/tx/src/types.rs @@ -18,7 +18,7 @@ use namada_core::borsh::{ use namada_core::chain::ChainId; use namada_core::collections::{HashMap, HashSet}; use namada_core::key::*; -use namada_core::masp::AssetData; +use namada_core::masp::{AssetData, TxId}; use namada_core::sign::SignatureIndex; use namada_core::storage::{BlockHeight, TxIndex}; use namada_core::time::DateTimeUtc; @@ -719,7 +719,7 @@ impl From for Vec { )] pub struct MaspBuilder { /// The MASP transaction that this section witnesses - pub target: namada_core::hash::Hash, + pub target: TxId, /// The decoded set of asset types used by the transaction. Useful for /// offline wallets trying to display AssetTypes. pub asset_types: HashSet, @@ -1185,6 +1185,18 @@ impl Tx { None } + /// Get the transaction section with the given hash + pub fn get_masp_section(&self, hash: &TxId) -> Option<&Transaction> { + for section in &self.sections { + if let Section::MaspTx(masp) = section { + if TxId::from(masp.txid()) == *hash { + return Some(masp); + } + } + } + None + } + /// Set the last transaction memo hash stored in the header pub fn set_memo_sechash(&mut self, hash: namada_core::hash::Hash) { let item = match self.header.batch.pop() { @@ -1553,9 +1565,10 @@ impl Tx { pub fn add_masp_tx_section( &mut self, tx: Transaction, - ) -> (&mut Self, namada_core::hash::Hash) { - let sechash = self.add_section(Section::MaspTx(tx)).get_hash(); - (self, sechash) + ) -> (&mut Self, TxId) { + let txid = tx.txid(); + self.add_section(Section::MaspTx(tx)); + (self, txid.into()) } /// Add a masp builder section to the tx builder diff --git a/wasm/Cargo.lock b/wasm/Cargo.lock index 7c491ea3d2..d5333f2109 100644 --- a/wasm/Cargo.lock +++ b/wasm/Cargo.lock @@ -3487,7 +3487,7 @@ dependencies = [ [[package]] name = "masp_note_encryption" version = "1.0.0" -source = "git+https://github.com/anoma/masp?rev=4ede1c42d76d6348af8224bc8bfac4404321fe82#4ede1c42d76d6348af8224bc8bfac4404321fe82" +source = "git+https://github.com/anoma/masp?rev=8d83b172698098fba393006016072bc201ed9ab7#8d83b172698098fba393006016072bc201ed9ab7" dependencies = [ "borsh 1.4.0", "chacha20", @@ -3500,7 +3500,7 @@ dependencies = [ [[package]] name = "masp_primitives" version = "1.0.0" -source = "git+https://github.com/anoma/masp?rev=4ede1c42d76d6348af8224bc8bfac4404321fe82#4ede1c42d76d6348af8224bc8bfac4404321fe82" +source = "git+https://github.com/anoma/masp?rev=8d83b172698098fba393006016072bc201ed9ab7#8d83b172698098fba393006016072bc201ed9ab7" dependencies = [ "aes", "bip0039", @@ -3532,7 +3532,7 @@ dependencies = [ [[package]] name = "masp_proofs" version = "1.0.0" -source = "git+https://github.com/anoma/masp?rev=4ede1c42d76d6348af8224bc8bfac4404321fe82#4ede1c42d76d6348af8224bc8bfac4404321fe82" +source = "git+https://github.com/anoma/masp?rev=8d83b172698098fba393006016072bc201ed9ab7#8d83b172698098fba393006016072bc201ed9ab7" dependencies = [ "bellman", "blake2b_simd", diff --git a/wasm/checksums.json b/wasm/checksums.json index 615281fee8..d5150da4b8 100644 --- a/wasm/checksums.json +++ b/wasm/checksums.json @@ -1,30 +1,30 @@ { - "tx_become_validator.wasm": "tx_become_validator.8b478f55a70a4513898d95a7ab66585f9a86b95e2b4824782e61c80a7f79c706.wasm", - "tx_bond.wasm": "tx_bond.ea935db08694f240f3329ff828395ac615ad87f3358dfce156e71ac567ad42f2.wasm", - "tx_bridge_pool.wasm": "tx_bridge_pool.c84d0b2ccf48d3e587dca788b0b159c1cab7eba5990734cd48ae9ace07112922.wasm", - "tx_change_consensus_key.wasm": "tx_change_consensus_key.235b3f0f7a459f65341a62ae77d90b665a7498f8e4c76c8102ab6184d511b834.wasm", - "tx_change_validator_commission.wasm": "tx_change_validator_commission.9195035b42fa34dd96793afd49725dbbcb1c4f927eaba3212251496c20401fc5.wasm", - "tx_change_validator_metadata.wasm": "tx_change_validator_metadata.7ba5938bb666259f3d8fcdccd4f31a0604360ec1f6b3ca51e6bb9be137b3ad17.wasm", - "tx_claim_rewards.wasm": "tx_claim_rewards.c0dae92db5367c6bcba112e95eb0e8d0a6d248d9b2901cf1129e97ab655c957c.wasm", - "tx_deactivate_validator.wasm": "tx_deactivate_validator.b03231cb1fcf7b02279292edf93eec47c8feda5e043e9cecf4d74e5ef08c89cb.wasm", - "tx_ibc.wasm": "tx_ibc.3b59d6295e506a6f62d5fda3af07e9e169fcff3267eb170a1304f8403a8c7c64.wasm", - "tx_init_account.wasm": "tx_init_account.e03f75e7be38ff2a709108b8f8cb66a86d9abb16f2dc7b26a4a3285b9789f319.wasm", - "tx_init_proposal.wasm": "tx_init_proposal.1cd06e990777757900fcf5ee8ab306421cc9fd34526a3990de7f468a05a8994a.wasm", - "tx_reactivate_validator.wasm": "tx_reactivate_validator.308b8189d365fd8ca03072e280d37fe92e179f1254978aab20009caf9901dece.wasm", - "tx_redelegate.wasm": "tx_redelegate.cddf6e9b64df8ae39b0b087d4b6d84448e46077860c2f3ed5d8b1de860f9d5ab.wasm", - "tx_resign_steward.wasm": "tx_resign_steward.be37dc2ad480fce55b76a115704d3c240a8c6db37f75b81577c3976578da818b.wasm", - "tx_reveal_pk.wasm": "tx_reveal_pk.48d1aa82ef612a820cfe589fae0bae4d34c9ed98f282a8fe2aefbeff6f3fe533.wasm", - "tx_shielded_transfer.wasm": "tx_shielded_transfer.3f92e650c7f5800211d1af877d230fe34d055cf73a6ab80291d49f64a94e0852.wasm", - "tx_shielding_transfer.wasm": "tx_shielding_transfer.a1c07ccf830489d99fc816e293d77b5beb565b0b5bafb7e6b2c4df9f54602ffd.wasm", + "tx_become_validator.wasm": "tx_become_validator.505bd779e20977423f95fb06545e2a52fa9d532e52cfd8e6047cdcfb89ff1696.wasm", + "tx_bond.wasm": "tx_bond.f22821dbb6afc8bd1cb0cfff2203cde6dab9c8fe3384dfad9bb52719bc23ab66.wasm", + "tx_bridge_pool.wasm": "tx_bridge_pool.3fe4f4fea78c390a2f3407a955f744c6d0ae622b02c2e828c1de6fb30836b1f6.wasm", + "tx_change_consensus_key.wasm": "tx_change_consensus_key.29d05852199f0182bb285b04446e6c93ef763d2fc09e8786867d57f372d7eb5e.wasm", + "tx_change_validator_commission.wasm": "tx_change_validator_commission.3d4722548214e3972d5539b5495e0580da04670f79100174d4090c0cef14434f.wasm", + "tx_change_validator_metadata.wasm": "tx_change_validator_metadata.35402afa5c46fd9b254aae2901ade07e1bb807c9fabc6162f9368cea925c50fd.wasm", + "tx_claim_rewards.wasm": "tx_claim_rewards.f0801cb55a2a46654ae7436fee21498facd1811c9aa2d40749d1e012830679d5.wasm", + "tx_deactivate_validator.wasm": "tx_deactivate_validator.400bede4da7da56057cd117c0ef64678b6ca2f350a61607c031221e7115b8de4.wasm", + "tx_ibc.wasm": "tx_ibc.011c17a90f7ee8483d9da78037ec25b7574d9b866492fd3418525074b2a32338.wasm", + "tx_init_account.wasm": "tx_init_account.25d68cf22e6083af2fe873590f377d7863027ab014b087e47c6dcbca4b7116d2.wasm", + "tx_init_proposal.wasm": "tx_init_proposal.d99ae3c79e298a97f660cd3fc7ae158dad7cfec3ae94db51ff786caef46f07fa.wasm", + "tx_reactivate_validator.wasm": "tx_reactivate_validator.b8610c845d9ce5ce33e02aa981332e7174a3a28c554e73a4d0ad968e159a25ed.wasm", + "tx_redelegate.wasm": "tx_redelegate.66f9a0d3209810d4124f5573d3fc6478c613b350cbb32cbe500cc1be860173df.wasm", + "tx_resign_steward.wasm": "tx_resign_steward.9c7db2c1c923dd07a3bcd63231d76da20d20ad7eb9f948fa0876422204f8d67e.wasm", + "tx_reveal_pk.wasm": "tx_reveal_pk.4ce7125a1a176c111499fc7788045b3fe4df809f7e5476040e02542dfaa8af47.wasm", + "tx_shielded_transfer.wasm": "tx_shielded_transfer.889487c1597ec39fa017b727f769ea9e94cdec1b455b880d1d5a6d6af84969af.wasm", + "tx_shielding_transfer.wasm": "tx_shielding_transfer.1a77af2f89ea3c9dfbf4c985c3548648901fa05cd2c61426a5ecd5d5f57ddd6d.wasm", "tx_transfer.wasm": "tx_transfer.98b685f3785001f0bd891c8d33b883da35edbb2298aba438de52478be9a907f1.wasm", - "tx_transparent_transfer.wasm": "tx_transparent_transfer.80707873f3f6168b42ac026d12cd9a282e54c1d86560a412309f8af3ea33aed6.wasm", - "tx_unbond.wasm": "tx_unbond.8fea5c9f27b383d7ea8c8adb279e81398a097c58b7ac935bff184ecb76ec07c0.wasm", - "tx_unjail_validator.wasm": "tx_unjail_validator.7dd675d4b9b04dcce279ba8641678f4201856a1e3aaadf8bfe0e47ad4cbbdffa.wasm", - "tx_unshielding_transfer.wasm": "tx_unshielding_transfer.7245bde191dec1d7c17ead60126067cb956ddcc4c86299fc1a582e28949a8884.wasm", - "tx_update_account.wasm": "tx_update_account.3da87050456f8bdaab621b4284834e116117cbf8c765d4eb7278f20a9e25fc99.wasm", - "tx_update_steward_commission.wasm": "tx_update_steward_commission.54ba8f4aac57ae670931686201475cfe978ad90df76a24a306d10378a38f4e5e.wasm", - "tx_vote_proposal.wasm": "tx_vote_proposal.6465948984980857130318b130798d60d2f70c2841612f6b82aa72c02451fa86.wasm", - "tx_withdraw.wasm": "tx_withdraw.c907e047e303a35989a5070f14c0ee8db38dbaa5649b3e37161b3c8be3604438.wasm", - "vp_implicit.wasm": "vp_implicit.d056f22e5cafeab19f44bdd8076885a96f06c2240cc9c440a70edeb87e1265b5.wasm", - "vp_user.wasm": "vp_user.a2ca2e93f35a5f104b869a02283127459bc3b51bcf0c67ee362f3d5291371e2f.wasm" + "tx_transparent_transfer.wasm": "tx_transparent_transfer.077aa1b6ae41b9c84fdece71d29afe03244cd38a061ea7acf4f26ff282d4a083.wasm", + "tx_unbond.wasm": "tx_unbond.69f7debd1a306a539d62e61b27e1b63c9d90aac6a4d2fcb8096ae49a40c065ec.wasm", + "tx_unjail_validator.wasm": "tx_unjail_validator.665755ee4d155de8548c83d6cb9d8f74f6fbc82330fb00d439e103410354e74b.wasm", + "tx_unshielding_transfer.wasm": "tx_unshielding_transfer.74b1722d3ad18c1eebbc50e27e9ac0c3927981ffe533b2257dbf830a2ea065d1.wasm", + "tx_update_account.wasm": "tx_update_account.f3971d2b1ee2dbc0d573f9894ba30457253589cd5937af5ba75881dd5ec3890e.wasm", + "tx_update_steward_commission.wasm": "tx_update_steward_commission.721178219d68ee6b6b05bb4a238d5b4aad212d8c4a4088607c2a8b263274c679.wasm", + "tx_vote_proposal.wasm": "tx_vote_proposal.dfd275734a3c8863a03a60a27258135c085402ae4362b1f4ff9f5897c1ea25b3.wasm", + "tx_withdraw.wasm": "tx_withdraw.60870f2e291c13a6b8e1339c3d0e69412f6584d69eabcbe65becea34a2bb196a.wasm", + "vp_implicit.wasm": "vp_implicit.58b281cada5be0401aa5fc0c0bb861b3b4995bd20e6bb0a866d18407e87facbd.wasm", + "vp_user.wasm": "vp_user.763957a00d75fc81aedc2f2b305ed4c00916ccff44229662cdc9b8d275d852e1.wasm" } \ No newline at end of file diff --git a/wasm/tx_ibc/src/lib.rs b/wasm/tx_ibc/src/lib.rs index e131a90c9a..9c1c19bec2 100644 --- a/wasm/tx_ibc/src/lib.rs +++ b/wasm/tx_ibc/src/lib.rs @@ -17,8 +17,8 @@ fn apply_tx(ctx: &mut Ctx, tx_data: BatchedTx) -> TxResult { { let shielded = tx_data .tx - .get_section(&masp_section_ref) - .and_then(|x| x.as_ref().masp_tx()) + .get_masp_section(&masp_section_ref) + .cloned() .ok_or_err_msg( "Unable to find required shielded section in tx data", ) diff --git a/wasm/tx_shielded_transfer/src/lib.rs b/wasm/tx_shielded_transfer/src/lib.rs index cc9e70a638..0b271cf9be 100644 --- a/wasm/tx_shielded_transfer/src/lib.rs +++ b/wasm/tx_shielded_transfer/src/lib.rs @@ -13,8 +13,8 @@ fn apply_tx(ctx: &mut Ctx, tx_data: BatchedTx) -> TxResult { let masp_section_ref = transfer.section_hash; let shielded = tx_data .tx - .get_section(&masp_section_ref) - .and_then(|x| x.as_ref().masp_tx()) + .get_masp_section(&masp_section_ref) + .cloned() .ok_or_err_msg("Unable to find required shielded section in tx data") .map_err(|err| { ctx.set_commitment_sentinel(); diff --git a/wasm/tx_shielding_transfer/src/lib.rs b/wasm/tx_shielding_transfer/src/lib.rs index 389942686b..9aee4b1f56 100644 --- a/wasm/tx_shielding_transfer/src/lib.rs +++ b/wasm/tx_shielding_transfer/src/lib.rs @@ -22,8 +22,8 @@ fn apply_tx(ctx: &mut Ctx, tx_data: BatchedTx) -> TxResult { let masp_section_ref = transfer.shielded_section_hash; let shielded = tx_data .tx - .get_section(&masp_section_ref) - .and_then(|x| x.as_ref().masp_tx()) + .get_masp_section(&masp_section_ref) + .cloned() .ok_or_err_msg("Unable to find required shielded section in tx data") .map_err(|err| { ctx.set_commitment_sentinel(); diff --git a/wasm/tx_unshielding_transfer/src/lib.rs b/wasm/tx_unshielding_transfer/src/lib.rs index 79bdac0757..f02663c11b 100644 --- a/wasm/tx_unshielding_transfer/src/lib.rs +++ b/wasm/tx_unshielding_transfer/src/lib.rs @@ -22,8 +22,8 @@ fn apply_tx(ctx: &mut Ctx, tx_data: BatchedTx) -> TxResult { let masp_section_ref = transfer.shielded_section_hash; let shielded = tx_data .tx - .get_section(&masp_section_ref) - .and_then(|x| x.as_ref().masp_tx()) + .get_masp_section(&masp_section_ref) + .cloned() .ok_or_err_msg("Unable to find required shielded section in tx data") .map_err(|err| { ctx.set_commitment_sentinel(); diff --git a/wasm_for_tests/Cargo.lock b/wasm_for_tests/Cargo.lock index 44008f9c09..7fddb77211 100644 --- a/wasm_for_tests/Cargo.lock +++ b/wasm_for_tests/Cargo.lock @@ -3568,7 +3568,7 @@ dependencies = [ [[package]] name = "masp_note_encryption" version = "1.0.0" -source = "git+https://github.com/anoma/masp?rev=4ede1c42d76d6348af8224bc8bfac4404321fe82#4ede1c42d76d6348af8224bc8bfac4404321fe82" +source = "git+https://github.com/anoma/masp?rev=8d83b172698098fba393006016072bc201ed9ab7#8d83b172698098fba393006016072bc201ed9ab7" dependencies = [ "borsh 1.2.1", "chacha20", @@ -3581,7 +3581,7 @@ dependencies = [ [[package]] name = "masp_primitives" version = "1.0.0" -source = "git+https://github.com/anoma/masp?rev=4ede1c42d76d6348af8224bc8bfac4404321fe82#4ede1c42d76d6348af8224bc8bfac4404321fe82" +source = "git+https://github.com/anoma/masp?rev=8d83b172698098fba393006016072bc201ed9ab7#8d83b172698098fba393006016072bc201ed9ab7" dependencies = [ "aes", "bip0039", @@ -3613,7 +3613,7 @@ dependencies = [ [[package]] name = "masp_proofs" version = "1.0.0" -source = "git+https://github.com/anoma/masp?rev=4ede1c42d76d6348af8224bc8bfac4404321fe82#4ede1c42d76d6348af8224bc8bfac4404321fe82" +source = "git+https://github.com/anoma/masp?rev=8d83b172698098fba393006016072bc201ed9ab7#8d83b172698098fba393006016072bc201ed9ab7" dependencies = [ "bellman", "blake2b_simd", From c7a45249c232006889239bdaaa146d92d96cb32e Mon Sep 17 00:00:00 2001 From: Murisi Tarusenga Date: Fri, 28 Jun 2024 22:12:16 +0200 Subject: [PATCH 3/3] Added changelog entry. --- .../bug-fixes/3463-non-malleable-masp-witness-data2.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changelog/unreleased/bug-fixes/3463-non-malleable-masp-witness-data2.md diff --git a/.changelog/unreleased/bug-fixes/3463-non-malleable-masp-witness-data2.md b/.changelog/unreleased/bug-fixes/3463-non-malleable-masp-witness-data2.md new file mode 100644 index 0000000000..e206354cba --- /dev/null +++ b/.changelog/unreleased/bug-fixes/3463-non-malleable-masp-witness-data2.md @@ -0,0 +1,2 @@ +- Fixed MASP witness data malleability in the Tx + ([\#3463](https://github.com/anoma/namada/pull/3463)) \ No newline at end of file