2018-05-28 15:10:20 +00:00
|
|
|
import namehash from 'eth-ens-namehash';
|
|
|
|
|
2018-06-26 17:47:01 +00:00
|
|
|
/*global EmbarkJS*/
|
2018-05-22 17:59:22 +00:00
|
|
|
let __embarkENS = {};
|
|
|
|
|
2018-06-15 15:23:55 +00:00
|
|
|
// resolver interface
|
2018-05-25 17:25:28 +00:00
|
|
|
__embarkENS.resolverInterface = [
|
2018-05-22 17:59:22 +00: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-07-10 19:59:42 +00:00
|
|
|
const providerNotSetError = 'ENS provider not set';
|
2018-06-26 19:56:23 +00:00
|
|
|
const NoDecodeAddrError = 'Error: Couldn\'t decode address from ABI: 0x';
|
|
|
|
const NoDecodeStringError = 'ERROR: The returned value is not a convertible string: 0x0';
|
|
|
|
|
2018-06-15 17:32:48 +00:00
|
|
|
__embarkENS.setProvider = function (config) {
|
2018-06-22 01:03:23 +00:00
|
|
|
const self = this;
|
|
|
|
EmbarkJS.onReady(() => {
|
|
|
|
self.ens = new EmbarkJS.Contract({abi: config.abi, address: config.address});
|
|
|
|
});
|
2018-05-23 16:46:02 +00:00
|
|
|
};
|
2018-05-22 17:59:22 +00:00
|
|
|
|
2018-07-10 19:59:42 +00:00
|
|
|
__embarkENS.resolve = function (name, callback) {
|
|
|
|
if (!this.ens) {
|
|
|
|
return callback(providerNotSetError);
|
|
|
|
}
|
2018-05-23 17:13:06 +00:00
|
|
|
|
2018-05-28 15:21:52 +00:00
|
|
|
let node = namehash.hash(name);
|
2018-07-10 19:59:42 +00:00
|
|
|
|
|
|
|
console.log('Name to check', name);
|
|
|
|
console.log('Node', node);
|
|
|
|
console.log(this.ens);
|
|
|
|
return this.ens.methods.resolver(node).call().then((resolverAddress) => {
|
|
|
|
console.log('address', resolverAddress);
|
|
|
|
let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress});
|
|
|
|
return resolverContract.methods.addr(node).call()
|
|
|
|
.then((addr) => {
|
|
|
|
console.log('ADRESSS', addr);
|
|
|
|
callback(null, addr);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
if (err === NoDecodeAddrError) {
|
|
|
|
return callback(name + " is not registered", "0x");
|
|
|
|
}
|
|
|
|
callback(err);
|
|
|
|
});
|
2018-06-25 17:25:22 +00:00
|
|
|
}).catch((err) => {
|
2018-06-26 19:56:23 +00:00
|
|
|
if (err === NoDecodeAddrError) {
|
2018-07-10 19:59:42 +00:00
|
|
|
return callback(name + " is not registered", "0x");
|
2018-06-25 17:25:22 +00:00
|
|
|
}
|
2018-07-10 19:59:42 +00:00
|
|
|
console.log('oldekwe^pfke');
|
|
|
|
callback(err);
|
2018-06-25 17:25:22 +00:00
|
|
|
});
|
2018-05-23 16:46:02 +00:00
|
|
|
};
|
2018-05-23 21:53:39 +00:00
|
|
|
|
2018-07-10 19:59:42 +00:00
|
|
|
__embarkENS.lookup = function (address, callback) {
|
|
|
|
if (!this.ens) {
|
|
|
|
return callback(providerNotSetError);
|
2018-06-18 18:54:37 +00:00
|
|
|
}
|
2018-07-10 19:59:42 +00:00
|
|
|
if (address.startsWith("0x")) {
|
|
|
|
address = address.slice(2);
|
|
|
|
}
|
|
|
|
console.log('Address', address);
|
2018-05-28 15:21:52 +00:00
|
|
|
let node = namehash.hash(address.toLowerCase() + ".addr.reverse");
|
2018-07-10 19:59:42 +00:00
|
|
|
console.log('Node', node);
|
2018-05-23 21:53:39 +00:00
|
|
|
|
2018-07-10 19:59:42 +00:00
|
|
|
return this.ens.methods.resolver(node).call().then((resolverAddress) => {
|
|
|
|
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);
|
2018-06-25 17:25:22 +00:00
|
|
|
}).catch((err) => {
|
2018-07-10 19:59:42 +00:00
|
|
|
console.log('Got error', err);
|
2018-06-26 19:56:23 +00:00
|
|
|
if (err === NoDecodeStringError || err === NoDecodeAddrError) {
|
2018-06-25 17:25:22 +00:00
|
|
|
console.log('Address does not resolve to name. Try syncing chain.');
|
|
|
|
return "";
|
|
|
|
}
|
2018-06-26 18:28:28 +00:00
|
|
|
return err;
|
2018-06-25 17:25:22 +00:00
|
|
|
});
|
2018-05-24 04:34:19 +00:00
|
|
|
};
|