-
Notifications
You must be signed in to change notification settings - Fork 2
/
derivation-paths.ts
68 lines (57 loc) · 1.53 KB
/
derivation-paths.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { DerivationPath } from '../derivation-paths';
export const HARDENED_OFFSET = 0x80000000;
/**
* Get a full derivation path as string.
*
* @param {string} derivationPath
* @param {number} index
* @return {string}
*/
export const getFullPath = (derivationPath: DerivationPath, index: number): string => {
return derivationPath.path.replace('<account>', index.toString(10));
};
/**
* Get the prefix segments from a (non-hardened) derivation path.
*
* @param {string} derivationPath
* @return {string}
*/
export const getPathPrefix = (derivationPath: string): string => {
const segments = derivationPath.split('/').slice(0, -1);
return segments.join('/');
};
/**
* Get the full derivation path as a number array.
*
* @param {DerivationPath} derivationPath
* @return {number[]}
*/
export const toArray = (derivationPath: string): number[] => {
const segments = derivationPath.split('/').slice(1);
return segments.map((segment) => {
const isHardened = segment.endsWith("'");
const index = parseInt(segment, 10);
if (isHardened) {
return HARDENED_OFFSET + index;
}
return index;
});
};
/**
* Get a full derivation path from a number array.
*
* @param {number[]} derivationPath
* @return {string}
*/
export const fromArray = (derivationPath: number[]): string => {
return [
'm',
...derivationPath.map((index) => {
const isHardened = index >= HARDENED_OFFSET;
if (isHardened) {
return `${index - HARDENED_OFFSET}'`;
}
return index;
})
].join('/');
};