Skip to content

Commit

Permalink
feat: add check in default claim to manage collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshvarma committed Sep 26, 2023
1 parent 5e9a0b8 commit aa587d0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pallets/unified-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ impl<T: Config> Pallet<T> {
);
// 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::<T>::contains_key(&evm_address),
Error::<T>::AlreadyMapped
);
// create double mappings for the pair with default evm address
Self::add_mappings(account_id, evm_address.clone());
Ok(evm_address)
Expand Down
18 changes: 18 additions & 0 deletions pallets/unified-accounts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <UnifiedAccounts as UnifiedAddressMapper<_>>::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::<TestRuntime>::AlreadyMapped
);
});
}

#[test]
fn replay_attack_should_not_be_possible() {
ExtBuilder::default().build().execute_with(|| {
Expand Down

0 comments on commit aa587d0

Please sign in to comment.