|
| 1 | +/******************************************************************************* |
| 2 | + * (c) 2022 Zondax AG |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + ********************************************************************************/ |
| 16 | +// THIS CODE WAS SECURITY REVIEWED BY KUDELSKI SECURITY, BUT NOT FORMALLY AUDITED |
| 17 | + |
| 18 | +// SPDX-License-Identifier: Apache-2.0 |
| 19 | +pragma solidity ^0.8.17; |
| 20 | + |
| 21 | +import "../types/CommonTypes.sol"; |
| 22 | + |
| 23 | +import "../cbor/FilecoinCbor.sol"; |
| 24 | + |
| 25 | +import "../utils/Actor.sol"; |
| 26 | + |
| 27 | +/// @title Library containing common handler functions used in the project |
| 28 | +/// @author Zondax AG |
| 29 | +library UtilsHandlers { |
| 30 | + using FilecoinCBOR for *; |
| 31 | + |
| 32 | + /// @notice the codec received is not valid |
| 33 | + error InvalidCodec(uint64); |
| 34 | + |
| 35 | + /// @notice filecoin method not handled |
| 36 | + error MethodNotHandled(uint64); |
| 37 | + |
| 38 | + /// @notice utility function meant to handle calls from other builtin actors. Arguments are passed as cbor serialized data (in filecoin native format) |
| 39 | + /// @param method the filecoin method id that is being called |
| 40 | + /// @param params raw data (in bytes) passed as arguments to the method call |
| 41 | + function handleFilecoinMethod(uint64 method, uint64 codec, bytes calldata params) internal pure returns (CommonTypes.UniversalReceiverParams memory) { |
| 42 | + if (method == CommonTypes.UniversalReceiverHookMethodNum) { |
| 43 | + if (codec != Misc.CBOR_CODEC) { |
| 44 | + revert InvalidCodec(codec); |
| 45 | + } |
| 46 | + |
| 47 | + return params.deserializeUniversalReceiverParams(); |
| 48 | + } else { |
| 49 | + revert MethodNotHandled(method); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// @param target The actor id you want to interact with |
| 54 | + function universalReceiverHook(CommonTypes.FilActorId target, CommonTypes.UniversalReceiverParams memory params) internal returns (int256, bytes memory) { |
| 55 | + bytes memory raw_request = params.serializeUniversalReceiverParams(); |
| 56 | + |
| 57 | + (int256 exit_code, bytes memory result) = Actor.callByID(target, CommonTypes.UniversalReceiverHookMethodNum, Misc.CBOR_CODEC, raw_request, 0, false); |
| 58 | + |
| 59 | + return (exit_code, result); |
| 60 | + } |
| 61 | +} |
0 commit comments