Additional tests for hdValidatePath (#897)

* Additional tests for hdValidatePath

* Additional

* slip for Polkadot
This commit is contained in:
Jaco Greeff
2021-02-26 10:43:55 +01:00
committed by GitHub
parent 2e0b8e4d11
commit 46bfe643a6
3 changed files with 36 additions and 6 deletions
@@ -30,7 +30,7 @@ const TESTS = {
]
},
Polkadot: {
slip44: 0x80000162,
slip44: 0x0162,
tests: [
{
ed25519: '0xe8c68348586d53e4e8d1a864b0e4e17c75e4eb06e0c63c1432bef2ba29e69d41',
@@ -0,0 +1,28 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { hdValidatePath } from '.';
const VECTORS: [string, boolean][] = [
["m/44'/60'/0'/0/0", true],
['m/0', true],
["m/123'", true],
["n/44'/60'/0'/0/0", false],
['m', false],
['m/', false],
["m/xyz'", false],
['m/123x', false],
['m/123"', false],
["m/123''", false],
["m/123'0'", false],
[`m/${0x80000000}`, false],
['m/-1', false]
];
describe('hdValidatePath', (): void => {
VECTORS.forEach(([path, result]): void => {
it(`validates ${path} as ${result.toString()}`, (): void => {
expect(hdValidatePath(path)).toEqual(result);
});
});
});
+7 -5
View File
@@ -8,9 +8,11 @@ export function hdValidatePath (path: string): boolean {
return false;
}
return !path
.split('/')
.slice(1)
.map((n) => parseInt(n.replace("'", ''), 10))
.some((n) => isNaN(n) && n < HARDENED);
const parts = path.split('/').slice(1);
return parts.every((n) => /^\d+'?$/.test(n)) && (
!parts
.map((n) => parseInt(n.replace("'", ''), 10))
.some((n) => isNaN(n) || (n >= HARDENED) || (n < 0))
);
}