如何将Hedera 原生地址转换为EVM 地址(反之亦然)? 假设我有以下内容:
- Hedera 原生帐户,地址类似于
0.0.12345
- Hedera 上的 EVM 帐户,地址类似于
0xabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde
我想将一些 HBAR 从我的 EVM 账户转移到我的 Hedera 本地账户。 我认为,这意味着我需要以某种方式将 Hedera 本地地址转换为 EVM 地址格式。 我该怎么做?
这个问题的第二部分只是上面的相反方向。 如果我想将一些 HBAR 从我的 Hedera 原生账户转移到我的 EVM 账户, 我需要将 EVM 地址转换为 Hedera 本地地址。 这也可以吗?
最佳答案
Hedera 原生地址采用 ${shard}.${realm}.${number}
格式。
实际上,目前只有一个分片和一个领域,
两者的值都是0
,因此只需转换number
组件即可。
为此,只需将 number
从十进制转换为十六进制,
然后在左侧补零,得到 40 位十六进制数。
使用您的地址的工作示例:
0.0.12345
-->数字
是12345
- 十进制的
12345
是十六进制的0x3039
- 由于
0x3039
有 4 位数字,因此我们需要用 36 个零填充,总共 40 位数字 0x000000000000000000000000000000000000
与0x3039
连接0x0000000000000000000000000000000000003039
接下来,您可以从您的 EVM 账户转账 HBAR 到您刚刚转换的地址 (0x0000000000000000000000000000000000003039
)。 他们检查您的 Hedera 原生帐户的余额 (0.0.12345
), 并且您应该有更新的 HBAR 余额。 这是有效的,因为在幕后, Hedera 网络将此类型的 EVM 地址视为 Hedera 本地帐户的别名。 请参阅HIP-583: Expand alias support in CryptoCreate & CryptoTransfer Transactions其中描述的是:
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.
详细说明 在 EntityIdHelper 中 of hedera-sdk-js ,我们在注释中有以下定义, 它标识了所有组件的精确映射 Hedera 原生地址到 EVM 地址。
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:
- 将每个组件的十进制转换为十六进制
- 将左侧的分片组件补零以获得 8 位的宽度
- 用零填充左侧的领域和数字部分,以获得 16 位的宽度
- 连接组件
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
显然,上面只是为了说明这个概念,
这确实不是您想要手动完成的事情。
因此,请使用 hedera-sdk-js
中的实现:
- 要从 EVM 地址转换为 Hedera 原生地址:
fromSolidityAddress(address)
- 从 Hedera 原生地址转换为 EVM 地址:
toSolidityAddress(address)
由此创建了一个代码片段:hedera-dev/hedera-code-snippets/blob/main/convert-hedera-native-address-to-evm-address
关于hedera-hashgraph - 如何将Hedera native 地址转换为EVM地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76182970/