2018-08-14 14:09:59 +00:00
|
|
|
/*global EmbarkJS, web3, registerSubDomain, namehash*/
|
2018-05-28 15:10:20 +00:00
|
|
|
|
2018-05-22 17:59:22 +00:00
|
|
|
let __embarkENS = {};
|
|
|
|
|
2018-06-15 15:23:55 +00:00
|
|
|
// resolver interface
|
2018-05-25 17:25:28 +00:00
|
|
|
__embarkENS.resolverInterface = [
|
2018-05-22 17:59:22 +00:00
|
|
|
{
|
|
|
|
"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"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2018-07-11 16:47:15 +00:00
|
|
|
const providerNotSetError = 'ENS provider not set';
|
|
|
|
const NoDecodeAddrError = 'Error: Couldn\'t decode address from ABI: 0x';
|
|
|
|
const NoDecodeStringError = 'ERROR: The returned value is not a convertible string: 0x0';
|
|
|
|
const reverseAddrSuffix = '.addr.reverse';
|
|
|
|
const voidAddress = '0x0000000000000000000000000000000000000000';
|
|
|
|
|
2018-05-25 17:25:28 +00:00
|
|
|
__embarkENS.registryAddresses = {
|
2018-05-22 17:59:22 +00:00
|
|
|
// Mainnet
|
|
|
|
"1": "0x314159265dd8dbb310642f98f50c066173c1259b",
|
|
|
|
// Ropsten
|
|
|
|
"3": "0x112234455c3a32fd11230c42e7bccd4a84e02010",
|
|
|
|
// Rinkeby
|
2018-05-23 16:46:02 +00:00
|
|
|
"4": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"
|
|
|
|
};
|
2018-05-22 17:59:22 +00:00
|
|
|
|
2018-07-11 16:47:15 +00:00
|
|
|
__embarkENS.setProvider = function (config) {
|
2018-05-28 15:21:52 +00:00
|
|
|
const self = this;
|
2018-07-11 16:47:15 +00:00
|
|
|
const ERROR_MESSAGE = 'ENS is not available in this chain';
|
|
|
|
self.registration = config.registration;
|
|
|
|
self.env = config.env;
|
|
|
|
|
|
|
|
EmbarkJS.onReady(() => {
|
|
|
|
web3.eth.net.getId()
|
|
|
|
.then((id) => {
|
|
|
|
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-22 01:03:23 +00:00
|
|
|
});
|
2018-06-15 14:04:19 +00:00
|
|
|
});
|
2018-05-23 16:46:02 +00:00
|
|
|
};
|
2018-05-22 17:59:22 +00:00
|
|
|
|
2018-07-10 19:59:42 +00:00
|
|
|
__embarkENS.resolve = function (name, callback) {
|
2018-07-11 16:47:15 +00:00
|
|
|
callback = callback || function () {};
|
2018-07-10 19:59:42 +00:00
|
|
|
if (!this.ens) {
|
|
|
|
return callback(providerNotSetError);
|
|
|
|
}
|
2018-05-28 15:21:52 +00:00
|
|
|
let node = namehash.hash(name);
|
2018-07-10 19:59:42 +00:00
|
|
|
|
2018-07-11 16:47:15 +00:00
|
|
|
function cb(err, addr) {
|
2018-06-26 19:56:23 +00:00
|
|
|
if (err === NoDecodeAddrError) {
|
2018-07-10 19:59:42 +00:00
|
|
|
return callback(name + " is not registered", "0x");
|
2018-06-25 17:25:22 +00:00
|
|
|
}
|
2018-07-11 16:47:15 +00:00
|
|
|
callback(err, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.ens.methods.resolver(node).call((err, resolverAddress) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
if (resolverAddress === voidAddress) {
|
|
|
|
return cb('Name not yet registered');
|
|
|
|
}
|
|
|
|
let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress});
|
|
|
|
resolverContract.methods.addr(node).call(cb);
|
2018-06-25 17:25:22 +00:00
|
|
|
});
|
2018-05-23 16:46:02 +00:00
|
|
|
};
|
2018-05-23 21:53:39 +00:00
|
|
|
|
2018-07-10 19:59:42 +00:00
|
|
|
__embarkENS.lookup = function (address, callback) {
|
2018-07-11 16:47:15 +00:00
|
|
|
callback = callback || function () {};
|
2018-07-10 19:59:42 +00:00
|
|
|
if (!this.ens) {
|
|
|
|
return callback(providerNotSetError);
|
2018-06-18 18:54:37 +00:00
|
|
|
}
|
2018-07-10 19:59:42 +00:00
|
|
|
if (address.startsWith("0x")) {
|
|
|
|
address = address.slice(2);
|
|
|
|
}
|
2018-07-11 16:47:15 +00:00
|
|
|
let node = web3.utils.soliditySha3(address.toLowerCase() + reverseAddrSuffix);
|
2018-05-23 21:53:39 +00:00
|
|
|
|
2018-07-11 16:47:15 +00:00
|
|
|
function cb(err, name) {
|
2018-06-26 19:56:23 +00:00
|
|
|
if (err === NoDecodeStringError || err === NoDecodeAddrError) {
|
2018-07-11 16:47:15 +00:00
|
|
|
return callback('Address does not resolve to name. Try syncing chain.');
|
2018-06-25 17:25:22 +00:00
|
|
|
}
|
2018-07-11 16:47:15 +00:00
|
|
|
return callback(err, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.ens.methods.resolver(node).call((err, resolverAddress) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
if (resolverAddress === voidAddress) {
|
|
|
|
return cb('Address not associated to a resolver');
|
|
|
|
}
|
|
|
|
let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress});
|
|
|
|
resolverContract.methods.name(node).call(cb);
|
2018-06-25 17:25:22 +00:00
|
|
|
});
|
2018-05-24 04:34:19 +00:00
|
|
|
};
|
2018-07-11 16:47:15 +00:00
|
|
|
|
|
|
|
__embarkENS.registerSubDomain = function (name, address, callback) {
|
|
|
|
callback = callback || function () {};
|
|
|
|
|
|
|
|
if (this.env !== 'development' && this.env !== 'privatenet') {
|
|
|
|
return callback('Sub-domain registration is only available in development or privatenet mode');
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
web3.utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix), address, console, EmbarkJS.Utils.secureSend, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
__embarkENS.isAvailable = function () {
|
|
|
|
return Boolean(this.isAvailable);
|
|
|
|
};
|
|
|
|
|
|
|
|
__embarkENS.register = function () {
|
|
|
|
return new Error("Not available with ENS");
|
2018-07-30 11:49:03 +00:00
|
|
|
};
|