如何将Hedera原生地址转换为非长零EVM地址?

huangapple go评论113阅读模式
英文:

How to convert a Hedera native address into a non-long-zero EVM address?

问题

使用Hedera SDK JS,我可以将帐户ID转换为“long-zero”格式的EVM地址。
例如:0.0.3996280 --> 0x00000000000000000000000000000000003cfa78

如相关问题:"如何将Hedera原生地址转换为EVM地址?"

如何转换为“non-long-zero”格式的EVM地址?
例如:0.0.3996280 --> 0x7394111093687e9710b7a7aeba3ba0f417c54474

参见Hashscan上的0.0.3996280

我需要这样做是因为当您通过Hedera SDKs发送ContractCallQuery时,任何调用的智能合约函数中可见的msg.sender的值是“non-long-zero”格式的EVM地址。

我目前正在执行的操作:

const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorEvmAddress = operatorId.toSolidityAddress();

然而,operatorEvmAddress是以“long-zero”格式表示的,
因此我不能在随后的智能合约交互中使用它。

英文:

Using Hedera SDK JS, I can convert an Account ID to "long-zero" format EVM address.
e.g. 0.0.3996280 --> 0x00000000000000000000000000000000003cfa78

(See related question: "How to convert a Hedera native address into an EVM address?".)

How do I convert to the "non-long-zero" format EVM address?
e.g. 0.0.3996280 --> 0x7394111093687e9710b7a7aeba3ba0f417c54474

(See 0.0.3996280 on Hashscan.)

I need this because when you send ContractCallQuery via Hedera SDKs, the value of msg.sender as visible within any smart contract functions invoked is the "non-long-zero" format EVM address.

What I'm doing currently:

const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorEvmAddress = operatorId.toSolidityAddress();

However, operatorEvmAddress is in the "long-zero" format,
and I therefore cannot use that in my subsequent smart contract interactions.

答案1

得分: 6

答案取决于您是否可以访问帐户的公钥。
请注意,如果您拥有私钥,您可以从中提取公钥。

如果您无法访问帐户的公钥

您可以使用SDK获取长零EVM地址,因为这是一种数学转换。

但是,您无法仅使用SDK获取非长零EVM地址,因为这不是数学转换。您需要查询网络状态,例如通过Hedera镜像节点查询。

如果您可以访问帐户的公钥

您可以使用SDK获取长零EVM地址非长零EVM地址。

const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorPrivateKey = PrivateKey.fromString(process.env.OPERATOR_KEY);
const operatorPublicKey = operatorPrivateKey.publicKey;

// AccountId.toSolidityAddress --> 长零
const operatorEvmAddressLongZero = operatorId.toSolidityAddress();

// PublicKey.toEvmAddress --> 非长零
const operatorEvmAddressNonLongZero = operatorPublicKey.toEvmAddress();
英文:

The answer depends on whether you have access to the public key of the account.
Note that if you have the private key, you can extract the public key from it.

If you do not have access to the account's public key:

You can obtain the long-zero EVM address using the SDK,
as this is a mathematical conversion.

However, you cannot obtain the non-long-zero EVM address using the SDK alone,
as this is not a mathematical conversion.
You will need to query the network state,
for example through a Hedera mirror node query.

If you do have access to the account's public key:

You can obtain both the long-zero EVM address and
the non-long-zero EVM address using the SDK.

const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorPrivateKey = PrivateKey.fromString(process.env.OPERATOR_KEY);
const operatorPublicKey = operatorPrivateKey.publicKey;

// AccountId.toSolidityAddress --> long-zero
const operatorEvmAddressLongZero = operatorId.toSolidityAddress();

// PublicKey.toEvmAddress --> non-long-zero
const operatorEvmAddressNonLongZero = operatorPublicKey.toEvmAddress();

</details>



# 答案2
**得分**: 1

正如[Ashe的回答](https://stackoverflow.com/a/76682765/194982)中所提到,并且根据[David的评论](https://stackoverflow.com/questions/76680532/how-to-convert-a-hedera-native-address-into-a-non-long-zero-evm-address/76763434#comment135196366_76682765)的要求:

> 如果您无法访问帐户的公钥
> ...
> 您将需要查询网络状态例如通过Hedera镜像节点查询

以下是一种通过镜像节点执行此操作的方法

```shell
curl \
  --silent \
  -X 'GET' \
  -H 'accept: application/json' \
  'https://testnet.mirrornode.hedera.com/api/v1/accounts/0.0.3996280?limit=1' \
  | jq --raw-output ".evm_address"

这将输出:

0x7394111093687e9710b7a7aeba3ba0f417c54474

这确实是与此帐户对应的非长零EVM地址。

英文:

As mentioned in Ashe's answer, and requested in David's comment:

> If you do not have access to the account's public key
> ...
> You will need to query the network state, for example through a Hedera mirror node query.​

Here is one way to do it, via the mirror node:

curl \
  --silent \
  -X &#39;GET&#39; \
  -H &#39;accept: application/json&#39; \
  &#39;https://testnet.mirrornode.hedera.com/api/v1/accounts/0.0.3996280?limit=1&#39; \
  | jq --raw-output &quot;.evm_address&quot;

This will output:

0x7394111093687e9710b7a7aeba3ba0f417c54474

which is indeed the non-long-zero EVM address that corresponds to this account.


Ref: Mirror Node Swagger for the above API:
https://testnet.mirrornode.hedera.com/api/v1/docs/#/accounts/getAccountByIdOrAliasOrEvmAddress

huangapple
  • 本文由 发表于 2023年7月13日 22:29:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定