From f418009ebf4d3b6d82857c62ffd01fe5e13c930c Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Wed, 16 Mar 2016 16:42:39 -0300 Subject: [PATCH] add express endpoint --- lib/expressapp.js | 13 +++++++++++++ lib/server.js | 6 ++++-- test/expressapp.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/lib/expressapp.js b/lib/expressapp.js index 74f085b..b4f9a08 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -350,6 +350,19 @@ ExpressApp.prototype.start = function(opts, cb) { }); }); + router.get('/v1/sendmaxinfo/', function(req, res) { + getServerWithAuth(req, res, function(server) { + var opts = {}; + opts.feePerKb = +req.query.feePerKb; + if (req.query.excludeUnconfirmedUtxos == '1') opts.excludeUnconfirmedUtxos = true; + if (req.query.returnInputs == '1') opts.returnInputs = true; + server.getSendMaxInfo(opts, function(err, info) { + if (err) return returnError(err, res, req); + res.json(info); + }); + }); + }); + router.get('/v1/utxos/', function(req, res) { var opts = {}; var addresses = req.query.addresses; diff --git a/lib/server.js b/lib/server.js index d05cb34..e7ac147 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1207,11 +1207,13 @@ WalletService.prototype.getSendMaxInfo = function(opts, cb) { lastFee = fee; }); - info.size = txp.getEstimatedSize(); info.fee = txp.getEstimatedFee(); info.amount = _.sum(txp.inputs, 'satoshis') - info.fee; - if (opts.returnInputs) info.inputs = txp.inputs; + if (opts.returnInputs) { + // TODO: Shuffle inputs + info.inputs = txp.inputs; + } return cb(null, info); }); diff --git a/test/expressapp.js b/test/expressapp.js index 3f20506..e70b60b 100644 --- a/test/expressapp.js +++ b/test/expressapp.js @@ -113,6 +113,38 @@ describe('ExpressApp', function() { }); }); + it('/v1/sendmaxinfo', function(done) { + var server = { + getSendMaxInfo: sinon.stub().callsArgWith(1, null, { + amount: 123 + }), + }; + var TestExpressApp = proxyquire('../lib/expressapp', { + './server': { + initialize: sinon.stub().callsArg(1), + getInstanceWithAuth: sinon.stub().callsArgWith(1, null, server), + } + }); + start(TestExpressApp, function() { + var requestOptions = { + url: testHost + ':' + testPort + config.basePath + '/v1/sendmaxinfo?feePerKb=10000&returnInputs=1', + headers: { + 'x-identity': 'identity', + 'x-signature': 'signature' + } + }; + request(requestOptions, function(err, res, body) { + should.not.exist(err); + res.statusCode.should.equal(200); + var args = server.getSendMaxInfo.getCalls()[0].args[0]; + args.feePerKb.should.equal(10000); + args.returnInputs.should.be.true; + JSON.parse(body).amount.should.equal(123); + done(); + }); + }); + }); + describe('Balance', function() { it('should handle cache argument', function(done) { var server = {