PublicResolver.sol 7.2 KB

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