Ryan Arndt
Ryan Arndt

Reputation: 148

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?

Upvotes: 2

Views: 1279

Answers (1)

bguiz
bguiz

Reputation: 28587

​ 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: ​

  • 0.0.12345 --> number is 12345
  • 12345 in decimal is 0x3039 in hexadecimal
  • Since 0x3039 has 4 digits, we need to pad with 36 zeroes, to make for 40 digits in total
  • 0x000000000000000000000000000000000000 concatenated with 0x3039
  • 0x0000000000000000000000000000000000003039 ​ 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: ​

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:

  • Convert decimal to hexadecimal, per component
  • Pad the shard component on the left with zeroes to obtain a width of 8 digits
  • Pad both the realm and number components on the left with zeroes to obtain a width of 16 digits
  • Concatenate components
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

Upvotes: 4

Related Questions