-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathapp.js
319 lines (261 loc) · 8.02 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
/**
* Module dependencies.
*/
var express = require('express');
var winston = require('winston');
var bitcoin = require('bitcoin-p2p');
var bigint = global.bigint = bitcoin.bigint;
global.Util = require('./util');
var app = module.exports = express.createServer();
var storage = new bitcoin.Storage('mongodb://localhost/bitcoin');
//var node = new bitcoin.Node();
//var chain = node.getBlockChain();
// 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());
});
function getOutpoints(txs, callback) {
// If we got only one tx, wrap it so we can use the same code afterwards
if (txs.hash) txs = [txs];
var txList = [];
txs.forEach(function (tx) {
tx.ins.forEach(function (txin) {
txList.push(txin.outpoint.hash);
});
});
storage.Transaction.find({_id: {"$in": txList}}, function (err, result) {
if (err) return callback(err);
try {
var txIndex = {};
result.forEach(function (tx) {
txIndex[tx.hash.toString('base64')] = tx;
});
txs.forEach(function (tx, i) {
tx.totalIn = bigint(0);
tx.totalOut = bigint(0);
tx.ins.forEach(function (txin, j) {
if (txin.isCoinBase()) return;
var op = txin.outpoint;
var srctx = txIndex[op.hash.toString('base64')];
if (srctx) {
txin.source = srctx.outs[op.index];
tx.totalIn = tx.totalIn.add(Util.valueToBigInt(txin.source.value));
} else {
throw new Error("Unable to find source output for tx "+
Util.formatHash(tx.hash));
}
});
tx.outs.forEach(function (txout) {
tx.totalOut = tx.totalOut.add(Util.valueToBigInt(txout.value));
});
if (!tx.isCoinBase()) tx.fee = tx.totalIn.sub(tx.totalOut);
});
callback(null);
} catch (e) {
return callback(e);
}
});
}
// Params
app.param('blockHash', function (req, res, next, hash){
hash = Util.decodeHex(hash).reverse();
storage.Block.findOne({_id: hash}, function (err, block) {
if (err) return next(err);
storage.Block.findOne({prev_hash: hash}, function (err, nextBlock) {
if (err) return next(err);
storage.Transaction.find({_id: {$in: block.txs}}, function (err, txs) {
if (err) return next(err);
getOutpoints(txs, function (err) {
if (err) return next(err);
var totalFee = bigint(0);
var totalOut = bigint(0);
txs.forEach(function (tx) {
tx.outs.forEach(function (txout) {
totalOut = totalOut.add(Util.valueToBigInt(txout.value));
});
if (tx.fee) totalFee = totalFee.add(tx.fee);
});
req.block = block;
req.nextBlock = nextBlock;
req.txs = txs;
req.totalFee = totalFee;
req.totalOut = totalOut;
next();
});
});
});
});
});
app.param('txHash', function (req, res, next, hash){
hash = Util.decodeHex(hash).reverse();
storage.Transaction.findOne({_id: hash}, function (err, tx) {
if (err) return next(err);
req.tx = tx;
// TODO: Show side chain blocks containing this tx
storage.Block.findOne({txs: tx._id, active: true}, function (err, block) {
if (err) return next(err);
req.block = block;
getOutpoints(tx, function (err) {
if (err) return next(err);
next();
});
});
});
});
app.param('addrBase58', function (req, res, next, addr){
var pubKeyHash = Util.addressToPubKeyHash(addr);
req.pubKeyHash = pubKeyHash;
// TODO: We have to limit no of transactions. Need to implement paging and fix
// "spent in" for this case.
storage.Transaction.find({affects: pubKeyHash}).limit(100).exec(function (err, txs) {
if (err) return next(err);
var txList = txs.map(function (tx) {
return tx._id;
});
storage.Block.find({txs: {$in: txList}}, function (err, blocks) {
if (err) return next(err);
getOutpoints(txs, function (err) {
if (err) return next(err);
var txsObj = {};
txs.forEach(function (tx) {
txsObj[tx.hash.toString('base64')] = tx;
});
blocks.forEach(function (block) {
block.txs.forEach(function (tx) {
var hash64 = tx.toString('base64');
if (txsObj[hash64]) {
txsObj[hash64].blockObj = block;
}
});
});
req.txsObj = txsObj;
var receivedCount = 0;
var receivedAmount = bigint(0);
var sentCount = 0;
var sentAmount = bigint(0);
var txOutsObj = {};
txs.forEach(function (tx, index) {
for (var i = 0; i < tx.outs.length; i++) {
var txout = tx.outs[i];
var script = txout.getScript();
var outPubKey = script.simpleOutPubKeyHash();
if (outPubKey && pubKeyHash.compare(outPubKey) == 0) {
receivedCount++;
var outIndex =
tx.hash.toString('base64')+":"+
i;
txOutsObj[outIndex] = txout;
receivedAmount = receivedAmount.add(Util.valueToBigInt(txout.value));
tx.myOut = txout;
}
};
});
txs.forEach(function (tx, index) {
if (tx.isCoinBase()) return;
tx.ins.forEach(function (txin, j) {
var script = txin.source.getScript();
var outPubKey = script.simpleOutPubKeyHash();
if (outPubKey && pubKeyHash.compare(outPubKey) == 0) {
sentCount++;
var outIndex =
txin.outpoint.hash.toString('base64')+":"+
txin.outpoint.index;
if (!txOutsObj[outIndex]) {
winston.warn('Outgoing transaction is missing matching incoming transaction.');
return;
}
txOutsObj[outIndex].spent = {
txin: txin,
tx: tx
};
sentAmount = sentAmount.add(Util.valueToBigInt(txin.source.value));
tx.myIn = txin;
}
});
});
// Make sure the transactions actually have something to do with us
txs = txs.filter(function (tx) {
return tx.myOut || tx.myIn;
});
// Calculate the current available balance
var totalAvailable = bigint(0);
for (var i in txOutsObj) {
if (!txOutsObj[i].spent) {
totalAvailable = totalAvailable.add(Util.valueToBigInt(txOutsObj[i].value));
}
}
var account = {};
account.pubKeyHash = pubKeyHash;
account.totalAvailable = totalAvailable;
account.receivedCount = receivedCount;
account.receivedAmount = receivedAmount;
account.sentCount = sentCount;
account.sentAmount = sentAmount;
req.account = account;
req.txs = txs;
req.txOutsObj = txOutsObj;
next();
});
});
});
});
// Routes
app.get('/', function(req, res){
storage.Block.find().sort('height', -1).limit(15).exec(function (err, rows) {
if (err) return next(err);
res.render('index', {
title: 'Home - Bitcoin Explorer',
latestBlocks: rows
});
});
});
app.get('/block/:blockHash', function (req, res) {
res.render('block', {
title: 'Block '+req.block.height+' - Bitcoin Explorer',
block: req.block,
txs: req.txs,
nextBlock: req.nextBlock,
totalAmount: req.totalAmount,
totalFee: req.totalFee,
totalOut: req.totalOut,
hexDifficulty: bigint(req.block.bits).toString(16)
});
});
app.get('/tx/:txHash', function (req, res) {
var totalOut = bigint(0);
req.tx.outs.forEach(function (txout) {
totalOut = totalOut.add(Util.valueToBigInt(txout.value));
});
res.render('transaction', {
title: 'Tx '+Util.formatHashAlt(req.tx.hash)+'... - Bitcoin Explorer',
tx: req.tx,
block: req.block,
totalOut: totalOut
});
});
app.get('/address/:addrBase58', function (req, res) {
res.render('address', {
title: 'Address '+(req.params.addrBase58)+' - Bitcoin Explorer',
address: req.params.addrBase58,
pubKeyHash: req.pubKeyHash,
account: req.account,
txs: req.txs,
txOutsObj: req.txOutsObj
});
});
// Only listen on $ node app.js
if (!module.parent) {
app.listen(3000);
winston.info("Express server listening on port " + app.address().port);
}