From 19d75bb44ff9cd24e81febef270fe2591e1e8ec0 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 25 Jul 2018 11:05:27 -0400 Subject: [PATCH] fix using only_owner --- lib/modules/ens/contracts/ENSRegistry.sol | 2 +- lib/modules/ens/contracts/FIFSRegistrar.sol | 12 +---- lib/modules/ens/contracts/Resolver.sol | 15 ++---- lib/modules/ens/embarkjs.js | 58 +++++++++++++-------- lib/modules/ens/index.js | 9 ++-- lib/utils/utils.js | 6 +++ 6 files changed, 55 insertions(+), 47 deletions(-) diff --git a/lib/modules/ens/contracts/ENSRegistry.sol b/lib/modules/ens/contracts/ENSRegistry.sol index 71235085..37917c81 100644 --- a/lib/modules/ens/contracts/ENSRegistry.sol +++ b/lib/modules/ens/contracts/ENSRegistry.sol @@ -54,7 +54,7 @@ contract ENSRegistry is ENS { * @param node The node to update. * @param resolver The address of the resolver. */ - function setResolver(bytes32 node, address resolver) public { + function setResolver(bytes32 node, address resolver) public only_owner(node, 0x0) { NewResolver(node, resolver); records[node].resolver = resolver; } diff --git a/lib/modules/ens/contracts/FIFSRegistrar.sol b/lib/modules/ens/contracts/FIFSRegistrar.sol index ce9d7d2f..d1d4924a 100644 --- a/lib/modules/ens/contracts/FIFSRegistrar.sol +++ b/lib/modules/ens/contracts/FIFSRegistrar.sol @@ -9,7 +9,6 @@ import './Resolver.sol'; contract FIFSRegistrar { ENS ens; bytes32 rootNode; - Resolver resolver; modifier only_owner(bytes32 subnode) { bytes32 node = sha3(rootNode, subnode); @@ -23,10 +22,9 @@ contract FIFSRegistrar { * @param ensAddr The address of the ENS registry. * @param node The node that this registrar administers. */ - function FIFSRegistrar(ENS ensAddr, bytes32 node, Resolver resolverAddr) public { + function FIFSRegistrar(ENS ensAddr, bytes32 node) public { ens = ensAddr; rootNode = node; - resolver = resolverAddr; } /** @@ -34,13 +32,7 @@ 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, address nodeAddress) public only_owner(subnode) { - bytes32 subdomainHash = sha3(rootNode, subnode); + function register(bytes32 subnode, address owner) public only_owner(subnode) { ens.setSubnodeOwner(rootNode, subnode, owner); - ens.setResolver(subdomainHash, resolver); //default resolver - bool resolveAccount = nodeAddress != address(0); - if (resolveAccount) { - resolver.setAddr(subdomainHash, nodeAddress); - } } } diff --git a/lib/modules/ens/contracts/Resolver.sol b/lib/modules/ens/contracts/Resolver.sol index 59162b42..1d339cbd 100644 --- a/lib/modules/ens/contracts/Resolver.sol +++ b/lib/modules/ens/contracts/Resolver.sol @@ -33,7 +33,8 @@ contract Resolver { mapping (bytes32 => Record) records; modifier only_owner(bytes32 node) { - require(ens.owner(node) == msg.sender); + address currentOwner = ens.owner(node); + require(currentOwner == 0 || currentOwner == msg.sender); _; } @@ -51,11 +52,7 @@ contract Resolver { * @param node The node to update. * @param addr The address to set. */ -// function setAddr(bytes32 node, address addr) public only_owner(node) { -// records[node].addr = addr; -// emit AddrChanged(node, addr); -// } - function setAddr(bytes32 node, address addr) public { + function setAddr(bytes32 node, address addr) public only_owner(node) { records[node].addr = addr; emit AddrChanged(node, addr); } @@ -79,11 +76,7 @@ contract Resolver { * @param node The node to update. * @param name The name to set. */ -// function setName(bytes32 node, string name) public only_owner(node) { -// records[node].name = name; -// emit NameChanged(node, name); -// } - function setName(bytes32 node, string name) public { + function setName(bytes32 node, string name) public only_owner(node) { records[node].name = name; emit NameChanged(node, name); } diff --git a/lib/modules/ens/embarkjs.js b/lib/modules/ens/embarkjs.js index bcea40e3..4824c47b 100644 --- a/lib/modules/ens/embarkjs.js +++ b/lib/modules/ens/embarkjs.js @@ -150,6 +150,8 @@ __embarkENS.setProvider = function (config) { const self = this; const ERROR_MESSAGE = 'ENS is not available in this chain'; EmbarkJS.onReady(() => { + self.registration = config.registration; + web3.eth.net.getId() .then((id) => { const registryAddress = self.registryAddresses[id] || config.registryAddress; @@ -202,7 +204,7 @@ __embarkENS.lookup = function (address, callback) { if (address.startsWith("0x")) { address = address.slice(2); } - let node = web3.utils.sha3(address.toLowerCase() + ".addr.reverse"); + let node = web3.utils.soliditySha3(address.toLowerCase() + ".addr.reverse"); function cb(err, name) { if (err === NoDecodeStringError || err === NoDecodeAddrError) { @@ -227,33 +229,47 @@ __embarkENS.registerSubDomain = function (name, address, callback) { const self = this; callback = callback || function () {}; + // TODO do something when no address const resolveAddr = address || '0x0000000000000000000000000000000000000000'; - const toSend = this.registrar.methods.register(web3.utils.sha3(name), web3.eth.defaultAccount, resolveAddr); + const subnode = namehash.hash(name); + const node = namehash.hash(`${name}.${self.registration.rootDomain}`); + const reverseNode = web3.utils.soliditySha3(resolveAddr.toLowerCase().substr(2) + '.addr.reverse'); + const toSend = this.registrar.methods.register(subnode, web3.eth.defaultAccount); + let transaction; - toSend.estimateGas().then(gasEstimated => { - return toSend.send({gas: gasEstimated + 1000}).then(transaction => { - if (transaction.status !== "0x1" && transaction.status !== "0x01") { - console.warn('Failed transaction', transaction); + toSend.estimateGas() + // Register domain + .then(gasEstimated => { + return toSend.send({gas: gasEstimated + 1000}); + }) + // Set resolver for the node + .then(transac => { + if (transac.status !== "0x1" && transac.status !== "0x01") { + console.warn('Failed transaction', transac); return callback('Failed to register. Check gas cost.'); } - - const reverseNode = web3.utils.sha3(resolveAddr.toLowerCase().substr(2) + '.addr.reverse'); - return self.ens.methods.setResolver(reverseNode, this.resolver.options.address).send((function (err, _result) { - if (err) { - return callback(err); - } - return self.resolver.methods.setName(reverseNode, name + '.embark.eth').send(function (err, _result) { - callback(err, transaction); - }); - })); - }).catch(err => { + transaction = transac; + return self.ens.methods.setResolver(node, self.resolver.options.address).send(); + }) + // Set address for node + .then(_result => { + return self.resolver.methods.setAddr(node, resolveAddr).send(); + }) + // Set resolver for the reverse node + .then(_result => { + return self.ens.methods.setResolver(reverseNode, self.resolver.options.address).send(); + }) + // Set name for reverse node + .then(_result => { + return self.resolver.methods.setName(reverseNode, name + '.embark.eth').send(); + }) + .then(_result => { + callback(null, transaction); + }) + .catch(err => { callback('Failed to register with error: ' + (err.message || err)); console.error(err); }); - }).catch(err => { - callback("Register would error. Is it already registered? Do you have token balance? Is Allowance set? " + (err.message || err)); - console.error(err); - }); }; __embarkENS.isAvailable = function () { diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index 45efc8c6..38579f88 100644 --- a/lib/modules/ens/index.js +++ b/lib/modules/ens/index.js @@ -11,6 +11,7 @@ class ENS { this.registration = this.namesConfig.register; this.embark = embark; + // TODO add checks to see if config is ok this.addENSToEmbarkJS(); this.configureContracts(); this.registerEvents(); @@ -38,6 +39,7 @@ class ENS { ], (err, results) => { // result[0] => ENSRegistry; result[1] => FIFSRegistrar; result[2] => FIFSRegistrar let config = { + registration: self.registration, registryAbi: results[0].abiDefinition, registryAddress: results[0].deployedAddress, registrarAbi: results[1].abiDefinition, @@ -62,8 +64,7 @@ class ENS { {abi: config.registrarAbi, address: config.registrarAddress}, (registrar) => { async.each(Object.keys(self.registration.domains), (subDomainName, eachCb) => { - const toSend = registrar.methods.register(utils.sha3(subDomainName), - defaultAccount, self.registration.domains[subDomainName]); + const toSend = registrar.methods.register(utils.soliditySha3(subDomainName), defaultAccount); toSend.estimateGas().then(gasEstimated => { return toSend.send({gas: gasEstimated + 1000, from: defaultAccount}).then(transaction => { @@ -127,11 +128,11 @@ class ENS { }, "FIFSRegistrar": { "deploy": true, - "args": ["$ENSRegistry", rootNode, "$Resolver"], + "args": ["$ENSRegistry", rootNode], "onDeploy": [ `ENSRegistry.methods.setOwner('${rootNode}', web3.eth.defaultAccount).send().then(() => { ENSRegistry.methods.setResolver('${rootNode}', "$Resolver").send(); - var reverseNode = web3.utils.sha3(web3.eth.defaultAccount.toLowerCase().substr(2) + '.addr.reverse'); + var reverseNode = web3.utils.soliditySha3(web3.eth.defaultAccount.toLowerCase().substr(2) + '.addr.reverse'); ENSRegistry.methods.setResolver(reverseNode, "$Resolver").send(); Resolver.methods.setAddr('${rootNode}', web3.eth.defaultAccount).send(); Resolver.methods.setName(reverseNode, '${this.registration.rootDomain}').send(); diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 785c3c19..669f8513 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -267,6 +267,11 @@ function sha3(arg) { return Web3.utils.sha3(arg); } +function soliditySha3(arg) { + const Web3 = require('web3'); + return Web3.utils.soliditySha3(arg); +} + function normalizeInput(input) { let args = Object.values(input); if (args.length === 0) { @@ -363,6 +368,7 @@ module.exports = { getExternalContractUrl, toChecksumAddress, sha3, + soliditySha3, normalizeInput, buildUrl, buildUrlFromConfig,