How can I convert a Hedera-native address to an EVM address (and vice versa)? Let's say I have the following:
- a Hedera-native account, with an address like
0.0.12345 - an EVM account on Hedera, with an address like
0xabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde I would like to transfer some HBAR from my EVM account to my Hedera-native account. This implies, I think, that I would need to somehow convert the Hedera-native address to an EVM-address format. How can I do this?
Part 2 of this question is simply the above in reverse direction. If I would like to transfer some HBAR from my Hedera-native account to my EVM account, I would need to covert the EVM address to a Hedera-native address. Is this also possible to do?
The Hedera-native addresses take the format of
${shard}.${realm}.${number}. In practice, currently there is only one shard, and one realm, both with the value of0, so you only need to convert thenumbercomponent. To do so simply convertnumberfrom decimal to hexadecimal, and then pad it on the left with zeroes to get a 40 digit hexadecimal number. Worked example with your address: 0.0.12345-->numberis1234512345in decimal is0x3039in hexadecimal0x3039has 4 digits, we need to pad with 36 zeroes, to make for 40 digits in total0x000000000000000000000000000000000000concatenated with0x30390x0000000000000000000000000000000000003039 Next, you can transfer HBAR from your EVM account to the address that you just converted (0x0000000000000000000000000000000000003039). Them check the balance of your Hedera-native account (0.0.12345), and you should have an updated HBAR balance. This works because under the hood, the Hedera networks treats the EVM address of this type as an alias for the Hedera-native account. See HIP-583: Expand alias support in CryptoCreate & CryptoTransfer Transactions where this is described: Detailed explanation In
EntityIdHelperof hedera-sdk-js, we have the following definition in the comments, which identifies the exact mapping of all components of Hedera-native addresses to EVM addresses. Obviously, the above is just to illustrate the concept, and this really isn't something you would want to do by hand. So use the implementation within
hedera-sdk-js:fromSolidityAddress(address)toSolidityAddress(address)Created a code snippet out of this:
hedera-dev/hedera-code-snippets/blob/main/convert-hedera-native-address-to-evm-address