Browse Source

Add validation of required arguments for future use

patch-2
Yemel Jardi 11 years ago
parent
commit
ea22f1361f
  1. 2
      browser/build.js
  2. 450
      browser/bundle.js
  3. 5961
      browser/testdata.js
  4. 12
      lib/BIP21.js
  5. 21
      test/test.BIP21.js

2
browser/build.js

@ -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) {

450
browser/bundle.js

File diff suppressed because one or more lines are too long

5961
browser/testdata.js

File diff suppressed because one or more lines are too long

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;
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,

21
test/test.BIP21.js

@ -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;
});
});

Loading…
Cancel
Save