From afd68116e71a27d7049d7e1e10d665eac7aed9b7 Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Tue, 15 Aug 2017 15:41:25 -0400 Subject: [PATCH] broadcast tx --- server/lib/api/block.js | 8 +++++--- server/lib/api/index.js | 9 +++++++-- server/lib/api/transaction.js | 21 ++++++++++++++++++--- server/package.json | 1 + 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/server/lib/api/block.js b/server/lib/api/block.js index 2ff49ed..2141347 100644 --- a/server/lib/api/block.js +++ b/server/lib/api/block.js @@ -53,7 +53,7 @@ module.exports = function BlockAPI(router) { poolInfo: {}, }); } else { - res.send(); + res.status(404).send('Not Found'); } }); }); @@ -107,8 +107,10 @@ module.exports = function BlockAPI(router) { }); router.get('/block-index/:height', (req, res) => { + let blockHeight = parseInt(req.params.height) || 1; + getBlock( - { height: req.params.height }, + { height: blockHeight }, { hash: 1 }, MAX_BLOCKS, (err, block) => { @@ -121,7 +123,7 @@ module.exports = function BlockAPI(router) { blockHash: block[0].hash, }); } else { - res.send(); + res.status(404).send('Not Found'); } }); }); diff --git a/server/lib/api/index.js b/server/lib/api/index.js index 0d4610e..e331c37 100644 --- a/server/lib/api/index.js +++ b/server/lib/api/index.js @@ -1,5 +1,6 @@ -const express = require('express'); -const config = require('../../config'); +const express = require('express'); +const config = require('../../config'); +const bodyParser = require('body-parser'); const app = express(); const api = express.Router(); @@ -7,6 +8,10 @@ const cors = require('./cors'); app.use(cors); +app.use(bodyParser.urlencoded({ extended: false })) + +app.use(bodyParser.json()) + // Serve insight ui front end from root dir public folder app.use('/', express.static('./public')); app.use('/blocks', express.static('./public')); diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index 5b7d7fd..472337a 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -52,6 +52,7 @@ module.exports = function transactionAPI(router) { } if (block[0]) { const height = block[0].height; + request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/${req.params.txid}`, (err, localRes, body) => { if (err) { logger.log('error', @@ -68,7 +69,7 @@ module.exports = function transactionAPI(router) { if (!body || !body.hash) { logger.log('error', 'No results found'); - res.status(501).send(); + res.status(404).send(); return; } res.send({ @@ -115,7 +116,6 @@ module.exports = function transactionAPI(router) { logger.log('err', err); } if (block[0]) { - const height = block[0].height; request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/block/${req.query.block}`, (err, localRes, body) => { if (err) { @@ -250,6 +250,21 @@ module.exports = function transactionAPI(router) { }); router.post('/tx/send', (req, res) => { - res.send('tx send stub'); + const rawtx = req.body.rawtx || ''; + console.log(rawtx); + request.post({ + url: `http://${config.bcoin_http}:${config.bcoin['http-port']}/broadcast`, + body: {"tx": rawtx }, + json: true, + }, (err, localRes, body) => { + if (err) { + logger.log('error', + `${err}`); + res.status(400).send(err); + return; + } + + res.json(true); + }); }); }; diff --git a/server/package.json b/server/package.json index 3305847..998ff30 100644 --- a/server/package.json +++ b/server/package.json @@ -14,6 +14,7 @@ "license": "MIT", "dependencies": { "bcoin": "^1.0.0-beta.14", + "body-parser": "^1.17.2", "express": "^4.15.3", "mongoose": "^4.11.5", "request": "^2.81.0",