From 7ded22fb2cfc855d74fbcaca7dbc73f93c610ee1 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 11 Apr 2016 15:40:55 -0400 Subject: [PATCH] block: add raw block endpoint --- lib/blocks.js | 26 ++++++++++++++++++++++++++ lib/index.js | 3 +++ 2 files changed, 29 insertions(+) diff --git a/lib/blocks.js b/lib/blocks.js index 3463748..572d54a 100644 --- a/lib/blocks.js +++ b/lib/blocks.js @@ -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) { diff --git a/lib/index.js b/lib/index.js index 5a915e4..0e02cfa 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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));