hedera-hashgraphhedera

How to convert a Hedera native address into an EVM address? ​


How can I convert a Hedera-native address to an EVM address (and vice versa)? ​ Let's say I have the following:

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?


Solution

  • ​ 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 of 0, so you only need to convert the number component. ​ To do so simply convert number from decimal to hexadecimal, and then pad it on the left with zeroes to get a 40 digit hexadecimal number. ​ Worked example with your address: ​

    If a user is using an Ethereum-native wallet ... they cannot send hbars to a shard.realm.num account ID, they must send it to a 20-byte address.

    Every account in Hedera has an Ethereum compatible 20-byte alias (known as the “long zero” alias since it is prefixed by many zeros), and an Ethereum-native wallet using the JSON-RPC relay can use that to send value to existing accounts with no modification to the existing accounts. ​


    Detailed explanation ​ In EntityIdHelper of 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. ​

    Solidity addresses are 20 bytes long and hex encoded, where the first 4 bytes represent the shard, the next 8 bytes represent the realm, and the last 8 bytes represent the num. All in Big Endian format ​ Using this description, we can convert, by hand, a Hedera-native account address into an EVM address. It can be summarised as:

    Hedera-native address pattern:
    S.R.N
    ​
    Hedera-native address example:
    17.2049.12345
    ​
    (Interrim step) Hedera-native address components in hexadecimal:
    0x11 0x801 0x3039
    ​
    EVM address pattern:
    0xSSSSSSSSRRRRRRRRRRRRRRRRNNNNNNNNNNNNNNNN
    ​
    EVM address example:
    0x0000001100000000000008010000000000003039
    ​
    

    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:


    Created a code snippet out of this: hedera-dev/hedera-code-snippets/blob/main/convert-hedera-native-address-to-evm-address