Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Compatible derive_account_id #234

Merged
merged 7 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion primitives/darwinia-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ impl Chain for DarwiniaLike {
pub struct AccountIdConverter;
impl Convert<H256, AccountId> for AccountIdConverter {
fn convert(hash: H256) -> AccountId {
let evm_address: H160 = hash.into();
// This way keep compatible with darwinia 1.0 substrate to evm account rule.
let evm_address = H160::from_slice(&hash.as_bytes()[0..20]);
evm_address.into()
}
}
20 changes: 18 additions & 2 deletions primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,24 @@ where
match id {
SourceAccount::Root =>
(ROOT_ACCOUNT_DERIVATION_PREFIX, bridge_id).using_encoded(blake2_256),
SourceAccount::Account(id) =>
(ACCOUNT_DERIVATION_PREFIX, bridge_id, id).using_encoded(blake2_256),
SourceAccount::Account(id) => {
let to_darwinia_old_account_id = |address| -> H256 {
let mut result = [0u8; 32];
result[0..4].copy_from_slice(b"dvm:");
result[11..31].copy_from_slice(address);
result[31] = result[1..31].iter().fold(result[0], |sum, &byte| sum ^ byte);
result.into()
};

// The aim is to keep the accounts derived from the evm account compatible with the
// darwinia 1.0 account id.
if id.encode().len() == 20 {
let account_id = to_darwinia_old_account_id(&id.encode());
(ACCOUNT_DERIVATION_PREFIX, bridge_id, account_id).using_encoded(blake2_256)
} else {
(ACCOUNT_DERIVATION_PREFIX, bridge_id, id).using_encoded(blake2_256)
}
},
}
.into()
}
Expand Down