put register function in another module that can be used by embark and embark js

This commit is contained in:
Jonathan Rainville 2018-07-25 12:48:46 -04:00 committed by Iuri Matias
parent 4f2d984081
commit 79c542162c
2 changed files with 48 additions and 1 deletions

View File

@ -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);";

View File

@ -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;
}