diff --git a/lib/modules/ens/embarkjs.js b/lib/modules/ens/embarkjs.js index 4824c47ba..ba83ca29a 100644 --- a/lib/modules/ens/embarkjs.js +++ b/lib/modules/ens/embarkjs.js @@ -1,6 +1,4 @@ -/*global EmbarkJS, web3*/ - -import namehash from 'eth-ens-namehash'; +/*global EmbarkJS, web3, registerSubDomain, namehash*/ let __embarkENS = {}; @@ -226,50 +224,15 @@ __embarkENS.lookup = function (address, callback) { }; __embarkENS.registerSubDomain = function (name, address, callback) { - const self = this; callback = callback || function () {}; - // TODO do something when no address - const resolveAddr = address || '0x0000000000000000000000000000000000000000'; - 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; + if (!address || !web3.utils.isAddress(address)) { + return callback('You need to specify a valid address for the subdomain'); + } - 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.'); - } - 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); - }); + // Register function generated by the index + registerSubDomain(this.ens, this.registrar, this.resolver, web3.eth.defaultAccount, name, this.registration.rootDomain, + web3.utils.soliditySha3(address.toLowerCase().substr(2) + '.addr.reverse'), address, callback); }; __embarkENS.isAvailable = function () { diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index 38579f887..f0f036a25 100644 --- a/lib/modules/ens/index.js +++ b/lib/modules/ens/index.js @@ -104,7 +104,7 @@ class ENS { } }); - let code = ""; + let code = fs.readFileSync(utils.joinPath(__dirname, 'register.js')).toString(); code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString(); code += "\nEmbarkJS.Names.registerProvider('ens', __embarkENS);"; diff --git a/lib/modules/ens/register.js b/lib/modules/ens/register.js new file mode 100644 index 000000000..6b10d4cea --- /dev/null +++ b/lib/modules/ens/register.js @@ -0,0 +1,47 @@ +const namehash = require('eth-ens-namehash'); + +function registerSubDomain(ens, registrar, resolver, defaultAccount, subdomain, rootDomain, reverseNode, address, callback) { + console.log('Register', arguments); + const subnode = namehash.hash(subdomain); + const node = namehash.hash(`${subdomain}.${rootDomain}`); + const toSend = registrar.methods.register(subnode, defaultAccount); + let 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.'); + } + transaction = transac; + return ens.methods.setResolver(node, resolver.options.address).send(); + }) + // Set address for node + .then(_result => { + return resolver.methods.setAddr(node, address).send(); + }) + // Set resolver for the reverse node + .then(_result => { + return ens.methods.setResolver(reverseNode, resolver.options.address).send(); + }) + // Set name for reverse node + .then(_result => { + return resolver.methods.setName(reverseNode, subdomain + '.embark.eth').send(); + }) + .then(_result => { + callback(null, transaction); + }) + .catch(err => { + callback('Failed to register with error: ' + (err.message || err)); + console.error(err); + }); +} + +if (typeof module !== 'undefined' && module.exports) { + module.exports = registerSubDomain; +}