diff --git a/tests/integration/Cargo.toml b/tests/integration/Cargo.toml index 54ac446226..80470fee3e 100644 --- a/tests/integration/Cargo.toml +++ b/tests/integration/Cargo.toml @@ -9,6 +9,7 @@ repository.workspace = true [dependencies] hex = { workspace = true } +libsecp256k1 = { workspace = true, features = ["hmac", "static-context"] } parity-scale-codec = { workspace = true } # frontier @@ -29,9 +30,9 @@ sp-io = { workspace = true } sp-runtime = { workspace = true } # astar dependencies -pallet-unified-accounts = { workspace = true } pallet-ethereum-checked = { workspace = true } pallet-evm-precompile-assets-erc20 = { workspace = true } +pallet-unified-accounts = { workspace = true } astar-primitives = { workspace = true } astar-runtime = { workspace = true, features = ["std"], optional = true } diff --git a/tests/integration/src/setup.rs b/tests/integration/src/setup.rs index 97d0b74fc9..026bf57fd0 100644 --- a/tests/integration/src/setup.rs +++ b/tests/integration/src/setup.rs @@ -24,25 +24,30 @@ pub use frame_support::{ weights::Weight, }; pub use pallet_evm::AddressMapping; -pub use pallet_unified_accounts::UnifiedAddressMapper; pub use sp_core::{H160, H256, U256}; +pub use sp_io::hashing::keccak_256; pub use sp_runtime::{AccountId32, MultiAddress}; -pub use astar_primitives::ethereum_checked::AccountMapping; +pub use astar_primitives::{ethereum_checked::AccountMapping, evm::UnifiedAddressMapper}; #[cfg(feature = "shibuya")] pub use shibuya::*; #[cfg(feature = "shibuya")] mod shibuya { use super::*; + pub use pallet_unified_accounts::SignatureHelper; pub use shibuya_runtime::*; /// 1 SBY. pub const UNIT: Balance = SBY; + pub fn alith_secret_key() -> libsecp256k1::SecretKey { + libsecp256k1::SecretKey::parse(&keccak_256(b"Alith")).unwrap() + } + /// H160 address mapped to `ALICE`. pub fn alith() -> H160 { - h160_from(ALICE) + UnifiedAccounts::eth_address(&alith_secret_key()) } /// Convert `H160` to `AccountId32`. @@ -50,11 +55,6 @@ mod shibuya { ::AddressMapping::into_account_id(address) } - /// Convert `AccountId32` to `H160`. - pub fn h160_from(account_id: AccountId32) -> H160 { - ::AccountMapping::into_h160(account_id) - } - /// Deploy an EVM contract with code. pub fn deploy_evm_contract(code: &str) -> H160 { assert_ok!(EVM::create2( @@ -106,6 +106,26 @@ mod shibuya { address } + /// Build the signature payload for given native account and eth private key + fn get_evm_signature(who: &AccountId32, secret: &libsecp256k1::SecretKey) -> [u8; 65] { + // sign the payload + UnifiedAccounts::eth_sign_prehash( + &::SignatureHelper::build_signing_payload( + who, + ), + secret, + ) + } + + /// Create the mappings for the accounts + pub fn connect_accounts(who: &AccountId32, secret: &libsecp256k1::SecretKey) { + assert_ok!(UnifiedAccounts::claim_evm_address( + RuntimeOrigin::signed(who.clone()), + UnifiedAccounts::eth_address(secret), + get_evm_signature(who, secret) + )); + } + pub fn claim_default_accounts(account: AccountId) { let default_h160 = UnifiedAccounts::to_default_h160(&account); assert_ok!(UnifiedAccounts::claim_default_evm_address( diff --git a/tests/integration/src/xvm.rs b/tests/integration/src/xvm.rs index b52ee6f39c..cc3031ad9c 100644 --- a/tests/integration/src/xvm.rs +++ b/tests/integration/src/xvm.rs @@ -158,7 +158,7 @@ const CALL_EVM_PAYBLE_NAME: &'static str = "call_xvm_payable"; fn evm_payable_call_via_xvm_works() { new_test_ext().execute_with(|| { // create account mappings - claim_default_accounts(ALICE); + connect_accounts(&ALICE, &alith_secret_key()); let evm_payable_addr = deploy_evm_contract(EVM_PAYABLE); @@ -229,7 +229,7 @@ fn wasm_payable_call_via_xvm_works() { fn calling_wasm_payable_from_evm_fails_if_caller_contract_balance_below_ed() { new_test_ext().execute_with(|| { // create account mappings - claim_default_accounts(ALICE); + connect_accounts(&ALICE, &alith_secret_key()); let wasm_payable_addr = deploy_wasm_contract(WASM_PAYABLE_NAME); let call_wasm_payable_addr = deploy_evm_contract(CALL_WASM_PAYBLE); @@ -271,7 +271,7 @@ fn calling_wasm_payable_from_evm_fails_if_caller_contract_balance_below_ed() { fn calling_wasm_payable_from_evm_works() { new_test_ext().execute_with(|| { // create account mappings - claim_default_accounts(ALICE); + connect_accounts(&ALICE, &alith_secret_key()); let wasm_payable_addr = deploy_wasm_contract(WASM_PAYABLE_NAME); let call_wasm_payable_addr = deploy_evm_contract(CALL_WASM_PAYBLE); @@ -304,7 +304,7 @@ fn calling_wasm_payable_from_evm_works() { fn calling_evm_payable_from_wasm_works() { new_test_ext().execute_with(|| { // create account mappings - claim_default_accounts(ALICE); + connect_accounts(&ALICE, &alith_secret_key()); let evm_payable_addr = deploy_evm_contract(EVM_PAYABLE); let wasm_address = deploy_wasm_contract(CALL_EVM_PAYBLE_NAME); @@ -348,7 +348,7 @@ fn calling_evm_payable_from_wasm_works() { fn reentrance_not_allowed() { new_test_ext().execute_with(|| { // create account mappings - claim_default_accounts(ALICE); + connect_accounts(&ALICE, &alith_secret_key()); // Call path: WASM -> EVM -> WASM let call_evm_payable_address = deploy_wasm_contract(CALL_EVM_PAYBLE_NAME);