ERC165Checker.sol 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. pragma solidity ^0.5.7;
  2. /**
  3. * @title ERC165Checker
  4. * @dev Use `using ERC165Checker for address`; to include this library
  5. * https://eips.ethereum.org/EIPS/eip-165
  6. */
  7. library ERC165Checker {
  8. // As per the EIP-165 spec, no interface should ever match 0xffffffff
  9. bytes4 private constant _INTERFACE_ID_INVALID = 0xffffffff;
  10. /*
  11. * bytes4(keccak256('supportsInterface(bytes4)')) == 0x01ffc9a7
  12. */
  13. bytes4 private constant _INTERFACE_ID_ERC165 = 0x01ffc9a7;
  14. /**
  15. * @notice Query if a contract supports ERC165
  16. * @param account The address of the contract to query for support of ERC165
  17. * @return true if the contract at account implements ERC165
  18. */
  19. function _supportsERC165(address account) internal view returns (bool) {
  20. // Any contract that implements ERC165 must explicitly indicate support of
  21. // InterfaceId_ERC165 and explicitly indicate non-support of InterfaceId_Invalid
  22. return _supportsERC165Interface(account, _INTERFACE_ID_ERC165) &&
  23. !_supportsERC165Interface(account, _INTERFACE_ID_INVALID);
  24. }
  25. /**
  26. * @notice Query if a contract implements an interface, also checks support of ERC165
  27. * @param account The address of the contract to query for support of an interface
  28. * @param interfaceId The interface identifier, as specified in ERC-165
  29. * @return true if the contract at account indicates support of the interface with
  30. * identifier interfaceId, false otherwise
  31. * @dev Interface identification is specified in ERC-165.
  32. */
  33. function _supportsInterface(address account, bytes4 interfaceId) internal view returns (bool) {
  34. // query support of both ERC165 as per the spec and support of _interfaceId
  35. return _supportsERC165(account) &&
  36. _supportsERC165Interface(account, interfaceId);
  37. }
  38. /**
  39. * @notice Query if a contract implements interfaces, also checks support of ERC165
  40. * @param account The address of the contract to query for support of an interface
  41. * @param interfaceIds A list of interface identifiers, as specified in ERC-165
  42. * @return true if the contract at account indicates support all interfaces in the
  43. * interfaceIds list, false otherwise
  44. * @dev Interface identification is specified in ERC-165.
  45. */
  46. function _supportsAllInterfaces(address account, bytes4[] memory interfaceIds) internal view returns (bool) {
  47. // query support of ERC165 itself
  48. if (!_supportsERC165(account)) {
  49. return false;
  50. }
  51. // query support of each interface in _interfaceIds
  52. for (uint256 i = 0; i < interfaceIds.length; i++) {
  53. if (!_supportsERC165Interface(account, interfaceIds[i])) {
  54. return false;
  55. }
  56. }
  57. // all interfaces supported
  58. return true;
  59. }
  60. /**
  61. * @notice Query if a contract implements an interface, does not check ERC165 support
  62. * @param account The address of the contract to query for support of an interface
  63. * @param interfaceId The interface identifier, as specified in ERC-165
  64. * @return true if the contract at account indicates support of the interface with
  65. * identifier interfaceId, false otherwise
  66. * @dev Assumes that account contains a contract that supports ERC165, otherwise
  67. * the behavior of this method is undefined. This precondition can be checked
  68. * with the `supportsERC165` method in this library.
  69. * Interface identification is specified in ERC-165.
  70. */
  71. function _supportsERC165Interface(address account, bytes4 interfaceId) private view returns (bool) {
  72. // success determines whether the staticcall succeeded and result determines
  73. // whether the contract at account indicates support of _interfaceId
  74. (bool success, bool result) = _callERC165SupportsInterface(account, interfaceId);
  75. return (success && result);
  76. }
  77. /**
  78. * @notice Calls the function with selector 0x01ffc9a7 (ERC165) and suppresses throw
  79. * @param account The address of the contract to query for support of an interface
  80. * @param interfaceId The interface identifier, as specified in ERC-165
  81. * @return success true if the STATICCALL succeeded, false otherwise
  82. * @return result true if the STATICCALL succeeded and the contract at account
  83. * indicates support of the interface with identifier interfaceId, false otherwise
  84. */
  85. function _callERC165SupportsInterface(address account, bytes4 interfaceId)
  86. private
  87. view
  88. returns (bool success, bool result)
  89. {
  90. bytes memory encodedParams = abi.encodeWithSelector(_INTERFACE_ID_ERC165, interfaceId);
  91. // solhint-disable-next-line no-inline-assembly
  92. assembly {
  93. let encodedParams_data := add(0x20, encodedParams)
  94. let encodedParams_size := mload(encodedParams)
  95. let output := mload(0x40) // Find empty storage location using "free memory pointer"
  96. mstore(output, 0x0)
  97. success := staticcall(
  98. 30000, // 30k gas
  99. account, // To addr
  100. encodedParams_data,
  101. encodedParams_size,
  102. output,
  103. 0x20 // Outputs are 32 bytes long
  104. )
  105. result := mload(output) // Load the result
  106. }
  107. }
  108. }