226 lines
5.2 KiB
JavaScript
Raw Normal View History

2018-08-14 17:17:47 +01:00
/*global EmbarkJS, web3, lookupAddress, resolveName, registerSubDomain, reverseAddrSuffix*/
2018-05-28 11:10:20 -04:00
let __embarkENS = {};
// resolver interface
__embarkENS.resolverInterface = [
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "addr",
"outputs": [
{
"name": "",
"type": "address"
}
],
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "content",
"outputs": [
{
"name": "",
"type": "bytes32"
}
],
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "addr",
"type": "address"
}
],
"name": "setAddr",
"outputs": [],
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "hash",
"type": "bytes32"
}
],
"name": "setContent",
"outputs": [],
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "name",
"type": "string"
}
],
"name": "setName",
"outputs": [],
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "contentType",
"type": "uint256"
}
],
"name": "ABI",
"outputs": [
{
"name": "",
"type": "uint256"
},
{
"name": "",
"type": "bytes"
}
],
"payable": false,
"type": "function"
}
];
const providerNotSetError = 'ENS provider not set';
__embarkENS.registryAddresses = {
// Mainnet
"1": "0x314159265dd8dbb310642f98f50c066173c1259b",
// Ropsten
"3": "0x112234455c3a32fd11230c42e7bccd4a84e02010",
// Rinkeby
"4": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"
};
__embarkENS.registryAddresses = {
// Mainnet
"1": "0x314159265dd8dbb310642f98f50c066173c1259b",
// Ropsten
"3": "0x112234455c3a32fd11230c42e7bccd4a84e02010",
// Rinkeby
"4": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"
};
__embarkENS.setProvider = function (config) {
2018-05-28 11:21:52 -04:00
const self = this;
const ERROR_MESSAGE = 'ENS is not available in this chain';
2018-07-25 16:27:30 -04:00
self.registration = config.registration;
self.env = config.env;
2018-07-25 16:12:17 -04:00
2018-07-25 16:27:30 -04:00
EmbarkJS.onReady(() => {
web3.eth.net.getId()
.then((id) => {
2018-08-27 16:22:53 -04:00
const registryAddress = self.registryAddresses[id] || config.registryAddress;
self.isAvailable = true;
self.ens = new EmbarkJS.Contract({abi: config.registryAbi, address: registryAddress});
self.registrar = new EmbarkJS.Contract({abi: config.registrarAbi, address: config.registrarAddress});
self.resolver = new EmbarkJS.Contract({abi: config.resolverAbi, address: config.resolverAddress});
})
.catch(err => {
if (err.message.indexOf('Provider not set or invalid') > -1) {
console.warn(ERROR_MESSAGE);
return;
}
console.error(err);
});
2018-06-15 10:04:19 -04:00
});
};
const createResolverContract = function (address, callback) {
2018-08-14 16:18:37 +01:00
callback(null, new EmbarkJS.Contract({abi: __embarkENS.resolverInterface, address}));
};
2018-07-10 15:59:42 -04:00
__embarkENS.resolve = function (name, callback) {
callback = callback || function () {};
2018-07-10 15:59:42 -04:00
if (!this.ens) {
return callback(providerNotSetError);
}
2018-08-14 16:18:37 +01:00
return resolveName(name, this.ens, createResolverContract, callback);
};
2018-07-10 15:59:42 -04:00
__embarkENS.lookup = function (address, callback) {
callback = callback || function () {};
2018-07-10 15:59:42 -04:00
if (!this.ens) {
return callback(providerNotSetError);
}
2018-08-14 16:18:37 +01:00
return lookupAddress(address, this.ens, web3.utils, createResolverContract.bind(this), callback);
};
__embarkENS.registerSubDomain = function (name, address, callback) {
callback = callback || function () {};
2018-08-22 18:32:43 -04:00
if (this.env !== 'development' && this.env !== 'privatenet') {
return callback('Sub-domain registration is only available in development or privatenet mode');
2018-07-25 16:27:30 -04:00
}
if (!this.registration || !this.registration.rootDomain) {
return callback('No rootDomain is declared in config/namesystem.js (register.rootDomain). Unable to register a subdomain until then.');
}
if (!address || !web3.utils.isAddress(address)) {
return callback('You need to specify a valid address for the subdomain');
}
// Register function generated by the index
registerSubDomain(this.ens, this.registrar, this.resolver, web3.eth.defaultAccount, name, this.registration.rootDomain,
2018-08-22 18:32:43 -04:00
web3.utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix), address, console, EmbarkJS.Utils.secureSend, callback);
};
__embarkENS.isAvailable = function () {
return Boolean(this.isAvailable);
};
2018-07-30 12:49:03 +01:00
__embarkENS.register = function () {
return new Error("Not available with ENS");
};