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
parent 19d75bb44f
commit c41961f3dd
3 changed files with 55 additions and 45 deletions

View File

@ -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 () {

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