embark/lib/modules/ens/embarkjs.js

245 lines
4.9 KiB
JavaScript
Raw Normal View History

/*global EmbarkJS, web3*/
2018-05-28 15:10:20 +00:00
import namehash from 'eth-ens-namehash';
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": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "kind",
"type": "bytes32"
}
],
"name": "has",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"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-10 19:59:42 +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';
__embarkENS.registryAddresses = {
// Mainnet
"1": "0x314159265dd8dbb310642f98f50c066173c1259b",
// Ropsten
"3": "0x112234455c3a32fd11230c42e7bccd4a84e02010",
// Rinkeby
"4": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"
};
__embarkENS.setProvider = function (config) {
const self = this;
const ERROR_MESSAGE = 'ENS is not available in this chain';
EmbarkJS.onReady(() => {
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);
2018-07-11 19:40:45 +00:00
return;
}
if (!self.registryAddresses[id]) {
console.warn(ERROR_MESSAGE);
return;
}
self.isAvailable = true;
self.ens = new EmbarkJS.Contract({abi: config.abi, address: self.registryAddresses[id]});
});
});
};
2018-07-10 19:59:42 +00:00
__embarkENS.resolve = function (name, callback) {
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
function cb(err, addr) {
if (err === NoDecodeAddrError) {
2018-07-10 19:59:42 +00:00
return callback(name + " is not registered", "0x");
}
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});
resolverContract.methods.addr(node).call(cb);
});
};
2018-07-10 19:59:42 +00:00
__embarkENS.lookup = function (address, callback) {
callback = callback || function () {};
2018-07-10 19:59:42 +00:00
if (!this.ens) {
return callback(providerNotSetError);
}
2018-07-10 19:59:42 +00:00
if (address.startsWith("0x")) {
address = address.slice(2);
}
2018-05-28 15:21:52 +00:00
let node = namehash.hash(address.toLowerCase() + ".addr.reverse");
function cb(err, name) {
if (err === NoDecodeStringError || err === NoDecodeAddrError) {
return callback('Address does not resolve to name. Try syncing chain.');
}
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);
};