2018-04-23 09:17:35 +00:00
|
|
|
pragma solidity ^0.4.23;
|
2018-01-09 17:07:58 +00:00
|
|
|
|
2018-03-24 03:40:03 +00:00
|
|
|
import "../common/Controlled.sol";
|
2018-04-23 09:17:35 +00:00
|
|
|
import "../token/ERC20Token.sol";
|
2018-05-01 14:58:05 +00:00
|
|
|
import "../ens/ENS.sol";
|
2018-05-02 22:59:05 +00:00
|
|
|
import "../ens/PublicResolver.sol";
|
2018-04-23 09:17:35 +00:00
|
|
|
|
2018-05-03 20:29:33 +00:00
|
|
|
/**
|
|
|
|
* @author Ricardo Guilherme Schmidt (Status Research & Development GmbH)
|
|
|
|
* @notice Sell ENS subdomains of owned domains.
|
|
|
|
*/
|
|
|
|
contract ENSSubdomainRegistry is Controlled {
|
|
|
|
|
|
|
|
ERC20Token public token;
|
|
|
|
ENS public ens;
|
|
|
|
PublicResolver public resolver;
|
2018-05-06 14:21:40 +00:00
|
|
|
address public parentRegistry;
|
|
|
|
|
2018-05-03 20:29:33 +00:00
|
|
|
uint256 public releaseDelay = 1 years;
|
|
|
|
mapping (bytes32 => Domain) public domains;
|
|
|
|
mapping (bytes32 => Account) public accounts;
|
|
|
|
|
2018-05-16 22:14:59 +00:00
|
|
|
event FundsOwner(bytes32 indexed subdomainhash, address fundsOwner);
|
2018-05-14 20:58:31 +00:00
|
|
|
event DomainPrice(bytes32 indexed namehash, uint256 price);
|
|
|
|
event DomainMoved(bytes32 indexed namehash, address newRegistry);
|
|
|
|
|
2018-05-07 03:37:56 +00:00
|
|
|
enum NodeState { Free, Owned, Moved }
|
2018-03-24 03:40:03 +00:00
|
|
|
struct Domain {
|
2018-05-07 03:37:56 +00:00
|
|
|
NodeState state;
|
2018-03-24 03:40:03 +00:00
|
|
|
uint256 price;
|
2018-01-10 01:19:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 20:29:33 +00:00
|
|
|
struct Account {
|
|
|
|
uint256 tokenBalance;
|
|
|
|
uint256 creationTime;
|
2018-05-16 22:14:59 +00:00
|
|
|
address fundsOwner;
|
2018-05-03 20:29:33 +00:00
|
|
|
}
|
2018-03-24 03:40:03 +00:00
|
|
|
|
2018-05-16 22:22:54 +00:00
|
|
|
modifier onlyParentRegistry {
|
|
|
|
require(msg.sender == parentRegistry);
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2018-05-03 20:29:33 +00:00
|
|
|
/**
|
|
|
|
* @notice Initializes a UserRegistry contract
|
|
|
|
* @param _token fee token base
|
|
|
|
* @param _ens Ethereum Name Service root address
|
|
|
|
* @param _resolver Default resolver to use in initial settings
|
2018-05-06 14:21:40 +00:00
|
|
|
* @param _parentRegistry Address of old registry (if any) for account migration.
|
2018-05-03 20:29:33 +00:00
|
|
|
*/
|
2018-04-23 09:17:35 +00:00
|
|
|
constructor(
|
2018-05-03 20:29:33 +00:00
|
|
|
ERC20Token _token,
|
|
|
|
ENS _ens,
|
2018-05-06 14:21:40 +00:00
|
|
|
PublicResolver _resolver,
|
|
|
|
address _parentRegistry
|
2018-03-24 03:40:03 +00:00
|
|
|
)
|
|
|
|
public
|
|
|
|
{
|
2018-05-03 20:29:33 +00:00
|
|
|
token = _token;
|
|
|
|
ens = _ens;
|
|
|
|
resolver = _resolver;
|
2018-05-06 14:21:40 +00:00
|
|
|
parentRegistry = _parentRegistry;
|
2018-01-10 01:19:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 20:29:33 +00:00
|
|
|
/**
|
|
|
|
* @notice Registers `_userHash` subdomain to `_domainHash` setting msg.sender as owner.
|
|
|
|
* @param _userHash choosen unowned subdomain hash
|
2018-05-08 02:41:21 +00:00
|
|
|
* @param _domainHash choosen contract owned domain hash
|
2018-05-03 20:29:33 +00:00
|
|
|
* @param _account optional address to set at public resolver
|
|
|
|
* @param _pubkeyA optional pubkey part A to set at public resolver
|
|
|
|
* @param _pubkeyB optional pubkey part B to set at public resolver
|
|
|
|
*/
|
2018-03-24 03:40:03 +00:00
|
|
|
function register(
|
|
|
|
bytes32 _userHash,
|
2018-05-02 22:59:05 +00:00
|
|
|
bytes32 _domainHash,
|
|
|
|
address _account,
|
|
|
|
bytes32 _pubkeyA,
|
|
|
|
bytes32 _pubkeyB
|
2018-03-24 03:40:03 +00:00
|
|
|
)
|
|
|
|
external
|
|
|
|
returns(bytes32 subdomainHash)
|
2018-05-02 22:59:05 +00:00
|
|
|
{
|
2018-03-24 03:40:03 +00:00
|
|
|
Domain memory domain = domains[_domainHash];
|
2018-05-07 03:37:56 +00:00
|
|
|
require(domain.state == NodeState.Owned);
|
2018-05-14 20:58:31 +00:00
|
|
|
subdomainHash = keccak256(_domainHash, _userHash);
|
2018-05-03 20:29:33 +00:00
|
|
|
require(ens.owner(subdomainHash) == address(0));
|
|
|
|
require(accounts[subdomainHash].creationTime == 0);
|
2018-05-06 23:42:06 +00:00
|
|
|
accounts[subdomainHash] = Account(domain.price, block.timestamp, msg.sender);
|
2018-05-14 20:58:31 +00:00
|
|
|
require(domain.price == 0 || token.allowance(msg.sender, address(this)) >= domain.price);
|
2018-05-03 20:37:42 +00:00
|
|
|
|
|
|
|
bool resolvePubkey = _pubkeyA != 0 || _pubkeyB != 0;
|
|
|
|
bool resolveAccount = _account != address(0);
|
2018-05-14 20:58:31 +00:00
|
|
|
if (resolvePubkey || resolveAccount) {
|
2018-05-03 20:37:42 +00:00
|
|
|
//set to self the ownship to setup initial resolver
|
|
|
|
ens.setSubnodeOwner(_domainHash, _userHash, address(this));
|
|
|
|
ens.setResolver(subdomainHash, resolver); //default resolver
|
2018-05-14 20:58:31 +00:00
|
|
|
if (resolveAccount) {
|
2018-05-03 20:37:42 +00:00
|
|
|
resolver.setAddr(subdomainHash, _account);
|
|
|
|
}
|
2018-05-14 20:58:31 +00:00
|
|
|
if (resolvePubkey) {
|
2018-05-03 20:37:42 +00:00
|
|
|
resolver.setPubkey(subdomainHash, _pubkeyA, _pubkeyB);
|
|
|
|
}
|
2018-05-14 20:58:31 +00:00
|
|
|
ens.setOwner(subdomainHash, msg.sender);
|
|
|
|
}else {
|
|
|
|
//transfer ownship of subdone to registrant
|
|
|
|
ens.setSubnodeOwner(_domainHash, _userHash, msg.sender);
|
|
|
|
}
|
|
|
|
if (domain.price > 0) {
|
|
|
|
//get payment
|
|
|
|
require(
|
|
|
|
token.transferFrom(
|
|
|
|
address(msg.sender),
|
|
|
|
address(this),
|
|
|
|
domain.price
|
|
|
|
)
|
|
|
|
);
|
2018-05-02 22:59:05 +00:00
|
|
|
}
|
2018-05-03 20:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @notice release subdomain and retrieve locked fee, needs to be called after `releasePeriod` from creation time.
|
|
|
|
* @param _userHash `msg.sender` owned subdomain hash
|
2018-05-08 02:41:21 +00:00
|
|
|
* @param _domainHash choosen contract owned domain hash
|
2018-05-03 20:29:33 +00:00
|
|
|
*/
|
|
|
|
function release(
|
2018-05-02 22:59:05 +00:00
|
|
|
bytes32 _userHash,
|
|
|
|
bytes32 _domainHash
|
2018-05-03 20:29:33 +00:00
|
|
|
)
|
2018-05-02 22:59:05 +00:00
|
|
|
external
|
|
|
|
{
|
2018-05-06 23:42:06 +00:00
|
|
|
bool isDomainController = ens.owner(_domainHash) == address(this);
|
2018-05-14 20:58:31 +00:00
|
|
|
bytes32 subdomainHash = keccak256(_domainHash, _userHash);
|
2018-05-03 20:29:33 +00:00
|
|
|
Account memory account = accounts[subdomainHash];
|
|
|
|
require(account.creationTime > 0);
|
2018-05-06 23:42:06 +00:00
|
|
|
if (isDomainController) {
|
|
|
|
require(msg.sender == ens.owner(subdomainHash));
|
2018-05-15 20:43:22 +00:00
|
|
|
require(account.creationTime + releaseDelay >= block.timestamp);
|
2018-05-06 14:21:40 +00:00
|
|
|
ens.setSubnodeOwner(_domainHash, _userHash, address(this));
|
|
|
|
ens.setResolver(subdomainHash, address(0));
|
2018-05-15 22:35:38 +00:00
|
|
|
ens.setOwner(subdomainHash, address(0));
|
2018-05-06 23:42:06 +00:00
|
|
|
} else {
|
2018-05-16 22:14:59 +00:00
|
|
|
require(msg.sender == account.fundsOwner);
|
2018-05-06 14:21:40 +00:00
|
|
|
}
|
2018-05-06 23:42:06 +00:00
|
|
|
delete accounts[subdomainHash];
|
2018-05-14 20:58:31 +00:00
|
|
|
if (account.tokenBalance > 0) {
|
|
|
|
require(token.transfer(msg.sender, account.tokenBalance));
|
|
|
|
}
|
|
|
|
|
2018-01-09 17:07:58 +00:00
|
|
|
}
|
|
|
|
|
2018-05-06 23:42:06 +00:00
|
|
|
/**
|
2018-05-16 22:14:59 +00:00
|
|
|
* @notice updates funds owner useful to move subdomain account to new registry.
|
2018-05-15 22:37:45 +00:00
|
|
|
* @param _userHash `msg.sender` owned subdomain hash
|
|
|
|
* @param _domainHash choosen contract owned domain hash
|
2018-05-06 23:42:06 +00:00
|
|
|
**/
|
2018-05-16 22:14:59 +00:00
|
|
|
function updateFundsOwner(
|
2018-05-15 22:35:38 +00:00
|
|
|
bytes32 _userHash,
|
|
|
|
bytes32 _domainHash
|
|
|
|
)
|
2018-05-15 22:37:45 +00:00
|
|
|
external
|
2018-05-15 22:35:38 +00:00
|
|
|
{
|
|
|
|
bytes32 subdomainHash = keccak256(_domainHash, _userHash);
|
|
|
|
require(accounts[subdomainHash].creationTime > 0);
|
|
|
|
require(msg.sender == ens.owner(subdomainHash));
|
|
|
|
require(ens.owner(_domainHash) == address(this));
|
2018-05-16 22:14:59 +00:00
|
|
|
accounts[subdomainHash].fundsOwner = msg.sender;
|
|
|
|
emit FundsOwner(subdomainHash, msg.sender);
|
|
|
|
|
2018-03-24 03:40:03 +00:00
|
|
|
}
|
2018-05-06 14:21:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @notice Migrate account to new registry
|
|
|
|
* @param _userHash `msg.sender` owned subdomain hash
|
2018-05-08 02:41:21 +00:00
|
|
|
* @param _domainHash choosen contract owned domain hash
|
2018-05-06 14:21:40 +00:00
|
|
|
**/
|
2018-05-06 22:16:50 +00:00
|
|
|
function moveAccount(
|
2018-05-06 14:21:40 +00:00
|
|
|
bytes32 _userHash,
|
|
|
|
bytes32 _domainHash
|
|
|
|
)
|
|
|
|
external
|
|
|
|
{
|
2018-05-14 20:58:31 +00:00
|
|
|
bytes32 subdomainHash = keccak256(_domainHash, _userHash);
|
2018-05-16 22:14:59 +00:00
|
|
|
require(msg.sender == accounts[subdomainHash].fundsOwner);
|
2018-05-17 13:54:49 +00:00
|
|
|
ENSSubdomainRegistry _newRegistry = ENSSubdomainRegistry(ens.owner(_domainHash));
|
2018-05-06 14:21:40 +00:00
|
|
|
Account memory account = accounts[subdomainHash];
|
|
|
|
delete accounts[subdomainHash];
|
2018-05-17 13:54:49 +00:00
|
|
|
require(address(this) == _newRegistry.parentRegistry());
|
2018-05-06 14:21:40 +00:00
|
|
|
token.approve(_newRegistry, account.tokenBalance);
|
2018-05-16 22:14:59 +00:00
|
|
|
_newRegistry.migrateAccount(_userHash, _domainHash, account.tokenBalance, account.creationTime, account.fundsOwner);
|
2018-05-06 14:21:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-06 22:16:50 +00:00
|
|
|
/**
|
|
|
|
@dev callabe only by parent registry to continue migration of domain
|
|
|
|
*/
|
|
|
|
function migrateDomain(
|
|
|
|
bytes32 _domain,
|
|
|
|
uint256 _price
|
|
|
|
)
|
|
|
|
external
|
2018-05-16 22:22:54 +00:00
|
|
|
onlyParentRegistry
|
2018-05-06 22:16:50 +00:00
|
|
|
{
|
2018-05-07 03:40:29 +00:00
|
|
|
require(domains[_domain].state == NodeState.Free);
|
2018-05-06 22:16:50 +00:00
|
|
|
require(ens.owner(_domain) == address(this));
|
2018-05-07 03:37:56 +00:00
|
|
|
domains[_domain] = Domain(NodeState.Owned, _price);
|
2018-05-06 22:16:50 +00:00
|
|
|
}
|
2018-05-06 14:21:40 +00:00
|
|
|
/**
|
|
|
|
* @dev callable only by parent registry for continue user opt-in migration
|
|
|
|
* @param _userHash any subdomain hash coming from parent
|
2018-05-08 02:41:21 +00:00
|
|
|
* @param _domainHash choosen contract owned domain hash
|
2018-05-06 14:21:40 +00:00
|
|
|
* @param _tokenBalance amount being transferred
|
|
|
|
* @param _creationTime any value coming from parent
|
2018-05-16 22:14:59 +00:00
|
|
|
* @param _fundsOwner fundsOwner for opt-out/release at domain move
|
2018-05-06 14:21:40 +00:00
|
|
|
**/
|
2018-05-06 22:16:50 +00:00
|
|
|
function migrateAccount(
|
2018-05-06 14:21:40 +00:00
|
|
|
bytes32 _userHash,
|
|
|
|
bytes32 _domainHash,
|
|
|
|
uint256 _tokenBalance,
|
2018-05-06 23:42:06 +00:00
|
|
|
uint256 _creationTime,
|
2018-05-16 22:14:59 +00:00
|
|
|
address _fundsOwner
|
2018-05-06 14:21:40 +00:00
|
|
|
)
|
|
|
|
external
|
2018-05-16 22:22:54 +00:00
|
|
|
onlyParentRegistry
|
2018-05-06 14:21:40 +00:00
|
|
|
{
|
2018-05-14 20:58:31 +00:00
|
|
|
bytes32 subdomainHash = keccak256(_domainHash, _userHash);
|
2018-05-16 22:14:59 +00:00
|
|
|
accounts[subdomainHash] = Account(_tokenBalance, _creationTime, _fundsOwner);
|
2018-05-14 20:58:31 +00:00
|
|
|
if (_tokenBalance > 0) {
|
|
|
|
require(token.transferFrom(parentRegistry, address(this), _tokenBalance));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-05-16 22:22:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @notice moves a domain to other Registry (will not move subdomains accounts)
|
|
|
|
* @param _newRegistry new registry hodling this domain
|
|
|
|
* @param _domain domain being moved
|
|
|
|
*/
|
|
|
|
function moveDomain(
|
|
|
|
ENSSubdomainRegistry _newRegistry,
|
|
|
|
bytes32 _domain
|
|
|
|
)
|
|
|
|
external
|
|
|
|
onlyController
|
|
|
|
{
|
|
|
|
require(ens.owner(_domain) == address(this));
|
|
|
|
require(domains[_domain].state == NodeState.Owned);
|
|
|
|
uint256 price = domains[_domain].price;
|
|
|
|
domains[_domain].state = NodeState.Moved;
|
|
|
|
ens.setOwner(_domain, _newRegistry);
|
|
|
|
_newRegistry.migrateDomain(_domain, price);
|
|
|
|
emit DomainMoved(_domain, _newRegistry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @notice Controller include new domain available to register
|
|
|
|
* @param _domain domain owned by user registry being activated
|
|
|
|
* @param _price cost to register subnode from this node
|
|
|
|
*/
|
|
|
|
function addDomain(
|
|
|
|
bytes32 _domain,
|
|
|
|
uint256 _price
|
|
|
|
)
|
|
|
|
external
|
|
|
|
onlyController
|
|
|
|
{
|
2018-05-23 19:52:30 +00:00
|
|
|
require(domains[_domain].state == NodeState.Free, "Domain state is not free");
|
|
|
|
require(ens.owner(_domain) == address(this), "Registry does not own domain");
|
2018-05-16 22:22:54 +00:00
|
|
|
domains[_domain] = Domain(NodeState.Owned, _price);
|
|
|
|
emit DomainPrice(_domain, _price);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @notice updates domain price
|
|
|
|
* @param _domain active domain being defined price
|
|
|
|
* @param _price new price
|
|
|
|
*/
|
|
|
|
function setDomainPrice(
|
|
|
|
bytes32 _domain,
|
|
|
|
uint256 _price
|
|
|
|
)
|
|
|
|
external
|
|
|
|
onlyController
|
|
|
|
{
|
|
|
|
Domain storage domain = domains[_domain];
|
|
|
|
require(domain.state == NodeState.Owned);
|
|
|
|
domain.price = _price;
|
|
|
|
emit DomainPrice(_domain, _price);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @notice updates default public resolver for newly registred subdomains
|
|
|
|
* @param _resolver new default resolver
|
|
|
|
*/
|
|
|
|
function setResolver(
|
|
|
|
address _resolver
|
|
|
|
)
|
|
|
|
external
|
|
|
|
onlyController
|
|
|
|
{
|
|
|
|
resolver = PublicResolver(_resolver);
|
|
|
|
}
|
2018-05-14 20:58:31 +00:00
|
|
|
|
|
|
|
function getPrice(bytes32 _domainHash)
|
|
|
|
external
|
|
|
|
view
|
|
|
|
returns(uint256 subdomainPrice)
|
|
|
|
{
|
|
|
|
subdomainPrice = domains[_domainHash].price;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAccountBalance(bytes32 _subdomainHash)
|
|
|
|
external
|
|
|
|
view
|
|
|
|
returns(uint256 accountBalance)
|
|
|
|
{
|
|
|
|
accountBalance = accounts[_subdomainHash].tokenBalance;
|
|
|
|
}
|
|
|
|
|
2018-05-16 22:14:59 +00:00
|
|
|
function getFundsOwner(bytes32 _subdomainHash)
|
2018-05-14 20:58:31 +00:00
|
|
|
external
|
|
|
|
view
|
2018-05-16 22:14:59 +00:00
|
|
|
returns(address fundsOwner)
|
2018-05-14 20:58:31 +00:00
|
|
|
{
|
2018-05-16 22:14:59 +00:00
|
|
|
fundsOwner = accounts[_subdomainHash].fundsOwner;
|
2018-05-06 14:21:40 +00:00
|
|
|
}
|
2018-05-17 13:54:49 +00:00
|
|
|
|
|
|
|
function getCreationTime(bytes32 _subdomainHash)
|
|
|
|
external
|
|
|
|
view
|
|
|
|
returns(uint256 creationTime)
|
|
|
|
{
|
|
|
|
creationTime = accounts[_subdomainHash].creationTime;
|
|
|
|
}
|
2018-05-03 20:29:33 +00:00
|
|
|
|
2018-05-23 19:52:30 +00:00
|
|
|
}
|