mirror of
https://github.com/status-im/embark-area-51.git
synced 2025-01-09 13:36:14 +00:00
put register function in another module that can be used by embark and embark js
This commit is contained in:
parent
19d75bb44f
commit
c41961f3dd
@ -1,6 +1,4 @@
|
|||||||
/*global EmbarkJS, web3*/
|
/*global EmbarkJS, web3, registerSubDomain, namehash*/
|
||||||
|
|
||||||
import namehash from 'eth-ens-namehash';
|
|
||||||
|
|
||||||
let __embarkENS = {};
|
let __embarkENS = {};
|
||||||
|
|
||||||
@ -226,50 +224,15 @@ __embarkENS.lookup = function (address, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
__embarkENS.registerSubDomain = function (name, address, callback) {
|
__embarkENS.registerSubDomain = function (name, address, callback) {
|
||||||
const self = this;
|
|
||||||
callback = callback || function () {};
|
callback = callback || function () {};
|
||||||
|
|
||||||
// TODO do something when no address
|
if (!address || !web3.utils.isAddress(address)) {
|
||||||
const resolveAddr = address || '0x0000000000000000000000000000000000000000';
|
return callback('You need to specify a valid address for the subdomain');
|
||||||
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()
|
// Register function generated by the index
|
||||||
// Register domain
|
registerSubDomain(this.ens, this.registrar, this.resolver, web3.eth.defaultAccount, name, this.registration.rootDomain,
|
||||||
.then(gasEstimated => {
|
web3.utils.soliditySha3(address.toLowerCase().substr(2) + '.addr.reverse'), address, callback);
|
||||||
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);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
__embarkENS.isAvailable = function () {
|
__embarkENS.isAvailable = function () {
|
||||||
|
@ -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 += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
|
||||||
code += "\nEmbarkJS.Names.registerProvider('ens', __embarkENS);";
|
code += "\nEmbarkJS.Names.registerProvider('ens', __embarkENS);";
|
||||||
|
|
||||||
|
47
lib/modules/ens/register.js
Normal file
47
lib/modules/ens/register.js
Normal 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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user