Add validation of required arguments for future use

This commit is contained in:
Yemel Jardi 2014-08-11 17:10:13 -03:00
parent ac525f226c
commit ea22f1361f
5 changed files with 20 additions and 6426 deletions

View File

@ -83,7 +83,7 @@ var createBitcore = function(opts) {
submodules.splice(submodules.indexOf('lib/PeerManager'), 1);
submodules.splice(submodules.indexOf('lib/NetworkMonitor'), 1);
var assert = require('assert');
assert(submodules.length == modules.length - 7);
assert(submodules.length == modules.length - 8);
}
if (opts.submodules) {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -66,7 +66,8 @@ BIP21.prototype.parse = function(uri) {
}
}
BIP21.prototype.isValid = function() {
BIP21.prototype.isValid = function(known) {
var knownArguments = known || [];
var valid = true;
if (typeof(this.data.amount) != 'undefined') {
@ -80,6 +81,13 @@ BIP21.prototype.isValid = function() {
// Require address or PayPro info
valid &= !!(this.address || this.data.r);
// Check required arguments
for (var key in this.data) {
if (key.indexOf('req-') == 0) {
valid &= knownArguments.indexOf(key) != -1;
}
}
return !!valid;
}
@ -92,8 +100,6 @@ BIP21.prototype.setAddress = function(addr) {
}
BIP21.prototype.getURI = function() {
if (!this.isValid()) throw new Error('Invalid state');
return URL.format({
protocol: 'bitcoin:',
host: this.address,

View File

@ -130,16 +130,6 @@ describe('BIP21', function() {
);
});
it('should fail with wrong arguments', function() {
(function() {
new BIP21(12);
}).should.throw(Error);
(function() {
new BIP21();
}).should.not.throw(Error);
});
it('should be case insensitive to protocol', function() {
var uri1 = new BIP21('bItcOin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
var uri2 = new BIP21('bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
@ -154,4 +144,13 @@ describe('BIP21', function() {
uri.setAddress('1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
uri.address.network().name.should.equal('livenet');
});
});
it('should check required arguments', function() {
var uri = new BIP21('bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?req-somethingyoudontunderstand=50&req-somethingelseyoudontget=999');
uri.isValid().should.be.false;
uri.isValid([
'req-somethingyoudontunderstand',
'req-somethingelseyoudontget'
]).should.be.true;
});
});