hd: better path parsing.

This commit is contained in:
Christopher Jeffrey 2017-08-11 05:07:04 -07:00
parent 60b345f59c
commit e6cd60f919
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -52,32 +52,39 @@ common.cache = new LRU(500);
common.parsePath = function parsePath(path, hard) {
assert(typeof path === 'string');
assert(typeof hard === 'boolean');
assert(path.length >= 1);
assert(path.length <= 3062);
const parts = path.split('/');
const root = parts.shift();
const root = parts[0];
if (root !== 'm'
&& root !== 'M'
&& root !== 'm\''
&& root !== 'M\'') {
throw new Error('Bad path root.');
throw new Error('Invalid path root.');
}
const result = [];
for (let part of parts) {
for (let i = 1; i < parts.length; i++) {
let part = parts[i];
const hardened = part[part.length - 1] === '\'';
if (hardened)
part = part.slice(0, -1);
if (part.length > 10)
throw new Error('Path index too large.');
if (!/^\d+$/.test(part))
throw new Error('Non-number path index.');
throw new Error('Path index is non-numeric.');
let index = parseInt(part, 10);
if ((index >>> 0) !== index)
throw new Error('Index out of range.');
throw new Error('Path index out of range.');
if (hardened) {
index |= common.HARDENED;
@ -85,7 +92,7 @@ common.parsePath = function parsePath(path, hard) {
}
if (!hard && (index & common.HARDENED))
throw new Error('Cannot derive hardened.');
throw new Error('Path index cannot be hardened.');
result.push(index);
}