I am writing a test to test evm contract instantiation using polkadot api.
I get to derive the evm account like this:
const alice = keyring.addFromUri('//Alice', { name: 'Alice default' });
const aliceEthAccount = addressToEvm(alice.addressRaw);
where addressToEvm
is an helper function from @polkadot/util-crypto
This piece of code from frontier does the conversion from H160 to AccountId on chain
fn into_account_id(address: H160) -> AccountId32 {
let mut data = [0u8; 24];
data[0..4].copy_from_slice(b"evm:");
data[4..24].copy_from_slice(&address[..]);
let hash = H::hash(&data);
AccountId32::from(Into::<[u8; 32]>::into(hash))
}
But then when i feed the aliceEthAccount
to my extrinsic to instantiate the evm contract, i get an address which does not correspond to my alice substrate address, which makes the origin wrong and also funds cannot be withdrawn from that wrong generated address.
Am i missing something? or is the way i am generating the H160(ethereum account) address wrong?