/** * @dev Provides a default implementation of a resolver for reverse records, * which permits only the owner to update it. */ contract DefaultReverseResolver is Resolver { AbstractENS public ens; mapping(bytes32=>string) public name;
/** * @dev Constructor * @param ensAddr The address of the ENS registry. */ functionDefaultReverseResolver(AbstractENS ensAddr){ ens = ensAddr; }
/** * @dev Only permits calls by the reverse registrar. * @param node The node permission is required for. */ modifier owner_only(bytes32 node) { require(msg.sender == ens.owner(node)); _; }
/** * @dev Sets the name for a node. * @param node The node to update. * @param _name The name to set. */ functionsetName(bytes32 node, string _name)publicowner_only(node){ name[node] = _name; } }
AbstractENS public ens; Resolver public defaultResolver;
/** * @dev Constructor * @param ensAddr The address of the ENS registry. * @param resolverAddr The address of the default reverse resolver. */ functionReverseRegistrar(AbstractENS ensAddr, Resolver resolverAddr){ ens = ensAddr; defaultResolver = resolverAddr; }
/** * @dev Transfers ownership of the reverse ENS record associated with the * calling account. * @param owner The address to set as the owner of the reverse record in ENS. * @return The ENS node hash of the reverse record. */ functionclaim(address owner)returns(bytes32 node){ return claimWithResolver(owner, 0); }
/** * @dev Transfers ownership of the reverse ENS record associated with the * calling account. * @param owner The address to set as the owner of the reverse record in ENS. * @param resolver The address of the resolver to set; 0 to leave unchanged. * @return The ENS node hash of the reverse record. */ functionclaimWithResolver(address owner, address resolver)returns(bytes32 node){ var label = sha3HexAddress(msg.sender); node = sha3(ADDR_REVERSE_NODE, label); var currentOwner = ens.owner(node);
// Update the resolver if required if(resolver != 0 && resolver != ens.resolver(node)) { // Transfer the name to us first if it's not already if(currentOwner != address(this)) { ens.setSubnodeOwner(ADDR_REVERSE_NODE, label, this); currentOwner = address(this); } ens.setResolver(node, resolver); }
// Update the owner if required if(currentOwner != owner) { ens.setSubnodeOwner(ADDR_REVERSE_NODE, label, owner); }
return node; }
/** * @dev Sets the `name()` record for the reverse ENS record associated with * the calling account. First updates the resolver to the default reverse * resolver if necessary. * @param name The name to set for this address. * @return The ENS node hash of the reverse record. */ functionsetName(string name)returns(bytes32 node){ node = claimWithResolver(this, defaultResolver); defaultResolver.setName(node, name); return node; }
/** * @dev Returns the node hash for a given account's reverse records. * @param addr The address to hash * @return The ENS node hash. */ functionnode(address addr)constantreturns(bytes32 ret){ return sha3(ADDR_REVERSE_NODE, sha3HexAddress(addr)); }
/** * @dev An optimised function to compute the sha3 of the lower-case * hexadecimal representation of an Ethereum address. * @param addr The address to hash * @return The SHA3 hash of the lower-case hexadecimal encoding of the * input address. */ functionsha3HexAddress(address addr)privatereturns(bytes32 ret){ addr; ret; // Stop warning us about unused variables assembly { let lookup := 0x3031323334353637383961626364656600000000000000000000000000000000 let i := 40 loop: i := sub(i, 1) mstore8(i, byte(and(addr, 0xf), lookup)) addr := div(addr, 0x10) i := sub(i, 1) mstore8(i, byte(and(addr, 0xf), lookup)) addr := div(addr, 0x10) jumpi(loop, i) ret := sha3(0, 40) } } }