Line data Source code
1 : // SPDX-License-Identifier: MIT 2 : 3 : pragma solidity ^0.6.0; 4 : 5 : /* 6 : * @dev Solidity implementation of the ENS namehash algorithm. 7 : * 8 : * Warning! Does not normalize or validate names before hashing. 9 : * Original version can be found here https://github.com/JonahGroendal/ens-namehash/ 10 : */ 11 : library ENSNamehash { 12 : function namehash(bytes memory domain) internal pure returns (bytes32) { 13 0 : return namehash(domain, 0); 14 : } 15 : 16 : function namehash(bytes memory domain, uint256 i) internal pure returns (bytes32) { 17 0 : if (domain.length <= i) return 0x0000000000000000000000000000000000000000000000000000000000000000; 18 : 19 0 : uint256 len = labelLength(domain, i); 20 : 21 0 : return keccak256(abi.encodePacked(namehash(domain, i + len + 1), keccak(domain, i, len))); 22 : } 23 : 24 : function labelLength(bytes memory domain, uint256 i) private pure returns (uint256) { 25 0 : uint256 len; 26 0 : while (i + len != domain.length && domain[i + len] != 0x2e) { 27 0 : len++; 28 : } 29 0 : return len; 30 : } 31 : 32 : function keccak( 33 : bytes memory data, 34 : uint256 offset, 35 : uint256 len 36 : ) private pure returns (bytes32 ret) { 37 0 : require(offset + len <= data.length); 38 : assembly { 39 0 : ret := keccak256(add(add(data, 32), offset), len) 40 : } 41 : } 42 : }