-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock-results-decoder.js
62 lines (55 loc) · 1.71 KB
/
block-results-decoder.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
54
55
56
57
58
59
60
61
62
const http = require('http');
const https = require('https');
(async () => {
try {
const url = process.argv[2];
const block_results = JSON.parse(await request(url));
if (!!block_results.result.txs_results) {
block_results.result.txs_results = block_results.result.txs_results.map(tx_result => {
tx_result.events = decodeEvents(tx_result.events);
return tx_result;
});
}
block_results.result.begin_block_events = decodeEvents(block_results.result.begin_block_events);
block_results.result.end_block_events = decodeEvents(block_results.result.end_block_events);
console.log(JSON.stringify(block_results));
process.exit(0);
} catch(err) {
console.error(err);
process.exit(1);
}
})();
const decodeEvents = events => {
if (!events) {
return events;
}
return events.map(event => {
event.attributes = event.attributes.map(attribute => {
return {
...attribute,
key: (Buffer.from(attribute.key, 'base64')).toString(),
value: attribute.value? (Buffer.from(attribute.value, 'base64')).toString(): "",
}
})
return event;
})
}
async function request(url) {
let lib = http;
if (url.startsWith('https')) {
lib = https;
}
return new Promise((resolve, reject) => {
lib.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(data);
});
}).on("error", (err) => {
reject(err);
});
});
}