From 0dcd31a773179dbd3b52eb8b1b83af2f4c3abc61 Mon Sep 17 00:00:00 2001 From: timofeevmd Date: Tue, 5 Mar 2024 11:39:59 +0400 Subject: [PATCH 1/2] [refactor] #4126: test to prevent cross chain replay attacks Signed-off-by: timofeevmd --- client/tests/integration/asset.rs | 6 ++- client/tests/integration/mod.rs | 1 + client/tests/integration/tx_chain_id.rs | 66 +++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 client/tests/integration/tx_chain_id.rs diff --git a/client/tests/integration/asset.rs b/client/tests/integration/asset.rs index 0d97a8b4f61..b473466d770 100644 --- a/client/tests/integration/asset.rs +++ b/client/tests/integration/asset.rs @@ -436,7 +436,11 @@ fn account_id_new(account_name: &str, account_domain: &str) -> AccountId { ) } -fn asset_id_new(definition_name: &str, definition_domain: &str, account_id: AccountId) -> AssetId { +pub fn asset_id_new( + definition_name: &str, + definition_domain: &str, + account_id: AccountId, +) -> AssetId { AssetId::new( AssetDefinitionId::new( definition_domain.parse().expect("Valid"), diff --git a/client/tests/integration/mod.rs b/client/tests/integration/mod.rs index c9ca0bc1052..c6754a553db 100644 --- a/client/tests/integration/mod.rs +++ b/client/tests/integration/mod.rs @@ -20,6 +20,7 @@ mod set_parameter; mod sorting; mod transfer_asset; mod triggers; +mod tx_chain_id; mod tx_history; mod tx_rollback; mod upgrade; diff --git a/client/tests/integration/tx_chain_id.rs b/client/tests/integration/tx_chain_id.rs new file mode 100644 index 00000000000..d9fa9ba9d97 --- /dev/null +++ b/client/tests/integration/tx_chain_id.rs @@ -0,0 +1,66 @@ +use std::str::FromStr; + +use iroha_crypto::KeyPair; +use iroha_data_model::prelude::*; +use test_network::*; + +use crate::integration::asset::asset_id_new; + +#[test] +fn send_tx_with_different_chain_id() { + let (_rt, _peer, test_client) = ::new().with_port(11_240).start_with_runtime(); + wait_for_genesis_committed(&[test_client.clone()], 0); + // Given + let sender_account_id = AccountId::from_str("sender@wonderland").unwrap(); + let sender_keypair = KeyPair::generate(); + let receiver_account_id = AccountId::from_str("receiver@wonderland").unwrap(); + let receiver_keypair = KeyPair::generate(); + let asset_definition_id = AssetDefinitionId::from_str("test_asset#wonderland").unwrap(); + let to_transfer = 1; + + let create_sender_account: InstructionBox = Register::account(Account::new( + sender_account_id.clone(), + sender_keypair.public_key().clone(), + )) + .into(); + let create_receiver_account: InstructionBox = Register::account(Account::new( + receiver_account_id.clone(), + receiver_keypair.public_key().clone(), + )) + .into(); + let register_asset_definition: InstructionBox = + Register::asset_definition(AssetDefinition::quantity(asset_definition_id.clone())).into(); + let register_asset: InstructionBox = Register::asset(Asset::new( + AssetId::new(asset_definition_id.clone(), sender_account_id.clone()), + AssetValue::Quantity(10), + )) + .into(); + test_client + .submit_all_blocking([ + create_sender_account, + create_receiver_account, + register_asset_definition, + register_asset, + ]) + .unwrap(); + let chain_id_0 = ChainId::from("0"); // Value configured by default + let chain_id_1 = ChainId::from("1"); + + let transfer_instruction = Transfer::asset_quantity( + asset_id_new("test_asset", "wonderland", sender_account_id.clone()), + to_transfer, + receiver_account_id.clone(), + ); + let asset_transfer_tx_0 = TransactionBuilder::new(chain_id_0, sender_account_id.clone()) + .with_instructions([transfer_instruction.clone()]) + .sign(&sender_keypair); + let asset_transfer_tx_1 = TransactionBuilder::new(chain_id_1, sender_account_id.clone()) + .with_instructions([transfer_instruction]) + .sign(&sender_keypair); + test_client + .submit_transaction_blocking(&asset_transfer_tx_0) + .unwrap(); + let _err = test_client + .submit_transaction_blocking(&asset_transfer_tx_1) + .unwrap_err(); +} From 4ceaa4e51abd3398f936534da1b913f3bb33033a Mon Sep 17 00:00:00 2001 From: timofeevmd Date: Tue, 5 Mar 2024 11:39:59 +0400 Subject: [PATCH 2/2] [refactor] #4126: test to prevent cross chain replay attacks Signed-off-by: timofeevmd --- client/tests/integration/asset.rs | 6 ++- client/tests/integration/mod.rs | 1 + client/tests/integration/tx_chain_id.rs | 66 +++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 client/tests/integration/tx_chain_id.rs diff --git a/client/tests/integration/asset.rs b/client/tests/integration/asset.rs index 420e1ffef46..2836e63fb0f 100644 --- a/client/tests/integration/asset.rs +++ b/client/tests/integration/asset.rs @@ -496,7 +496,11 @@ fn account_id_new(account_name: &str, account_domain: &str) -> AccountId { ) } -fn asset_id_new(definition_name: &str, definition_domain: &str, account_id: AccountId) -> AssetId { +pub fn asset_id_new( + definition_name: &str, + definition_domain: &str, + account_id: AccountId, +) -> AssetId { AssetId::new( AssetDefinitionId::new( definition_domain.parse().expect("Valid"), diff --git a/client/tests/integration/mod.rs b/client/tests/integration/mod.rs index c9ca0bc1052..c6754a553db 100644 --- a/client/tests/integration/mod.rs +++ b/client/tests/integration/mod.rs @@ -20,6 +20,7 @@ mod set_parameter; mod sorting; mod transfer_asset; mod triggers; +mod tx_chain_id; mod tx_history; mod tx_rollback; mod upgrade; diff --git a/client/tests/integration/tx_chain_id.rs b/client/tests/integration/tx_chain_id.rs new file mode 100644 index 00000000000..d9fa9ba9d97 --- /dev/null +++ b/client/tests/integration/tx_chain_id.rs @@ -0,0 +1,66 @@ +use std::str::FromStr; + +use iroha_crypto::KeyPair; +use iroha_data_model::prelude::*; +use test_network::*; + +use crate::integration::asset::asset_id_new; + +#[test] +fn send_tx_with_different_chain_id() { + let (_rt, _peer, test_client) = ::new().with_port(11_240).start_with_runtime(); + wait_for_genesis_committed(&[test_client.clone()], 0); + // Given + let sender_account_id = AccountId::from_str("sender@wonderland").unwrap(); + let sender_keypair = KeyPair::generate(); + let receiver_account_id = AccountId::from_str("receiver@wonderland").unwrap(); + let receiver_keypair = KeyPair::generate(); + let asset_definition_id = AssetDefinitionId::from_str("test_asset#wonderland").unwrap(); + let to_transfer = 1; + + let create_sender_account: InstructionBox = Register::account(Account::new( + sender_account_id.clone(), + sender_keypair.public_key().clone(), + )) + .into(); + let create_receiver_account: InstructionBox = Register::account(Account::new( + receiver_account_id.clone(), + receiver_keypair.public_key().clone(), + )) + .into(); + let register_asset_definition: InstructionBox = + Register::asset_definition(AssetDefinition::quantity(asset_definition_id.clone())).into(); + let register_asset: InstructionBox = Register::asset(Asset::new( + AssetId::new(asset_definition_id.clone(), sender_account_id.clone()), + AssetValue::Quantity(10), + )) + .into(); + test_client + .submit_all_blocking([ + create_sender_account, + create_receiver_account, + register_asset_definition, + register_asset, + ]) + .unwrap(); + let chain_id_0 = ChainId::from("0"); // Value configured by default + let chain_id_1 = ChainId::from("1"); + + let transfer_instruction = Transfer::asset_quantity( + asset_id_new("test_asset", "wonderland", sender_account_id.clone()), + to_transfer, + receiver_account_id.clone(), + ); + let asset_transfer_tx_0 = TransactionBuilder::new(chain_id_0, sender_account_id.clone()) + .with_instructions([transfer_instruction.clone()]) + .sign(&sender_keypair); + let asset_transfer_tx_1 = TransactionBuilder::new(chain_id_1, sender_account_id.clone()) + .with_instructions([transfer_instruction]) + .sign(&sender_keypair); + test_client + .submit_transaction_blocking(&asset_transfer_tx_0) + .unwrap(); + let _err = test_client + .submit_transaction_blocking(&asset_transfer_tx_1) + .unwrap_err(); +}