diff --git a/pallets/unified-accounts/src/lib.rs b/pallets/unified-accounts/src/lib.rs index 77ec369349..6ee0ad41c8 100644 --- a/pallets/unified-accounts/src/lib.rs +++ b/pallets/unified-accounts/src/lib.rs @@ -241,6 +241,12 @@ impl Pallet { ); // get the default evm address let evm_address = T::DefaultAccountMapping::into_h160(account_id.clone()); + // make sure default address is not already mapped, this should not + // happen but for sanity check. + ensure!( + !NativeToEvm::::contains_key(&evm_address), + Error::::AlreadyMapped + ); // create double mappings for the pair with default evm address Self::add_mappings(account_id, evm_address.clone()); Ok(evm_address) diff --git a/pallets/unified-accounts/src/tests.rs b/pallets/unified-accounts/src/tests.rs index 19724e368f..f3d8ff2ed9 100644 --- a/pallets/unified-accounts/src/tests.rs +++ b/pallets/unified-accounts/src/tests.rs @@ -222,6 +222,24 @@ fn account_default_claim_works() { }); } +#[test] +fn account_default_claim_should_not_work_if_collision() { + ExtBuilder::default().build().execute_with(|| { + let bob_default_h160 = >::to_default_h160(&BOB); + + // connect alice native with bob's default address + // in real world possibilty of this happening is minuscule + UnifiedAccounts::add_mappings(ALICE, bob_default_h160); + + // bob try claiming default h160 address, it should fail since alice already + // has mapping in place with it. + assert_noop!( + UnifiedAccounts::claim_default_evm_address(RuntimeOrigin::signed(BOB)), + Error::::AlreadyMapped + ); + }); +} + #[test] fn replay_attack_should_not_be_possible() { ExtBuilder::default().build().execute_with(|| {