mirror of https://github.com/embarklabs/embark.git
fix ens by using right addres depending on chain and also add isAvailable
This commit is contained in:
parent
ffbec61554
commit
8542c0f3f9
|
@ -374,6 +374,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
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
|
/*global EmbarkJS, web3*/
|
||||||
|
|
||||||
import namehash from 'eth-ens-namehash';
|
import namehash from 'eth-ens-namehash';
|
||||||
|
|
||||||
/*global EmbarkJS*/
|
|
||||||
let __embarkENS = {};
|
let __embarkENS = {};
|
||||||
|
|
||||||
// resolver interface
|
// resolver interface
|
||||||
|
@ -157,47 +158,62 @@ const providerNotSetError = 'ENS provider not set';
|
||||||
const NoDecodeAddrError = 'Error: Couldn\'t decode address from ABI: 0x';
|
const NoDecodeAddrError = 'Error: Couldn\'t decode address from ABI: 0x';
|
||||||
const NoDecodeStringError = 'ERROR: The returned value is not a convertible string: 0x0';
|
const NoDecodeStringError = 'ERROR: The returned value is not a convertible string: 0x0';
|
||||||
|
|
||||||
|
__embarkENS.registryAddresses = {
|
||||||
|
// Mainnet
|
||||||
|
"1": "0x314159265dd8dbb310642f98f50c066173c1259b",
|
||||||
|
// Ropsten
|
||||||
|
"3": "0x112234455c3a32fd11230c42e7bccd4a84e02010",
|
||||||
|
// Rinkeby
|
||||||
|
"4": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"
|
||||||
|
};
|
||||||
|
|
||||||
__embarkENS.setProvider = function (config) {
|
__embarkENS.setProvider = function (config) {
|
||||||
const self = this;
|
const self = this;
|
||||||
|
const ERROR_MESSAGE = 'ENS is not available in this chain';
|
||||||
EmbarkJS.onReady(() => {
|
EmbarkJS.onReady(() => {
|
||||||
self.ens = new EmbarkJS.Contract({abi: config.abi, address: config.address});
|
web3.eth.net.getId((err, id) => {
|
||||||
|
if (err) {
|
||||||
|
if (err.message.indexOf('Provider not set or invalid') > -1) {
|
||||||
|
console.warn(ERROR_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
if (!self.registryAddresses[id]) {
|
||||||
|
console.warn(ERROR_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.isAvailable = true;
|
||||||
|
self.ens = new EmbarkJS.Contract({abi: config.abi, address: self.registryAddresses[id]});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
__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);
|
if (err === NoDecodeAddrError) {
|
||||||
console.log(this.ens);
|
return callback(name + " is not registered", "0x");
|
||||||
return this.ens.methods.resolver(node).call().then((resolverAddress) => {
|
}
|
||||||
console.log('address', resolverAddress);
|
callback(err, addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.ens.methods.resolver(node).call((err, resolverAddress) => {
|
||||||
|
if (err) {
|
||||||
|
return cb(err);
|
||||||
|
}
|
||||||
let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress});
|
let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress});
|
||||||
return resolverContract.methods.addr(node).call()
|
resolverContract.methods.addr(node).call(cb);
|
||||||
.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) {
|
|
||||||
return callback(name + " is not registered", "0x");
|
|
||||||
}
|
|
||||||
console.log('oldekwe^pfke');
|
|
||||||
callback(err);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
__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);
|
||||||
}
|
}
|
||||||
|
@ -208,21 +224,22 @@ __embarkENS.lookup = function (address, callback) {
|
||||||
let node = namehash.hash(address.toLowerCase() + ".addr.reverse");
|
let node = namehash.hash(address.toLowerCase() + ".addr.reverse");
|
||||||
console.log('Node', node);
|
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);
|
||||||
|
}
|
||||||
|
let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress});
|
||||||
|
resolverContract.methods.name(node).call(cb);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
__embarkENS.isAvailable = function () {
|
||||||
|
return Boolean(this.isAvailable);
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue