Files
common/packages/util-crypto/src/key/extractPath.ts
T
Jaco Greeff f07d66562b Remove default exports (#755)
* Remove default exports

* Cleanup tests

* Exports in util

* Single secp256k1 instance

* Length check

* format check

* encoded lengths
2020-11-08 18:07:31 +01:00

38 lines
885 B
TypeScript

// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { assert } from '@polkadot/util';
import { DeriveJunction } from './DeriveJunction';
const RE_JUNCTION = /\/(\/?)([^/]+)/g;
export interface ExtractResult {
parts: null | string[];
path: DeriveJunction[];
}
/**
* @description Extract derivation junctions from the supplied path
*/
export function keyExtractPath (derivePath: string): ExtractResult {
const parts = derivePath.match(RE_JUNCTION);
const path: DeriveJunction[] = [];
let constructed = '';
if (parts) {
constructed = parts.join('');
parts.forEach((value: string): void => {
path.push(DeriveJunction.from(value.substr(1)));
});
}
assert(constructed === derivePath, `Re-constructed path "${constructed}" does not match input`);
return {
parts,
path
};
}