Merge branch 'txs-by-address' into next-merge
This commit is contained in:
commit
e210e34dbc
@ -3,41 +3,119 @@ const logger = require('../logger');
|
|||||||
|
|
||||||
const MAX_BLOCKS = 200;
|
const MAX_BLOCKS = 200;
|
||||||
|
|
||||||
|
// Shoe horned in. Not dry, also in blocks. Make db api later
|
||||||
|
function getBlock(params, options, limit, cb) {
|
||||||
|
const defaultOptions = { _id: 0 };
|
||||||
|
|
||||||
|
if (!Number.isInteger(limit)) {
|
||||||
|
limit = MAX_BLOCKS;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(defaultOptions, options);
|
||||||
|
|
||||||
|
Block.find(
|
||||||
|
params,
|
||||||
|
defaultOptions,
|
||||||
|
cb)
|
||||||
|
.sort({ height: -1 })
|
||||||
|
.limit(limit);
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = function AddressAPI(router) {
|
module.exports = function AddressAPI(router) {
|
||||||
router.get('/addr/:addr', (req, res) => {
|
router.get('/addr/:addr', (req, res) => {
|
||||||
res.send(req.params.addr);
|
res.json({
|
||||||
|
addrStr: req.params.addr,
|
||||||
|
balance: 0,
|
||||||
|
balanceSat: 0,
|
||||||
|
totalReceived: 0,
|
||||||
|
totalReceivedSat: 0,
|
||||||
|
totalSent: 0,
|
||||||
|
totalSentSat: 0,
|
||||||
|
unconfirmedBalance: 0,
|
||||||
|
unconfirmedBalanceSat: 0,
|
||||||
|
unconfirmedTxApperances: 0,
|
||||||
|
txApperances: 5,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/addr/:addr/utxo', (req, res) => {
|
router.get('/addr/:addr/utxo', (req, res) => {
|
||||||
res.send(req.params.addr);
|
res.send('1');
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/addr/:addr/balance', (req, res) => {
|
router.get('/addr/:addr/balance', (req, res) => {
|
||||||
res.send(req.params.addr);
|
res.send('2');
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/addr/:addr/totalReceived', (req, res) => {
|
router.get('/addr/:addr/totalReceived', (req, res) => {
|
||||||
res.send(req.params.addr);
|
res.send('3');
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/addr/:addr/totalSent', (req, res) => {
|
router.get('/addr/:addr/totalSent', (req, res) => {
|
||||||
res.send(req.params.addr);
|
res.send('4');
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/addr/:addr/unconfirmedBalance', (req, res) => {
|
router.get('/addr/:addr/unconfirmedBalance', (req, res) => {
|
||||||
res.send(req.params.addr);
|
res.send('5');
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/addrs/:addrs/utxo', (req, res) => {
|
router.get('/addrs/:addrs/utxo', (req, res) => {
|
||||||
res.send(req.params.addrs);
|
res.send('6');
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/addrs/utxo', (req, res) => {
|
router.post('/addrs/utxo', (req, res) => {
|
||||||
res.send('post stub');
|
res.send('7');
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/addrs/:addrs/txs', (req, res) => {
|
router.get('/addrs/:addrs/txs', (req, res) => {
|
||||||
res.send(req.params.addrs);
|
getBlock(
|
||||||
|
{
|
||||||
|
$or:
|
||||||
|
[
|
||||||
|
{ 'txs.outputs.address': req.params.addr },
|
||||||
|
{ 'txs.inputs.prevout.hash': req.params.addr },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{ rawBlock: 0 },
|
||||||
|
MAX_BLOCKS,
|
||||||
|
(err, block) => {
|
||||||
|
if (err) {
|
||||||
|
res.status(501).send();
|
||||||
|
logger.log('err', err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (block[0]) {
|
||||||
|
const b = block[0];
|
||||||
|
res.json({
|
||||||
|
pagesTotal: 1,
|
||||||
|
txs: b.txs.map(tx => ({
|
||||||
|
txid: tx.hash,
|
||||||
|
version: tx.version,
|
||||||
|
locktime: tx.locktime,
|
||||||
|
vin: tx.inputs.map(input => ({
|
||||||
|
coinbase: input.script,
|
||||||
|
sequence: input.sequence,
|
||||||
|
n: 0,
|
||||||
|
addr: input.address,
|
||||||
|
})),
|
||||||
|
vout: tx.outputs.map(output => ({
|
||||||
|
value: output.value / 1e8,
|
||||||
|
n: 0,
|
||||||
|
scriptPubKey: {
|
||||||
|
hex: '',
|
||||||
|
asm: '',
|
||||||
|
addresses: [output.address],
|
||||||
|
type: output.type,
|
||||||
|
},
|
||||||
|
spentTxid: '',
|
||||||
|
spentIndex: 0,
|
||||||
|
spentHeight: 0,
|
||||||
|
})),
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.send();
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/addrs/txs', (req, res) => {
|
router.post('/addrs/txs', (req, res) => {
|
||||||
|
|||||||
@ -6,9 +6,13 @@ const MAX_TXS = 20;
|
|||||||
const MAX_BLOCKS = 1;
|
const MAX_BLOCKS = 1;
|
||||||
|
|
||||||
// Shoe horned in. Not dry, also in blocks. Make db api later
|
// Shoe horned in. Not dry, also in blocks. Make db api later
|
||||||
function getBlock(params, options, cb) {
|
function getBlock(params, options, limit, cb) {
|
||||||
const defaultOptions = { _id: 0 };
|
const defaultOptions = { _id: 0 };
|
||||||
|
|
||||||
|
if (!Number.isInteger(limit)) {
|
||||||
|
limit = MAX_BLOCKS;
|
||||||
|
}
|
||||||
|
|
||||||
Object.assign(defaultOptions, options);
|
Object.assign(defaultOptions, options);
|
||||||
|
|
||||||
Block.find(
|
Block.find(
|
||||||
@ -16,7 +20,7 @@ function getBlock(params, options, cb) {
|
|||||||
defaultOptions,
|
defaultOptions,
|
||||||
cb)
|
cb)
|
||||||
.sort({ height: -1 })
|
.sort({ height: -1 })
|
||||||
.limit(MAX_BLOCKS);
|
.limit(limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -85,15 +89,11 @@ module.exports = function transactionAPI(router) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.get('/txs', (req, res) => {
|
router.get('/txs', (req, res) => {
|
||||||
/*
|
|
||||||
const txsBy = req.query.blocks ||
|
|
||||||
req.query.address;
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (req.query.block) {
|
if (req.query.block) {
|
||||||
getBlock(
|
getBlock(
|
||||||
{ hash: req.query.block },
|
{ hash: req.query.block },
|
||||||
{ rawBlock: 0 },
|
{ rawBlock: 0 },
|
||||||
|
MAX_BLOCKS,
|
||||||
(err, block) => {
|
(err, block) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.status(501).send();
|
res.status(501).send();
|
||||||
@ -133,7 +133,52 @@ module.exports = function transactionAPI(router) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if (req.query.address) {
|
} else if (req.query.address) {
|
||||||
|
getBlock(
|
||||||
|
{ $or:
|
||||||
|
[
|
||||||
|
{ 'txs.outputs.address': req.query.address },
|
||||||
|
{ 'txs.inputs.prevout.hash': req.query.address },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{ rawBlock: 0 },
|
||||||
|
MAX_BLOCKS,
|
||||||
|
(err, block) => {
|
||||||
|
if (err) {
|
||||||
|
res.status(501).send();
|
||||||
|
logger.log('err', err);
|
||||||
|
} else if (block[0]) {
|
||||||
|
const b = block[0];
|
||||||
|
res.json({
|
||||||
|
pagesTotal: 1,
|
||||||
|
txs: b.txs.map(tx => ({
|
||||||
|
txid: tx.hash,
|
||||||
|
version: tx.version,
|
||||||
|
locktime: tx.locktime,
|
||||||
|
vin: tx.inputs.map(input => ({
|
||||||
|
coinbase: input.script,
|
||||||
|
sequence: input.sequence,
|
||||||
|
n: 0,
|
||||||
|
addr: input.address,
|
||||||
|
})),
|
||||||
|
vout: tx.outputs.map(output => ({
|
||||||
|
value: output.value / 1e8,
|
||||||
|
n: 0,
|
||||||
|
scriptPubKey: {
|
||||||
|
hex: '',
|
||||||
|
asm: '',
|
||||||
|
addresses: [output.address],
|
||||||
|
type: output.type,
|
||||||
|
},
|
||||||
|
spentTxid: '',
|
||||||
|
spentIndex: 0,
|
||||||
|
spentHeight: 0,
|
||||||
|
})),
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.send();
|
||||||
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
getTransactions(
|
getTransactions(
|
||||||
{},
|
{},
|
||||||
|
|||||||
@ -7,30 +7,13 @@ const logger = require('../logger');
|
|||||||
|
|
||||||
function parse(entry, txs) {
|
function parse(entry, txs) {
|
||||||
txs.forEach((tx) => {
|
txs.forEach((tx) => {
|
||||||
//const txJSON = tx.toJSON();
|
|
||||||
|
|
||||||
tx.outputs.forEach((output) => {
|
tx.outputs.forEach((output) => {
|
||||||
const outputJSON = output.toJSON();
|
const outputJSON = output.toJSON();
|
||||||
|
|
||||||
/*
|
|
||||||
return new OutputModel({
|
|
||||||
address: outputJSON.address,
|
|
||||||
script: outputJSON.script,
|
|
||||||
value: outputJSON.value,
|
|
||||||
});*/
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tx.inputs.forEach((input) => {
|
tx.inputs.forEach((input) => {
|
||||||
const inputJSON = input.toJSON();
|
const inputJSON = input.toJSON();
|
||||||
|
|
||||||
/* return new InputModel({
|
|
||||||
prevout: inputJSON.prevout,
|
|
||||||
script: inputJSON.script,
|
|
||||||
witness: inputJSON.witness,
|
|
||||||
sequence: inputJSON.sequence,
|
|
||||||
address: inputJSON.address,
|
|
||||||
});
|
|
||||||
})*/
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user