Skip to content
Prev Previous commit
Next Next commit
Validating path
Again through typeforce
  • Loading branch information
karelbilek committed Feb 13, 2016
commit d2b43f1dfee056fe115a3cf9571c06039d6fd17d
2 changes: 1 addition & 1 deletion src/hdnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ HDNode.prototype.isNeutered = function () {
}

HDNode.prototype.derivePath = function (path) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why this just doesn't take a string as input?

typeforce(types.String, path)
typeforce(types.Path, path)

var splitPath = path.split('/')
if (splitPath[0] === 'm') {
Expand Down
8 changes: 7 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ function UInt53 (value) {
Math.floor(value) === value
}

function Path (value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BIP32Path? shrug

It's private anyway, so I'm not worried too much.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for BIP32Path or BIP32DerivePath

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

return typeforce.String(value) &&
value.match(/^([m]\/)?([0-9]+[']?\/)*([0-9]+[']?)$/)
}

// external dependent types
var BigInt = typeforce.quacksLike('BigInteger')
var ECPoint = typeforce.quacksLike('Point')
Expand Down Expand Up @@ -57,7 +62,8 @@ var types = {
UInt8: UInt8,
UInt31: UInt31,
UInt32: UInt32,
UInt53: UInt53
UInt53: UInt53,
Path: Path
}

for (var typeName in typeforce) {
Expand Down
29 changes: 26 additions & 3 deletions test/hdnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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/)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not great, but, its not terrible either. ACK for now with room for improvement haha.
(I have no suggestions yet)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ 'foo', '', 2, ... ].forEach(function (path) { assert.throws(function () { master.derivePath(path) }, /Expected Path/) })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And put those negative fixtures in the json fixtures under invalid: { derivePath: [...] }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Runn1ng if you're time is running short on this (not that there has been any hint of that), I'll see if I can submit a PR to this branch (on your repo) to help out :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All right, I can do that

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/)
})
})
})