From aa327131e8849c0f49e562c1220b7663bb932cfe Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 11 Aug 2017 05:12:18 -0700 Subject: [PATCH] hd: improve arg parsing. --- lib/hd/private.js | 13 ++++++++----- lib/hd/public.js | 7 +++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/hd/private.js b/lib/hd/private.js index 89c381a9..25e4f4ac 100644 --- a/lib/hd/private.js +++ b/lib/hd/private.js @@ -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); diff --git a/lib/hd/public.js b/lib/hd/public.js index 3ce7772d..6ff97a97 100644 --- a/lib/hd/public.js +++ b/lib/hd/public.js @@ -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.');