-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlookup.js
53 lines (50 loc) · 1.28 KB
/
lookup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const Web3 = require("web3");
const nets = [];
const NodeCache = require("node-cache");
const DHT = require("@hyperswarm/dht");
var base32 = require("hi-base32");
const node = new DHT();
const { Packet } = require('dns2');
const ABI = [
{
"inputs": [
{
"internalType": "string",
"name": "_name",
"type": "string"
}
],
"name": "getAddress",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function",
"constant": true
},
];
const ips = {};
module.exports = async (outname, question, net, contract, prefix, any) => {
if (!net) return;
if (!nets[net]) {
nets[net] = [];
const web3 = nets[net].web3 = new Web3(net);
nets[net].contract = new web3.eth.Contract(ABI, contract);
nets[net].cache = new NodeCache({ stdTTL: 60 * 60 * 10, checkperiod: 120 });
}
const update = async () => {
let address = await nets[net].contract.methods.getAddress(outname.toLowerCase()).call();
try {
address = JSON.parse(address);
} catch(e) {}
if (address && any) return address;
return address || '{}';
};
const cache = nets[net].cache.get(question);
if (cache) return cache;
return update() || '';
};