-
-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathrecursivelyFind.ts
61 lines (57 loc) · 2.09 KB
/
recursivelyFind.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
import fs from "node:fs";
import path from "node:path";
import {VOTING_KEYSTORE_FILE} from "../validatorDir/paths";
/**
* Find files recursively in `dirPath` whose filename matches a custom function
* @param dirPath
* Return `true` for a given filepath to be included
* @param filenameMatcher
*/
export function recursivelyFind(dirPath: string, filenameMatcher: (filename: string) => boolean): string[] {
let filepaths: string[] = [];
for (const filename of fs.readdirSync(dirPath)) {
const filepath = path.join(dirPath, filename);
if (fs.statSync(filepath).isDirectory()) {
filepaths = filepaths.concat(recursivelyFind(filepath, filenameMatcher));
} else if (filenameMatcher(filename)) {
filepaths.push(filepath);
}
}
return filepaths;
}
/**
* Find voting keystores recursively in `dirPath`
*/
export function recursivelyFindVotingKeystores(dirPath: string): string[] {
return recursivelyFind(dirPath, isVotingKeystore);
}
/**
* Returns `true` if we should consider the `filename` to represent a voting keystore.
*/
export function isVotingKeystore(filename: string): boolean {
// All formats end with `.json`.
return (
filename.endsWith(".json") &&
// Keystores generated by clients
(filename === VOTING_KEYSTORE_FILE ||
// The format exported by the `eth2.0-deposit-cli` library.
//
// Reference to function that generates keystores:
// eslint-disable-next-line max-len
// https://github.com/ethereum/eth2.0-deposit-cli/blob/7cebff15eac299b3b1b090c896dd3410c8463450/eth2deposit/credentials.py#L58-L62
//
// Since we include the key derivation path of `m/12381/3600/x/0/0` this should only ever match
// with a voting keystore and never a withdrawal keystore.
//
// Key derivation path reference:
//
// https://eips.ethereum.org/EIPS/eip-2334
/keystore-m_12381_3600_[0-9]+_0_0-[0-9]+.json/.test(filename))
);
}
/**
* Returns true if filename is a BLS Keystore passphrase file
*/
export function isPassphraseFile(filename: string): boolean {
return /[0-9A-Fa-f]{96}/.test(filename);
}