|
| 1 | +/** |
| 2 | + * The SimpleExtendedResolver is really only meant to test the validation of the parent ens domain |
| 3 | + * `extended-resolver.eth` and, separately, the subdomains of this parent domain. We then "resolve" |
| 4 | + * to arbitrary addresses 0x000000000000000000000000000000000000dEaD for subdomain validations and |
| 5 | + * 0x000000000000000000000000000000000000bEEF for the parent domain validation so that we can be |
| 6 | + * sure each case was validated via the appropriate logic via the `resolve()` function of the contract. |
| 7 | + */ |
| 8 | + |
| 9 | + |
| 10 | +pragma solidity >=0.4.24; |
| 11 | + |
| 12 | +interface ENS { |
| 13 | + |
| 14 | + // Logged when the owner of a node assigns a new owner to a subnode. |
| 15 | + event NewOwner(bytes32 node, bytes32 label, address owner); |
| 16 | + |
| 17 | + // Logged when the owner of a node transfers ownership to a new account. |
| 18 | + event Transfer(bytes32 node, address owner); |
| 19 | + |
| 20 | + // Logged when the resolver for a node changes. |
| 21 | + event NewResolver(bytes32 node, address resolver); |
| 22 | + |
| 23 | + // Logged when the TTL of a node changes |
| 24 | + event NewTTL(bytes32 node, uint64 ttl); |
| 25 | + |
| 26 | + |
| 27 | + function setSubnodeOwner(bytes32 node, bytes32 label, address owner) external; |
| 28 | + function setResolver(bytes32 node, address resolver) external; |
| 29 | + function setOwner(bytes32 node, address owner) external; |
| 30 | + function setTTL(bytes32 node, uint64 ttl) external; |
| 31 | + function owner(bytes32 node) external view returns (address); |
| 32 | + function resolver(bytes32 node) external view returns (address); |
| 33 | + function ttl(bytes32 node) external view returns (uint64); |
| 34 | +} |
| 35 | + |
| 36 | +pragma solidity >= 0.7.0; |
| 37 | + |
| 38 | +abstract contract ResolverBase { |
| 39 | + bytes4 private constant INTERFACE_META_ID = 0x01ffc9a7; |
| 40 | + |
| 41 | + function supportsInterface(bytes4 interfaceID) virtual public pure returns(bool) { |
| 42 | + return interfaceID == INTERFACE_META_ID; |
| 43 | + } |
| 44 | + |
| 45 | + function isAuthorised(bytes32 node) internal virtual view returns(bool); |
| 46 | + |
| 47 | + modifier authorised(bytes32 node) { |
| 48 | + require(isAuthorised(node)); |
| 49 | + _; |
| 50 | + } |
| 51 | + |
| 52 | + function bytesToAddress(bytes memory b) internal pure returns(address payable a) { |
| 53 | + require(b.length == 20); |
| 54 | + assembly { |
| 55 | + a := div(mload(add(b, 32)), exp(256, 12)) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + function addressToBytes(address a) internal pure returns(bytes memory b) { |
| 60 | + b = new bytes(20); |
| 61 | + assembly { |
| 62 | + mstore(add(b, 32), mul(a, exp(256, 12))) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +contract ExtendedResolver is ResolverBase { |
| 68 | + ENS ens; |
| 69 | + |
| 70 | + bytes4 constant private EXTENDED_RESOLVER_INTERFACE_ID = 0x9061b923; |
| 71 | + string constant extendedResolverParentDomain = "\x11extended-resolver\x03eth\x00"; |
| 72 | + bytes32 constant extendedResolverNamehash = 0xf0a378cc2afe91730d0105e67d6bb037cc5b8b6bfec5b5962d9b637ff6497e55; |
| 73 | + |
| 74 | + /** |
| 75 | + * A mapping of authorisations. An address that is authorised for a name |
| 76 | + * may make any changes to the name that the owner could, but may not update |
| 77 | + * the set of authorisations. |
| 78 | + * (node, owner, caller) => isAuthorised |
| 79 | + */ |
| 80 | + mapping(bytes32=>mapping(address=>mapping(address=>bool))) public authorisations; |
| 81 | + |
| 82 | + event AuthorisationChanged(bytes32 node, address owner, address target, bool isAuthorised); |
| 83 | + |
| 84 | + constructor(ENS _ens) public { |
| 85 | + ens = _ens; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @dev Sets or clears an authorisation. |
| 90 | + * Authorisations are specific to the caller. Any account can set an authorisation |
| 91 | + * for any name, but the authorisation that is checked will be that of the |
| 92 | + * current owner of a name. Thus, transferring a name effectively clears any |
| 93 | + * existing authorisations, and new authorisations can be set in advance of |
| 94 | + * an ownership transfer if desired. |
| 95 | + * |
| 96 | + * @param node The name to change the authorisation on. |
| 97 | + * @param target The address that is to be authorised or deauthorised. |
| 98 | + * @param isAuthorised True if the address should be authorised, or false if it should be deauthorised. |
| 99 | + */ |
| 100 | + function setAuthorisation(bytes32 node, address target, bool isAuthorised) external { |
| 101 | + authorisations[node][msg.sender][target] = isAuthorised; |
| 102 | + emit AuthorisationChanged(node, msg.sender, target, isAuthorised); |
| 103 | + } |
| 104 | + |
| 105 | + function isAuthorised(bytes32 node) override internal view returns(bool) { |
| 106 | + address owner = ens.owner(node); |
| 107 | + return owner == msg.sender || authorisations[node][owner][msg.sender]; |
| 108 | + } |
| 109 | + |
| 110 | + function supportsInterface(bytes4 interfaceID) override public pure returns(bool) { |
| 111 | + return interfaceID == EXTENDED_RESOLVER_INTERFACE_ID || super.supportsInterface(interfaceID); |
| 112 | + } |
| 113 | + |
| 114 | + // Simple resolve method solely used to test ENSIP-10 / Wildcard Resolution functionality |
| 115 | + function resolve(bytes calldata dnsName, bytes calldata data) external view returns (bytes memory) { |
| 116 | + // validate 'extended-resolver.eth' parent domain |
| 117 | + if (keccak256(dnsName) == keccak256(bytes(extendedResolverParentDomain)) && data.length >= 36) { |
| 118 | + require(bytes32(data[4:36]) == extendedResolverNamehash, "parent domain not validated appropriately"); |
| 119 | + return abi.encode(address(0x000000000000000000000000000000000000bEEF)); |
| 120 | + } else { |
| 121 | + uint length = uint8(dnsName[0]); |
| 122 | + // validate children of 'extended-resolver.eth' parent domain |
| 123 | + require(keccak256(dnsName[1 + length:]) == keccak256(bytes(extendedResolverParentDomain)), "subdomain not validated appropriately"); |
| 124 | + return abi.encode(address(0x000000000000000000000000000000000000dEaD)); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments