add put endpoint, rename module to wallet-api
This commit is contained in:
parent
44cb188c61
commit
789c18a6df
@ -14,6 +14,8 @@ var storage = multer.memoryStorage();
|
|||||||
var upload = multer({ storage: storage });
|
var upload = multer({ storage: storage });
|
||||||
var validators = require('./validators');
|
var validators = require('./validators');
|
||||||
var utils = require('./utils');
|
var utils = require('./utils');
|
||||||
|
var _ = require('lodash');
|
||||||
|
var bodyParser = require('body-parser');
|
||||||
|
|
||||||
var WalletService = function(options) {
|
var WalletService = function(options) {
|
||||||
BaseService.call(this, options);
|
BaseService.call(this, options);
|
||||||
@ -50,11 +52,12 @@ WalletService.prototype._endpointUTXOs = function() {
|
|||||||
var self = this;
|
var self = this;
|
||||||
return function(req, res) {
|
return function(req, res) {
|
||||||
var walletId = req.params.walletId;
|
var walletId = req.params.walletId;
|
||||||
|
var queryMempool = req.query.queryMempool === false ? false : true;
|
||||||
//var tip = self.node.bitcoind.tip;
|
//var tip = self.node.bitcoind.tip;
|
||||||
// TODO: get the height of the tip
|
// TODO: get the height of the tip
|
||||||
//var height = tip;
|
//var height = tip;
|
||||||
var height = null;
|
var height = null;
|
||||||
self._getUtxos(walletId, height, function(err, utxos) {
|
self._getUtxos(walletId, height, options, function(err, utxos) {
|
||||||
if(err) {
|
if(err) {
|
||||||
return utils.sendError(err);
|
return utils.sendError(err);
|
||||||
}
|
}
|
||||||
@ -70,11 +73,18 @@ WalletService.prototype._endpointGetBalance= function() {
|
|||||||
var self = this;
|
var self = this;
|
||||||
return function(req, res) {
|
return function(req, res) {
|
||||||
var walletId = req.params.walletId;
|
var walletId = req.params.walletId;
|
||||||
|
var queryMempool = req.query.queryMempool === false ? false : true;
|
||||||
|
|
||||||
//var tip = self.node.bitcoind.tip;
|
//var tip = self.node.bitcoind.tip;
|
||||||
// TODO: get the height of the tip
|
// TODO: get the height of the tip
|
||||||
//var height = tip;
|
//var height = tip;
|
||||||
var height = null;
|
var height = null;
|
||||||
self._getBalance(walletId, height, function(err, balance) {
|
|
||||||
|
var options = {
|
||||||
|
queryMempool: queryMempool
|
||||||
|
};
|
||||||
|
|
||||||
|
self._getBalance(walletId, height, options, function(err, balance) {
|
||||||
if(err) {
|
if(err) {
|
||||||
return utils.sendError(err);
|
return utils.sendError(err);
|
||||||
}
|
}
|
||||||
@ -93,8 +103,13 @@ WalletService.prototype._endpointGetAddresses = function() {
|
|||||||
|
|
||||||
self._getAddresses(walletId, function(err, addresses) {
|
self._getAddresses(walletId, function(err, addresses) {
|
||||||
if(err) {
|
if(err) {
|
||||||
return utils.sendError(err);
|
return utils.sendError(err, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!addresses) {
|
||||||
|
return res.status(404).send('Not found');
|
||||||
|
}
|
||||||
|
|
||||||
res.status(200).jsonp({
|
res.status(200).jsonp({
|
||||||
addresses: addresses
|
addresses: addresses
|
||||||
});
|
});
|
||||||
@ -118,7 +133,45 @@ WalletService.prototype._endpointPostAddresses = function() {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
WalletService.prototype._getUtxos = function(walletId, height, callback) {
|
WalletService.prototype._endpointPutAddresses = function() {
|
||||||
|
var self = this;
|
||||||
|
return function(req, res) {
|
||||||
|
var newAddresses = req.body;
|
||||||
|
|
||||||
|
if(!Array.isArray(req.body)) {
|
||||||
|
return utils.sendError(new Error('Must PUT an array'), res);
|
||||||
|
}
|
||||||
|
|
||||||
|
var walletId = req.params.walletId;
|
||||||
|
|
||||||
|
self._getAddresses(walletId, function(err, oldAddresses) {
|
||||||
|
if(err) {
|
||||||
|
return utils.sendError(err, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!oldAddresses) {
|
||||||
|
return res.status(404).send('Not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
var allAddresses = _.union(oldAddresses, newAddresses);
|
||||||
|
|
||||||
|
var amountAdded = allAddresses.length - oldAddresses.length;
|
||||||
|
|
||||||
|
self._storeAddresses(walletId, allAddresses, function(err) {
|
||||||
|
if(err) {
|
||||||
|
return utils.sendError(err, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).jsonp({
|
||||||
|
walletId: walletId,
|
||||||
|
amountAdded: amountAdded
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
WalletService.prototype._getUtxos = function(walletId, height, options, callback) {
|
||||||
// TODO get the balance only to this height
|
// TODO get the balance only to this height
|
||||||
var self = this;
|
var self = this;
|
||||||
self._getAddresses(walletId, function(err, addresses) {
|
self._getAddresses(walletId, function(err, addresses) {
|
||||||
@ -126,20 +179,20 @@ WalletService.prototype._getUtxos = function(walletId, height, callback) {
|
|||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.node.services.bitcoind.getAddressUnspentOutputs(addresses, {queryMempool: false}, callback);
|
self.node.services.bitcoind.getAddressUnspentOutputs(addresses, options, callback);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
WalletService.prototype._getBalance = function(walletId, height, callback) {
|
WalletService.prototype._getBalance = function(walletId, height, options, callback) {
|
||||||
// TODO get the balance only to this height
|
// TODO get the balance only to this height
|
||||||
var self = this;
|
var self = this;
|
||||||
self._getAddresses(walletId, function(err, addresses) {
|
self._getAddresses(walletId, function(err, addresses) {
|
||||||
if(err) {
|
if(err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
self.node.services.bitcoind.getAddressUnspentOutputs(addresses, {
|
|
||||||
queryMempool: false
|
|
||||||
}, function(err, utxos) {
|
self.node.services.bitcoind.getAddressUnspentOutputs(addresses, options, function(err, utxos) {
|
||||||
if(err) {
|
if(err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
@ -168,6 +221,9 @@ WalletService.prototype._endpointGetInfo = function() {
|
|||||||
WalletService.prototype.setupRoutes = function(app, express) {
|
WalletService.prototype.setupRoutes = function(app, express) {
|
||||||
var s = this;
|
var s = this;
|
||||||
var v = validators;
|
var v = validators;
|
||||||
|
|
||||||
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
app.get('/info',
|
app.get('/info',
|
||||||
s._endpointGetInfo()
|
s._endpointGetInfo()
|
||||||
);
|
);
|
||||||
@ -180,15 +236,19 @@ WalletService.prototype.setupRoutes = function(app, express) {
|
|||||||
app.get('/wallets/:walletId',
|
app.get('/wallets/:walletId',
|
||||||
s._endpointGetAddresses()
|
s._endpointGetAddresses()
|
||||||
);
|
);
|
||||||
app.post('/wallets/addresses',
|
app.put('/wallets/:walletId/addresses',
|
||||||
|
s._endpointPutAddresses()
|
||||||
|
);
|
||||||
|
app.post('/wallets',
|
||||||
upload.single('addresses'),
|
upload.single('addresses'),
|
||||||
v.checkAddresses,
|
v.checkAddresses,
|
||||||
s._endpointPostAddresses()
|
s._endpointPostAddresses()
|
||||||
);
|
);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
WalletService.prototype.getRoutePrefix = function() {
|
WalletService.prototype.getRoutePrefix = function() {
|
||||||
return 'bws';
|
return 'wallet-api';
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = WalletService;
|
module.exports = WalletService;
|
||||||
@ -147,6 +147,9 @@ exports.checkWalletId = function(req, res, next) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.checkAddresses = function(req, res, next) {
|
exports.checkAddresses = function(req, res, next) {
|
||||||
|
if(req.body) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
if (!req.file || !req.file.buffer) {
|
if (!req.file || !req.file.buffer) {
|
||||||
generateError(406, 'Content-Type must be set to multipart/form' +
|
generateError(406, 'Content-Type must be set to multipart/form' +
|
||||||
Loading…
Reference in New Issue
Block a user