-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathread_names.js
More file actions
executable file
·93 lines (86 loc) · 3.68 KB
/
read_names.js
File metadata and controls
executable file
·93 lines (86 loc) · 3.68 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env nodejs
/*
* Copyright (c) 2016-2020 Savoir-faire Linux Inc.
*
* Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
'use strict';
const fs = require('fs');
const Web3 = require('web3');
const web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
const REG_ADDR_FILE = "contractAddress.txt";
const REG_ABI_reserveFor = ['bytes32', 'address', 'address', 'string', 'string'];
let REG_ADDR = "0xe53cb2ace8707526a5050bec7bcf979c57f8b44f";
function readContractAddress() {
fs.readFile(REG_ADDR_FILE, function(err, content) {
if (err) {
console.log("Can't read contract address: " + err);
} else {
REG_ADDR = String(content).trim().toLowerCase();
}
web3.eth.getBlockNumber((err, content) => getAllNames(content));
});
}
function getAllNames(totalBlocks) {
let nextBlock = 0;
let rem = totalBlocks;
fs.unlinkSync('names.json');
const outFd = fs.openSync('names.json', 'a');
fs.write(outFd, '[\n', e => { if (e) console.log(e) });
const cb = function(error, block) {
rem--;
if (error) {
console.log("Can't get block: " + error);
} else {
const transactionNum = block.transactions.length;
for (let t=0; t<transactionNum; t++) {
try {
const tr = block.transactions[t];
if (tr.to && tr.to.toLowerCase() == REG_ADDR) {
const p = web3.eth.abi.decodeParameters(REG_ABI_reserveFor, tr.input.substr(10));
const n = web3.utils.hexToUtf8(p[0]);
console.log("Entry: " + n + " -> " + p[1] + " " + p[2]);
const newObj = {"name": n,"addr":p[2], "owner":p[1]};
if (p[3])
newObj["publickey"] = p[3];
if (p[4])
newObj["signature"] = p[4];
fs.write(outFd, JSON.stringify(newObj) + ',\n', e => { if (e) console.log(e) });
} else {
console.log("Wrong contract: " + tr.to + " expected " + REG_ADDR);
}
} catch (err) {
console.log("Error reading transaction: " + err);
}
}
}
if (nextBlock < totalBlocks)
web3.eth.getBlock(nextBlock++, true, cb);
if (rem == 0) {
console.log("Found " + NAME_LIST.length + " name mappings");
fs.write(outFd, ']', e => { if (e) console.log(e) });
fs.close(outFd, () => {});
} else if (!error && block && block.transactions.length) {
console.log("Listing names: " + Math.round(100-100*rem/totalBlocks) + "%, " + rem + " remaining... ");
}
};
console.log("Starting... total blocks: " + totalBlocks);
// 256 concurrent requests
for (; nextBlock < totalBlocks && nextBlock < 256; nextBlock++)
web3.eth.getBlock(nextBlock, true, cb);
}
readContractAddress();