-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwrite_names.js
More file actions
executable file
·259 lines (238 loc) · 9.13 KB
/
write_names.js
File metadata and controls
executable file
·259 lines (238 loc) · 9.13 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/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';
var async = require("async");
var BigNumber = require('bignumber.js');
var fs = require('fs');
var Web3 = require('web3');
var web3 = new Web3();
var path = require('path');
var fs = require('fs');
var argv = require('minimist')(process.argv.slice(2));
if(argv['_'] < 1)
throw ("Specify Batch Input File as: nodejs " + path.basename(__filename) + " <filename>" );
function validateFile(filename){
if ( path.isAbsolute(filename) && fs.existsSync(filename) )
return filename
else if ( !path.isAbsolute(filename) && fs.existsSync("./" +filename))
return path.resolve(filename)
return false
}
var providedPath = String(argv['_'][0])
batchInputFile = validateFile(providedPath);
if(!batchInputFile)
throw "File " + providedPath + " does not exist"
Object.getPrototypeOf(web3.eth).awaitConsensus = function(txhash, mined_cb) {
var ethP = this;
var tries = 5;
var filter = this.filter('latest');
filter.watch(function(error, result) {
if (error)
console.log("watch error: " + error);
var receipt = ethP.getTransactionReceipt(txhash);
if (receipt && receipt.transactionHash == txhash) {
filter.stopWatching();
mined_cb();
} else if (!--tries) {
mined_cb("Transaction timeout..");
}
});
}
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var coinbase = web3.eth.coinbase;
console.log("Coinbase: "+ coinbase);
var balance = web3.eth.getBalance(coinbase);
console.log("Balance:" +balance.toString(10));
var REG_FILE = __dirname + "/contract/registrar.out.json";
var REG_ADDR_FILE = __dirname + "/contractAddress.txt";
var NAME_VALIDATOR = new RegExp('^[a-z0-9-_]{3,32}$');
var account;
var regAddress = "0xe53cb2ace8707526a5050bec7bcf979c57f8b44f";
var regData;
var regContract;
var reg;
function unlockAccount() {
web3.personal.unlockAccount(coinbase, "toto");
}
function getRemainingGaz() {
return web3.eth.getBalance(coinbase) / web3.eth.gasPrice;
}
function loadContract(onContractLoaded) {
fs.readFile(REG_ADDR_FILE, function(err, content) {
if (err) {
throw "Contract Address issues, run server to initialize contract address first time";
} else {
regAddress = String(content).trim();
}
fs.readFile(REG_FILE, function(err, data){
if (err) {
console.log("Can't read contract ABI: " + err);
throw err;
}
regData = JSON.parse(data).contracts.registrar.GlobalRegistrar;
regContract = web3.eth.contract(regData.abi);
console.log("Loading name contract from blockchain at " + regAddress);
web3.eth.getCode(regAddress, function(error, result) {
if (error)
console.log("Error getting contract code: " + error);
if (!result || result == "0x") {
console.log("Contract not found at " + regAddress);
throw "Contract Address issues, run server to initialize contract address"
} else {
regContract.at(regAddress, function(err, result) {
console.log("Contract found and loaded from " + regAddress);
if(!err) {
reg = result;
onContractLoaded();
}
else {
console.error("err: " + err);
}
});
}
});
});
});
}
function checkName(name) {
try {
return Boolean(name.match(NAME_VALIDATOR));
} catch (e) {
return false;
}
}
function formatName(name) {
return '0x' + new Buffer(name, 'utf8').toString('hex');
}
function isHashZero(h) {
return !h || h == "0x" || h == "0x0" || h == "0x0000000000000000000000000000000000000000";
}
function parseString(s) {
return s ? web3.toUtf8(s) : s;
}
function formatAddress(address) {
if (address) {
var s = address.trim();
try {
if (s.startsWith("ring:"))
s = s.substr(5);
if (!s.startsWith("0x"))
s = "0x" + s;
if (new BigNumber(s.substr(2), 16) == 0)
return undefined;
return s;
} catch (err) {}
}
return undefined;
}
function registerName(addressparam, nameparam, ownerparam, pk, sig, mined_cb){
try {
var addr = formatAddress(addressparam);
if (!addr) {
console.log("Error parsing input address "+ addressparam);
return;
}
try {
ownerparam = formatAddress(ownerparam);
if (!ownerparam)
throw "no owner";
} catch (err) {
console.log("Error parsing input: " + err);
return;
}
if (!checkName(nameparam)) {
console.log("error: invalid name "+ nameparam);
return;
}
console.log("Got reg request (" + nameparam + " -> " + addr + ") from " + ownerparam);
reg.owner(nameparam, function(err, owner) {
if (owner == 0) {
reg.name(addr, function(err, res) {
try {
if (err)
console.log("Error checking name: " + err);
var name = parseString(res);
if (name) {
console.log("Address " + addr + " already registered with name: " + name);
return;
} else {
console.log("Remaing gaz: " + getRemainingGaz());
unlockAccount();
reg.reserveFor.sendTransaction(formatName(nameparam), ownerparam, addr, pk, sig, {
from: coinbase,
gas: 3000000
}, function(terr, reg_c) {
if (terr) {
console.log("Transaction error " + JSON.stringify(terr));
} else {
console.log("Transaction sent " + reg_c);
// Send answer as soon as the transaction is queued
console.log("Success!")
web3.eth.awaitConsensus(reg_c, function(error) {
mined_cb(reg_c)
if (error) {
console.log(error);
return;
}
});
}
});
}
} catch (err) {
console.log("Address registration exception: " + err);
}
});
} else {
if (owner == ownerparam) {
reg.addr(nameparam, function(err, reg_addr) {
if (reg_addr == addr) {
console.log("Success!");
return;
} else {
console.log("Error!" + err);
return;
}
});
} else {
console.log("Error!");;
console.log("Owner "+ owner);
console.log("Ownerparam "+ ownerparam);
return;
}
}
});
} catch (err) {
console.log("Address registration exception: " + err);
}
}
function startWrites(){
var NAME_LIST = JSON.parse(fs.readFileSync(batchInputFile, 'utf8'));
console.log(String(NAME_LIST.length) + " inserts to do");
//create parallel queue that does 256 registerNames parallely
var q = async.queue(function(task, callback) {
registerName(task['addr'], task['name'], task['owner'], task['publickey'], task['signature'], callback);
}, 256);
for (var i = 0; i < NAME_LIST.length; i++) {
q.push(NAME_LIST[i], function(c){console.log("Mined registry: "+ c)});
console.log("Queued " + i);
}
}
unlockAccount();
loadContract(startWrites);