github.com/daeglee/go-ethereum@v0.0.0-20190504220456-cad3e8d18e9b/contracts/ens/contract/PublicResolver.sol (about) 1 pragma solidity >=0.4.25; 2 3 import "./ENS.sol"; 4 5 /** 6 * A simple resolver anyone can use; only allows the owner of a node to set its 7 * address. 8 */ 9 contract PublicResolver { 10 11 bytes4 constant INTERFACE_META_ID = 0x01ffc9a7; 12 bytes4 constant ADDR_INTERFACE_ID = 0x3b3b57de; 13 bytes4 constant NAME_INTERFACE_ID = 0x691f3431; 14 bytes4 constant ABI_INTERFACE_ID = 0x2203ab56; 15 bytes4 constant PUBKEY_INTERFACE_ID = 0xc8690233; 16 bytes4 constant TEXT_INTERFACE_ID = 0x59d1d43c; 17 bytes4 constant CONTENTHASH_INTERFACE_ID = 0xbc1c58d1; 18 19 event AddrChanged(bytes32 indexed node, address a); 20 event NameChanged(bytes32 indexed node, string name); 21 event ABIChanged(bytes32 indexed node, uint256 indexed contentType); 22 event PubkeyChanged(bytes32 indexed node, bytes32 x, bytes32 y); 23 event TextChanged(bytes32 indexed node, string indexedKey, string key); 24 event ContenthashChanged(bytes32 indexed node, bytes hash); 25 26 struct PublicKey { 27 bytes32 x; 28 bytes32 y; 29 } 30 31 struct Record { 32 address addr; 33 string name; 34 PublicKey pubkey; 35 mapping(string=>string) text; 36 mapping(uint256=>bytes) abis; 37 bytes contenthash; 38 } 39 40 ENS ens; 41 42 mapping (bytes32 => Record) records; 43 44 modifier onlyOwner(bytes32 node) { 45 require(ens.owner(node) == msg.sender); 46 _; 47 } 48 49 /** 50 * Constructor. 51 * @param ensAddr The ENS registrar contract. 52 */ 53 constructor(ENS ensAddr) public { 54 ens = ensAddr; 55 } 56 57 /** 58 * Sets the address associated with an ENS node. 59 * May only be called by the owner of that node in the ENS registry. 60 * @param node The node to update. 61 * @param addr The address to set. 62 */ 63 function setAddr(bytes32 node, address addr) external onlyOwner(node) { 64 records[node].addr = addr; 65 emit AddrChanged(node, addr); 66 } 67 68 /** 69 * Sets the contenthash associated with an ENS node. 70 * May only be called by the owner of that node in the ENS registry. 71 * @param node The node to update. 72 * @param hash The contenthash to set 73 */ 74 function setContenthash(bytes32 node, bytes calldata hash) external onlyOwner(node) { 75 records[node].contenthash = hash; 76 emit ContenthashChanged(node, hash); 77 } 78 79 /** 80 * Sets the name associated with an ENS node, for reverse records. 81 * May only be called by the owner of that node in the ENS registry. 82 * @param node The node to update. 83 * @param name The name to set. 84 */ 85 function setName(bytes32 node, string calldata name) external onlyOwner(node) { 86 records[node].name = name; 87 emit NameChanged(node, name); 88 } 89 90 /** 91 * Sets the ABI associated with an ENS node. 92 * Nodes may have one ABI of each content type. To remove an ABI, set it to 93 * the empty string. 94 * @param node The node to update. 95 * @param contentType The content type of the ABI 96 * @param data The ABI data. 97 */ 98 function setABI(bytes32 node, uint256 contentType, bytes calldata data) external onlyOwner(node) { 99 // Content types must be powers of 2 100 require(((contentType - 1) & contentType) == 0); 101 102 records[node].abis[contentType] = data; 103 emit ABIChanged(node, contentType); 104 } 105 106 /** 107 * Sets the SECP256k1 public key associated with an ENS node. 108 * @param node The ENS node to query 109 * @param x the X coordinate of the curve point for the public key. 110 * @param y the Y coordinate of the curve point for the public key. 111 */ 112 function setPubkey(bytes32 node, bytes32 x, bytes32 y) external onlyOwner(node) { 113 records[node].pubkey = PublicKey(x, y); 114 emit PubkeyChanged(node, x, y); 115 } 116 117 /** 118 * Sets the text data associated with an ENS node and key. 119 * May only be called by the owner of that node in the ENS registry. 120 * @param node The node to update. 121 * @param key The key to set. 122 * @param value The text data value to set. 123 */ 124 function setText(bytes32 node, string calldata key, string calldata value) external onlyOwner(node) { 125 records[node].text[key] = value; 126 emit TextChanged(node, key, key); 127 } 128 129 /** 130 * Returns the text data associated with an ENS node and key. 131 * @param node The ENS node to query. 132 * @param key The text data key to query. 133 * @return The associated text data. 134 */ 135 function text(bytes32 node, string calldata key) external view returns (string memory) { 136 return records[node].text[key]; 137 } 138 139 /** 140 * Returns the SECP256k1 public key associated with an ENS node. 141 * Defined in EIP 619. 142 * @param node The ENS node to query 143 * @return x, y the X and Y coordinates of the curve point for the public key. 144 */ 145 function pubkey(bytes32 node) external view returns (bytes32 x, bytes32 y) { 146 return (records[node].pubkey.x, records[node].pubkey.y); 147 } 148 149 /** 150 * Returns the ABI associated with an ENS node. 151 * Defined in EIP205. 152 * @param node The ENS node to query 153 * @param contentTypes A bitwise OR of the ABI formats accepted by the caller. 154 * @return contentType The content type of the return value 155 * @return data The ABI data 156 */ 157 function ABI(bytes32 node, uint256 contentTypes) external view returns (uint256, bytes memory) { 158 Record storage record = records[node]; 159 160 for (uint256 contentType = 1; contentType <= contentTypes; contentType <<= 1) { 161 if ((contentType & contentTypes) != 0 && record.abis[contentType].length > 0) { 162 return (contentType, record.abis[contentType]); 163 } 164 } 165 166 bytes memory empty; 167 return (0, empty); 168 } 169 170 /** 171 * Returns the name associated with an ENS node, for reverse records. 172 * Defined in EIP181. 173 * @param node The ENS node to query. 174 * @return The associated name. 175 */ 176 function name(bytes32 node) external view returns (string memory) { 177 return records[node].name; 178 } 179 180 /** 181 * Returns the address associated with an ENS node. 182 * @param node The ENS node to query. 183 * @return The associated address. 184 */ 185 function addr(bytes32 node) external view returns (address) { 186 return records[node].addr; 187 } 188 189 /** 190 * Returns the contenthash associated with an ENS node. 191 * @param node The ENS node to query. 192 * @return The associated contenthash. 193 */ 194 function contenthash(bytes32 node) external view returns (bytes memory) { 195 return records[node].contenthash; 196 } 197 198 /** 199 * Returns true if the resolver implements the interface specified by the provided hash. 200 * @param interfaceID The ID of the interface to check for. 201 * @return True if the contract implements the requested interface. 202 */ 203 function supportsInterface(bytes4 interfaceID) external pure returns (bool) { 204 return interfaceID == ADDR_INTERFACE_ID || 205 interfaceID == NAME_INTERFACE_ID || 206 interfaceID == ABI_INTERFACE_ID || 207 interfaceID == PUBKEY_INTERFACE_ID || 208 interfaceID == TEXT_INTERFACE_ID || 209 interfaceID == CONTENTHASH_INTERFACE_ID || 210 interfaceID == INTERFACE_META_ID; 211 } 212 }