@chainsafe/lodestar 1.35.0-dev.8644a83c62 → 1.35.0-dev.8961b06c11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (271) hide show
  1. package/.git-data.json +1 -1
  2. package/bin/lodestar.js +3 -0
  3. package/bin/lodestar.ts +3 -0
  4. package/lib/applyPreset.d.ts.map +1 -0
  5. package/lib/applyPreset.js +1 -1
  6. package/lib/applyPreset.js.map +1 -1
  7. package/lib/cli.d.ts +3 -3
  8. package/lib/cli.d.ts.map +1 -0
  9. package/lib/cli.js +1 -1
  10. package/lib/cli.js.map +1 -1
  11. package/lib/cmds/beacon/handler.d.ts +1 -1
  12. package/lib/cmds/beacon/handler.d.ts.map +1 -0
  13. package/lib/cmds/beacon/handler.js +1 -1
  14. package/lib/cmds/beacon/handler.js.map +1 -1
  15. package/lib/cmds/beacon/index.d.ts.map +1 -0
  16. package/lib/cmds/beacon/initBeaconState.d.ts.map +1 -0
  17. package/lib/cmds/beacon/initBeaconState.js.map +1 -1
  18. package/lib/cmds/beacon/initPeerIdAndEnr.d.ts +2 -2
  19. package/lib/cmds/beacon/initPeerIdAndEnr.d.ts.map +1 -0
  20. package/lib/cmds/beacon/initPeerIdAndEnr.js +1 -1
  21. package/lib/cmds/beacon/initPeerIdAndEnr.js.map +1 -1
  22. package/lib/cmds/beacon/options.d.ts.map +1 -0
  23. package/lib/cmds/beacon/paths.d.ts.map +1 -0
  24. package/lib/cmds/bootnode/handler.d.ts +13 -8
  25. package/lib/cmds/bootnode/handler.d.ts.map +1 -0
  26. package/lib/cmds/bootnode/handler.js +2 -2
  27. package/lib/cmds/bootnode/handler.js.map +1 -1
  28. package/lib/cmds/bootnode/index.d.ts.map +1 -0
  29. package/lib/cmds/bootnode/options.d.ts.map +1 -0
  30. package/lib/cmds/bootnode/options.js +2 -1
  31. package/lib/cmds/bootnode/options.js.map +1 -1
  32. package/lib/cmds/dev/files.d.ts.map +1 -0
  33. package/lib/cmds/dev/handler.d.ts.map +1 -0
  34. package/lib/cmds/dev/handler.js +1 -1
  35. package/lib/cmds/dev/handler.js.map +1 -1
  36. package/lib/cmds/dev/index.d.ts.map +1 -0
  37. package/lib/cmds/dev/options.d.ts.map +1 -0
  38. package/lib/cmds/index.d.ts.map +1 -0
  39. package/lib/cmds/lightclient/handler.d.ts.map +1 -0
  40. package/lib/cmds/lightclient/index.d.ts.map +1 -0
  41. package/lib/cmds/lightclient/options.d.ts.map +1 -0
  42. package/lib/cmds/validator/blsToExecutionChange.d.ts.map +1 -0
  43. package/lib/cmds/validator/blsToExecutionChange.js.map +1 -1
  44. package/lib/cmds/validator/handler.d.ts.map +1 -0
  45. package/lib/cmds/validator/handler.js +3 -5
  46. package/lib/cmds/validator/handler.js.map +1 -1
  47. package/lib/cmds/validator/import.d.ts.map +1 -0
  48. package/lib/cmds/validator/index.d.ts.map +1 -0
  49. package/lib/cmds/validator/keymanager/decryptKeystoreDefinitions.d.ts.map +1 -0
  50. package/lib/cmds/validator/keymanager/decryptKeystores/index.d.ts.map +1 -0
  51. package/lib/cmds/validator/keymanager/decryptKeystores/poolSize.d.ts.map +1 -0
  52. package/lib/cmds/validator/keymanager/decryptKeystores/threadPool.d.ts.map +1 -0
  53. package/lib/cmds/validator/keymanager/decryptKeystores/threadPool.js +4 -1
  54. package/lib/cmds/validator/keymanager/decryptKeystores/threadPool.js.map +1 -1
  55. package/lib/cmds/validator/keymanager/decryptKeystores/types.d.ts.map +1 -0
  56. package/lib/cmds/validator/keymanager/decryptKeystores/worker.d.ts.map +1 -0
  57. package/lib/cmds/validator/keymanager/impl.d.ts.map +1 -0
  58. package/lib/cmds/validator/keymanager/impl.js +4 -0
  59. package/lib/cmds/validator/keymanager/impl.js.map +1 -1
  60. package/lib/cmds/validator/keymanager/interface.d.ts.map +1 -0
  61. package/lib/cmds/validator/keymanager/keystoreCache.d.ts.map +1 -0
  62. package/lib/cmds/validator/keymanager/persistedKeys.d.ts.map +1 -0
  63. package/lib/cmds/validator/keymanager/persistedKeys.js +1 -0
  64. package/lib/cmds/validator/keymanager/persistedKeys.js.map +1 -1
  65. package/lib/cmds/validator/keymanager/server.d.ts.map +1 -0
  66. package/lib/cmds/validator/keymanager/server.js +2 -0
  67. package/lib/cmds/validator/keymanager/server.js.map +1 -1
  68. package/lib/cmds/validator/list.d.ts.map +1 -0
  69. package/lib/cmds/validator/options.d.ts.map +1 -0
  70. package/lib/cmds/validator/options.js +2 -2
  71. package/lib/cmds/validator/paths.d.ts.map +1 -0
  72. package/lib/cmds/validator/signers/importExternalKeystores.d.ts.map +1 -0
  73. package/lib/cmds/validator/signers/index.d.ts.map +1 -0
  74. package/lib/cmds/validator/signers/logSigners.d.ts.map +1 -0
  75. package/lib/cmds/validator/slashingProtection/export.d.ts.map +1 -0
  76. package/lib/cmds/validator/slashingProtection/import.d.ts.map +1 -0
  77. package/lib/cmds/validator/slashingProtection/index.d.ts.map +1 -0
  78. package/lib/cmds/validator/slashingProtection/options.d.ts.map +1 -0
  79. package/lib/cmds/validator/slashingProtection/utils.d.ts.map +1 -0
  80. package/lib/cmds/validator/slashingProtection/utils.js +1 -1
  81. package/lib/cmds/validator/slashingProtection/utils.js.map +1 -1
  82. package/lib/cmds/validator/voluntaryExit.d.ts.map +1 -0
  83. package/lib/cmds/validator/voluntaryExit.js +1 -1
  84. package/lib/cmds/validator/voluntaryExit.js.map +1 -1
  85. package/lib/config/beaconNodeOptions.d.ts.map +1 -0
  86. package/lib/config/beaconNodeOptions.js +2 -1
  87. package/lib/config/beaconNodeOptions.js.map +1 -1
  88. package/lib/config/beaconParams.d.ts.map +1 -0
  89. package/lib/config/index.d.ts.map +1 -0
  90. package/lib/config/peerId.d.ts.map +1 -0
  91. package/lib/config/types.d.ts.map +1 -0
  92. package/lib/index.d.ts.map +1 -0
  93. package/lib/index.js.map +1 -1
  94. package/lib/migrations/index.d.ts +1 -0
  95. package/lib/migrations/index.d.ts.map +1 -0
  96. package/lib/migrations/index.js +1 -1
  97. package/lib/networks/chiado.d.ts.map +1 -0
  98. package/lib/networks/dev.d.ts.map +1 -0
  99. package/lib/networks/ephemery.d.ts.map +1 -0
  100. package/lib/networks/gnosis.d.ts.map +1 -0
  101. package/lib/networks/holesky.d.ts.map +1 -0
  102. package/lib/networks/hoodi.d.ts.map +1 -0
  103. package/lib/networks/index.d.ts.map +1 -0
  104. package/lib/networks/index.js +1 -2
  105. package/lib/networks/index.js.map +1 -1
  106. package/lib/networks/mainnet.d.ts.map +1 -0
  107. package/lib/networks/sepolia.d.ts.map +1 -0
  108. package/lib/options/beaconNodeOptions/api.d.ts.map +1 -0
  109. package/lib/options/beaconNodeOptions/builder.d.ts.map +1 -0
  110. package/lib/options/beaconNodeOptions/chain.d.ts.map +1 -0
  111. package/lib/options/beaconNodeOptions/eth1.d.ts.map +1 -0
  112. package/lib/options/beaconNodeOptions/execution.d.ts.map +1 -0
  113. package/lib/options/beaconNodeOptions/index.d.ts.map +1 -0
  114. package/lib/options/beaconNodeOptions/metrics.d.ts.map +1 -0
  115. package/lib/options/beaconNodeOptions/monitoring.d.ts.map +1 -0
  116. package/lib/options/beaconNodeOptions/network.d.ts +1 -0
  117. package/lib/options/beaconNodeOptions/network.d.ts.map +1 -0
  118. package/lib/options/beaconNodeOptions/network.js +6 -3
  119. package/lib/options/beaconNodeOptions/network.js.map +1 -1
  120. package/lib/options/beaconNodeOptions/sync.d.ts.map +1 -0
  121. package/lib/options/globalOptions.d.ts.map +1 -0
  122. package/lib/options/index.d.ts.map +1 -0
  123. package/lib/options/logOptions.d.ts.map +1 -0
  124. package/lib/options/paramsOptions.d.ts.map +1 -0
  125. package/lib/paths/global.d.ts.map +1 -0
  126. package/lib/paths/rootDir.d.ts.map +1 -0
  127. package/lib/util/errors.d.ts.map +1 -0
  128. package/lib/util/ethers.d.ts.map +1 -0
  129. package/lib/util/feeRecipient.d.ts.map +1 -0
  130. package/lib/util/file.d.ts.map +1 -0
  131. package/lib/util/file.js +1 -1
  132. package/lib/util/file.js.map +1 -1
  133. package/lib/util/format.d.ts.map +1 -0
  134. package/lib/util/fs.d.ts.map +1 -0
  135. package/lib/util/gitData/gitDataPath.d.ts.map +1 -0
  136. package/lib/util/gitData/index.d.ts.map +1 -0
  137. package/lib/util/gitData/index.js.map +1 -1
  138. package/lib/util/gitData/writeGitData.d.ts.map +1 -0
  139. package/lib/util/hasher_bun.d.ts +3 -0
  140. package/lib/util/hasher_bun.d.ts.map +1 -0
  141. package/lib/util/hasher_bun.js +118 -0
  142. package/lib/util/hasher_bun.js.map +1 -0
  143. package/lib/util/hasher_nodejs.d.ts +3 -0
  144. package/lib/util/hasher_nodejs.d.ts.map +1 -0
  145. package/lib/util/hasher_nodejs.js +3 -0
  146. package/lib/util/hasher_nodejs.js.map +1 -0
  147. package/lib/util/index.d.ts +3 -3
  148. package/lib/util/index.d.ts.map +1 -0
  149. package/lib/util/index.js +3 -3
  150. package/lib/util/index.js.map +1 -1
  151. package/lib/util/jwt.d.ts.map +1 -0
  152. package/lib/util/lockfile.d.ts.map +1 -0
  153. package/lib/util/logger.d.ts.map +1 -0
  154. package/lib/util/logger.js +1 -1
  155. package/lib/util/logger.js.map +1 -1
  156. package/lib/util/object.d.ts.map +1 -0
  157. package/lib/util/object.js.map +1 -1
  158. package/lib/util/passphrase.d.ts.map +1 -0
  159. package/lib/util/process.d.ts.map +1 -0
  160. package/lib/util/progress.d.ts.map +1 -0
  161. package/lib/util/proposerConfig.d.ts.map +1 -0
  162. package/lib/util/proposerConfig.js.map +1 -1
  163. package/lib/util/pruneOldFilesInDir.d.ts.map +1 -0
  164. package/lib/util/sleep.d.ts.map +1 -0
  165. package/lib/util/stripOffNewlines.d.ts.map +1 -0
  166. package/lib/util/types.d.ts.map +1 -0
  167. package/lib/util/version.d.ts.map +1 -0
  168. package/package.json +33 -20
  169. package/src/applyPreset.ts +92 -0
  170. package/src/cli.ts +56 -0
  171. package/src/cmds/beacon/handler.ts +267 -0
  172. package/src/cmds/beacon/index.ts +18 -0
  173. package/src/cmds/beacon/initBeaconState.ts +275 -0
  174. package/src/cmds/beacon/initPeerIdAndEnr.ts +199 -0
  175. package/src/cmds/beacon/options.ts +214 -0
  176. package/src/cmds/beacon/paths.ts +62 -0
  177. package/src/cmds/bootnode/handler.ts +203 -0
  178. package/src/cmds/bootnode/index.ts +13 -0
  179. package/src/cmds/bootnode/options.ts +109 -0
  180. package/src/cmds/dev/files.ts +52 -0
  181. package/src/cmds/dev/handler.ts +86 -0
  182. package/src/cmds/dev/index.ts +18 -0
  183. package/src/cmds/dev/options.ts +110 -0
  184. package/src/cmds/index.ts +15 -0
  185. package/src/cmds/lightclient/handler.ts +36 -0
  186. package/src/cmds/lightclient/index.ts +18 -0
  187. package/src/cmds/lightclient/options.ts +21 -0
  188. package/src/cmds/validator/blsToExecutionChange.ts +91 -0
  189. package/src/cmds/validator/handler.ts +300 -0
  190. package/src/cmds/validator/import.ts +111 -0
  191. package/src/cmds/validator/index.ts +28 -0
  192. package/src/cmds/validator/keymanager/decryptKeystoreDefinitions.ts +189 -0
  193. package/src/cmds/validator/keymanager/decryptKeystores/index.ts +1 -0
  194. package/src/cmds/validator/keymanager/decryptKeystores/poolSize.ts +16 -0
  195. package/src/cmds/validator/keymanager/decryptKeystores/threadPool.ts +75 -0
  196. package/src/cmds/validator/keymanager/decryptKeystores/types.ts +12 -0
  197. package/src/cmds/validator/keymanager/decryptKeystores/worker.ts +24 -0
  198. package/src/cmds/validator/keymanager/impl.ts +425 -0
  199. package/src/cmds/validator/keymanager/interface.ts +35 -0
  200. package/src/cmds/validator/keymanager/keystoreCache.ts +91 -0
  201. package/src/cmds/validator/keymanager/persistedKeys.ts +268 -0
  202. package/src/cmds/validator/keymanager/server.ts +86 -0
  203. package/src/cmds/validator/list.ts +35 -0
  204. package/src/cmds/validator/options.ts +461 -0
  205. package/src/cmds/validator/paths.ts +95 -0
  206. package/src/cmds/validator/signers/importExternalKeystores.ts +69 -0
  207. package/src/cmds/validator/signers/index.ts +176 -0
  208. package/src/cmds/validator/signers/logSigners.ts +81 -0
  209. package/src/cmds/validator/slashingProtection/export.ts +110 -0
  210. package/src/cmds/validator/slashingProtection/import.ts +70 -0
  211. package/src/cmds/validator/slashingProtection/index.ts +12 -0
  212. package/src/cmds/validator/slashingProtection/options.ts +15 -0
  213. package/src/cmds/validator/slashingProtection/utils.ts +56 -0
  214. package/src/cmds/validator/voluntaryExit.ts +232 -0
  215. package/src/config/beaconNodeOptions.ts +68 -0
  216. package/src/config/beaconParams.ts +87 -0
  217. package/src/config/index.ts +3 -0
  218. package/src/config/peerId.ts +50 -0
  219. package/src/config/types.ts +3 -0
  220. package/src/index.ts +28 -0
  221. package/src/migrations/index.ts +0 -0
  222. package/src/networks/chiado.ts +20 -0
  223. package/src/networks/dev.ts +27 -0
  224. package/src/networks/ephemery.ts +9 -0
  225. package/src/networks/gnosis.ts +18 -0
  226. package/src/networks/holesky.ts +17 -0
  227. package/src/networks/hoodi.ts +16 -0
  228. package/src/networks/index.ts +236 -0
  229. package/src/networks/mainnet.ts +34 -0
  230. package/src/networks/sepolia.ts +17 -0
  231. package/src/options/beaconNodeOptions/api.ts +110 -0
  232. package/src/options/beaconNodeOptions/builder.ts +63 -0
  233. package/src/options/beaconNodeOptions/chain.ts +326 -0
  234. package/src/options/beaconNodeOptions/eth1.ts +95 -0
  235. package/src/options/beaconNodeOptions/execution.ts +92 -0
  236. package/src/options/beaconNodeOptions/index.ts +50 -0
  237. package/src/options/beaconNodeOptions/metrics.ts +39 -0
  238. package/src/options/beaconNodeOptions/monitoring.ts +61 -0
  239. package/src/options/beaconNodeOptions/network.ts +401 -0
  240. package/src/options/beaconNodeOptions/sync.ts +65 -0
  241. package/src/options/globalOptions.ts +72 -0
  242. package/src/options/index.ts +3 -0
  243. package/src/options/logOptions.ts +70 -0
  244. package/src/options/paramsOptions.ts +72 -0
  245. package/src/paths/global.ts +24 -0
  246. package/src/paths/rootDir.ts +11 -0
  247. package/src/util/errors.ts +20 -0
  248. package/src/util/ethers.ts +44 -0
  249. package/src/util/feeRecipient.ts +6 -0
  250. package/src/util/file.ts +167 -0
  251. package/src/util/format.ts +76 -0
  252. package/src/util/fs.ts +59 -0
  253. package/src/util/gitData/gitDataPath.ts +48 -0
  254. package/src/util/gitData/index.ts +70 -0
  255. package/src/util/gitData/writeGitData.ts +10 -0
  256. package/src/util/hasher_bun.ts +133 -0
  257. package/src/util/hasher_nodejs.ts +3 -0
  258. package/src/util/index.ts +17 -0
  259. package/src/util/jwt.ts +10 -0
  260. package/src/util/lockfile.ts +45 -0
  261. package/src/util/logger.ts +105 -0
  262. package/src/util/object.ts +15 -0
  263. package/src/util/passphrase.ts +25 -0
  264. package/src/util/process.ts +25 -0
  265. package/src/util/progress.ts +58 -0
  266. package/src/util/proposerConfig.ts +136 -0
  267. package/src/util/pruneOldFilesInDir.ts +27 -0
  268. package/src/util/sleep.ts +3 -0
  269. package/src/util/stripOffNewlines.ts +6 -0
  270. package/src/util/types.ts +8 -0
  271. package/src/util/version.ts +74 -0
