fix ens by using right addres depending on chain and also add isAvailable

This commit is contained in:
Jonathan Rainville 2018-07-11 12:47:15 -04:00 committed by Iuri Matias
parent 0180bb7cb2
commit 5498046d3f
2 changed files with 87 additions and 81 deletions

View File

@ -408,6 +408,10 @@ EmbarkJS.Names.lookup = function (identifier, callback) {
return this.currentNameSystems.lookup(identifier, callback); return this.currentNameSystems.lookup(identifier, callback);
}; };
EmbarkJS.Names.isAvailable = function () {
return this.currentNameSystems.isAvailable();
};
// To Implement // To Implement

View File

@ -1,6 +1,5 @@
import namehash from 'eth-ens-namehash'; /*global EmbarkJS, web3, registerSubDomain, namehash*/
/*global web3, EmbarkJS*/
let __embarkENS = {}; let __embarkENS = {};
// resolver interface // resolver interface
@ -56,27 +55,6 @@ __embarkENS.resolverInterface = [
], ],
"type": "function" "type": "function"
}, },
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "kind",
"type": "bytes32"
}
],
"name": "has",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"type": "function"
},
{ {
"constant": false, "constant": false,
"inputs": [ "inputs": [
@ -153,6 +131,12 @@ __embarkENS.resolverInterface = [
} }
]; ];
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';
__embarkENS.registryAddresses = { __embarkENS.registryAddresses = {
// Mainnet // Mainnet
"1": "0x314159265dd8dbb310642f98f50c066173c1259b", "1": "0x314159265dd8dbb310642f98f50c066173c1259b",
@ -162,90 +146,108 @@ __embarkENS.registryAddresses = {
"4": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A" "4": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"
}; };
const providerNotSetError = 'ENS provider not set'; __embarkENS.setProvider = function (config) {
const NoDecodeAddrError = 'Error: Couldn\'t decode address from ABI: 0x';
const NoDecodeStringError = 'ERROR: The returned value is not a convertible string: 0x0';
__embarkENS.setProvider = function () {
const self = this; const self = this;
// get network id and then assign ENS contract based on that const ERROR_MESSAGE = 'ENS is not available in this chain';
let registryAddresses = this.registryAddresses; self.registration = config.registration;
this.ens = null; self.env = config.env;
web3.eth.net.getId().then(id => {
if (registryAddresses[id] !== undefined) { EmbarkJS.onReady(() => {
EmbarkJS.onReady(() => { web3.eth.net.getId()
self.ens = new EmbarkJS.Contract({abi: self.registryInterface, address: registryAddresses[id]}); .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);
}); });
}
// todo: deploy at this point
}).catch(e => {
if (e.message.indexOf('Provider not set or invalid') > -1) {
console.warn('ENS is not available in this chain');
return;
}
console.error(e);
}); });
}; };
__embarkENS.resolve = function (name, callback) { __embarkENS.resolve = function (name, callback) {
callback = callback || function () {};
if (!this.ens) { if (!this.ens) {
return callback(providerNotSetError); return callback(providerNotSetError);
} }
let node = namehash.hash(name); let node = namehash.hash(name);
console.log('Name to check', name); function cb(err, addr) {
console.log('Node', node);
console.log(this.ens);
return this.ens.methods.resolver(node).call().then((resolverAddress) => {
console.log('address', resolverAddress);
let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress});
return resolverContract.methods.addr(node).call()
.then((addr) => {
console.log('ADRESSS', addr);
callback(null, addr);
})
.catch(err => {
if (err === NoDecodeAddrError) {
return callback(name + " is not registered", "0x");
}
callback(err);
});
}).catch((err) => {
if (err === NoDecodeAddrError) { if (err === NoDecodeAddrError) {
return callback(name + " is not registered", "0x"); return callback(name + " is not registered", "0x");
} }
console.log('oldekwe^pfke'); callback(err, addr);
callback(err); }
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);
}); });
}; };
__embarkENS.lookup = function (address, callback) { __embarkENS.lookup = function (address, callback) {
callback = callback || function () {};
if (!this.ens) { if (!this.ens) {
return callback(providerNotSetError); return callback(providerNotSetError);
} }
if (address.startsWith("0x")) { if (address.startsWith("0x")) {
address = address.slice(2); address = address.slice(2);
} }
console.log('Address', address); let node = web3.utils.soliditySha3(address.toLowerCase() + reverseAddrSuffix);
let node = namehash.hash(address.toLowerCase() + ".addr.reverse");
console.log('Node', node);
return this.ens.methods.resolver(node).call().then((resolverAddress) => { function cb(err, name) {
console.log('Resolver address', resolverAddress);
let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress});
resolverContract.methods.name(node).call()
.then(name => {
console.log('Name');
callback(null, name);
})
.catch(callback);
}).catch((err) => {
console.log('Got error', err);
if (err === NoDecodeStringError || err === NoDecodeAddrError) { if (err === NoDecodeStringError || err === NoDecodeAddrError) {
console.log('Address does not resolve to name. Try syncing chain.'); return callback('Address does not resolve to name. Try syncing chain.');
return "";
} }
return err; 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);
}); });
}; };
__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");
};