github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/contracts/ens/contract/ens.sol (about)

     1  // Ethereum Name Service contracts by Nick Johnson <nick@ethereum.org>
     2  // 
     3  // To the extent possible under law, the person who associated CC0 with
     4  // ENS contracts has waived all copyright and related or neighboring rights
     5  // to ENS.
     6  // 
     7  // You should have received a copy of the CC0 legalcode along with this
     8  // work.  If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
     9  
    10  /**
    11   * The ENS registry contract.
    12   */
    13  contract ENS {
    14      struct Record {
    15          address owner;
    16          address resolver;
    17      }
    18      
    19      mapping(bytes32=>Record) records;
    20      
    21      // Logged when the owner of a node assigns a new owner to a subnode.
    22      event NewOwner(bytes32 indexed node, bytes32 indexed label, address owner);
    23  
    24      // Logged when the owner of a node transfers ownership to a new account.
    25      event Transfer(bytes32 indexed node, address owner);
    26  
    27      // Logged when the owner of a node changes the resolver for that node.
    28      event NewResolver(bytes32 indexed node, address resolver);
    29      
    30      // Permits modifications only by the owner of the specified node.
    31      modifier only_owner(bytes32 node) {
    32          if(records[node].owner != msg.sender) throw;
    33          _
    34      }
    35      
    36      /**
    37       * Constructs a new ENS registrar, with the provided address as the owner of the root node.
    38       */
    39      function ENS(address owner) {
    40          records[0].owner = owner;
    41      }
    42      
    43      /**
    44       * Returns the address that owns the specified node.
    45       */
    46      function owner(bytes32 node) constant returns (address) {
    47          return records[node].owner;
    48      }
    49      
    50      /**
    51       * Returns the address of the resolver for the specified node.
    52       */
    53      function resolver(bytes32 node) constant returns (address) {
    54          return records[node].resolver;
    55      }
    56  
    57      /**
    58       * Transfers ownership of a node to a new address. May only be called by the current
    59       * owner of the node.
    60       * @param node The node to transfer ownership of.
    61       * @param owner The address of the new owner.
    62       */
    63      function setOwner(bytes32 node, address owner) only_owner(node) {
    64          Transfer(node, owner);
    65          records[node].owner = owner;
    66      }
    67  
    68      /**
    69       * Transfers ownership of a subnode sha3(node, label) to a new address. May only be
    70       * called by the owner of the parent node.
    71       * @param node The parent node.
    72       * @param label The hash of the label specifying the subnode.
    73       * @param owner The address of the new owner.
    74       */
    75      function setSubnodeOwner(bytes32 node, bytes32 label, address owner) only_owner(node) {
    76          var subnode = sha3(node, label);
    77          NewOwner(node, label, owner);
    78          records[subnode].owner = owner;
    79      }
    80  
    81      /**
    82       * Sets the resolver address for the specified node.
    83       * @param node The node to update.
    84       * @param resolver The address of the resolver.
    85       */
    86      function setResolver(bytes32 node, address resolver) only_owner(node) {
    87          NewResolver(node, resolver);
    88          records[node].resolver = resolver;
    89      }
    90  }
    91  
    92  /**
    93   * A registrar that allocates subdomains to the first person to claim them. It also deploys
    94   * a simple resolver contract and sets that as the default resolver on new names for
    95   * convenience.
    96   */
    97  contract FIFSRegistrar {
    98      ENS ens;
    99      PublicResolver defaultResolver;
   100      bytes32 rootNode;
   101      
   102      /**
   103       * Constructor.
   104       * @param ensAddr The address of the ENS registry.
   105       * @param node The node that this registrar administers.
   106       */
   107      function FIFSRegistrar(address ensAddr, bytes32 node) {
   108          ens = ENS(ensAddr);
   109          defaultResolver = new PublicResolver(ensAddr);
   110          rootNode = node;
   111      }
   112  
   113      /**
   114       * Register a name, or change the owner of an existing registration.
   115       * @param subnode The hash of the label to register.
   116       * @param owner The address of the new owner.
   117       */    
   118      function register(bytes32 subnode, address owner) {
   119          var node = sha3(rootNode, subnode);
   120          var currentOwner = ens.owner(node);
   121          if(currentOwner != 0 && currentOwner != msg.sender)
   122              throw;
   123  
   124          // Temporarily set ourselves as the owner
   125          ens.setSubnodeOwner(rootNode, subnode, this);
   126          // Set up the default resolver
   127          ens.setResolver(node, defaultResolver);
   128          // Set the owner to the real owner
   129          ens.setOwner(node, owner);
   130      }
   131  }
   132  
   133  contract Resolver {
   134      event AddrChanged(bytes32 indexed node, address a);
   135      event ContentChanged(bytes32 indexed node, bytes32 hash);
   136  
   137      function has(bytes32 node, bytes32 kind) returns (bool);
   138      function addr(bytes32 node) constant returns (address ret);
   139      function content(bytes32 node) constant returns (bytes32 ret);
   140  }
   141  
   142  /**
   143   * A simple resolver anyone can use; only allows the owner of a node to set its
   144   * address.
   145   */
   146  contract PublicResolver is Resolver {
   147      ENS ens;
   148      mapping(bytes32=>address) addresses;
   149      mapping(bytes32=>bytes32) contents;
   150      
   151      modifier only_owner(bytes32 node) {
   152          if(ens.owner(node) != msg.sender) throw;
   153          _
   154      }
   155  
   156      /**
   157       * Constructor.
   158       * @param ensAddr The ENS registrar contract.
   159       */
   160      function PublicResolver(address ensAddr) {
   161          ens = ENS(ensAddr);
   162      }
   163  
   164      /**
   165       * Fallback function.
   166       */
   167      function() {
   168          throw;
   169      }
   170  
   171      /**
   172       * Returns true if the specified node has the specified record type.
   173       * @param node The ENS node to query.
   174       * @param kind The record type name, as specified in EIP137.
   175       * @return True if this resolver has a record of the provided type on the
   176       *         provided node.
   177       */
   178      function has(bytes32 node, bytes32 kind) returns (bool) {
   179          return (kind == "addr" && addresses[node] != 0) ||
   180                 (kind == "content" && contents[node] != 0);
   181      }
   182      
   183      /**
   184       * Returns the address associated with an ENS node.
   185       * @param node The ENS node to query.
   186       * @return The associated address.
   187       */
   188      function addr(bytes32 node) constant returns (address ret) {
   189          ret = addresses[node];
   190          if(ret == 0)
   191              throw;
   192      }
   193      
   194      /**
   195       * Returns the content hash associated with an ENS node.
   196       * @param node The ENS node to query.
   197       * @return The associated content hash.
   198       */
   199      function content(bytes32 node) constant returns (bytes32 ret) {
   200          ret = contents[node];
   201          if(ret == 0)
   202              throw;
   203      }
   204  
   205      /**
   206       * Sets the address associated with an ENS node.
   207       * May only be called by the owner of that node in the ENS registry.
   208       * @param node The node to update.
   209       * @param addr The address to set.
   210       */
   211      function setAddr(bytes32 node, address addr) only_owner(node) {
   212          addresses[node] = addr;
   213          AddrChanged(node, addr);
   214      }
   215      
   216      /**
   217       * Sets the content hash associated with an ENS node.
   218       * May only be called by the owner of that node in the ENS registry.
   219       * @param node The node to update.
   220       * @param hash The content hash to set.
   221       */
   222      function setContent(bytes32 node, bytes32 hash) only_owner(node) {
   223          contents[node] = hash;
   224          ContentChanged(node, hash);
   225      }
   226  }