add API call to return unspent outputs for an address

This commit is contained in:
Matias Alejo Garcia 2014-02-19 21:47:00 -03:00
parent b53b2f0f5c
commit 79814e10fa
3 changed files with 61 additions and 16 deletions

View File

@ -7,32 +7,48 @@
var Address = require('../models/Address'),
common = require('./common');
exports.address = function(req, res, next, addr) {
var getAddr = function(req, res, next) {
var a;
try {
var addr = req.param('addr');
a = Address.new(addr);
} catch (e) {
return common.handleErrors({message: 'Invalid address:' + e.message, code: 1}, res, next);
common.handleErrors({message: 'Invalid address:' + e.message, code: 1}, res, next);
return null;
}
return a;
};
a.update(function(err) {
if (err)
exports.show = function(req, res, next) {
var a = getAddr(req, res, next);
if (a)
a.update(function(err) {
if (err) {
return common.handleErrors(err, res);
}
else {
req.address = a;
return next();
return res.jsonp(a);
}
});
};
/**
*/
exports.show = function(req, res) {
if (req.address) {
res.jsonp(req.address);
}
exports.unspents = function(req, res, next) {
var a = getAddr(req, res, next);
if (a)
a.getUnspents(function(err, unspents) {
if (err)
return common.handleErrors(err, res);
else {
console.log('[addresses.js.47]', unspents); //TODO
return res.jsonp(unspents);
}
});
};

View File

@ -69,6 +69,35 @@ function spec() {
}
Address.prototype.getUnspents = function(next) {
var self = this;
console.log('[Address.js.74:this:]'); //TODO
if (!self.addrStr) return next();
var ret = [];
var db = new TransactionDb();
db.fromAddr(self.addrStr, function(err,txOut){
if (err) return next(err);
txOut.forEach(function(txItem){
if (!txItem.spentTxId) {
ret.push({
address: self.addrStr,
txid: txItem.txid,
vout: txItem.index,
ts: txItem.ts,
amount: txItem.value_sat / BitcoreUtil.COIN,
// TODO => actually 1+
// confirmations: 1,
});
}
});
return next(err,ret);
});
};
Address.prototype.update = function(next) {
var self = this;
if (!self.addrStr) return next();

View File

@ -21,8 +21,8 @@ module.exports = function(app) {
// Address routes
var addresses = require('../app/controllers/addresses');
app.get('/api/addr/:addr', addresses.show);
app.param('addr', addresses.address);
// app.get('/api/addr/:addr', addresses.show);
app.get('/api/addr/:addr/unspents', addresses.unspents);
// Status route
var st = require('../app/controllers/status');