From 79814e10faf42e800918a2b097b9ef9cd8836a87 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 19 Feb 2014 21:47:00 -0300 Subject: [PATCH] add API call to return unspent outputs for an address --- app/controllers/addresses.js | 44 ++++++++++++++++++++++++------------ app/models/Address.js | 29 ++++++++++++++++++++++++ config/routes.js | 4 ++-- 3 files changed, 61 insertions(+), 16 deletions(-) diff --git a/app/controllers/addresses.js b/app/controllers/addresses.js index 95e49c1d..c7347c06 100644 --- a/app/controllers/addresses.js +++ b/app/controllers/addresses.js @@ -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); + } + }); }; + + diff --git a/app/models/Address.js b/app/models/Address.js index 9c804524..7dcca559 100644 --- a/app/models/Address.js +++ b/app/models/Address.js @@ -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(); diff --git a/config/routes.js b/config/routes.js index 44a3591f..f480d7e2 100644 --- a/config/routes.js +++ b/config/routes.js @@ -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');