Browse Source

Merge pull request #1089 from eordano/check/hdpriv

Add precondition checks to HDPrivateKey.from*
patch-2
Manuel Aráoz 10 years ago
parent
commit
de121a43a9
  1. 14
      lib/hdprivatekey.js
  2. 15
      lib/hdpublickey.js
  3. 3
      lib/util/js.js
  4. 36
      test/hdkeys.js
  5. 11
      test/hdprivatekey.js
  6. 3
      test/hdpublickey.js

14
lib/hdprivatekey.js

@ -4,6 +4,7 @@
var assert = require('assert'); var assert = require('assert');
var buffer = require('buffer'); var buffer = require('buffer');
var _ = require('lodash'); var _ = require('lodash');
var $ = require('./util/preconditions');
var BN = require('./crypto/bn'); var BN = require('./crypto/bn');
var Base58 = require('./encoding/base58'); var Base58 = require('./encoding/base58');
@ -277,7 +278,18 @@ HDPrivateKey._validateNetwork = function(data, networkArg) {
return null; return null;
}; };
HDPrivateKey.fromJSON = HDPrivateKey.fromObject = HDPrivateKey.fromString = function(arg) { HDPrivateKey.fromJSON = function(arg) {
$.checkArgument(JSUtil.isValidJSON(arg), 'No valid JSON string was provided');
return new HDPrivateKey(arg);
};
HDPrivateKey.fromString = function(arg) {
$.checkArgument(_.isString(arg), 'No valid string was provided');
return new HDPrivateKey(arg);
};
HDPrivateKey.fromObject = function(arg) {
$.checkArgument(_.isObject(arg), 'No valid argument was provided');
return new HDPrivateKey(arg); return new HDPrivateKey(arg);
}; };

15
lib/hdpublickey.js

@ -1,6 +1,8 @@
'use strict'; 'use strict';
var _ = require('lodash'); var _ = require('lodash');
var $ = require('./util/preconditions');
var BN = require('./crypto/bn'); var BN = require('./crypto/bn');
var Base58 = require('./encoding/base58'); var Base58 = require('./encoding/base58');
var Base58Check = require('./encoding/base58check'); var Base58Check = require('./encoding/base58check');
@ -356,7 +358,18 @@ HDPublicKey._validateBufferArguments = function (arg) {
} }
}; };
HDPublicKey.fromString = HDPublicKey.fromObject = HDPublicKey.fromJSON = function(arg) { HDPublicKey.fromJSON = function(arg) {
$.checkArgument(JSUtil.isValidJSON(arg), 'No valid JSON string was provided');
return new HDPublicKey(arg);
};
HDPublicKey.fromObject = function(arg) {
$.checkArgument(_.isObject(arg), 'No valid argument was provided');
return new HDPublicKey(arg);
};
HDPublicKey.fromString = function(arg) {
$.checkArgument(_.isString(arg), 'No valid string was provided');
return new HDPublicKey(arg); return new HDPublicKey(arg);
}; };

3
lib/util/js.js

@ -29,6 +29,9 @@ module.exports = {
*/ */
isValidJSON: function isValidJSON(arg) { isValidJSON: function isValidJSON(arg) {
var parsed; var parsed;
if (!_.isString(arg)) {
return false;
}
try { try {
parsed = JSON.parse(arg); parsed = JSON.parse(arg);
} catch (e) { } catch (e) {

36
test/hdkeys.js

@ -10,12 +10,48 @@
/* jshint maxstatements: 100 */ /* jshint maxstatements: 100 */
/* jshint unused: false */ /* jshint unused: false */
var _ = require('lodash');
var should = require('chai').should(); var should = require('chai').should();
var expect = require('chai').expect;
var bitcore = require('..'); var bitcore = require('..');
var Networks = bitcore.Networks; var Networks = bitcore.Networks;
var HDPrivateKey = bitcore.HDPrivateKey; var HDPrivateKey = bitcore.HDPrivateKey;
var HDPublicKey = bitcore.HDPublicKey; var HDPublicKey = bitcore.HDPublicKey;
describe('HDKeys building with static methods', function() {
var classes = [HDPublicKey, HDPrivateKey];
var clazz, index;
_.each(classes, function(clazz) {
var expectStaticMethodFail = function(staticMethod, argument, message) {
expect(clazz[staticMethod].bind(null, argument)).to.throw(message);
};
it(clazz.name + ' fromJSON checks that a valid JSON is provided', function() {
var errorMessage = 'No valid JSON string was provided';
var method = 'fromJSON';
expectStaticMethodFail(method, undefined, errorMessage);
expectStaticMethodFail(method, null, errorMessage);
expectStaticMethodFail(method, 'invalid JSON', errorMessage);
expectStaticMethodFail(method, '{\'singlequotes\': true}', errorMessage);
expectStaticMethodFail(method, {}, errorMessage);
});
it(clazz.name + ' fromString checks that a string is provided', function() {
var errorMessage = 'No valid string was provided';
var method = 'fromString';
expectStaticMethodFail(method, undefined, errorMessage);
expectStaticMethodFail(method, null, errorMessage);
expectStaticMethodFail(method, {}, errorMessage);
});
it(clazz.name + ' fromObject checks that an object is provided', function() {
var errorMessage = 'No valid argument was provided';
var method = 'fromObject';
expectStaticMethodFail(method, undefined, errorMessage);
expectStaticMethodFail(method, null, errorMessage);
expectStaticMethodFail(method, '', errorMessage);
});
});
});
describe('BIP32 compliance', function() { describe('BIP32 compliance', function() {
it('should initialize test vector 1 from the extended public key', function() { it('should initialize test vector 1 from the extended public key', function() {

11
test/hdprivatekey.js

@ -78,14 +78,13 @@ describe('HDPrivate key interface', function() {
)); ));
}); });
describe('should error with a nonsensical argument', function() { describe('instantiation', function() {
it('like a number', function() { it('invalid argument: can not instantiate from a number', function() {
expectFailBuilding(1, hdErrors.UnrecognizedArgument); expectFailBuilding(1, hdErrors.UnrecognizedArgument);
}); });
}); it('allows no-new calling', function() {
HDPrivateKey(xprivkey).toString().should.equal(xprivkey);
it('allows no-new calling', function() { });
HDPrivateKey(xprivkey).toString().should.equal(xprivkey);
}); });
it('inspect() displays correctly', function() { it('inspect() displays correctly', function() {

3
test/hdpublickey.js

@ -122,7 +122,6 @@ describe('HDPublicKey interface', function() {
return new HDPublicKey(buffers); return new HDPublicKey(buffers);
}, errors.InvalidB58Checksum); }, errors.InvalidB58Checksum);
}); });
}); });
describe('error checking on serialization', function() { describe('error checking on serialization', function() {
@ -246,7 +245,7 @@ describe('HDPublicKey interface', function() {
it('rejects illegal paths', function() { it('rejects illegal paths', function() {
var valid; var valid;
valid = HDPublicKey.isValidPath('m/-1/12'); valid = HDPublicKey.isValidPath('m/-1/12');
valid.should.equal(false); valid.should.equal(false);

Loading…
Cancel
Save