Add precondition checks to HDPrivateKey.from*
This commit is contained in:
parent
20315b5fa4
commit
0938eadab5
@ -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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -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) {
|
||||||
|
|||||||
@ -78,14 +78,39 @@ 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);
|
var expectStaticMethodFail = function(staticMethod, argument, message) {
|
||||||
|
expect(HDPrivateKey[staticMethod].bind(null, argument)).to.throw(message);
|
||||||
|
};
|
||||||
|
it('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('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('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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('inspect() displays correctly', function() {
|
it('inspect() displays correctly', function() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user