allow 1 <= n <= 15 && 1 <= m <= n

This commit is contained in:
Ivan Socolsky 2016-05-09 11:10:56 -03:00
parent 1ce36b6742
commit 700c898508
No known key found for this signature in database
GPG Key ID: FAECE6A05FAA4F56
3 changed files with 76 additions and 47 deletions

View File

@ -18,8 +18,8 @@ Wallet.create = function(opts) {
var x = new Wallet();
$.checkArgument(_.isNumber(opts.m), '"m" is expected to be a number');
$.checkArgument(_.isNumber(opts.n), '"n" is expected to be a number');
$.shouldBeNumber(opts.m);
$.shouldBeNumber(opts.n);
x.version = '1.0.0';
x.createdOn = Math.floor(Date.now() / 1000);
@ -47,8 +47,8 @@ Wallet.create = function(opts) {
Wallet.fromObj = function(obj) {
var x = new Wallet();
$.checkArgument(_.isNumber(obj.m), '"m" is expected to be a number');
$.checkArgument(_.isNumber(obj.n), '"n" is expected to be a number');
$.shouldBeNumber(obj.m);
$.shouldBeNumber(obj.n);
x.version = obj.version;
x.createdOn = obj.createdOn;
@ -77,22 +77,6 @@ Wallet.prototype.toObject = function() {
return x;
};
/* For compressed keys, m*73 + n*34 <= 496 */
Wallet.COPAYER_PAIR_LIMITS = {
1: 1,
2: 2,
3: 3,
4: 4,
5: 4,
6: 4,
7: 3,
8: 3,
9: 2,
10: 2,
11: 1,
12: 1,
};
/**
* Get the maximum allowed number of required copayers.
* This is a limit imposed by the maximum allowed size of the scriptSig.
@ -104,7 +88,7 @@ Wallet.getMaxRequiredCopayers = function(totalCopayers) {
};
Wallet.verifyCopayerLimits = function(m, n) {
return (n >= 1 && n <= 12) && (m >= 1 && m <= Wallet.COPAYER_PAIR_LIMITS[n]);
return (n >= 1 && n <= 15) && (m >= 1 && m <= n);
};
Wallet.prototype.isShared = function() {

View File

@ -168,7 +168,6 @@ describe('Wallet service', function() {
});
});
it('should fail to create wallet with no name', function(done) {
var opts = {
name: '',
@ -184,37 +183,71 @@ describe('Wallet service', function() {
});
});
it('should fail to create wallet with invalid copayer pairs', function(done) {
var invalidPairs = [{
it('should check m-n combination', function(done) {
var pairs = [{
m: 0,
n: 0
}, {
m: 0,
n: 2
}, {
m: 2,
n: 1
}, {
m: 0,
n: 10
n: 0,
valid: false,
}, {
m: 1,
n: 20
n: 1,
valid: true,
}, {
m: 2,
n: 3,
valid: true,
}, {
m: 0,
n: 2,
valid: false,
}, {
m: 2,
n: 1,
valid: false,
}, {
m: 0,
n: 10,
valid: false,
}, {
m: 1,
n: 20,
valid: false,
}, {
m: 10,
n: 10
n: 10,
valid: true,
}, {
m: 15,
n: 15,
valid: true,
}, {
m: 16,
n: 16,
valid: false,
}, {
m: 1,
n: 15,
valid: true,
}, {
m: -2,
n: -2,
valid: false,
}, ];
var opts = {
id: '123',
name: 'my wallet',
pubKey: TestData.keyPair.pub,
};
async.each(invalidPairs, function(pair, cb) {
async.each(pairs, function(pair, cb) {
opts.m = pair.m;
opts.n = pair.n;
server.createWallet(opts, function(err) {
should.exist(err);
err.message.should.equal('Invalid combination of required copayers / total copayers');
if (!pair.valid) {
should.exist(err);
err.message.should.equal('Invalid combination of required copayers / total copayers');
} else {
should.not.exist(err);
}
return cb();
});
}, function(err) {

View File

@ -12,22 +12,34 @@ describe('Wallet', function() {
describe('#create', function() {
it('will throw with an invalid string argument for "m" or "n"', function() {
(function() {
Wallet.create({m: '2', n: 2});
}).should.throw('"m" is expected to be a number');
Wallet.create({
m: '2',
n: 2
});
}).should.throw('Variable should be a Number.');
(function() {
Wallet.create({m: 2, n: '2'});
}).should.throw('"n" is expected to be a number');
Wallet.create({
m: 2,
n: '2'
});
}).should.throw('Variable should be a Number.');
});
});
describe('#fromObj', function() {
it('will throw with an invalid string argument for "m" or "n"', function() {
(function() {
Wallet.fromObj({m: '2', n: 2});
}).should.throw('"m" is expected to be a number');
Wallet.fromObj({
m: '2',
n: 2
});
}).should.throw('Variable should be a Number.');
(function() {
Wallet.fromObj({m: 2, n: '2'});
}).should.throw('"n" is expected to be a number');
Wallet.fromObj({
m: 2,
n: '2'
});
}).should.throw('Variable should be a Number.');
});
it('read a wallet', function() {
var w = Wallet.fromObj(testWallet);