From 79c542162c67cc57fbe0df49bff74441029341cc Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 25 Jul 2018 12:48:46 -0400 Subject: [PATCH] put register function in another module that can be used by embark and embark js --- lib/modules/ens/index.js | 2 +- lib/modules/ens/register.js | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 lib/modules/ens/register.js diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index 38579f88..f0f036a2 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 00000000..6b10d4ce --- /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; +}