2018-05-28 11:10:20 -04:00
|
|
|
import namehash from 'eth-ens-namehash';
|
|
|
|
|
2018-05-22 12:59:22 -05:00
|
|
|
/*global web3*/
|
|
|
|
let __embarkENS = {};
|
|
|
|
|
2018-06-15 10:23:55 -05:00
|
|
|
// resolver interface
|
2018-05-25 12:25:28 -05:00
|
|
|
__embarkENS.resolverInterface = [
|
2018-05-22 12:59:22 -05: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": 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-06-15 10:23:55 -05:00
|
|
|
__embarkENS.setProvider = function (jsonObject) {
|
|
|
|
this.ens = JSON.parse(jsonObject);
|
2018-05-23 11:46:02 -05:00
|
|
|
};
|
2018-05-22 12:59:22 -05:00
|
|
|
|
|
|
|
__embarkENS.resolve = function(name) {
|
2018-05-23 11:46:02 -05:00
|
|
|
const self = this;
|
2018-05-23 12:13:06 -05:00
|
|
|
|
2018-06-15 10:04:19 -04:00
|
|
|
if (self.ens === undefined) return;
|
2018-05-23 12:13:06 -05:00
|
|
|
|
2018-05-28 11:21:52 -04:00
|
|
|
let node = namehash.hash(name);
|
2018-05-23 12:13:06 -05:00
|
|
|
|
2018-05-28 11:42:01 -04:00
|
|
|
return self.ens.methods.resolver(node).call().then((resolverAddress) => {
|
2018-05-25 12:25:28 -05:00
|
|
|
let resolverContract = new web3.eth.Contract(self.resolverInterface, resolverAddress);
|
2018-05-23 12:13:06 -05:00
|
|
|
return resolverContract.methods.addr(node).call();
|
2018-05-23 11:46:02 -05:00
|
|
|
}).then((addr) => {
|
2018-05-23 12:13:06 -05:00
|
|
|
return addr;
|
2018-05-23 11:46:02 -05:00
|
|
|
}).catch(err => err);
|
|
|
|
};
|
2018-05-23 16:53:39 -05:00
|
|
|
|
|
|
|
__embarkENS.lookup = function(address) {
|
|
|
|
const self = this;
|
|
|
|
|
2018-06-15 10:04:19 -04:00
|
|
|
if (self.ens === undefined) return;
|
2018-05-23 16:53:39 -05:00
|
|
|
|
2018-05-24 07:57:14 -05:00
|
|
|
if (address.startsWith("0x")) address = address.slice(2);
|
2018-05-23 16:53:39 -05:00
|
|
|
|
2018-05-28 11:21:52 -04:00
|
|
|
let node = namehash.hash(address.toLowerCase() + ".addr.reverse");
|
2018-05-23 16:53:39 -05:00
|
|
|
|
2018-05-28 11:42:01 -04:00
|
|
|
return self.ens.methods.resolver(node).call().then((resolverAddress) => {
|
2018-05-25 12:25:28 -05:00
|
|
|
let resolverContract = new web3.eth.Contract(self.resolverInterface, resolverAddress);
|
2018-05-23 16:53:39 -05:00
|
|
|
return resolverContract.methods.name(node).call();
|
|
|
|
}).then((name) => {
|
2018-05-23 23:34:19 -05:00
|
|
|
if (name === "" || name === undefined) throw Error("ENS name not found");
|
2018-05-23 16:53:39 -05:00
|
|
|
return name;
|
|
|
|
}).catch(err => err);
|
2018-05-23 23:34:19 -05:00
|
|
|
};
|