hedera-hashgraph - 如何将Hedera native 地址转换为EVM地址?​

标签 hedera-hashgraph hedera

如何将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 位数字
  • 0x0000000000000000000000000000000000000x3039 连接
  • 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 中的实现:


由此创建了一个代码片段: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/

相关文章:

hedera-hashgraph - 如何在仅调用一次 TokenMintTransaction 的 Hedera 上类型转换多个 NFT?

javascript - 如何使用 Hashgraph JS SDK 检查帐户是否与 HTS token 关联

hedera-hashgraph - Hedera 中的委托(delegate)合约 ID 是什么

typescript - 如何创建由一个账户签名但使用另一个账户支付的 Hedera 交易

javascript - 如何使用 Hashgraph JS SDK 关联/取消关联 HTS token

solidity - Hedera 上几乎相同的交易中 `gasUsed` 值存在巨大差异 - 为什么?