This repository has been archived by the owner on Nov 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
rsk-faucet.js
336 lines (287 loc) · 8.95 KB
/
rsk-faucet.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
var express = require('express');
var compression = require('compression');
var session = require('express-session');
var fileStore = require('session-file-store')(session);
var bodyParser = require('body-parser');
var request = require('request');
var app = express();
var fs = require('fs');
var Web3 = require('web3');
var Tx = require('ethereumjs-tx');
var CronJob = require('cron').CronJob;
var rskUtil = require('rskjs-util');
const cookieParser = require('cookie-parser')
const namehash = require('eth-ens-namehash').hash;
function shouldCompress (req, res) {
if (req.headers['x-no-compression']) {
// don't compress responses with this request header
return false
}
// fallback to standard filter function
return compression.filter(req, res)
}
// compress all responses
app.use(compression({filter: shouldCompress}))
app.use('/css', express.static('css'));
app.use('/img', express.static('img'));
app.use('/lib', express.static('lib'));
app.use('/fonts', express.static('fonts'));
app.use(express.static('public'));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use( bodyParser.urlencoded({ extended: true }) ); // to support URL-encoded bodies
const captchaUrl = '/captcha.jpg'
const captchaId = 'captcha'
const captchaFieldName = 'captcha'
app.use(cookieParser())
const captcha = require('captcha').create({ cookie: captchaId })
var port;
var rskNode;
var faucetAddress;
var valueToSend;
var gasPrice;
var gas;
var captchaSecret;
var faucetPrivateKey;
var faucetHistory = {};
function readConfig(){
const obj=JSON.parse(fs.readFileSync('./config.json', 'utf8'));
port = obj.port;
rskNode = obj.rskNode;
faucetAddress = obj.faucetAddress;
faucetPrivateKey = new Buffer(obj.faucetPrivateKey, 'hex');
valueToSend = obj.valueToSend;
captchaSecret = obj.captchaSecret;
gas = obj.gas;
}
readConfig();
app.set('trust proxy', 1) // trust first proxy
app.use(session({
secret: captchaSecret,
proxy: true,
key: 'session.sid',
resave: false,
saveUninitialized: true,
cookie: { secure: true },
store: new fileStore()
}))
var job = new CronJob({
cronTime: '* */59 * * * *',
onTick: function() {
for (var storeAddress in faucetHistory) {
if ( faucetHistory.hasOwnProperty(storeAddress) ) {
var now = new Date().getTime();
//86400000 = 1 day
if(now - faucetHistory[storeAddress].timestamp >= 86400000) {
delete faucetHistory[storeAddress];
}
}
}
}, start: false, timeZone: 'America/Los_Angeles'});
job.start();
function getWeb3() {
if (web3)
return web3;
console.log('using web3', rskNode);
web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://' + rskNode));
return web3;
}
var web3;
getWeb3();
function executeTransfer(destinationAddress) {
var gasPrice = estimateGasPrice();
var rawTx = buildTx(destinationAddress, getNonce(), gasPrice);
var result = web3.eth.sendRawTransaction(rawTx.toString('hex'), function(err, hash){
if (!err)
console.log('transaction hash', hash);
});
}
function buildTx(account, nonce, gasPrice) {
var rawTx = {
nonce: nonce,
gasPrice: gasPrice,
gas: gas,
value: valueToSend,
to: account
}
var tx = new Tx(rawTx);
tx.sign(faucetPrivateKey);
var serializedTx = tx.serialize();
return serializedTx;
}
function getNonce(){
var result = web3.eth.getTransactionCount(faucetAddress, "pending");
return result;
}
function estimateGasPrice(){
var block = web3.eth.getBlock("latest")
if (block.minimumGasPrice <= 1) {
return 1;
} else {
return block.minimumGasPrice * 1.01;
}
}
function accountAlreadyUsed(account) {
var acc = account.toLowerCase();
return acc in faucetHistory;
}
function isValidRSKAddress(address, chainId) {
if (address === '0x0000000000000000000000000000000000000000') {
return false;
}
if (address.substring(0, 2) !== '0x') {
return false;
} else if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
return false;
} else if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) {
return true;
} else {
return rskUtil.isValidChecksumAddress(address, chainId);
}
}
app.get('/*', function (req, res, next) {
if ( req.url.indexOf("/img/") === 0
|| req.url.indexOf("/lib/") === 0
|| req.url.indexOf("/fonts/") === 0
|| req.url.indexOf("/css/font-awesome/css/") === 0
|| req.url.indexOf("/css/font-awesome/fonts/") === 0
|| req.url.indexOf("/css/") === 0
) {
res.setHeader("Cache-Control", "public, max-age=300000");
res.setHeader("Expires", new Date(Date.now() + 300000).toUTCString());
}
next();
});
app.get(captchaUrl, captcha.image());
app.get('/balance', function (req, res) {
var balance = web3.eth.getBalance(faucetAddress);
balance = web3.fromWei(balance, "ether");
return res.status(200).send(balance);
});
const isRNS = name => {
const labels = name.split('.');
return labels[labels.length - 1] === 'rsk';
}
// rns fcfs testnet variant
// read more: https://docs.rns.rifos.org/RNS-Testnet/
const rns = web3.eth.contract([
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "resolver",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]).at('0xeff983147ae97758c04f65ac7dee7c7cacf48ba2')
app.post('/', function (req, res) {
const reqValue = req.body.rskAddress;
if (isRNS(reqValue)) {
const hash = namehash(reqValue);
rns.resolver(hash, (err, resolverAddress) => {
if (err) return res.status(400).send('RNS error.');
if (resolverAddress === '0x0000000000000000000000000000000000000000') return res.status(400).send('No address resolution found.');
const resolver = web3.eth.contract([
{
"constant": true,
"inputs": [
{
"name": "interfaceID",
"type": "bytes4"
}
],
"name": "supportsInterface",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "pure",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "addr",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]).at(resolverAddress);
resolver.supportsInterface('0x3b3b57de', (err, result) => {
if (err || !result) return res.status(400).send('No address resolution found.');
resolver.addr(hash, (err, addr) => {
if (err) return res.status(400).send('RNS error.');
console.log(addr)
return executeDispense(req, res, addr.toLowerCase());
})
})
})
} else {
executeDispense(req, res, reqValue);
}
});
function executeDispense (req, res, address) {
if (accountAlreadyUsed(address)) {
console.log('Address already used today:', address);
return res.status(400).send('Address already used today.');
}
if(req.body[captchaFieldName] === undefined || req.body[captchaFieldName] === '' || req.body[captchaFieldName] === null) {
console.log('No req.body.' + captchaFieldName);
return res.status(400).send("Please complete captcha.");
}
var isSyncing = web3.eth.syncing;
if(!isSyncing) {
// Success will be true or false depending upon captcha validation.
var valid = captcha.check(req, req.body[captchaFieldName])
if(valid !== undefined && !valid) {
console.log('Invalid captcha ', req.body[captchaFieldName]);
return res.status(400).send("Failed captcha verification.");
}
console.log('Sending RSKs to ' + address);
console.log('Captcha ' + req.body[captchaFieldName]);
executeTransfer(address)
faucetHistory[address.toLowerCase()] = {timestamp: new Date().getTime()};
if (isValidRSKAddress(address, 31)) {
res.send('Successfully sent some RBTCs to ' + address + '.');
} else {
console.log(rskUtil.toChecksumAddress(address.toLowerCase(), 31));
res.send('Successfully sent some RBTCs to ' + address + '. </br>' +
'Please consider using this address with RSK Testnet checksum: ' + rskUtil.toChecksumAddress(address.toLowerCase(), 31));
}
} else {
res.status(400).send('We can not tranfer any amount right now. Try again later.' + address + '.');
}
}
app.listen(port, function () {
console.log('RSK Faucet started on port ' + port);
});