hd: improve arg parsing.

This commit is contained in:
Christopher Jeffrey 2017-08-11 05:12:18 -07:00
parent e6cd60f919
commit aa327131e8
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 13 additions and 7 deletions

View File

@ -177,22 +177,25 @@ HDPrivateKey.prototype.destroy = function destroy(pub) {
/**
* Derive a child key.
* @param {Number|String} - Child index or path.
* @param {Number} index - Derivation index.
* @param {Boolean?} hardened - Whether the derivation should be hardened.
* @returns {HDPrivateKey}
*/
HDPrivateKey.prototype.derive = function derive(index, hardened) {
assert(util.isU32(index));
assert(typeof index === 'number');
if ((index >>> 0) !== index)
throw new Error('Index out of range.');
if (this.depth >= 0xff)
throw new Error('Depth too high.');
if (hardened) {
index |= common.HARDENED;
index >>>= 0;
}
if (this.depth >= 0xff)
throw new Error('Depth too high.');
const id = this.getID(index);
const cache = common.cache.get(id);

View File

@ -143,7 +143,7 @@ HDPublicKey.prototype.destroy = function destroy() {
/**
* Derive a child key.
* @param {Number|String} - Child index or path.
* @param {Number} index - Derivation index.
* @param {Boolean?} hardened - Whether the derivation
* should be hardened (throws if true).
* @returns {HDPrivateKey}
@ -151,7 +151,10 @@ HDPublicKey.prototype.destroy = function destroy() {
*/
HDPublicKey.prototype.derive = function derive(index, hardened) {
assert(util.isU32(index));
assert(typeof index === 'number');
if ((index >>> 0) !== index)
throw new Error('Index out of range.');
if ((index & common.HARDENED) || hardened)
throw new Error('Cannot derive hardened.');