Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Add function to resolve eth address to actor id (#176)
Browse files Browse the repository at this point in the history
* feat: add function to resolve eth address to actor id
* fix: correct typo
  • Loading branch information
emmanuelm41 authored Jan 26, 2023
1 parent f21734e commit 42400f0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions contracts/v0.8/PrecompilesAPI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ library PrecompilesAPI {
return uint64(actor_id);
}

function resolveEthAddress(bytes memory addr) internal view returns (uint64) {
bytes memory delegatedAddr = abi.encodePacked(hex"040a", addr);

(bool success, bytes memory raw_response) = address(RESOLVE_ADDRESS_PRECOMPILE_ADDR).staticcall(delegatedAddr);
require(success == true, "resolve eth address error");

uint256 actor_id = abi.decode(raw_response, (uint256));

return uint64(actor_id);
}

function lookupDelegatedAddress(uint64 actor_id) internal view returns (bytes memory) {
(bool success, bytes memory raw_response) = address(LOOKUP_DELEGATED_ADDRESS_PRECOMPILE_ADDR).staticcall(
abi.encodePacked(uint256(actor_id))
Expand Down
4 changes: 4 additions & 0 deletions contracts/v0.8/tests/precompiles.test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ contract PrecompilesApiTest {
return PrecompilesAPI.resolveAddress(addr);
}

function resolve_eth_address(bytes memory addr) public view returns (uint64) {
return PrecompilesAPI.resolveEthAddress(addr);
}

function lookup_delegated_address(uint64 actor_id) public view returns (bytes memory) {
return PrecompilesAPI.lookupDelegatedAddress(actor_id);
}
Expand Down
19 changes: 19 additions & 0 deletions testing/tests/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,24 @@ mod tests {
assert_eq!(res.msg_receipt.exit_code.value(), 0);
assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001b8cf5bc0e542a1620184208f78c0cff516cce96");

println!("Calling `resolve_eth_address`");

let message = Message {
from: sender[0].1,
to: Address::new_id(contract_actor_id),
gas_limit: 1000000000,
method_num: EvmMethods::InvokeContract as u64,
sequence: 6,
params: RawBytes::new(hex::decode("58640D6B2CE40000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001476C499BE8821B5B9860144D292FFF728611BFD1A000000000000000000000000").unwrap()),
..Message::default()
};

let res = executor
.execute_message(message, ApplyKind::Explicit, 100)
.unwrap();

assert_eq!(res.msg_receipt.exit_code.value(), 0);
assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58200000000000000000000000000000000000000000000000000000000000000191");

}
}

0 comments on commit 42400f0

Please sign in to comment.