Add validation of required arguments for future use
This commit is contained in:
parent
ac525f226c
commit
ea22f1361f
|
@ -83,7 +83,7 @@ var createBitcore = function(opts) {
|
||||||
submodules.splice(submodules.indexOf('lib/PeerManager'), 1);
|
submodules.splice(submodules.indexOf('lib/PeerManager'), 1);
|
||||||
submodules.splice(submodules.indexOf('lib/NetworkMonitor'), 1);
|
submodules.splice(submodules.indexOf('lib/NetworkMonitor'), 1);
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
assert(submodules.length == modules.length - 7);
|
assert(submodules.length == modules.length - 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts.submodules) {
|
if (opts.submodules) {
|
||||||
|
|
File diff suppressed because one or more lines are too long
5961
browser/testdata.js
5961
browser/testdata.js
File diff suppressed because one or more lines are too long
12
lib/BIP21.js
12
lib/BIP21.js
|
@ -66,7 +66,8 @@ BIP21.prototype.parse = function(uri) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BIP21.prototype.isValid = function() {
|
BIP21.prototype.isValid = function(known) {
|
||||||
|
var knownArguments = known || [];
|
||||||
var valid = true;
|
var valid = true;
|
||||||
|
|
||||||
if (typeof(this.data.amount) != 'undefined') {
|
if (typeof(this.data.amount) != 'undefined') {
|
||||||
|
@ -80,6 +81,13 @@ BIP21.prototype.isValid = function() {
|
||||||
// Require address or PayPro info
|
// Require address or PayPro info
|
||||||
valid &= !!(this.address || this.data.r);
|
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;
|
return !!valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,8 +100,6 @@ BIP21.prototype.setAddress = function(addr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
BIP21.prototype.getURI = function() {
|
BIP21.prototype.getURI = function() {
|
||||||
if (!this.isValid()) throw new Error('Invalid state');
|
|
||||||
|
|
||||||
return URL.format({
|
return URL.format({
|
||||||
protocol: 'bitcoin:',
|
protocol: 'bitcoin:',
|
||||||
host: this.address,
|
host: this.address,
|
||||||
|
|
|
@ -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() {
|
it('should be case insensitive to protocol', function() {
|
||||||
var uri1 = new BIP21('bItcOin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
|
var uri1 = new BIP21('bItcOin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
|
||||||
var uri2 = new BIP21('bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
|
var uri2 = new BIP21('bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
|
||||||
|
@ -154,4 +144,13 @@ describe('BIP21', function() {
|
||||||
uri.setAddress('1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
|
uri.setAddress('1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
|
||||||
uri.address.network().name.should.equal('livenet');
|
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;
|
||||||
|
});
|
||||||
});
|
});
|
Loading…
Reference in New Issue