-
Notifications
You must be signed in to change notification settings - Fork 4
/
MergeRecovery.js
180 lines (144 loc) · 4.83 KB
/
MergeRecovery.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict'
const fs = require('fs');
const readline = require('readline');
const eosjs = require('eosjs');
const fetch = require('node-fetch');
const {TextDecoder, TextEncoder} = require('text-encoding');
const Throttle = require('promise-parallel-throttle');
const logger = require('single-line-log');
const ArgumentParser = require('argparse').ArgumentParser;
const log = logger.stdout;
class Parser {
constructor(opts) {
this.snapshotInput = opts.snapshotInput;
this.snapshotOutput = opts.snapshotOutput;
this.recovered = opts.recovered;
this.accounts = {};
this.recoveredAccounts = {};
this.snapMeta = {
account_count: 0,
recovered_count: 0,
matched_count: 0
};
console.log("Writing to " + this.snapshotOutput);
try {
fs.unlinkSync(this.snapshotOutput);
} catch (e) {
console.warn(this.snapshotOutput + " did not yet exist");
}
}
async parse() {
await this.parseRecovered();
await this.parseFile();
}
writeRow(writer, accountName) {
let acct = this.accounts[accountName];
if (!acct) {
console.error("Couldn't get user object to write row: " + accountName);
} else {
writer.write(acct.id +
"," + acct.ethKey +
"," + acct.accountName +
"," + acct.pubKey +
"," + acct.balance + "\n"
)
}
}
async writeCsv() {
const writer = fs.createWriteStream(this.snapshotOutput);
for (let accountName in this.accounts)
this.writeRow(writer, accountName);
writer.end();
}
async parseRecovered() {
let thisParser = this;
return new Promise(function(resolve, reject) {
console.log("Parsing recovered keys: " + thisParser.recovered);
let rl = readline.createInterface({
input: fs.createReadStream(thisParser.recovered),
terminal: false
});
rl.on('line', async function(line) {
let parts = line.split(',');
let ethKey = parts[0];
let pubKey = parts[1];
thisParser.snapMeta.recovered_count++;
thisParser.recoveredAccounts[ethKey.toLowerCase()] = {
ethKey: ethKey.toLowerCase(),
pubKey: pubKey
};
});
rl.on('close', async function() {
console.log("Found " + thisParser.snapMeta.recovered_count + " recovered accounts");
resolve();
});
});
}
async parseFile() {
console.log("Parsing: " + JSON.stringify(this.snapshotInput));
let snapMeta = this.snapMeta;
let accounts = {};
let rl = readline.createInterface({
input: fs.createReadStream(this.snapshotInput),
terminal: false
});
let thisParser = this;
rl.on('line', async function(line) {
let parts = line.split(',');
let id = parts[0];
let ethKey = parts[1];
let accountName = parts[2];
let pubKey = parts[3];
let balance = parts[4];
snapMeta.account_count++;
if (thisParser.recoveredAccounts.hasOwnProperty(ethKey.toLowerCase())) {
console.log("Eth key " + ethKey + " recovered, replacing " + pubKey + " with " + thisParser.recoveredAccounts[ethKey].pubKey);
pubKey = thisParser.recoveredAccounts[ethKey].pubKey;
snapMeta.matched_count++;
}
accounts[accountName] = {
id: id,
ethKey: ethKey,
balance: balance,
accountName: accountName,
pubKey: pubKey
};
});
rl.on('close', async function() {
thisParser.accounts = accounts;
await thisParser.writeCsv();
console.log("Account count: " + snapMeta.account_count);
console.log("Matched count: " + snapMeta.matched_count);
});
}
}
var argParser = new ArgumentParser({
version: '1.0.0',
addHelp: true,
description: 'Telos snapshot injection and validation'
});
argParser.addArgument(
'snapshot-input',
{
help: 'The path to the snapshot file to use'
}
);
argParser.addArgument(
'recovered',
{
help: 'The 2 column csv file of key recovered accounts'
}
)
argParser.addArgument(
'snapshot-output',
{
help: 'If --write-csv is passed, this will be the file to write to'
}
);
let args = argParser.parseArgs();
let opts = {
snapshotInput: args["snapshot-input"],
snapshotOutput: args["snapshot-output"],
recovered: args.recovered
};
(new Parser(opts).parse());