forked from iquidus/explorer
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
356 lines (323 loc) · 10.7 KB
/
app.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
var express = require('express')
, path = require('path')
, bitcoinapi = require('bitcoin-node-api')
, favicon = require('serve-favicon')
, logger = require('morgan')
, cookieParser = require('cookie-parser')
, bodyParser = require('body-parser')
, settings = require('./lib/settings')
, routes = require('./routes/index')
, lib = require('./lib/explorer')
, db = require('./lib/database')
, i18next = require('i18next')
, i18nextMiddleware = require('i18next-express-middleware')
, i18Backend = require('i18next-node-fs-backend')
, request = require('request')
, fs = require('fs');
var app = express();
// bitcoinapi
bitcoinapi.setWalletDetails(settings.wallet);
var commands = [];
for(i=0; i < settings.commands_needed.length; i++){
var cmds = JSON.parse(fs.readFileSync("coin_commands/"+settings.commands_needed[i]));
if(settings.heavy){
for(k=0; k < cmds.heavy.length; k++){
commands.push(cmds.heavy[k]);
}
}
for(k=0; k < cmds.default.length; k++){
commands.push(cmds.default[k]);
}
}
bitcoinapi.setAccess('only', commands);
// Language setup
i18next
.use(i18Backend)
.use(i18nextMiddleware.LanguageDetector)
.init({
interpolation: {
format: function(value, format, lng) {
if (format === 'uppercase') return value.toUpperCase();
if(value instanceof Date) return moment(value).format(format);
return value;
}
},
backend: {
loadPath: __dirname + '/locale/{{lng}}/{{ns}}.json',
addPath: __dirname + '/locale/{{lng}}/{{ns}}.missing.json'
},
detection: {
order: ['querystring', 'cookie'],
caches: ['cookie']
},
fallbackLng: settings.language_fallback,
preload: [settings.language],
saveMissing: true,
debug: false
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(i18nextMiddleware.handle(i18next));
app.use(favicon(path.join(__dirname, settings.favicon)));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// Add Languages to Local Variabels
app.use(function (req, res, next) {
res.locals.currentlang = req.language;
next();
})
// Language Files for Datatable
app.use('/datatable/lang', function(req,res){
i18next.changeLanguage(req.language, (err, t) => {
if (err) return console.log('something went wrong loading', err);
res.send(i18next.t("datatable", { returnObjects: true }));
});
});
// routes
app.use('/api', bitcoinapi.app);
app.use('/', routes);
app.use('/ext/getmoneysupply', function(req,res){
lib.get_supply(function(supply){
res.send(' '+supply);
});
});
app.use('/ext/getaddress/:hash', function(req,res){
db.get_address(req.params.hash, function(address){
if (address) {
var a_ext = {
address: address.a_id,
sent: (address.sent / 100000000),
received: (address.received / 100000000),
balance: (address.balance / 100000000).toString().replace(/(^-+)/mg, ''),
last_txs: address.txs,
};
res.send(a_ext);
} else {
res.send({ error: 'address not found.', hash: req.params.hash})
}
});
});
app.use('/ext/getbalance/:hash', function(req,res){
db.get_address(req.params.hash, function(address){
if (address) {
res.send((address.balance / 100000000).toString().replace(/(^-+)/mg, ''));
} else {
res.send({ error: 'address not found.', hash: req.params.hash})
}
});
});
app.use('/ext/get_masternode_rewards/:hash/:since', function(req, res){
db.get_masternode_rewards(req.params.since, req.params.hash,function(rewards){
if(rewards){
res.json(rewards);
} else {
res.send({error: "something wrong", hash: req.params.hash, since: req.params.since});
}
})
})
app.use('/ext/getdistribution', function(req,res){
db.get_richlist(settings.coin, function(richlist){
db.get_stats(settings.coin, function(stats){
db.get_distribution(richlist, stats, function(dist){
res.send(dist);
});
});
});
});
app.use('/ext/getlasttxsajax', function(req,res){
if(typeof req.query.length === 'undefined' || req.query.length > settings.index.last_txs){
req.query.length = settings.index.last_txs;
}
db.get_last_txs_ajax(req.query.start, req.query.length,function(txs, count){
var data = [];
for(i=0; i<txs.length; i++){
var row = [];
row.push(txs[i].blockindex);
row.push(txs[i].blockhash);
row.push(txs[i].txid);
row.push(txs[i].vout.length);
row.push((txs[i].total / 100000000).toFixed(settings.decimal_places));
row.push(new Date((txs[i].timestamp) * 1000).toUTCString());
data.push(row);
}
res.json({"data":data, "draw": req.query.draw, "recordsTotal": count, "recordsFiltered": count});
});
});
/*
pull request 270
*/
app.use('/ext/getaddresstxsajax', function(req,res){
if(typeof req.query.length === 'undefined' || req.query.length > settings.txcount){
req.query.length = settings.txcount;
}
db.get_address_txs_ajax(req.query.address, req.query.start, req.query.length,function(txs, count){
var data = [];
for(i=0; i<txs.length; i++){
if(typeof txs[i].txid !== "undefined") {
var out = 0
var vin = 0
txs[i].vout.forEach(function (r) {
if (r.addresses == req.query.address) {
out = r.amount;
}
});
txs[i].vin.forEach(function (s) {
if (s.addresses == req.query.address) {
vin = s.amount
}
});
var row = [];
row.push(new Date((txs[i].timestamp) * 1000).toUTCString());
row.push(txs[i].txid);
row.push(out);
row.push(vin);
data.push(row);
}
}
res.json({"data":data, "draw": req.query.draw, "recordsTotal": count, "recordsFiltered": count});
});
});
// End pull request 270
app.use('/ext/getaddresstransactions/:hash', function(req,res){
db.get_address_ajax(req.params.hash,req.query.start, req.query.length,function(txs){
var data = [];
var length = (parseInt(req.query.length) + parseInt(req.query.start)); //facepalm
//facepalm for days. I'm sure there's a saying out there that just because you can, doesn't mean you should.
//works though.
var ntx = txs.txes.reverse();
for(i=req.query.start; i<length; i++){
var row = [];
var mtx = ntx[i];
row.push(lib.format_unixtime(mtx.timestamp));
row.push(mtx.txid);
var done = false;
var out = 0;
var vin = 0;
for(r = 0; r < mtx.vout.length; r++){
if(mtx.vout[r].addresses == req.params.hash)
out = mtx.vout[r].amount;
}
for(s = 0; s < mtx.vin.length; s++){
if(mtx.vin[s].addresses == req.params.hash)
out = mtx.vin[s].amount;
}
if (out > 0 && vin > 0){
var amount = (out - vin) / 100000000
if (amount < 0){
amount = amount * -1
console.log('here');
row.push("- "+amount.toFixed(settings.decimal_places));
}else if(amount > 0){
row.push( "+ "+ amount.toFixed(settings.decimal_places));
}else{
row.push(amount.toFixed(settings.decimal_places))
}
}else if(out > 0){
var amount = out / 100000000
row.push("+ " + amount.toFixed(settings.decimal_places))
}else{
var amount = vin / 100000000
row.push("- " +amount.toFixed(settings.decimal_places))
}
data.push(row);
}
res.json({"data":data, "draw": req.query.draw, "recordsTotal": txs.totalTxes, "recordsFiltered": txs.totalTxes});
});
});
app.use('/ext/getlasttxs/:min', function(req,res){
db.get_last_txs(settings.index.last_txs, (req.params.min * 100000000), function(txs){
res.send({data: txs});
});
});
app.post('/address/:hash/claim', function(req, res){
var address = req.body.address;
var signature = req.body.signature;
var message = req.body.message;
request({
url: 'http://127.0.0.1:' + settings.port + '/api/verifymessage?address='+address+ '&signature='+ signature + '&message=' + message,
method: 'GET',
}, function(error, response, body){
//console.log('error', error);
//console.log('response', response);
if(body == "false"){
console.log('failed');
res.json({"status": "failed", "error":true, "message": error});
}else if(body == "true"){
db.update_label(address, message, function(){
res.json({"status": "success"});
})
}
});
})
app.use('/ext/connections', function(req,res){
db.get_peers(function(peers){
res.send({data: peers});
});
});
//Masternodes
app.use('/ext/getmasternodes', function(req, res) {
db.get_masternodes(function(masternode){
res.send({data: masternode});
});
});
// locals
app.set('title', settings.title);
app.set('symbol', settings.symbol);
app.set('coin', settings.coin);
//app.set('locale', locale);
app.set('display', settings.display);
app.set('markets', settings.markets);
app.set('genesis_block', settings.genesis_block);
app.set('index', settings.index);
app.set('heavy', settings.heavy);
app.set('txcount', settings.txcount);
app.set('nethash', settings.nethash);
app.set('nethash_units', settings.nethash_units);
app.set('show_sent_received', settings.show_sent_received);
app.set('logo', settings.logo);
app.set('theme', settings.theme);
app.set('labels', settings.labels);
app.set('decimal_places', settings.decimal_places);
app.set('frontpage_layout', settings.frontpage_layout);
app.set('coininfo', settings.coininfo);
app.set('address', settings.address);
/*for(i=0;i<Object.keys(settings.social).length; i++)
{
app.set(Object.keys(settings.social)[i], Object.values(settings.social)[i]);
}*/
/*
Object.keys(settings.social).forEach(function(key) {
app.set(key, settings.social[key]);
});*/
app.set("social", settings.social);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;