-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
450 lines (393 loc) · 14.2 KB
/
Copy pathserver.js
File metadata and controls
450 lines (393 loc) · 14.2 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
require('dotenv').config();
const express = require('express');
const axios = require('axios');
const path = require('path');
const pkg = require('./package.json');
const app = express();
// Configuration from environment
const config = {
port: process.env.PORT || 3000,
electrsApi: process.env.ELECTRS_API || 'http://127.0.0.1:50010',
explorerName: process.env.EXPLORER_NAME || 'DedooExplorer',
coinName: process.env.COIN_NAME || 'Coin',
coinTicker: process.env.COIN_TICKER || 'COIN',
coinTagline: process.env.COIN_TAGLINE || 'A blockchain explorer',
logoUrl: process.env.LOGO_URL || '/img/logo.png',
websiteUrl: process.env.WEBSITE_URL || '',
githubUrl: process.env.GITHUB_URL || '',
telegramUrl: process.env.TELEGRAM_URL || '',
twitterUrl: process.env.TWITTER_URL || '',
discordUrl: process.env.DISCORD_URL || '',
// Mining/Consensus
algorithm: process.env.ALGORITHM || 'SHA256',
diffAdjustment: process.env.DIFF_ADJUSTMENT || 'DGW3',
blockTime: parseInt(process.env.BLOCK_TIME) || 120,
softwareName: pkg.name,
version: pkg.version
};
const PORT = config.port;
const ELECTRS_API = config.electrsApi;
// View engine setup
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Static files
app.use(express.static(path.join(__dirname, 'public')));
// Make config available to all views
app.locals.config = config;
// Helper functions
const formatHash = (hash, length = 16) => {
if (!hash) return '';
return hash.length > length ? `${hash.slice(0, length / 2)}...${hash.slice(-length / 2)}` : hash;
};
const formatNumber = (num) => {
if (num === undefined || num === null) return '0';
return num.toLocaleString();
};
const formatBytes = (bytes) => {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
};
const formatTimeAgo = (timestamp) => {
const now = Math.floor(Date.now() / 1000);
const diff = now - timestamp;
if (diff < 60) return `${diff} seconds ago`;
if (diff < 3600) return `${Math.floor(diff / 60)} minutes ago`;
if (diff < 86400) return `${Math.floor(diff / 3600)} hours ago`;
return `${Math.floor(diff / 86400)} days ago`;
};
const formatDate = (timestamp) => {
return new Date(timestamp * 1000).toLocaleString();
};
const formatHashrate = (hashrate) => {
if (hashrate >= 1e18) return (hashrate / 1e18).toFixed(2) + ' EH/s';
if (hashrate >= 1e15) return (hashrate / 1e15).toFixed(2) + ' PH/s';
if (hashrate >= 1e12) return (hashrate / 1e12).toFixed(2) + ' TH/s';
if (hashrate >= 1e9) return (hashrate / 1e9).toFixed(2) + ' GH/s';
if (hashrate >= 1e6) return (hashrate / 1e6).toFixed(2) + ' MH/s';
if (hashrate >= 1e3) return (hashrate / 1e3).toFixed(2) + ' KH/s';
return hashrate.toFixed(2) + ' H/s';
};
const formatDifficulty = (diff) => {
if (diff >= 1e12) return (diff / 1e12).toFixed(2) + 'T';
if (diff >= 1e9) return (diff / 1e9).toFixed(2) + 'B';
if (diff >= 1e6) return (diff / 1e6).toFixed(2) + 'M';
if (diff >= 1e3) return (diff / 1e3).toFixed(2) + 'K';
return diff.toFixed(2);
};
// Make helpers available to all views
app.locals.formatHash = formatHash;
app.locals.formatNumber = formatNumber;
app.locals.formatBytes = formatBytes;
app.locals.formatTimeAgo = formatTimeAgo;
app.locals.formatDate = formatDate;
app.locals.formatHashrate = formatHashrate;
app.locals.formatDifficulty = formatDifficulty;
// API proxy helper
const apiCall = async (endpoint) => {
try {
const response = await axios.get(`${ELECTRS_API}${endpoint}`, { timeout: 10000 });
return response.data;
} catch (error) {
console.error(`API Error for ${endpoint}:`, error.message);
throw error;
}
};
// ============ PAGES ============
// Dashboard
app.get('/', async (req, res) => {
try {
const [blocks, tipHeight, mempool, supplyData] = await Promise.all([
apiCall('/blocks'),
apiCall('/blocks/tip/height'),
apiCall('/mempool/recent').catch(() => []),
apiCall('/blockchain/getsupply').catch(() => ({ total_amount_float: 0 }))
]);
// Calculate stats from recent blocks
const avgBlockTime = blocks.length > 1
? Math.round((blocks[0].timestamp - blocks[blocks.length - 1].timestamp) / (blocks.length - 1))
: 120;
// Estimate hashrate from difficulty (rough estimate)
const latestDifficulty = blocks[0]?.difficulty || 0;
const hashrate = (latestDifficulty * Math.pow(2, 32)) / avgBlockTime;
res.render('index', {
title: 'Dashboard',
blocks: blocks.slice(0, 15),
tipHeight,
mempoolCount: mempool.length,
difficulty: latestDifficulty,
avgBlockTime,
hashrate,
supply: supplyData.total_amount_float || 0,
page: 'dashboard'
});
} catch (error) {
res.render('error', { title: 'Error', message: 'Failed to load dashboard', error: error.message, page: 'error' });
}
});
// Blocks list
app.get('/blocks', async (req, res) => {
try {
const page = parseInt(req.query.page) || 1;
const tipHeight = await apiCall('/blocks/tip/height');
const startHeight = tipHeight - ((page - 1) * 25);
const blocks = await apiCall(`/blocks/${startHeight}`);
const totalPages = Math.ceil((tipHeight + 1) / 25);
res.render('blocks', {
title: 'Blocks',
blocks,
currentPage: page,
totalPages,
tipHeight,
page: 'blocks'
});
} catch (error) {
res.render('error', { title: 'Error', message: 'Failed to load blocks', error: error.message, page: 'error' });
}
});
// Block detail
app.get('/block/:hash', async (req, res) => {
try {
const { hash } = req.params;
const txPage = parseInt(req.query.txPage) || 0;
const [block, transactions] = await Promise.all([
apiCall(`/block/${hash}`),
apiCall(`/block/${hash}/txs/${txPage * 25}`)
]);
// Get previous and next block hashes
let prevBlock = null, nextBlock = null;
if (block.previousblockhash) {
prevBlock = block.previousblockhash;
}
// Try to get next block
try {
const nextBlockHash = await apiCall(`/block-height/${block.height + 1}`);
nextBlock = nextBlockHash;
} catch (e) {
// No next block
}
const totalTxPages = Math.ceil(block.tx_count / 25);
res.render('block', {
title: `Block ${block.height}`,
block,
transactions,
txPage,
totalTxPages,
prevBlock,
nextBlock,
page: 'blocks'
});
} catch (error) {
res.render('error', { title: 'Error', message: 'Block not found', error: error.message, page: 'error' });
}
});
// Transactions list (mempool + recent)
app.get('/transactions', async (req, res) => {
try {
const mempool = await apiCall('/mempool/recent').catch(() => []);
// Get recent confirmed transactions from latest blocks
const blocks = await apiCall('/blocks');
let recentTxs = [];
for (const block of blocks.slice(0, 5)) {
try {
const txs = await apiCall(`/block/${block.id}/txs/0`);
recentTxs = recentTxs.concat(txs.map(tx => ({
...tx,
block_height: block.height,
block_time: block.timestamp
})));
if (recentTxs.length >= 25) break;
} catch (e) {
continue;
}
}
res.render('transactions', {
title: 'Transactions',
mempool,
recentTxs: recentTxs.slice(0, 25),
page: 'transactions'
});
} catch (error) {
res.render('error', { title: 'Error', message: 'Failed to load transactions', error: error.message, page: 'error' });
}
});
// Transaction detail
app.get('/tx/:txid', async (req, res) => {
try {
const { txid } = req.params;
const tx = await apiCall(`/tx/${txid}`);
// Calculate totals
let totalInput = 0, totalOutput = 0;
tx.vin.forEach(vin => {
if (vin.prevout && vin.prevout.value) {
totalInput += vin.prevout.value;
}
});
tx.vout.forEach(vout => {
totalOutput += vout.value || 0;
});
res.render('transaction', {
title: `Transaction ${formatHash(txid)}`,
tx,
totalInput,
totalOutput,
page: 'transactions'
});
} catch (error) {
res.render('error', { title: 'Error', message: 'Transaction not found', error: error.message, page: 'error' });
}
});
// Address detail
app.get('/address/:address', async (req, res) => {
try {
const { address } = req.params;
const page = parseInt(req.query.page) || 0;
const utxoPage = parseInt(req.query.utxo_page) || 0;
// Fetch address info and transactions
const [addressInfo, txsData] = await Promise.all([
apiCall(`/address/${address}`),
apiCall(`/address/${address}/txs?start_index=${page * 25}&limit=25`)
]);
// Fetch UTXOs with pagination (separate try-catch for graceful degradation)
let utxos = [];
let totalUtxos = 0;
let utxoError = null;
try {
const utxoData = await apiCall(`/address/${address}/utxo?start_index=${utxoPage * 25}&limit=25`);
utxos = utxoData.utxos || utxoData || [];
totalUtxos = utxoData.total || utxos.length;
} catch (err) {
utxoError = err.message;
// If UTXOs fail, still show address with empty UTXOs
}
// Handle different response formats
const transactions = txsData.transactions || txsData;
const totalTxs = txsData.total || addressInfo.chain_stats?.tx_count || 0;
// Calculate balance
const chainStats = addressInfo.chain_stats || {};
const mempoolStats = addressInfo.mempool_stats || {};
const confirmedBalance = (chainStats.funded_txo_sum || 0) - (chainStats.spent_txo_sum || 0);
const pendingBalance = (mempoolStats.funded_txo_sum || 0) - (mempoolStats.spent_txo_sum || 0);
res.render('address', {
title: `Address ${formatHash(address)}`,
address,
addressInfo,
transactions,
utxos,
totalUtxos,
utxoPage,
utxoError,
confirmedBalance,
pendingBalance,
totalTxs,
currentPage: page,
totalPages: Math.ceil(totalTxs / 25),
page: 'address'
});
} catch (error) {
res.render('error', { title: 'Error', message: 'Address not found', error: error.message, page: 'error' });
}
});
// Statistics page
app.get('/statistics', async (req, res) => {
try {
const [blocks, tipHeight] = await Promise.all([
apiCall('/blocks'),
apiCall('/blocks/tip/height')
]);
// Calculate stats
const avgBlockTime = blocks.length > 1
? Math.round((blocks[0].timestamp - blocks[blocks.length - 1].timestamp) / (blocks.length - 1))
: 120;
const latestDifficulty = blocks[0]?.difficulty || 0;
const hashrate = (latestDifficulty * Math.pow(2, 32)) / avgBlockTime;
// Get daily tx counts (simplified - from recent blocks)
const dailyStats = blocks.map(b => ({
date: new Date(b.timestamp * 1000).toLocaleDateString(),
txCount: b.tx_count,
size: b.size
}));
res.render('statistics', {
title: 'Statistics',
tipHeight,
avgBlockTime,
hashrate,
difficulty: latestDifficulty,
dailyStats,
page: 'statistics'
});
} catch (error) {
res.render('error', { title: 'Error', message: 'Failed to load statistics', error: error.message, page: 'error' });
}
});
// Search handler
app.get('/search', async (req, res) => {
const query = req.query.q?.trim();
if (!query) {
return res.redirect('/');
}
// Check if it's a block height (number only)
if (/^\d+$/.test(query)) {
try {
const blockHash = await apiCall(`/block-height/${query}`);
return res.redirect(`/block/${blockHash}`);
} catch (e) {
// Not a valid block height
}
}
// Check if it's a block hash (64 hex chars)
if (/^[a-fA-F0-9]{64}$/.test(query)) {
try {
await apiCall(`/block/${query}`);
return res.redirect(`/block/${query}`);
} catch (e) {
// Try as transaction
try {
await apiCall(`/tx/${query}`);
return res.redirect(`/tx/${query}`);
} catch (e2) {
// Not found
}
}
}
// Try as address
try {
await apiCall(`/address/${query}`);
return res.redirect(`/address/${query}`);
} catch (e) {
// Not found
}
res.render('error', {
title: 'Not Found',
message: 'No results found',
error: `Could not find block, transaction, or address matching: ${query}`,
page: 'search'
});
});
// ============ API PROXY ============
app.get('/api/*', async (req, res) => {
try {
const endpoint = req.path.replace('/api', '');
const data = await apiCall(endpoint);
res.json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Error handler
app.use((err, req, res, next) => {
console.error(err);
res.status(500).render('error', {
title: 'Error',
message: 'Internal Server Error',
error: err.message,
page: 'error'
});
});
// Start server
app.listen(PORT, () => {
console.log(`🚀 ${config.explorerName} running at http://localhost:${PORT}`);
console.log(`📡 Connected to electrs at ${ELECTRS_API}`);
});