/******************************************************************************* * (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.17; import "../types/CommonTypes.sol"; /// @title Library containing miscellaneous functions used on the project /// @author Zondax AG library Misc { /// @notice the codec received is not valid error InvalidCodec(uint64); /// @notice filecoin method not handled error MethodNotHandled(uint64); uint64 constant DAG_CBOR_CODEC = 0x71; uint64 constant CBOR_CODEC = 0x51; uint64 constant NONE_CODEC = 0x00; // Code taken from Openzeppelin repo // Link: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/0320a718e8e07b1d932f5acb8ad9cec9d9eed99b/contracts/utils/math/SignedMath.sol#L37-L42 /// @notice get the abs from a signed number /// @param n number to get abs from /// @return unsigned number function abs(int256 n) internal pure returns (uint256) { unchecked { // must be unchecked in order to support `n = type(int256).min` return uint256(n >= 0 ? n : -n); } } /// @notice validate if an address exists or not /// @dev read this article for more information https://blog.finxter.com/how-to-find-out-if-an-ethereum-address-is-a-contract/ /// @param addr address to check /// @return whether the address exists or not function addressExists(address addr) internal view returns (bool) { bytes32 codehash; assembly { codehash := extcodehash(addr) } return codehash != 0x0; } /// Returns the data size required by CBOR.writeFixedNumeric function getPrefixSize(uint256 data_size) internal pure returns (uint256) { if (data_size <= 23) { return 1; } else if (data_size <= 0xFF) { return 2; } else if (data_size <= 0xFFFF) { return 3; } else if (data_size <= 0xFFFFFFFF) { return 5; } return 9; } function getBytesSize(bytes memory value) internal pure returns (uint256) { return getPrefixSize(value.length) + value.length; } function getCidSize(bytes memory value) internal pure returns (uint256) { return getPrefixSize(2) + value.length; } function getFilActorIdSize(CommonTypes.FilActorId value) internal pure returns (uint256) { uint64 val = CommonTypes.FilActorId.unwrap(value); return getPrefixSize(uint256(val)); } function getChainEpochSize(CommonTypes.ChainEpoch value) internal pure returns (uint256) { int64 val = CommonTypes.ChainEpoch.unwrap(value); if (val >= 0) { return getPrefixSize(uint256(uint64(val))); } else { return getPrefixSize(uint256(uint64(-1 - val))); } } function getBoolSize() internal pure returns (uint256) { return getPrefixSize(1); } }