diff --git a/src/hdnode.js b/src/hdnode.js index 198fe96..e478bb2 100644 --- a/src/hdnode.js +++ b/src/hdnode.js @@ -292,7 +292,7 @@ HDNode.prototype.isNeutered = function () { } HDNode.prototype.derivePath = function (path) { - typeforce(types.String, path) + typeforce(types.Path, path) var splitPath = path.split('/') if (splitPath[0] === 'm') { diff --git a/src/types.js b/src/types.js index 7ddf8c8..6a85ac9 100644 --- a/src/types.js +++ b/src/types.js @@ -26,6 +26,11 @@ function UInt53 (value) { Math.floor(value) === value } +function Path (value) { + return typeforce.String(value) && + value.match(/^([m]\/)?([0-9]+[']?\/)*([0-9]+[']?)$/) +} + // external dependent types var BigInt = typeforce.quacksLike('BigInteger') var ECPoint = typeforce.quacksLike('Point') @@ -57,7 +62,8 @@ var types = { UInt8: UInt8, UInt31: UInt31, UInt32: UInt32, - UInt53: UInt53 + UInt53: UInt53, + Path: Path } for (var typeName in typeforce) { diff --git a/test/hdnode.js b/test/hdnode.js index 2220835..d1c60f4 100644 --- a/test/hdnode.js +++ b/test/hdnode.js @@ -312,8 +312,7 @@ describe('HDNode', function () { var pathSplit = path.split('/').slice(i + 2) var pathEnd = pathSplit.join('/') - var pathEndM = 'm/' + path - + var pathEndM = 'm/' + pathEnd var child = cn.derivePath(pathEnd) verifyVector(child, cc, pathSplit.length + i + 1) @@ -390,7 +389,7 @@ describe('HDNode', function () { }, /Expected UInt32/) }) - it('throws on non-numbers', function () { + it('throws on wrong types', function () { var f = fixtures.valid[0] var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST) @@ -406,6 +405,30 @@ describe('HDNode', function () { assert.throws(function () { master.derive('foo') }, /Expected UInt32/) + assert.throws(function () { + master.derivePath() + }, /Expected Path/) + assert.throws(function () { + master.derivePath(2) + }, /Expected Path/) + assert.throws(function () { + master.derivePath([2, 3, 4]) + }, /Expected Path/) + assert.throws(function () { + master.derivePath('/') + }, /Expected Path/) + assert.throws(function () { + master.derivePath('m/m/123') + }, /Expected Path/) + assert.throws(function () { + master.derivePath('a/0/1/2') + }, /Expected Path/) + assert.throws(function () { + master.derivePath('m/0/ 1 /2') + }, /Expected Path/) + assert.throws(function () { + master.derivePath('m/0/1.5/2') + }, /Expected Path/) }) }) })