hashgraph / hashgraph/hedera-sdk-reference

Getting AccountId/ContractId from an evm address is confusing after HIP-583

Open
#128 12 comments 2 reactions 0 assignees View on GitHub
enhancement
Dominant language
HTML
Stars
7
Forks
3
PR merge metrics
No merged PRs in 30d

Description

### Problem

After the implementation of [HIP-583](https://hips.hedera.com/hip/hip-583) and the introduction to `evmAddress` there are several issues that confuse users and need to be addressed. After this HIP, we now have 2 formats of evm addresses - the so called `account-num alias` (or long-zero format address) and the `evm address alias` (references taken from the hip doc).

The issues are as follows:
1. Before the introduction of the new `evm address alias`, users were using the single method `AccountId/ContractId.fromSolidityAddress(address: string)` which basically parses the provided 20 bytes to `shard` - first 4 bytes, `realm` - next 8 bytes and `num` - last 8 bytes and generates a proper `AccountId/ContractId`. After the introduction of the HIP, some users are trying to pass the new `evm address alias` to the same function and since this field is generated as in Ethereum (rightmost 20 bytes of the 32 byte Keccak-256 hash of an ECDSA public key), the algorithm for parsing the `account-num alias` can yield enormous or even negative values for each of `shard`, `realm` and `num` fields. This is why users should use the new method [fromEvmAddress()](https://github.com/hashgraph/hedera-sdk-js/blob/develop/src/contract/ContractId.js#L66). An [example](https://github.com/hashgraph/hedera-sdk-js/issues/1740) - here the passed address is of `evm address alias` and respectively if you use the wrong function - [fromSolidityAddress()](https://github.com/hashgraph/hedera-sdk-js/blob/develop/src/contract/ContractId.js#L151) you get wrong result

2. The second one, following the same [example](https://github.com/hashgraph/hedera-sdk-js/issues/1740) is that if you try getting the `AccountId.fromEvmAddress(0, 0, "0xdfc61db3604e254611827ecf0d42e6f4b2e256ac")` with the correct function, the result would be `AccountId` with zeros as `shard`, `realm` and `number` because we cannot extract the `num` from the passed evm address
```
AccountId {
shard: Long { low: 0, high: 0, unsigned: false },
realm: Long { low: 0, high: 0, unsigned: false },
num: Long { low: 0, high: 0, unsigned: false },
aliasKey: null,
evmAddress: EvmAddress {
_bytes:
},
_checksum: null
}
```
So using this method, we set `0` by default to the `num` field and pass the `shard` and `realm` provided from the method call. Which is confusing because if you try to query the [mirror node](https://testnet.mirrornode.hedera.com/api/v1/accounts/0xdfc61db3604e254611827ecf0d42e6f4b2e256ac) you will get the actual `AccoundId` fields, in this case `0.0.1171` as @mgarbs expects

### Solution

Proposals for the issues:

1. Here we thought of 2 approaches and we have to decide which one to implement
- Introduce a 3rd more generic method e.g. `fromAddress()`, add check whether or not it is a `account-num alias` and navigate the flow to the proper method based on the format of the passed address
- Or add those checks inside the present methods - `fromEvmAddress()` and `fromSolidityAddress()` and again based on the result, pass the flow to the proper method of these two
- 3rd option would be to do both - add the checks to each of those methods, add `fromAddress()` to handle both evm address formats and lastly to deprecate the present two methods in favour of `fromAddress()`

2. Here an approach would be to query somehow the mirror node and get the data we need but the tricky part is that in this part we do not have access to a client or something. This is just parsing functionality where we just try to generate an object from an input. We either need to specify the network to know which mirror node to call and get the info or add a client and to be honest, both approaches wont be a good user experience...

### Alternatives

@SimiHunjan @gregscullard @ochikov @Nana-EC
```[tasklist]
### Tasks
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.