More cleanup, naming fixes and comments
This commit is contained in:
parent
e3b8413eee
commit
3b8b900405
@ -1,5 +1,6 @@
|
|||||||
const Message = require('bitcore-message');
|
const Message = require('bitcore-message');
|
||||||
|
|
||||||
|
// Copied from previous source
|
||||||
function verifyMessage(req, res) {
|
function verifyMessage(req, res) {
|
||||||
const address = req.body.address || req.query.address;
|
const address = req.body.address || req.query.address;
|
||||||
const signature = req.body.signature || req.query.signature;
|
const signature = req.body.signature || req.query.signature;
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
// Change to have services push blocks/txs
|
||||||
module.exports = function addressrouter(io) {
|
module.exports = function addressrouter(io) {
|
||||||
io.on('connection', (socket) => {
|
io.on('connection', (socket) => {
|
||||||
socket.on('subscribe', (data) => {
|
socket.on('subscribe', (data) => {
|
||||||
|
|||||||
@ -7,6 +7,7 @@ const db = require('../db');
|
|||||||
|
|
||||||
const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}/`;
|
const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}/`;
|
||||||
|
|
||||||
|
// Retrieve Bcoin status
|
||||||
function getStatus(cb) {
|
function getStatus(cb) {
|
||||||
request(`${API_URL}`, (err, localRes, status) => {
|
request(`${API_URL}`, (err, localRes, status) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@ -26,6 +27,7 @@ function getStatus(cb) {
|
|||||||
}
|
}
|
||||||
// UI assigns Multiple Responsibilities depending on params
|
// UI assigns Multiple Responsibilities depending on params
|
||||||
module.exports = function statusAPI(router) {
|
module.exports = function statusAPI(router) {
|
||||||
|
// Get last block hash or node status
|
||||||
router.get('/status', (req, res) => {
|
router.get('/status', (req, res) => {
|
||||||
if (req.query.q === 'getLastBlockHash') {
|
if (req.query.q === 'getLastBlockHash') {
|
||||||
db.blocks.getBlock(
|
db.blocks.getBlock(
|
||||||
@ -73,7 +75,7 @@ module.exports = function statusAPI(router) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// Get Bcoin sync status
|
||||||
router.get('/sync', (req, res) => {
|
router.get('/sync', (req, res) => {
|
||||||
getStatus((err, status) => {
|
getStatus((err, status) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@ -87,16 +89,16 @@ module.exports = function statusAPI(router) {
|
|||||||
return res.status(404).send();
|
return res.status(404).send();
|
||||||
}
|
}
|
||||||
res.json({
|
res.json({
|
||||||
status: status.chain.progress === 100 ? 'synced' : 'syncing',
|
status: status.chain.progress === 100 ? 'synced': 'syncing',
|
||||||
blockChainHeight: status.chain.height,
|
blockChainHeight: status.chain.height,
|
||||||
syncPercentage: Math.round(status.chain.progress * 100),
|
syncPercentage: Math.round(status.chain.progress * 100),
|
||||||
height: status.chain.height,
|
height: status.chain.height,
|
||||||
error: null,
|
error: null,
|
||||||
type: 'bcoin node',
|
type: 'bcoin node',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
// Copied from previous source
|
||||||
router.get('/peer', (req, res) => {
|
router.get('/peer', (req, res) => {
|
||||||
res.json({
|
res.json({
|
||||||
connected: true,
|
connected: true,
|
||||||
|
|||||||
@ -7,7 +7,9 @@ const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`;
|
|||||||
const MAX_TXS = config.api.max_txs;
|
const MAX_TXS = config.api.max_txs;
|
||||||
|
|
||||||
module.exports = function transactionAPI(router) {
|
module.exports = function transactionAPI(router) {
|
||||||
|
// Txs by txid
|
||||||
router.get('/tx/:txid', (req, res) => {
|
router.get('/tx/:txid', (req, res) => {
|
||||||
|
// Get max block height for calculating confirmations
|
||||||
db.blocks.getBlock(
|
db.blocks.getBlock(
|
||||||
{},
|
{},
|
||||||
{ height: 1 },
|
{ height: 1 },
|
||||||
@ -19,57 +21,57 @@ module.exports = function transactionAPI(router) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const height = block.height;
|
const height = block.height;
|
||||||
|
// Bcoin transaction data
|
||||||
request(`${API_URL}/tx/${req.params.txid}`, (error, localRes, body) => {
|
request(`${API_URL}/tx/${req.params.txid}`, (error, localRes, tx) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
logger.log('error',
|
logger.log('error',
|
||||||
`${error}`);
|
`${error}`);
|
||||||
return res.status(404).send();
|
return res.status(404).send();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
body = JSON.parse(body);
|
tx = JSON.parse(tx);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.log('error',
|
logger.log('error',
|
||||||
`${e}`);
|
`${e}`);
|
||||||
return res.status(404).send();
|
return res.status(404).send();
|
||||||
}
|
}
|
||||||
if (!body || !body.hash) {
|
if (!tx || !tx.hash) {
|
||||||
logger.log('error',
|
logger.log('error',
|
||||||
'No results found');
|
'No results found');
|
||||||
return res.status(404).send();
|
return res.status(404).send();
|
||||||
}
|
}
|
||||||
return res.send({
|
return res.send({
|
||||||
txid: body.hash,
|
txid: tx.hash,
|
||||||
version: body.version,
|
version: tx.version,
|
||||||
time: body.ps,
|
time: tx.ps,
|
||||||
blocktime: body.ps,
|
blocktime: tx.ps,
|
||||||
locktime: body.locktime,
|
locktime: tx.locktime,
|
||||||
blockhash: body.block,
|
blockhash: tx.block,
|
||||||
fees: body.fee / 1e8,
|
fees: tx.fee / 1e8,
|
||||||
confirmations: height - body.height + 1,
|
confirmations: height - tx.height + 1,
|
||||||
valueOut: body.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
|
valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
|
||||||
vin: body.inputs.map(input => ({
|
vin: tx.inputs.map(input => ({
|
||||||
addr: input.coin ? input.coin.address : '',
|
addr: input.coin ? input.coin.address : '',
|
||||||
value: input.coin ? input.coin.value / 1e8 : 0,
|
value: input.coin ? input.coin.value / 1e8 : 0,
|
||||||
})),
|
})),
|
||||||
vout: body.outputs.map(output => ({
|
vout: tx.outputs.map(output => ({
|
||||||
scriptPubKey: {
|
scriptPubKey: {
|
||||||
addresses: [output.address],
|
addresses: [output.address],
|
||||||
},
|
},
|
||||||
value: output.value / 1e8,
|
value: output.value / 1e8,
|
||||||
})),
|
})),
|
||||||
isCoinbase: body.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
|
isCoinbase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// That callback hell
|
// /txs is overloaded. Next ver separate concerns
|
||||||
router.get('/txs', (req, res) => {
|
router.get('/txs', (req, res) => {
|
||||||
const pageNum = parseInt(req.query.pageNum) || 0;
|
const pageNum = parseInt(req.query.pageNum) || 0;
|
||||||
const rangeStart = pageNum * MAX_TXS;
|
const rangeStart = pageNum * MAX_TXS;
|
||||||
const rangeEnd = rangeStart + MAX_TXS;
|
const rangeEnd = rangeStart + MAX_TXS;
|
||||||
|
// get txs for blockhash
|
||||||
if (req.query.block) {
|
if (req.query.block) {
|
||||||
db.blocks.getBlock(
|
db.blocks.getBlock(
|
||||||
{},
|
{},
|
||||||
@ -81,6 +83,7 @@ module.exports = function transactionAPI(router) {
|
|||||||
return res.status(404).send();
|
return res.status(404).send();
|
||||||
}
|
}
|
||||||
const height = block.height;
|
const height = block.height;
|
||||||
|
// Get Bcoin data
|
||||||
request(`${API_URL}/block/${req.query.block}`, (error, localRes, block) => {
|
request(`${API_URL}/block/${req.query.block}`, (error, localRes, block) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
logger.log('error',
|
logger.log('error',
|
||||||
@ -124,7 +127,8 @@ module.exports = function transactionAPI(router) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else if (req.query.address) {
|
} else if (req.query.address) {
|
||||||
db.blocks.getBlock(
|
// Get txs by address
|
||||||
|
db.blocks.getBestHeight(
|
||||||
{},
|
{},
|
||||||
{ height: 1 },
|
{ height: 1 },
|
||||||
1,
|
1,
|
||||||
@ -173,10 +177,11 @@ module.exports = function transactionAPI(router) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
// Get last n txs
|
||||||
db.txs.getTransactions(
|
db.txs.getTransactions(
|
||||||
{},
|
{},
|
||||||
{},
|
{},
|
||||||
50,
|
MAX_TXS,
|
||||||
(err, txs) => {
|
(err, txs) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.log('err',
|
logger.log('err',
|
||||||
@ -221,7 +226,7 @@ module.exports = function transactionAPI(router) {
|
|||||||
router.post('/tx/send', (req, res) => {
|
router.post('/tx/send', (req, res) => {
|
||||||
const rawtx = req.body.rawtx || '';
|
const rawtx = req.body.rawtx || '';
|
||||||
request.post({
|
request.post({
|
||||||
url: `http://${config.bcoin_http}:${config.bcoin['http-port']}/broadcast`,
|
url: `${API_URL}/broadcast`,
|
||||||
body: { tx: rawtx },
|
body: { tx: rawtx },
|
||||||
json: true,
|
json: true,
|
||||||
}, (err, localRes, body) => {
|
}, (err, localRes, body) => {
|
||||||
|
|||||||
@ -54,7 +54,19 @@ function getBlock(params, options, limit, cb) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getBestHeight(cb) {
|
||||||
|
getBlock({}, {}, 1, (err, block) => {
|
||||||
|
if (err) {
|
||||||
|
logger.log('error',
|
||||||
|
`getBlock: ${err.err}`);
|
||||||
|
return cb(err);
|
||||||
|
}
|
||||||
|
return cb(null, block.height);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getBlock,
|
getBlock,
|
||||||
getBlocks,
|
getBlocks,
|
||||||
|
getBestHeight,
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user