ERC165, ERC721, ERC1155 Interface

4e5ung·2022년 9월 28일
0

Interface

ERC721, ERC1155 등 표준 Interface가 정해져 있으며,

다음과 같은 방법을 통해 각 Interface를 얻은 후 XOR 연산을 통해 ERC721, ERC1155 의 Interface 값을 얻을 수 있다.

bytes4(keccak256('balanceOf(address)')) == 0x70a08231
bytes4(keccak256('ownerOf(uint256)')) == 0x6352211e
bytes4(keccak256('approve(address,uint256)')) == 0x095ea7b3
bytes4(keccak256('getApproved(uint256)')) == 0x081812fc
bytes4(keccak256('setApprovalForAll(address,bool)')) == 0xa22cb465
bytes4(keccak256('isApprovedForAll(address,address)')) == 0xe985e9c5
bytes4(keccak256('transferFrom(address,address,uint256)')) == 0x23b872dd
bytes4(keccak256('safeTransferFrom(address,address,uint256)')) == 0x42842e0e
bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)')) == 0xb88d4fde

=> 0x70a08231 ^ 0x6352211e ^ 0x095ea7b3 ^ 0x081812fc ^
0xa22cb465 ^ 0xe985e9c ^ 0x23b872dd ^ 0x42842e0e ^ 0xb88d4fde == 0x80ac58cd

ERC165

Interface 검증을 할 수 있는 함수를 제공, ERC721, ERC1155 등 ERC165를 상속받아 사용한다.

supportsInterface 

Interface 검증

Interface 등록

  • ERC721 realease-V4.0 (solidity ^0.8.0) 이전
    bytes4 private constant _INTERFACE_ID_ERC721 = 0x80ac58cd;

    constructor () public {
        // register the supported interfaces to conform to ERC721 via ERC165
        _registerInterface(_INTERFACE_ID_ERC721);
    }
  • ERC721 realease-V4.0 (solidity ^0.8.0) 이후

    별도의 Interface 등록 없이 검증 단계에서 확인

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v2.3.0/contracts/token/ERC721/ERC721.sol

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v4.0/contracts/token/ERC721/ERC721.sol

Interface 검증

  • ERC721 realease-V4.0 (solidity ^0.8.0) 이전
    function supportsInterface(bytes4 interfaceId) external view returns (bool) {
        return _supportedInterfaces[interfaceId];
    }
  • ERC721 realease-V4.0 (solidity ^0.8.0) 이후

    type(IERC721).interfaceId 통해 등록없이 확인

    function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
        return interfaceId == type(IERC721).interfaceId
            || interfaceId == type(IERC721Metadata).interfaceId
            || super.supportsInterface(interfaceId);
    }

InterfaceID 구분

supportsInterface 함수를 통해 구분하며, 제공하는 인터페이스일 경우 True 반환

ERC721 Interface

InterfaceID 0x80ac58cd

await nftContract.supportsInterface('0x80ac58cd')

ERC1155 Interface

InterfaceID 0xd9b67a26

await nft1155Contract.supportsInterface('0xd9b67a26')

0개의 댓글