PublicResolver.sol 7.1 KB

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