2018-05-23 14:58:18 +00:00
|
|
|
const namehash = require('eth-ens-namehash');
|
2018-05-22 17:59:22 +00:00
|
|
|
/*global web3*/
|
|
|
|
let __embarkENS = {};
|
|
|
|
|
|
|
|
// registry interface for later
|
|
|
|
var registryInterface = [
|
|
|
|
{
|
|
|
|
"constant": true,
|
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "node",
|
|
|
|
"type": "bytes32"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "resolver",
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "",
|
|
|
|
"type": "address"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"type": "function"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"constant": true,
|
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "node",
|
|
|
|
"type": "bytes32"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "owner",
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "",
|
|
|
|
"type": "address"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"type": "function"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"constant": false,
|
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "node",
|
|
|
|
"type": "bytes32"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "resolver",
|
|
|
|
"type": "address"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "setResolver",
|
|
|
|
"outputs": [],
|
|
|
|
"type": "function"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"constant": false,
|
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "node",
|
|
|
|
"type": "bytes32"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "label",
|
|
|
|
"type": "bytes32"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "owner",
|
|
|
|
"type": "address"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "setSubnodeOwner",
|
|
|
|
"outputs": [],
|
|
|
|
"type": "function"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"constant": false,
|
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "node",
|
|
|
|
"type": "bytes32"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "owner",
|
|
|
|
"type": "address"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "setOwner",
|
|
|
|
"outputs": [],
|
|
|
|
"type": "function"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
var 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"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
var registryAddresses = {
|
|
|
|
// Mainnet
|
|
|
|
"1": "0x314159265dd8dbb310642f98f50c066173c1259b",
|
|
|
|
// Ropsten
|
|
|
|
"3": "0x112234455c3a32fd11230c42e7bccd4a84e02010",
|
|
|
|
// Rinkeby
|
2018-05-23 16:46:02 +00:00
|
|
|
"4": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"
|
|
|
|
};
|
2018-05-22 17:59:22 +00:00
|
|
|
|
2018-05-23 18:00:34 +00:00
|
|
|
__embarkENS.setProvider = function () {
|
2018-05-22 17:59:22 +00:00
|
|
|
const self = this;
|
2018-05-23 18:00:34 +00:00
|
|
|
|
2018-05-22 17:59:22 +00:00
|
|
|
// get network id and then assign ENS contract based on that
|
|
|
|
self.ens = web3.eth.net.getId().then(id => {
|
2018-05-23 16:46:02 +00:00
|
|
|
if (registryAddresses[id] !== undefined) {
|
|
|
|
return new web3.eth.Contract(registryInterface, registryAddresses[id]);
|
|
|
|
}
|
|
|
|
// todo: deploy at this point
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
};
|
2018-05-22 17:59:22 +00:00
|
|
|
|
|
|
|
__embarkENS.resolve = function(name) {
|
2018-05-23 16:46:02 +00:00
|
|
|
const self = this;
|
2018-05-23 17:13:06 +00:00
|
|
|
|
2018-05-23 18:00:34 +00:00
|
|
|
if (self.ens === undefined) return undefined;
|
2018-05-23 17:13:06 +00:00
|
|
|
|
|
|
|
let node = namehash(name);
|
|
|
|
|
|
|
|
self.ens.methods.resolver(node).call().then((resolverAddress) => {
|
2018-05-23 16:46:02 +00:00
|
|
|
let resolverContract = new web3.eth.Contract(resolverInterface, resolverAddress);
|
2018-05-23 17:13:06 +00:00
|
|
|
return resolverContract.methods.addr(node).call();
|
2018-05-23 16:46:02 +00:00
|
|
|
}).then((addr) => {
|
2018-05-23 17:13:06 +00:00
|
|
|
return addr;
|
2018-05-23 16:46:02 +00:00
|
|
|
}).catch(err => err);
|
|
|
|
};
|
2018-05-23 21:53:39 +00:00
|
|
|
|
|
|
|
__embarkENS.NameNotFound = Error("ENS name not found");
|
|
|
|
|
|
|
|
__embarkENS.lookup = function(address) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
if (self.ens === undefined) return undefined;
|
|
|
|
|
|
|
|
if(address.startsWith("0x"))
|
|
|
|
address = address.slice(2);
|
|
|
|
|
|
|
|
let node = namehash(address.toLowerCase() + ".addr.reverse")
|
|
|
|
|
|
|
|
self.ens.methods.resolver(node).call().then((resolverAddress) => {
|
|
|
|
let resolverContract = new web3.eth.Contract(resolverInterface, resolverAddress);
|
|
|
|
return resolverContract.methods.name(node).call();
|
|
|
|
}).then((name) => {
|
|
|
|
return name;
|
|
|
|
}).catch(err => err);
|
|
|
|
}
|