Validating path
Again through typeforce
This commit is contained in:
parent
182698f53d
commit
d2b43f1dfe
3 changed files with 34 additions and 5 deletions
|
@ -292,7 +292,7 @@ HDNode.prototype.isNeutered = function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
HDNode.prototype.derivePath = function (path) {
|
HDNode.prototype.derivePath = function (path) {
|
||||||
typeforce(types.String, path)
|
typeforce(types.Path, path)
|
||||||
|
|
||||||
var splitPath = path.split('/')
|
var splitPath = path.split('/')
|
||||||
if (splitPath[0] === 'm') {
|
if (splitPath[0] === 'm') {
|
||||||
|
|
|
@ -26,6 +26,11 @@ function UInt53 (value) {
|
||||||
Math.floor(value) === value
|
Math.floor(value) === value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Path (value) {
|
||||||
|
return typeforce.String(value) &&
|
||||||
|
value.match(/^([m]\/)?([0-9]+[']?\/)*([0-9]+[']?)$/)
|
||||||
|
}
|
||||||
|
|
||||||
// external dependent types
|
// external dependent types
|
||||||
var BigInt = typeforce.quacksLike('BigInteger')
|
var BigInt = typeforce.quacksLike('BigInteger')
|
||||||
var ECPoint = typeforce.quacksLike('Point')
|
var ECPoint = typeforce.quacksLike('Point')
|
||||||
|
@ -57,7 +62,8 @@ var types = {
|
||||||
UInt8: UInt8,
|
UInt8: UInt8,
|
||||||
UInt31: UInt31,
|
UInt31: UInt31,
|
||||||
UInt32: UInt32,
|
UInt32: UInt32,
|
||||||
UInt53: UInt53
|
UInt53: UInt53,
|
||||||
|
Path: Path
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var typeName in typeforce) {
|
for (var typeName in typeforce) {
|
||||||
|
|
|
@ -312,8 +312,7 @@ describe('HDNode', function () {
|
||||||
|
|
||||||
var pathSplit = path.split('/').slice(i + 2)
|
var pathSplit = path.split('/').slice(i + 2)
|
||||||
var pathEnd = pathSplit.join('/')
|
var pathEnd = pathSplit.join('/')
|
||||||
var pathEndM = 'm/' + path
|
var pathEndM = 'm/' + pathEnd
|
||||||
|
|
||||||
var child = cn.derivePath(pathEnd)
|
var child = cn.derivePath(pathEnd)
|
||||||
verifyVector(child, cc, pathSplit.length + i + 1)
|
verifyVector(child, cc, pathSplit.length + i + 1)
|
||||||
|
|
||||||
|
@ -390,7 +389,7 @@ describe('HDNode', function () {
|
||||||
}, /Expected UInt32/)
|
}, /Expected UInt32/)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws on non-numbers', function () {
|
it('throws on wrong types', function () {
|
||||||
var f = fixtures.valid[0]
|
var f = fixtures.valid[0]
|
||||||
var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
|
var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
|
||||||
|
|
||||||
|
@ -406,6 +405,30 @@ describe('HDNode', function () {
|
||||||
assert.throws(function () {
|
assert.throws(function () {
|
||||||
master.derive('foo')
|
master.derive('foo')
|
||||||
}, /Expected UInt32/)
|
}, /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/)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue