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

Commit

Permalink
add new functions to create FilAddress types (#327)
Browse files Browse the repository at this point in the history
* feat: add new functions to create FilAddress types
  • Loading branch information
emmanuelm41 authored Feb 28, 2023
1 parent 807d352 commit d6f20ea
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions contracts/v0.8/utils/FilAddresses.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,35 @@ import "@ensdomains/buffer/contracts/Buffer.sol";
library FilAddresses {
using Buffer for Buffer.buffer;

/// @notice allow to get a delegated address (f4) from an eth address
error InvalidAddress();

/// @notice allow to get a FilAddress from an eth address
/// @param addr eth address to convert
/// @return delegated filecoin address
function getDelegatedAddress(address addr) internal pure returns (CommonTypes.FilAddress memory) {
/// @return new filecoin address
function fromEthAddress(address addr) internal pure returns (CommonTypes.FilAddress memory) {
return CommonTypes.FilAddress(abi.encodePacked(hex"0410", 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
Expand All @@ -51,12 +73,4 @@ library FilAddresses {

return addr.data.length <= 256;
}

/// @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));
}
}

0 comments on commit d6f20ea

Please sign in to comment.