/******************************************************************************* * (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"; import "../utils/Leb128.sol"; import "@ensdomains/buffer/contracts/Buffer.sol"; /// @notice This library is a set a functions that allows to handle filecoin addresses conversions and validations /// @author Zondax AG library FilAddresses { using Buffer for Buffer.buffer; error InvalidAddress(); /// @notice allow to get a FilAddress from an eth address /// @param addr eth address to convert /// @return new filecoin address function fromEthAddress(address addr) internal pure returns (CommonTypes.FilAddress memory) { return CommonTypes.FilAddress(abi.encodePacked(hex"040a", addr)); } /// @notice allow to create a Filecoin address from an actorID /// @param actorID uint64 actorID /// @return address filecoin address function fromActorID(uint64 actorID) internal pure returns (CommonTypes.FilAddress memory) { Buffer.buffer memory result = Leb128.encodeUnsignedLeb128FromUInt64(actorID); return CommonTypes.FilAddress(abi.encodePacked(hex"00", result.buf)); } /// @notice allow to create a Filecoin address from bytes /// @param data address in bytes format /// @return filecoin address function fromBytes(bytes memory data) internal pure returns (CommonTypes.FilAddress memory) { CommonTypes.FilAddress memory newAddr = CommonTypes.FilAddress(data); if (!validate(newAddr)) { revert InvalidAddress(); } return newAddr; } /// @notice allow to validate if an address is valid or not /// @dev we are only validating known address types. If the type is not known, the default value is true /// @param addr the filecoin address to validate /// @return whether the address is valid or not function validate(CommonTypes.FilAddress memory addr) internal pure returns (bool) { if (addr.data[0] == 0x00) { return addr.data.length <= 10; } else if (addr.data[0] == 0x01 || addr.data[0] == 0x02) { return addr.data.length == 21; } else if (addr.data[0] == 0x03) { return addr.data.length == 49; } else if (addr.data[0] == 0x04) { return addr.data.length <= 64; } return addr.data.length <= 256; } }