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..f68e08da 100644 --- a/app/models/Address.js +++ b/app/models/Address.js @@ -69,6 +69,36 @@ function spec() { } + + Address.prototype.getUnspents = function(next) { + var self = this; + 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){ + + // we are filtering out even unconfirmed spents! + // add || !txItem.spentIsConfirmed + if (!txItem.spentTxId) { + ret.push({ + address: self.addrStr, + txid: txItem.txid, + vout: txItem.index, + ts: txItem.ts, + amount: txItem.value_sat / BitcoreUtil.COIN, + confirmations: txItem.isConfirmed ? 1 : 0, // TODO => actually is 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'); diff --git a/insight.js b/insight.js index d4c2891d..d290dc06 100644 --- a/insight.js +++ b/insight.js @@ -1,5 +1,6 @@ 'use strict'; + //Set the node enviornment variable if not set before process.env.NODE_ENV = process.env.NODE_ENV || 'development';