|
| 1 | +pragma solidity ^0.4.24; |
| 2 | + |
| 3 | + |
| 4 | +/** |
| 5 | + * @title ERC165Checker |
| 6 | + * @dev Use `using ERC165Checker for address`; to include this library |
| 7 | + * https://github.com/ethereum/EIPs/blob/master/EIPS/eip-165.md |
| 8 | + */ |
| 9 | +library ERC165Checker { |
| 10 | + // As per the EIP-165 spec, no interface should ever match 0xffffffff |
| 11 | + bytes4 private constant InterfaceId_Invalid = 0xffffffff; |
| 12 | + |
| 13 | + bytes4 private constant InterfaceId_ERC165 = 0x01ffc9a7; |
| 14 | + /** |
| 15 | + * 0x01ffc9a7 === |
| 16 | + * bytes4(keccak256('supportsInterface(bytes4)')) |
| 17 | + */ |
| 18 | + |
| 19 | + |
| 20 | + /** |
| 21 | + * @notice Query if a contract supports ERC165 |
| 22 | + * @param _address The address of the contract to query for support of ERC165 |
| 23 | + * @return true if the contract at _address implements ERC165 |
| 24 | + */ |
| 25 | + function supportsERC165(address _address) |
| 26 | + internal |
| 27 | + view |
| 28 | + returns (bool) |
| 29 | + { |
| 30 | + // Any contract that implements ERC165 must explicitly indicate support of |
| 31 | + // InterfaceId_ERC165 and explicitly indicate non-support of InterfaceId_Invalid |
| 32 | + return supportsERC165Interface(_address, InterfaceId_ERC165) && |
| 33 | + !supportsERC165Interface(_address, InterfaceId_Invalid); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @notice Query if a contract implements an interface, also checks support of ERC165 |
| 38 | + * @param _address The address of the contract to query for support of an interface |
| 39 | + * @param _interfaceId The interface identifier, as specified in ERC-165 |
| 40 | + * @return true if the contract at _address indicates support of the interface with |
| 41 | + * identifier _interfaceId, false otherwise |
| 42 | + * @dev Interface identification is specified in ERC-165. |
| 43 | + */ |
| 44 | + function supportsInterface(address _address, bytes4 _interfaceId) |
| 45 | + internal |
| 46 | + view |
| 47 | + returns (bool) |
| 48 | + { |
| 49 | + // query support of both ERC165 as per the spec and support of _interfaceId |
| 50 | + return supportsERC165(_address) && |
| 51 | + supportsERC165Interface(_address, _interfaceId); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @notice Query if a contract implements interfaces, also checks support of ERC165 |
| 56 | + * @param _address The address of the contract to query for support of an interface |
| 57 | + * @param _interfaceIds A list of interface identifiers, as specified in ERC-165 |
| 58 | + * @return true if the contract at _address indicates support all interfaces in the |
| 59 | + * _interfaceIds list, false otherwise |
| 60 | + * @dev Interface identification is specified in ERC-165. |
| 61 | + */ |
| 62 | + function supportsInterfaces(address _address, bytes4[] _interfaceIds) |
| 63 | + internal |
| 64 | + view |
| 65 | + returns (bool) |
| 66 | + { |
| 67 | + // query support of ERC165 itself |
| 68 | + if (!supportsERC165(_address)) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + // query support of each interface in _interfaceIds |
| 73 | + for (uint256 i = 0; i < _interfaceIds.length; i++) { |
| 74 | + if (!supportsERC165Interface(_address, _interfaceIds[i])) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // all interfaces supported |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @notice Query if a contract implements an interface, does not check ERC165 support |
| 85 | + * @param _address The address of the contract to query for support of an interface |
| 86 | + * @param _interfaceId The interface identifier, as specified in ERC-165 |
| 87 | + * @return true if the contract at _address indicates support of the interface with |
| 88 | + * identifier _interfaceId, false otherwise |
| 89 | + * @dev Assumes that _address contains a contract that supports ERC165, otherwise |
| 90 | + * the behavior of this method is undefined. This precondition can be checked |
| 91 | + * with the `supportsERC165` method in this library. |
| 92 | + * Interface identification is specified in ERC-165. |
| 93 | + */ |
| 94 | + function supportsERC165Interface(address _address, bytes4 _interfaceId) |
| 95 | + private |
| 96 | + view |
| 97 | + returns (bool) |
| 98 | + { |
| 99 | + // success determines whether the staticcall succeeded and result determines |
| 100 | + // whether the contract at _address indicates support of _interfaceId |
| 101 | + (bool success, bool result) = callERC165SupportsInterface( |
| 102 | + _address, _interfaceId); |
| 103 | + |
| 104 | + return (success && result); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @notice Calls the function with selector 0x01ffc9a7 (ERC165) and suppresses throw |
| 109 | + * @param _address The address of the contract to query for support of an interface |
| 110 | + * @param _interfaceId The interface identifier, as specified in ERC-165 |
| 111 | + * @return success true if the STATICCALL succeeded, false otherwise |
| 112 | + * @return result true if the STATICCALL succeeded and the contract at _address |
| 113 | + * indicates support of the interface with identifier _interfaceId, false otherwise |
| 114 | + */ |
| 115 | + function callERC165SupportsInterface( |
| 116 | + address _address, |
| 117 | + bytes4 _interfaceId |
| 118 | + ) |
| 119 | + private |
| 120 | + view |
| 121 | + returns (bool success, bool result) |
| 122 | + { |
| 123 | + bytes memory encodedParams = abi.encodeWithSelector( |
| 124 | + InterfaceId_ERC165, |
| 125 | + _interfaceId |
| 126 | + ); |
| 127 | + |
| 128 | + // solium-disable-next-line security/no-inline-assembly |
| 129 | + assembly { |
| 130 | + let encodedParams_data := add(0x20, encodedParams) |
| 131 | + let encodedParams_size := mload(encodedParams) |
| 132 | + |
| 133 | + let output := mload(0x40) // Find empty storage location using "free memory pointer" |
| 134 | + mstore(output, 0x0) |
| 135 | + |
| 136 | + success := staticcall( |
| 137 | + 30000, // 30k gas |
| 138 | + _address, // To addr |
| 139 | + encodedParams_data, |
| 140 | + encodedParams_size, |
| 141 | + output, |
| 142 | + 0x20 // Outputs are 32 bytes long |
| 143 | + ) |
| 144 | + |
| 145 | + result := mload(output) // Load the result |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
0 commit comments