139 lines
3.8 KiB
Solidity
139 lines
3.8 KiB
Solidity
pragma solidity 0.4.24;
|
|
|
|
/**
|
|
* The ENS registry contract.
|
|
*/
|
|
contract ENSMock {
|
|
// Logged when the ownership of a node is transferred to a new account.
|
|
event Transfer(bytes32 indexed node, address owner);
|
|
|
|
// Logged when the resolver for a node changes.
|
|
event NewResolver(bytes32 indexed node, address resolver);
|
|
|
|
// Logged when the TTL of a node changes
|
|
event NewTTL(bytes32 indexed node, uint64 ttl);
|
|
|
|
struct Record {
|
|
address owner;
|
|
address resolver;
|
|
uint64 ttl;
|
|
}
|
|
|
|
mapping(bytes32 => Record) public records;
|
|
|
|
/**
|
|
* @dev Constructs a new ENS registrar.
|
|
*/
|
|
constructor() public {
|
|
records[0x0].owner = msg.sender;
|
|
}
|
|
|
|
/**
|
|
* @dev Sets the record for a node.
|
|
* @param node The node to update.
|
|
* @param owner The address of the new owner.
|
|
* @param resolver The address of the resolver.
|
|
* @param ttl The TTL in seconds.
|
|
*/
|
|
function setRecord(
|
|
bytes32 node,
|
|
address owner,
|
|
address resolver,
|
|
uint64 ttl
|
|
) external {
|
|
setOwner(node, owner);
|
|
_setResolverAndTTL(node, resolver, ttl);
|
|
}
|
|
|
|
/**
|
|
* @dev Transfers ownership of a node to a new address. May only be called by the current owner of the node.
|
|
* @param node The node to transfer ownership of.
|
|
* @param owner The address of the new owner.
|
|
*/
|
|
function setOwner(bytes32 node, address owner) public {
|
|
_setOwner(node, owner);
|
|
emit Transfer(node, owner);
|
|
}
|
|
|
|
/**
|
|
* @dev Sets the resolver address for the specified node.
|
|
* @param node The node to update.
|
|
* @param resolver The address of the resolver.
|
|
*/
|
|
function setResolver(bytes32 node, address resolver) public {
|
|
emit NewResolver(node, resolver);
|
|
records[node].resolver = resolver;
|
|
}
|
|
|
|
/**
|
|
* @dev Sets the TTL for the specified node.
|
|
* @param node The node to update.
|
|
* @param ttl The TTL in seconds.
|
|
*/
|
|
function setTTL(bytes32 node, uint64 ttl) public {
|
|
emit NewTTL(node, ttl);
|
|
records[node].ttl = ttl;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the address that owns the specified node.
|
|
* @param node The specified node.
|
|
* @return address of the owner.
|
|
*/
|
|
function owner(bytes32 node) public view returns (address) {
|
|
address addr = records[node].owner;
|
|
if (addr == address(this)) {
|
|
return address(0x0);
|
|
}
|
|
|
|
return addr;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the address of the resolver for the specified node.
|
|
* @param node The specified node.
|
|
* @return address of the resolver.
|
|
*/
|
|
function resolver(bytes32 node) public view returns (address) {
|
|
return records[node].resolver;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the TTL of a node, and any records associated with it.
|
|
* @param node The specified node.
|
|
* @return ttl of the node.
|
|
*/
|
|
function ttl(bytes32 node) public view returns (uint64) {
|
|
return records[node].ttl;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns whether a record has been imported to the registry.
|
|
* @param node The specified node.
|
|
* @return Bool if record exists
|
|
*/
|
|
function recordExists(bytes32 node) public view returns (bool) {
|
|
return records[node].owner != address(0x0);
|
|
}
|
|
|
|
function _setOwner(bytes32 node, address owner) internal {
|
|
records[node].owner = owner;
|
|
}
|
|
|
|
function _setResolverAndTTL(
|
|
bytes32 node,
|
|
address resolver,
|
|
uint64 ttl
|
|
) internal {
|
|
if (resolver != records[node].resolver) {
|
|
records[node].resolver = resolver;
|
|
emit NewResolver(node, resolver);
|
|
}
|
|
|
|
if (ttl != records[node].ttl) {
|
|
records[node].ttl = ttl;
|
|
emit NewTTL(node, ttl);
|
|
}
|
|
}
|
|
}
|