Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip]: Exit transaction generation #165

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 74 additions & 11 deletions src/electron/BashUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@
/**
* This BashUtils module provides different file and OS utility functions. Those functions should
* work across all our supported operating systems including Linux, macOS and Windows.
*
*
* @module
*/

import { promisify } from 'util';

import { constants, readdir } from 'fs';
import { constants, readdir, readFile } from 'fs';
import { access, stat } from 'fs/promises';

import path from "path";

import { fileSync } from "tmp";
import { Keystore } from '../react/types';
import { KEYSTORE_FILE_PREFIX } from '../react/constants';

const readdirProm = promisify(readdir);
const readFileProm = promisify(readFile);

/**
* Check for the existence of a file or a directory on the filesystem.
*
*
* @param filename The path to the file or directory.
*
*
* @returns Returns a Promise<boolean> that includes a true value if file or directory exists.
*/
const doesFileExist = async (filename: string): Promise<boolean> => {
Expand All @@ -35,9 +38,9 @@ const doesFileExist = async (filename: string): Promise<boolean> => {

/**
* Check for the existence of a directory on the filesystem.
*
*
* @param directory The path to the directory.
*
*
* @returns Returns a Promise<boolean> that includes a true value if the directory exists.
*/
const doesDirectoryExist = async (directory: string): Promise<boolean> => {
Expand All @@ -49,9 +52,9 @@ const doesDirectoryExist = async (directory: string): Promise<boolean> => {

/**
* Check if we can write a file in a directory.
*
*
* @param directory The path to the directory.
*
*
* @returns Returns true if the directory is writable and if a file can be written in the
* directory. Returns false if not.
*/
Expand All @@ -76,12 +79,36 @@ const isDirectoryWritable = async (directory: string): Promise<boolean> => {
}
}

/**
* Finds all files with whom filename starts with some value in a directory.
*
* @param directory The path to the directory.
* @param startsWith Filename match to look for.
*
* @returns Returns a Promise<string[]> that includes the path to all files that match.
* Returns empty array if none match.
*/
const findAllFiles = async (directory: string, startsWith: string): Promise<string[]> => {
const entries = await readdirProm(directory, { withFileTypes: true });

return entries.reduce((foundFiles: string[], entry) => {
if (entry.isFile() && entry.name.startsWith(startsWith)) {
return [
...foundFiles,
path.join(directory, entry.name),
];
}

return foundFiles;
}, []);
}

/**
* Find the first file whom filename starts with some value in a directory.
*
*
* @param directory The path to the directory.
* @param startsWith Filename match to look for.
*
*
* @returns Returns a Promise<string> that includes the path to the file if found. Returns empty
* string if not found.
*/
Expand All @@ -97,9 +124,45 @@ const findFirstFile = async (directory: string, startsWith: string): Promise<str
return "";
}

/**
* Will read the provided file paths and attempt to convert each one to a Keystore object.
*
* @param filePaths The paths to each file to attempt to convert
*
* @returns Returns a Promise<Keystore[]> that contains metadata on each keystore file.
* Returns empty array if no valid keystore files are found
*/
const readKeystoreInformation = async (filePaths: string[]): Promise<Keystore[]> => {
const keystores: Keystore[] = [];
for (const filePath of filePaths) {
const fileData = await readFileProm(filePath, "utf8");
try {
const parsedKeystore: any = JSON.parse(fileData);
const fileName = filePath.split('/').at(-1) || filePath;
const keystoreIndex = fileName.split(KEYSTORE_FILE_PREFIX).at(-1)?.split('_')[0];
keystores.push({
publicKey: parsedKeystore.pubkey,
shortenedPub: `${parsedKeystore.pubkey.substring(0, 6)}...${parsedKeystore.pubkey.slice(-6)}`,
index: keystoreIndex || "Unknown index",
fileName: fileName, // Used as the unique identifier
fullPath: filePath,
password: "",
validPassword: false,
validatorIndex: "",
})
} catch (e) {
// Failed to parse file
}
}

return keystores;
}

export {
doesFileExist,
doesDirectoryExist,
isDirectoryWritable,
findFirstFile
findAllFiles,
findFirstFile,
readKeystoreInformation,
};
Loading