Merge pull request #267 from isocolsky/client_version

Client version
This commit is contained in:
Matias Alejo Garcia 2015-06-29 12:27:12 -03:00
commit 1d7b635d2a
4 changed files with 31 additions and 6 deletions

View File

@ -33,7 +33,7 @@ ExpressApp.prototype.start = function(opts, cb) {
this.app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'x-signature,x-identity,X-Requested-With,Content-Type,Authorization');
res.setHeader('Access-Control-Allow-Headers', 'x-signature,x-identity,x-client-version,X-Requested-With,Content-Type,Authorization');
next();
});
var allowCORS = function(req, res, next) {
@ -104,6 +104,13 @@ ExpressApp.prototype.start = function(opts, cb) {
};
};
function getServer(req, res, cb) {
var opts = {
clientVersion: req.header('x-client-version'),
};
return WalletService.getInstance(opts);
};
function getServerWithAuth(req, res, cb) {
var credentials = getCredentials(req);
if (!credentials)
@ -115,6 +122,7 @@ ExpressApp.prototype.start = function(opts, cb) {
copayerId: credentials.copayerId,
message: req.method.toLowerCase() + '|' + req.url + '|' + JSON.stringify(req.body),
signature: credentials.signature,
clientVersion: req.header('x-client-version'),
};
WalletService.getInstanceWithAuth(auth, function(err, server) {
if (err) return returnError(err, res, req);
@ -123,7 +131,7 @@ ExpressApp.prototype.start = function(opts, cb) {
};
router.post('/v1/wallets/', function(req, res) {
var server = WalletService.getInstance();
var server = getServer(req, res);
server.createWallet(req.body, function(err, walletId) {
if (err) return returnError(err, res, req);
res.json({
@ -134,7 +142,7 @@ ExpressApp.prototype.start = function(opts, cb) {
router.post('/v1/wallets/:id/copayers/', function(req, res) {
req.body.walletId = req.params['id'];
var server = WalletService.getInstance();
var server = getServer(req, res);
server.joinWallet(req.body, function(err, result) {
if (err) return returnError(err, res, req);

View File

@ -131,8 +131,16 @@ WalletService.shutDown = function(cb) {
});
};
WalletService.getInstance = function() {
return new WalletService();
/**
* Gets an instance of the server without authentication.
* @param {Object} opts
* @param {string} opts.clientVersion - A string that identifies the client issuing the request
*/
WalletService.getInstance = function(opts) {
opts = opts || {};
var server = new WalletService();
server.clientVersion = opts.clientVersion;
return server;
};
/**
@ -141,6 +149,7 @@ WalletService.getInstance = function() {
* @param {string} opts.copayerId - The copayer id making the request.
* @param {string} opts.message - The contents of the request to be signed.
* @param {string} opts.signature - Signature of message to be verified using the copayer's requestPubKey
* @param {string} opts.clientVersion - A string that identifies the client issuing the request
*/
WalletService.getInstanceWithAuth = function(opts, cb) {

View File

@ -2,7 +2,7 @@
"name": "bitcore-wallet-service",
"description": "A service for Mutisig HD Bitcoin Wallets",
"author": "BitPay Inc",
"version": "0.0.37",
"version": "0.0.38",
"keywords": [
"bitcoin",
"copay",

View File

@ -653,6 +653,14 @@ describe('Wallet service', function() {
});
});
describe('#getInstance', function() {
it('should get server instance', function() {
var server = WalletService.getInstance({
clientVersion: 'bwc-0.0.1',
});
server.clientVersion.should.equal('bwc-0.0.1');
});
});
describe('#getInstanceWithAuth', function() {
it('should get server instance for existing copayer', function(done) {