block access to BWC < 1.2 clients
This commit is contained in:
parent
0f3449a864
commit
b863df0b70
|
@ -122,4 +122,24 @@ Utils.formatSize = function(size) {
|
|||
return (size / 1000.).toFixed(4) + 'kB';
|
||||
};
|
||||
|
||||
Utils.parseVersion = function(version) {
|
||||
var v = {};
|
||||
|
||||
if (!version) return null;
|
||||
|
||||
var x = version.split('-');
|
||||
if (x.length != 2) {
|
||||
v.agent = version;
|
||||
return v;
|
||||
}
|
||||
v.agent = _.contains(['bwc', 'bws'], x[0]) ? 'bwc' : x[0];
|
||||
x = x[1].split('.');
|
||||
v.major = parseInt(x[0]);
|
||||
v.minor = parseInt(x[1]);
|
||||
v.patch = parseInt(x[2]);
|
||||
|
||||
return v;
|
||||
};
|
||||
|
||||
|
||||
module.exports = Utils;
|
||||
|
|
|
@ -170,6 +170,14 @@ WalletService.shutDown = function(cb) {
|
|||
*/
|
||||
WalletService.getInstance = function(opts) {
|
||||
opts = opts || {};
|
||||
|
||||
var version = Utils.parseVersion(opts.clientVersion);
|
||||
if (version && version.agent == 'bwc') {
|
||||
if (version.major == 0 || (version.major == 1 && version.minor < 2)) {
|
||||
throw new ClientError(Errors.codes.UPGRADE_NEEDED, 'BWC clients < 1.2 are no longer supported.');
|
||||
}
|
||||
}
|
||||
|
||||
var server = new WalletService();
|
||||
server._setClientVersion(opts.clientVersion);
|
||||
return server;
|
||||
|
@ -186,7 +194,13 @@ WalletService.getInstance = function(opts) {
|
|||
WalletService.getInstanceWithAuth = function(opts, cb) {
|
||||
if (!checkRequired(opts, ['copayerId', 'message', 'signature'], cb)) return;
|
||||
|
||||
var server = new WalletService();
|
||||
var server;
|
||||
try {
|
||||
server = WalletService.getInstance(opts);
|
||||
} catch (ex) {
|
||||
return cb(ex);
|
||||
}
|
||||
|
||||
server.storage.fetchCopayerLookup(opts.copayerId, function(err, copayer) {
|
||||
if (err) return cb(err);
|
||||
if (!copayer) return cb(new ClientError(Errors.codes.NOT_AUTHORIZED, 'Copayer not found'));
|
||||
|
@ -197,7 +211,6 @@ WalletService.getInstanceWithAuth = function(opts, cb) {
|
|||
|
||||
server.copayerId = opts.copayerId;
|
||||
server.walletId = copayer.walletId;
|
||||
server._setClientVersion(opts.clientVersion);
|
||||
return cb(null, server);
|
||||
});
|
||||
};
|
||||
|
@ -550,27 +563,8 @@ WalletService.prototype._setClientVersion = function(version) {
|
|||
};
|
||||
|
||||
WalletService.prototype._parseClientVersion = function() {
|
||||
function parse(version) {
|
||||
var v = {};
|
||||
|
||||
if (!version) return null;
|
||||
|
||||
var x = version.split('-');
|
||||
if (x.length != 2) {
|
||||
v.agent = version;
|
||||
return v;
|
||||
}
|
||||
v.agent = _.contains(['bwc', 'bws'], x[0]) ? 'bwc' : x[0];
|
||||
x = x[1].split('.');
|
||||
v.major = parseInt(x[0]);
|
||||
v.minor = parseInt(x[1]);
|
||||
v.patch = parseInt(x[2]);
|
||||
|
||||
return v;
|
||||
};
|
||||
|
||||
if (_.isUndefined(this.parsedClientVersion)) {
|
||||
this.parsedClientVersion = parse(this.clientVersion);
|
||||
this.parsedClientVersion = Utils.parseVersion(this.clientVersion);
|
||||
}
|
||||
return this.parsedClientVersion;
|
||||
};
|
||||
|
|
|
@ -51,13 +51,46 @@ describe('Wallet service', function() {
|
|||
describe('#getInstance', function() {
|
||||
it('should get server instance', function() {
|
||||
var server = WalletService.getInstance({
|
||||
clientVersion: 'bwc-0.0.1',
|
||||
clientVersion: 'bwc-2.9.0',
|
||||
});
|
||||
server.clientVersion.should.equal('bwc-0.0.1');
|
||||
server.clientVersion.should.equal('bwc-2.9.0');
|
||||
});
|
||||
it('should not get server instance for BWC lower than v1.2', function() {
|
||||
var err;
|
||||
try {
|
||||
var server = WalletService.getInstance({
|
||||
clientVersion: 'bwc-1.1.99',
|
||||
});
|
||||
} catch (ex) {
|
||||
err = ex;
|
||||
}
|
||||
should.exist(err);
|
||||
err.code.should.equal('UPGRADE_NEEDED');
|
||||
});
|
||||
it('should get server instance for non-BWC clients', function() {
|
||||
var server = WalletService.getInstance({
|
||||
clientVersion: 'dummy-1.0.0',
|
||||
});
|
||||
server.clientVersion.should.equal('dummy-1.0.0');
|
||||
server = WalletService.getInstance({});
|
||||
(clientVersion == null).should.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getInstanceWithAuth', function() {
|
||||
it('should not get server instance for BWC lower than v1.2', function(done) {
|
||||
var server = WalletService.getInstanceWithAuth({
|
||||
copayerId: '1234',
|
||||
message: 'hello world',
|
||||
signature: 'xxx',
|
||||
clientVersion: 'bwc-1.1.99',
|
||||
}, function(err, server) {
|
||||
should.exist(err);
|
||||
should.not.exist(server);
|
||||
err.code.should.equal('UPGRADE_NEEDED');
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should get server instance for existing copayer', function(done) {
|
||||
helpers.createAndJoinWallet(1, 2, function(s, wallet) {
|
||||
var xpriv = TestData.copayers[0].xPrivKey;
|
||||
|
@ -69,18 +102,18 @@ describe('Wallet service', function() {
|
|||
copayerId: wallet.copayers[0].id,
|
||||
message: 'hello world',
|
||||
signature: sig,
|
||||
clientVersion: 'bwc-0.0.1',
|
||||
clientVersion: 'bwc-2.0.0',
|
||||
}, function(err, server) {
|
||||
should.not.exist(err);
|
||||
server.walletId.should.equal(wallet.id);
|
||||
server.copayerId.should.equal(wallet.copayers[0].id);
|
||||
server.clientVersion.should.equal('bwc-0.0.1');
|
||||
server.clientVersion.should.equal('bwc-2.0.0');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail when requesting for non-existent copayer', function(done) {
|
||||
it.only('should fail when requesting for non-existent copayer', function(done) {
|
||||
var message = 'hello world';
|
||||
var opts = {
|
||||
copayerId: 'dummy',
|
||||
|
|
Loading…
Reference in New Issue