-
Notifications
You must be signed in to change notification settings - Fork 5
/
zmsgpull
executable file
·80 lines (74 loc) · 2.4 KB
/
zmsgpull
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env node
const {NodeClient} = require('hs-client');
const {Network} = require('hsd');
const {Resource} = require('hsd/lib/dns/resource');
const fs = require('fs');
const constants = require("./lib/constants.js");
const prepend = require('prepend');
const network = Network.get('main');
const nodeOptions = {
network: network.type,
port: network.rpcPort,
apiKey: fs.readFileSync(__dirname+"/keys/node").toString().trim()
};
const nodeClient = new NodeClient(nodeOptions);
// Aliases
async function getBlock(height) { return await nodeClient.getBlock(height) }
async function getBlockTx(txh) { return await nodeClient.getTX(txh) }
async function getNameByHash(hash) { return await nodeClient.execute('getnamebyhash', [ hash ]) }
async function getHeight() { return await nodeClient.execute('getblockcount') }
// Functions
async function getUpdates(block) {
let rupdates = [];
for(let x=0;x<block.txs.length;x++) {
const tx = block.txs[x];
for(let y=0;y<tx.outputs.length;y++) {
const output = tx.outputs[y];
if(output.covenant.action === "UPDATE") {
let name = await getNameByHash(output.covenant.items[0]);
if(output.covenant.items[2].length!=0) {
let records = await Resource.decode(Buffer.from(output.covenant.items[2],'hex')).getJSON(name);
rupdates.push({ name: name, records: records.records });
}
}
}
}
return rupdates;
}
function findZmsg(record) {
for(let x=0;x<record.length;x++) {
const rec = record[x];
if(rec.type=='TXT' && rec.txt) {
for(let y=0;y<rec.txt.length;y++) {
const txt = rec.txt[y];
if(txt.startsWith(constants.MAGIC_ZMSG))
return txt.substr(constants.MAGIC_ZMSG_LENGTH);
}
}
}
return null;
}
(async () => {
let x;
if(fs.existsSync(__dirname+"/.lastBlock"))
x = parseInt(fs.readFileSync(__dirname+"/.lastBlock"));
else
x = constants.ZMSG_START;
if(!x)
x = constants.ZMSG_START;
const height = await getHeight();
for(;x<=height;x++) {
console.log(x);
let block = await getBlock(x);
let updates = await getUpdates(block);
if(updates.length>0) {
for(let y=0;y<updates.length;y++) {
let zmsg = findZmsg(updates[y].records);
if(zmsg) {
prepend(__dirname+"/.posts",`${x},${updates[y].name},${zmsg}`,(err) => {});
}
}
}
}
fs.writeFileSync(__dirname+"/.lastBlock",x.toString());
})();