From 2d0f34f215ccdd4f35ed7d3f9149dbd88fba838f Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 13 Jul 2018 12:37:34 -0400 Subject: [PATCH] add resolver contract --- lib/modules/ens/contracts/ENSRegistry.sol | 14 +++- lib/modules/ens/contracts/FIFSRegistrar.sol | 18 +++-- lib/modules/ens/contracts/Resolver.sol | 42 ++++++++++++ lib/modules/ens/index.js | 73 ++++++++------------- 4 files changed, 93 insertions(+), 54 deletions(-) create mode 100644 lib/modules/ens/contracts/Resolver.sol diff --git a/lib/modules/ens/contracts/ENSRegistry.sol b/lib/modules/ens/contracts/ENSRegistry.sol index 251c1100..8d11a08f 100644 --- a/lib/modules/ens/contracts/ENSRegistry.sol +++ b/lib/modules/ens/contracts/ENSRegistry.sol @@ -16,7 +16,7 @@ contract ENSRegistry is ENS { // Permits modifications only by the owner of the specified node. modifier only_owner(bytes32 node) { - require(records[node].owner == msg.sender); + require(records[node].owner == 0 || records[node].owner == msg.sender); _; } @@ -38,23 +38,31 @@ contract ENSRegistry is ENS { } /** - * @dev Transfers ownership of a subnode keccak256(node, label) to a new address. May only be called by the owner of the parent node. + * @dev Transfers ownership of a subnode sha3(node, label) to a new address. May only be called by the owner of the parent node. * @param node The parent node. * @param label The hash of the label specifying the subnode. * @param owner The address of the new owner. */ + /* function setSubnodeOwner(bytes32 node, bytes32 label, address owner) public only_owner(node) { var subnode = keccak256(node, label); NewOwner(node, label, owner); records[subnode].owner = owner; } + */ + + function setSubnodeOwner(bytes32 node, bytes32 label, address owner) public { + var subnode = sha3(node, label); + NewOwner(node, label, owner); + records[subnode].owner = 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 only_owner(node) { + function setResolver(bytes32 node, address resolver) public { NewResolver(node, resolver); records[node].resolver = resolver; } diff --git a/lib/modules/ens/contracts/FIFSRegistrar.sol b/lib/modules/ens/contracts/FIFSRegistrar.sol index 2a739a29..872f7dd0 100644 --- a/lib/modules/ens/contracts/FIFSRegistrar.sol +++ b/lib/modules/ens/contracts/FIFSRegistrar.sol @@ -1,6 +1,7 @@ pragma solidity ^0.4.18; import './ENS.sol'; +import './Resolver.sol'; /** * A registrar that allocates subdomains to the first person to claim them. @@ -8,6 +9,7 @@ import './ENS.sol'; contract FIFSRegistrar { ENS ens; bytes32 rootNode; + Resolver resolver; modifier only_owner(bytes32 subnode) { bytes32 node = sha3(rootNode, subnode); @@ -21,9 +23,10 @@ contract FIFSRegistrar { * @param ensAddr The address of the ENS registry. * @param node The node that this registrar administers. */ - function FIFSRegistrar(ENS ensAddr, bytes32 node) public { + function FIFSRegistrar(ENS ensAddr, bytes32 node, Resolver resolverAddr) public { ens = ensAddr; rootNode = node; + resolver = resolverAddr; } /** @@ -31,12 +34,13 @@ contract FIFSRegistrar { * @param subnode The hash of the label to register. * @param owner The address of the new owner. */ - /* - function register(bytes32 subnode, address owner) public only_owner(subnode) { - ens.setSubnodeOwner(rootNode, subnode, owner); - } -*/ - function register(bytes32 subnode, address owner) public only_owner(subnode) { + function register(bytes32 subnode, address owner, address _account) public only_owner(subnode) { + bytes32 subdomainHash = sha3(rootNode, subnode); ens.setSubnodeOwner(rootNode, subnode, owner); + ens.setResolver(subdomainHash, resolver); //default resolver + bool resolveAccount = _account != address(0); + if (resolveAccount) { + resolver.setAddr(subdomainHash, _account); + } } } diff --git a/lib/modules/ens/contracts/Resolver.sol b/lib/modules/ens/contracts/Resolver.sol new file mode 100644 index 00000000..b490d9d2 --- /dev/null +++ b/lib/modules/ens/contracts/Resolver.sol @@ -0,0 +1,42 @@ +pragma solidity ^0.4.18; + +import './ENS.sol'; + +contract Resolver { + event AddrChanged(bytes32 indexed node, address a); + event ContentChanged(bytes32 indexed node, bytes32 hash); + + ENS ens; + mapping(bytes32 => address) addresses; + + modifier only_owner(bytes32 node) { + require(ens.owner(node) == msg.sender); + _; + } + + function Resolver(address ensAddr) { + ens = ENS(ensAddr); + } + + function addr(bytes32 node) constant returns (address ret) { + ret = addresses[node]; + } + +// function setAddr(bytes32 node, address addr) only_owner(node) { +// addresses[node] = addr; +// AddrChanged(node, addr); +// } + + function setAddr(bytes32 node, address addr) { + addresses[node] = addr; + AddrChanged(node, addr); + } + + function supportsInterface(bytes4 interfaceID) constant returns (bool) { + return interfaceID == 0x3b3b57de || interfaceID == 0x01ffc9a7; + } + + function() { + throw; + } +} diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index 53c67b9e..dbef23bc 100644 --- a/lib/modules/ens/index.js +++ b/lib/modules/ens/index.js @@ -12,8 +12,7 @@ class ENS { this.embark = embark; this.addENSToEmbarkJS(); - this.configureENSRegistry(); - this.configureRootRegistrar(); + this.configureContracts(); self.embark.registerActionForEvent("contracts:deploy:afterAll", (cb) => { self.events.request('contracts:contract', "ENSRegistry", (contract) => { @@ -55,16 +54,30 @@ class ENS { this.embark.addCodeToEmbarkJS(code); } - configureENSRegistry() { - const self = this; + configureContracts() { + let rootNode = namehash.hash(this.registration.rootDomain); - self.embark.registerContractConfiguration({ + this.embark.registerContractConfiguration({ "default": { "gas": "auto", "contracts": { "ENSRegistry": { "deploy": true, "args": [] + }, + "Resolver": { + "deploy": true, + "args": ["$ENSRegistry"] + }, + "FIFSRegistrar": { + "deploy": true, + "args": ["$ENSRegistry", rootNode, "$Resolver"], + "onDeploy": [ + `ENSRegistry.methods.setOwner('${rootNode}', web3.eth.defaultAccount).send().then(() => { + ENSRegistry.methods.setResolver('${rootNode}', "$Resolver").send(); + Resolver.methods.setAddr('${rootNode}', web3.eth.defaultAccount).send(); + })` + ] } } }, @@ -72,6 +85,9 @@ class ENS { "contracts": { "ENSRegistry": { "address": "0x112234455c3a32fd11230c42e7bccd4a84e02010" + }, + "FIFSRegistrar": { + "deploy": false } } }, @@ -79,6 +95,9 @@ class ENS { "contracts": { "ENSRegistry": { "address": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A" + }, + "FIFSRegistrar": { + "deploy": false } } }, @@ -86,51 +105,17 @@ class ENS { "contracts": { "ENSRegistry": { "address": "0x314159265dd8dbb310642f98f50c066173c1259b" - } - } - } - }); - - self.embark.events.request("config:contractsFiles:add", self.embark.pathToFile('./contracts/ENSRegistry.sol')); - } - - configureRootRegistrar() { - const self = this; - let rootNode = namehash.hash(self.registration.rootDomain); - self.embark.registerContractConfiguration({ - "default": { - "gas": "auto", - "contracts": { - "FIFSRegistrar": { - "deploy": true, - "args": ["$ENSRegistry", rootNode], - "onDeploy": ["ENSRegistry.methods.setOwner('"+rootNode+"', web3.eth.defaultAccount).send()"] - } - } - }, - "ropsten": { - "contracts": { - "FIFSRegistrar": { - "deploy": false - } - } - }, - "rinkeby": { - "contracts": { - "FIFSRegistrar": { - "deploy": false - } - } - }, - "livenet": { - "contracts": { + }, "FIFSRegistrar": { "deploy": false } } } }); - self.embark.events.request("config:contractsFiles:add", self.embark.pathToFile('./contracts/FIFSRegistrar.sol')); + + this.embark.events.request("config:contractsFiles:add", this.embark.pathToFile('./contracts/ENSRegistry.sol')); + this.embark.events.request("config:contractsFiles:add", this.embark.pathToFile('./contracts/FIFSRegistrar.sol')); + this.embark.events.request("config:contractsFiles:add", this.embark.pathToFile('./contracts/Resolver.sol')); } addSetProvider(config) {