This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathPrecompilesAPI.sol
74 lines (61 loc) · 3.02 KB
/
PrecompilesAPI.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*******************************************************************************
* (c) 2022 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
//
// THIS CODE WAS SECURITY REVIEWED BY KUDELSKI SECURITY, BUT NOT FORMALLY AUDITED
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
import "./types/CommonTypes.sol";
/// @title This library simplify the call of FEVM precompiles contracts.
/// @author Zondax AG
library PrecompilesAPI {
address constant RESOLVE_ADDRESS_PRECOMPILE_ADDR = 0xFE00000000000000000000000000000000000001;
address constant LOOKUP_DELEGATED_ADDRESS_PRECOMPILE_ADDR = 0xfE00000000000000000000000000000000000002;
/// @notice an error happened trying to call the actor
error FailToCallActor();
/// @notice get the actor id from an actor address
/// @param addr actor address you want to get id from (in bytes format, not string)
/// @return the actor id
function resolveAddress(CommonTypes.FilAddress memory addr) internal view returns (uint64) {
(bool success, bytes memory raw_response) = address(RESOLVE_ADDRESS_PRECOMPILE_ADDR).staticcall(addr.data);
if (!success) {
revert FailToCallActor();
}
uint256 actor_id = abi.decode(raw_response, (uint256));
return uint64(actor_id);
}
/// @notice get the actor id from an eth address
/// @param addr eth address you want to get id from (in bytes format)
/// @return the actor id
function resolveEthAddress(address 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);
if (!success) {
revert FailToCallActor();
}
uint256 actor_id = abi.decode(raw_response, (uint256));
return uint64(actor_id);
}
/// @notice get the actor delegated address (f4) from an actor id
/// @param actor_id actor id you want to get the delegated address (f4) from
/// @return delegated address in bytes format (not string)
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)));
if (!success) {
revert FailToCallActor();
}
return raw_response;
}
}