@@ -0,0 +1,24 @@
1
+ import {GlobalArgs} from "../options/index.js";
2
+ import {getDefaultDataDir} from "./rootDir.js";
3
+
4
+ export type GlobalPaths = {
5
+ dataDir: string;
6
+ };
7
+
8
+ /**
9
+ * Defines the path structure of the globally used files
10
+ *
11
+ * ```bash
12
+ * $dataDir
13
+ * └── $paramsFile
14
+ * ```
15
+ */
16
+ export function getGlobalPaths(args: Partial<GlobalArgs>, network: string): GlobalPaths {
17
+ // Set dataDir to network name iff dataDir is not set explicitly
18
+ const dataDir = args.dataDir || getDefaultDataDir(network);
19
+ return {
20
+ dataDir,
21
+ };
22
+ }
23
+
24
+ export const defaultGlobalPaths = getGlobalPaths({dataDir: "$dataDir"}, "$network");
@@ -0,0 +1,11 @@
1
+ import os from "node:os";
2
+ import path from "node:path";
3
+
4
+ /**
5
+ * Follows XDG Base Directory Specification
6
+ * https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#basics
7
+ */
8
+ export function getDefaultDataDir(network: string): string {
9
+ const dataHome = process.env.XDG_DATA_HOME || path.join(os.homedir(), ".local", "share");
10
+ return path.join(dataHome, "lodestar", network);
11
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Expected error that shouldn't print a stack trace
3
+ */
4
+ export class YargsError extends Error {}
5
+
6
+ export type Result<T> = {err: null; result: T} | {err: Error};
7
+ export async function wrapError<T>(promise: Promise<T>): Promise<Result<T>> {
8
+ try {
9
+ return {err: null, result: await promise};
10
+ } catch (err) {
11
+ return {err: err as Error};
12
+ }
13
+ }
14
+ export function wrapFnError<T>(fn: () => T): Result<T> {
15
+ try {
16
+ return {err: null, result: fn()};
17
+ } catch (err) {
18
+ return {err: err as Error};
19
+ }
20
+ }
@@ -0,0 +1,44 @@
1
+ import fs from "node:fs";
2
+ import {ethers} from "ethers";
3
+
4
+ /**
5
+ * Returns a connected ethers signer from a variety of provider options
6
+ */
7
+ export async function getEthersSigner({
8
+ keystorePath,
9
+ keystorePassword,
10
+ rpcUrl,
11
+ rpcPassword,
12
+ ipcPath,
13
+ chainId,
14
+ }: {
15
+ keystorePath?: string;
16
+ keystorePassword?: string;
17
+ rpcUrl?: string;
18
+ rpcPassword?: string;
19
+ ipcPath?: string;
20
+ chainId: number;
21
+ }): Promise<ethers.Signer> {
22
+ if (keystorePath && keystorePassword) {
23
+ const keystoreJson = fs.readFileSync(keystorePath, "utf8");
24
+ const wallet = await ethers.Wallet.fromEncryptedJson(keystoreJson, keystorePassword);
25
+ const eth1Provider = rpcUrl
26
+ ? new ethers.JsonRpcProvider(rpcUrl)
27
+ : new ethers.InfuraProvider({name: "deposit", chainId});
28
+ return wallet.connect(eth1Provider);
29
+ }
30
+
31
+ if (rpcUrl) {
32
+ const signer = await new ethers.JsonRpcProvider(rpcUrl).getSigner();
33
+ if (rpcPassword) {
34
+ await signer.unlock(rpcPassword);
35
+ }
36
+ return signer;
37
+ }
38
+
39
+ if (ipcPath) {
40
+ return new ethers.IpcSocketProvider(ipcPath).getSigner();
41
+ }
42
+
43
+ throw Error("Must supply either keystorePath, rpcUrl, or ipcPath");
44
+ }
@@ -0,0 +1,6 @@
1
+ export function parseFeeRecipient(feeRecipientHex: string): string {
2
+ if (!/^0x[a-fA-F0-9]{40}$/i.test(feeRecipientHex)) {
3
+ throw Error(`Invalid feeRecipient= ${feeRecipientHex}, expected format: ^0x[a-fA-F0-9]{40}$`);
4
+ }
5
+ return feeRecipientHex.toLowerCase();
6
+ }
@@ -0,0 +1,167 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import {Readable} from "node:stream";
4
+ import stream from "node:stream/promises";
5
+ import {ReadableStream as NodeReadableStream} from "node:stream/web";
6
+ import yaml from "js-yaml";
7
+ import {fetch} from "@lodestar/utils";
8
+
9
+ const {load, dump, FAILSAFE_SCHEMA, Type} = yaml;
10
+
11
+ import {mkdir} from "./fs.js";
12
+
13
+ export const yamlSchema = FAILSAFE_SCHEMA.extend({
14
+ implicit: [
15
+ new Type("tag:yaml.org,2002:str", {
16
+ kind: "scalar",
17
+ construct: function construct(data) {
18
+ return data !== null ? data : "";
19
+ },
20
+ }),
21
+ ],
22
+ });
23
+
24
+ export enum FileFormat {
25
+ json = "json",
26
+ yaml = "yaml",
27
+ yml = "yml",
28
+ toml = "toml",
29
+ }
30
+
31
+ /**
32
+ * Parse file contents as Json.
33
+ */
34
+ export function parse<T>(contents: string, fileFormat: FileFormat): T {
35
+ switch (fileFormat) {
36
+ case FileFormat.json:
37
+ return JSON.parse(contents) as T;
38
+ case FileFormat.yaml:
39
+ case FileFormat.yml:
40
+ return load(contents, {schema: yamlSchema}) as T;
41
+ default:
42
+ return contents as unknown as T;
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Stringify file contents.
48
+ */
49
+ export function stringify(obj: unknown, fileFormat: FileFormat): string {
50
+ let contents: string;
51
+ switch (fileFormat) {
52
+ case FileFormat.json:
53
+ contents = JSON.stringify(obj, null, 2);
54
+ break;
55
+ case FileFormat.yaml:
56
+ case FileFormat.yml:
57
+ contents = dump(obj, {schema: yamlSchema});
58
+ break;
59
+ default:
60
+ contents = obj as string;
61
+ }
62
+ return contents;
63
+ }
64
+
65
+ /**
66
+ * Write a JSON serializable object to a file
67
+ *
68
+ * Serialize either to json, yaml, or toml
69
+ */
70
+ export function writeFile(filepath: string, obj: unknown, options: fs.WriteFileOptions = "utf-8"): void {
71
+ mkdir(path.dirname(filepath));
72
+ const fileFormat = path.extname(filepath).substr(1);
73
+ fs.writeFileSync(filepath, typeof obj === "string" ? obj : stringify(obj, fileFormat as FileFormat), options);
74
+ }
75
+
76
+ /**
77
+ * Create a file with `600 (-rw-------)` permissions
78
+ * *Note*: 600: Owner has full read and write access to the file,
79
+ * while no other user can access the file
80
+ */
81
+ export function writeFile600Perm(filepath: string, obj: unknown, options?: fs.WriteFileOptions): void {
82
+ writeFile(filepath, obj, options);
83
+ fs.chmodSync(filepath, "0600");
84
+ }
85
+
86
+ /**
87
+ * Read a JSON serializable object from a file
88
+ *
89
+ * Parse either from json, yaml, or toml
90
+ * Optional acceptedFormats object can be passed which can be an array of accepted formats, in future can be extended to include parseFn for the accepted formats
91
+ */
92
+ export function readFile<T>(filepath: string, acceptedFormats?: string[]): T {
93
+ const fileFormat = path.extname(filepath).substr(1);
94
+ if (acceptedFormats && !acceptedFormats.includes(fileFormat)) throw new Error(`UnsupportedFileFormat: ${filepath}`);
95
+ const contents = fs.readFileSync(filepath, "utf-8");
96
+ return parse(contents, fileFormat as FileFormat);
97
+ }
98
+
99
+ /**
100
+ * @see readFile
101
+ * If `filepath` does not exist returns null
102
+ */
103
+ export function readFileIfExists<T>(filepath: string, acceptedFormats?: string[]): T | null {
104
+ try {
105
+ return readFile(filepath, acceptedFormats);
106
+ } catch (e) {
107
+ if ((e as {code: string}).code === "ENOENT") {
108
+ return null;
109
+ }
110
+ throw e;
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Download from URL or copy from local filesystem
116
+ * @param urlOrPathSrc "/path/to/file.szz" | "https://url.to/file.szz"
117
+ */
118
+ export async function downloadOrCopyFile(pathDest: string, urlOrPathSrc: string): Promise<void> {
119
+ if (isUrl(urlOrPathSrc)) {
120
+ await downloadFile(pathDest, urlOrPathSrc);
121
+ } else {
122
+ mkdir(path.dirname(pathDest));
123
+ await fs.promises.copyFile(urlOrPathSrc, pathDest);
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Downloads a genesis file per network if it does not exist
129
+ */
130
+ export async function downloadFile(pathDest: string, url: string): Promise<void> {
131
+ if (!fs.existsSync(pathDest)) {
132
+ mkdir(path.dirname(pathDest));
133
+ const res = await fetch(url);
134
+ if (!res.ok) {
135
+ throw new Error(`Failed to download file from ${url}: ${res.status} ${res.statusText}`);
136
+ }
137
+
138
+ if (!res.body) {
139
+ throw new Error("Response body is null");
140
+ }
141
+
142
+ await stream.pipeline(Readable.fromWeb(res.body as unknown as NodeReadableStream), fs.createWriteStream(pathDest));
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Download from URL to memory or load from local filesystem
148
+ * @param urlOrPathSrc "/path/to/file.szz" | "https://url.to/file.szz"
149
+ */
150
+ export async function downloadOrLoadFile(pathOrUrl: string): Promise<Uint8Array> {
151
+ if (isUrl(pathOrUrl)) {
152
+ const res = await fetch(pathOrUrl);
153
+ if (!res.ok) {
154
+ throw new Error(`Failed to download file from ${pathOrUrl}: ${res.status} ${res.statusText}`);
155
+ }
156
+ const arrayBuffer = await res.arrayBuffer();
157
+ return new Uint8Array(arrayBuffer);
158
+ }
159
+ return fs.promises.readFile(pathOrUrl);
160
+ }
161
+
162
+ /**
163
+ * Returns boolean for whether the string is a URL.
164
+ */
165
+ function isUrl(pathOrUrl: string): boolean {
166
+ return pathOrUrl.startsWith("http");
167
+ }
@@ -0,0 +1,76 @@
1
+ import {PublicKey} from "@chainsafe/blst";
2
+ import {fromHex} from "@lodestar/utils";
3
+
4
+ /**
5
+ * 0x prefix a string if not prefixed already
6
+ */
7
+ export function ensure0xPrefix(hex: string): string {
8
+ if (!hex.startsWith("0x")) hex = `0x${hex}`;
9
+ return hex;
10
+ }
11
+
12
+ export function isValidatePubkeyHex(pubkeyHex: string): boolean {
13
+ return /^0x[0-9a-fA-F]{96}$/.test(pubkeyHex);
14
+ }
15
+
16
+ export function getPubkeyHexFromKeystore(keystore: {pubkey?: string}): string {
17
+ if (!keystore.pubkey) {
18
+ throw Error("Invalid keystore, must contain .pubkey property");
19
+ }
20
+
21
+ const pubkeyHex = ensure0xPrefix(keystore.pubkey);
22
+ if (!isValidatePubkeyHex(pubkeyHex)) {
23
+ throw Error(`Invalid keystore pubkey format ${pubkeyHex}`);
24
+ }
25
+
26
+ return pubkeyHex;
27
+ }
28
+
29
+ /**
30
+ * Parse string inclusive range: `0..32`, into an array of all values in that range
31
+ */
32
+ export function parseRange(range: string): number[] {
33
+ if (!range.includes("..")) {
34
+ throw Error(`Invalid range '${range}', must include '..'`);
35
+ }
36
+
37
+ const [from, to] = range.split("..").map((n) => parseInt(n));
38
+
39
+ if (Number.isNaN(from)) throw Error(`Invalid range from isNaN '${range}'`);
40
+ if (Number.isNaN(to)) throw Error(`Invalid range to isNaN '${range}'`);
41
+ if (from > to) throw Error(`Invalid range from > to '${range}'`);
42
+
43
+ const arr: number[] = [];
44
+ for (let i = from; i <= to; i++) {
45
+ arr.push(i);
46
+ }
47
+
48
+ return arr;
49
+ }
50
+
51
+ export function assertValidPubkeysHex(pubkeysHex: string[]): void {
52
+ for (const pubkeyHex of pubkeysHex) {
53
+ const pubkeyBytes = fromHex(pubkeyHex);
54
+ PublicKey.fromBytes(pubkeyBytes, true);
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Parses a file to get a list of bootnodes for a network.
60
+ * Bootnodes file is expected to contain bootnode ENR's concatenated by newlines, or commas for
61
+ * parsing plaintext, YAML, JSON and/or env files.
62
+ */
63
+ export function parseBootnodesFile(bootnodesFile: string): string[] {
64
+ const enrs = [];
65
+ for (const line of bootnodesFile.trim().split(/\r?\n/)) {
66
+ for (const entry of line.split(",")) {
67
+ const sanitizedEntry = entry.replace(/['",[\]{}.]+/g, "").trim();
68
+
69
+ if (sanitizedEntry.includes("enr:-")) {
70
+ const parsedEnr = `enr:-${sanitizedEntry.split("enr:-")[1].split("#")[0].trim()}`;
71
+ enrs.push(parsedEnr);
72
+ }
73
+ }
74
+ }
75
+ return enrs;
76
+ }
package/src/util/fs.ts ADDED
@@ -0,0 +1,59 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+
4
+ /**
5
+ * Maybe create a directory
6
+ */
7
+ export function mkdir(dirname: string): void {
8
+ if (!fs.existsSync(dirname)) fs.mkdirSync(dirname, {recursive: true});
9
+ }
10
+
11
+ /** NodeJS POSIX errors subset */
12
+ type ErrorFs = Error & {code: "ENOENT" | "ENOTDIR"};
13
+
14
+ /**
15
+ * Attempts to unlink a file, return true if it is deleted, false if not found
16
+ */
17
+ export function unlinkSyncMaybe(filepath: string): boolean {
18
+ try {
19
+ fs.unlinkSync(filepath);
20
+ return true;
21
+ } catch (e) {
22
+ const {code} = e as ErrorFs;
23
+ if (code === "ENOENT") return false;
24
+
25
+ throw e;
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Attempts rm a dir, return true if it is deleted, false if not found
31
+ */
32
+ export function rmdirSyncMaybe(dirpath: string): boolean {
33
+ try {
34
+ fs.rmSync(dirpath, {recursive: true});
35
+ return true;
36
+ } catch (e) {
37
+ const {code} = e as ErrorFs;
38
+ // about error codes https://nodejs.org/api/fs.html#fspromisesrmdirpath-options
39
+ // ENOENT error on Windows and an ENOTDIR
40
+ if (code === "ENOENT" || code === "ENOTDIR") return false;
41
+
42
+ throw e;
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Find all files recursively in `dirPath`
48
+ */
49
+ export function recursiveLookup(dirPath: string, filepaths: string[] = []): string[] {
50
+ if (fs.statSync(dirPath).isDirectory()) {
51
+ for (const filename of fs.readdirSync(dirPath)) {
52
+ recursiveLookup(path.join(dirPath, filename), filepaths);
53
+ }
54
+ } else {
55
+ filepaths.push(dirPath);
56
+ }
57
+
58
+ return filepaths;
59
+ }
@@ -0,0 +1,48 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import {fileURLToPath} from "node:url";
4
+
5
+ // Global variable __dirname no longer available in ES6 modules.
6
+ // Solutions: https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules
7
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
8
+
9
+ // Persist git data and distribute through NPM so CLI consumers can know exactly
10
+ // at what commit was this src build. This is used in the metrics and to log initially.
11
+ //
12
+ // - For NPM release (stable): Only the version is persisted. Once must then track the version's tag
13
+ // in Github to resolve that version to a specific commit. While this is okay, git-data.json gives
14
+ // a gurantee of the exact commit at build time.
15
+ //
16
+ // - For NPM release (dev): canary commits include the commit, so this feature is not really
17
+ // necessary. However, it's more cumbersome to have conditional logic on stable / dev.
18
+ //
19
+ // - For build from source: .git folder is available in the context of the built code, so it can extract
20
+ // branch and commit directly without the need for .git-data.json.
21
+ //
22
+ // - For build from source dockerized: This feature is required to know the branch and commit, since
23
+ // git data is not persisted past the build. However, .dockerignore prevents .git folder from being
24
+ // copied into the container's context, so .git-data.json can't be generated.
25
+
26
+ /**
27
+ * WARNING!! If you change this path make sure to update:
28
+ * - 'packages/cli/package.json' -> .files -> `".git-data.json"`
29
+ */
30
+ export const gitDataPath = path.resolve(__dirname, "../../../.git-data.json");
31
+
32
+ /** Git data type used to construct version information string and persistence. */
33
+ export type GitData = {
34
+ /** "developer-feature" */
35
+ branch: string;
36
+ /** "80c248bb392f512cc115d95059e22239a17bbd7d" */
37
+ commit: string;
38
+ };
39
+
40
+ /** Writes a persistent git data file. */
41
+ export function writeGitDataFile(gitData: GitData): void {
42
+ fs.writeFileSync(gitDataPath, JSON.stringify(gitData, null, 2));
43
+ }
44
+
45
+ /** Reads the persistent git data file. */
46
+ export function readGitDataFile(): GitData {
47
+ return JSON.parse(fs.readFileSync(gitDataPath, "utf8")) as GitData;
48
+ }
@@ -0,0 +1,70 @@
1
+ import {execSync} from "node:child_process";
2
+ // This file is created in the build step and is distributed through NPM
3
+ // MUST be in sync with `-/gitDataPath.ts` and `package.json` files.
4
+ import {GitData, readGitDataFile} from "./gitDataPath.js";
5
+
6
+ /** Reads git data from a persisted file or local git data at build time. */
7
+ export function readAndGetGitData(): GitData {
8
+ try {
9
+ // Gets git data containing current branch and commit info from persistent file.
10
+ let persistedGitData: Partial<GitData>;
11
+ try {
12
+ persistedGitData = readGitDataFile();
13
+ } catch (_e) {
14
+ persistedGitData = {};
15
+ }
16
+
17
+ const currentGitData = getGitData();
18
+
19
+ return {
20
+ // If the CLI is run from source, prioritze current git data
21
+ // over `.git-data.json` file, which might be stale here.
22
+ branch:
23
+ currentGitData.branch && currentGitData.branch.length > 0
24
+ ? currentGitData.branch
25
+ : (persistedGitData.branch ?? ""),
26
+ commit:
27
+ currentGitData.commit && currentGitData.commit.length > 0
28
+ ? currentGitData.commit
29
+ : (persistedGitData.commit ?? ""),
30
+ };
31
+ } catch (_e) {
32
+ return {
33
+ branch: "",
34
+ commit: "",
35
+ };
36
+ }
37
+ }
38
+
39
+ /** Gets git data containing current branch and commit info from CLI. */
40
+ export function getGitData(): GitData {
41
+ return {
42
+ branch: process.env.GIT_BRANCH ?? getBranch(),
43
+ commit: process.env.GIT_COMMIT ?? getCommit(),
44
+ };
45
+ }
46
+
47
+ /** Tries to get branch from git CLI. */
48
+ function getBranch(): string {
49
+ try {
50
+ return shellSilent("git rev-parse --abbrev-ref HEAD");
51
+ } catch (_e) {
52
+ return "";
53
+ }
54
+ }
55
+
56
+ /** Tries to get commit from git from git CLI. */
57
+ function getCommit(): string {
58
+ try {
59
+ return shellSilent("git rev-parse --verify HEAD");
60
+ } catch (_e) {
61
+ return "";
62
+ }
63
+ }
64
+
65
+ /** Silent shell that won't pollute stdout, or stderr */
66
+ function shellSilent(cmd: string): string {
67
+ return execSync(cmd, {stdio: ["ignore", "pipe", "ignore"]})
68
+ .toString()
69
+ .trim();
70
+ }
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env node
2
+
3
+ // For RATIONALE of this file, check packages/cli/src/util/gitData/gitDataPath.ts
4
+ // Persist exact commit in NPM distributions for easier tracking of the build
5
+
6
+ import {writeGitDataFile} from "./gitDataPath.js";
7
+ import {getGitData} from "./index.js";
8
+
9
+ // Script to write the git data file (json) used by the build procedures to persist git data.
10
+ writeGitDataFile(getGitData());
@@ -0,0 +1,133 @@
1
+ import {byteArrayIntoHashObject} from "@chainsafe/as-sha256";
2
+ import {
3
+ HashComputationLevel,
4
+ HashObject,
5
+ Hasher,
6
+ Node,
7
+ doDigestNLevel,
8
+ doMerkleizeBlockArray,
9
+ doMerkleizeBlocksBytes,
10
+ } from "@chainsafe/persistent-merkle-tree";
11
+ import {digest2Bytes32, digest2Bytes32Into, hashInto} from "@lodestar/bun";
12
+
13
+ /**
14
+ * Best SIMD implementation is in 512 bits = 64 bytes
15
+ * If not, hashtree will make a loop inside
16
+ * Given sha256 operates on a block of 4 bytes, we can hash 16 inputs at once
17
+ * Each input is 64 bytes
18
+ */
19
+ const PARALLEL_FACTOR = 16;
20
+ const MAX_INPUT_SIZE = PARALLEL_FACTOR * 64;
21
+ const uint8Input = new Uint8Array(MAX_INPUT_SIZE);
22
+ const uint32Input = new Uint32Array(uint8Input.buffer);
23
+ const uint8Output = new Uint8Array(PARALLEL_FACTOR * 32);
24
+ // having this will cause more memory to extract uint32
25
+ // const uint32Output = new Uint32Array(uint8Output.buffer);
26
+ // convenient reusable Uint8Array for hash64
27
+ const hash64Input = uint8Input.subarray(0, 64);
28
+ const hash64Output = uint8Output.subarray(0, 32);
29
+ // size input array to 2 HashObject per computation * 32 bytes per object
30
+ const destNodes: Node[] = new Array<Node>(PARALLEL_FACTOR);
31
+
32
+ export const hasher: Hasher = {
33
+ name: "hashtree-bun",
34
+ hashInto,
35
+ digest64(obj1: Uint8Array, obj2: Uint8Array): Uint8Array {
36
+ return digest2Bytes32(obj1, obj2);
37
+ },
38
+ digest64Into: (obj1: Uint8Array, obj2: Uint8Array, output: Uint8Array): void => {
39
+ digest2Bytes32Into(obj1, obj2, output);
40
+ },
41
+ digest64HashObjects(left: HashObject, right: HashObject, parent: HashObject): void {
42
+ hashObjectsToUint32Array(left, right, uint32Input);
43
+ hashInto(hash64Input, hash64Output);
44
+ byteArrayIntoHashObject(hash64Output, 0, parent);
45
+ },
46
+ merkleizeBlocksBytes(blocksBytes: Uint8Array, padFor: number, output: Uint8Array, offset: number): void {
47
+ doMerkleizeBlocksBytes(blocksBytes, padFor, output, offset, hashInto);
48
+ },
49
+ merkleizeBlockArray(blocks, blockLimit, padFor, output, offset) {
50
+ doMerkleizeBlockArray(blocks, blockLimit, padFor, output, offset, hashInto, uint8Input);
51
+ },
52
+ digestNLevel(data: Uint8Array, nLevel: number): Uint8Array {
53
+ return doDigestNLevel(data, nLevel, hashInto);
54
+ },
55
+ executeHashComputations(hashComputations: HashComputationLevel[]): void {
56
+ for (let level = hashComputations.length - 1; level >= 0; level--) {
57
+ const hcArr = hashComputations[level];
58
+ if (!hcArr) {
59
+ // should not happen
60
+ throw Error(`no hash computations for level ${level}`);
61
+ }
62
+
63
+ if (hcArr.length === 0) {
64
+ // nothing to hash
65
+ continue;
66
+ }
67
+
68
+ // hash every 16 inputs at once to avoid memory allocation
69
+ let i = 0;
70
+ for (const {src0, src1, dest} of hcArr) {
71
+ if (!src0 || !src1 || !dest) {
72
+ throw new Error(`Invalid HashComputation at index ${i}`);
73
+ }
74
+ const indexInBatch = i % PARALLEL_FACTOR;
75
+ const offset = indexInBatch * 16;
76
+
77
+ hashObjectToUint32Array(src0, uint32Input, offset);
78
+ hashObjectToUint32Array(src1, uint32Input, offset + 8);
79
+ destNodes[indexInBatch] = dest;
80
+ if (indexInBatch === PARALLEL_FACTOR - 1) {
81
+ hashInto(uint8Input, uint8Output);
82
+ for (const [j, destNode] of destNodes.entries()) {
83
+ byteArrayIntoHashObject(uint8Output, j * 32, destNode);
84
+ }
85
+ }
86
+ i++;
87
+ }
88
+
89
+ const remaining = hcArr.length % PARALLEL_FACTOR;
90
+ // we prepared data in input, now hash the remaining
91
+ if (remaining > 0) {
92
+ const remainingInput = uint8Input.subarray(0, remaining * 64);
93
+ const remainingOutput = uint8Output.subarray(0, remaining * 32);
94
+ hashInto(remainingInput, remainingOutput);
95
+ // destNodes was prepared above
96
+ for (let j = 0; j < remaining; j++) {
97
+ byteArrayIntoHashObject(remainingOutput, j * 32, destNodes[j]);
98
+ }
99
+ }
100
+ }
101
+ },
102
+ };
103
+
104
+ function hashObjectToUint32Array(obj: HashObject, arr: Uint32Array, offset: number): void {
105
+ arr[offset] = obj.h0;
106
+ arr[offset + 1] = obj.h1;
107
+ arr[offset + 2] = obj.h2;
108
+ arr[offset + 3] = obj.h3;
109
+ arr[offset + 4] = obj.h4;
110
+ arr[offset + 5] = obj.h5;
111
+ arr[offset + 6] = obj.h6;
112
+ arr[offset + 7] = obj.h7;
113
+ }
114
+
115
+ // note that uint32ArrayToHashObject will cause more memory
116
+ function hashObjectsToUint32Array(obj1: HashObject, obj2: HashObject, arr: Uint32Array): void {
117
+ arr[0] = obj1.h0;
118
+ arr[1] = obj1.h1;
119
+ arr[2] = obj1.h2;
120
+ arr[3] = obj1.h3;
121
+ arr[4] = obj1.h4;
122
+ arr[5] = obj1.h5;
123
+ arr[6] = obj1.h6;
124
+ arr[7] = obj1.h7;
125
+ arr[8] = obj2.h0;
126
+ arr[9] = obj2.h1;
127
+ arr[10] = obj2.h2;
128
+ arr[11] = obj2.h3;
129
+ arr[12] = obj2.h4;
130
+ arr[13] = obj2.h5;
131
+ arr[14] = obj2.h6;
132
+ arr[15] = obj2.h7;
133
+ }
@@ -0,0 +1,3 @@
1
+ import {hasher} from "@chainsafe/persistent-merkle-tree/hasher/hashtree";
2
+
3
+ export {hasher};