improve parsing of m-of-n on wallet creation

This commit is contained in:
Ivan Socolsky 2015-02-21 15:19:22 -03:00
parent 09853e6af0
commit 8299f609ae
3 changed files with 64 additions and 12 deletions

View File

@ -19,7 +19,12 @@ var walletName = args[0];
var copayerName = args[2] || process.env.USER;
var network = program.testnet ? 'testnet' : 'livenet';
var mn = utils.parseMN(args[1]);
var mn;
try {
mn = utils.parseMN(args[1]);
} catch (ex) {
die(ex);
}
var client = utils.getClient(program);
client.createWallet(walletName, copayerName, mn[0], mn[1], network, function(err, secret) {

View File

@ -10,17 +10,17 @@ var die = Utils.die = function(err) {
}
};
Utils.parseMN = function(MN) {
if (!MN)
die('No m-n parameter');
var mn = MN.split('-');
Utils.parseMN = function(text) {
if (!text) throw new Error('No m-n parameter');
var m = parseInt(mn[0]);
var n = parseInt(mn[1]);
var regex = /^(\d+)(-|of|-of-)?(\d+)$/i;
var match = regex.exec(text.trim());
if (!m || !n) {
die('Bad m-n parameter:' + MN);
}
if (!match || match.length === 0) throw new Error('Invalid m-n parameter');
var m = parseInt(match[1]);
var n = parseInt(match[3]);
if (m > n) throw new Error('Invalid m-n parameter');
return [m, n];
};

View File

@ -4,9 +4,56 @@ var _ = require('lodash');
var chai = require('chai');
var sinon = require('sinon');
var should = chai.should();
var CliUtils = require('./cli-utils');
var CliUtils = require('../cli-utils');
describe('CliUtils', function() {
describe('#parseMN', function() {
it('should successfully parse m & n', function() {
var texts = {
'1-1': [1, 1],
'1-of-1': [1, 1],
'1of1': [1, 1],
'1-OF-2': [1, 2],
'1OF2': [1, 2],
' 2-2': [2, 2],
'2-3 ': [2, 3],
'10-10': [10, 10],
'10-of-10': [10, 10],
};
_.each(texts, function(expected, text) {
var result = CliUtils.parseMN(text);
result.should.deep.equal(expected);
});
});
it('should fail to parse incorrect m & n', function() {
var texts = [
'',
' ',
'1',
'x-1',
'1-x',
'of-1-1',
'2-2-of',
'1-1-1',
' 1_1 ',
'2-1',
'2-of-1',
'-1-2',
'1--2',
'x-of-2',
];
_.each(texts, function(text) {
var valid = true;
try {
CliUtils.parseMN(text);
} catch (e) {
valid = false;
}
valid.should.be.false;
});
});
});
describe('#parseAmount', function() {
it('should successfully parse amounts', function() {
var texts = {
@ -56,7 +103,7 @@ describe('CliUtils', function() {
_.each(texts, function(text) {
var valid = true;
try {
var amount = CliUtils.parseAmount(text);
CliUtils.parseAmount(text);
} catch (e) {
valid = false;
}