HDPublicKey: Add precondition checks for static methods
This commit is contained in:
parent
0938eadab5
commit
9c3170cb3a
@ -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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -91,25 +91,25 @@ describe('HDPrivate key interface', function() {
|
|||||||
it('fromJSON checks that a valid JSON is provided', function() {
|
it('fromJSON checks that a valid JSON is provided', function() {
|
||||||
var errorMessage = 'No valid JSON string was provided';
|
var errorMessage = 'No valid JSON string was provided';
|
||||||
var method = 'fromJSON';
|
var method = 'fromJSON';
|
||||||
expectStaticMethodFail(method, undefined, errorMessage);
|
expectStaticMethodFail(method, undefined, errorMessage);
|
||||||
expectStaticMethodFail(method, null, errorMessage);
|
expectStaticMethodFail(method, null, errorMessage);
|
||||||
expectStaticMethodFail(method, 'invalid JSON', errorMessage);
|
expectStaticMethodFail(method, 'invalid JSON', errorMessage);
|
||||||
expectStaticMethodFail(method, '{\'singlequotes\': true}', errorMessage);
|
expectStaticMethodFail(method, '{\'singlequotes\': true}', errorMessage);
|
||||||
expectStaticMethodFail(method, {}, errorMessage);
|
expectStaticMethodFail(method, {}, errorMessage);
|
||||||
});
|
});
|
||||||
it('fromString checks that a string is provided', function() {
|
it('fromString checks that a string is provided', function() {
|
||||||
var errorMessage = 'No valid string was provided';
|
var errorMessage = 'No valid string was provided';
|
||||||
var method = 'fromString';
|
var method = 'fromString';
|
||||||
expectStaticMethodFail(method, undefined, errorMessage);
|
expectStaticMethodFail(method, undefined, errorMessage);
|
||||||
expectStaticMethodFail(method, null, errorMessage);
|
expectStaticMethodFail(method, null, errorMessage);
|
||||||
expectStaticMethodFail(method, {}, errorMessage);
|
expectStaticMethodFail(method, {}, errorMessage);
|
||||||
});
|
});
|
||||||
it('fromObject checks that an object is provided', function() {
|
it('fromObject checks that an object is provided', function() {
|
||||||
var errorMessage = 'No valid argument was provided';
|
var errorMessage = 'No valid argument was provided';
|
||||||
var method = 'fromObject';
|
var method = 'fromObject';
|
||||||
expectStaticMethodFail(method, undefined, errorMessage);
|
expectStaticMethodFail(method, undefined, errorMessage);
|
||||||
expectStaticMethodFail(method, null, errorMessage);
|
expectStaticMethodFail(method, null, errorMessage);
|
||||||
expectStaticMethodFail(method, '', errorMessage);
|
expectStaticMethodFail(method, '', errorMessage);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -122,7 +122,35 @@ describe('HDPublicKey interface', function() {
|
|||||||
return new HDPublicKey(buffers);
|
return new HDPublicKey(buffers);
|
||||||
}, errors.InvalidB58Checksum);
|
}, errors.InvalidB58Checksum);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('building with static methods', function() {
|
||||||
|
var expectStaticMethodFail = function(staticMethod, argument, message) {
|
||||||
|
expect(HDPublicKey[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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('error checking on serialization', function() {
|
describe('error checking on serialization', function() {
|
||||||
@ -246,7 +274,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…
Reference in New Issue
Block a user