embark/lib/modules/ens/embarkjs.js

196 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-05-28 15:10:20 +00:00
import namehash from 'eth-ens-namehash';
/*global web3*/
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"
}
];
__embarkENS.setProvider = function (config) {
this.ens = new web3.eth.Contract(config.abi, config.address);
};
__embarkENS.resolve = function (name) {
const self = this;
2018-06-15 14:04:19 +00:00
if (self.ens === undefined) return;
2018-05-28 15:21:52 +00:00
let node = namehash.hash(name);
2018-05-28 15:42:01 +00:00
return self.ens.methods.resolver(node).call().then((resolverAddress) => {
let resolverContract = new web3.eth.Contract(self.resolverInterface, resolverAddress);
return resolverContract.methods.addr(node).call();
}).then((addr) => {
return addr;
}).catch(err => err);
};
__embarkENS.lookup = function (address, cb) {
const self = this;
if (!self.ens) {
console.log("ENS provider not set. Exitting.");
return;
}
if (address.startsWith("0x")) address = address.slice(2);
2018-05-28 15:21:52 +00:00
let node = namehash.hash(address.toLowerCase() + ".addr.reverse");
2018-05-28 15:42:01 +00:00
return self.ens.methods.resolver(node).call().then((resolverAddress) => {
let resolverContract = new web3.eth.Contract(self.resolverInterface, resolverAddress);
return resolverContract.methods.name(node).call();
}).then((name) => {
if (name === "" || name === undefined) {
return cb(new Error("Name not found"));
}
return cb(null, name);
}).catch(cb);
};