forked from MrTiggr/node-bitcoin-explorer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
147 lines (117 loc) · 3.63 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
/**
* Module dependencies.
*/
var express = require('express');
var winston = require('winston');
var Step = require('step');
var bitcoin = require('bitcoinjs');
var RpcClient = require('jsonrpc2').Client;
var bigint = global.bigint = bitcoin.bigint;
global.Util = require('./util');
var fs = require('fs');
var init = require('bitcoinjs/daemon/init');
var config = init.getConfig();
var app = module.exports = express.createServer();
var rpcClient = new RpcClient(config.jsonrpc.port, config.jsonrpc.host,
config.jsonrpc.username, config.jsonrpc.password);
var rpc = rpcClient.connectSocket();
rpc.on('connect', function () {
var moduleSrc = fs.readFileSync(__dirname + '/query.js', 'utf8');
rpc.call('definerpcmodule', ['explorer', moduleSrc], function (err) {
if (err) {
console.error('Error registering query module: '+err.toString());
}
});
});
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Params
app.param('blockHash', function (req, res, next, hash){
hash = Util.decodeHex(hash).reverse();
var hash64 = hash.toString('base64');
rpc.call('explorer.blockquery', [hash64], function (err, data) {
if (err) return next(err);
req.block = data;
next();
});
});
app.param('txHash', function (req, res, next, hash){
hash = Util.decodeHex(hash).reverse();
var hash64 = hash.toString('base64');
rpc.call('explorer.txquery', [hash64], function (err, tx) {
if (err) return next(err);
req.tx = tx;
// TODO: Show side chain blocks containing this tx
next();
});
});
app.param('addrBase58', function (req, res, next, addr){
var pubKeyHash = Util.addressToPubKeyHash(addr);
req.pubKeyHash = pubKeyHash;
var pubKeyHash64 = pubKeyHash.toString('base64');
rpc.call('explorer.addrquery', [pubKeyHash64], function (err, addr) {
if (err) return next(err);
req.addr = addr;
next();
});
});
// Routes
app.get('/', function(req, res, next){
rpc.call('explorer.indexquery', [10], function (err, result) {
if (err) {
next(err);
return;
}
result.title = 'Home - Bitcoin Explorer';
res.render('index', result);
});
});
app.get('/block/:blockHash', function (req, res) {
res.render('block', {
title: 'Block '+req.block.block.height+' - Bitcoin Explorer',
block: req.block.block,
txs: req.block.txs,
nextBlock: req.block.nextBlock,
totalAmount: req.block.totalAmount,
totalFee: req.block.totalFee,
totalOut: req.block.totalOut,
blockValue: req.block.blockValue,
hexDifficulty: bigint(req.block.block.bits).toString(16)
});
});
app.get('/tx/:txHash', function (req, res) {
res.render('transaction', {
title: req.tx.tx ?
('Tx '+req.tx.tx.hash.substr(0, 10)+'... - Bitcoin Explorer') :
'Unknown tx',
tx: req.tx.tx,
block: req.tx.block,
totalOut: req.tx.totalOut
});
});
app.get('/address/:addrBase58', function (req, res) {
res.render('address', {
title: 'Address '+(req.params.addrBase58)+' - Bitcoin Explorer',
address: req.params.addrBase58,
account: req.addr.account,
txs: req.addr.txs
});
});
// Only listen on $ node app.js
if (!module.parent) {
app.listen(3000);
winston.info("Express server listening on port " + app.address().port);
}