block: add raw block endpoint

This commit is contained in:
Braydon Fuller 2016-04-11 15:40:55 -04:00
parent 19c5b617d1
commit 7ded22fb2c
2 changed files with 29 additions and 0 deletions

View File

@ -65,6 +65,26 @@ BlockController.prototype.block = function(req, res, next, hash) {
}
};
/**
* Find rawblock by hash and height...
*/
BlockController.prototype.rawBlock = function(req, res, next, blockArg) {
var self = this;
self.node.getRawBlock(blockArg, function(err, blockBuffer) {
if(err && err.code === -5) {
return common.handleErrors(null, res);
} else if(err) {
return common.handleErrors(err, res);
}
req.rawBlock = {
rawblock: blockBuffer.toString('hex')
};
next();
});
};
BlockController.prototype.transformBlock = function(block, info) {
var blockObj = block.toObject();
var transactionIds = blockObj.transactions.map(function(tx) {
@ -99,6 +119,12 @@ BlockController.prototype.show = function(req, res) {
}
};
BlockController.prototype.showRaw = function(req, res) {
if (req.rawBlock) {
res.jsonp(req.rawBlock);
}
};
BlockController.prototype.blockIndex = function(req, res, next, height) {
this.node.services.bitcoind.getBlockHeader(parseInt(height), function(err, info) {
if (err) {

View File

@ -138,6 +138,9 @@ InsightAPI.prototype.setupRoutes = function(app) {
app.get('/block/:blockHash', this.cacheLong(), blocks.show.bind(blocks));
app.param('blockHash', blocks.block.bind(blocks));
app.get('/rawblock/:blockArg', this.cacheLong(), blocks.showRaw.bind(blocks));
app.param('blockArg', blocks.rawBlock.bind(blocks));
app.get('/block-index/:height', this.cacheLong(), blocks.blockIndex.bind(blocks));
app.param('height', blocks.blockIndex.bind(blocks));